Tutorial

Nuke Python Snippets

By 2020-10-28December 6th, 2020No Comments

Some usefull Python snippets for nuke…

Disable “postage stamps” on all nodes:

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

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()

 

Remove all animation from a selected nodes:

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

 

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())}]