Python Resources

Updated on July 16  2014

Python In Houdini Examples

Deborah R. Fowler



Python - General Examples (for Houdini specific examples click here)

Posted Feb 12  2014
Updated July 13  2016

NOTE: If you wish to know your python version:
import sys
print(sys.version)


Here is a simple script to handle file manipulation:

Problem: A student had generated some files caching out a simulation. Instead of typing the usual .bgeo.gz they accidently typed a comma instead of a period. As a result the files were not recognized since their format was then .bgeo. 

Solution: rename the files to .bgeo 

Here is the script:

import os
for filename in os.listdir( "." ):
    if filename.endswith( ",gz" ):
        os.rename( filename, filename[:-3] )


Here is another handy one for Windows users:

Problem: Needed to print out the names of the files in a directory.

Solution: run the following python script (2.7) (listfile.py)

import os
file = open("./list.txt","w")
for filename in os.listdir("."):
        print filename
        file.write(filename)
        file.write("\n")
file.close()


Here is one related to the Renderfarm:

Problem:
Renders from the farm are output.XXXX.exr, but if compiling multiple shots need unique names.

Solution: run the following python script (2.7) (renameFiles.py)


Here is on related to Houdini and Maya:

Problem: Bringing in a single frame of point data from Houdini into Maya:
Note
in Maya to run Python open the script editor window at the bottom left MISSING IMAGEand select python tab. Load script (or type in a new one) and execute:

Solution: output the point data to a file.hclassic (ascii format) and then run the following script in maya (mayaReadPtsFromHclassic.py)

file = open(“C:/Users/Deborah/Desktop/toMaya/untitled.hclassic”,”r”)

l = []

for line in file:

                for t in line.split():

                                try:

                                                l.append(float(t))

                                except ValueError:

                                                pass

points = l[8:]

 

n = 1

tuples = []

for num in points:

                if (n % 4 != 0):

                                tuples.append(num)

                n = n+1

print tuples

 

import maya.cmds as cmds

for n in range(0,len(tuples),3):

cmds.polySphere(r=.1)

cmds.move(tuples[n],tuples[n+1],tuples[n+2])