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