Making custom scripts easily accessible can be a big time saver and necessary when optimising your pipeline. This tutorial will explain how you can setup custom menus in Nuke, that will execute your own Python scripts.
Though The Foundry’s own guide is fairly thorough, it’s missing a complete example of running your own scripts.
So let’s get started!
Creating the menu file
Create a file called menu.py inside the one of the folders listed below based on your system.
Linux: /usr/local/Nuke10.5v7/plugins /usr/local/Nuke10.5v7/plugins /nukescripts ~/.nuke Mac OS X: /Applications/Nuke10.5v7/Nuke10.5v7.app/Contents/MacOS/plugins /Applications/Nuke10.5v7/plugins/nukescripts ~/.nuke Windows: C:\Program Files\Nuke10.5v7\plugins\nukescripts C:\Program Files (x86)\Nuke10.5v7\plugins\nukescripts C:\Program Files\Nuke10.5v7\plugins ~\.nuke
Open menu.py and add the following code:
import my_script menu = nuke.menu("Nuke") menu.addCommand("My Menu/Run My Awesome Code", lambda: my_script.my_function())
The first line imports our custom script, which we’ll make in a second. The last two lines creates a variable that points to Nuke’s menu, and afterwards adds a new menu called “My Menu” with the menu item “Run My Awesome Code”. The
lambda: part is the imported module and function which will be called. It will end up looking like this.
Adding a script
Next, create a file called “my_script.py” with the following code:
import nuke def my_function(): read = nuke.createNode("Read") read["name"].setValue("Hello") write = nuke.createNode("Write") write["name"].setValue("World")
Putting things into subfolders
To avoid creating a mess, it would be best if we saved our files in some subfolders. Assuming you’ve placed your files in one of the above-mentioned locations, create a folder called “my_scripts”, and move “menu.py” to it. Create another folder inside called “scripts”, and move “my_script.py” to it. In the end you should have a structure like this:
In order for Nuke to read our files, we need to add a couple of “init.py” files that adds our new folders to Nuke’s plugin paths. The final structure will look like this:
Now, open the first “init.py” file add the line
For the “init.py” inside “my_scripts” add
That’s it! Restart Nuke, to see your new and shiny menu appear.
Download the sample code: nuke-menu-sample.zip