Create a gist now

Instantly share code, notes, and snippets.

@shidarin /luts.py
Last active Oct 16, 2015

What would you like to do?
Controlling Nuke Lut Behavior
# =============================================================================
# Controlling Nuke Lut Behavior
# =============================================================================
# From: Controlling Nuke Lut Behavior
# http://shidarin.github.io/2013/controlling-nuke-lut-behavior/
# By: Sean Wallitsch (sean@grenadehop.com)
# =============================================================================
"""Functions for creating LUT groups to be used as ViewerProcesses within Nuke
Synopsis
--------
Functions:
lutGroup1D()
Builds a proper 1D Viewing Lut that only affects the rgb channels,
protecting data channels and the alpha. Can be used on any 1D lut
registered with Nuke.
lutGroup3D()
Builds a proper 3D viewer lut that only affects the rgb channels,
protecting the data channels while affecting all additional layers.
Licensing:
The MIT License (MIT)
Copyright (c) 2014 Rhythm & Hues Studios
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
# =============================================================================
# IMPORTS
# =============================================================================
# Nuke Imports
import nuke
# =============================================================================
# PUBLIC FUNCTIONS
# =============================================================================
def lutGroup1D(lutName):
"""Builds a proper 1D viewer lut that only affects the rgb channels
Args:
lutName : (str)
The 1d lut name. Must be already registered in Nuke's settings.
Raises:
N/A
Returns:
(<nuke.node.Group>)
A nuke group node that will handle the 1d LUT correctly.
"""
# Start our group
group = nuke.nodes.Group()
group.begin()
inputNode = nuke.nodes.Input() # Group input
REMOVE_KNOBS = {
'operation': 'remove',
'channels': 'depth',
'channels2': 'motion',
'channels3': 'forward',
'channels4': 'backward',
}
# Remove special layers if present
remove = nuke.nodes.Remove(
inputs=[inputNode],
)
for knob in REMOVE_KNOBS:
remove[knob].setValue(REMOVE_KNOBS[knob])
conversion = nuke.createNode('ViewerLUT')
conversion['rgb_only'].setValue(True)
conversion['current'].setValue(lutName)
conversion.setInput(0, remove)
# Add our special channels back in
copy = nuke.nodes.Copy(
inputs=[inputNode, conversion],
channels='all',
)
# We need to eliminate any attempts at auto copy.
for i in range(4):
copy['from' + str(i)].setValue('none')
nuke.nodes.Output(
inputs=[copy],
)
group.end()
return group
# =============================================================================
def lutGroup3D(lutFile, inputSpace=None):
"""Builds a proper 3D viewer lut that only affects the rgb channels
Args:
lutFile : (str)
The filepath to a 3d lut
inputSpace=None : (str)
The colorspace the 3d lut file expects.
Raises:
N/A
Returns:
(<nuke.node.Group>)
A nuke node group that will handle the 3d LUT correctly.
"""
# Start our group
group = nuke.nodes.Group()
group.begin()
inputNode = nuke.nodes.Input() # Group input
REMOVE_KNOBS = {
'operation': 'remove',
'channels': 'depth',
'channels2': 'motion',
'channels3': 'forward',
'channels4': 'backward',
}
# Remove special layers if present
remove = nuke.nodes.Remove(
inputs=[inputNode],
)
for knob in REMOVE_KNOBS:
remove[knob].setValue(REMOVE_KNOBS[knob])
if inputSpace:
# Convert to Cineon or LogC colorspace
conversion = nuke.createNode('ViewerLUT')
conversion['rgb_only'].setValue(True)
conversion['current'].setValue(inputSpace)
conversion.setInput(0, remove)
else:
conversion = remove
# Use our 3d LUT file
mainLut = nuke.nodes.OCIOFileTransform(
inputs=[conversion],
channels='all',
file=lutFile,
)
# Add our special channels back in
copy = nuke.nodes.Copy(
inputs=[inputNode, mainLut],
channels='all',
)
# We need to eliminate any attempts at auto copy.
for i in range(4):
copy['from' + str(i)].setValue('none')
nuke.nodes.Output(
inputs=[copy],
)
group.end()
return group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment