Hi I have a very simple state. Generally, it's just a preset 'Selection' state with one modification. After accepting selection with Enter key, I'd like to change state to View state. So I've added one line in onStopSelection method. Unfortunately, it crashes Houdini. Here's the code:
importhouimportviewerstate.utilsassuclassState(object):def__init__(self,state_name,scene_viewer):self.state_name=state_nameself.scene_viewer=scene_viewerdefonStopSelection(self,kwargs):""" Called when a bound selector has been terminated """selector_name=kwargs["name"]self.log(selector_name+" has stopped")self.scene_viewer.enterViewState()# my modificationdefonSelection(self,kwargs):""" Called when a selector has selected something """selection=kwargs["selection"]self.log(selection)# Return False to keep the selector active or True to stop it.returnFalsedefonStartSelection(self,kwargs):""" Called when a bound selector has been started """selector_name=kwargs["name"]self.log(selector_name+" has started")defonExit(self,kwargs):""" Called when the state terminates """state_parms=kwargs["state_parms"]print('onExit')defcreateViewerStateTemplate():""" Mandatory entry point to create and return the viewer state template to register. """state_typename=kwargs["type"].definition().sections()["DefaultState"].contents()state_label="Tomek::dev::testingstate::1.0"state_cat=hou.sopNodeTypeCategory()template=hou.ViewerStateTemplate(state_typename,state_label,state_cat)template.bindFactory(State)template.bindIcon(kwargs["type"].icon())# The selector will start when the state enterstemplate.bindGeometrySelector("SOP: Select a primitive",quick_select=True,name="My Primitive Selector")returntemplate
I can't tell you exactly why the error occurs - but I can at least confirm it. And as it is in your example, you are trying to enter a state you are already in. Even if you would succeed in entering another state with the onStopSelection event, you would not have the selection available in the new state anymore.
To enter a new viewer state from an existing you can do something like this: