Advertisement
mrhumbility

Make Merge

Jul 27th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. import hou
  2.  
  3.  
  4. def main():
  5.     # well just use one of the selected nodes to get the pwd and
  6.     # make the merge
  7.     approvedCategories = ["Sop", "Cop2", "Driver", "Chop", "Dop"]
  8.     if hou.selectedNodes():
  9.         node = hou.selectedNodes()[0]
  10.     else:
  11.         return
  12.  
  13.     curCategory = node.type().category().name()
  14.  
  15.     nodeType = hou.ui.displayMessage(
  16.         "Merge or Switch?", buttons=("Merge", "Switch", "Cancel"))
  17.     if nodeType == 2:
  18.         return None
  19.  
  20.     # Ask if we want to gather the inputs
  21.     count = 0
  22.     for aNode in hou.selectedNodes():
  23.         if aNode.outputs():
  24.             count += 1
  25.     if count >= 1:
  26.         result = hou.ui.displayMessage(
  27.             "Gather connections?", buttons=("Yes", "No", "Cancel"))
  28.     else:
  29.         result = 1
  30.     if result == 2:
  31.         return None
  32.  
  33.     if curCategory in approvedCategories:
  34.         if nodeType == 1:
  35.             mergeNode = node.parent().createNode("switch", "switch")
  36.             mergeNode.setColor(hou.Color([1, 1, 0.4]))
  37.         else:
  38.             mergeNode = node.parent().createNode("merge", "merge")
  39.  
  40.         # We will get the lowest pos from the selected nodes so that
  41.         # we can place the merge below this. We will also take the
  42.         # average x pos to place them merge in the middle of the
  43.         # selection
  44.         xPositions = []
  45.         yPositions = []
  46.         nodes = []
  47.         for aNode in hou.selectedNodes():
  48.             nodes.append([aNode.position()[0], aNode])
  49.             xPositions.append(aNode.position()[0])
  50.             yPositions.append(aNode.position()[1])
  51.  
  52.         xPos = sum(xPositions)/float(len(xPositions))
  53.  
  54.         # If the nodes are right on top of each other, place the merge off to
  55.         # a side
  56.         if xPos == max(set(xPositions), key=xPositions.count):
  57.             xPos += 3
  58.  
  59.         yPos = min(yPositions)
  60.         mergeNode.setPosition(hou.Vector2(xPos, yPos-1.5))
  61.  
  62.         # Sort the nodes by their x pos so it connects them to the
  63.         # merge in the correct order
  64.         nodes.sort()
  65.  
  66.         # Pick up all the outputs from the nodes being merged
  67.         # and set the inputs on the merge and whatnot
  68.         for i in xrange(len(nodes)):
  69.             # Try to set the input. If there is some error we will
  70.             # just skip. This happens if one of the selected nodes
  71.             # is a rop node or a chopnet, something like that.
  72.             if result == 0:
  73.                 for outputs in nodes[i][1].outputs():
  74.                     x = 0
  75.                     for inputs in outputs.inputs():
  76.                         if inputs is not None:
  77.                             if nodes[i][1].name() == inputs.name():
  78.                                 outputs.setInput(x, mergeNode)
  79.                             x += 1
  80.             try:
  81.                 mergeNode.setNextInput(nodes[i][1])
  82.             except:
  83.                 pass
  84.  
  85.         # Set selections and display flags where proper
  86.         mergeNode.setCurrent(True, clear_all_selected=True)
  87.         if curCategory == "Sop":
  88.             mergeNode.setDisplayFlag(True)
  89.             mergeNode.setRenderFlag(True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement