Python Resources

Updated on Aug 4 2024

PythonInHoudini

Deborah R. Fowler



Python in Houdini

Updated Oct 6  2013
Updated Aug 20  2019
Updated June 26  2022 - adding Paul Ambrosiussen's Python In Houdini overview video from Feb 28, 2022
Updated Aug 4  2024


Examples


Also on https://www.youtube.com/watch?v=qP1Jc_9As1c&t=55s

Here I will outline the where (getting the code into Houdini) and the how (hou syntax). In addition, creating a shelf tool, an overview of some syntax and how to create a command line gui in python are given below.
This provides an
overview to get you started, please see the above Examples as well.

In Houdini there are many ways to run python code and Houdini is very python friendly. One of the easiest is to create a Python sop. Two other common ways are using the python shell and python source editor (for those of you coming from a mel coding background this one will be most familiar):


External Commands in Houdini

A less frequently used case would be to execute external commands from inside Houdini python scripts. I have created an example which uses both Windows command cmd as well as hcmd from Houdini's bin to demonstrate the syntax. I have then put it into a "Make your own" format as described above.
Example hipnc here. This uses "subprocess" management in python .


Shelf tool

You can add a tool to an existing shell by simply right clicking on the shelf icon area and selecting "New Tool". Probably best to make it in its own shelf tab.

Left click on the + shelf icon and select "New Shelf". Give it a name and label and click accept. Now under this new shelf tab, right click and select "New Tool". Click on the "New Tool" and select the Script tab. You can now enter your code here in Python (alternatively in hscript). This is now accessible from the shelf.


Creating nodes - these will work from a python shell script or in a python source window

Suppose you want to create some geometry, here is a quick example demonstrating some useful commands:

n = hou.node( 'obj' ).createNode( 'geo ')
mergethem = n.createNode('merge')
aSphere = n.createNode('sphere')
mergethem.setInput(0,aSphere)
tx = aSphere.parm('tx')
tx.set(10)
mergethem.setDisplayFlag(1)

Note that n, mergethem and aSphere are simply variables. In the first line, the hou module, node is the function (method) and 'obj' is the argument to that function. n.path() would give us obj/geo for example. We are then setting a sphere 10 units on the screen. This will give you a sphere node connected to a merge node with the display flag on the merge - so on the scene you will see a sphere at 10 units on the x-axis. For more functions/commands refer to the documentation.

MISSING IMAGE

If you wanted to get rid of a node the command for this would be:

someVariableName = hou.node('obj/geo1')
someVariableName.destroy()

MISSING
                IMAGE

Phyllotactic example using Python Source Window

A version with top level controls using the addSpareParmTuple command which was made clearer thanks to an odforce posting (see below). 


Simple GUI in Houdini

See the example file above (final .py file). Odforce posting clarifying addSpareParmTuple command is here
An example of adding a parameter to a node would be (angle is a parameter to my function and geo is set to a node by using hou.node):

angleTemplate = hou.FloatParmTemplate( "angle", "angle", 1, default_value=([angle]), min=0, max=360, min_is_strict=False, max_is_strict=False, naming_scheme=hou.parmNamingScheme.Base1, disable_when="", tags={})

geo.addSpareParmTuple( angleTemplate, in_folder=(["Phyllotaxis Pattern"]), create_missing_folders=True)

More recently (Houdini 19.5.303), I tested this in a python node in obj context - you can see the code in exampleCreatePythonParmViaScript.hiplc