Function that mostly duplicates Nuke's Ctrl + Shift + drag-and-drop replace-node functionality.
swapOutNode
Updated: 6 February 2012
Author: nathan.rusch
Compatible Nuke versions: 5.2 or later
Compatibility: Linux, Mac, Windows
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.
Update 1.1: Added getConnectedNodes function to simply return lists of a node's inputs and outputs.