A function and a panel interface to replace nodes in a script.
replaceNodes
Updated: 21 December 2010
Author: iva.busquets
Compatible Nuke versions: 6.0 or later
Compatibility: Linux, Mac, Windows
This is a convenience function and a Panel to replace all nodes of a certain class with new nodes of another class.
- User can choose to act on all nodes or selected nodes. If 'all nodes' is chosen, then you can also choose to act only at the root level or recursively go inside groups.
- Panel allows to specify more than 1 node class to be replaced. I.E. Replace all 'Grade', 'Add' and 'Multiply' nodes with 'ColorCorrect' nodes. To do so, just type the different node classes separated by a space.
- Panel will only allow valid node classes. After typing in the text knob, it will compare what you typed with a list of available node classes. If no match is found, that particular node class gets deleted from the text knob. For convenience, the panel will also try to produce a case-insensitive match if it doesn't find an exact match. I.E. 'ColorCorrect', 'colorcorrect', 'colorCorrect', etc... should always turn into the right node class inside the text knob.
- Panel gives a bit of info on how many nodes of the old class(es) were found, and in what context.
- After choosing all the options, hitting Replace! will replace all the targeted nodes with a new node of the new class that was specified. If old and new nodes have a different number of maximum inputs (ie. replacing a Merge2 node with a Blur node), the behaviour should be the same as replacing those nodes in the interface using Ctrl + Shift + Drag new node on top of the old one. This ensures the mask input will always be connected to the correct input, and that the new node won't try to use more inputs than it's capable of.
- Additionally, depending on the options chosen in the panel, every new node will try to copy knob values from the old node, and restore links on any other nodes that were referencing the old node, so that they now reference the new node. Check the tootips for those options (replace knobs, replace expressions and restore Links) to find more about them.
- Apart from the Panel, the main function behind it, replaceNodes(), is exposed if someone wants to use it in other python scripts, or expand it to handle different scenarios.
- Download replaceNodes.py, and copy it anywhere in your PYTHON_PATH or nuke.pluginPath
- To use any of the functions inside, just import it either in your menu.py or your init.py:
import replaceNodes
- From here on, you should be able to use the functions inside. There's 3 of them:
replaceNodes.launchReplaceNodesModalPanel()
replaceNodes.getAllNodesOfClass(...)
replaceNodes.replaceNodes(...)
Use help(functionName) on any of them for more info.
- To add the panel launcher to a menu item:
m = nuke.menu('Nuke') replaceMenu = m.addMenu('ReplaceNodes') replaceMenu.addCommand('Replace Nodes Panel', 'replaceNodes.launchReplaceNodesModalPanel()')
I haven't tested this too thoroughly, so any feedback is more than welcome. :)