Nuke Py


file operations
    # Get the current file path
    file_path = 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("write_node_name", start_frame, end_frame)

get and select a node
    node = nuke.toNode("node_name")
    node.knob("selected").setValue(True)
create a node, set attributes, and add custom knobs
    node = nuke.createNode("node_type")
    node["attr"].setValue(value)
    
    knob = nuke.KnobType("knob_name", "knob_label")
    node.addKnob(knob)
    
    # Example: read node
    read_node = nuke.createNode("Read")
    read_node["file"].setValue("full/path/to/image_%04d.exr")

custom knobs are added to the “user” tab

using nuke.thisNode
    # Get a value
    value = nuke.thisNode().knob("knob_name").value()
    
    # Set a value
    nuke.thisNode().knob("knob_name").setValue(value)

useful for working with the current active node

get all nodes of a certain type
    nodes = nuke.allNodes("node_type")
    for node in nodes:
        value = node.knob("knob_name").value()
        node.knob("knob_name").setValue(value)

import a node tree
    nuke.nodePaste("/full/path/to/script.nk")

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

load a gizmo
    nuke.load("gizmo_name")

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")
    menu_name = "customMenu"
    menubar.addCommand(menu_name+"/Cmd Name", showPanelName)
    menubar.addCommand(menu_name+"/Cmd Name", functionName)  

the final script must be called “menu.py” and added to the .nuke folder
more information


some knobs
    # Text field
    self.text = nuke.String_Knob("name", "label")
    self.addKnob(self.text)
    
    # Option menu
    items = ["red", "green", "blue"]
    self.menu = nuke.Enumeration_Knob("name", "label", items)
    self.addKnob(self.menu)
    
    # Python button
    self.python = nuke.PyScript_Knob("name", "label:", "one line python script")
    self.addKnob(self.python)
    
    # Separator (using text)
    self.separator = nuke.Text_Knob("")
    self.addKnob(self.separator)
working with knobs
    # Get the name of a knob
    name = knob.name()
    
    # Get the value of a knob
    value = self.knobName.value()
using knobChanged (color picker example)
    class ColorPanel(nukescripts.PythonPanel):
        def __init__(self):
            nukescripts.PythonPanel.__init__(self, "Color Picker")
            
            colors = ["red", "green", "blue"]
            self.color = nuke.Enumeration_Knob("color", "Color:", colors)
            self.addKnob(self.color)
            
            current_color = self.color.value()
            self.text = nuke.String_Knob("text", "Selected:", current_color)
            self.addKnob(self.text)
            
        def knobChanged(self, knob):
            if knob.name() == "color": # Optional (in this case)
                new_color = self.color.value()
                self.text.setValue(new_color)
                
        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”