Jump to content
Sign in to follow this  

Scene As Python Code

Recommended Posts

Hi All,

I was playing around with exporting networks as python code. Here is a short script you can place in a Shelf Tool to export networks into executable python code. The resulting Python file appears on your desktop in a folder called HIPAsCode and is named like your .hip file. Certain parameters don't convert well. Fortunately those parameters are detectable and you can just comment those lines out. (i.e. hou_parm.set("{...) The fallout for commenting these troublesome lines of code are that the node in question will simply fallback default values instead of the values from the original .hip file.

import hou, os

# Framerange.
frame_start = hou.playbar.playbackRange()[0]
frame_end   = hou.playbar.playbackRange()[1]
# Construct paths.
export_name = "%s.py" % hou.expandString("$HIPNAME")    #"Scene1.py"
home = os.path.dirname(os.path.expanduser("~"))
desktop = os.path.normpath(os.path.join(home, 'Desktop'))
project_dir = os.path.normpath(os.path.join(desktop, 'HIPAsCode'))
file_output = os.path.normpath(os.path.join(project_dir, export_name))

# Create the project directory if it does not exist.
if not os.path.exists(project_dir):

file = open(file_output, "w")
file.write("import hou\n\n")
file.write('# Some node creation parameters do not work. bug H15.5?\n')
file.write('# Before you execute this resulting code, use find/replace hou_parm.set("{ with #hou_parm.set("{\n')
file.write('# This will comment the lines that do not currently work.\n\n')

s = "hou.playbar.setPlaybackRange(%s,%s)\n" % (frame_start, frame_end)

# Export nodes as code.
lst_node_root = ["/obj","/shop","/out"]
for node_path in lst_node_root:
    start_node = hou.node(node_path)
    s = start_node.asCode(brief=True, recurse=True, save_creation_commands=True, save_spare_parms=True)



Edited by Atom
  • Like 2

Share this post

Link to post
Share on other sites


how load this file back to the houdini to restore information from file?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this