Skip to main content

Hey community! 
I am currently trying a simple operation of bulk-changing a property of the first subcomponent in a master component A. The master component has many variants always containing 1 instance of other components, which are built up with the same “architecture”. (Need to do this because when selecting them, the exposed nested values are not available on the right). 

My code always throws the error that its failing to set the PROPERTY. 

It’s weird since in the console I can see that all the values are available, it just struggles to assign it?
Perhaps I am missing something trivial, since I barely code anymore...

 

const NEW_VALUE = "NEWVAL"; // Set your target value

const selection = figma.currentPage.selection.filter(node => node.type === "INSTANCE");

if (selection.length === 0) {
figma.notify("Select instances of the master component A.");
figma.closePlugin();
} else {
selection.forEach(instance => {
if (instance.type === "INSTANCE") {
// Find the first nested instance inside this master instance
const nestedInstance = instance.findOne(node => node.type === "INSTANCE");

if (nestedInstance && "setProperties" in nestedInstance) {
const properties = nestedInstance.componentProperties;

if (properties && "TYPE" in properties) {
// Locate the master component for this nested instance
const masterComponent = nestedInstance.mainComponent;

if (masterComponent && masterComponent.parent?.type === "COMPONENT_SET") {
const componentSet = masterComponent.parent;
const availableValues = componentSet.variantGroupPropertiesP"TYPE"]?.values || v];

console.log(`Available "TYPE" values:`, availableValues);
console.log(`Current "TYPE" value:`, propertiesp"TYPE"]);

if (availableValues.includes(NEW_VALUE)) {
try {
nestedInstance.setProperties({ Type: NEW_VALUE });
console.log(`Updated "TYPE" to ${NEW_VALUE}`);
} catch (error) {
console.error(`Failed to set "TYPE" to ${NEW_VALUE}:`, error);
}
} else {
console.warn(`"${NEW_VALUE}" is not a valid option for "TYPE".`);
}
} else {
console.warn("Nested instance does not belong to a variant set.");
}
} else {
console.warn(`No "TYPE" property found in`, properties);
}
}
}
});

figma.notify("Type property update attempted.");
figma.closePlugin();
}

 

Hi ​@Sebastian_Holzki , currently facing the same issue and it drives me crazy 🙂 Setting some variants works, others raise the same error you encountered. Have you found any solution yet?


Hi ​@Sebastian_Holzki,

found a “solution” for my case. The problem was that the component had a different variants, and not all variant combinations were available. Thus, when trying to set the variants in a specific order, it did throw the error.

Example: You have a component for table cells with following properties:

  • Alignment: Left (default), Right
  • Content: Text (default),  Number, Link, Button

Now say the component does not support right-aligned buttons because the underlying design system does not allow this. Thus, there is no variant where both alignment = right AND content = button.

I haven’t 100% figured out yet how/why this happens, but when I first set alignment and then content, it works (figma will simply fallback to the left alignment variant). If however I first set content and then try to set alignment, it will tell me that it’s “unable to find variant properties with these values”.

Hope this helps.

 


Reply