This topic covers how to use the NUKE command line to execute Python scripts.
NUKE can be executed in Python interpreter mode by using the -t argument. For example:
/Applications/Nuke6.3v1/Nuke6.3v1.app/Nuke6.3v1 -t
NUKE 6.3v1, 64 bit, built Jul 4 2011.
Copyright (c) 2011 The Foundry Visionmongers Ltd. All Rights Reserved.
Using "nukebeta" licence.
>>>
To execute a Python script at startup, use the script file name after the -t switch:
./Nuke6.3v1 -t <mypythonscript.py>
When NUKE runs a Python script, it passes the script arguments to the script in sys.argv.
For example, first make a file called test.py containing the following:
## make a file called test.py that contains the following
import sys, nuke
print sys.argv
Now run this from the command line to see what sys.argv contains:
./Nuke6.3v1 -t test.py Hello World
NUKE 6.3v1, 64 bit, built Jul 4 2011.
Copyright (c) 2011 The Foundry Visionmongers Ltd. All Rights Reserved.
Using "nukebeta" licence.
['test.py', 'Hello', 'World']
As you can see, NUKE passes the arguments after -t to the Python interpreter.
The only exception to this is if the last argument is a number or frame range. For example:
./Nuke6.3v1 -t test.py Hello World 1,20
In this case, the 1,20 is stripped from sys.args and the frame range is available in nuke.tcl(‘frames all’).
To access all the command-line arguments passed to nuke you can use:
nuke.rawArgs
For example, edit the test.py to contain the following:
import sys, nuke
print nuke.rawArgs
Now execute this:
./Nuke6.3v1 -t test.py Hello World 1,2
NUKE 6.3v1, 64 bit, built Jul 4 2011.
Copyright (c) 2011 The Foundry Visionmongers Ltd. All Rights Reserved.
Using "nukebeta" licence.
['/Applications/Nuke6.3v1b37/Nuke6.3v1b37.app/Nuke6.3v1b37', '-t', 'test.py', 'Hello', 'World', '1,2']
It is sometimes useful to modify existing NUKE scripts using Python, for instance, when rewriting file paths.
The following script opens up a NUKE script on the command-line, rewrites all the write file paths, and finally saves the script:
### save this file as replaceWritePaths.py
import nuke
import os
import sys
def RecursiveFindNodes(nodeClass, startNode):
if startNode.Class() == nodeClass:
yield startNode
elif isinstance(startNode, nuke.Group):
for child in startNode.nodes():
for foundNode in RecursiveFindNodes(nodeClass, child):
yield foundNode
if len ( sys.argv ) != 5:
print 'Usage: NUKE replaceWritePaths.py <nuke_script> <new_nuke_script> <in_file_pattern> <new_file_pattern>'
sys.exit(-1)
inScript = sys.argv[1]
outScript = sys.argv[2]
inPattern = sys.argv[3]
outPattern = sys.argv[4]
nuke.scriptOpen( inScript )
allWriteNodes = [w for w in RecursiveFindNodes('Write', nuke.root())]
for write in allWriteNodes:
path = write['file'].value()
path = path.replace( inPattern, outPattern )
write['file'].setValue( path )
nuke.scriptSave( outScript )
This example has the following syntax when run:
./Nuke6.3v1 replaceWritePaths.py <nuke_script> <new_nuke_script> <in_file_pattern> <new_file_pattern>
When running Python with the -t switch, NUKE does not render or execute any nodes by default.
To render a node you need to call:
nuke.execute( nodeName, firstFrame, lastFrame )
However, when you use the -x switch to render a frame range and you supply a Python file (.py) instead of a NUKE (.nk) script, NUKE executes the Python first and then renders all the writes in the script with the given frame range.
For example, save this script as convert.py:
import sys
import nuke
r = nuke.nodes.Read(file = sys.argv[1])
w = nuke.nodes.Write(file = sys.argv[2])
w.setInput(0, r)
Now, as an example, run the script from the command line as shown to convert some jpegs to tifs for 5 frames:
Nuke6.3v1 -x convert.py myfiles.###.jpg myfiles.###.tif 1,5