Houdini Python Temp

From bernie's
Jump to: navigation, search

Set screenshot as network background (linux)

spPeavu.png

todo: set&use a global var to choose where to store the screenshots

import hou
import os
import subprocess

hipname = str(hou.getenv('HIPNAME'))
timestamp = subprocess.check_output('date +"%y%m%d_%H%M%S"',shell=True).rstrip()
path = '/harcoded/path/'+hipname+str(timestamp)+'.png'
#print path
os.system('import '+path)

editor = hou.ui.paneTabOfType(hou.paneTabType.NetworkEditor)
bounds = editor.visibleBounds()

node = editor.currentNode().parent()

'''
#TODO: ATTACH TO NODE/NETWORKVIEW
nb = editor.currentNode().parent().createNetworkBox()
nbbounds = bounds
nbbounds.setTo(bounds[0],bounds[1]+5,bounds[2],bounds[3]-5)
nb.setBounds(nbbounds)
nb.resize([0,.5])
nb.setMinimized(1)
nb.setComment('ctrl i to delete')
'''

image = hou.NetworkImage()
image.setPath(path)
image.setRect(bounds)
editor.setBackgroundImages([image])

#we need this otherwise the bg image doesn't get saved. dunno why
dic = '[{"path": "'+path+'", "rect": ['+str(bounds.min()[0])+','+str(bounds.min()[1])+','+str(bounds.max()[0])+','+str(bounds.max()[1])+']}]'
node.setUserData("backgroundimages", dic)

Create points from parsed text file

node = hou.pwd()
geo = node.geometry()
frame = geo.addAttrib(hou.attribType.Point, "frame", 0)
isNan = geo.addAttrib(hou.attribType.Point, "isnan", 0)

# Add code to modify contents of geo.
# Use drop down menu to select examples.


import re

fpath = '/prod/prod1/Projets/RESSOURCES/_graphistes/Bernie/joss/nuagepts.txt'
fpathvals = '/prod/prod1/Projets/RESSOURCES/_graphistes/Bernie/joss/nuagevals.txt'
pts = open(fpath)
ptsvals = open(fpathvals)

line = 'empty'

while line:
    line = pts.readline()
    lineb = ptsvals.readline()
    if len(line) > 8:
        curline = re.split(r'\t+', line.rstrip())
        for e in range(len(curline)):
            curline[e] = float(curline[e].replace(',','.'))
        pt = geo.createPoint()
        pt.setPosition(hou.Vector3(curline))
        floatFrame = lineb.rstrip()
        if floatFrame == 'NaN':
            floatFrame = 0
            pt.setAttribValue(isNan, 1)
        pt.setAttribValue(frame, int(floatFrame))

mikros

import hou
import os

def getKey(item):
    return item.position()[1]

def buildUiLine(mianFileNode,pTG,c):
    
    node = mianFileNode
    versionN = int(node.evalParm('cacheVersion'))
    checked = False if node.isBypassed() else True
    sf = int(node.evalParm('fx'))
    ef = int(node.evalParm('fy'))

    name = mianFileNode.name()
    n = str(c)

    t = hou.FolderParmTemplate("folder"+n, name, folder_type=hou.folderType.Simple, default_value=0, ends_tab_group=False)
    t.setTags({"group_type": "simple"})
    
    t2 = hou.ToggleParmTemplate("checkbx_"+n, " ", default_value=checked)
    t2.setJoinWithNext(True)
    t.addParmTemplate(t2)
    
    t2 = hou.IntParmTemplate("versBox_"+n, "v#", 1, default_value=([versionN]), min=0, max=10, min_is_strict=False, max_is_strict=False, naming_scheme=hou.parmNamingScheme.Base1, menu_items=([]), menu_labels=([]), icon_names=([]), item_generator_script="", item_generator_script_language=hou.scriptLanguage.Python, menu_type=hou.menuType.Normal)
    t2.setJoinWithNext(True)
    t.addParmTemplate(t2)
    
    t2 = hou.IntParmTemplate("startEnd_"+n, "start/end", 2, default_value=([sf,ef]), min=-1, max=1, min_is_strict=False, max_is_strict=False, naming_scheme=hou.parmNamingScheme.XYZW, menu_items=([]), menu_labels=([]), icon_names=([]), item_generator_script="", item_generator_script_language=hou.scriptLanguage.Python, menu_type=hou.menuType.Normal)
    t2.setJoinWithNext(True)
    t2.setTags({"units": ""})
    t.addParmTemplate(t2)
    
    t2 = hou.ButtonParmTemplate("gotoBut_"+n, "goto")
    t2.setScriptCallback("hou.clearAllSelected();hou.node('"+node.path()+"').setSelected(1)")
    t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
    t2.setJoinWithNext(True)
    t.addParmTemplate(t2)
    
    t2 = hou.ButtonParmTemplate("exploreBut_"+n, "explore")
    path = os.path.dirname(os.path.dirname(node.evalParm('outputPath')))
    expression = "import os.path;os.system('xdg-open \"%s\"' % '"+path+"')"
    t2.setScriptCallback(expression)
    t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
    t2.setJoinWithNext(True)
    t.addParmTemplate(t2)
    
    t2 = hou.ButtonParmTemplate("renderBut_"+n, "render")
    t2.setScriptCallback("hou.node('"+node.path()+"').parm('render').pressButton()")
    t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
    t2.setJoinWithNext(True)
    t.addParmTemplate(t2)
    
    t2 = hou.ButtonParmTemplate("farmBut_"+n, "farm")
    
    t2.setScriptCallback("hou.node('"+node.path()+"').parm('sendToFarm').pressButton()")
    t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
    
    
    t.addParmTemplate(t2)
    pTG.append(t)
    return n

#variables and shit
sel = sorted(hou.selectedNodes(), key=getKey,reverse=True)

mianfiles = []
counter = -1

chkexpression = ''
unchkexpression = ''
renderselected = ''
farmselected = ''
setversion = ''

#build ctrl node
ctrl = sel[0].parent().createNode('null','mianRenderCtrl')
ctrl.setUserData('nodeshape', 'trapezoid_down')
ctrl.setColor(hou.Color((1,1,.1)))
ctrl.moveToGoodPosition(False)
ctrl.setSelected(True,True)

#spaghetti code 1
for node in sel:
    if node.parent().path() != '/obj':
        if node.type().name() == 'mianFile':
            counter += 1
            mianfiles.append([counter,node])
            
            c = str(counter)
            renderselected += "if hou.parm('checkbx_"+c+"').eval() == 1: hou.parm('renderBut_"+c+"').pressButton()\n"
            farmselected += "if hou.parm('checkbx_"+c+"').eval() == 1: hou.parm('farmBut_"+c+"').pressButton()\n"
            setversion += "if hou.parm('checkbx_"+c+"').eval() == 1: hou.parm('versBox_"+c+"').set(hou.parm('globversion').eval())\n"
            chkexpression += "hou.parm('checkbx_"+c+"').set(1);"
            unchkexpression += "hou.parm('checkbx_"+c+"').set(0);"
    
    else:
        for subnode in node.children():
            if subnode.type().name() == 'mianFile':
                counter += 1
                mianfiles.append([counter,subnode])
                
                c = str(counter)
                renderselected += "if hou.parm('checkbx_"+c+"').eval() == 1: hou.parm('renderBut_"+c+"').pressButton()\n"
                farmselected += "if hou.parm('checkbx_"+c+"').eval() == 1: hou.parm('farmBut_"+c+"').pressButton()\n"
                setversion += "if hou.parm('checkbx_"+c+"').eval() == 1: hou.parm('versBox_"+c+"').set(hou.parm('globversion').eval())\n"
                chkexpression += "hou.parm('checkbx_"+c+"').set(1);"
                unchkexpression += "hou.parm('checkbx_"+c+"').set(0);"
                

#spaghetti code 2 UI
hou_parm_template_group = hou.ParmTemplateGroup()

t = hou.FolderParmTemplate("standardfolder_1", "Mianfiles Render Tool", folder_type=hou.folderType.Simple, default_value=0, ends_tab_group=False)


t2 = hou.ButtonParmTemplate("chkAll", "Check All")
t2.setScriptCallback(chkexpression)
t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
t2.setJoinWithNext(True)
t.addParmTemplate(t2)

t2 = hou.ButtonParmTemplate("unchk", "Uncheck All")
t2.setScriptCallback(unchkexpression)
t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
t2.setJoinWithNext(True)
t.addParmTemplate(t2)

t2 = hou.ButtonParmTemplate("render", "Render Selected")
t2.setScriptCallback(renderselected)
t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
t2.setJoinWithNext(True)
t.addParmTemplate(t2)

t2 = hou.ButtonParmTemplate("farm", "Farm Selected")
t2.setScriptCallback(farmselected)
t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
t.addParmTemplate(t2)

t2 = hou.ButtonParmTemplate("setver", "Set version for checked:")
t2.setScriptCallback(setversion)
t2.setScriptCallbackLanguage(hou.scriptLanguage.Python)
t2.setJoinWithNext(True)
t.addParmTemplate(t2)

t2 = hou.IntParmTemplate("globversion", "Label", 1, default_value=([1]), min=0, max=10, min_is_strict=False, max_is_strict=False, naming_scheme=hou.parmNamingScheme.Base1, menu_items=([]), menu_labels=([]), icon_names=([]), item_generator_script="", item_generator_script_language=hou.scriptLanguage.Python, menu_type=hou.menuType.Normal)
t2.hideLabel(True)
t.addParmTemplate(t2)

hou_parm_template_group.append(t)

for subnodetuple in mianfiles:
    buildUiLine(subnodetuple[1],hou_parm_template_group,subnodetuple[0])
    
#build ui
deleteexpression = ''
if counter > -1:
    ctrl.setParmTemplateGroup(hou_parm_template_group)


    #lock stuff, setup connections
    for uiN in mianfiles:
        hou_parm_tuple = ctrl.parmTuple("startEnd_"+str(uiN[0]))
        hou_parm_tuple.lock((True, True))
        expression = "ch('"+ctrl.path()+"/versBox_"+str(uiN[0])+"')"
        uiN[1].parm('cacheVersion').setExpression(expression)
        #make sure to delete expressions when node is deleted
        deleteexpression += 'python -c \'hou.node("'+uiN[1].path()+'").parm("cacheVersion").deleteAllKeyframes()\'\n'
        
        
    ctrl.setDeleteScript(deleteexpression, hou.scriptLanguage.Hscript)
    
    group = ctrl.parmTemplateGroup()
    group.hideFolder('Standard', True)
    ctrl.setParmTemplateGroup(group)