Nuke TCL/Python Snippets

Nuke TCL/Python Snippets

Helpful documentation

Nuke Python Developer’s Guide
Nuke Python API
Knob Types
Some Flags

TCL:

Get input node name:

[value this.input0.name]


Set a variable in expression with TCL:

[set VARIABLENAME VALUE; return]

To use this variable later needs to type it as:

$VARIABLENAME

TCL String Operation that returns the 5th character from top input node name:

[string index [value [topnode this.input0].name] 5]

Use TCL string operation to converts to uppercase or lowercase:

[string tolower [value]]

Check if anything is plugged in to an input:

[exists this.input0]]
if nuke.thisNode().input(0) 

Check input1 and check if anything is plugged in:

[value [topnode this.input0].name]
[exists parent.[string tolower [value [topnode this.input1].name]]]

This handy getting filename from topmost read I grabbed from Nukepedia

[lindex [split [lindex [split [knob [topnode].file] .] 0] /] end]
[file dirname [knob [topnode].file]]/[lindex [split [lindex [split [knob [topnode].file] .] 0] /] end]_conv.%04d.exr 

Get nuke script base name without file extension

[lindex [split [file rootname ] /] end]

Read metadata with TCL

[metadata input/filename]

Using expressions in Text node (needs to square bracket the whole thing):

[expression frame==1?1:0]

Get Bezier1’s point 0 (which is first point) position in Roto1 node :

[value Roto1.curves.Bezier1.curve_points.0.main]

Python:

Get the last letter/section from node name, separated by “_”:

nuke.thisNode().name().rsplit("_").__getitem__(2)

From the above, can be modified to acquire file extension

nuke.thisNode()['file'].getValue().rsplit(".").__getitem__(2)

Set expression with python found here, especially useful when setting expression to a dropdown menu which is usually not accessible

nuke.selectedNode()['antialiasing'].setExpression('$gui? 0:3')

List of all nodes but exclude selected ones:

n = nuke.allNodes()
for i in nuke.selectedNodes():
    n.remove(i)
    return n

Replace frame paddings (%04d) and extension with .mov

import re
v = nuke.toNode('Read1')['file'].value()
print re.sub(r'%.*d.exr', 'mov', v)

Search for frame paddings

f = nukescripts.replaceHashes(nuke.filename(n))
padd = re.search(r'%.*d', f) print padd.group(0)

Check if program exist from python script, from here

shutil.which

It was introduced in Python 3.3 and is cross-platform, supporting Linux, Mac, and Windows. It is also available in Python 2.x via whichcraft.

def is_tool(name):
    """Check whether `name` is on PATH and marked as executable."""

    # from whichcraft import which
    from shutil import which

    return which(name) is not None

distutils.spawn.find_executable

def is_tool(name):
    """Check whether `name` is on PATH."""

    from distutils.spawn import find_executable

    return find_executable(name) is not None

Launch terminal via Python in different linux distros

from distutils.spawn import find_executable
 
def isNot_exec(name):
    #check whether terminal program exists
    return find_executable(name) is None
 
def findTerminal():
    termList = ["x-terminal-emulator", "konsole", "gnome-terminal", "urxvt", "rxvt", "termit", "terminator", "Eterm", "aterm", "uxterm", "xterm", "roxterm", "xfce4-terminal", "termite", "lxterminal", "mate-terminal", "terminology", "st", "qterminal", "lilyterm", "tilix", "terminix", "kitty", "guake", "tilda", "alacritty", "hyper"]
    #list taken from https://github.com/i3/i3/blob/next/i3-sensible-terminal
    i = 0
    for term in termList:
        if isNot_exec(term):
            i += 1
        else:
            break
    return termList[i]

Get working directory in python

import os
print os.getcwd()

Check if variable not equal to multiple things


while choice not in [1, 2, 3]:

Get filename from path

import os

#Get filename with extension
name = os.path.basename(path)
#filename without extension
os.path.splitext(name)[0] #extension is [1]

Check if node is gizmo

print 'gizmo_file' in nuke.selectedNode().knobs()

Get list of nodes inside selected gizmo

nodes = nuke.allNodes(group=nuke.selectedNode())

Leave a Reply

Your email address will not be published. Required fields are marked *