Skip to content

ocio

Helper functions to apply OCIO colorspace settings on groups.

This tries to set the relevant OCIO settings on the group's look and render pipeline similar to what the OpenColorIO Basic Color Management package does in OpenRV through its ocio_source_setup python file.

This assumes that the OpenColorIO Basic Color Management package of RV is both installed and loaded.

OCIONotActiveForGroup

Bases: RuntimeError

Error raised when OCIO is not enabled on the group node.

Source code in client/ayon_openrv/api/ocio.py
20
21
class OCIONotActiveForGroup(RuntimeError):
    """Error raised when OCIO is not enabled on the group node."""

get_group_ocio_file_node(group)

Return OCIOFile node from source group

Source code in client/ayon_openrv/api/ocio.py
31
32
33
34
35
def get_group_ocio_file_node(group):
    """Return OCIOFile node from source group"""
    pipeline = group_member_of_type(group, "RVLinearizePipelineGroup")
    if pipeline:
        return group_member_of_type(pipeline, "OCIOFile")

get_group_ocio_look_node(group)

Return OCIOLook node from source group

Source code in client/ayon_openrv/api/ocio.py
24
25
26
27
28
def get_group_ocio_look_node(group):
    """Return OCIOLook node from source group"""
    pipeline = group_member_of_type(group, "RVLookPipelineGroup")
    if pipeline:
        return group_member_of_type(pipeline, "OCIOLook")

set_current_ocio_active_state(state)

Set the OCIO state for the currently active source.

This is a hacky workaround to enable/disable the OCIO active state for a source since it appears to be that there's no way to explicitly trigger this callback from the ocio_source_setup.OCIOSourceSetupMode instance which does these changes.

Source code in client/ayon_openrv/api/ocio.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def set_current_ocio_active_state(state):
    """Set the OCIO state for the currently active source.

    This is a hacky workaround to enable/disable the OCIO active state for
    a source since it appears to be that there's no way to explicitly trigger
    this callback from the `ocio_source_setup.OCIOSourceSetupMode` instance
    which does these changes.

    """
    # TODO: Make this logic less hacky
    # See: https://community.shotgridsoftware.com/t/how-to-enable-disable-ocio-and-set-ocio-colorspace-for-group-using-python/17178  # noqa

    group = rv.commands.viewNode()
    ocio_node = get_group_ocio_file_node(group)
    if state == bool(ocio_node):
        # Already in correct state
        return

    window = rv.qtutils.sessionWindow()
    menu_bar = window.menuBar()
    for action in menu_bar.actions():
        if action.text() != "OCIO" or action.toolTip() != "OCIO":
            continue

        ocio_menu = action.menu()

        for ocio_action in ocio_menu.actions():
            if ocio_action.toolTip() == "File Color Space":
                # The first entry is for "current source" instead
                # of all sources so we need to break the for loop
                # The first action of the file color space menu
                # is the "Active" action. So lets take that one
                active_action = ocio_action.menu().actions()[0]

                active_action.trigger()
                return

    raise RuntimeError(
        "Unable to set active state for current source. Make "
        "sure the OCIO package is installed and loaded."
    )

set_group_ocio_active_state(group, state)

Set the OCIO state for the 'currently active source'.

This is a hacky workaround to enable/disable the OCIO active state for a source since it appears to be that there's no way to explicitly trigger this callback from the ocio_source_setup.OCIOSourceSetupMode instance which does these changes.

Source code in client/ayon_openrv/api/ocio.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def set_group_ocio_active_state(group, state):
    """Set the OCIO state for the 'currently active source'.

    This is a hacky workaround to enable/disable the OCIO active state for
    a source since it appears to be that there's no way to explicitly trigger
    this callback from the `ocio_source_setup.OCIOSourceSetupMode` instance
    which does these changes.

    """
    ocio_node = get_group_ocio_file_node(group)
    if state == bool(ocio_node):
        # Already in correct state
        return

    with active_view(group):
        set_current_ocio_active_state(state)

set_group_ocio_colorspace(group, colorspace)

Set the group's OCIOFile node ocio.inColorSpace property.

This only works if OCIO is already 'active' for the group. T

Source code in client/ayon_openrv/api/ocio.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def set_group_ocio_colorspace(group, colorspace):
    """Set the group's OCIOFile node ocio.inColorSpace property.

    This only works if OCIO is already 'active' for the group. T

    """
    # RV OCIO package
    import ocio_source_setup  # noqa: F401
    node = get_group_ocio_file_node(group)

    if not node:
        raise OCIONotActiveForGroup(
            "Unable to find OCIOFile node for {}".format(group)
        )

    rv.commands.setStringProperty(
        f"{node}.ocio.inColorSpace", [colorspace], True
    )