Tuesday, May 4, 2010

Accessing hou from a Regular Python Shell

Accessing hou from a Regular Python Shell

By simply importing the hou module into a regular Python shell, you can easily integrate Houdini into your existing Python-based scripts. The first time you import hou, Python will load in all of Houdini’s libraries and initialize an empty Houdini session. The hou module effectively stores a Houdini session, and you can load into that session from a hip file, inspect that file from your script, and input and output information to and from nodes in that sesssion.

In order to import the hou module, you need to tell Python to search in $HFS/houdini/scripts/python for Python modules. One way to accomplish this is to append this to the PYTHONPATH environment variable before starting Python, and another is to append the path to the sys.path from within Python. The following Python snippet will import the hou module into a standard Python shell, assuming that you sourced houdini_setup to set $HFS.

#!/usr/bin/python2.5
def enableHouModule():
'''Set up the environment so that "import hou" works.'''
import sys, os

# Importing hou will load in Houdini's libraries and initialize Houdini.
# In turn, Houdini will load any HDK extensions written in C++. These
# extensions need to link against Houdini's libraries, so we need to
# make sure that the symbols from Houdini's libraries are visible to
# other libraries that Houdini loads. So, we adjust Python's dlopen
# flags before importing hou.
if hasattr(sys, "setdlopenflags"):
old_dlopen_flags = sys.getdlopenflags()
import DLFCN
sys.setdlopenflags(old_dlopen_flags | DLFCN.RTLD_GLOBAL)

try:
import hou
except ImportError:
# Add $HFS/houdini/scripts/python to sys.path so Python can find the
# hou module.
sys.path.append(os.environ['HFS'] + "/houdini/scripts/python")
import hou
finally:
if hasattr(sys, "setdlopenflags"):
sys.setdlopenflags(old_dlopen_flags)

enableHouModule()
import hou

When you import the hou module, Python will check out a license. By default, it will use a Houdini Batch license, and use a Houdini Master license if no batch license could be found. If you want it to use a Houdini Escape license, you can set the HOUDINI_SCRIPT_LICENSE variable to “hescape” before importing the hou module. You can also set this variable from within your Python script with os.environ['HOUDINI_SCRIPT_LICENSE'] = 'hescape'.

By default, the hou module will not return the Houdini license until the Python interpreter exits. However, if you have a long running Python script and want to quickly acquire and release a license, you can call hou.releaseLicense when you are done with the hou module. Subsequent calls into the hou module will reacquire the license. Note that Houdini’s session data and libraries will not be unloaded from memory until Python exits.

The hou module, when imported by Python or Houdini, will loop through the directories in $HOUDINI_SCRIPT_PATH, and for each directory found, append that directory plus “/python” to sys.path. Also note that $HOUDINI_SCRIPT_PATH defaults to the directories in $HOUDINI_PATH with an additional “/scripts” suffix. For example, if you use the default $HOUDINI_PATH, importing hou will append $HOME/houdiniX.Y/scripts/python to Python sys.path.
hython

Hython is a Python shell that ships with Houdini that is slightly different from the standard Python shell in the following ways:

*

It automatically adds $HFS/houdini/scripts/python to sys.path and imports the hou module when it starts up.
*

You can pass .hip files on the command line and it will load them.
*

It supports tab completion, and you can press tab twice to list possible completions.
*

It can receive and handle Houdini openport commands while waiting for console input.

User Contributed Notes add a note
There are no user-contributed notes for this page.

No comments:

Post a Comment