1. Design & Illustration
  2. Graphic Design
  3. 3D Design

Write a Render Manager for Nuke using Python

Scroll to top
Read Time: 15 min

Learn how to write a custom render manager for Nuke using Python, allowing you to render one or more Nuke projects without needing to open the software.

1. Introduction

The purpose of this tutorial is to explain how to write a software that allows you to manage the rendering process in Nuke. You might have several Nuke comps that need to be rendered, so by using such a program you can render all of them at once without opening Nuke itself, that means the system is not loading Nuke's graphic interface so it can reserve more memory for the rendering process. Here you can see an example of the program you are going to build:

Graphic User Interface.
The program rendering three projects.

The program has a clear user interface that allows you to organize and queue as many renders as you need.

Requirements

In this tutorial I assume you have a basic understanding of Python and some dos commands. This software is meant to be run on the Windows operating system. The tools you will need are the following:

Python 2.x installed ( https://www.python.org ) Do not use the 3.x version because Nuke doesn't support it.

wxPython library ( http://www.wxpython.org ) This allows you to create a user interface. You might also use Tkinter, Qt, but this is not covered in this tutorial.

Software structure

We will call this software NukeRenderManager. The program is made of three files:

  • NukeRenderingManager.py

  • exeNuke.bat

  • Rendering.py

NukeRenderingManager.py: it contains everything about the graphic user interface and all the information regarding the location of the Nuke projects and all frame ranges.

exeNuke.bat: it is in charge of launching Nuke in terminal mode by passing through all the information coming from the NukeRenderingManager.py file. This file is called for each render, so if three Nuke comps need to be rendered, this file will be run three times.

Rendering.py: it gets all the information from exeNuke.bat and perform the rendering. This file is executed for each Nuke project.

2. Writing the NukeRenderingManager.py

Description

The NukeRenderingManager.py manages the user interface and organize the list of the projects to render.

The User Interface

To build our user interface we utilize the wxPython library. As I said before, you can use a different library but for the purpose of this tutorial, I will explain wxPython. To install it you just need to download the installer, launch it and everything is ready (you can find the link above). After the library is installed you need to start Python 2.x IDLE and this gives you the Python shell. From the File menu choose New File, now you have an empty editor. If you want you can use any other editor you might feel comfortable with. 

Empty Python Editor.

 Save the file as NukeRenderingManager.py and put it in any folder you want.

The first thing to do is importing the modules we need. The first is os that allows us to use the operating system functions, the second is the wx that is going to be useful to build a graphic user interface:

1
import os
2
import wx

We are going to create a window that contains all we need, so we achieve this goal by creating a custom class that is derived from wx.Frame:

1
Class mainWindow(wx.Frame):

Then we implement the constructor by calling the wx.Frame.__init__:

1
def __init__(self):
2
        
3
#constructor

4
wx.Frame.__init__(self,None,title="Nuke Rendering Manager",size=(600,300),style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX)

Then we create a status bar:

1
self.CreateStatusBar()

 We add a text control to show which Nuke projects are going to be processed:

1
# prepare the Nuke scripts list on screen

2
self.NukeScriptsList=wx.TextCtrl(self, style=wx.TE_MULTILINE)
3
self.NukeScriptsList.SetEditable(False)
4
self.NukeScriptsList.SetBackgroundColour((120,120,120))
5
self.NukeScriptsList.SetForegroundColour((50,255,50))
6
self.NukeScriptsList.SetValue('Nuke scripts:\n')

The wx.TextCtrl provide us an area where we can write the list, we need it as multiline so we declare wx.TE_MULTILINE. We don’t need it to be editable so we use SetEditable(False), then we define some colours and finally we show a text.

Then we create a render button:

1
# it creates the render button

2
self.RenderButton=wx.Button(self,label="Render",pos=(8,200))

 A very important thing is the sizer. The sizer allows us to define a layout, we will be using the BoxSizer that places elements horizontally and vertically, we choose a vertical placement for the text control and the button:

1
self.layout=wx.BoxSizer(wx.VERTICAL)
2
self.layout.Add(self.NukeScriptsList,1,wx.EXPAND)
3
self.layout.Add(self.RenderButton,0,wx.EXPAND)
4
self.SetSizer(self.layout)

The second parameter in the Add method is a numer that describes how mush space each element occupies, 0 means that the minimum size will be used, 1 means that space available will be occupied, in our case we want the button to minimized and the text control to have the remaining space.

We prepare some variables:

1
self.NukeScripts=[]
2
self.dirName=""
3
self.fileName=""

Then we prepare the menu. We start by creating a menubar as wx.MenuBar(), we create e menu called filemenu as wx.Menu(), we add the Add Nuke Scripts and Exit items and append them to the filmenu. And finally we append filemenu to menuBar:

1
# it creates menu items

2
menuBar=wx.MenuBar()
3
4
filemenu=wx.Menu()
5
addNukeScript=filemenu.Append(wx.ID_ANY,"Add Nuke script","Add Nuke script")
6
ClearList=filemenu.Append(wx.ID_ANY,"Clear list","Clear list")
7
exitEvt=filemenu.Append(wx.ID_EXIT,"Exit","Exit")
8
9
menuBar.Append(filemenu,"File")
10
self.SetMenuBar(menuBar)

wx.ID_ANY wx.ID_EXIT are used to provide an ID to the elements, in the first case we get an ID for the item, but in the second case we have a ID_EXIT that creates a special ID for the exit action.

The next step is to let these elements perform some operation, for that we use the wx.Bind function that allows us to bind the element to a specific function:

1
self.Bind(wx.EVT_MENU,self.onAdd,addNukeScript)

The first argument says that we are dealing with a menu event, the second calls the function that we want to link to this element and the third is the element itself. In this case is the addNukeScritp item in the menu. We still have to implement the self.onAdd function, we will do that later:

1
self.Bind(wx.EVT_MENU,self.onClearList,ClearList)

 The ClearList action is bound to the onClearList method:

1
self.Bind(wx.EVT_BUTTON,self.onRender,self.RenderButton)

Here we bind the self.RenderButton to the self.onRender function that we have to implement:

1
self.Bind(wx.EVT_MENU, self.onExit,exitEvt)

 Finally we assign the self.onExit function to the exitEvt element.

To complete the constructor we show the mainWindow:

1
# it shows the main window

2
self.Show(True)

So far we have our constructor:

1
import os
2
import wx
3
4
class mainWindow(wx.Frame):
5
6
    def __init__(self):
7
        
8
        #constructor

9
        wx.Frame.__init__(self,None,title="Nuke Rendering Manager",size=(600,300),style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX)
10
11
        # it creates a status bar

12
        self.CreateStatusBar()
13
14
        # prepare the Nuke scripts list on screen

15
        self.NukeScriptsList=wx.TextCtrl(self, style=wx.TE_MULTILINE)
16
        self.NukeScriptsList.SetEditable(False)
17
        self.NukeScriptsList.SetBackgroundColour((120,120,120))
18
        self.NukeScriptsList.SetForegroundColour((50,255,50))
19
        self.NukeScriptsList.SetValue('Nuke scripts:\n')
20
21
        # it creates the render button

22
        self.RenderButton=wx.Button(self,label="Render",pos=(8,8))
23
24
        # layout

25
        self.layout=wx.BoxSizer(wx.VERTICAL)
26
        self.layout.Add(self.NukeScriptsList,1,wx.EXPAND)
27
        self.layout.Add(self.RenderButton,0,wx.EXPAND)
28
        self.SetSizer(self.layout)
29
        
30
        #variables

31
        self.NukeScripts=[]
32
        self.dirName=""
33
        self.fileName=""
34
35
        # it creates menu items

36
        menuBar=wx.MenuBar()
37
38
        filemenu=wx.Menu()
39
        addNukeScript=filemenu.Append(wx.ID_ANY,"Add Nuke script","Add Nuke script")
40
        ClearList=filemenu.Append(wx.ID_ANY,"Clear list","Clear list")
41
        exitEvt=filemenu.Append(wx.ID_EXIT,"Exit","Exit")
42
43
        menuBar.Append(filemenu,"File")
44
        self.SetMenuBar(menuBar)
45
46
        # it binds elements to events

47
        self.Bind(wx.EVT_MENU,self.onAdd,addNukeScript)
48
        self.Bind(wx.EVT_MENU,self.onClearList,ClearList)
49
        self.Bind(wx.EVT_BUTTON,self.onRender,self.RenderButton)
50
        self.Bind(wx.EVT_MENU, self.onExit,exitEvt)
51
52
        # it shows the main window

53
        self.Show(True)
Snapshot of the editor.

Let’s have a look at the functions. The first thing I want to explain is onAdd that is executed whenever the menu event addNukeScript is called. The goal of this function is to add the Nuke scripts information on a list:

1
# it adds Nuke scripts on the list

2
    def onAdd(self,event):
3
        wildcard="Nuke scripts *.nk|*.nk"
4
        dlg=wx.FileDialog(self,message="Add Nuke script",wildcard=wildcard,style=wx.OPEN)
5
        if dlg.ShowModal()==wx.ID_OK:
6
            self.dirName=dlg.GetDirectory()
7
            self.fileName=dlg.GetFilename()
8
            self.NukeScripts.append(self.dirName+self.fileName)
9
            self.updateList()
10
        dlg.Destroy()

Because this function is called as an event occurs, as we define it we have to include an extra parameter that in this case we called event. We define a wildcard as a string, that is useful to guide users to what extension they must look for:

1
wildcard="Nuke scripts *.nk|*.nk"

A file open dialog is created and as the user clicks OK, we memorize the directory and the file name to our variables and we call updateList to update the screen:

1
if dlg.ShowModal()==wx.ID_OK:
2
            self.dirName=dlg.GetDirectory()
3
            self.fileName=dlg.GetFilename()
4
            self.NukeScripts.append(self.dirName+self.fileName)
5
            self.updateList()

The updateList method clears the screen, loops through the NukeScripts list and writes again on the screen:

1
#it updates the Nuke scripts list on screen

2
    def updateList(self):
3
        self.NukeScriptsList.Clear()
4
        for i in self.NukeScripts:
5
            self.NukeScriptsList.AppendText(i+"\n") 

The onClearList function clears the screen and the NukeScripts list:

1
def onClearList(self,event):
2
        self.NukeScriptsList.Clear()
3
        self.NukeScripts=[]

We have onRender() , that will be implemented in the next section, and the onExit function that closes the application:

1
# it starts the rendering process

2
    def onRender(self,event):
3
        print "Rendering..."
4
5
    # it closes the program

6
    def onExit(self,event):
7
        self.Close(True)

This is the mainWindow class definition, now we need to make an instance of it in order to see and use it, but first we have to create an wx.App object:

1
app=wx.App(False)

Then we create our mainWindow instance:

1
mainWindow=mainWindow()

Finally we need to call the MainLoop function to start the application:

1
app.MainLoop()

So at this point we have the NukeRenderingManager.py code except the onRender method that we are going to implement in the next section.

To make our program more robust I added a couple of lines to make some checks. As we load a Nuke script, it would be good if we check if the file extension is .nk, even if the wildcard filters our choice. We use the os.path.splitext function, then if the extension is .nk we proceed as normal:

1
#we check if we have a Nuke script

2
            self.extension=os.path.splitext(self.fileName)
3
            
4
            if self.extension[1]==".nk":    
5
                self.NukeScripts.append(self.dirName+self.fileName)
6
                self.updateList()

The os.path.splitext gives back a list with the name and the file extension at the [0] and [1] position. Since we are loading external files, it might be possible that some of them may be corrupted, so to increase the quality of our application we will handle the exceptions:

1
# it adds Nuke scripts on the list

2
    def onAdd(self,event):
3
        wildcard="Nuke scripts *.nk|*.nk"
4
        dlg=wx.FileDialog(self,message="Add Nuke script",wildcard=wildcard,style=wx.OPEN)
5
6
        try:
7
            if dlg.ShowModal()==wx.ID_OK:
8
                self.dirName=dlg.GetDirectory()
9
                self.fileName=dlg.GetFilename()
10
            
11
                #we check if we have a Nuke script

12
                self.extension=os.path.splitext(self.fileName)
13
            
14
                if self.extension[1]==".nk":    
15
                    self.NukeScripts.append(self.dirName+\\+self.fileName)
16
                    self.updateList()
17
        except:
18
            print "unable to read this file"
19
                
20
        dlg.Destroy()

 As you have noticed I have used self.NukeScripts.append(self.dirName+”\\”+self.fileName), I had to add “\\” because I found out that if any nuke script is located in c:\ it returns c:\, you have to add the \ manually.

Before the end of this section I want to mention that in order to make the entire system work, we should avoid placing the nuke scripts, the exeNuke.bat and the Rendering.py files in a folder that has a very long path. I have been testing the program and for some reason when this path is too long it doesn’t work, it might because the prompt is not able to handle such strings.

So our NukeRenderingManager.py is the following:

1
import os
2
import wx
3
4
class mainWindow(wx.Frame):
5
6
    def __init__(self):
7
        
8
        #constructor

9
        wx.Frame.__init__(self,None,title="Nuke Rendering Manager",size=(600,300),style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX)
10
11
        # it creates a status bar

12
        self.CreateStatusBar()
13
14
        # prepare the Nuke scripts list on screen

15
        self.NukeScriptsList=wx.TextCtrl(self, style=wx.TE_MULTILINE)
16
        self.NukeScriptsList.SetEditable(False)
17
        self.NukeScriptsList.SetBackgroundColour((120,120,120))
18
        self.NukeScriptsList.SetForegroundColour((50,255,50))
19
        self.NukeScriptsList.SetValue('Nuke scripts:\n')
20
21
        # it creates the render button

22
        self.RenderButton=wx.Button(self,label="Render",pos=(8,8))
23
24
        # layout

25
        self.layout=wx.BoxSizer(wx.VERTICAL)
26
        self.layout.Add(self.NukeScriptsList,1,wx.EXPAND)
27
        self.layout.Add(self.RenderButton,0,wx.EXPAND)
28
        self.SetSizer(self.layout)
29
        
30
        # variables

31
        self.NukeScripts=[]
32
        self.dirName=""
33
        self.fileName=""
34
35
        # it creates menu items

36
        menuBar=wx.MenuBar()
37
38
        filemenu=wx.Menu()
39
        addNukeScript=filemenu.Append(wx.ID_ANY,"Add Nuke script","Add Nuke script")
40
        ClearList=filemenu.Append(wx.ID_ANY,"Clear list","Clear list")
41
        exitEvt=filemenu.Append(wx.ID_EXIT,"Exit","Exit")
42
43
        menuBar.Append(filemenu,"File")
44
        self.SetMenuBar(menuBar)
45
46
        # it binds elements to events

47
        self.Bind(wx.EVT_MENU,self.onAdd,addNukeScript)
48
        self.Bind(wx.EVT_MENU,self.onClearList,ClearList)
49
        self.Bind(wx.EVT_BUTTON,self.onRender,self.RenderButton)
50
        self.Bind(wx.EVT_MENU, self.onExit,exitEvt)
51
52
        # it shows the main window

53
        self.Show(True)
54
55
    #it updates the Nuke scripts list on screen

56
    def updateList(self):
57
        self.NukeScriptsList.Clear()
58
        for i in self.NukeScripts:
59
            self.NukeScriptsList.AppendText(i+"\n")
60
61
    # it adds Nuke scripts on the list

62
    def onAdd(self,event):
63
        wildcard="Nuke scripts *.nk|*.nk"
64
        dlg=wx.FileDialog(self,message="Add Nuke script",wildcard=wildcard,style=wx.OPEN)
65
66
        try:
67
            if dlg.ShowModal()==wx.ID_OK:
68
                self.dirName=dlg.GetDirectory()
69
                self.fileName=dlg.GetFilename()
70
            
71
                #we check if we have a Nuke script

72
                self.extension=os.path.splitext(self.fileName)
73
            
74
                if self.extension[1]==".nk":
75
                    self.NukeScripts.append(self.dirName+"\\"+self.fileName)
76
                    self.updateList()
77
        except:
78
            print "unable to read this file"
79
                
80
        dlg.Destroy()
81
82
    def onClearList(self,event):
83
        self.NukeScriptsList.Clear()
84
        self.NukeScripts=[]
85
86
    # it starts the rendering process for each Nuke script

87
    def onRender(self,event):
88
            #to implement

89
	return
90
91
    # it closes the program

92
    def onExit(self,event):
93
        self.Close(True)
94
                        
95
96
app=wx.App(False)
97
mainWindow=mainWindow()
98
app.MainLoop()
Another snapshot of the editor.

3. Writing the exeNuke.bat file

Description

A bat file is recognized by the Windows operating system as a collection of commands. You can write any kind of command you want, you can also launch programs, and that is a feature we are going to use. If you are familiar with prompt instructions you will find this process easy.

 First of all you need to open an empty text file (I recommend Notepad), and save it as exeNuke.bat. As I mentioned in the previous section, we should avoid placing these files in a location that has a very long path, that because the prompt is not able to handle it, so place all the three files we are writing on your drive, with just a few subfolders, something like c:\NukeRenderingManager or c:\myProjects\NukeRenderingManager. 

That rule applies to the Nuke scripts as well, they might be located in a different place, but make sure that the path isn't too long.

Implementation

I want to briefly explain how Nuke works. We usually work in Nuke through its graphic user interface, but for some specific tasks we might want to run it in terminal mode. That means we only write commands to perform any usual operation, it looks like a Windows prompt:

The way we send instructions to Nuke in terminal mode is by writing some Python code. Let’s suppose you want to create a Blur node, you can type nuke.createNode(‘Blur’) and so on. What we are going to do is let the bat file open Nuke in terminal mode and start the render of a project, doing everything by sending commands and without any graphic user interface.

The first instruction are:

1
C:\
2
C:

 This is to make sure we can start typing the Nuke path in order to launch it:

1
cd Programmi\Nuke6.2v6
2
Nuke6.2 –t

Of course these lines might be different, write the location of your machine. The –t means terminal mode. If you double click on your exeNuke.bat file you should see Nuke in terminal mode. If you want to quit, just type quit() and hit Enter. In order to perform the rendering we also need to execute the Rendering.py file, so we can update our code:

1
cd\
2
c:
3
cd Programmi\Nuke6.2v6
4
Nuke6.2 -t c:\NukeRenderingManager\Rendering.py

By adding the location of the Rendering.py file, we ask to open Nuke in terminal mode and execute the Rendering.py that contains all the code to perform the rendering, and as I said before terminal mode requires the Python language, so we use the Rendering.py code. But we still need one piece of information, the Rendering.py file needs to know where the Nuke scripts are located. 

Remember that the exeNuke.bat and Rendering.py will be called for each Nuke script, so If we have to render three projects they will be launched three times. But each time they are called the Rendering.py needs to know where the scritp is located, to achieve this task we need to get this information from the above NukeRenderingManager.py.

Snapshot of the batch file editor .

Complete NukeRenderingManagerFile.py

The only method we need to implement is onRender(). What we do is loop through NukeScripts and call the bat file each time:

1
# it starts the rendering process for each Nuke script
2
    def onRender(self,event):
3
        for i in self.NukeScripts:
4
            os.system("C:/exeNuke.bat"+ " "+i)

We use the os.system function to execute the file. As you have noticed we also pass i as the argument after a space. We basically send the NukeScript path to the batch file. The fact that we can easily send this information to the batch file gives us a great flexibility.

Complete the exeNuke.bat file

The way a batch file gets arguments is by using the symbol % followed by a number, because we passed one information we will write %1. Here the complete code:

1
cd\
2
c:
3
cd Programmi\Nuke6.2v6
4
Nuke6.2 -t c:\Rendering.py %1

 We launch Nuke and we call the Rendering.py by giving it the path of the script as an argument.

Before I conclude this section I want to recap the process described until now. NukeRenderingManager.py provides us the graphic user interface and organizes the list of the Nuke scripts to be rendered. For each of the scripts exeNuke.bat and Rendering.py will be called. The first is in charge of running Nuke in terminal mode, grabbing the path of the script to be processed and passing it to the Rendering.py that will perform the render itself. Now we need to implement Rendering.py.

4. Writing the Rendering.py file

Implementation

The first thing we need to do is grab the path of the script we passed into the batch file. To accomplish this, we simply use the following statement sys.argv[1]. Then we transform this information in string:

1
prj=str(sys.argv[1])

The instruction to open a Nuke project is the following:

1
nuke.scriptOpen(prj)

Now we have the script ready to use. What we need to do now is look for the write node we want and render. In my example, the write node I need is called Write1, but you can use any name you want. Here is the complete code:

1
prj=str(sys.argv[1])
2
3
nuke.scriptOpen(prj)
4
5
for i in nuke.allNodes():
6
    if i.Class()=="Write":
7
        if i['name'].getValue()=="Write1":
8
            first_frame=nuke.Root().knob('first_frame').value()
9
            last_frame=nuke.Root().knob('last_frame').value()
10
            nuke.execute(i,first_frame,last_frame)

What we do is loop through all the nodes in the script, we check if the node is a write one, we control that the name is Write1, we get the first and the last frame of the project and we use the nuke.execute function to execute the render.

Snapshot of the rendering.py file.

Conclusion

To launch the program just double click on the NukeRenderingManager.py. Enjoy!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Design & Illustration tutorials. Never miss out on learning about the next big thing.
One subscription. Unlimited Downloads.
Get unlimited downloads