Layer.copy() or components.append(component.copy())can not work

my code:

layers = Glyphs.font.glyphs['uni4F11'].layers
for layer in layers:
	if layer.components:
		for component in layer.components:
			newlayer = layer.copy()
			newlayer.width = layer.width =

only add code: components.copy() or newlayer.components.append(component.copy()), glyphs doesn’t respond, a red ICON is spinning. other codes work ok.

glyphs:2.6.4 (1286)

You should not add stuff to a list that you iterating over.

What are you trying to do? Adding a layer for each component?

thanks reply:
i want to copy or move a layer’s component to another layer.

demo code:

layer = Glyphs.font.selectedLayers[0] # current layer

# add component

# add component at specific position
layer.components.append(GSComponent('dieresis', NSPoint(100, 100)))

# delete specific component
for i, component in enumerate(layer.components):
        if component.componentName == 'dieresis':

# copy components from another layer
import copy
layer.components = copy.copy(anotherlayer.components)

# copy one component to another layer

i use smartcomponents. one layer only has one smartcomponent.
so i want to move smartcomponents in one layer to several layers.
how to do? 100000+ layers, so i want to do by scripting

i have resoved.

for i in range(len(layers)):
	if layers[i].components:
		for j in range(len(layers[i].components)):
			# newlayer = copy.copy(layer)
			newlayer = GSLayer()
			# print(newlayer.layerId,layer.layerId)
			newlayer.width = layers[i].width = layers[i].components[j].name
			# print(
			# layer.components.remove(component)
			# newcomopent = GSComponent(
1 Like

(When you post code, add three “back-ticks” or grave (```) before and after the code. I reformatted you code to make it readable.)