HOM Bundles Example: Extending the hou Module by Wrapping Hscript

Overview

This example illustrates how to extend the hou module, using Python, by calling hscript commands and functions. This particular example shows how to add node bundle support to the hou module. It also monkeypatches the hou module, adding 3 new functions to the module.

Note

Node bundles are already implemented in the hou module. See hou.NodeBundle for more information.

The example is split into two parts. Part 1 contains a simpler, minimalistic implementation that is easier to read but doesn’t contain all the functionality or handle all the error cases. Part 2 contains a full implementation.

To use this example, start up Houdini from the cookbook/bundles/part2 directory. Then, from the Python shell, run “import hou_bundles” to install the extensions to the hou module. Then you can use the hou.myAddNodeBundle, hou.myNodeBundles, and hou.myNodeBundle functions. For example:

Location

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

Sample Usage

Create a sphere and torus from the shelf. Houdini will create /obj/sphere_object1 and /obj/torus_object1 nodes. Now open a Python shell and run the following:

# Importing hou_bundles will add bundle-related functionality to the hou
# module.
>>> import hou_bundles

# Create a new bundle and display information about it.
>>> b = hou.addNodeBundle("mybundle")
>>> b
<hou.NodeBundle mybundle>
>>> b.name()
'mybundle'
>>> b.nodes()
()

# Add the sphere object and sphere sop to the bundle.
>>> sphere_obj = hou.node("/obj/sphere_object1")
>>> b.addNode(sphere_obj)
>>> b.addNode(sphere_obj.node("sphere1"))
>>> b.nodes()
(<hou.ObjNode of type geo at /obj/sphere_object1>, <hou.SopNode of type sphere at /obj/sphere_object1/sphere1>)

# Ask if particular nodes are in the bundle.
>>> b.containsNode(sphere_obj)
True
>>> b.containsNode(hou.node("/obj/torus_object1"))
False

# Change the name of the bundle.
>>> b.setName("mybundle2")
>>> b.name()
'mybundle2'

# Change the filter on the bundle.
>>> b.filter()
hou.nodeTypeFilter.NoFilter
>>> b.setFilter(hou.nodeTypeFilter.Object)
>>> b.filter()
hou.nodeTypeFilter.Object
>>> b.nodes()
(<hou.ObjNode of type geo at /obj/sphere_object1>)

Module Implementation

This example is split into two parts. Part 1 provides a simpler, easier to understand, but less complete implementation. Part 2 provides a full implementation, with support for filtering and error handling.

Subtopics

Clear