Basic Python and TCL

This is a basic tutorial looking at some of the most useful Python and TCL commands an artist may need when working with Nuke.

Working with menu.py

In your documents folder you have a hidden directory called .nuke This is automatically created when you start Nuke for the first time and is a custom folder for all your Nuke scripts, gizmos, icons, etc. In this folder you can create a menu.py file which is loaded every time Nuke starts. As we go through this section we’ll add the Python and TCL commands into the menu.py file so that next time we start Nuke everything will be set up correctly.

Adding Custom Hotkeys to Existing Nodes

In this example we’ll add a custom hotkey to the Reformat node. In order to do this we need to recreate the node itself and add a hotkey at the same time.

We first declare a variable toolbar = nuke.menu(‘Nodes’)
We add the command to a menu (already existing) toolbar.addCommand(‘Transform/Reformat’
Then we add the command to create the Node ‘nuke .createNode(“Reformat”)’
Finally we add the new hotkey, ctrl+r ‘ctrl+r’
The complete script looks like this:

toolbar = nuke.menu(‘Nodes’)
toolbar.addCommand(‘Transform/Reformat’, ‘nuke .createNode(“Reformat”)’, ‘ctrl+r’)

This script can be saved directly into your menu.py file so it is available each time you start Nuke.

Adding a Custom Toolbar

In this example we’ll add a new Toolbar and assign the EdgeBlur tool we made in the Gizmo’s section to it. We’ll also add icons to the new Toolbar and to the Gizmo we’re adding, finally we’ll again add a hotkey.

We first declare a variable customToolbar = nuke.toolbar(‘myToolbar’)
We then add a new Toolbar, which you’ll find under Pane, with an icon MattIcon.png myMenu=customToolbar.addMenu(‘Matt’, ‘MattIcon.png’)

Note, the icon must be 24 x 24 pixels in size, 8bit colour, and saved as a .png non-interlaced.

Then we add the command to create the menu item myMenu.addCommand(‘edgeBlurTool’
Then we add the command to add the gizmo to the menu ‘nuke.createNode(“edgeBlurTool”)’
Next we add the hotkey, alt+e ‘alt+e’
Finally we add the icon for the menu item icon=’edgeBlurTool.png’)
The complete script looks like this:

customToolbar= nuke.toolbar(‘myToolbar’)
myMenu=customToolbar.addMenu(‘Matt’, ‘MattIcon.png’)
myMenu.addCommand(‘edgeBlurTool’, ‘nuke.createNode(“edgeBlurTool”)’, ‘alt+e’, icon=’edgeBlurTool.png’)

This script can be saved directly into your menu.py file so it is available each time you start Nuke.

Changing Tool Settings

In this example, we’ll make some changes to existing Nodes so they function slightly differently from the way they do straight out of the box.

Firstly we’ll change the Roto node to start with the output set to Alpha and premultiply set to RGB.

nuke.knobDefault(‘Roto.output’, ‘alpha’)
nuke.knobDefault(‘Roto.premultiply’, ‘rgb’)

‘Roto.output’ is the node name, Roto, and then the knob name, output.
‘alpha’ is the new knob value.

Next we’ll change the Exposure node to start with the Adjust In set to Stops.

nuke.knobDefault(‘EXPTool.mode’, ‘Stops’)

Note, the Exposure node is in fact called EXPTool. To find out the true name for a node, select it in the Node Graph and use the hotkey I to bring up the Info Viewer. Also the knob name ‘Adjust in’, when hovered over, tells you it’s really called ‘mode’. Be aware of these issues!

Instead of writing ‘Stops’ you could also use the index 0, 1, 2, 3, etc. Stops is the first one so it would be 0.

nuke.knobDefault(‘EXPTool.mode’, ‘0’)

This script can be saved directly into your menu.py file so it is available each time you start Nuke.

Displaying Data from a Knob on the Node in the Node Graph

Sometimes it is useful to see what a node is doing. For this first example, we’ll change the Exposure tool to show what ‘Adjust in’ setting has been selected.

nuke.knobDefault(‘EXPTool.label’, ‘[value mode]’)

Note, the ‘label’ knob is found in the Node tab of every tool. Also this command is using TCL not Python.

This script can be saved directly into your menu.py file so it is available each time you start Nuke.
Adding Custom Formats

Instead of having to add new formats which you use regularly, these can be added automatically using the following commands.

nuke.addFormat(‘1024 576 PAL Widescreen’)
nuke.addFormat(‘2048 1024 LatLong_2k’)
nuke.addFormat(‘1024 512 Lat Long_1k’)
nuke.addFormat(‘512 256 Lat Long_.5k’)

You can also make a format your default format for every time Nuke starts – normally it’s set to 2K_Super_35(full-ap).

palwide = ‘1024 576 PAL Widescreen’
nuke.knobDefault(‘Root.format’, palwide)

This script can be saved directly into your menu.py file so it is available each time you start Nuke.

Creating Saved Layout Names under the Layout Menu

Instead of having your saved layouts as Restore Layout1, Restore Layout2, etc. they can be given custom names by using the following commands.

savedLayouts = menubar.addMenu(‘&Layout’)
savedLayouts.addCommand(‘Nuke Standard’, ‘nuke.restoreWindowLayout(1)’, ‘+F1’)
savedLayouts.addCommand(‘Animation’, ‘nuke.restoreWindowLayout(2)’, ‘+F2’)

This script can be saved directly into your menu.py file so it is available each time you start Nuke.

Updated menu.py

# Adding Custom Hotkeys to Existing Nodes
toolbar = nuke.menu(‘Nodes’)
toolbar.addCommand(‘Transform/Reformat’, ‘nuke .createNode(“Reformat”)’, ‘ctrl+r’)

# Adding a Custom Toolbar
customToolbar= nuke.toolbar(‘myToolbar’)
myMenu=customToolbar.addMenu(‘Matt’, ‘MattIcon.png’)
myMenu.addCommand(‘edgeBlurTool’, ‘nuke.createNode(“edgeBlurTool”)’, ‘alt+e’, icon=’edgeBlurTool.png’)

# Changing Tool Settings
nuke.knobDefault(‘Roto.output’, ‘alpha’)
nuke.knobDefault(‘Roto.premultiply’, ‘rgb’)
nuke.knobDefault(‘EXPTool.mode’, ‘Stops’)
nuke.knobDefault(‘EXPTool.label’, ‘[value mode]’)

# Displaying Data from a Knob on the Node in the Node Graph
nuke.addFormat(‘1024 576 PAL Widescreen’)
nuke.addFormat(‘2048 1024 LatLong_2k’)
nuke.addFormat(‘1024 512 Lat Long_1k’)
nuke.addFormat(‘512 256 Lat Long_.5k’)

# Adding Custom Formats
palwide = ‘1024 576 PAL Widescreen’
nuke.knobDefault(‘Root.format’, palwide)

# Creating Saved Layout Names under the Layout Menu
savedLayouts = menubar.addMenu(‘&Layout’)
savedLayouts.addCommand(‘Nuke Standard’, ‘nuke.restoreWindowLayout(1)’, ‘+F1’)
savedLayouts.addCommand(‘Animation’, ‘nuke.restoreWindowLayout(2)’, ‘+F2’)