Add New Functionality to Default Nodes With addOnCreate()

Categories Nuke, Python

I use the addOnCreate() function all the time to power up my Nuke nodes. The aim of this quick post is to demonstrate how you can use it in your own workflow to add new functionality to Nuke’s default nodes!

Our task for this quick tutorial is to add a “Set to Current Frame” button on our FrameHold node.

Here’s the code:

def customFrameHold():

    n = nuke.thisNode()
    n.addKnob(nuke.PyScript_Knob('setFrame', "Set to Current Frame", "{nuke.thisNode()['first_frame'].setValue(nuke.frame())}"))

nuke.addOnCreate(customFrameHold, nodeClass="FrameHold")

Let’s start with the last line of the code, which is the most important. nuke.addOnCreate is basically saying, “whenever X is created, run Y function, and restrict it to Z type (class) of node”. In our case, we want Z type of node to be a FrameHold node. Without the last bit, our function will be added to every single node, so it’s quite important to remember!

We’re creating a function called customFrameHold() and telling it to add a PyScript_Knob (if you Right-Click > Manage User Knobs on a node, then add a knob, this is the Python Script Button option). Then we’re simply setting 3 parameters. The first, setFrame, is a name that we can use to programmatically reference the button with Python later if we need to; second is, Set to Current Frame, which is the label that’s displayed on our button; and lastly, we’re adding a line of python that runs when the button is clicked, which will set the first_frame knob in our FrameHold node to the current frame.

Now, after adding this code to your and restarting Nuke, every time you create a FrameHold node, you’ll have our fancy new button displayed! Something to be aware of is, it is currently not possible to add your own knobs to the main tab of a node. Any time we programmatically add a new knob, a User tab is created for us… Hopefully this gets fixed in a future release of Nuke!

For a list of all the knobs available for you to create, you can reference Nuke’s Developer Guide.