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

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?

What do you mean by “unlinked”?

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

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.

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

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?

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:

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

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

figma.getStyleById(node.effectStyleId);
1 Like