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.
How to switch multiple viewer states in HDA?
1338 6 2-
- alexmajewski
- Member
- 99 posts
- Joined: 8月 2017
- オンライン
-
- ubietyworld
- Member
- 20 posts
- Joined: 11月 2022
- オフライン
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.
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
- Member
- 99 posts
- Joined: 8月 2017
- オンライン
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.
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
- スタッフ
- 450 posts
- Joined: 2月 2018
- オフライン
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
Edited by mabelzile - 2025年7月30日 14:07:52
-
- raincole
- Member
- 687 posts
- Joined: 8月 2019
- オフライン
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
- Member
- 99 posts
- Joined: 8月 2017
- オンライン
-
- elbriga14
- Member
- 7 posts
- Joined: 9月 2023
- オフライン
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 usingBut 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.
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")
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.
Edited by elbriga14 - 2025年8月28日 09:37:08
-
- Quick Links