Houdini Py


create a node
    parent = hou.node("/obj")
    node = parent.createNode("node_type", name="node_name")
get a node
    # Using absolute path
    node = hou.node("/path/to/node")

    # Using relative path
    parent = hou.node("/path/to/node")
    node = parent.node("node_name")
        # or...
    node = parent.node("../../node_name")
check if a node exists
    if not hou.node("/path/to/node"):
        # the node does not exist

get some node properties
    name = node.name()
    type = node.type().name()
    position = node.position()
    color = node.color()
    comment = node.comment()
set some node properties
    node.setName("node_name")
    node.setColor(hou.Color((1, 1, 1)))
    node.setPosition([x, y])
    node.setComment("comment")
    
    # Some node flags
    node.setGenericFlag(hou.nodeFlag.Render, True)
    node.setGenericFlag(hou.nodeFlag.Visible, True)
    node.setGenericFlag(hou.nodeFlag.DisplayComment, True)
    
    # Node Shape
    node.setUserData("nodeshape", "light") # lightbulb shape

delete a node
    node.destroy()
unlock a node
    node.allowEditingOfContents()

get selected node(s)
    selected_nodes = hou.selectedNodes()
set current selected node
    node.setCurrent(True, clear_all_selected=True)
get current node (within node callback or expression)
    node = hou.pwd()

get all nodes of a type
    hou.objNodeTypeCategory().nodeType("obj_node_type_name").instances()
    
    # To get all node type categories
    print hou.nodeTypeCategories().items()
    
    # To get node type name components
    hou.hda.componentsFromFullNodeTypeName("node_type_name") 

get node children
    # Get all top level child nodes
    nodes = node.children()

    # Get all children
    nodes = node.allSubChildren()
get node parent
    parent = node.parent()

get primitive groups
    groups = node.geometry().primGroups()

    for group in groups:
        name = group.name()

connect nodes
    input_node = hou.node("/path/to/node")
    output_node = hou.node("/path/to/node")
    
    # Option 1
    output_node.sestInput(output_index, input_node)
    
    # Option 2
    output_node.setNamedInput("parm_name", input_node, input_node_index)

note: the connection goes from [input_node] —> [output_node]

insert a new node between two connected nodes
    output_connections = input_node.outputConnections()
    new_node = input_node.createOutputNode("node_type")
    
    if output_connections:   
        connection = output_connections[0]
        output_node = connection.outputNode()
        # output_index = connection.outputIndex()
        # input_node = connection.inputNode()
        input_index = connection.inputIndex()
        
        output_node.setInput(input_index, new_node)

layout nodes
    # Equal to hitting "L" in network view
    node.layoutChildren()
    
    # Layout specific nodes (in reference to first node in list)
    node.layoutChildren(items=[nodes], horizontal_spacing=1.5, vertical_spacing=1)
    
    # Layout a single node
    node.moveToGoodPosition()

get a parameter
    value = node.parm("parm_name").eval()  # Method 1
    value = node.parm("parm_name").rawValue()  # Method 2
    value = node.parm("parm_name").evalAsNode  # Method 3
    value = node.evalParm("parm_name")  # Method 4
set a parameter
    node.parm("parm_name").set(value)
    
    # Set an expression
    node.parm("parm_name").setExpression(expression, language=hou.exprLanguage.Python)
    
    # Set a channel reference
    node.parm("parm_name").set(ref_node.parm("parm_name"))

create a parameter
    parm_group = node.parmTemplateGroup()
    new_parm = hou.FloatParmTemplate(name="name", label="label", default_value=1)
    parm_group.insertAfter("existing_parm_name", new_parm)
    node.setParmTemplateGroup(parm_group)
get all parm templates on a node
    parm_group = node.parmTemplateGroup()
    templates = parm_group.parmTemplates()
remove a parm from a node
    parm_group = node.parmTemplateGroup()
    parm_group.remove(template)
    node.setParmTemplateGroup(parm_group)

create and customize a sticky note
    note = node.createStickyNote()
    
    note.setName("note_name")
    note.setSize([w, h])
    note.setPosition([x, y])
    node.setText("sticky note text")
    note.setTextSize(0.5)
    note.setColor(hou.Color((1, 1, 1)))
    note.setTextColor(hou.Color((0, 0, 0)))

create and customize a network box
    box = node.createNetworkBox()
    
    box.setName("box_name")
    box.setSize([w, h])
    box.setPosition([x, y])
    box.setComment("title")
    box.setColor(hou.Color((1, 1, 1)))
get network boxes
    # All network boxes
    boxes = node.networkBoxes()
    
    # A specific network box
    box = node.findNetworkBox("box_name")
get all nodes in a network box
    nodes = box.nodes()

get current context
    network_editor = None
    for pane in hou.ui.paneTabs():
        if isinstance(pane, hou.NetworkEditor) and pane.isCurrentTab():
            network_editor = pane
            
    if network_editor:
        network_node = network_editor.pwd()
        network_path = network_node.path()

display a message
    hou.ui.displayMessage("message to user")

create undo group
    with hou.undos.group("group name"):
        # commands to undo together

working with visualizers
    visualize_node = hou.node("path/to/visualize_node")
    node_category = hou.viewportVisualizerCategory.Node
    
    # Get visualizers associated with a visualize node
    visualizer_list = hou.viewportVisualizers.visualizers(category=node_category, 
                                                          node=visualize_node)
    
    # Get visualizer types
    type_list = hou.viewportVisualizers.types()
    marker_type = type_list[0]
    
    # Create a visualizer
    visualizer = hou.viewportVisualizers.createVisualizer(marker_type, 
                                                          category=node_category, 
                                                          node=visualize_node)
    
    # Set type and parameters
    visualizer.setType(marker_type)
    visualizer.setParm("parm_name", value)

To create custom tool uis for houdini, use QT