Useful basic python commands for Houdini

An easy way to get started with python scripting in Houdini is to use the hou.session() module stored in the .hip file itself. This you can use by simply opening the python source editor under the windows tab and checking that it’s written: “hou.session” on the top part of the window.


Start a new python function by writing:

def my_function():

# start your function here

Save the function by pressing apply. To run the function, open Python Shell from the windows tab and type:

hou.session.my_function()

Press enter and Houdini will run the function.


Some useful basic commands:

  • get any node to a variable:

    node = hou.node(“/obj/geo1/box1”)

  • get this nodes any parameter to a variable: 

    parameter = node.parm(“scale”)

    • to find out nodes any parameter name for python scripting, hover over the parameter name with mouse and there will be a pop-up explaining the parameters name that can be used for python scripting.

  • get this parameters value: 

    value = parameter.eval()

  • set this parameter to any value:

    new_value = 2.34

    parameter.set(new_value)

  • press any button with python

    hou.node(“/obj/node/node”).parm(“parameter_name”).pressButton()


All of the sub-modules, classes, and functions to access in Houdini.

https://www.sidefx.com/docs/houdini/hom/hou/index.html


Example of a basic python script in Houdini. This script creates and saves randomly scaled boxes. Paste this script in the Python source editor and run the function in the Python shell.

def create_random_boxes(): import random #import random module for randomizing parameters obj = hou.node("/obj") #store obj level path to variable obj geo1 = obj.createNode("geo", "geo1") #create geometry object node geo1 box1 = geo1.createNode("box", "box1") #create box node named box1 inside geo1 rop1 = geo1.createNode("rop_geometry", "rop1") #create rop geometry node named rop1 inside geo1 rop1.setInput(0,box1,0) #wire rop1 first input stream to box1 first output stream scale = box1.parm("scale") #set box1 scale parameter to variable rop1_filename = rop1.parm("sopoutput")#set rop1 file name parameter to variable rop1_export = rop1.parm("execute")#set rop1 export button to variable file_prefix = "$HIP/geo/box" #set prefix for dynamic file naming amount_of_boxes = 12 #set iteration amount to variable for i in range(0, amount_of_boxes, 1): rand_value = random.random() #sets random value from 0 to 1 scale.set(rand_value) #set box1 scale parameter to rand_value a = str(i).rjust(3,'0') #create string variable from counting variable i and add three zeros of padding final_path = file_prefix + "_" + a + ".abc" #create dynamic file path rop1_filename.set(final_path) #set rop1 file path to the dynamic file path rop1_export.pressButton() #press rop1 export button
Previous
Previous

For-loop VEX vs Python

Next
Next

Replace hairs that share root position (VEX)