I would like to run a simple script to bind a variable to the existing style. This is what I tried
// get a style ref
let style = figma.getLocalPaintStyles()[0];
// get a var ref
let variable = figma.variables.getVariableById("VariableID:1:53");
// bind?
figma.variables.setBoundVariableForPaint(style.paints[0], 'color', variable);
It returns a solid paint indeed, however, it has no effect on a given style. Confusing…
Page 1 / 1
Alright, seem like it does not change the pain itself but returns a new one.
variables.forEach(variable => {
const name = variable.name;
let style = getStyleByName(name);
if(style) {
let newPaints = style.paints.map(paint => {
return figma.variables.setBoundVariableForPaint(paint, 'color', variable);
});
style.paints = newPaints;
}
});
Very confusing API indeed. I think there’s an issue with your code, afaik you can’t update paints of existing styles, which has been a shortcoming I haven’t been able to overcome and I think is not possible (no updateLocalStyles for instance only create). The line style.paints = newPaints; shouldn’t work I believe, although confusingly it doesn’t give a warning when using TypeScript, the type is typed as readonly Painto].
Update:
To my surprise it’s possible to update paints by reassigning, I wish I tried this approach before! I was able to create the styles to variables functionality similar to Styles-to-Variables-Converter.
throws a ts error for colorVar in setBoundVariableForPaint. Any ideas as to why? The Variable is getting fetched from a variable file that is available to the file 😕
Your colorVar isn’t variable yet, it’s an object that you can feed into setValueForMode(modeId: string, newValue: uVariableValue])
And then do as usual
I’m trying to apply these new color variables by key when their name matches a currently used fillStyle.
So I want to push something like this in that colorVar property?
Ultimately I’m trying to swap FillStyles with Color Variables is the goal. Seems like setBoundVariableForPaint is the obvious way to do that but I’m not 100% clear on the types of the properties in that method. Like what goes is the third param expecting here?