How to use removeCallback?

I’m working on a script with a basic UI, and am trying to use Glyphs callbacks for DOCUMENTOPENED, DOCUMENTCLOSED, and DOCUMENTACTIVATED.

Using the basic export callback demo, I can add my custom callback function for DOCUMENTOPENED. But I cannot remove the callback function with .removeCallback(). I’ve tried both of these options:

Glyphs.removeCallback( myCustomCallback )
Glyphs.removeCallback( myCustomCallback,  DOCUMENTOPENED)

Neither seem to work—my callback function continues to run when a new doc opens. I’ve even tried del myCustomCallback to remove my callback function entirely, but it continues to run when a doc opens!

I’m assuming that it’s important to have a removeCallback for any addCallback. Is that true? How critical is it? If my script hits an error/exception and stops working, will Glyphs have any trouble with a callback I added? Please describe how I should add and remove callbacks responsibly. Thanks!

@mekkablue

After researching how event observers and callbacks work in Robofont (which I also have to support for my script), I’ve been able to get this working in Glyphs too. The Glyphs docs don’t mention that removeCallback will only work when used in the same script “run” as the addCallback (I’m not sure “run” is the right term, technically). I had been experimenting with callbacks in the Macro Panel, by running my script with addCallback, then, later, changing it to removeCallback and running the script again. Unlike most things in the Glyphs API, that doesn’t work. addCallback and removeCallback need to be in the same script.

This means that, practically speaking, callbacks are only useful in a class—like I have for a Vanilla window UI. This is a demo of the callbacks working:

# While this little window is open,
# my callback will run when a document opens

from vanilla import Window

class workingCallbackDemo(object):

	def __init__(self):
		self.w = Window((200, 200))
		
		Glyphs.addCallback( self.myCustomCallback, DOCUMENTOPENED )
		
		# Need to bind a method/function to when the window closes,
		# to remove the event callback first
		self.w.bind('close', self.prepareToCloseWindow)

		self.w.open()
		
	def myCustomCallback(self, info):
		openFonts = Glyphs.fonts
		print( 'There are %d fonts open' % len(openFonts) )
		
	def prepareToCloseWindow(self, sender):
		Glyphs.removeCallback( self.myCustomCallback, DOCUMENTOPENED )

workingCallbackDemo()

I wish the documentation mentioned any of this. @mekkablue, can you add some mention of this? I don’t understand the technical side of this well enough to know how to describe it for the documentation.

@GeorgSeifert I also noticed that when the DOCUMENTCLOSED event happens, Glyphs still includes the closed document in Glyphs.fonts. To test that, just use the demo I just posted above, and change the callback to DOCUMENTCLOSED.

Closing the only open document will print “There are 1 fonts open”. Is this a bug? If not, is there any way I can figure out which documents are open when one closes? This is critical to my script.