Python
Contents
Set an existing preset to a node using python
I couldnt find a direct python route for this but hscript has a exactly what i was looking for. so cross using it inside python solves all problems.
Here is the hscript:
oppresetload operator_name preset_name
Example:
oppresetload /obj/model/lsystem1 Lightning
In Python we can use the hou.hscript() funtion to run a hscript inside python
import hou
hou.hscript("oppresetload /obj/model/lsystem1 Lightning")
This would be equivalent of the above hscript function
Accessing current camera
Here is an example how we can get the current camera data and manipulate them through python
I use this script to set the nearclip plane values to quickly hide the geos in viewport. Will be very usefull when you want to toggle hide/unhide quickly while matching to something to a backplate
import hou,toolutils
viewer = toolutils.sceneViewer()
viewport = viewer.curViewport()
camera = hou.GeometryViewport.camera(viewport)
near = camera.parm("near")
if near.eval()==0.0088:
near.set(999999)
else:
near.set(0.0088)
print "nearclip of camera %s set to %s"%(camera.name(),near.eval())
Get print all uniuqe attributes
# example to print all unique materials
node = hou.selectedNodes()[0]
shop_path = node.geometry().findPrimAttrib("shop_materialpath")
if shop_path :
for path in shop_path.strings() :
print path
To Reload a module in Houdini without closing the session
#using the reload() method of importlib module
import movfxTools
import importlib
importlib.reload(movfxTools)
Arnold light from Mantra light
Extracts exposure,intensity and sizes of mantra area lights and create arnold light of same values
import hou
node = hou.selectedNodes()[0];
int = node.parm('light_intensity').eval()
exp = node.parm('light_exposure').eval()
asX = node.parm('areasize1').eval()
asY = node.parm('areasize2').eval()
posx = node.position()[0]
posy = node.position()[1] - 1
pos = [posx,posy]
print pos
context = node.parent()
arLight = context.createNode('arnold_light')
arLight.setInput(0, node)
arLight.setPosition(pos)
arLight.parm('ar_light_type').set(3)
arLight.parm('ar_intensity').set(int)
arLight.parm('ar_exposure').set(exp)
arLight.parm('ar_quad_sizex').set(asX)
arLight.parm('ar_quad_sizey').set(asY)
print arLight
Align Nodes
"A" is the shortcut to quickly align nodes, But it distributes the nodes evenly , we cant align in one axis without modifying the positions in other axis
So I wrote this simple code for my shelf to do that.
import hou
def alignHorizontal():
nodes = hou.selectedNodes()
posYs = [node.position().y() for node in nodes]
avgPosY = 0.0
for posY in posYs:
avgPosY += posY
avgPosY = avgPosY/len(posYs)
for node in nodes:
x = node.position().x()
node.setPosition([x,avgPosY])
alignHorizontal()
import hou
def alignVertical():
nodes = hou.selectedNodes()
posXs = [node.position().x() for node in nodes]
avgPosX = 0.0
for posX in posXs:
avgPosX += posX
avgPosX = avgPosX/len(posXs)
for node in nodes:
y = node.position().y()
node.setPosition([avgPosX,y])
alignVertical()
Netbox with padding
####netbox with padding - by mohanpugaz on sidefx forum
import hou
### net box with padding
### set your padding here
padX = 2
padY = 2
##
##init vars
nodes = hou.selectedNodes()
parent = nodes[0].parent()
pad = (padX,padY)
posx = []
posy = []
curpos = 0
## run a loop to get all nodes x and y position
for n in nodes:
curpos = 0
curpos = n.position()
posx.append(curpos[0])
posy.append(curpos[1])
## extract the min and max postion from the position lists and arrange it in a sequence(see help on boundingRectangle.setTo)
maxpos = (max(posx),max(posy))
minpos = (min(posx),min(posy))
seq = (minpos[0],minpos[1],maxpos[0],maxpos[1])
## create a new bounding rectangle and set our min and max values
bound = hou.BoundingRect()
bound.setTo(seq)
## expand the bound
bound.expand(pad)
## create a netbox and set our expanded bound value
box = parent.createNetworkBox()
box.setBounds(bound)
Viewer Screen capture shelf tool
from time import gmtime, strftime
# GET VIEWPORT CAMERA PATH
cur_desktop = hou.ui.curDesktop()
desktop = cur_desktop.name()
panetab = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer).name()
persp = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer).curViewport().name()
camera_path = desktop + "." + panetab + "." + "world" + "." + persp
# BUILD DEFAULT FILE NAME FROM CURRENT TIME
default_filename = strftime("screenshot_%d_%b_%Y_%H_%M_%S.jpg", gmtime())
# SELECT FILE
#filename = hou.ui.selectFile( title='Select Screenshot File', default_value=default_filename, file_type=hou.fileType.Image )
filename = "ip"
# WRITE TO FILE
if filename is not None:
frame = hou.frame()
hou.hscript( "viewwrite -f %d %d %s '%s'" % (frame, frame, camera_path, filename) )
RND for HoudiiniCollectFiles(WIP!!!!!!)
import os
def findFileInputs(node):
"""this function takes the given node object and return list of
parameters which are file references"""
parms = node.parms()
fileParms = []
for parm in parms:
if parm.parmTemplate().type().name() == "String":
if parm.parmTemplate().stringType().name() == "FileReference":
fileParms.append(parm)
return fileParms
def findKids(node):
nodeList = []
def kids(node):
for kid in node.children():
nodeList.append(kid)
kids(kid)
return nodeList
nodeList = kids(node)
return nodeList
root = hou.node("/")
files = []
nodeFiles = []
paths = []
for kid in findKids(root):
nodeFiles=findFileInputs(kid)
files+=nodeFiles
for afile in files:
paths.append(afile.eval())
paths = filter(None, paths)
for path in paths:
print path
For activating node inside an asset (for example paint node here)
#1 callback script for the button
hou.pwd().hdaModule().functionName(kwargs)
#2 In the asset edit > in python module
def functionName(scriptargs):
sopnode = scriptargs['node']
viewer = toolutils.sceneViewer()
paint = sopnode.node("paint_node_name_here")
paint.setCurrent(True, True)
viewer.enterCurrentNodeState()
Parameter init
__init__(name, label, num_components, default_value=(), min=0.0, max=10.0, min_is_strict=False, max_is_strict=False, look=hou.parmLook.Regular, naming_scheme=hou.parmNamingScheme.XYZW, disable_when=None, is_hidden=False, is_label_hidden=False, join_with_next=False, help=None, script_callback=None, script_callback_language=hou.scriptLanguage.Hscript, tags={}, default_expression=(), default_expression_language=())
Most usefull commands:
hou.ui.displayMessage("string")
hou.expressionGlobals() - for setting custom function to work on parameter expression
hou.parm('/obj/geo1/file1/reload').pressButton()
Useful Links:
To start learnig from basic houdini python
http://www.sidefx.com/docs/houdini/hom/intro
Script Locations
http://www.sidefx.com/docs/houdini/hom/locations
Startup Tuts
http://www.warshaw.co.uk/links/pythonh.html
Adding Menus