Why does this script break my colorfont?

One thing that annoyed me is how I can’t add layers and set the ColorPalette values to multiple Glyphs at once, so I thought I’d write a script to do so.

def addColorLayers(glyph):
	for c in [3, 1, 2, 0, 4, 1]:
		layer = GSLayer()
		layer.attributes['colorPalette'] = c
for myFont in Glyphs.fonts:
	for myGlyph in myFont.glyphs:
		if(len(myGlyph.layers) == 1):

This causes existing colored glyphs to break along with their component glyphs. Is there something I’m missing, do I need to set an ID or something?

The script looks find to me.

I would not run a script like that for all open fonts. And setting the masterID makes it a bit more reusable.

palettes = [3, 1, 2, 0, 4, 1]
masterID = Font.masters[0].id

def addColorLayers(glyph, palettes, masterID):
	for c in palettes:
		layer = GSLayer()
		layer.associatedMasterId = masterID
		layer.attributes['colorPalette'] = c

for myGlyph in Font.glyphs:
	if(len(myGlyph.layers) == 1):
		addColorLayers(myGlyph, palettes, masterID)

Thanks, I tried it, but for some reason it still breaks my colorfont. The regular BW version works fine, but when I open the color preview by selecting a color layer I get the “empty base glyph” error message on every glyph after running the script.

It might be that for glyphs that use components, you added the color layers to the component glyph and those will be used now, but they are empty. I would run the script only on selected glyphs. That way you can be sure to not apply it in unneeded places.