Why does font.selectedLayers return a layer if none are selected?

IMO, this is a selected layer:

but this is not:

however, font.selectedLayers[0] will still be populated even if there is only a cursor in front of it

the reason I’m asking is this makes it difficult to infer the intention of the user when doing batch operations on “selected” glyphs. for example, let’s say I select some glyphs in the font view, but I want to see what happens to them after I run a script: I will double click them all to open in the editor view. glyph.selected remains True for all those glyphs. now, lets say, in the editor view, I want to select a smaller subset, or add a glyph. so I say, ok, if layers are selected in the editor, then those are the glyphs to look at instead, and if none are, then we’ll look at where glyph.selected is true.

consider this code:

def getSelected(font):
	if font.selectedLayers[0]:
		return [l.parent for l in font.selectedLayers]
	else:
		return [g for g in font.glyphs if g.selected]
print(getSelected(Glyphs.font))

hopefully it is clear why I’m asking! and maybe there is a simple method for this that I am not aware of. but to me, I think the problem is that, if you have a cursor in front of a layer in the editor view, it says it is selected, when, in my opinion, it is not

The logic simply is that the layer with the cursor before it is selected.

Apply a filter, anything, to the layer with the cursor in front of it and you will find that it applies to this glyph (and always has). Maybe I am misunderdtanding, but why would you want to list only the glyphs currently highlighted by the cursor?

1 Like

Font.selectedLayers depends on the current tab. glyph.selected is always the selection in the font view. So don’t mix the two.

If you need to distinguish the two cases in the screenshot, you can check for the actual text selection range in the edit view: font.currentTab.textRange In the first case, it will be 1, in the second it will be zero.

1 Like

no misunderstanding, you got it right — the use case is imperfect input :sweat_smile: sometimes I like to do batch operations in the font view, sometimes in the layer view, sometimes I make the incorrect assumption I can just highlight a few glyphs that I’ve just brought into the layer view and run a script, but whoops, my script was only paying attention to glyph.selected. or whoops, my script was paying attention to selectedLayers. I’m aiming for logic where the layers override the glyphs when I highlight text in the editor view.

I agree in theory! I shouldn’t mix them. in practice I accidentally do it all the time :woozy_face:

perfect! that did it. thank you both :blush: