Special layers scripting

I’m trying to make a simple script that tells me if a layer is present in a glyph:

for glyph in Font.glyphs:
	if glyph.selected:
		for layer in glyph.layers:
			if layer.name == "Light":
				print(layer.name)

This works fine for normal layers, but doesn’t intermediate layers like {400, 100}, so this doesn’t work:

for glyph in Font.glyphs:
	if glyph.selected:
		for layer in glyph.layers:
			if layer.name == "{400, 100}":
				print(layer.name)

although I copy the name from the printed layer output. Is there a special naming scheme for these?

Is this in Glyphs 2, or Glyphs 3?

Glyphs 3

As you will see if you run

for layer in Layer.parent.layers:
    print(layer.name)

the layer name is not “Light”, but “{400, 100}”. What exactly are you trying to achieve with your script?

If you are trying to find out whether an intermediate layer exists for a specific parent master, you can check the layer’s associatedMasterId.

Side note, I would personally iterate over Font.selectedLayers and then iterate over layer.parent.layers.

Sorry, my question was unclear, I edited it. If I change the name of the layer in the script to the layer that I know is in the glyph, it’s not printed. From the first script I know that it should work. So I wonder if the special layer name that I enter is wrong. I’m doing this to check how I can access that layer as a basis for a later script.

Can I ask what exactly you are trying to accomplish with your script? It’s generally not a good idea to use names as references. What exactly are you trying to achieve?

On yet another side note, the approach still works for me, a special layer name like {400, 100} is successfully recognised when I check against it.

Check for spaces in the layer name; I’ve been there :smiley:

OMG, finally! There is a tab in the special layers’ names :man_facepalming:

Thank you both! @SCarewe I was writing a long-winded reply how it still doesn’t work, but the culprint was the tab. I just have a file with a bunch of special layers in the wrong coordinates. Not all are wrong and not all glyphs have them. So I would rename or delete the wrong ones. I always print before something baaad happens, hence this code.

1 Like

I would not rely on the names. Please the layer.attributes for the actual data that defines the special layers.

Or can not, by the way? If there was a random tab in the layer name, it’s probably not a special layer in the first place, because names don’t define axes anymore, do they

Yes. The name are only user labels and might have special characters or change the format at any time.