Skip to main content
Solved

How do you set an effect style from within a plugin?

  • October 6, 2022
  • 8 replies
  • 1203 views

Dylan_Faust

Hey there,

I’m making an “icon setup” script for my artist, and trying to automate some repetitive steps they need to do every time. I’m wondering, is there a way to set the effect style via code? I’ve tried grabbing the effectStyleId of my desired style, and then assigning it via code, however it appears to become unlinked when I do this. Any ideas?

Best answer by tank666

And if you want to get the EffectStyle from the node it’s applied to, you can use this code:

figma.getStyleById(node.effectStyleId);
View original

8 replies

tank666
  • 4854 replies
  • October 6, 2022

What do you mean by “unlinked”?


Dylan_Faust

Maybe I should say “detached”. It’s as if I’ve clicked this icon in the image. All of the effects on my layers become simple effects, rather than correctly referencing the effect style.

Screen Shot 2022-10-06 at 5.00.15 AM

Screen Shot 2022-10-06 at 5.01.32 AM


Dylan_Faust

Ah apologies, with my prior issue I was simply assigning the wrong style ID. I’ve gotten it working by basically duplicating both the effects and the effect style ID from an example icon onto my “target” icons. I’m still curious if there’s a way to load the effect in more dynamically, like how you can load in library components by their key… I suppose if nothing else, I could attach the style to a library component, and load that in dynamically.


tank666
  • 4854 replies
  • October 6, 2022

For styles from the library, use figma.importStyleByKeyAsync(key).


Dylan_Faust

Ah yes, I did see that, but I’m not sure how you actually get the key for the style? I see the EffectStyle type has a key parameter… but I’m not sure how to actually get my hands on the EffectStyle to extract that. The SceneNode has an effects field, and an effectStyleId, neither of which are an EffectStyle. Is there some lookup I’m overlooking, to get the EffectStyle by effectStyleId, so I can extract that key?


tank666
  • 4854 replies
  • October 6, 2022

To get style keys from a library file, you need to have access to that file. That is, open the library file and use the .getLocalEffectStyle() method to get the EffectStyle. Or use the REST API.

You then need to store those keys somewhere so you can use the .importStyleByKeyAsync(key) method.
See a similar thread:


Dylan_Faust

Amazing, this is exactly what I was looking for. Thanks so much for the info!


tank666
  • 4854 replies
  • Answer
  • October 6, 2022

And if you want to get the EffectStyle from the node it’s applied to, you can use this code:

figma.getStyleById(node.effectStyleId);

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