Monday, January 18, 2010

Houdini Python parameter expressions

When scripting in Houdini, there are two languages that you can use. The old scripting language is called hscript, and the new one (gradually replacing hscript) is Python. When scripting in Python, there are many different places where you can insert Python scripts - the two most important are:
  • Parameter expressions: these are Python scripts inside the parameter input fields. You can right click on an input filed, and select Expressions > Edit Expressions. You need to make sure that your scripting language is set to Python. These scripts are saved as part of you Houdini model, in you .hip (or .hipnc) file.
  • Python operators: these are custom nodes implemented from scratch in Python. These are saved in .otl files, and can be used the same way as the built in nodes.

In yesterdays workshop, we focused on parameter expressions, and we wrote some expressions to find the average of a set of input points. Here is the script:
# Get the data
this_node = hou.node(".")
inputs = this_node.inputs()
first_input = inputs[0]
input_geometry = first_input.geometry()
points = input_geometry.points()

# Loop through add add up all the points
x = 0
for point in points:
    point_pos = point.position()
    x += point_pos[0]

# Divide the values by the number of points
num_points = len(points)
x = x / num_points

# Return the value
return x
The Houdini functions and classes are all in the hou module. When scripting in Houdini, the hou module is imported automatically, so there is no need to have 'import hou' in your scripts. The key ones to loo at from todays sessions are the following:
In order to better understand what is going when using the Houdini classes above, review Python classes in one of the tutorials. For example, in the Instant Hacking  tutorial , scroll down to the last section "More Abstraction — Objects and Object-Oriented Programming". This gives a quick explanation of classes.

    No comments:

    Post a Comment