Nuke Scripting

Nuke scripting is done in pyhton which is an interpreted dynamically typed language. So it will be translated on runtime no need for compiling to run. Besides that it looks at blank space so watch out. nuke python documentation python documentation

=Using the Script Editor= For scripting go click the button on the top left of the nuke pane. Go to windows-> Script Editor. In the script editor you can write and execute scripts.

Loading and Saving Scripts
On the tool bar are buttons for saving, importing and importing combined with executing. The scripts are saved as .py files in the custom scripts folder. Don't forget the extention! .pyc is another extentions that means python compiled. Despite python doesn't need compiling it can be done and compiled scripts are faster.

toolbar
At the top bar of the script editor are buttons for saving and importing (or importing and executing) scripts.

Put latest executed script in script editor
control + [ The brackets with control work like a undo redo options so you can go back and forth through executed scripts.

executing a script
control + [

clear output window
control + backspace

echo python commands
Preferences --> Script Editor --> echo python commands to output window Checking this will output the code of all the operations you do in nuke to the output window. This is useful for figuring out the python command for certain operations.

=User Input=

nuke.getInput
This command workst similar to the input pyhton command. Which doesn't work in nuke. This command will prompt a dialog box with the message that is declared between the brackets and a type field. The command return the user input. Example: var = nuke.getInput("werkt dit?") print(var) ja Here the user input was "ja"
 * 1) Result:

=Duplicate selected nodes= nukescripts.node_copypaste Where nukescripts is the model node_copypaste the function and the brackets indicate that it's a function.

=Selection=

Store selected Node
variable = nuke.selectedNode

Getting a value of a knob object from a selected node
nuke.selectedNode.knob.('knobName'.value) or nuke.selectedNode.knob.('knobName'.getValue)

Getting the Node object by its name
nuke.toNode('nodeName')

=Creating Nodes= After a node is created it's always selected. So when creating multiple nodes there always connected and aligned vertically by default.

Create node in a variable
When creating a node in a variable you give yourself the opportunity to call the node later in the script to do other operations on it. namevariable = nuke.createNode('classofthenode') Example frameholdcam = nuke.createNode('FrameHold')

=Node Connection=

setInput
This function will set the input of a node. a.setInput(b,c) a = is the node name. This can be a hard coded node name or a variable storing a node name. b = the input number, for example when a node only has one input this has to be 0. Or when you want to connect to the mask input for a grade for example this had to be 1. c = Where to connect the node to. If it shouldn't connect to anything you can put "None" in this field. Example Project3D1.setInput(0,FrameHold3)

=Comment= Piece of script that doesn't do anything and is just for clarification of the code. A comment can be made by starting the line with a # symbol.

=Changing Knobs=

Get the name of a knob
Hover your mouse over the knob you want to know the name of.

Set Value on a Knob
a.knob('b').setValue(c) a is the node name. This can be a hard coded node name or a variable storing a node name. b is the name of the knob you want to target. c is the value that the knob has to take. When it's a knob with a drop-down list it will pick according to the list number so 0 will be the first thing in the list and 1 the second etcetera. Example: FrameHold1.knob('first_frame').setValue(10)

for in loop
This is a python script to loop trough a piece of code until it's executed for everything within the loop. This can be used to change a knob on a lot of nodes at once: for i in nuke.selectedNodes: i.knob('Choose a knob here').setValue(Choose a value here) To change all the bounding boxes of multiple merges to B for example: for i in nuke.selectedNodes: i.knob('bbox').setValue(3)

Set expression on a knob
a.knob('b').setExpression(c) a is the node name. This can be a hard coded node name or a variable storing a node name. b is the name of the knob you want to target. c is the Expression that the knob has to take. Example frameholdcam.knob('first_frame').setExpression(frameholdimg.knob('name').value+'.knob.first_frame') frameholdimg.knob('name').value is the way of targetting a soft coded node from within the expression editor.

Change the color of a node
NodeName.knob('tile_color').setValue(colorcode) for the color of the locator use NodeName.knob('gl_color').setValue(colorcode)

Get the colorcode of a node
You can use the get value function to print the color code print nuke.selectedNode.knob('nameOfTheKnob').getValue Example print nuke.selectedNode.knob('tile_color').getValue

Change the label of a node
nodeName.knob('label').setValue('label text')

=Timeline=

Current Frame
To call the current frame: nuke.frame

=Scripts=

Change all bounding-boxes to B
for i in nuke.selectedNodes: i.knob('bbox').setValue(3)