Tool Creation - Main Script - For Loop


Right let’s look at adding a simple for loop. We want to loop through all the objects at object level and see which ones we have selected. For the selected nodes we will set their material parameter to point at the dummy_shaders material.

We already have the variable node that represents /obj so we can use that with the .children() method to get all the nodes at object level. We will use the temp variable i which often represents an index in a for loop to temporarily store the different objects. Remembering to add a colon and indent as we move inside the loop. Python is a loosely typed language but indentation is particular important.

nodes = node.children()
for i in nodes:
        print i

Now we can loop through all the nodes let’s pop in a simple if statement to just print the selected ones. Here we can use the .isSelected() method.

nodes = node.children()
for i in nodes:
            if i.isSelected():
                        print  i

We could then quite simply add the dummy_shaders material into the object level material parameter for each of the selected nodes.

nodes = node.children()
for i in nodes:
            if i.isSelected()
            i.parm(‘shop_materialpath’).set(‘/obj/SHADING_CONTROL_PANEL/dummy_shaders’)

shadow

Wiith this for loop we can get immediate feedback that the selected node now has the material assigned to it. The openGL shading in Houdini shows a transparent box signifying the VEX shadowmatte material has been assigned.

Here is the script so far.

#simple script with looping


#   create a variable to hold /obj
node = hou.node('/obj')
#   create a shopnet and assign that shopnet to the variable shaders
shaders = node.createNode('shopnet', 'SHADING_CONTROL_PANEL')
#   create a material sop inside the shopnet
dummy_shaders = shaders.createNode('material', 'dummy_shaders', run_init_scripts=False)

#   add shadow and reflection materials
shadow = dummy_shaders.createNode('v_shadowmatte', 'shadow')
reflection = dummy_shaders.createNode("v_metal", "reflection")

#   refine the VEX Metal parameters for the desired reflective properties
reflection.parm('diffr').set(0)
reflection.parm('diffg').set(0)
reflection.parm('diffb').set(0)
reflection.parm('reflr').set(1)
reflection.parm('reflg').set(1)
reflection.parm('reflb').set(1)

#   create a sub output and switch
suboutput = dummy_shaders.createNode('suboutput')
switch = dummy_shaders.createNode('switch')

#   wire it all up
switch.setFirstInput(shadow)
switch.setNextInput(reflection)
suboutput.setFirstInput(switch)

#   tidy it all up
suboutput.moveToGoodPosition()
switch.moveToGoodPosition()
shadow.moveToGoodPosition()
reflection.moveToGoodPosition()


#   create a template for the drop down menu
template = hou.MenuParmTemplate("dummy_shaders", "Dummy Shaders", "01", ("shadow", "reflection",), 0)
#   add to the SHADING_CONTROL_PANEL
shaders.addSpareParmTuple(template, ("Shading",), True)
#   create relative references
switch.parm('input').setExpression('ch("/obj/SHADING_CONTROL_PANEL/dummy_shaders")')

#   create loop to find selected and add material
nodes = node.children()
for i in nodes:
    if i.isSelected():
        i.parm('shop_materialpath').set('/obj/SHADING_CONTROL_PANEL/dummy_shaders')


back to top