26 Sep 2018 | Sujay
Nuke is Foundry’s node-based compositing application widely used for post-processing tasks. This tutorial will cover how to build a simple plugin for Nuke. I will be using Nuke 11.1v1 for this tutorial along with Visual Studio 2015.
Nuke has an NDK (Nuke Developer Kit) which allows you to write and compile C++ plugins. In order to start writing the plugin, we need to set up the project in Visual Studio
To get started, find the NDK directory in Nuke’s installation directory (C:\Program Files\Nuke11.1v1\Documentation\NDK). This is where you will be building your plugins.
The easiest way to set up the environment is to clone the existing example project and rename it to accordingly. In the NDK folder, you will find a project folder called vc10.
This folder contains an ExampleProject which we will start changing. But first, make a copy of the entire vc10 directory. It’s better to leave the original untouched and experiment on the clone.
Open the solution file (ExamplePlugin.sln) in Visual Studio 2015(might ask for administrator permission, give it). Build the project.
We don’t need our plugin to be called ExamplePlugin do we?? So, let’s give it a name. I will call mine MyNukePlugin.
In the solution explorer, I rename my ExamplePlugin.cpp file to MyNukePlugin.cpp. I will also rename my project to MyNukePlugin. Make sure the dll name and the plugin class name is same. In the source file, we need to find and replace all ExamplePlugin to MyNukePlugin.
If everything is good, then you should be able to compile the project and TA-DA, you should have your first plugin ready.
Test this on Nuke, go to Other –> All Plugins –> Update. Then look for ExamplePlugin in Other –> All Plugins and select your plugin. Connect the input node to an image and output to the viewer as shown here.
Connect the input node to an image and output to the viewer as shown here. You should see that the image in the viewer turns into a grayscale image.
Congratulations! you have built your first Plugin for Nuke. Let us look into the code and see whats happening.
The following bit of code is a for including the necessary libraries for the plugin and defining the class. The class basically inherits from Iop class. Since our plugin is based on the 2D architecture of the Nuke, this is the class we inherit from. You can read more about 2D architecture from here.
Since our class inherits Iop, we need to define 3 virtual functions that are: _validate(), _request(), and engine().
The execution order goes from _validate() –> _request() –> engine().
In the following code, we update a bit about how Nuke knows the plugin. We can define the name of the plugin and description/help here.
The following code has 2 functions, _validate() and _request().
_validate() handles, validating the input with in a bounding box(bbox).
_request() is called by NUKE on your Iop to indicate the region and channels of interest. We can request multiple inputs in this function.
Both these functions are run on the main thread hence should avoid performing any time-consuming operations.
The magic happens here in this functions. Any modification that needs to be done for the image needs to be performed here. This thread runs on worker threads and hence we need to ensure that the code inside this is thread safe.
This function operates on each row of the image. We declare a row, inputRow of width ‘r’. We then get the data to the inputRow using the function inputRow.get(). Here we can request the data that is necessary by specifying the channels and length of the row.
We then traverse through each pixel of the row and perform any necessary operations here. In our case, we need to convert the pixel into a grayscale pixel. For each pixel we traverse, we add the values in all the channels and divide by the number of channels(in this case 3, because we are only interested in RGB values of the image) to get the grayscale values. We write that to the outRow that is passed into the function.
The complete code is available in the following gist.
This is one simple method to get started with Nuke Plugins. This post doesn’t cover all the necessary concepts for the plugin development. If you need to develop more advanced plugins please refer to Nuke’s NDK documentation.