Advertisement
mrhumbility

Connect Nodes

Jul 27th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import hou
  2.  
  3.  
  4. def main():
  5.     # first we make sure we are in an approved category by checking
  6.     # against one of the selected nodes
  7.     approvedCategories = ["Sop", "Driver", "Cop2", "Chop", "Vop", "Object",
  8.                           "Dop"]
  9.     if hou.selectedNodes():
  10.         node = hou.selectedNodes()[0]
  11.     else:
  12.         return
  13.     curCategory = node.type().category().name()
  14.     if curCategory in approvedCategories:
  15.         # if we are in vops we need to reverse sort and sort from
  16.         # the x pos not the y pos
  17.         if curCategory == "Vop":
  18.             direction = 0
  19.             r = True
  20.         else:
  21.             direction = 1
  22.             r = False
  23.  
  24.         # loop through the selected nodes and then sort by pos
  25.         positions = []
  26.         for aNode in hou.selectedNodes():
  27.             positions.append([aNode.position()[direction], aNode])
  28.         positions = sorted(positions, reverse=r)
  29.  
  30.         bottomNode = positions[0][1]
  31.         if curCategory == "Sop":
  32.             bottomNode.setDisplayFlag(True)
  33.             bottomNode.setRenderFlag(True)
  34.  
  35.         # now we loop through the sorted list and select
  36.         # which two nodes to connect
  37.         if len(positions) == 2:
  38.             nodeTwo = positions[1][1]
  39.             nodeOne = positions[0][1]
  40.             try:
  41.                 for aNode in nodeOne.inputs():
  42.                     if aNode is not None:
  43.                         if aNode.name() in nodeTwo.name():
  44.                             # not sure if return is the correct/ smartest way
  45.                             # to break out of the loop but break, pass, and
  46.                             # continue didnt work and return did so there it
  47.                             # is.
  48.                             return None
  49.                 nodeOne.setNextInput(nodeTwo)
  50.             except:
  51.                 nodeOne.setFirstInput(nodeTwo)
  52.         else:
  53.             for i in xrange(len(positions)-1):
  54.                 nodeTwo = positions[i+1][1]
  55.                 nodeOne = positions[i][1]
  56.  
  57.                 # if the curent node is a merge we can just append
  58.                 # the selection. We test to make sure that the connection
  59.                 # has not already been made though so we dont get double
  60.                 # geo. Otherwise we just set the first input.
  61.                 try:
  62.                     if nodeOne.type().name() == "merge":
  63.                         for aNode in nodeOne.inputs():
  64.                             if aNode is not None:
  65.                                 if aNode.name() in nodeTwo.name():
  66.                                     # not sure if return is the correct/
  67.                                     # smartest way to break out of the loop
  68.                                     # but break, pass, and continue didnt work
  69.                                     # and return did so there it is.
  70.                                     return None
  71.                         nodeOne.setNextInput(nodeTwo)
  72.                     else:
  73.                         nodeOne.setFirstInput(nodeTwo)
  74.                 except:
  75.                     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement