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()