Using the Command-line

This topic covers how to use the NUKE command line to execute Python scripts.

Running NUKE in Python Mode

NUKE can be executed in Python interpreter mode by using the -t argument. For example:

<Nuke-install-path>/Nuke13.0 -t
Nuke 13.0v1, 64 bit, built Mar  5 2021.
Copyright (c) 2021 The Foundry Visionmongers Ltd.  All Rights Reserved.
>>>

To execute a Python script at startup, use the script file name after the -t switch:

<Nuke-install-path>/Nuke13.0 -t <mypythonscript.py>

Using Command-line Arguments

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:

import sys, nuke
print('sys.argv: {}'.format(sys.argv))

Now run this from the command line to see what sys.argv contains:

<Nuke-install-path>/Nuke13.0 -t test.py Hello World!
Nuke 13.0v1, 64 bit, built Mar  5 2021.
Copyright (c) 2021 The Foundry Visionmongers Ltd.  All Rights Reserved.
sys.argv: ['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:

<Nuke-install-path>/Nuke13.0 -t test.py Hello World 1,2
WARNING: The command line argument ' 1,23' will be used as a Frame Range argument and will not be forward to the python sys.argv.
To define a frame range argument use the -F option.
Nuke 13.0v1, 64 bit, built Mar  5 2021.
Copyright (c) 2021 The Foundry Visionmongers Ltd.  All Rights Reserved.
sys.argv: ['test.py', 'Hello', 'World!']

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('sys.argv: {}'.format(sys.argv))
print('nuke.rawArgs: {}'.format(nuke.rawArgs))

Now execute this:

<Nuke-install-path>/Nuke13.0 -t test.py Hello World 1,2
WARNING: The command line argument ' 1,2' will be used as a Frame Range argument and will not be forward to the python sys.argv.
To define a frame range argument use the -F option.
Nuke 13.0v1, 64 bit, built Mar  5 2021.
Copyright (c) 2021 The Foundry Visionmongers Ltd.  All Rights Reserved.
sys.argv: ['test.py', 'Hello', 'World!']
nuke.rawArgs: ['./Nuke13.0', '-t', 'test.py', 'Hello', 'World!', '1,2']

Modifying Existing NUKE Scripts

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:

./Nuke13.0 replaceWritePaths.py <nuke_script> <new_nuke_script> <in_file_pattern> <new_file_pattern>
Where:
  • <nuke_script> is the script to read in.

  • <new_nuke_script> is the name to save the modified script to.

  • <in_file_pattern> is string to search for in all the write file paths.

  • <new_file_pattern> is the string to replace <in_file_pattern> in all the write file paths.

Executing Frame Ranges

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:

./Nuke13.0 -x convert.py myfiles.###.jpg myfiles.###.tif 1,5