Jump to content

Scene As Python Code


Atom

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):
    os.mkdir(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)
file.write(s)

# 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)
    file.write(s)

file.close()

 

Edited by Atom
  • Like 2
Link to comment
Share on other sites

  • 5 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...