[4004] Adding a glyph to a font adds additional layers

I want to build a glyph with all layers before adding it to the font, then finally add it. After running the script, there’s an additional layer called “(empty)” in the glyph.

from GlyphsApp import Glyphs, GSFont, GSGlyph, GSLayer

f = GSFont()
Glyphs.fonts.append(f)
g = GSGlyph(name="Hello")
l = GSLayer()
l.associatedMasterId = f.masters[0].id
print(len(g.layers))
g.layers.append(l)
print(len(g.layers))
f.glyphs.append(g)
print(len(g.layers))

Do I have to add the glyphs to the font before doing anything else, so that the expected number of layers are created?

A new layer has a random layer ID. Adding it to the glyph will add a layer “copy” without a name. What you like to do is this:

from GlyphsApp import Glyphs, GSFont, GSGlyph, GSLayer

font = GSFont()
master = font.masters[0]
master.addDefaultMetrics() # to make sure there are any metrics. 
glyph = GSGlyph(name="Hello")
layer = GSLayer()
layer.associatedMasterId = master.id
glyph.layers[master.id] = layer
font.glyphs.append(glyph)
Glyphs.fonts.append(font)

or

from GlyphsApp import Glyphs, GSFont, GSGlyph

font = GSFont()
master = font.masters[0]
master.addDefaultMetrics() # to make sure there are any metrics. 
glyph = GSGlyph(name="Hello")
font.glyphs.append(glyph)
layer = glyph.layers[master.id]
print(layer)
Glyphs.fonts.append(font)
1 Like