Last active
January 12, 2019 23:41
-
-
Save BenMcEwan/9cd0b41d5f80996a1b48fa34ade3da00 to your computer and use it in GitHub Desktop.
Instances Read & Camera nodes in Nuke, so you don't have to risk cloning and corrupting your script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Don't forget to add "import nodeInstancer" in your menu.py | |
import nuke | |
def readInstancer(): | |
# Save original node type to variable and deselect, so that multiple new instances don't daisy chain upon creation. | |
originalNode = nuke.selectedNode() | |
originalNodeClass = originalNode.Class() | |
originalNode['selected'].setValue(0) | |
origXpos = originalNode['xpos'].value() | |
origYpos = originalNode['ypos'].value() | |
# Create Postage Stamp | |
nodeInstance = nuke.createNode('PostageStamp') | |
# Hook up the input of our instance to the original node | |
nodeInstance.setInput(0, originalNode) | |
# Set instance name & node colour, then hide it's input | |
nodeInstance['tile_color'].setValue(int('%02x%02x%02x%02x' % (50, 50, 50, 1), 16)) | |
nodeInstance.setName(originalNode['name'].value() + " INSTANCE") | |
nodeInstance['hide_input'].setValue(1) | |
# Force instance position to be below + off to the right a tad for clear visibility | |
nodeInstance['xpos'].setValue(origXpos + 100) | |
nodeInstance['ypos'].setValue(origYpos + 150) | |
# If it doesn't already exist, create new tab, then create a disabled file knob that shows the original file path. At IE, this will be blank for Cameras. | |
nodeInstance.addKnob(nuke.Tab_Knob('InstanceTab', "Instance")) | |
nodeInstance.addKnob(nuke.File_Knob('origFile', "Original File")) | |
nodeInstance['origFile'].setValue(originalNode['file'].value()) | |
nodeInstance['origFile'].setEnabled(False) | |
# Create Python script button, that jumps to the original. | |
nodeInstance.addKnob(nuke.PyScript_Knob('showOriginal', "Show Original Node", "origNode = nuke.thisNode().input(0)\norigXpos = origNode.xpos()\norigYpos = origNode.ypos()\nnuke.zoom(2, [origXpos,origYpos])\nnuke.thisNode()['selected'].setValue(False)\norigNode['selected'].setValue(True)\nnuke.show(origNode)")) | |
nodeInstance['showOriginal'].setFlag(0x1000) | |
# If it doesn't already exist, add instance tab to the original node | |
if originalNode.knob('InstanceTabOrig'): | |
return | |
else: | |
originalNode.addKnob(nuke.Tab_Knob('InstanceTabOrig', "Instance")) | |
# Add buttons to original node to show/hide viewer inputs, and de-instance everything. | |
originalNode.addKnob(nuke.PyScript_Knob('showHideInputs', "Show / Hide Hidden Inputs", "n = nuke.thisNode()\nchildren = n.dependent(nuke.INPUTS | nuke.HIDDEN_INPUTS)\nallinstances = []\n\nfor i in children:\n if i.Class() =='PostageStamp' or i.Class() == 'Scene':\n allinstances.append(i)\n\nfor x in allinstances:\n if x['hide_input'].value() == 1:\n x['hide_input'].setValue(0)\n else:\n x['hide_input'].setValue(1)")) | |
originalNode.addKnob(nuke.PyScript_Knob('deinstanceNodes', "De-Instance Connected Nodes", "import nuke\n\ndef duplicate_node(node, to_file = None):\n\n orig_selection = nuke.selectedNodes()\n\n [n.setSelected(False) for n in nuke.selectedNodes()]\n node.setSelected(True)\n\n if to_file is not None:\n nuke.nodeCopy(to_file)\n [n.setSelected(False) for n in orig_selection]\n return\n\n\n nuke.nodeCopy('%clipboard%')\n node.setSelected(False)\n\n if to_file is None:\n nuke.nodePaste('%clipboard%')\n new_node = nuke.selectedNode()\n [n.setSelected(False) for n in nuke.selectedNodes()]\n [n.setSelected(True) for n in orig_selection]\n\n return new_node\n\n\n\n\n\n\n\n\n\nn = nuke.thisNode()\nchildren = n.dependent(nuke.INPUTS | nuke.HIDDEN_INPUTS)\nallinstances = []\n\nfor i in children:\n if i.Class() == 'PostageStamp' or i.Class() == 'Camera2':\n allinstances.append(i)\n\nn.removeKnob(n.knobs()['showHideInputs'])\nn.removeKnob(n.knobs()['deinstanceNodes'])\nn.removeKnob(n.knobs()['InstanceTabOrig'])\n\nfor x in allinstances:\n instanceChildren = x.dependent(nuke.INPUTS | nuke.HIDDEN_INPUTS)\n instanceChildrenList = []\n\n newNode = duplicate_node(n)\n newNode['xpos'].setValue(x['xpos'].value())\n newNode['ypos'].setValue(x['ypos'].value())\n\n for g in instanceChildren:\n instanceChildrenList.append(g)\n\n for k in instanceChildrenList:\n k.setInput(0, newNode)\n\n nuke.delete(x)\n\nnuke.message(str(len(allinstances))+' instances converted to their original nodes')")) | |
def cameraInstancer(): | |
# Save original node type to variable and deselect, so that multiple new instances don't daisy chain upon creation. | |
originalNode = nuke.selectedNode() | |
originalNodeClass = originalNode.Class() | |
originalNode['selected'].setValue(0) | |
origXpos = originalNode['xpos'].value() | |
origYpos = originalNode['ypos'].value() | |
# Create Postage Stamp | |
nodeInstance = nuke.createNode('Camera2') | |
# Expression-link knobs | |
originalNodeName = originalNode.name() | |
for j in nodeInstance.knobs(): | |
nodeInstance[j].setExpression( originalNodeName + '.' + j ) | |
nodeInstance['xpos'].setExpression('') | |
nodeInstance['ypos'].setExpression('') | |
nodeInstance['selected'].setExpression('') | |
nodeInstance['tile_color'].setExpression('') | |
# Set instance name & node colour | |
nodeInstance['tile_color'].setValue(int('%02x%02x%02x%02x' % (50, 50, 50, 1), 16)) | |
nodeInstance.setName(originalNode['name'].value() + " INSTANCE") | |
# Force instance position to be below + off to the right a tad for clear visibility | |
nodeInstance['xpos'].setValue(origXpos + 100) | |
nodeInstance['ypos'].setValue(origYpos + 150) | |
# If it doesn't already exist, create new tab. | |
nodeInstance.addKnob(nuke.Tab_Knob('InstanceTab', "Instance")) | |
# Create Python script button, that jumps to the original. | |
nodeInstance.addKnob(nuke.PyScript_Knob('showOriginal', "Show Original Node", "origNode = nuke.thisNode().dependencies(1)[0]\norigXpos = origNode.xpos()\norigYpos = origNode.ypos()\nnuke.zoom(2, [origXpos,origYpos])\nnuke.thisNode()['selected'].setValue(False)\norigNode['selected'].setValue(True)\nnuke.show(origNode)")) | |
nodeInstance['showOriginal'].setFlag(0x1000) | |
# If it doesn't already exist, add instance tab to the original node | |
if originalNode.knob('InstanceTabOrig'): | |
return | |
else: | |
originalNode.addKnob(nuke.Tab_Knob('InstanceTabOrig', "Instance")) | |
# Add button to original node to de-instance everything. | |
originalNode.addKnob(nuke.PyScript_Knob('deinstanceNodes', "De-Instance Connected Nodes", "import nuke\n\ndef duplicate_node(node, to_file = None):\n\n orig_selection = nuke.selectedNodes()\n\n [n.setSelected(False) for n in nuke.selectedNodes()]\n node.setSelected(True)\n\n if to_file is not None:\n nuke.nodeCopy(to_file)\n [n.setSelected(False) for n in orig_selection]\n return\n\n\n nuke.nodeCopy('%clipboard%')\n node.setSelected(False)\n\n if to_file is None:\n nuke.nodePaste('%clipboard%')\n new_node = nuke.selectedNode()\n [n.setSelected(False) for n in nuke.selectedNodes()]\n [n.setSelected(True) for n in orig_selection]\n\n return new_node\n\n\n\n\n\n\n\n\n\nn = nuke.thisNode()\nchildren = n.dependent(nuke.EXPRESSIONS)\nallinstances = []\n\nfor i in children:\n if i.Class() == 'Camera2':\n allinstances.append(i)\n\nn.removeKnob(n.knobs()['deinstanceNodes'])\nn.removeKnob(n.knobs()['InstanceTabOrig'])\n\nfor x in allinstances:\n instanceChildren = x.dependent(nuke.EXPRESSIONS)\n instanceChildrenList = []\n\n newNode = duplicate_node(n)\n newNode['xpos'].setValue(x['xpos'].value())\n newNode['ypos'].setValue(x['ypos'].value())\n\n for g in instanceChildren:\n instanceChildrenList.append(g)\n\n for k in instanceChildrenList:\n k.setInput(0, newNode)\n\n nuke.delete(x)\n\nnuke.message(str(len(allinstances))+' instances converted to their original nodes')")) | |
def nodeInstancer(): | |
originalNode = nuke.selectedNode() | |
originalNodeClass = originalNode.Class() | |
# If the selected node is a Read node, create a Read instance. | |
if originalNodeClass == 'Read': | |
readInstancer() | |
# If the selected node is a Camera, and not an instance of another Camera, we want to create a camera instance. | |
elif originalNodeClass == 'Camera2' and originalNode['tile_color'].value() != (int('%02x%02x%02x%02x' % (50, 50, 50, 1), 16)): | |
cameraInstancer() | |
# If the selected node isn't a Read or Camera node, throw up an error message. | |
else: | |
nuke.message("Only Read and Camera nodes may be instanced.") | |
# Add a menu item under the Edit menu, and assign meta+i as the hotkey | |
nuke.menu("Nuke").addCommand("Edit/Node Instancer", 'nodeInstancer()', 'meta+i') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment