Select Scripts in Houdini

Select Scripts in Houdini

Here is a nifty little nugget that came in handy while I was working on a scene with tons of geometry. The problem I had was constantly toggling the display of some geometry on and off to work on sections of the scene. Having to look through the network to find the node, turning off the display to do a few modifications, rinse and repeat. That got old rather quick. There are different ways to deal with this in Houdini but Select Scripts in Houdini object nodes offer a quick way to create some custom controls rather easily.

Select Scripts

Houdini object nodes, such as Null objects or Geometry objects, have tucked away in the Misc tab a parameter field called Select Script. This field allows you to insert HScript scripts inline or link to external scripts.

Now, here is the cool part. The reason it’s called Select is because the script in this field gets executed every time you select the object node in the viewport using the Select tool or Pose tools. Read that again. The script gets executed every time you select the object node in the viewport. The script will not run when you select the node in the Network Editor or select the object using some other tool. So if you are frantically selecting your node in the Network Editor and nothing is happening, now you know why.

Let’s do a simple example. Create a Geometry or Null object. You can create any other Object level node. Switch over to the Misc tab and in the Select Script field type the following:

message Waffle and Scrambles

 

Select Script Field
Select Script Field

 

I’m not going to go over HScript basics here but the message command brings up a dialog box with the specified text. In this case, our text is “Waffle and Scrambles”. Once you enter the HScript command, grab your Select tool and select your object in the viewport. When you select the node, a window will pop up with your message. Yay!

 

Running the script
Running the script

 

As you can imagine, if you know HScript you can get creative with this and create some complex operations based on your selections. Here is an example of using the opadd command that creates a camera and an ambient light. Remember you can link to a more elaborate script that sets parameters and gets you a cup of coffee. This is Houdini after all 😉

opadd cam; opadd ambient

 

Using opadd
Using opadd

 

Disabling Objects

Here is a more practical example. We want to be able to turn off the display flag on certain nodes once we select our control object that has the Select script applied. In this case, we are going to call upon the opset command which allows us to turn various operator flags on or off. Have a look at the docs for all the various options you can give the command. The signature for opset is like so:

opset [-q] [flag on|off|toggle|number] ... operators ...

The way the command works is by calling opset, setting one or more flags and listing the operators by name. You can use file matching patterns to grab the nodes you want to affect or add them individually. We are going to use the -d option for the display flag which will control the display of the object on or off in the viewport.

The command offers a handy toggle option for just this sort of thing. Add the following script to the node that will serve as the UI control and type the following in its Select Script field:

opset -d toggle geo*

 

Select Scripts Toggle
Select Scripts Toggle

 

Notice I’m using a wildcard to to pick out all nodes that start with “geo”. I think it’s kind of cool for quick viewport selection tools. It’s especially useful for rigging controls. Again, while these examples are rather trivial, remember you can create more elaborate scripts in an external text file and link to it. All the HScript language is available to you. You can even create the script in Python and run it using the HScript python command. This is just a quick and dirty script that doesn’t take into account error handling and simply creates a directory on the Desktop when the Select Script runs. Very nice!

import os

def createFile():
  os.chdir('C:/Users/HoudiniTricks/Desktop')
  os.mkdir('./HoudiniTricks')

if __name__ == '__main__':
  createFile()

 

HScript python command
HScript python command

 

Python scripts
Python scripts
Share:
990adjustments

990adjustments

I am a motion designer & developer based out of South Florida. When not designing or animating pixels, I wrangle some code. If all else fails, I watch Twilight Zone, I Love Lucy, or Three's Company reruns.