Turntable avec Python#

import os
import hou
import toolutils

class turntable():

    def __init__(self):
        button_choice, (startframe, turntableTime, lightspinTime) = hou.ui.readMultiInput(
        "Set the turntable settings", ("start frame", "turntable duration", "lightspin duration"),
        initial_contents = ("1001","50","50"),
        title = "Turntable and lightspin settings",
        buttons = ("Create turntable", "Cancel"),
        default_choice=0, close_choice=1,
        )
        print(button_choice)
        print(values)
        if button_choice == 0:
            self.createTT(int(startframe), int(turntableTime), int(lightspinTime))

    def createTT(self, startframe, turntableTime, lightspinTime):
        envmap = hou.ui.selectFile(start_directory="E:/RESSOURCES/HDR/HDRI")

        ####### TTRIG IMPORT #######
        # Importing Turntable chromeball rig
        dump = os.environ
        dump = dump.get("HOUDINI_USER_PREF_DIR")
        file = dump + "/scripts/python/TTrig.py"
        openfile = hou.readFile(file)
        exec(openfile)

        # Frame camera on model
        viewer = toolutils.sceneViewer().curViewport()
        viewer.lockCameraToView(True)
        model.setSelected(1)
        viewer.frameSelected()
        viewer.saveViewToCamera(cam)
        viewer.setCamera(cam)

        # Setting up parameters of camera and add padding in Z
        cam.parm("focal").set(50)
        cam.parm("aperture").set(41.4214)
        camZ = cam.parm("tz").eval()
        cam.parm("tz").set(camZ + 2)

        ######## CONTROL AND LIGHT SETUP ########
        # Create a null for model
        ctrl = subnet.createNode("null", "CTRL_" + name)
        ctrl.setInput(0, ctrl)

        # Create light
        light = subnet.createNode("envlight", "envLight_A")
        light.parm("env_map").set(envmap)
        light.parm("light_contribprimary").set(1)

        # Clean the nodes position
        subnet.layoutChildren()

        # DEFINE keyframess
        startTT = startframe
        endTT = startframe + turntableTime
        startSpin = endTT+1
        endSpin = startSpin + lightspinTime
        # set houdini playbar
        hou.playbar.setFrameRange(startTT, endSpin)
        hou.playbar.setPlaybackRange(startTT, endSpin)

        # Define a dictionary containing the keyframes and their values
        ditTT = {startTT:0, endTT:360}
        dictSpin = {startSpin:0, endSpin:360}

        for data in dictTT:
            key = hou.Keyframe()
            key.setFrame(data)
            key.setValue(dictTT.get(data))
            ctrl.parm("ry").setKeyframe(key)

        for data in dictSpin:
            key = hou.Keyframe()
            key.setFrame(data)
            key.setValue(dictSpin.get(data))
            light.parm("ry").setKeyframe(key)

        ######## ROPNET RENDER #######
        # Creating render nodes and setting their parameters
        ropnet = subnet.createNode("ropnet")
        mantra = ropnet.createNode("ifd", "mantra_turntable_" + name)
        mantra.parm("camera").set(cam.path())
        myoptions = mantra.parm("trange").menuItems()
        mantra.parm("trange").set(myoptions[1])
        mantra.parm("vm_miraysamples").set(4)
        mantra.parm("vobject").set(subnet.name())
        mantra.parm("alights").set(subnet.name() + "/" + light.name())

turntable()