How can i append a component to a layer?

i want to create a script that can put two glyphs together,
so i’m going to copy one glyph and add it to another layer
but i don’t know how to make it.
plz help me!

You like to add components, not the copy of the shapes?
That is much easier:

newGlyph = GSGlyph("uni4E00_comma")
font.glyphs.append(newGlyph)
newLayer = newGlyph.layers[0]
newLayer.shapes.append(GSComponent("uni4E00"))
newLayer.shapes.append(GSComponent("comma"))

Specially line 4 in your script is dangerous. It will return something completely different then you think. It is a list of shapes, most likely not from the comma glyph but from whatever glyph you have selected (that might be the comma for now but relying on the selection in the edit or font is not good. And you don’t need to get the comma glyph to get there. Use the targetFont directly. Are you trying to get to the master that is selected in the UI?

tab = targetFont.currentTab
masterIdx = tab.masterIndex
master = targetFont.masters[masterIdx]
glyph = targetFont.glyphs["comma"]
layer = glyph.layers[master.id]

And if you like to add the shapes (and not a component), do it like this:

glyphA = font.glyphs["A"]
glyphB = font.glyphs["B"]
layerA = glyphA.layers[0] # you might need to iterate all masters if you have more than one.
layerB = glyphB.layers[0]
for shape in layerA.shapes:
    layerB.shapes.append(shape.copy())

or

import copy
newShapes = copy.deepcopy(layerA.shapes)
layerB.shapes.extend(newShapes)
1 Like

thanks for your reply, that helps me a lot

One more question.
Actually I want to copy the whole glyph and add the comma as component to create a new glyph. like this


But the kerning would be different when I did it in the way you showed me.
i tried to create a new glyph manually, and i founded it was not same as master.

i think i made it!
is it right?
截屏2021-08-28 下午5.52.10

I would copy the original glyph but make a new one and add both shapes as components. You just need to copy the width

new_glyph = GSGlyph("uni4E00_comma")
Font.glyphs.append(new_glyph)
glyph_4E00 = Font.glyphs["uni4E00"]
new_glyph.layers[0].width = glyph_4E00.layers[0].width

(and next time, copy paste the code here instead of a screenshot)