Search - User list
Full Version: How to switch multiple viewer states in HDA?
Root » Technical Discussion » How to switch multiple viewer states in HDA?
alexmajewski
I'm learning Python viewer states and I'm trying to find out if and how can I switch between states.
Let's say we have 3 wildly different modes/states in our tool, and we'd like to have three separate state classes that we toggle with hotkeys.

Is that possible? I'm specifically asking about keeping the logic of those modes/states in separate sections of code. Three separate __init__(), onEnter(), onMouseEvent(), etc. in a single HDA.
ubietyworld
Maybe you figured this out by now, but the way I'm approaching it is to have a single python state with multiple modes, and then have a hidden int parameter on the HDA that tracks which mode the python state should launch into when activated.

So let's say we have a parameter on the HDA called stateTracker that can either be 0 or 1 or 2, and those values are associated with python state mode A, B, or C.

At that point you can just have a callback script that first sets the parameter to whichever value is associated with the state you want to launch into, and then launches into a single python state that reads the parameter state to decide which UI and functionality to load into the viewport.
alexmajewski
Having a single viewer state class is what I was initially hoping to avoid, because this solution can get visually messy if we make a bunch of switch statements (for state logic) in one long class definition.

In this case, I think I'm going to try to split my states/modes into separate files, each of them having the exact same functions, and have my viewer state class just execute their code depending on the currently chosen state/mode. My motivation for starting this thread is to find a way of keeping it clean and easy to maintain.
mabelzile
Take a look at this HDA. It contains multiple nodes, each using their specific state with a script for switching states. You always need to select the proper node to enter a state, unless your state is nodeless, which I don't think is the goal here.

Image Not Found
raincole
mabelzile
Take a look at this HDA. It contains multiple nodes, each using their specific state with a script for switching states. You always need to select the proper node to enter a state, unless your state is nodeless, which I don't think is the goal here.

Image Not Found


It throws some errors:

ERROR: C:/Users/_/houdini20.5/config/Dialogs/20.5.654/SOP/MultiViewerState (2253): Variable "HS" not defined.

ERROR: C:/Users/_/houdini20.5/config/Dialogs/20.5.654/SOP/MultiViewerState (2253): Syntax error: syntax error, unexpected end of file

alexmajewski
@raincole I also got some errors but I think it's fine, in the end it does properly demonstrate switching states. Does it not?

I haven't had a moment to fully explore it, just took a quick glance, but I just wanted to say thank you @mabelzile!
elbriga14
This discussion was a big help for me, however it doesn't completely solve the problem in my case.
I've tried several approaches with @mabelzile 's script as a basis, but in my case the hda is distributed to our team, and the defintion is locked by SVN.
Therefore, it is not possible to edit the node's definition using
hda_def.sections()["DefaultState"].setContents("m_custom_state")
But I can't seem to correctly enter any state if it is not specified in the "Default state" entry of the root node.

The extra complication in my case is that I need users to select the root.hda, and either enter the state "kinefx_rigpose" for a RigPose node inside, or other custom states defined in python. Therefore, even nesting several python states in one big state class would not be a complete solution.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB