Skip to content

colorspace

Nuke Colorspace related methods

colorspace_exists_on_node(node, colorspace_name)

Check if colorspace exists on node

Look through all options in the colorspace knob, and see if we have an exact match to one of the items.

Parameters:

Name Type Description Default
node Node

nuke node object

required
colorspace_name str

color profile name

required

Returns:

Name Type Description
bool

True if exists

Source code in client/ayon_nuke/api/colorspace.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def colorspace_exists_on_node(node, colorspace_name):
    """ Check if colorspace exists on node

    Look through all options in the colorspace knob, and see if we have an
    exact match to one of the items.

    Args:
        node (nuke.Node): nuke node object
        colorspace_name (str): color profile name

    Returns:
        bool: True if exists
    """
    node_knob_keys = node.knobs().keys()

    if "colorspace" in node_knob_keys:
        colorspace_knob = node["colorspace"]
    elif "floatLut" in node_knob_keys:
        colorspace_knob = node["floatLut"]
    else:
        log.warning(f"Node '{node.name()}' does not have colorspace knob")
        return False

    return colorspace_name in get_colorspace_list(colorspace_knob, node)

create_viewer_profile_string(viewer, display=None, path_like=False)

Convert viewer and display to string

Parameters:

Name Type Description Default
viewer str

viewer name

required
display Optional[str]

display name

None
path_like Optional[bool]

if True, return path like string

False

Returns:

Name Type Description
str

viewer config string

Source code in client/ayon_nuke/api/colorspace.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def create_viewer_profile_string(viewer, display=None, path_like=False):
    """Convert viewer and display to string

    Args:
        viewer (str): viewer name
        display (Optional[str]): display name
        path_like (Optional[bool]): if True, return path like string

    Returns:
        str: viewer config string
    """
    if not display:
        return viewer

    if path_like:
        return "{}/{}".format(display, viewer)
    return "{} ({})".format(viewer, display)

get_colorspace_list(colorspace_knob, node=None, consider_aliases=True)

Get available colorspace profile names

Parameters:

Name Type Description Default
colorspace_knob Knob

nuke knob object

required
node Optional[Node]

nuke node for caching differentiation

None
consider_aliases Optional[bool]

optional flag to indicate if

True

Returns:

Name Type Description
list

list of strings names of profiles

Source code in client/ayon_nuke/api/colorspace.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def get_colorspace_list(colorspace_knob, node=None, consider_aliases=True):
    """Get available colorspace profile names

    Args:
        colorspace_knob (nuke.Knob): nuke knob object
        node (Optional[nuke.Node]): nuke node for caching differentiation
        consider_aliases (Optional[bool]): optional flag to indicate if
        colorspace aliases should be added to results list

    Returns:
        list: list of strings names of profiles
    """
    results = []

    # making sure any node is provided
    node = node or nuke.root()
    # unique script based identifier
    script_name = nuke.root().name()
    node_name = node.fullName()
    identifier_key = f"{script_name}_{node_name}"

    if _COLORSPACES_CACHE.get(identifier_key) is None:
        # This pattern is to match with roles which uses an indentation and
        # parentheses with original colorspace. The value returned from the
        # colorspace is the string before the indentation, so we'll need to
        # convert the values to match with value returned from the knob,
        # ei. knob.value().
        pattern = r"[\t,]+"
        for colorspace in nuke.getColorspaceList(colorspace_knob):
            colorspace_and_aliases = re.split(pattern, colorspace)
            if consider_aliases:
                results.extend(colorspace_and_aliases)
            else:
                results.append(colorspace_and_aliases[0])

        _COLORSPACES_CACHE[identifier_key] = results

    return _COLORSPACES_CACHE[identifier_key]

get_display_and_view_colorspaces(root_node)

Get all possible display and view colorspaces

This is stored in class variable to avoid multiple calls.

Parameters:

Name Type Description Default
root_node Node

root node

required

Returns:

Name Type Description
list

all possible display and view colorspaces

Source code in client/ayon_nuke/api/colorspace.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def get_display_and_view_colorspaces(root_node):
    """Get all possible display and view colorspaces

    This is stored in class variable to avoid multiple calls.

    Args:
        root_node (nuke.Node): root node

    Returns:
        list: all possible display and view colorspaces
    """
    script_name = nuke.root().name()
    if _DISPLAY_AND_VIEW_COLORSPACES_CACHE.get(script_name) is None:
        # getting it from `monitorOutLUT` because that is the only shared
        # display and view knob for 13 > nuke version and returns
        # correct list of display and view colorspace profiles.
        colorspace_knob = root_node["monitorOutLUT"]
        colorspaces = nuke.getColorspaceList(colorspace_knob)
        _DISPLAY_AND_VIEW_COLORSPACES_CACHE[script_name] = colorspaces

    return _DISPLAY_AND_VIEW_COLORSPACES_CACHE[script_name]

get_formatted_colorspace(colorspace_name, formatting_data, root_node=None)

Format colorspace profile name into string.

This method is formatting colorspace profile name. It is iterating over all possible combinations of input string which could be separated by COLOR_VALUE_SEPARATOR defined in constants.

If Anatomy template tokens are used but formatting data is not provided, it will try any other available variants in next position of the separator.

This method also validate that the formatted colorspace profile name is available in currently run nuke session ocio config.

Example

from ayon_nuke.api.colorspace import get_formatted_colorspace colorspace_name = "{project_code}_{context};ACES - ACEScg" formatting_data = { ... "context": "01sh010", ... "project_code": "proj01" ...} new_colorspace_name = get_formatted_colorspace( ... colorspace_name, formatting_data) print(new_colorspace_name) "proj01_01sh010"

Parameters:

Name Type Description Default
colorspace_name str

colorspace profile name

required
formatting_data dict

formatting data

required
root_node Optional[Node]

root node

None

Returns:

Name Type Description
str

formatted colorspace profile string ex: "ACES - ACEScg"

Source code in client/ayon_nuke/api/colorspace.py
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def get_formatted_colorspace(
        colorspace_name, formatting_data, root_node=None):
    """Format colorspace profile name into string.

    This method is formatting colorspace profile name. It is iterating
    over all possible combinations of input string which could be separated
    by COLOR_VALUE_SEPARATOR defined in constants.

    If Anatomy template tokens are used but formatting data is not provided,
    it will try any other available variants in next position of the separator.

    This method also validate that the formatted colorspace profile name is
    available in currently run nuke session ocio config.

    Example:
        >>> from ayon_nuke.api.colorspace import get_formatted_colorspace
        >>> colorspace_name = "{project_code}_{context};ACES - ACEScg"
        >>> formatting_data = {
        ...    "context": "01sh010",
        ...    "project_code": "proj01"
        ...}
        >>> new_colorspace_name = get_formatted_colorspace(
        ...    colorspace_name, formatting_data)
        >>> print(new_colorspace_name)
        "proj01_01sh010"


    Args:
        colorspace_name (str): colorspace profile name
        formatting_data (dict): formatting data
        root_node (Optional[nuke.Node]): root node

    Returns:
        str: formatted colorspace profile string
            ex: "ACES - ACEScg"
    """
    if not root_node:
        root_node = nuke.root()

    colorspaces = colorspace_name.split(COLOR_VALUE_SEPARATOR)

    # iterate via all found colorspaces
    for citem in colorspaces:
        # format any template tokens used in the string
        citem_resolved = StringTemplate(citem.strip()).format_strict(
            formatting_data)
        log.debug("Resolved colorspace: `{}`".format(citem_resolved))

        # making sure formatted colorspace exists in running session
        if colorspace_exists_on_node(root_node, citem_resolved):
            return citem_resolved

get_formatted_display_and_view(view_profile, formatting_data, root_node=None)

Format display and view profile into string.

This method is formatting a display and view profile. It is iterating over all possible combinations of display and view colorspaces. Those could be separated by COLOR_VALUE_SEPARATOR defined in constants.

If Anatomy template tokens are used but formatting data is not provided, it will try any other available variants in next position of the separator.

This method also validate that the formatted display and view profile is available in currently run nuke session ocio config.

Example

from ayon_nuke.api.colorspace import get_formatted_display_and_view view_profile = { ... "view": "{context};sRGB", ... "display": "{project_code};ACES" ... } formatting_data = { ... "context": "01sh010", ... "project_code": "proj01" ...} display_and_view = get_formatted_display_and_view( ... view_profile, formatting_data) print(display_and_view) "01sh010 (proj01)"

Parameters:

Name Type Description Default
view_profile dict

view and display profile

required
formatting_data dict

formatting data

required
root_node Optional[Node]

root node

None

Returns:

Name Type Description
str

formatted display and view profile string ex: "sRGB (ACES)"

Source code in client/ayon_nuke/api/colorspace.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def get_formatted_display_and_view(
        view_profile, formatting_data, root_node=None):
    """Format display and view profile into string.

    This method is formatting a display and view profile. It is iterating
    over all possible combinations of display and view colorspaces. Those
    could be separated by COLOR_VALUE_SEPARATOR defined in constants.

    If Anatomy template tokens are used but formatting data is not provided,
    it will try any other available variants in next position of the separator.

    This method also validate that the formatted display and view profile is
    available in currently run nuke session ocio config.

    Example:
        >>> from ayon_nuke.api.colorspace import get_formatted_display_and_view
        >>> view_profile = {
        ...     "view": "{context};sRGB",
        ...     "display": "{project_code};ACES"
        ... }
        >>> formatting_data = {
        ...    "context": "01sh010",
        ...    "project_code": "proj01"
        ...}
        >>> display_and_view = get_formatted_display_and_view(
        ...    view_profile, formatting_data)
        >>> print(display_and_view)
        "01sh010 (proj01)"


    Args:
        view_profile (dict): view and display profile
        formatting_data (dict): formatting data
        root_node (Optional[nuke.Node]): root node

    Returns:
        str: formatted display and view profile string
            ex: "sRGB (ACES)"
    """
    if not root_node:
        root_node = nuke.root()

    views = view_profile["view"].split(COLOR_VALUE_SEPARATOR)

    # display could be optional in case nuke_default ocio config is used
    displays = []
    if view_profile["display"]:
        displays = view_profile["display"].split(COLOR_VALUE_SEPARATOR)

    # generate all possible combination of display/view
    display_views = []
    for view in views:
        # display could be optional in case nuke_default ocio config is used
        if not displays:
            display_views.append(view.strip())
            continue

        for display in displays:
            display_views.append(
                create_viewer_profile_string(
                    view.strip(), display.strip(), path_like=False
                )
            )

    for dv_item in display_views:
        # format any template tokens used in the string
        dv_item_resolved = StringTemplate(dv_item).format_strict(
            formatting_data)
        log.debug("Resolved display and view: `{}`".format(dv_item_resolved))

        # making sure formatted colorspace exists in running session
        if dv_item_resolved in get_display_and_view_colorspaces(root_node):
            return dv_item_resolved

get_formatted_display_and_view_as_dict(view_profile, formatting_data, root_node=None)

Format display and view profile into dict.

This method is formatting a display and view profile. It is iterating over all possible combinations of display and view colorspaces. Those could be separated by COLOR_VALUE_SEPARATOR defined in constants.

If Anatomy template tokens are used but formatting data is not provided, it will try any other available variants in next position of the separator.

This method also validate that the formatted display and view profile is available in currently run nuke session ocio config.

Example

from ayon_nuke.api.colorspace import get_formatted_display_and_view_as_dict # noqa view_profile = { ... "view": "{context};sRGB", ... "display": "{project_code};ACES" ... } formatting_data = { ... "context": "01sh010", ... "project_code": "proj01" ...} display_and_view = get_formatted_display_and_view_as_dict( ... view_profile, formatting_data) print(display_and_view) {"view": "01sh010", "display": "proj01"}

Parameters:

Name Type Description Default
view_profile dict

view and display profile

required
formatting_data dict

formatting data

required
root_node Optional[Node]

root node

None

Returns:

Name Type Description
dict

formatted display and view profile in dict ex: {"view": "sRGB", "display": "ACES"}

Source code in client/ayon_nuke/api/colorspace.py
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def get_formatted_display_and_view_as_dict(
        view_profile, formatting_data, root_node=None):
    """Format display and view profile into dict.

    This method is formatting a display and view profile. It is iterating
    over all possible combinations of display and view colorspaces. Those
    could be separated by COLOR_VALUE_SEPARATOR defined in constants.

    If Anatomy template tokens are used but formatting data is not provided,
    it will try any other available variants in next position of the separator.

    This method also validate that the formatted display and view profile is
    available in currently run nuke session ocio config.

    Example:
        >>> from ayon_nuke.api.colorspace import get_formatted_display_and_view_as_dict  # noqa
        >>> view_profile = {
        ...     "view": "{context};sRGB",
        ...     "display": "{project_code};ACES"
        ... }
        >>> formatting_data = {
        ...    "context": "01sh010",
        ...    "project_code": "proj01"
        ...}
        >>> display_and_view = get_formatted_display_and_view_as_dict(
        ...    view_profile, formatting_data)
        >>> print(display_and_view)
        {"view": "01sh010", "display": "proj01"}


    Args:
        view_profile (dict): view and display profile
        formatting_data (dict): formatting data
        root_node (Optional[nuke.Node]): root node

    Returns:
        dict: formatted display and view profile in dict
            ex: {"view": "sRGB", "display": "ACES"}
    """
    if not root_node:
        root_node = nuke.root()

    views = view_profile["view"].split(COLOR_VALUE_SEPARATOR)

    # display could be optional in case nuke_default ocio config is used
    displays = []
    if view_profile["display"]:
        displays = view_profile["display"].split(COLOR_VALUE_SEPARATOR)

    # generate all possible combination of display/view
    display_views = []
    for view in views:
        # display could be optional in case nuke_default ocio config is used
        if not displays:
            display_views.append({"view": view.strip(), "display": None})
            continue

        for display in displays:
            display_views.append(
                {"view": view.strip(), "display": display.strip()})

    root_display_and_view = get_display_and_view_colorspaces(root_node)
    for dv_item in display_views:
        # format any template tokens used in the string
        view = StringTemplate.format_strict_template(
            dv_item["view"], formatting_data
        )
        # for config without displays - nuke_default
        test_string = view
        display = dv_item["display"]
        if display:
            display = StringTemplate.format_strict_template(
                display, formatting_data
            )
            test_string = create_viewer_profile_string(
                view, display, path_like=False
            )

        log.debug(f"Resolved View: '{view}' Display: '{display}'")

        # Make sure formatted colorspace exists in running ocio config session
        if test_string in root_display_and_view:
            return {
                "view": view,
                "display": display,
            }