QUICK TIP #2: NUKE | Automatic Render Directories
Today I am going to walk you through how to set up Nuke so it will automatically create render directories. The files are ready to go just in case you cannot be troubled with reading through this walkthrough! If you do not want to waste your time you can download the project files (above) and follow the install text!
What you will need:
- Nuke 6 or greater ( I will be using Nuke 8 )
- Simple Text Editor ( I recommend Sublime but some like Notepad (Windows), GEdit (Linux), or TextEdit ( OSX )
Nuke may not provide us with a way to create directories automatically at render time but its does provide us with an amazing way to make our own custom auto-directory-create functionality.
Our steps are:
- Locate our .nuke folder
- Create three simple text files ( menu.py, init.py, callbacks.py )
- Create our first function to setup our write nodes.
- Create the callbacks using Nuke's Python API
- Create an RGBA default for our write nodes ( as an extra treat )
Locating our .nuke folder
When we launch Nuke, its scurries about our Hard Disk looking in specific locations to see if there is anything it should load. Things like our layouts, recent files list, plugins, etc. We can place files in these locations to control and manipulate Nuke's UI and Behaviour.
For us we want to use our local .nuke folder which lives ( by default ) in our Users Home Directory
If you do not see the folder, make sure you have 'show hidden file/folder' (or some version of that) turned on. If you have open Nuke at least once you will see this folder.
Inside this folder, you ( if you have not used it yet) you will see files like layout#.xml or recent_files. These are files nuke uses to save your settings for your username. Be careful not to delete them or you may lose you super awesome layouts you spent so long perfecting!
For more information about this click here!
Creating Our Simple Text Files
As mentioned above, when Nuke Launches it looks in the .nuke folder for some files. We can use these files nuke looks for to do some cool stuff. Create the following three text files.
menu.py - This python file is read when Nuke Launches in GUI Mode ( Graphic User Interface ) which is typically how we work with Nuke. You should use this file to do things that are specific to the GUI like, creating menus, changing colors, loading tools that are for GUI space only.
init.py - This python file is read everytime Nuke is launched ( GUI or Non-GUI ) This file loads before the menu.py. You should use it to do things that Nuke needs when not in GUI Mode like loading plugins, gizmos, or toolsets and making global callbacks and script you want available in Non-GUI mode.
callbacks.py - This file is not a file nuke looks for, this is going to be where we place our main code to do the work. We will tell nuke to load this file.
Create these three files!
For more information on the menu and init files click here
Creating Our Main Callback Function:
Open the callbacks.py script in a text editor. We are going to place some code in there! In this file place the following code, I explain it after ( or you can skip over that to the next section )
import nuke def write_callback(): """ Callback code for write style nodes """ # Setup Auto Path Creation node = nuke.thisNode() node['beforeRender'].setValue("f = os.path.split(nuke.thisNode().knob('file').value()); os.makedirs(f) if not os.path.exists(f) else None") return None
What this code says is, on Line 1 we import the nuke python module so we can use Nuke Python Modules. On Line 3 we define a function called "write_callback" which will be called later to perform the task of setting up the write(s). Next, on Line 8 you define a variable called "node" which we use to get the Node (Object). Finally, on Line 9, we set the nodes 'beforeRender' knob to have a value. This value is the key! What is does is checks for the file directory for the file path and attempts to create the folders if it is missing!
A bit about the 'beforeRender' knob, this maybe the first time you have seen this! But I assure you its always been there and it allows us to input a string ( line of words and letters) into this field and 'before the write node renders' that little string will be read as python code! I prefer to do it this way because if something stops working or it is failing on the farm I can delete this from the node and it will behave like a normal node. In a later Tutorial we will explore a more professional approach to this!
However, Save the callback file!
Create the Callback
Now lets create the working piece! Open the init.py file and type in:
import nuke from callbacks import write_callback nuke.addOnCreate(write_callback, nodeClass='Write') nuke.addOnCreate(write_callback, nodeClass='DeepWrite') nuke.addOnCreate(write_callback, nodeClass='WriteGeo')
Lets break it down!
import nuke from callbacks import write_callback
On these lines we import the nuke module. The "from callbacks import write_callback" simple says from the callbacks module ( that's callbacks.py) import the function called "write_callback".
nuke.addOnCreate(write_callback, nodeClass='Write') nuke.addOnCreate(write_callback, nodeClass='DeepWrite') nuke.addOnCreate(write_callback, nodeClass='WriteGeo')
The Next Lines are where the callback magic happens! nuke.addOnCreate() is a callback function Nuke has available that allows us to tie an action to the creation of a node. The functions allows to specify a 'class' or type of node, In our code, we limit our callback to the Write, DeepWrite, WriteGeo nodes! Lets explore this callback more:
nuke.addOnCreate( func, *args, **kwargs, nodeClass="*")
The callback takes up to four arguments (technically)
func - this is the function object ( instead of write_callback() we type write_callback which give its the func object)
*args - These are a like of positional argument the function func needs ( in our case we do not have these! So we can leave is blank)
**kwargs - These are a dictionary of keyword arguments that function func might use ( in our case we do not have these either!)
nodeClass - This is a keyword argument where we can define the nodeClass to apply to! By Default it is set to '*' which means all nodes. In our case that wont work so we specify our Write, DeepWrite, and WriteGeo types.
Cool now save this File! For more info on callback check out this!
Nuke's Write Nodes, by default, are set to RGB channels which drives me crazy! Instead! Lets make RGBA the default! Add this following code to your menu.py file (the file that we created above!) and BOOM! RGBA default for the Write node.
import nuke nuke.knobDefault( 'Write.channels', 'rgba' )
Thanks for Reading! If you have any questions put it in the comments!