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
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
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?
@tank666 sure
Open this file https://www.figma.com/file/2iQtWRfyqEOZo0udal3lqD/Test-file?node-id=0%3A1&t=HZ7z7PAmo0oLygXl-1
Select the Component Awesome
instance
Run Scripter
plugin
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)
Run
buttonsome “runs” will be successful, but some not.
Thanks for the detailed answer.
The error occurs because your component set is missing variants:
You’re right, I didn’t think about that Thanks for your help!