Houdini 11 Houdini Object Model HOM Cookbook

Overview

This example defines a Python SOP that copies its input, creates a Cd (diffuse color) point attribute, and assigns each point a color based on the distance to a position.

Location

Supporting files for this example are in $HFS/houdini/help/hom/cookbook/color_falloff, also found in the cookbook/color_falloff directory of cookbook_files.tar.gz.

Viewing the SOPs Output

  • Load color_falloff.hip.

  • Press enter in the viewer to enter the handle tool, enabling the SOP’s translate handle.

  • Move the handle around to change the position parameter and change the falloff parameter, and how it affects the SOP’s output.

Exploring the SOPs Implementation

  • The color falloff Python SOP is stored in PythonSops.otl. Right-click on the color_falloff1 SOP and bring up the Type Properties dialog.

  • Click on the Parameters tab to see the pos and falloff parameters. These parameters were created exactly the same way you would for a normal digital asset.

  • Click on the Handles tab and you’ll see that a translate handle is bound to the posx, posy, and posz parameters.

  • Click on the Code tab to see the Python code that Houdini runs when the SOP cooks.

# When an instance of this Python SOP cooks, Houdini will have set hou.pwd()
# to the Python SOP instance.  Access the hou.Geometry object for this SOP.
# Since we're calling from hou.SopNode.geometry from a Python SOP
# implementation, we'll have write access to the geometry.
geo = hou.pwd().geometry()

# Create the "Cd" point attribute value, giving it a default value of white
# (1, 1, 1), and store the returned hou.Attrib object.
cd = geo.addAttrib(hou.attribType.Point, "Cd", (1.0, 1.0, 1.0))

# Evaluate the pos parm tuple, and create a hou.Vector3 out of it so we can
# later do vector subtraction.  Also evaluate the falloff value.
pos = hou.Vector3(hou.parmTuple("pos").eval())
falloff = max(hou.ch("falloff"), 0.0001)

for point in geo.points():
    # Compute the distance from this point to the position parameter, divide
    # the distance by the falloff value, and clamp it to be between 0 and 1.
    distance = (point.position() - pos).length()
    value = min(distance / falloff, 1.0)

    # Create a color object, and set the hue and value of the color based on
    # the normalized distance.
    color = hou.Color()
    color.setHSV((value * 256, 1.0, value))

    # Extract the RGB values from the color object and store them in this
    # point's Cd attribute value.
    point.setAttribValue(cd, color.rgb())

Creating a new Python SOP

Note that if you want to create your own Python SOP, you can do so by selecting File → New Operator Type and picking “Python Type” as the operator style and “Geometry Operator” as the network type.

See also