Recreating Node Hierarchies with opscript in Houdini

Recreating Node Hierarchies with opscript in Houdini

Since the beginning of time, Maya users have had the ability to echo all the commands that are executed by the application within the script editor. If you recall, Houdini provides similar functionality through the commandecho HScript command. That’s great for when you are just starting to place down nodes. However, what happens when you already have a node hierarchy done and would like to recreate the network with a script? Maybe you want to recreate the network at a future date, pass on the script to friends and family members at cocktail parties, or use with other pipeline scripts? Well, you are in luck because this is where the opscript command comes in. Recreating node hierarchies with opscript in Houdini is really simple as you will soon see.

Using opscript

The opscript command prints the commands necessary to recreate one operator or recursively apply to the entire operator hierarchy. So if you have an object level node and run it through opscript, you will get back a print out of the commands needed to recreate that particular node or node hierarchy. The command takes several optional flags that let you include or omit information in the generated script. For example, the -g flag allows you to pass in a name argument when sourcing the script.

Let’s look at how to use the command. Create a geometry object of some type and open up a Textport. Type in opscript followed by the path to the node. You can just drag the node into the Textport window.

Using opscript
Using opscript

 

You should see the commands that recreate the operator printed to the Textport. If you inspect the output code, it’s basically a bunch of commands that set the different properties and parameters of the node. For example, have a look at one of the first commands such as oplocate. This HScript command gets or sets the x and y coordinates of the operator tiles in the network view. Add enough of these commands and you get your whole script. If you copy all the commands, paste them back into the Textport and hit enter, you will recreate the operator. Very nice!

Recreate operator
Recreate operator

 

One thing you may be asking  yourself is that the output only seems to be giving us the code for this object level operator. What happened to the hierarchy within? In order to grab all the nodes within your top level operator, you would use the -r flag. This flag dives into the operator and generates the commands to recreate not only the top level object but the the entire operator hierarchy within. I’m not going to go over all the flags but check out the docs for the list of options you have. I have found -r to be the most useful so far. Remember, you can combine the flags.

Writing Out the Script

Now, while it’s nice to see the  script in the Textport, how do we work with the output? We could just copy the output and paste it to a text document. That can work. There is an easier and faster way however.

If you’re familiar with Shell scripting then the concept of standard input and output redirection should immediately pop a light bulb over your head. If you are not, that is fine. The shell and many UNIX commands take their input through a process known as standard input (stdin). The output after a command runs is known as standard output (stdout).  So, stdin could be in the form of user input from a basic program or from a command line argument such as the operator we feed opscript. The output we get back from opscript, which is usually to the terminal window, is the standard output.

Without getting into this too deeply, your output could go to almost anywhere. You can take the standard output and redirect it to a file for instance. This is done with the greater alligator sign (>). Ok, fine, greater than symbol. Boring…

Since we want to save the output of our command to a script file on disk, we would type the following code in the Textport and if everything worked, you should see your script file on disk.

opscript -r /obj/sphere_object1 > $HIP/myscript.cmd
opscript redirection
opscript redirection

 

HScript file
HScript file

 

Reading In the Script

So now that you have your script, how do you run it? Again, rather simple. We use the cmdread command. All cmdread does is run the commands in a specified file. It has a couple of flags such as suppressing warnings in your script and giving you a verbose print out as the commands are executed. Let’s try it out.

cmdread command
cmdread command

 

The great thing about this is you can go in and modify the script. I actually modified the values for the x & y coordinates of the node so it wouldn’t be placed right on top of the original. Notice the original node is renamed. On production scripts you probably want to use something like the -g flag and pass in the name for the top-level node being recreated to avoid any name clashes.

There you have it. Another nice way to create scripts. Another benefit of using opscript is it gives you the internal names of parameters and operators for use in HScript or Python scripts. Have fun!

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.