Inconsistent layer.name reporting in foreground() loop

I’m coding a reporter plugin and I want it only active when an actual layer (a master, or one listed in the sidebar panel) is edited. My check that disables the plugin for the background layers is to see if layer.name is of type None.

While this works most of the times, sometimes the script logs the layer.name of the background as the active layer’s name. So when I edit the “Regular” layer it will report “Regular” as expected, and when I switch to the background it will report the expression “layer.name is None” as True, as expected. Only, sometimes the layer.name is still “Regular” (or whatever other was the active layer before switching to the background) when editing the background.
I seen there is the layer.background object, but can it be used to somehow check what is active?

Is this inconsistency a bug? And is there a better way to detect that currently the background is being edited?

Edit: Just a note: I have open only a single font. And also with different fonts this happens.

The name is not a good way to determine if it is a background layer or not. I would check for the className:

if layer.className() == "GSLayer":

# or 

if layer.className() == "GSBackgroundLayer":

Cheers, I knew the string name wasn’t the best idea, just didn’t know better :slight_smile:

Your suggestion works, I’ll see if this eliminates the same occasional erratic reporting, but it most likely does.

Any idea though as to why it would give a wrong name with the layer.name at times and just reports the layer.name to be the same as the “main” layer’s?

Can you send me the glyph where this happens?

I tried to produce an explicit test case, but this seems to happen at random either to do with closing and reopening Glyphs or closing and reopening a glyph file, I couldn’t quite determine which.

I tested with the file I first noticed this. Also tested with a copy of that file with all glyphs removed but one. And I tested with a fresh new file without anything except 3 masters set up. On each three randomly the layer.name is reported as the name of the original editing layer or of type “None” - and switching back between editing and background this remains consistent for as long as the file / glyphs is open - but might be consistently different another time the file / glyphs is opened.

The code in my reporter plugin to test is:

def foreground(self, layer):
    self.logToConsole("Layer name '%s' and class '%s'" % (str(layer.name), str(layer.className())))

This will for the same file and same glyph produce once (first editing a glyph, then switching to background):

Layer name 'Bold' and class 'GSLayer'
Layer name 'None' and class 'GSBackgroundLayer'

and reopening the file / glyphs another time it might be:

Layer name 'Bold' and class 'GSLayer'
Layer name 'Bold' and class 'GSBackgroundLayer'

This happened literally for an empty new file with only 3 masters set up, on Version 2.4.2 (1026). I put the sample code (not isolated from the rest of the WIP plugin, but with that lone above console trace) on github.

I have fixed this.

1 Like