swapOutNode v1.1
This location is for Registered Users Only.
Perhaps you need to login or register.
5.2, 6.0, 6.1, 6.2, 6.3 or later


Swap Out Node
This function was written per a request to mimic Nuke's native Ctrl + Shift + drag-and-drop node-replacement trick. It duplicates pretty closely, with one notable key difference (that I've noticed so far, anyway):
If you replace a node with multiple primary inputs (say, a Merge) with a single-primary-input node (say, a Grade), the node that was connected to the secondary pipe of the Merge (the A pipe by default) will now be connected to the "mask" input of the Grade node. This differs from the native functionality in that normally only primary inputs are taken into consideration, and node connection stops short of mask inputs.
I've been thinking of ways to get around this issue (.optionalInput() isn't a reliable solution unfortunately), but unless you're doing something really crazy, it shouldn't be a big deal.
It's pretty bare-bones so far, but if you have any ideas or run into any issues, let me know and I'll modify it.
Comments
def swapOutNode(tar getNode, newNode):
if not isinstance(targ etNode, nuke.Node) or not isinstance(newN ode, nuke.Node):
return False
sourcePos = (newNode.xpos() , newNode.ypos())
targetPos = (targetNode.xpo s(), targetNode.ypos ())
oldSel = []
if nuke.selectedNo des():
oldSel = [node for node in nuke.selectedNo des()]
nukescripts.cle ar_selection_re cursive()
inputNodes = []
outputNodes = []
inputNodes1 = []
outputNodes1 = []
for i in range(targetNod e.inputs()):
inputNodes.appe nd((i, targetNode.inpu t(i)))
for depNode in nuke.dependentN odes(nuke.INPUT S | nuke.HIDDEN_INP UTS, targetNode):
for i in range(depNode.i nputs()):
if depNode.input(i ) == targetNode:
outputNodes.app end((i, depNode))
depNode.setInpu t(i, None)
for ii in range(newNode.i nputs()):
inputNodes1.app end((ii, newNode.input(i i)))
for depNode in nuke.dependentN odes(nuke.INPUT S | nuke.HIDDEN_INP UTS, newNode):
for ii in range(depNode.i nputs()):
if depNode.input(i i) == newNode:
outputNodes1.ap pend((ii, depNode))
depNode.setInpu t(ii, None)
targetNode.setS elected(True)
newNode.setSele cted(True)
nuke.extractSel ected()
targetNode.setS elected(False)
newNode['xpos'] .setValue(targe tPos[0])
newNode['ypos'] .setValue(targe tPos[1])
targetNode['xpo s'].setValue(so urcePos[0])
targetNode['ypo s'].setValue(so urcePos[1])
for inNode in inputNodes:
newNode.setInpu t(inNode[0], inNode[1])
for outNode in outputNodes:
outNode[1].setI nput(outNode[0] , newNode)
for inNode in inputNodes1:
targetNode.setI nput(inNode[0], inNode[1])
for outNode in outputNodes1:
outNode[1].setI nput(outNode[0] , targetNode)
for node in oldSel:
node.setSelecte d(True)
return True
import swapOutNode
zzz.addCommand('swapOutNode', 'swapOutNode.swapOutNode()')
thanks
RSS feed for comments to this post