Code Snippets: Nuke Py

get the current file path

scriptPath = nuke.root().name()

save the file

nuke.scriptSaveAs(filename = 'full/path/with/filename.nk')

close the file

nuke.scriptClear()

open a file

nuke.scriptOpen('full/path/to/file.nk')

set project directory

nuke.root()['project_directory'].setValue('[python {nuke.script_directory()}]')

execute a render

nuke.execute('writeNodeName', startFrame, endFrame)

create a node, set attributes, and add custom knobs

n = nuke.createNode('NodeName')
n['attr'].setValue(value)

knob = nuke.KnobType('knob', 'Text')
n.addKnob(knob)

# example: read node
r = nuke.createNode('Read')
r['file'].setValue('full/path/to/image_%04d.exr')

custom knobs added to nodes appear under the "user" tab


using nuke.thisNode

# get a value
value = nuke.thisNode().knob('knobName').value()

# set a value
nuke.thisNode().knob('knobName').setValue(value)

useful for working with the currently active node


get and iterate through a list of all nodes of a certain type

nodes = nuke.allNodes('nodeName')
for node in nodes:
    value = node.knob('knobName').value()
    node.knob('knobName').setValue(value)

import a node tree

nuke.nodePaste('full/path/to/script/with/nodetree.nk')

the desired nodes should be exported as a separate nuke script prior


load a gizmo

nuke.load('gizmoName')

the gizmo needs to be added to the .nuke folder prior

basic menu start

import nukescripts

if nuke.env['gui']:
    # create a dialog
    class PanelName(nukescripts.PythonPanel):
        def __init__(self):
            nukescripts.PythonPanel.__init__(self, 'Panel Name')

            # add desired knobs here
            
        def showModalDialog(self):
            result = nukescripts.PythonPanel.showModalDialog(self)
            if result:
                # add what happens when "ok" on dialog is clicked
        
        def showPanelName():
            return PanelName().showModalDialog()
    
    # create a function        
    def functionName():
        # add what happens when menu item is clicked
            
# Add dialogs/functions to the custom menu
menubar = nuke.menu('Nuke')
menuName = 'customName'
menubar.addCommand(menuName+'/Cmd Name', showPanelName)
menubar.addCommand(menuName+'/Cmd Name', functionName)

the final script will need to be called "menu.py" and added to the .nuke folder


some knobs

# text field
self.text = nuke.String_Knob('text', 'Text:')
self.addKnob(self.text)

# option menu
items = ['red', 'green', 'blue']
self.menu = nuke.Enumeration_Knob('menu', 'Menu Text:', items)
self.addKnob(self.menu)

# text used as a separator
self.separator = nuke.Text_Knob('')
self.addKnob(self.separator)

# python button
self.python = nuke.PyScript_Knob('python', 'Run Script:', "full python script in one line format")
self.addKnob(self.python)

working with knobs

# get the name of a knob
name = knob.name()

# get the value of a knob
value = self.knobName.value()

using knobChanged to edit knob values

class ColorPanel(nukescripts.PythonPanel):
    def __init__(self):
        nukescripts.PythonPanel.__init__(self, 'Color Picker')
        
        colors = ['red', 'yellow', 'blue']
        self.color = nuke.Enumeration_Knob('color', 'Color:', colors)
        self.addKnob(self.color)
        
        currentColor = self.color.value()
        self.text = nuke.String_Knob('text', 'Selected:', currentColor)
        self.addKnob(self.text)
        
    def knobChanged(self, knob):
        if knob.name() == 'color': # optional (in this case)
            newColor = self.color.value()
            self.text.setValue(newColor)
            
    def showModalDialog(self):
        result = nukescripts.PythonPanel.showModalDialog(self)
        if result:
            # add what happens when "ok" is clicked
        
    def showColorPanel():
        return ColorPanel().showModalDialog()

.nuke default location

linux = "/home/username/.nuke"
macOSx = "/Users/username/.nuke"
windows = "drive:\Users\username\.nuke"