Q100248: How to call Python functions automatically with Nuke's callbacks

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.

 

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 a callback, please take the following steps (please read on for more information on each step):

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


1. Test the commands 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 by 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())

This will print the following to the console:

# node = nuke.toNode("Write1")
# print(node["file_type"].getValue())
# Result: 2.0

In this case, 2.0 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 command that sets the destination "file" path to a temporary location looks like this:

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

 

2. Define your callback function, add it as a callback, and save it to init.py inside the .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 other.


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 Nuke opens, 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.


FURTHER HELP

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 How to raise a Support ticket article.

    We're sorry to hear that

    Please tell us why