top of page

Code snippet: Setting multiple attributes based on node names

Ellie Ansell

A python script used for quickly setting attributes on nodes for the spider rig.



"""Desc
Gives attributes a value depending on criteria e.g. the node type, the node's name. 
Example usage: 
Select all nodes in the rig. The nodes of type BONE with name 'l_leg' have their 'display' attribute set to 'ch(../../set_display)'
"""
print "\n[matching nodes script]"

import fnmatch

def node_matches_criteria(node, name_fnmatch, node_type):
    """
    Returns true if the node matches the criteria.
    """
    if node_type in node.type().name():
        if fnmatch.fnmatch(node.name(),name_fnmatch):
            return True
    else:
        return False
          
def set_node(node, set_attr, attr_name, attr_mode, attr_value, 
            set_flag, flag_selectable, flag_display):
    """
    Sets the attr/ flag for the node.
    """
    if set_attr:
        if attr_mode == "str":
            node.parm(attr_name).setExpression(attr_value)
        elif attr_mode == "val":
            node.parm(attr_name).set(attr_value)
            
    if set_flag:
        node.setSelectableInViewport(flag_selectable)
        node.setDisplayFlag(flag_display)
        
    return True
    
def main():

    # Fetching parm attributes
    set_attr = hou.pwd().parm("set_attr").eval()
    set_flag = hou.pwd().parm("set_flag").eval()

    node_type = hou.pwd().parm("node_type").eval()
    node_name = hou.pwd().parm("node_name").eval()

    attr_name = hou.pwd().parm("attrib_name").eval()
    attr_mode_options = {0: "str", 1:"val"}
    attr_mode = attr_mode_options[hou.pwd().parm("attrib_type").eval()]
    attr_value = hou.pwd().parm("attrib_val").eval() if attr_mode=="val" else hou.pwd().parm("attrib_str").eval()
    print "[Debug] [attrs] name: {0}, mode: {1}, value: {2}, set: {3}".format(attr_name, attr_mode, attr_value, set_attr)
    
    flag_selectable = hou.pwd().parm("flag_selectable").eval()
    flag_display = hou.pwd().parm("flag_display").eval()
    print "[Debug] [flags] Selectable: {0}, Display: {1}, set: {2}".format(flag_selectable, flag_display, set_flag)
    
    # Main work here
    nodes = hou.selectedNodes()

    matching_nodes = [n for n in nodes if node_matches_criteria(n, node_name, node_type)]
    for node in matching_nodes:
        print "[Debug] Node: {0}".format(node)
        
        set_node(node, set_attr, attr_name, attr_mode, attr_value, set_flag, flag_selectable, flag_display)
        print "Matched node: %s\n" % n
main()




17 views0 comments

Recent Posts

See All

Comments


  • LinkedIn // Professional Bio
  • Facebook // ellieansellart page
  • Instagram // Informal fun
  • Vimeo

© 2020 by Ellie Ansell

Happily created using Wix.com

bottom of page