To Merge here: App Nuke

Technique Section

  • nuke environment setting
    NUKE_PATH
    D:/myPath_for_nuke
  • nuke will auto run all the init.py in the path directories before launch
    init.py
    import sys
    if nuke.NUKE_VERSION_STRING.startswith('9'):
        nuke.pluginAddPath('./9.0') # relative path
        sys.path.append('D:/myOtherPythonModulePath') # abs path
  • launch nuke with custom python initial script
    Nuke.exe replaceWritePaths.py other_args
  • menu creation with one-line and auto creation if not exist
    # it automatically create xTool menu in menubar, 
    # and create "CompHelper" menu item that call function string, with shortcut and icon 
    import sys,os;myPath=r'D:\Dev\CompHelper'
    myPath in sys.path or sys.path.append(myPath);import CompHelper;
    cmd = nuke.menu('Nuke').addCommand("xTool/CompHelper", 'CompHelper.main()',icon=os.path.join(myPath,'icons','CompHelper.png'))
    cmd.setIcon(os.path.join(myPath,'icons','CompHelper.png'))
  • save script
    nuke.scriptSave( outScript )
     
    curScript = nuke.toNode("root").name()
    nuke.scriptSaveAs("/scratch/snapshot.nk")
    nuke.scriptSaveAs(curScript)

Node attribute knob Operation

  • set knob to readonly/locked
    knob.setEnabled(False)
     
    # lock with a flag
    nuke.selectedNode()[knob].setFlag(nuke.DISABLED) 
    nuke.selectedNode()[knob].clearFlag(nuke.DISABLED)
  • root first frame and last frame
    start = int(nuke.Root().knob('first_frame').value())
    end = int(nuke.Root().knob('last_frame').value())
  • get and goto frame
    curFrame = nuke.frame() # get
    nuke.frame(1002) # goto
  • set a nuke node default
    nuke.knobDefault( 'Write.afterRender', 'import myFun;myFun.some_action()')
  • nuke script's root root
    root = nuke.thisNode()
  • read
    my_node = nuke.createNode('Read')
    my_node['file'].fromUserText('/path/to/your/file.mov')
  • get selected nodes with type filtering
    selected = [x for x in nuke.selectedNodes() if x.Class() == 'Read']
  • find all write nodes
    def getNodeByClass(nodeClass, startNode):
        if startNode.Class() == nodeClass:
            yield startNode
        elif isinstance(startNode, nuke.Group):
            for child in startNode.nodes():
                for foundNode in RecursiveFindNodes(nodeClass, child):
                    yield foundNode
    allWriteNodes = [w for w in getNodeByClass('Write', nuke.root())]
  • render write node
    # set after render python script
    cur_node.knob('afterRender').setValue("WriteToMov()") # call a python function after done rendering
    # call render
    nuke.execute( nodeName, firstFrame, lastFrame )

Knob Operation

  • add knob callback
    node.knob('knobChanged')
    nuke.addKnobChanged(function)
    nuke.removeKnobChanged(function)

Nuke Tcl Expression

example

# file attribute expression:
[value rdir]/finals/[value shot]/NK_[value shot].%03d.dpx
# get evaluate result
print (nuke.thisNode()["file"].evaluate())

# mix python in tcl
file "\[value nas_root]/\[value project]/\[value seq]/zup_renders/Comp/\[value RenderType]/\[value shot]_[value FileName]_v[python {str(int(nuke.thisNode()['ver'].value())).zfill(3)} ]/\[value shot]_[value FileName]_v[python {str(int(nuke.thisNode()['ver'].value())).zfill(3)} ].####.\[value file_type]" 

# use cmd
 knobChanged "\ncur=nuke.thisNode()\npathA = \[ cur\[each].value() for each in \['nas_root', 'project', 'seq'] ]\npathB = \['zup_renders/Comp', cur\['RenderType'].value() ]\nsegC = cur\['shot'].value()\nif cur\['FileName'].value() !='':\n    segC+='_'+cur\['FileName'].value()\nsegC+='_v'+ str(int(cur\['ver'].value())).zfill(3)\npathD = \[ segC+'.####.'+cur\['file_type'].value() ]\ncur\['file'].setValue( '/'.join( pathA + pathB + \[segC] + pathD ) )\n"

Nuke API

  • nuke used library: nuke help/Documentation/html/content/comp_environment/third_party_software/third_party_libraries.html