Skip to content

Instantly share code, notes, and snippets.

@jamesrobinsonvfx
jamesrobinsonvfx / set_read_only_parm.py
Last active August 9, 2022 19:46
Context manager for unlocking/locking parameters
import contextlib
@contextlib.contextmanager
def set_read_only_parm(parm):
if not isinstance(parm, (hou.Parm, hou.ParmTuple)):
return
# Unlock the parm
parm.lock(False)
@jamesrobinsonvfx
jamesrobinsonvfx / expandcollapsestringdemo.py
Created February 20, 2022 22:55
Expand / Collapse String Parameter Snippets
"""Disable When / Hide When Expressions
Used to control parameter visibility.
"""
# Hide When Expression
{ expanded == 1 }
# Hide When Expression (multiparm)
@jamesrobinsonvfx
jamesrobinsonvfx / parm_menu_list.py
Last active January 25, 2022 11:00
Double up a list for Houdini menu parameter scripts
items = ["raspberry", "grape"]
return [x for y in [(x, x) for x in items] for x in y]
# ["raspberry", "raspberry", "grape", "grape"]
# Or fancier
[x for y in [(x, x.title()) for x in presets] for x in y]
# ["raspberry", "Raspberry", "grape", "Grape"]
@jamesrobinsonvfx
jamesrobinsonvfx / ftrimify.py
Last active January 22, 2022 04:41
Wrap an ftrim() function around channel refs
import re
import hou
def ftrimify(parm):
"""Wrap an ftrim() function around channel refs.
Args:
parm (hou.Parm): Parameter to add ftrim()s to. Should be a
@jamesrobinsonvfx
jamesrobinsonvfx / wildcard_camelcase.hscript
Last active September 24, 2021 18:14
Wildcard for camelCased paths in Houdini parameters
// Match forearm, leftArm, and arm
@name=*[aA]rm
@jamesrobinsonvfx
jamesrobinsonvfx / toggle_manual.py
Last active March 9, 2023 20:32
Toggle Manual Mode in Houdini
def toggle_manual():
"""Toggle cook mode"""
current_mode = hou.updateModeSetting()
auto = (hou.updateMode.AutoUpdate, hou.updateMode.OnMouseUp)
toggle_cook = hou.shelves.tool("toggle_manual")
toggle_cook.setLabel("Auto Update")
if current_mode in auto:
hou.setUpdateMode(hou.updateMode.Manual)
toggle_cook.setIcon("MISC_cook")
elif current_mode == hou.updateMode.Manual:
@jamesrobinsonvfx
jamesrobinsonvfx / multiparm_instance_index.py
Last active January 27, 2022 21:04
Easiest way to get multiparm instance index
# Callbacks
idx = kwargs["script_multiparm_index"]
# In a parameter expression
idx = evaluatingParm().multiParmInstanceIndices[0] # Returns a tuple. Will need subscript for the right level, and test for IndexError
# In a parameter expression, better
import re
idx = re.match(r".+(\d+)$", evaluatingParm().name()).group(1)
@jamesrobinsonvfx
jamesrobinsonvfx / first_last_filetime.py
Last active August 20, 2021 20:52
Quick 'n' dirty script to include as part of a custom cache node, or a shelf tool to calculate cache times for frame sequences (Python 3)
import math
import os
import re
def timedelta(file1, file2):
t1 = os.path.getmtime(file1)
t2 = os.path.getmtime(file2)
time = abs(t2 - t1) / 60.0
min = math.floor(time)
@jamesrobinsonvfx
jamesrobinsonvfx / rbd_to_instancepoints.h
Last active August 20, 2021 22:43
Convert RBDs (Fetch Packed Geometry from DOP network style) to instancepoints. Pretty sure this is lifted from one of Henry Foster (Toadstorm)'s odforce/sidefx forum replies.
// Get packed xform and pivot
matrix xform = getpackedtransform(0, i@ptnum);
vector pivot = primintrinsic(0, "pivot", i@ptnum);
// Orient for instancing
p@orient = quaternion(matrix3(xform));
// Crack pscale
vector scale = cracktransform(0, 0, 2, 0, xform);
f@pscale = avg(scale);
@jamesrobinsonvfx
jamesrobinsonvfx / set_viewer_to_cam.py
Created July 20, 2021 03:53
Set viewport to camera
import toolutils as tu
viewer = tu.sceneViewer()
viewports = viewer.viewports()
cam = hou.node("/obj/rendercam")
# There are always 4 viewports!
# https://www.sidefx.com/forum/topic/38171/#post-174454
for vp in viewports:
vp.setCamera(cam)