Skip to main content
March 26, 2023
Solved

setProperties error

  • March 26, 2023
  • 4 replies
  • 1022 views

Sometimes when I generate and set new props randomly with API, I’m getting this error.

which is strange because the instance props I want to replace, and new props, are the same 🤔

This topic has been closed for replies.
Best answer by tank666

Thanks for the detailed answer.

The error occurs because your component set is missing variants:

  • Size=small, Radius=on, Rotate=on;
  • Size=medium, Radius=on, Rotate=on;
  • Size=big, Radius=on, Rotate=on.

4 replies

tank666
March 26, 2023

Please add steps to reproduce the bug. Also tell if all required components (variants) exist in the component set? Can you add a link to the test file and sample code?

UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
March 26, 2023

@tank666 sure

  1. Open this file https://www.figma.com/file/2iQtWRfyqEOZo0udal3lqD/Test-file?node-id=0%3A1&t=HZ7z7PAmo0oLygXl-1

  2. Select the Component Awesome instance

  3. Run Scripter plugin

  4. Paste this code

const instanceVariant = figma.currentPage.selection[0] as InstanceNode

const sortProperties = (arr: string[]) => {
  const props = {};
  arr.forEach((str) => {
    str.split(",").forEach((pair) => {
      const [prop, value] = pair.trim().split("=");
      if (!props[prop]) {
        props[prop] = [];
      }
      if (!props[prop].includes(value)) {
        props[prop].push(value);
      }
    });
  });
  Object.keys(props).forEach((key) => {
    props[key].sort();
  });
  return props;
};

function getRandomValues(obj) {
  const result = {};
  for (const prop in obj) {
    const values = obj[prop];
    const randomIndex = Math.floor(Math.random() * values.length);
    result[prop] = values[randomIndex];
  }
  return result;
}

const allVariantsNamesArr = instanceVariant.mainComponent.parent.children.map(
	(child) => child.name
);

const sortedVariants = sortProperties(allVariantsNamesArr)

const randomProps = getRandomValues(sortedVariants)

instanceVariant.setProperties(randomProps)

// console.log(randomProps)
  1. Start to click on Run button

some “runs” will be successful, but some not.

tank666
tank666Solution
March 26, 2023

Thanks for the detailed answer.

The error occurs because your component set is missing variants:

  • Size=small, Radius=on, Rotate=on;
  • Size=medium, Radius=on, Rotate=on;
  • Size=big, Radius=on, Rotate=on.
UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
March 26, 2023

You’re right, I didn’t think about that 🫠 Thanks for your help!