Create Simple Python Scripts To Speed Up Your Workflow

Categories Nuke, Python, Workflow

Nuke is incredibly flexible and customizable out of the box, and empowers artists to work they way they want to work to get a shot to final. But there’s a whole other world of untapped potential when you utilize Python to further expand it! In this post, I’m going to show you an example of how even the most simple python script can save you time every day.

If you’re doing something more than once every day, automate it.

I’m a huge advocate of this mindset, and am always searching for ways to improve my workflow. Seconds saved many times a day add up incredibly quickly! Upon embarking on a journey to do this with Python, I asked myself, “what node do I use in every script, and how can I make it work better for me?” The first thing that came to mind was the Merge node, and trying to find the exact operation I needed to use from the large drop-down list. Doesn’t sound like a big deal at first, but once I created a solution to speed this up, it noticeably improved my output!

The solution? Hotkeys for everything, and a Python script that “inverts” the current merge operation! My most-used merge operations are Plus and From (dealing with CG AOVs), Mask and Stencil (dealing with mattes) & Merge and Under for combining things together.

Here is how I lay out my hotkeys:

m = Over (of course)
alt+m = Under
alt+i = Mask
alt+o = Stencil
alt+[ = Plus
alt+] = From

Add the following to your menu.py to assign the hotkeys.

nuke.menu('Nodes').addMenu('Merge/Merges').addCommand('Under', 'nuke.createNode("Merge2", "operation under", False)', "alt+m", shortcutContext=2)
nuke.menu('Nodes').addMenu('Merge/Merges').addCommand('Stencil', 'nuke.createNode("Merge2", "operation stencil bbox B", False)', "alt+o", shortcutContext=2)
nuke.menu('Nodes').addMenu('Merge/Merges').addCommand('Mask', 'nuke.createNode("Merge2", "operation mask bbox A", False)', "alt+i", shortcutContext=2)
nuke.menu('Nodes').addMenu('Merge/Merges').addCommand('Plus', 'nuke.createNode("Merge2", "operation plus", False)', "alt+[", shortcutContext=2)
nuke.menu('Nodes').addMenu('Merge/Merges').addCommand('From', 'nuke.createNode("Merge2", "operation from", False)', "alt+]", shortcutContext=2)

If you haven’t created custom hotkeys in Nuke before, check out the Adding Keyboard Shortcuts section in this article I wrote.

Just having these hotkeys assigned was great, but I wanted to go one step further. I wanted a way to set the operation to the inverse of what it was currently doing. Whilst creating this, I realized it could extend to toggling between matchmove & stabilize in a tracker node too!

I wrote the following function, which figures out the selected node’s Class (is it a Merge node, or a Tracker node?), finds the current value of the operation/transform knob, then flips it to the inverse what Ctrl+Shift+S is pressed. The last line adds a physical button to the Edit menu, so I can still use it in-case my hotkey gets overwritten by a studio’s menu.py.

def operationSwitcher():

    selectedNode = nuke.selectedNode()

    if selectedNode.Class() == "Tracker4":
        if selectedNode.knob('transform').value() == "match-move":
            selectedNode.knob('transform').setValue('stabilize')
        else:
            selectedNode.knob('transform').setValue('match-move')

    elif selectedNode.Class() == "Merge2":

        if selectedNode.knob('operation').value() == "stencil":
            selectedNode.knob('operation').setValue('mask')
          elif selectedNode.knob('operation').value() == "mask":
            selectedNode.knob('operation').setValue('stencil')

    elif selectedNode.knob('operation').value() == "over":
            selectedNode.knob('operation').setValue('under')
        elif selectedNode.knob('operation').value() == "under":
            selectedNode.knob('operation').setValue('over')

    elif selectedNode.knob('operation').value() == "plus":
            selectedNode.knob('operation').setValue('from')
        elif selectedNode.knob('operation').value() == "from":
            selectedNode.knob('operation').setValue('plus')

# Add to edit menu
nuke.menu('Nuke').addCommand( 'Edit/Switch Operation', operationSwitcher, "ctrl+shift+s")

Update: This code was written to be easy to follow. For a more-efficient script that does the same thing without relying on copy/pasting if statements all over the place, click here!

Here it is in action!

Simple code, easy results, faster comps, right?!

Figuring this out years ago opened my eyes to so many possibilities for how Nuke could be customized to make me work far more efficiently than I thought I could! As a tribute to this idea, I dedicated Issue 005 of my Comp Newsletter to incredibly smart compositors creating incredibly powerful custom tools & python scripts for us all to use.

Looking for more? Here is my collection of other python scripts I use all the time!

——————–
If you liked this post, and would like to gain a better understanding of the fundamentals of Python in Nuke, check out my course: Python for Nuke 101.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.