Python Resources

Updated on Feb 5 2022

Python In Houdini Examples

Deborah R. Fowler



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

Posted Feb 12  2014
Updated May 9  2022

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

If you are new to the os library please also see my section on introduction to system calls


Creating a folder (directory) with code is easy!



You could also move and rename the folder or the blue slides on TipsForE4.pdf

MISSING IMAGE


Practical use of os commands? Suppose your rendered an animation and you want to extend the start a certain number of frames - you can add those frames using a python script. See pad.py (written in python 3.8).


You can simply duplicate a frame (suppose you have held frames in an animation, no need to render them all). Here is a simple example.

duplicateFrame.py (written in python 3.8 or 9)

Note that you can also loop a frame in knitting together using ffmpeg - see Extending a Frame on my ffmpeg tips


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) python 2.7

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])


Problem: You want to mv all files that are evenly numbered into a directory.
Solution: 
A bit brute force, but does the job. Always avoid absolute paths
replaced:
if (f[-5:-4] == "0" or f[-5:-4] == "2" or f[-5:-4] == "4" or f[-5:-4] == "6" or f[-5:-4] == "8" ):

import os, shutil
path = "C:/Users/Deborah/Desktop/..../MyFiles/"
moveto = "C:/Users/Deborah/Desktop/...MyFiles/Evens/"
files = os.listdir(path)
for f in files:
    src = path+f
    dst = moveto+f
    if (f[-5:-4] in ["0","2","4","6","8"]):
        shutil.move(src,dst)

Aside
: moving to a directory is
import os
os.chdir(path)

Problem: You want to create the same rendered files, but in the reverse order (like the zigzag play in mplay)
Solution: Take the 1-99 (100 frames) and write them out so they are backward order
Hint: It would be better to make this more general. Left as an exercise. (Written in 2.7 - add parantheses on the print (filename, dest) and you are all set in python 3)