Skip to main content
Tutorial

Nuke Python Snippets

A collection of nuke python commands that can be used as they are, or as a reference when writing your own code.

The main purpose of this page is to help compositors that already know python, but don’t code all day, to speed up their writing.  If you need proper guidance into the world of Python in Nuke the Foundry has a Python Developers Guide

For a more complete list of functions and modules that Nuke can interpret and execute, please refer to the Nuke Python API (Application Programming Interface).

Set project script frame range:

nuke.root()['first_frame'].setValue(1001)
nuke.root()['last_frame'].setValue(1100)

Set “Missing frames” to nearest on all selected Read and DeepRead nodes:

for n in nuke.selectedNodes():
  if n.Class() == 'Read' or n.Class() == 'DeepRead':
    n['on_error'].setValue(3)

Set frame range for all selected read nodes:

first = 1001
last = 1062

for n in nuke.selectedNodes('Read'):
    n['first'].setValue(first)
    n['last'].setValue(last)
    n['origfirst'].setValue(first)
    n['origlast'].setValue(last)

for n in nuke.selectedNodes('DeepRead'):
    n['first'].setValue(first)
    n['last'].setValue(last)
    n['origfirst'].setValue(first)
    n['origlast'].setValue(last)

Disable “postage stamps” on all nodes:

for a in nuke.allNodes():
  try:
    a['postage_stamp'].setValue(0)
  except:
    pass

Replace part of your file path in the selected Read nodes:

sn = nuke.selectedNodes()

for n in sn:
  path = n.knob('file').value()
  new_path = path.replace('%04d.exr','lt.%04d.exr')
  n.knob('file').setValue(new_path)

Check if a node exist:

if nuke.toNode(name) is not None:
    exists=True

#Or use "nuke.exists(name_of_node)"

Print inputs dependencies of a selected node:

for a in nuke.selectedNode().dependencies():
    print a.name()

Print inputs dependent of a selected node:

for a in nuke.selectedNode().dependent():
    print a.name()

 

Import all nodes from another script:

nuke.loadToolset("/home/user/Templates/script.nk")

Remove all animation from a selected nodes:

for a in nuke.selectedNode().knobs():
    nuke.selectedNode()[a].clearAnimated()

 

Progress bar with sub-process:

import time
import nuke

frames = 10
iterations = 10

task1 = nuke.ProgressTask("Frame")
task2 = nuke.ProgressTask("Iteration")

for f in range(frames):
  if task1.isCancelled():
    break
  percent1 = int(100*(float(f) / (frames-1)))
  task1.setProgress(percent1)
  task1.setMessage("Frame %s of %d" % (f, frames))
  time.sleep(0.2)
  for i in range(iterations):
    if task2.isCancelled():
      break
    percent2 = int(100*(float(i) / (iterations-1)))
    task2.setProgress(percent2)
    task2.setMessage("Iterations %s of %d" % (i, iterations))
    time.sleep(0.1)

del task1, task2

Copy selected node with connections:

node = nuke.selectedNode()
 
#clean selection
for n in nuke.selectedNodes():
    n.setSelected(False)
#copy
node.setSelected(True)
nuke.nodeCopy("%clipboard%")
node.setSelected(False)

#paste
copyNode = nuke.nodePaste("%clipboard%")
copyNode.setSelected(False)
nuke.show(copyNode)
 
#connection
for i in range(node.inputs()):
    copyNode.setInput(i, node.input(i))
    
#positions
copyNode['xpos'].setValue(node.xpos()+25)
copyNode['ypos'].setValue(node.ypos()+25)

Disable heavy nodes like – Defocus, VectorBlur, Convolve, oflow, TVIscale:

for s in nuke.allNodes():
    classTypes = ['Defocus' , 'VectorBlur', 'Convolve', 'oflow', 'TVIscale']
    for n in classTypes:
        if n in s.Class():
            s['disable'].setValue(1)

Change all Merge nodes with “mask” operation mask in bbox “intersection” and ones with “stensil” in bbox “B”:

n = nuke.allNodes()

for i in n:
    if  i.Class() == "Merge2":
        if i.knob("operation").value() == "mask":
            i.knob("bbox").setValue("intersection")
        elif i.knob("operation").value() == "stencil":
            i.knob("bbox").setValue("B")
        else:
            pass

Set all the merges to bbox B:

for n in nuke.allNodes('Merge2'):
  n['bbox'].setValue("B")

Disconnect all viewers at the start (To add on your Menu.py):

def disconnectViewers():
  nuke.selectAll()
  nuke.invertSelection()

  for n in nuke.allNodes():
    if n.Class() == "Viewer":
    n['selected'].setValue(True)

  nuke.extractSelected()
nuke.addOnScriptLoad(disconnectViewers)

Change to ‘hold’ instead of ‘black’ in all Read Nodes(for both before and after):

for n in nuke.selectedNodes('Read'):
    n['before'].setValue(0)
    n['after'].setValue(0)

Python inside “Text” node Section

Write down the integer of an user knob “num” value:

[python {int(nuke.thisNode().knob('num').value())}]

Return result of (Current frame – 1000):

[python {nuke.frame()-1000}]