no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | graphic:python:nuke [2021/08/22 19:44] (current) – created ying | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | To Merge here: [[appwiki: | ||
+ | |||
+ | ====== Technique Section ====== | ||
+ | ===== useful plugin ===== | ||
+ | |||
+ | * https:// | ||
+ | * a standalone python editor for nuke, but can be extended | ||
+ | ===== Pipeline ===== | ||
+ | |||
+ | * pyside for nuke https:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | * https:// | ||
+ | * fxphd NUK230 PySide for NUKE | ||
+ | * http:// | ||
+ | |||
+ | * Method 1: nuke environment setting, use that to let nuke load its init.py and menu.py there < | ||
+ | NUKE_PATH | ||
+ | D:/ | ||
+ | </ | ||
+ | * Method 2: edit ~/ | ||
+ | import nuke | ||
+ | nuke.pluginAddPath(' | ||
+ | </ | ||
+ | |||
+ | ==== Environment Detection ==== | ||
+ | |||
+ | * system detection< | ||
+ | import sys | ||
+ | osMode = ' | ||
+ | if sys.platform in [' | ||
+ | osMode = ' | ||
+ | elif sys.platform == ' | ||
+ | osMode = ' | ||
+ | elif sys.platform == ' | ||
+ | osMode = ' | ||
+ | print(" | ||
+ | |||
+ | </ | ||
+ | * nuke detect GUI mode <code python> | ||
+ | import nuke | ||
+ | nuke.env[' | ||
+ | </ | ||
+ | |||
+ | ==== Global Custom Pipeline integration ==== | ||
+ | |||
+ | * nuke will auto run all the init.py in the path directories before launch <code python init.py> | ||
+ | import sys | ||
+ | if nuke.NUKE_VERSION_STRING.startswith(' | ||
+ | nuke.pluginAddPath(' | ||
+ | sys.path.append(' | ||
+ | </ | ||
+ | * launch nuke with custom python initial script <code bash> | ||
+ | Nuke.exe replaceWritePaths.py other_args | ||
+ | </ | ||
+ | |||
+ | * menu.py file is another place for you to put menu related script, it is like init.py but more for menu creation< | ||
+ | # nuke main menu object | ||
+ | nuke.menu(' | ||
+ | |||
+ | # nuke gizmo menu object | ||
+ | cur_menu = nuke.menu(" | ||
+ | cur_menu.addCommand(' | ||
+ | |||
+ | # custom image formats | ||
+ | nuke.addFormat(" | ||
+ | </ | ||
+ | ==== Callback Function ==== | ||
+ | |||
+ | * the event driven functions that nuke will execute when the event happend. You can put those event function in init.py file, which nuke reads after launch. (in both gui and cmd mode) | ||
+ | * nuke before render event example <code python> | ||
+ | |||
+ | ==== commandline render ==== | ||
+ | |||
+ | * render the all write node in script < | ||
+ | " | ||
+ | </ | ||
+ | * render the Defined Write Node< | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== UI Integration ===== | ||
+ | |||
+ | * menu creation with one-line and auto creation if not exist <code python> | ||
+ | # it automatically create xTool menu in menubar, | ||
+ | # and create " | ||
+ | import sys, | ||
+ | myPath in sys.path or sys.path.append(myPath); | ||
+ | cmd = nuke.menu(' | ||
+ | cmd.setIcon(os.path.join(myPath,' | ||
+ | </ | ||
+ | |||
+ | * save script <code python> | ||
+ | nuke.scriptSave( outScript ) | ||
+ | |||
+ | curScript = nuke.toNode(" | ||
+ | nuke.scriptSaveAs("/ | ||
+ | nuke.scriptSaveAs(curScript) | ||
+ | </ | ||
+ | |||
+ | ==== Node attribute knob Operation ==== | ||
+ | |||
+ | * set knob to readonly/ | ||
+ | |||
+ | # lock with a flag | ||
+ | nuke.selectedNode()[knob].setFlag(nuke.DISABLED) | ||
+ | nuke.selectedNode()[knob].clearFlag(nuke.DISABLED) | ||
+ | </ | ||
+ | |||
+ | ==== menu operation ==== | ||
+ | |||
+ | **common menu operation codes** | ||
+ | |||
+ | <code python> | ||
+ | # top menu and side bar menu obj | ||
+ | nuke.menu(" | ||
+ | nuke.toolbar(" | ||
+ | |||
+ | # add menu and add cmd obj | ||
+ | menubar = nuke.menu(" | ||
+ | my_menu = menubar.addMenu(" | ||
+ | my_menu.addCommand(" | ||
+ | |||
+ | menubar.addMenu(" | ||
+ | |||
+ | # get items of a ui object | ||
+ | # list direct child UI object | ||
+ | menubar.items() | ||
+ | # return menu child with that name | ||
+ | menubar.menu(' | ||
+ | # return menu or menuItem child w that name | ||
+ | menubar.findItem(' | ||
+ | # loop method of listing | ||
+ | name_item_child_list = [ [cur_item.name(), | ||
+ | print(name_item_child_list ) | ||
+ | |||
+ | </ | ||
+ | |||
+ | * ref: | ||
+ | * http:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | ===== Setting Operation ===== | ||
+ | |||
+ | * root first frame and last frame <code python> | ||
+ | start = int(nuke.Root().knob(' | ||
+ | end = int(nuke.Root().knob(' | ||
+ | </ | ||
+ | * get and goto frame <code python> | ||
+ | curFrame = nuke.frame() # get | ||
+ | nuke.frame(1002) # goto | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== File Operation ===== | ||
+ | |||
+ | * Common maya like file operation, like Import/Edit file, and export selected/ save file as. <code python> | ||
+ | # import file to viewport | ||
+ | nuke.nodePaste(in_filepath.replace(' | ||
+ | # edit File | ||
+ | nuke.scriptOpen(in_filepath.replace(' | ||
+ | # save Selected as File | ||
+ | nuke.nodeCopy(out_filepath) | ||
+ | # save file as | ||
+ | nuke.scriptSaveAs(out_filepath) | ||
+ | </ | ||
+ | * get current script file path <code python> | ||
+ | full_path = '' | ||
+ | try: | ||
+ | full_path = nuke.scriptName() | ||
+ | except: | ||
+ | nuke.message(' | ||
+ | </ | ||
+ | |||
+ | ===== Node Operation ===== | ||
+ | * set a nuke node default <code python> | ||
+ | nuke.knobDefault( ' | ||
+ | </ | ||
+ | |||
+ | * nuke script' | ||
+ | </ | ||
+ | * nuke callBack function to access current call node <code python> | ||
+ | * read <code python> | ||
+ | my_node = nuke.createNode(' | ||
+ | my_node[' | ||
+ | </ | ||
+ | * get selected nodes with type filtering <code python> | ||
+ | selected = [x for x in nuke.selectedNodes() if x.Class() == ' | ||
+ | </ | ||
+ | * find all read nodes <code python> | ||
+ | nuke_readNodes = [x for x in nuke.allNodes(recurseGroups=True) if x.Class() == ' | ||
+ | self.memoData[' | ||
+ | </ | ||
+ | * find all write nodes <code python> | ||
+ | def getNodeByClass(nodeClass, | ||
+ | if startNode.Class() == nodeClass: | ||
+ | yield startNode | ||
+ | elif isinstance(startNode, | ||
+ | for child in startNode.nodes(): | ||
+ | for foundNode in RecursiveFindNodes(nodeClass, | ||
+ | yield foundNode | ||
+ | allWriteNodes = [w for w in getNodeByClass(' | ||
+ | </ | ||
+ | |||
+ | * render write node <code python> | ||
+ | # set after render python script | ||
+ | cur_node.knob(' | ||
+ | # call render | ||
+ | nuke.execute( nodeName, firstFrame, lastFrame )</ | ||
+ | |||
+ | * ref: https:// | ||
+ | |||
+ | ====== Knob Operation ====== | ||
+ | |||
+ | * bake keyframe animation | ||
+ | * ref: https:// | ||
+ | |||
+ | * add knob callback <code python> | ||
+ | node.knob(' | ||
+ | nuke.addKnobChanged(function) | ||
+ | nuke.removeKnobChanged(function) | ||
+ | </ | ||
+ | ====== Nuke Tcl Expression ====== | ||
+ | |||
+ | * Nuke Tcl Expression is not python, but nuke's own syntax for UI starts and node copy, | ||
+ | * it functions like Maya mel expression but it doesn' | ||
+ | * it use [] to contain a element, and operation is inside it with parameters and spaces. | ||
+ | * the Tcl expression support data operation, logic operation, regex, and python script. | ||
+ | * tip example: http:// | ||
+ | * auto write node: http:// | ||
+ | * ref: | ||
+ | * https:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * nuke help/ | ||
+ | |||
+ | **Replace Single Line String to Multi Line Display** | ||
+ | * put noteStr text input value into current message text block value, the single line format as "test / better one / best line" as <code tcl> | ||
+ | * it is useful to put [argv 8] like in the noteStr value, as cmd arg has to be in single line format | ||
+ | |||
+ | **Use Express to Link StrValue to IntValue** | ||
+ | * create a user text input knob, then use expression to link int knob to this text knob, and use [argv 7] like cmd arg to drive text value then updating the int value. (since int knob seems can't set as [argv 7] like) | ||
+ | |||
+ | **example** | ||
+ | |||
+ | < | ||
+ | # file attribute expression: | ||
+ | [value rdir]/ | ||
+ | # get evaluate result | ||
+ | print (nuke.thisNode()[" | ||
+ | |||
+ | # mix python in tcl | ||
+ | file " | ||
+ | |||
+ | # use cmd | ||
+ | | ||
+ | </ | ||
+ | |||
+ | * TCL and Python Expression that trigger by frame reading error, as a way to detect error frame like corrupted exr, ref: https:// | ||
+ | - create a user integer knob on Read node | ||
+ | - set its value expression (Python) < | ||
+ | - or set its value expression (TCL) < | ||
+ | - or create a user integer knob on Read node downstream connected node and | ||
+ | - set its value express (Python) < | ||
+ | - or set its value express (TCL), which take its topnode < | ||
+ | |||
+ | {{: | ||
+ | |||
+ | * addition info: | ||
+ | * Windows > Error Console for error information | ||
+ | ====== Nuke API ====== | ||
+ | |||
+ | * nuke used library: nuke help/ | ||