Running a python script when changing the viewport

Is it possible to trigger a python script by changing viewports?

Hi Fwk

I think it is, never implemented it myself, but searching here I found:

However maybe viewportchanges are not that obvious to subscribe to:

HTH
-Willem

Thank you for the response. I am not having any problem that I am trying to solve, Wanted to auto update the layout views, which I have many in my document. I have assigned a named position (exploded view) to each layout, and I have a script that does just that, each time I switch to that layout, i will run the script to restore the position associated with that layout. I was curious if I can run the script when I switch layouts. And I will try my best to digest the information you send. Still new in this,

If it’s only to detect a change of active view you can use the below.

Note that if you run the script it will add the watcher to the active Rhino session.
Now each time you change a view it will run the method do_something_for_this_view()
That is were you run your code to restore the position associated with that layout.

If you run the script again it will remove the watcher.

Does this make sense?

################################################################################
# SampleEventHandler.py
# Copyright (c) 2018 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
#
# striped to anly act on activeview change bij Willem Derks 2021 06 10
################################################################################
import Rhino
import scriptcontext as sc



def do_something_for_this_view():
    active_view = sc.doc.Views.ActiveView
    name = active_view.ActiveViewport.Name
    print 'changed to view "{}"'.format(name)

################################################################################
# SampleEventHandler class
################################################################################
class ViewChangeEventHandler():
    
    # Initializer
    def __init__(self):
        # Enable the event handlers
        Rhino.Display.RhinoView.SetActive += self.on_setactiveview
        self.Enabled = True
    
    # Disables the event handlers
    def Disable(self):
        Rhino.Display.RhinoView.SetActive -= self.on_setactiveview
        self.Enabled = False
    
    # Returns the enabled state
    def IsEnabled(self):
        return self.Enabled
    
    # AddRhinoObject event handler
    def on_setactiveview(self, sender, e):
        do_something_for_this_view()

        
################################################################################
# toggle_viewchange_eventhandler function
################################################################################
def toggle_viewchange_eventhandler():
    # See if we already have a handler
    if sc.sticky.has_key('viewchange_eventhandler'):
        # Delete the handler
        handler = sc.sticky.pop('viewchange_eventhandler', None)
        if handler:
            handler.Disable()
            handler = None
            print ' ViewChangeEventHandler disabled'  
    else:
        # Create handler
        handler = ViewChangeEventHandler()
        # Add the handler to the sticky dictionary so it
        # survives when the main function ends.
        sc.sticky['viewchange_eventhandler'] = handler       
        print ' ViewChangeEventHandler enabled'  
    
################################################################################
# Check to see if this file is being executed as the 'main' python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
################################################################################
if __name__ == '__main__':
    toggle_viewchange_eventhandler()    

Yes, it is working. :smiley:

1 Like