SOLVED selection observer



  • Is there a way to observe for changes in selection of points and components?
    Tools have a selection object, but how to access it outside a tool?



  • But then I have to handle all the glyph changes somehow, like this:

    from mojo.events import addObserver, removeObserver, postEvent
    
    
    class DummyController(object):
        
        def __init__(self):
            self.glyph = CurrentGlyph()
            addObserver(self, "currentGlyphChanged", "currentGlyphChanged")
            self.addSelectionObserver()
        
        def currentGlyphChanged(self, notification):
            self.removeSelectionObserver()
            self.glyph = notification["glyph"]
            self.addSelectionObserver()
            postEvent("selectionChanged")
        
        def glyphSelectionChanged(self, notification):
            postEvent("selectionChanged")
        
        def addSelectionObserver(self):
            if self.glyph is not None:
                self.glyph.addObserver(self, "glyphSelectionChanged", "Glyph.SelectionChanged")
        
        def removeSelectionObserver(self):
            if self.glyph is not None:
                self.glyph.removeObserver(self, "Glyph.SelectionChanged")
    
        
    class DummyObserver():
        
        def __init__(self):
            addObserver(self, "selectionChanged", "selectionChanged")
        
        def selectionChanged(self, notification):
            self.glyph = notification["glyph"]
            if self.glyph is not None:
                print(self.glyph.selectedPoints, self.glyph.selectedComponents)
    
    
    DummyController()
    DummyObserver()
    

    I was thinking there might be something like this already built in.


  • admin

    there is a way to observe selection changes:

    
    class DummyController(object):
        
        def __init__(self):
            self.glyph = CurrentGlyph()
            self.glyph.addObserver(self, "myCallback", "Glyph.SelectionChanged")
            print("done")
        
        def myCallback(self, notification):
            print("Selection Changed")
            
        
    DummyController()
    

    don’t forget the removeObserver when you are done...


Log in to reply