Skip to main content
Question

"Unable to find variant" using setProperties while the value is definitely there


Sebastian_Holzki

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.variantGroupProperties["TYPE"]?.values || [];

            console.log(`Available "TYPE" values:`, availableValues);
            console.log(`Current "TYPE" value:`, properties["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();
}

 

0 replies

Be the first to reply!

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings