Lately, I’ve been playing more and more with the Houdini Python API. One of the things I like to do when learning a new Python module is inspect the module itself to see what it has to offer. I can access the Houdini documentation for this but I usually end up using the Houdini shell for inspecting the Houdini
The dir() Built-In
A nice way of inspecting Python modules is by using the built-in convenience function
dir() function takes in an optional object and spits out a list of names in the current scope. This function behaves differently depending on what type of object is passed in. So if you pass in a module object, it will contain a list of the module’s attributes and if you pass in a class object or type, the list will contain the names of its attributes, and recursively of the attributes of its bases. Don’t worry if none of this makes any sense.
Keep in mind, this will work in the Houdini Python shell or any Python interactive shell. Let’s use a regular shell for now and then move on to the Houdini shell. I’m on OS X but this should be the same on Windows or Linux.
Let’s say we want to see what methods are available in Python’s
os module. This module allows you to interact with the operating system. Super handy! We can use
dir() to inspect what attributes and methods are available. First I will import the
os module and then use the
dir() function passing in our imported module. This will take two lines of code.
import os dir(os)
That’s it! If you’re new to Python, don’t worry too much about the names that have leading and trailing double underscores. Have a look at the lowercase names such as
listdir. If you were to look up these methods in the Python documentation to check what they do and how to use them, you will find that these methods are used to change into a directory, get the current working directory, and list the contents of a directory respectively. You can do this for any module. Simple, yes?
Hou Can It Be Now?
Now that you know how to use
dir(), let’s move on over to Houdini. Open the Houdini Python shell found under the
Window menu or using the shortcut
alt(option) + shift + p. Using
hou, since this is the module we want to inspect, we get the following output:
This is a big one! The big daddy. The king of the hill. The Michael Knight. The Matrix… The reason is because the hou package is the top of a hierarchy of modules, functions, and classes that define what is known as the
Houdini Object Model, or the
HOM if you want to impress your friends at Starbucks. This is the API controlled through Python that lets you do the magic.
One issue when using the dir function on a module is that the print out is hard to read. Here are two ways to tackle this. First, you can of course loop through the list that is returned by calling
dir(hou),to get a nice column displaying the contents of the list like so:
for i in dir(hou): print(i)
The second method is by using Python’s data pretty print module
pprint. It may seem more complex but it’s actually really easy to use. The module defines one class that you use to create a
Pretty Print Module pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None)
However, you can call several derivate functions which we will use here to keep it simple. First thing I will do is import the
pprint module into my namespace. I then call the
pprint() function passing in
dir(hou) as our data stream. I can optionally also add formatting parameters which then will get passed on to the
PrettyPrinter constructor. In this case, I’m indenting the output by 15 units.
import pprint pprint.pprint(dir(hou), indent=15)
Either way you choose, it is definitely much nicer on the eyes! You can even save it out to a file and peruse it in your free time while sipping on some Gin & Juice.