# Python

> The nuke Python module, the Script Editor, menu.py / init.py and what does not work.

Oink includes Python modules named `nuke` and `nukescripts` with the familiar API, so pipeline scripts, `menu.py` files and tools from Nukepedia run with few or no changes. They report themselves as Nuke 17 (`nuke.NUKE_VERSION_MAJOR == 17`), because node and knob names follow Nuke 17; `nuke.OPENCOMP` is `True` if a script needs to tell the difference.

Every function and class is listed in the [Python nuke API reference](/docs/reference/python-api).

## Script Editor

*Workspace › Script Editor* opens it as a tab beside Properties. Output is on top, input below.

| Key | Does |
|---|---|
| Ctrl+Enter | run the selection, or everything |
| Ctrl+[ / Ctrl+] | previous / next command in the history |
| Ctrl+Space | complete the name under the cursor |

All runs share one namespace with `nuke` and `nukescripts` already imported. A run is one undo step.

## What works

- Creating, finding, connecting and deleting nodes: `nuke.createNode` (connects and places like Nuke), `nuke.nodes.Blur(...)`, `nuke.toNode('Group1.Blur1')`, `nuke.allNodes`, selection functions.
- Knobs: `node['size'].setValue(5)`, `getValue`, animation (`setAnimated`, `setValueAt`, `setExpression`), adding and removing user knobs.
- `nuke.root()` frame range, fps and format; `nuke.execute(write, first, last)` renders Write nodes.
- `scriptOpen`, `scriptSave`, `nodeCopy`, `nodePaste` (.nk and .oink).
- `nuke.Undo`, `thisNode`, `thisKnob`, groups with `with group:`.
- Dialogs: `nuke.message`, `ask`, `getInput`, `getFilename`, `choice`, `nuke.ProgressTask`, `nuke.Panel` and `nukescripts.PythonPanel` (modal, floating or docked with `addToPane`) use the Properties knob widgets; see [Extending (for TDs)](/docs/developers/extending#python-panels).
- Callbacks: onCreate, onUserCreate, onDestroy, knobChanged, onScriptLoad / Save / Close, before / after render and frame render, filenameFilter (applied to every file path a node reads or writes).
- Menus: `nuke.menu('Nuke')`, `'Nodes'`, `'Node Graph'`, `'Viewer'` with `addCommand`, `addMenu`, shortcuts. Items join the menu bar, the node toolbar and the Tab menu.

## menu.py, init.py and the plugin path

At startup Oink runs, for `~/.opencomp` (or `$OPENCOMP_USER_DIR`) and then every folder in `NUKE_PATH` and `OPENCOMP_PLUGIN_PATH`: each folder's `init.py` first, then every `menu.py`. `nuke.pluginAddPath` works as in Nuke. Errors are printed and never stop the app.

## What does not work

- **Qt / PySide panels inside the app.** The interface is not Qt: PySide tools run in a separate Qt helper process and open as floating windows (they can't dock). Their `import nuke` drives the comp. Setup and limits: [Extending (for TDs)](/docs/developers/extending#pyside--qt-tools).
- Viewer, Roto / Paint, OFX and Hiero / Studio Python APIs.
- `updateUI`, `autolabel` and `autoSave` callbacks are registered but never called.
- Node `metadata`, `bbox`, `channels` and `layers` answers are approximate.


---
Sources: opencomp_core/nukepy/nuke/__init__.py, opencomp_core/nukepy/nuke/_nodes.py, opencomp_core/nukepy/nuke/_knobs.py, opencomp_core/nukepy/nuke/_menus.py, opencomp_core/nukepy/nuke/_callbacks.py, opencomp_core/nukepy/nuke/_panels.py, opencomp_server/nuke_commands.py, opencomp_electron/src/components/scripteditor
