Typing numbers into parameters is sometimes a poor way to decide a position or an area.
Clicking and dragging directly in the viewport is far easier. Houdini's "viewer state" is what you use for that.
What a viewer state is
It is a Python class that Houdini runs when you select a node and press Enter in the viewport.
It receives mouse and key input, draws guide shapes and shows prompts. In my repo, the state that makes a node operable is called a PVS (Python Viewer State).
Who does what, C++ or Python
The C++ side has no viewport code at all.
C++ defines the parameters and cooks from them, nothing more. The viewport interaction reaches the node by the Python side reading and writing those parameters.
The only link is a name.
The state file is {NodeName}State.py, and the state's name equals the node type's name. Houdini automatically calls createViewerStateTemplate() from the viewer_states/ folder.
def createViewerStateTemplate():
template = hou.ViewerStateTemplate(
STATE_NAME, "UE Collision Multi", hou.sopNodeTypeCategory()
)
template.bindFactory(UECollisionPVSState)
template.bindIcon("SOP_collision")
template.bindNodeChangeEvent(
[hou.nodeEventType.ParmTupleChanged,
hou.nodeEventType.InputDataChanged])
return templateData passes only through parameters.
For example, Python writes the numbers of the selected faces into a string parameter, and C++ reads it as a list of numbers. Because the parameters are the single source of truth, nothing is lost when you save and reopen.
What you can actually do
UE Collision Multi (UE_Collision_PVS) lets you click mesh faces to select them and commit each selection as one convex-hull collision set.
Left click toggles a face, Shift adds, Enter commits, Delete removes a committed set and Esc cancels the selection. If the mouse moves 5.0 pixels or more, it is not counted as a click.
PRIME Blueprint draws rectangles and circles in the viewport, and you change width, depth, height and radius with arrow and cube handles.
Shift plus scroll switches the shape type (rectangle, circle, curve).
A shared handle kit
Handles are not built per node. They come from shared parts (Common/Viewer/hdk_viewer/handles/).
It holds discs, height, ramps, scale, tilt and more, and the rule is to import them, not copy them.
The interaction rules are also uniform.
Ctrl plus click places (drag sets the radius), click selects, a cyan diamond moves, Delete removes and Esc cancels. Only the handles relevant to the current operation are shown, and rows that do not apply become a dim disc.
What I learned and banned
The causes of broken interactions are written straight into a "Forbidden" section of the rules.
- SideFX handles: to move multiparm items, do not use
bindViewerHandleor the translate manipulators. Use a cyan diamond of fixed pixel size. - Coordinates: a node can be transformed, so convert hits from
ui_event.ray()between world and node space. - Heightfields: do not call
geo.intersecton the volume. Clamp XZ to the bounding box and then callVolume.sample. - Drawing: rebuild drawables only when something changed or on interaction, not on every
onDraw. - Undo: wrap edits in
hou.undos.group(...)so they undo as one step.
Summary
The repo has 92 *State.py files.
A node you can operate is far easier to use than one you type numbers into. The rule is a single one: if you build a SOP that moves several things in the viewport, build its state in the same change. One reference sample (TEMP_Template) is fixed, and you copy it.
Next, as a wrap-up of everything so far, I will write about what was good about the HDK.
Comments