Package nuke :: Module colorspaces
[hide private]
[frames] | no frames]

Source Code for Module nuke.colorspaces

  1  """ 
  2  A collection of tools and functions to help manage LUTs and color configuration 
  3  """ 
  4  from _nuke_color import * # bring the libnuke PythonAPI for color into scope 
  5  import callbacks 
  6  import types 
  7  import nuke 
  8   
  9  defaultLUTMappers = {} # dictionary of all mappers 
 10   
11 -class ColorspaceLookupError(Exception):
12 """ an excpetion that should be thrown when looking up the colorspace """ 13 pass
14 15
16 -def _attemptColorspaceNameMatch(colorspaceName) :
17 """ Look through all options in the colorpsace knob, and see if we have an 18 exact match to one of the items. """ 19 node = nuke.thisNode() 20 try: 21 colorspaceKnob = node['colorspace'] 22 except ValueError: 23 # failed to get the Knob from the Node because the Node may be unattached 24 # when loading script or similar 25 return False 26 allColorspaces = getColorspaceList( colorspaceKnob ) 27 return colorspaceName in allColorspaces
28 29
30 -def _lookUpDataTypeDefaultSettings(colorspaceName, dataTypeHint):
31 """ 32 Nuke's default handling of colorspace lookups. 33 34 Maps applicable dataTypeHint values to knobs on the Preferecne panel 35 36 Possible values for dataTypeHint are 37 Nuke inbuilt data-type hints (map to knobs) 38 nuke.MONITOR == 0 39 nuke.VIEWER == 1 40 nuke.INT8 == 2 41 nuke.INT16 == 3 42 nuke.LOG == 4 43 nuke.FLOAT == 5 44 Other numeric values which map to those in DDImage/LUT.h 45 6 == DD::Image::LUT::GAMMA1_8 46 7 == DD::Image::LUT::GAMMA2_2 47 8 == DD::Image::LUT::GAMMA2_4 48 9 == DD::Image::LUT::PANALOG 49 10 == DD::Image::LUT::REDLOG 50 11 == DD::Image::LUT::VIPERLOG 51 12 == DD::Image::LUT::ALEXAV3LOGC 52 13 == DD::Image::LUT::PLOGLIN 53 14 == DD::Image::LUT::SLOG 54 15 == DD::Image::LUT::SLOG1 55 16 == DD::Image::LUT::SLOG2 56 17 == DD::Image::LUT::SLOG3 57 18 == DD::Image::LUT::CLOG 58 19 == DD::Image::LUT::PROTUNE 59 """ 60 def getKnobValue( knobName ) : 61 root = nuke.root() 62 try: 63 knob = root[ knobName ] 64 except ValueError: 65 raise ColorspaceLookupError 66 return knob.value()
67 68 retString = colorspaceName 69 if dataTypeHint == nuke.MONITOR: # 0 = LUT::MONITOR 70 retString = getKnobValue( "monitorLut" ) 71 elif dataTypeHint == nuke.VIEWER: # 1 = LUT::VIEWER 72 pass 73 elif dataTypeHint == nuke.INT8: # 2 = LUT::INT8 74 retString = getKnobValue( "int8Lut" ) 75 elif dataTypeHint == nuke.INT16: # 3 = LUT::INT16 76 retString = getKnobValue( "int16Lut" ) 77 elif dataTypeHint == nuke.LOG: # 4 = LUT::LOG 78 retString = getKnobValue( "logLut" ) 79 elif dataTypeHint == nuke.FLOAT: # 5 = LUT::FLOAT 80 retString = getKnobValue( "floatLut" ) 81 return retString 82 83
84 -def _nukeDefaultColorSpaceMapper(colorspaceName, dataTypeHint):
85 """ 86 Allows colorspaces selections to be altered before use on Readers and Writers 87 """ 88 if colorspaceName == "." : 89 raise RuntimeError( "Nuke has provided invalid colorspace name" ) 90 try: 91 retName = _lookUpDataTypeDefaultSettings(colorspaceName, dataTypeHint) 92 except ColorspaceLookupError: 93 retName = colorspaceName 94 # TODO: find best name in colosrpace 95 return retName
96 97
98 -def addDefaultColorspaceMapper(call, args=(), kwargs={}, nodeClass='*'):
99 """ 100 Add a function to modify default colorspaces before Nuke passes them to 101 Readers or Writers. 102 103 Functions should have the same positional argument as in the definition of 104 defaultLUTMapper() 105 106 All added functions are called in backwards order. 107 """ 108 callbacks._addCallback(defaultLUTMappers, call, args, kwargs, nodeClass)
109 110
111 -def removeDefaultColorspaceMapper(call, args=(), kwargs={}, nodeClass='*'):
112 """ 113 Remove a previously-added callback with the same arguments. 114 """ 115 callbacks._removeCallback(defaultLUTMappers, call, args, kwargs, nodeClass)
116 117
118 -def _doColorSpaceCallbacks( colorspace, dataTypeHint, callbacks, errorMsg ) :
119 """ 120 Perform the colorspace callbacks 121 expects a string or 'None' to be returned. 122 """ 123 for funcData in callbacks: 124 func = funcData[0] 125 args = funcData[1] 126 kwargs = funcData[2] 127 s = func(colorspace, dataTypeHint, *args, **kwargs) 128 if not isinstance(s, str) and s is not None: 129 raise TypeError( errorMsg + ". Got type %s"%str(type(s)) ) 130 if s is not None: 131 colorspace = s 132 return colorspace
133 134
135 -def defaultColorspaceMapper(colorspace, dataTypeHint):
136 """ 137 Called by libnuke. 138 Calls into Node-level callbacks first, then global callbacks 139 140 Arguments: 141 colorspace - the name string of the initial colorspace 142 dataTypeHint - sometimes Readers/Writer request the default for a 143 particular data-type, i.e. int8, in16, float, etc. 144 Return: 145 The return should be the transformed/modified colorspace name. 146 None is the same as returning the string unchanged. 147 """ 148 import __main__ 149 150 # Do Nuke's in built mapping first. 151 colorspace = _nukeDefaultColorSpaceMapper(colorspace, dataTypeHint) 152 153 # Then do callbacks registered for this Node type 154 colorspaceCbs = defaultLUTMappers.get(nuke.thisClass()) 155 if colorspaceCbs: 156 nodeErrMsg = ( "Colorspace Filter on Node '%s' returned invalid type," 157 "expected string or None"%( nuke.theClass() ) ) 158 colorspace = _doColorSpaceCallbacks( colorspace, dataTypeHint, colorspaceCbs, nodeErrMsg ) 159 160 # Do global manipulations afterwards 161 globalCbs = defaultLUTMappers.get('*') 162 if globalCbs: 163 globalErrMsg = ( "Global Colorspace Filter returned invalid type," 164 "expected string or None" ) 165 colorspace = _doColorSpaceCallbacks( colorspace, dataTypeHint, globalCbs, globalErrMsg ) 166 167 return colorspace
168
169 -def getColorspaceList(colorspaceKnob) :
170 """ 171 Get a list of all colorspaces listed in an enumeration knob. 172 This will strip family names if the knob has the STRIP_CASCADE_PREFIX flag set. 173 """ 174 175 allColorspaces = colorspaceKnob.values() 176 strippedColorspaces = [] 177 178 if not colorspaceKnob.getFlag(nuke.STRIP_CASCADE_PREFIX) : 179 return allColorspaces 180 else: 181 for strippedColorspace in allColorspaces: 182 # Strip up until the last '/', which represents the family name 183 strippedColorspace = strippedColorspace.split('/')[-1] 184 strippedColorspaces.append(strippedColorspace) 185 186 return strippedColorspaces
187