Python
in Houdini

Here I will outline the where (getting the
code into Houdini) and the how (hou syntax):

There are many excellent references
for using python in Houdini.
One is the MasterClass on SideFX website here.
There is also information in the sidefx documentation but here is
an overview to get you started:

In Houdini there are many ways to run
python code and Houdini is very python friendly. One of the
easiest is to create a python sop. Two other common ways are
using the python shell and python source editor (for those of you
coming from a mel coding background this one will be most
familiar):

Python Source Editor (under the
Windows menu) - stores your python code in the hip file
Node: Any function you type in here you can also call from the
shell by adding the prefix hou.session to the function call.

Shelf Tool

Using python in expressions

The python module
within HDA is like Autodesk's Maya Embedded Language.
Examples exist below as well as on the Examples link at the top
of this page.

Python Sop

Python Shell

This is similar to
a hscript textport shell. Under Windows select Python Shell -
this will open a python shell window. Another way is to simply
select Windows->Desktop->Technical. This is an excellent
default layout which includes a python shell at the bottom
middle of the screen. You can call functions you have created in
the python source editor (using the hou.session prefix) or
import .py file (see below).

Python Source Editor

The code you type into the python source editor
is saved with your hip file.

For example, bring up the Python
Source Editor and create a function to print hello world:

def printHello():
print("Hello World")

If you add the call printHello() to
the code it will print "Hello World" each time you hit Apply. (see
hip file).

You can also call this function in
the python shell by typing hou.session.printHello()

If you want to use houdini specific
calls, you would have to import hou.
So for example, to create a sphere in the python source editor you
would type

In this case, each time you hit apply a new
sphere would be added. (see hip file.) Below
I hit apply 5 times.

Importing py
files

You can also create files outside of houdini
using any text editor. Notepad++ or gedit are two commonly used
editors. For example, I have used the phyllotactic pattern
example written in python. I have used calls to houdini to
create the geometry.

Open up Houdini and set the desktop to
technical (or simply open up a python shell under the Window
menu). Download the py
file and hip
file. To run the code in the python shell (make sure you
save the files appropriately, and on Windows, launch by double
clicking the hipnc). NOTE: if you run houdini from the command
line in the directory where the files are the imports will work
perfectly.

import
circlehouPhyllo
circlehouPhyllo.phyllocircle(10)

Note when you import this, it creates a
compiled python file (.pyc) in your folder).

(There is a sample file
here with the code in the python source editor instead.)
An excellent exercise would be to modify this code so the
spheres were in one geo node. Secondly, try setting this up with
instances instead.

Shelf tool

You can add a tool to an existing shell by
simply right clicking on the shelf icon area and selecting "New
Tool". Probably best to make it in its own shelf tab.

Left click on the + shelf icon
and select "New Shelf". Give it a name and label and click
accept. Now under this new shelf tab, right click and select
"New Tool". Click on
the "New Tool" and select the Script tab. You can now enter your
code here in Python (alternatively in hscript). This is now
accessible from the shelf.

Using Python in expressions

Hscript and Python can co-exist
in your parameters. The calls to the hou module sometimes make
the expressions look longer.In Hscript $F becomes in Python
hou.frame() or for local parameters: Hscript
$CY becomes in Python lvar("CY")(Note that it will put the
non-default type in purple and the others in green but they are
not broken. They can co-exist as long as you have the type that
you intend to input selected before typing in the expression)

So far the where has been explained where, but
what about how - syntax.

Creating nodes

Suppose you want to create some
geometry, here is a quick example demonstrating some useful
commands:

Note that n, mergethem and
aSphere are simply variables. In the first line, the hou module,
node is the function (method) and 'obj' is the argument to that
function. n.path() would give us obj/geo for example. We are
then setting a sphere 10 units on the screen. This will give you
a sphere node connected to a merge node with the display flag on
the merge - so on the scene you will see a sphere at 10 units on
the x-axis. For more functions/commands refer to the
documentation.

If you wanted to get rid of the file node that
comes with this geo node by default, the command for this would
be:

Note below are step by step examples - if you
are in a hurry for the final result at the bottom of this
section labeled Final Result.

As an exercise, go back to the phyllotaxis
demo above and get rid of the file nodes. Put them all in one
geo, and merge them given what you have just learned above. (Answer
here) - note these are a step by step examples - if you
are in a hurry for the final results see below).

Next step would be to set up the network much
the same way as you would interactively - with a copy node. (Answer here).
Finally, a user interface should be implemented - here is an
example using a vopsop (Answer
here).

See the example file above (final .py file).
Odforce posting clarifying addSpareParmTuple command is here
An example of adding a parameter to a node would be (angle is a
parameter to my function and geo is set to a node by using
hou.node):