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