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 61 root = nuke.thisRoot() 62 63 def getKnobValue( knobName ) : 64 try: 65 knob = root[ knobName ] 66 except ValueError: 67 raise ColorspaceLookupError 68 return knob.value()
69 70 retString = colorspaceName 71 if dataTypeHint == nuke.MONITOR: # 0 = LUT::MONITOR 72 retString = getKnobValue( "monitorLut" ) 73 elif dataTypeHint == nuke.VIEWER: # 1 = LUT::VIEWER 74 pass 75 elif dataTypeHint == nuke.INT8: # 2 = LUT::INT8 76 retString = getKnobValue( "int8Lut" ) 77 elif dataTypeHint == nuke.INT16: # 3 = LUT::INT16 78 retString = getKnobValue( "int16Lut" ) 79 elif dataTypeHint == nuke.LOG: # 4 = LUT::LOG 80 retString = getKnobValue( "logLut" ) 81 elif dataTypeHint == nuke.FLOAT: # 5 = LUT::FLOAT 82 retString = getKnobValue( "floatLut" ) 83 return retString 84 85
86 -def _nukeDefaultColorSpaceMapper(colorspaceName, dataTypeHint):
87 """ 88 Allows colorspaces selections to be altered before use on Readers and Writers 89 """ 90 if colorspaceName == "." : 91 raise RuntimeError( "Nuke has provided invalid colorspace name" ) 92 try: 93 retName = _lookUpDataTypeDefaultSettings(colorspaceName, dataTypeHint) 94 except ColorspaceLookupError: 95 retName = colorspaceName 96 # TODO: find best name in colosrpace 97 return retName
98 99
100 -def addDefaultColorspaceMapper(call, args=(), kwargs={}, nodeClass='*'):
101 """ 102 Add a function to modify default colorspaces before Nuke passes them to 103 Readers or Writers. 104 105 Functions should have the same positional argument as in the definition of 106 defaultLUTMapper() 107 108 All added functions are called in backwards order. 109 """ 110 callbacks._addCallback(defaultLUTMappers, call, args, kwargs, nodeClass)
111 112
113 -def removeDefaultColorspaceMapper(call, args=(), kwargs={}, nodeClass='*'):
114 """ 115 Remove a previously-added callback with the same arguments. 116 """ 117 callbacks._removeCallback(defaultLUTMappers, call, args, kwargs, nodeClass)
118 119
120 -def _doColorSpaceCallbacks( colorspace, dataTypeHint, callbacks, errorMsg ) :
121 """ 122 Perform the colorspace callbacks 123 expects a string or 'None' to be returned. 124 """ 125 for funcData in callbacks: 126 func = funcData[0] 127 args = funcData[1] 128 kwargs = funcData[2] 129 s = func(colorspace, dataTypeHint, *args, **kwargs) 130 if not isinstance(s, str) and s is not None: 131 raise TypeError( errorMsg + ". Got type %s"%str(type(s)) ) 132 if s is not None: 133 colorspace = s 134 return colorspace
135 136
137 -def defaultColorspaceMapper(colorspace, dataTypeHint):
138 """ 139 Called by libnuke. 140 Calls into Node-level callbacks first, then global callbacks 141 142 Arguments: 143 colorspace - the name string of the initial colorspace 144 dataTypeHint - sometimes Readers/Writer request the default for a 145 particular data-type, i.e. int8, in16, float, etc. 146 Return: 147 The return should be the transformed/modified colorspace name. 148 None is the same as returning the string unchanged. 149 """ 150 import __main__ 151 152 # Do Nuke's in built mapping first. 153 colorspace = _nukeDefaultColorSpaceMapper(colorspace, dataTypeHint) 154 155 # Then do callbacks registered for this Node type 156 colorspaceCbs = defaultLUTMappers.get(nuke.thisClass()) 157 if colorspaceCbs: 158 nodeErrMsg = ( "Colorspace Filter on Node '%s' returned invalid type," 159 "expected string or None"%( nuke.theClass() ) ) 160 colorspace = _doColorSpaceCallbacks( colorspace, dataTypeHint, colorspaceCbs, nodeErrMsg ) 161 162 # Do global manipulations afterwards 163 globalCbs = defaultLUTMappers.get('*') 164 if globalCbs: 165 globalErrMsg = ( "Global Colorspace Filter returned invalid type," 166 "expected string or None" ) 167 colorspace = _doColorSpaceCallbacks( colorspace, dataTypeHint, globalCbs, globalErrMsg ) 168 169 return colorspace
170
171 -def getColorspaceList(colorspaceKnob) :
172 """ 173 Get a list of all colorspaces listed in an enumeration knob. 174 This will strip family names if the knob has the STRIP_CASCADE_PREFIX flag set. 175 """ 176 177 allColorspaces = colorspaceKnob.values() 178 strippedColorspaces = [] 179 180 if not colorspaceKnob.getFlag(nuke.STRIP_CASCADE_PREFIX) : 181 return allColorspaces 182 else: 183 for strippedColorspace in allColorspaces: 184 # Strip up until the last '/', which represents the family name 185 strippedColorspace = strippedColorspace.split('/')[-1] 186 strippedColorspaces.append(strippedColorspace) 187 188 return strippedColorspaces
189