Better way to select a created anchor?

This:

anchors = []
		for a in l.anchors:
			if a.name == "bottom":
				dupe = a.copy()
				dupe.name = "cedilla"
				dupe.selected = True # anchor not in the glyph yet, causes error
				anchors.append(dupe)
			anchors.append(a)
		l.anchors = anchors

Will fail with:

Traceback (most recent call last):
File “”, line 26
File “GlyphsApp/GlyphsApp/init.py”, line 5972, in
File “GlyphsApp/GlyphsApp/init.py”, line 3855, in SetObjectInLayer_selected
AttributeError: ‘NoneType’ object has no attribute ‘selection’

One can remove the dupe.selected = True and do a second loop, after the anchors have been added, e.g.

for a in l.anchors:
	if a.name == "cedilla":
		a.selected = True

And this works. But is there a better way to write this? Specifically setting .selected on something that’s not yet in the glyph?

What’s wrong with

layer.anchors['cedilla'].selected = True
1 Like
bottom = l.anchors["bottom"]
if bottom is not None:
	cedilla = bottom.copy()
	cedilla.name = "cedilla"
	l.anchors.append(cedilla)
	cedilla.selected = True
1 Like

Ah, append already pushes it into the right context, good :+1:

Thanks! Didn’t know you can access anchors by their name like that :man_facepalming: