Python Code#

Sauvegarder des nodes en code Python#

import os
import hou

# set what to store
sel = hou.selectedNodes()[0]
code = sel.asCode(brief=False, recurse= True)
# print(code)

# set script name
ui = hou.ui.readInput(
    message="set the script name",
    initial_contents=sel.name(),
    buttons= ["SAVE", "CANCEL"],
    title = "script name")
scriptname = "test"
print(ui)

if ui[0] == 0:
    # set path
    path = os.environ
    # for x, y in path.items():
    #     print(x, y)
    pref = path.get('HOUDINI_USER_PREF_DIR')
    # print(pref)
    path = pref + "/snippets/" + ui[1] + ".py"
    # print(path)
    file = open(path, 'w')
    file.write(code)
    file.close()

Charger et exécuter un script Python#

import os
import hou

path = os.environ
pref = path.get('HOUDINI_USER_PREF_DIR')
path = pref + "/snippets/"

# define python script to load and execute
file = hou.ui.selectFile(path, title="select a python script")
# reading python file
openfile = hou.readFile(file)
# execute python file
exec(openfile)

Caméra et visualiseur de scène (scene viewer)#

import toolutils
import hou

# define cam and objects
cam = hou.node("/obj/cam1")
box = hou.node("/obj/box1")
# save parms of camera
focal = cam.parm("focal").eval()
aperture = cam.parm("aperture").eval()
# defining viewer
viewer = toolutils.sceneViewer().curViewport()
# print(viewer)
viewer.lockCameraToView(True)
# framing the view to the box
box.setSelected(1,1)
viewer.frameSelected()
# assign the viewer to the camera
viewer.saveViewToCamera(cam)
viewer.setCamera(cam)
# load/ reapply parms
cam.parm("focal").set(focal)
cam.parm("aperture").set(aperture)
# viewer.lockCameraToView(True)