Can I toggle layer visibility in script?



  • Can I toggle layer visibility in script? It seems possible via the setDisplayOption method. By following this with getDisplayOption for each layer, it seems like it should be working. However, my open glyph windows don't update.

    I think maybe I need to use changed(), or something similar?

    # Hide other layers for all open fonts
    
    currentLayerName = CurrentLayer().name
    
    for f in AllFonts():
        for layer in f.layers:
            if layer.name != currentLayerName:
                
                layer.setDisplayOption('all', False)
                
                # do I need something like this?
                layer.changed()
    
    

    Any help is appreciated. Thanks!


  • admin

    this should work:

    currentLayerName = CurrentLayer().name
    
    for f in AllFonts():
        for layer in f.layers:
            if layer.name != currentLayerName:
                
                layer.setDisplayOption('Fill', False) 
                layer.setDisplayOption('Stroke', True)
                layer.setDisplayOption('Points', True)
                layer.setDisplayOption('Coordinates', False) 
                layer.setDisplayOption('all', True)
    
                # do I need something like this?
                layer.changed()
    

    There is an ambiguity in the method names and will change it in the next updates:

    • getDisplayOption returns now a dict
    • setDisplayOption requires a key, value (this must actually be a dict with multiple values...)




  • @frederik said in Can I toggle layer visibility in script?:

    layer.setDisplayOption('Fill', False)
    layer.setDisplayOption('Stroke', True)
    layer.setDisplayOption('Points', True)
    layer.setDisplayOption('Coordinates', False)
    layer.setDisplayOption('all', True)

    Thanks, @frederik! Unfortunately, this doesn't seem to be working for me just yet.

    Just to clarify, my overall problem is this: I have 12+ masters I want to edit at the same time. Sometimes, I want to view background layers, to see prior iterations, etc. Other times (like right now), I want to hide those layers so they don't distract me as I make the foreground layer compatible. I don't want to go through every one of the masters and click off the layers, because that would be really tedious, and something I'd likely have to do quite a few times in the future.

    310ae26a-ff4c-4ec2-852f-e854df2c9195-image.png

    Is there some reason that the layer display options wouldn't be working to hide non-foreground layers? Do I need to use something like layer.changed() in my script, to make the settings take effect?


    Note: I'm in Version 3.3b (build 1906031450), if that makes a difference.



  • I've been able to slightly modify Andy's script to work for what I need.

    I've made it toggle background points along with strokes, across all open fonts.

    Thanks for pointing me to it, Connor! It's even better having it tied to a hotkey.

    (Overall, I think maybe my main problem before was missing f.getLayer(layerName) in front of setDisplayOption.)

    
    from mojo.UI import SetCurrentLayerByName
    from mojo.events import addObserver, removeObserver
                         
    class ShowHideLayers():
        
        """
        Startup Script:
            Assigns the "h" key to show/hide layer outlines across all open fonts.
            -- Modified from https://github.com/andyclymer/RoboFont-StartupScripts
        """
    
        def __init__(self):
            addObserver(self, "keyDown", "keyDown")
            
        def showHideOption(self, displayOptionKey, currentDisplayOptionValue):
            af = AllFonts()
            for f in af:
                for layerName in f.layerOrder:
                    if not layerName == "foreground":
                        f.getLayer(layerName).setDisplayOption(displayOptionKey, not currentDisplayOptionValue)
    
        def keyDown(self, info):
            event = info["event"]
            characters = event.characters()
            modifierFlags = event.modifierFlags()
            if characters == "h":
                f = CurrentFont()
                if len(f.layerOrder) > 1:
                    nextLayer = f.layerOrder[1]
                    # Get the current display status for the first non-foreground layer
                    currentDisplayOption = f.getLayer(nextLayer).getDisplayOption()["Stroke"]
                    # show/hide stroke and points
                    self.showHideOption("Stroke", currentDisplayOption)
                    self.showHideOption("Points", currentDisplayOption)
                    
                    
    ShowHideLayers()
    

  • admin

    and there is a silly typo in the toggling the all option... will be fixed in the next update



  • Hi,

    Just checking in on this thread to note for posterity that "all" works again when setting the Display Option:

    f = CurrentFont()
    for layer in f.layers:
        layer.setDisplayOption("all", False)
    

    I'm on Beta Version 3.3b (build 1910021654)


Log in to reply