Python State crashes when changing state to View State

   1663   6   3
User Avatar
Member
10 posts
Joined: Jan. 2015
Offline
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:

import hou
import viewerstate.utils as su

class State(object):
    def __init__(self, state_name, scene_viewer):
        self.state_name = state_name
        self.scene_viewer = scene_viewer

    def onStopSelection(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 modification

    def onSelection(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.
        return False

    def onStartSelection(self, kwargs):
        """ Called when a bound selector has been started
        """
        selector_name = kwargs["name"]
        self.log(selector_name + " has started")


    def onExit(self,kwargs):
        """ Called when the state terminates
        """
        state_parms = kwargs["state_parms"]
        print('onExit')


def createViewerStateTemplate():
    """ 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 enters
    template.bindGeometrySelector("SOP: Select a primitive", quick_select=True, name="My Primitive Selector")


    return template

How do I change states from within state?
User Avatar
Member
143 posts
Joined: May 2017
Offline
Hi Tomek P,

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:
...
    def onKeyEvent(self, kwargs):
        ui_event = kwargs['ui_event']
        if(ui_event.device().keyString() == "Enter"):
            self.scene_viewer.setCurrentState("curve::2.0")

This [www.sidefx.com] might be interesting.
User Avatar
Member
10 posts
Joined: Jan. 2015
Offline
Thanks. I'll give it a shot.
User Avatar
Staff
401 posts
Joined: Feb. 2018
Offline
Can you can open a bug for this one please ? Not tested but a possible workaround is to delay the call to enterViewState() like so:

import hdefereval
hdefereval.executeDeferred(deferEnterViewState, self.scene_viewer)

def deferEnterViewState(scene_viewer):
   scene_viewer.enterViewState()
User Avatar
Member
240 posts
Joined: Oct. 2014
Offline
Speaking of hdefereval, is this documented somewhere? I'm trying to find more info on this.
- Tim Crowson
Technical/CG Supervisor
User Avatar
Member
240 posts
Joined: Oct. 2014
Offline
vik_lc
@Tim
Hi,
in the definition are some information, otherwise in the documentation under Event Handling [www.sidefx.com], if not already seen.


Yes thank you, I saw that but it doesn't have much information.
- Tim Crowson
Technical/CG Supervisor
User Avatar
Staff
401 posts
Joined: Feb. 2018
Offline
Tim Crowson
Speaking of hdefereval, is this documented somewhere? I'm trying to find more info on this.

There is no detailed documentation per se, you can find some documentation in the custom menu section [www.sidefx.com]

You can also look in the python module code to get more details.
$HH/python3.9libs/hdefereval.py
  • Quick Links