HoudiniPython

From cgwiki

Make a general python input window

Text editor demo.gif

Download scene: File:python_code_window.hip

The python console in Houdini is a live command line, which is great for some things, but doesn't allow you to enter multi-line code easily. Maya's script editor allows this, I wanted a similar thing in Houdini.

The python SOP looks like its the answer, but don't be fooled. Its the python equivalent of a point wrangle, its designed to process geometry, not do general node or UI fiddling (which is really what you'd want python for).

With the help of Luke Gravett, here's a way to make something akin to maya's python script editor. This is version 2 of such a thing, it's just a null with a text editor and a button to execute the code. An earlier version of this used an OTL/HDA, which was more fiddly than necessary.

  1. Make a null sop, name it 'my_python_code'
  2. parameter pane, gear menu, 'Edit Parameter Interface...':
  3. add a string parameter, label 'Code', enable 'multi-line string', language 'Python'
  4. add a button, label 'Run', change the callback method to python ('little dropdown at the end of the line), python callback is
exec(kwargs['node'].parm('parm').eval())
  1. Hit Accept


Now you can type in code, click the button, make magic.

Get selected nodes

hou.selectedNodes()


Get point attributes from a node

From the node get its geometry, then its point attributes, then the short names of those attributes.

[ x.name() for x in hou.node('/obj/mygeo/mysop').geometry().pointAttribs() ]


To go one further and make a nice list to feed to an attribute delete node, use a join() with a single space, prepend with ^'s, and stick an asterisk on the front:

print '*',' '.join([ '^'+x.name() for x in hou.node('/obj/geo1/mysop').geometry().pointAttribs()])


will return

* ^id ^Cd ^Alpha ^center ^orient ^P ^uniqueId ^materialId

Drag most things into the python window

Do this, and what you drag will be converted into the python text equivalent. This works for nodes, parameters, shelf buttons, most parts of the UI.

Write out mmb error text to file

Handy!

open('/tmp/error.txt','w').write(hou.node('/path/to/node').errors())

Get parent vs get input

Parent in houdini means the container; ie if you have a subnet1, and inside is box1, if you ask box1 for its parent, its subnet1.

n = hou.node('/obj/subnet1/null2')
n.parent()
# <hou.SopNode of type subnet at /obj/subnet1>


If you have box1 connected to mountain1, and ask mountain1 for its inputs, you'll get box1 (as a list).

n = hou.node('/obj/mountain1')
n.inputs()
# (<hou.SopNode of type box at /obj/box1>,)


get attrib from point listed in group field of sop

So many layers of indirection!

# get list of nodes, here i've dragged a bunch of nodes inot a node list parm i added to a python script node I created above
nodes = kwargs['node'].parent().parm('nodes').eval()
 
# for each node in the list:
for n in nodes.split(' '):
   n = hou.node(n)
 
   # read the group field, here they're all in a format like '@myattrib=57-58' 
   group = n.parm('group').eval()
 
   # use the handy glopPoints function to convert that group syntax to a list of points
   for p in n.geometry().globPoints(group):
        # get the attrib we're after!
        print p.attribValue('awesomeattrib')

Create a cop file node for every subdir of a directory

Assumes a lot; that the image sequence within each folder is of the same name, and the parent folder only contains subfolders with images, no error checking is done, may contain traces of peanut etc...

import os
 
dir = '/path/to/parent/folder'
seq = 'render.$F4.exr'
 
subdirs = [x for x in os.listdir(dir) if os.path.isdir(os.path.join(dir,x))]
 
for d in subdirs:
    imgpath = os.path.join(dir,d)+'/'+seq
    filenode = hou.node('/img').createNode('file')
    filenode.parm('filename1').set(imgpath)