Q100354: How to activate a Nuke menu command using Python
This article covers how to pythonically activate a command from a Nuke menu system.
Nuke’s graphical user interface (GUI) is very customisable, and as part of that you can add, move or activate various items in the menu system, allowing users to set up custom pipelines with full integration into Nuke.
Nuke’s menu system is comprised of two main classes, Menus and MenuItems, definitions for which can be seen below:
- Menu class: A container for other Menus or MenuItems
- MenuItem class: A container for a QAction which performs an action in Nuke
The Menu and MenuItem classes are used to make the menu structures inside Nuke. You can also use them to make your own menu structures, allowing for custom menus. More information about the two classes can be found here:
A QAction is a QT/PySide class and is an abstract action which can be used to perform commands.
More information about QActions can be found in the QT documentation here: http://doc.qt.io/qt-5/qaction.html
When you execute a command from a Nuke menu, you are actually telling a MenuItem container to invoke its associated QAction. For example, when you select the Clone QAction in the Edit menu, a node is cloned, as that is the command assigned to that QAction.
In order to activate a command in Nuke's menu structure pythonically, you need to "find" the particular MenuItem container and invoke its associated QAction. This can be done using the code below:
mainMenu = nuke.menu("Nuke") mainMenu.findItem("Edit/Clone").invoke()
The first line of code
mainMenu = nuke.menu("Nuke")
returns the Menu class of the main Nuke menu toolbar.
If you're looking to access the nodes toolbar, this can be done with the same command by replacing “Nuke” with “Nodes”.
The second line
uses the findItem() function to find the 'Clone' MenuItem container inside the 'Edit' Menu, then activates the QAction of that MenuItem via the .invoke() function. The result in this case would be cloning the selected nodes.
NOTE: To create the path to find a particular QAction in a menu system, you need to use / as separator every time you enter a sub menu. For example: Edit/Clone, Viewer/View/Next, etc.
0 out of 0 found this helpful