Martin Sawtell's website

Python in Houdini

(How to get it working and use it normally and remain sane)

NOTE: SESI has since published a python technical evening that you can view here. This will have much more comprehensive and up to date info, but for the quick and dirty here you go:

For someone figuring out how to get python going in Houdini for the first time things can be pretty frustrating; IE where exactly do you put your python stuff, and how do you get houdini to call it in a way that gives you what you want. As you would expect there's lots of different places python code can live, and lots of ways to call it.

Grab this .hip and read the stickies.

OK so going over the .hip, python is living in these places:
-An inline hscript wrapper that just runs whatever python is in there. Not very flexible but you can call external scripts stored in:
-The OTL that's been embedded into the .hip
-The hou.session library

A note about the "hou" library- this is where Houdini has it's library of python commands, its called the Houdini Object Model (HOM). Go look at the documentation for the Houdini 10 HOM here

The inline script:

`pythonexprs("hou.chsop('../path')")`
The backticks either end are telling hscript to evaluate whatever is inside, the function being "pythonexprs", which just runs whatever you tell it to run. In this case hou.chsop which just grabs whatever parameter you tell it to. This whole statement is basically just running ch("../path") or chsop("../path"). Very simple but it shows python doing its thing and returning a parameter you can use.

The OTL script:
Is again called with an inline call:

`pythonexprs("hou.node('../').hdaModule().modifyPath('../path')")`
This is inside the OTL's subnet, hou.node('../') is returning the python object 1 layer above the call- in this case the OTL itself. hou.node('../').hdaModule() is therefore runing the method hdaModule() on the OTL. If you right click on the OTL and click "edit type properties", browse to the scripts tab you can see that a python script called modifyPath has been added. So hdaModule().modifyPath(inputparameter) is running the modifyPath() method on the OTL. Which is python. So there you go. The input to modifyPath is "../path", which looks like an hscript call and that doesn't make much sense- but it works. So the method screws around with the ../path parameter (which lives on the surface of the OTL) and returns a modified result, freeing you to do whatever string manipulation you want in python.

A note on the OTL
The OTL is stored in the .hip, along with whatever scripts or parameters are on it. The way to do this is to save the OTL to the Embedded library- right click on your OTL-to-be and click Create Digital Asset, give it a name, and instead of a path to a .otl put the word Embedded. This will save it to the current .hip

hou.session()

In every hip there exists a hou.session() library. You can get to this by clicking the windows menu item (up top) and then python source editor. Like the embedded OTL the hou.session() library is embedded to the current .hip An example of a call to a hou.session method is:

`pythonexprs("hou.session.g2(3)")`
The method it's calling is called g2 (weird name, but nevermind that) and giving it the number 3 as an input. It just returns another number.

So there you go, a bunch of places to store scripts and how to store them. The .hip linked at the top should give you a basic working framework to pull parameters from various places and Do Stuff to them.