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
			newlayer.name = component.name
			newlayer.components.append(component.copy())
			layers.append(newlayer)

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)

1 Like

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
layer.components.append(GSComponent('dieresis'))

# 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':
                del(layer.components[i])
                break

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

# copy one component to another layer
layer.components.append(anotherlayer.component[0].copy())

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
thanks

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
			newlayer.name = layers[i].components[j].name
			# print(component.name)
			layers.append(newlayer)
			# layer.components.remove(component)
			# newcomopent = GSComponent(component.name)
			newlayer.components.append(layers[i].components[j].copy())
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.)