Q100248: Adding Callbacks in Nuke

Follow

SUMMARY

This article will explain how to set up a callback in Nuke and how to find the correct Python commands to use for callbacks.

The full list of available callbacks and examples can be found in the Nuke Python Developer Guide for Nuke 10.0 or Nuke 10.5.

 

MORE INFORMATION

A callback is a piece of code added to the Nuke Environment that calls Python functions automatically when various events (such as creating a node or loading a script) happen in Nuke.

This is useful in many situations, for example to set preferences for knob values when a node is created, to automatically save incremental versions of a script or to change Project Settings when setting up a new project.

Below you can find an example of how to create a callback that will set the 'file type' of a Write node to exr and set the 'file' path to a temporary location, on every newly created Write nodes in Nuke.

 

To set up the callback, please take the following steps (please read on for more information on each step):

  1. Test the commands you want to use in Nuke's Script Editor to make sure they work correctly.
  2. Define your callback function and add it as a callback in a text file saved as init.py inside your .nuke folder.
  3. Next time you open Nuke, the callback will be executed when the specified event occurs.

 

1. Test the commands you want to use in Nuke's Script Editor to make sure they work correctly

To create a callback, it is often helpful to test the Python commands in the Script Editor inside a Nuke session first. You can find more information on the available Python API commands in the Nuke Python Developer Guide and the Nuke Python API Reference.

The names of the node’s knobs for scripting can usually be found in the label displayed when hovering over the knob.

For example, the 'file' knob in the Write node can be referenced for scripting using 'file', and the 'file type' knob is referenced for scripting via 'file_type':


To get the current value of a particular knob, for example the 'file type', run the following commands in the Script Editor:

node = nuke.toNode(“Write1”)
# replace Write1 with the name of
# your Write node if it is different

print node["file_type"].getValue() 

These will print the following to the console:

# Result: print node["file_type"].getValue()
# Result: 2.0

2.0 in this case is the index of ‘dpx’ in the dropdown list of file types.

To change the 'file type' to ‘exr’ via Python, enter the following command in the Script Editor:

node["file_type"].setValue(3)

This is one of the commands we want to add to our callback. The other one that sets the destination 'file' path to a temporary location looks like this:

node["file"].setValue("/tmp/test.####.exr")

 

2. Define your callback function and add it as a callback in a text file saved as init.py inside your .nuke folder 

Define all the actions you want your callback to perform in a single Python function and add this function as an OnUserCreate callback, as demonstrated in the example code below:

import nuke

def writeSettings():
node = nuke.thisNode()
node["file_type"].setValue(3)
node["file"].setValue("/tmp/test.####.exr")

# add callback to execute this every time a Write node is created
nuke.addOnUserCreate(writeSettings, nodeClass="Write")


Copy and paste this into a text editor, and save the file.

Rename the file to init.py and make sure file extensions are shown in your file browser when you do this, to verify that the file extension is .py and not .txt or different.


This file needs to be placed in your .nuke folder, which is the first location where Nuke will look for plug-ins at startup. Please refer to this article to locate your .nuke folder: Q100048: Nuke Directory Locations

 

3. Next time you open Nuke, the callback will be executed when the specified event occurs

After you’ve done this, the callback will be added to the Nuke environment and the next time you launch the application and create new Write nodes, these will have the 'file' and 'file type' settings applied.

NOTE: If you would like every node of a particular class to have specific settings applied, also those loaded in from older, saved scripts, you can use the nuke.addOnCreate callback instead of nuke.addOnUserCreate.

 

If you are encountering any issues in setting up your callbacks, then please open a Support ticket and let us know the issue you are encountering and the troubleshooting steps you have taken so far.

For more information on how to open a Support ticket, please refer to the 'Using the Support Portal' article.

Was this article helpful?
0 out of 0 found this helpful