Jump to content

Arrange Nodes By Material Usage


Atom

Recommended Posts

Hi All,

 

I have been importing FBX files and often they have a lot of nodes but only a few materials. Most of the time I discard the imported materials and override with new ones. However, the standard FBX import offers no arrangement feature and I am left with a task of individually selecting each node and then clicking on the material tab to see what material, if any, is applied to each object node.

 

I wrote a short script that scans all geo based nodes at the /obj level and stacks them vertically based upon shared materials. Then I can simply select the vertical stack of nodes and override the materials for all those shared objects in one paste.

 

Before Script:

post-12295-0-50756300-1432907711_thumb.p

 

After Script:

post-12295-0-34766300-1432907723_thumb.p

 

Script: Place this code in a new shelf tool.

import hou
 
def returnShopPath(passed_name):
    result = None
    n = hou.node(passed_name)
    if n != None:
        result = n.parm("shop_materialpath").eval()
    return result
            
def childrenOfNode(node, filter):
    # Return nodes of type matching the filter (i.e. geo etc...).
    result = []
    if node != None:
        for n in node.children():
            t = str(n.type())
            if t != None:
                for filter_item in filter:
                    if (t.find(filter_item) != -1):
                        # Filter nodes based upon passed list of strings.
                        result.append('%s~%s' % (n.name(), t))
                    result += childrenOfNode(n, filter)
    return result
 
def uniquifyList(seq, idfun=None): 
    #http://www.peterbe.com/plog/uniqifiers-benchmark
    # f5 order preserving
   if idfun is None:
       def idfun(x): return x
   seen = {}
   result = []
   for item in seq:
       marker = idfun(item)
       # in old Python versions:
       # if seen.has_key(marker)
       # but in new ones:
       if marker in seen: continue
       seen[marker] = 1
       result.append(item)
   return result
 
# Construct paths.
node_path = '/obj'
x_spacing = 2
y_spacing = 1
 
# Get a list of all materials applied to geometry objects.
lst_geo_mat = []
nodes = childrenOfNode(hou.node(node_path),["Object geo"]) #Other valid filters are Sop, Object, cam.
for node in nodes:
    ary = node.split("~")
    if len(ary) > 0:
        node_candidate = "%s/%s" % (node_path, ary[0])
        n = hou.node(node_candidate)
        if n !=None:
            shop_path = returnShopPath(node_candidate)
            if shop_path != None:
                lst_geo_mat.append(shop_path)
 
# Scan the unique material list for matching /obj nodes and stack them up vertically.
unique_materials = uniquifyList(lst_geo_mat)
for (i,mat) in enumerate(unique_materials):
    if i%2==0:
        y = 0
    else:
        # Stagger vertical start so long names are still readable and do not collide horizontaly.
        y = 0.5
    for node in nodes:
        ary = node.split("~")
        if len(ary) > 0:
            node_candidate = "%s/%s" % (node_path, ary[0])
            n = hou.node(node_candidate)
            if n !=None:
                shop_path = returnShopPath(node_candidate)
                if shop_path == mat:
                    # This node should be vertically layed out with it's companions sharing this material.
                    node_loc = hou.Vector2((i*x_spacing,y*y_spacing))
                    n.setPosition(node_loc)
                    y += 1
                    
Edited by Atom
Link to comment
Share on other sites

you can as well always just pick material in Material Palette and click Select button to select all objects with that material, then you can change it to whatever you want or select another material and use Assign button

Link to comment
Share on other sites

Hmm...

 

Where is this select button located? I don't see one in the SHOP network area and the basic select tool has no options to select from material.

Using version 14.

 

post-12295-0-19189000-1433002455_thumb.j

Edited by Atom
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...