Python snippets

list all nodetypes

nt_categories = hou.nodeTypeCategories().items()
for nt_category in nt_categories :
    nodetypes = nt_category[1].nodeTypes()
    for nodetype in nodetypes :
        print nodetype

node list pattern glob

pattern   = "font_bar_*"
node_glob = hou.node("..").glob(pattern)
str_out   = ""

for node in node_glob: 
    str_out += pwd().relativePathTo( node ) + " "
    
return str_out

ctrl shift pressed when selectGeometry

import hou
import toolutils
from PySide2 import QtGui, QtCore, QtWidgets
def sel():
    selection = toolutils.sceneViewer().selectGeometry(prompt='Select POINTS', sel_index=0,   \
            allow_drag=False,  # drag and transform, we don't want that  \  
            quick_select=True,   # select on mouse_up, immediately, don't expect to be accepted  \
            use_existing_selection=True, initial_selection = None,   \
            initial_selection_type = None, ordered=False,   \
            geometry_types=(hou.geometryType.Points,),   \
            primitive_types=(), allow_obj_sel=True,   \
            icon=None, label=None, prior_selection_paths=[],   \
            prior_selection_ids=[], prior_selections=[],   \
            allow_other_sops=True, consume_selections=True)
    
    
    node = selection.nodes()[0]
    sel_geo = node.geometry()
    sel_pattern = str(selection)
    key_pressed = QtWidgets.QApplication.keyboardModifiers()
    if key_pressed == QtCore.Qt.ShiftModifier:
        print "shift"
    if key_pressed == (QtCore.Qt.ControlModifier | QtCore.Qt.ShiftModifier) :
        print "shift-ctrl"
        
    for sel_point in sel_geo.globPoints(sel_pattern):
        try:
            path_hda = sel_point.stringAttribValue("path_hda")
            frame = sel_point.intAttribValue("frame")
            print path_hda + "/" + str(frame)
        except:
            attributes_missing = 1
     # repeat
    sel()
    
sel()

enlarge bounding boxes

thanks F1!

node = hou.node('/obj/geo1/file1')
geo = node.geometry()
bba = geo.boundingBox()


node = hou.node('/obj/geo1/transform1')
geo = node.geometry()
bbb = geo.boundingBox()

bba.enlargeToContain(bbb)

add event callback

has to be a tuple of events, thanks Graham!

def name_changed(node, event_type, **kwargs):
   print("The geometry object is now named", node.name())

hou.node("/obj/geo1").addEventCallback( (hou.nodeEventType.NameChanged,) , name_changed)

reference copy

Thanks to Henry, Toadstorm!

parent = hou.node('/path/to/parent')
nodeToCopy = parent.node('./nodeToCopy')
parent.copyItems( (nodeToCopy, ), channel_reference_originals = True)

load from file

Thanks to Alex!

geo = hou.pwd().geometry()
for f in random_file_list:
 geo.loadFromFile(f)

create attribute

Thanks to F1!

geo = hou.pwd().geometry()
new_p = geo.addAttrib(hou.attribType.Point, 'new_p', 0.0)
old_p = geo.findPointAttrib('old_p')

for point in geo.points():
    val = point.attribValue(old_p)
    point.setAttribValue(new_p, val)

### Or do it this way:

geo = hou.pwd().geometry()
new_p = geo.addAttrib(hou.attribType.Point, 'new_p', 0.0)

values = geo.pointFloatAttribValues('old_p')
geo.setPointFloatAttribValues('new_p', values)

first keyframe's time

thanks to Toadstorm!

keyframes = hou.node('/path/to/node').parm('someParameter').keyframes()
min = 999999
for k in keyframes:
    if k.frame() < min:
        min = k.frame()
return min

event callback on parm change

thanks to MrScienceOfficer!

node.addEventCallback(hou.nodeEventType.ParmTupleChanged, call_func)

all the tabs

thanks to Bonsak!

tabs = hou.ui.paneTabs() # Get all the tabs

for tab in tabs: # Loop over them
    if tab.type() == hou.paneTabType.NetworkEditor: # Test the type
        tab.setPref('showdep','2') # Set

#get
#networkeditor.getPrefs()

pane under cursor

thanks to Varomix!

def getNetworkType():
    # get desktop
    curdesk = hou.ui.curDesktop()
    activepane = curdesk.paneTabUnderCursor()
    return activepane.type()

Bind light's "Enable" checkbox to the display flag

obj = hou.node(".")
return obj.isDisplayFlagSet()

Reference SOP input from chopnet Geometry

chopnode = hou.node("..")
chopname = chopnode.name()                  # chopnet_smooth
chopnet,task = chopname.split('_')          # chopnet smooth
channelnodepath = "../../channel_" + task   # ../../channel_smooth
channelnode = hou.node(channelnodepath)
inputpath = channelnode.inputs()[0].path()
return inputpath

instances() nodes of type in the scene

thanks to julian johnson!

node_type = hou.objNodeTypeCategory().nodeTypes()['bone']
for x in node_type.instances():
    print x

hou.playbar.setPlaybackRange(self, start, end)

print node.type().name() # file, filecache, delete, solver, dopimport, ...
node = hou.pwd()
path = node.path()
objContextNodeName = path.split('/)[-2]