© 2014 by GATIMEDIA

PYTHON SNIPPETS

Introduction

Here you'll find tips and tricks in the form of useful python commands to help in Nuke.  You use them by entering the text in the examples into the Script Editor panel in Nuke either by typing it or just copy and paste.

 

Using python in a script editor is a powerful tool with many possible use.

Python itself is an interpreted, high-level, general-purpose programming language.

For more visit Wikipedia.

 

To make it more efficient for artists to use python in Nuke the Foundry developed `Nuke Python API` (API = Application Programming Interface) which is basically a bunch of specified commands (functions and modules) that Nuke can interpret and execute. You can find HERE.

 

If you feel like you are in need of a proper guidance into the world of Python in Nuke the Foundry has a Python Developers Guide.

Snippets with explanation

## Set selected Read`s Colorspace to AlexaLogC ##
 

nodes = nuke.selectedNodes()

for node in nodes:

    node.knob("colorspace").setValue(12)


 

## Select nodes that starts with a specific string ##
 

for node in nuke.allNodes():

    if node['name'].value().startswith("Multiply"):

        node['selected'].setValue(True)
 

#TIP: Type the name of the node you want to select where the `Multiply` is now.


 

## Changing values in selected nodes ##
 

for i in nuke.selectedNodes() :

    i['value'].setValue(2)
 

#TIP: Type the name of the knob you want to change where the `value` is now.

 

You can find the knob`s name if you hover over the mouse of it as

 

it sometimes not what appears on the node ( e.g.: Grade`s `gain` is called `white`)


 

## Changing values in selected specific class ##
 

for i in nuke.selectedNodes():

    nodeClass = i.Class()

    if nodeClass == 'Multiply':

        i.knob("value").setValue(5)
 

#TIP: You can find out the node`s class if you select one and hit `i` which bring out the information of

the node as the node`s class is not necessarily the node`s name ( e.g.: Merge`s class is `Merge2`)
 

## Removes all animation from selected nodes ##
 

for node in nuke.selectedNodes():

    for knob in node.knobs():

        if nuke.Knob.isAnimated(node[knob]):

            nuke.Knob.clearAnimated(node[knob])


 

## Removes all animation from selected nodes 2 ##
 

for a in nuke.selectedNode().knobs():

    nuke.selectedNode()[a].clearAnimated()


 

## Shows all dependencies of the selected node ##
 

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

    print a.name()
 

## Reload all Read nodes ##
 

for s in nuke.allNodes('Read'):

    s['reload'].execute()
 

## List knobs of selected node ##
 

print( nuke.selectedNode() )


 

## Delete all nodes that are not selected ##

 

s = nuke.selectedNodes()

b = nuke.allNodes()

for n in b:

    if n not in s:

        nuke.delete(n)

 

## Disable `postage stamps` in all nodes ##

 

for a in nuke.allNodes():

    try:

        a['postage_stamp'].setValue(0)

    except:

        pass

 

#TIP: As the checkbox knob uses boolean you can use (False) as (0) OR (True) as (1) for value.

#TIP: Every nodes has `postage stamp` knob so by setting the values True = 1 might affect nodes you didn`t intended to adjust.

 

## Disable `postage stamps`in multiple classes ##

 

nodes_classes = ["Read", "PostageStamp", "Constant", "ColorBars", "CheckerBoard2", "ColorWheel"]
 

for node in nuke.allNodes(group=nuke.root()):

    if node.Class() in nodes_classes:

        try:

            node["postage_stamp"].setValue(False)

        except Exception:

            pass


 

## Show Performance Timer ##
 

nuke.startPerformanceTimers()


 

## Hide Performance Timer ##
 

nuke.stopPerformanceTimers()
 

#TIP: Nuke`s performance profiling is a useful tool to find heavy nodes and errors in a script.

Find out more HERE.

 

## Adding $gui to selected nodes` `disable knob ##

 

selectedNode = nuke.selectedNode()

selectedNode["disable"].setExpression("$gui")

 

## Adding expression to a node in a Group ##

 

m = nuke.toNode('NodeName')

m.begin()

nuke.toNode('NodeInGroup').[filter].setExpression(frame')

m.end()

 

#TIP: You can use the setExpression function to add expression to knobs that manually don`t take expressions like `Transform/filter`

 

## Adding image to a node`s `icon` knob ##

nodes = nuke.selectedNodes()

img_path = '/Volumes/...'

for node in nodes:

    node.knob('icon').setValue(img_path)

 

#TIP: `icon` is a hidden knob of every nodes that is empty by standard

#TIP: If you want to remove the image you added change the ending of the code to ...setValue('')

## Links I recommend ##

Hope you will find it useful!