1 """
2 A collection of tools and functions to help manage LUTs and color configuration
3 """
4 from _nuke_color import *
5 import callbacks
6 import types
7 import nuke
8
9 defaultLUTMappers = {}
10
12 """ an excpetion that should be thrown when looking up the colorspace """
13 pass
14
15
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
24
25 return False
26 allColorspaces = getColorspaceList( colorspaceKnob )
27 return colorspaceName in allColorspaces
28
29
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:
79 retString = getKnobValue( "monitorLut" )
80 elif dataTypeHint == nuke.VIEWER:
81 pass
82 elif dataTypeHint == nuke.INT8:
83 retString = getKnobValue( "int8Lut" )
84 elif dataTypeHint == nuke.INT16:
85 retString = getKnobValue( "int16Lut" )
86 elif dataTypeHint == nuke.LOG:
87 retString = getKnobValue( "logLut" )
88 elif dataTypeHint == nuke.FLOAT:
89 retString = getKnobValue( "floatLut" )
90 return retString
91
92
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
104 return retName
105
106
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
125
126
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
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
160 colorspace = _nukeDefaultColorSpaceMapper(colorspace, dataTypeHint)
161
162
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
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
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
192 strippedColorspace = strippedColorspace.split('/')[-1]
193 strippedColorspaces.append(strippedColorspace)
194
195 return strippedColorspaces
196