Skip to content

load_frames

Loader for image sequences and single frames in OpenRV.

FramesLoader

Bases: LoaderPlugin

Load frames into OpenRV.

Source code in client/ayon_openrv/plugins/load/openrv/load_frames.py
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
class FramesLoader(load.LoaderPlugin):
    """Load frames into OpenRV."""

    label = "Load Frames"
    product_types: ClassVar[set] = {"*"}
    representations: ClassVar[set] = {"*"}
    extensions: ClassVar[set] = {ext.lstrip(".") for ext in IMAGE_EXTENSIONS}
    order = 0

    icon = "code-fork"
    color = "orange"

    def load(
        self,
        context: dict,
        name: str | None = None,
        namespace: str | None = None,
        options: dict | None = None,
    ) -> None:
        """Load the frames into OpenRV."""
        filepath = rv.commands.sequenceOfFile(
            self.filepath_from_context(context),
        )[0]

        rep_name = os.path.basename(filepath)

        # change path
        namespace = namespace or context["folder"]["name"]
        loaded_node = rv.commands.addSourceVerbose([filepath])

        node = self._finalize_loaded_node(loaded_node, rep_name, filepath)

        # update colorspace
        self.set_representation_colorspace(node, context["representation"])

        imprint_container(
            node,
            name=name,
            namespace=namespace,
            context=context,
            loader=self.__class__.__name__,
        )

    def _finalize_loaded_node(self, loaded_node, rep_name, filepath):
        """Finalize the loaded node in OpenRV.

        We are organizing all loaded sources under a switch group so we can
        let user switch between versions later on. Every new updated verion is
        added as new media representation under the switch group.

        We are removing firstly added source since it does not have a name.

        Args:
            loaded_node (str): The node that was loaded.
            rep_name (str): The name of the representation.
            filepath (str): The path of the representation.

        Returns:
            str: The node that was loaded.

        """
        node = loaded_node

        rv.commands.addSourceMediaRep(
            loaded_node,
            rep_name,
            [filepath],
        )
        rv.commands.setActiveSourceMediaRep(
            loaded_node,
            rep_name,
        )
        switch_node = rv.commands.sourceMediaRepSwitchNode(loaded_node)
        node_type = rv.commands.nodeType(switch_node)

        for node in rv.commands.sourceMediaRepsAndNodes(switch_node):
            source_node_name = node[0]
            source_node = node[1]
            node_type = rv.commands.nodeType(source_node)
            node_gorup = rv.commands.nodeGroup(source_node)

            # we are removing the firstly added wource since it does not have
            # a name and we don't want to confuse the user with multiple
            # versions of the same source but one of them without a name
            if node_type == "RVFileSource" and source_node_name == "":
                rv.commands.deleteNode(node_gorup)
            else:
                node = source_node
                break

        rv.commands.setStringProperty(f"{node}.media.name", [rep_name], True)

        rv.commands.reload()
        return node

    def update(self, container: dict, context: dict) -> None:
        """Update loaded container."""
        node = container["node"]
        filepath = rv.commands.sequenceOfFile(
            self.filepath_from_context(context),
        )[0]

        repre_entity = context["representation"]

        new_rep_name = os.path.basename(filepath)
        source_reps = rv.commands.sourceMediaReps(node)
        self.log.warning(f">> source_reps: {source_reps}")

        if new_rep_name not in source_reps:
            # change path
            rv.commands.addSourceMediaRep(
                node,
                new_rep_name,
                [filepath],
            )
        else:
            self.log.warning(">> new_rep_name already in source_reps")

        rv.commands.setActiveSourceMediaRep(
            node,
            new_rep_name,
        )
        source_rep_name = rv.commands.sourceMediaRep(node)
        self.log.info(f"New source_rep_name: {source_rep_name}")

        # update colorspace
        self.set_representation_colorspace(node, context["representation"])

        # add data for inventory manager
        rv.commands.setStringProperty(
            f"{node}.ayon.representation",
            [repre_entity["id"]],
            True,
        )
        rv.commands.reload()

    def remove(self, container: dict) -> None:  # noqa: PLR6301
        """Remove loaded container."""
        node = container["node"]
        # since we are organizing all loaded sources under a switch group
        # we need to remove all the source nodes organized under it
        switch_node = rv.commands.sourceMediaRepSwitchNode(node)
        if not switch_node:
            # just in case someone removed it maunally
            return

        for node in rv.commands.sourceMediaRepsAndNodes(switch_node):
            source_node_name = node[0]
            source_node = node[1]
            node_type = rv.commands.nodeType(source_node)
            node_group = rv.commands.nodeGroup(source_node)

            if node_type == "RVFileSource":
                self.log.info(f"Removing: {source_node_name}")
                rv.commands.deleteNode(node_group)

        rv.commands.reload()
        # switch node is child of some other node. find its parent node
        parent_node = rv.commands.nodeGroup(switch_node)
        if parent_node:
            self.log.info(f"Removing: {parent_node}")
            rv.commands.deleteNode(parent_node)

    @staticmethod
    def set_representation_colorspace(node: str, representation: dict) -> None:
        """Set colorspace based on representation data."""
        colorspace_data = representation.get("data", {}).get("colorspaceData")
        if colorspace_data:
            colorspace = colorspace_data["colorspace"]
            # TODO: Confirm colorspace is valid in current OCIO config
            #   otherwise errors will be spammed from OpenRV for invalid space
            group = rv.commands.nodeGroup(node)

            # Enable OCIO for the node and set the colorspace
            set_group_ocio_active_state(group, state=True)
            set_group_ocio_colorspace(group, colorspace)

    def switch(self, container, context):
        self.update(container, context)

load(context, name=None, namespace=None, options=None)

Load the frames into OpenRV.

Source code in client/ayon_openrv/plugins/load/openrv/load_frames.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def load(
    self,
    context: dict,
    name: str | None = None,
    namespace: str | None = None,
    options: dict | None = None,
) -> None:
    """Load the frames into OpenRV."""
    filepath = rv.commands.sequenceOfFile(
        self.filepath_from_context(context),
    )[0]

    rep_name = os.path.basename(filepath)

    # change path
    namespace = namespace or context["folder"]["name"]
    loaded_node = rv.commands.addSourceVerbose([filepath])

    node = self._finalize_loaded_node(loaded_node, rep_name, filepath)

    # update colorspace
    self.set_representation_colorspace(node, context["representation"])

    imprint_container(
        node,
        name=name,
        namespace=namespace,
        context=context,
        loader=self.__class__.__name__,
    )

remove(container)

Remove loaded container.

Source code in client/ayon_openrv/plugins/load/openrv/load_frames.py
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
def remove(self, container: dict) -> None:  # noqa: PLR6301
    """Remove loaded container."""
    node = container["node"]
    # since we are organizing all loaded sources under a switch group
    # we need to remove all the source nodes organized under it
    switch_node = rv.commands.sourceMediaRepSwitchNode(node)
    if not switch_node:
        # just in case someone removed it maunally
        return

    for node in rv.commands.sourceMediaRepsAndNodes(switch_node):
        source_node_name = node[0]
        source_node = node[1]
        node_type = rv.commands.nodeType(source_node)
        node_group = rv.commands.nodeGroup(source_node)

        if node_type == "RVFileSource":
            self.log.info(f"Removing: {source_node_name}")
            rv.commands.deleteNode(node_group)

    rv.commands.reload()
    # switch node is child of some other node. find its parent node
    parent_node = rv.commands.nodeGroup(switch_node)
    if parent_node:
        self.log.info(f"Removing: {parent_node}")
        rv.commands.deleteNode(parent_node)

set_representation_colorspace(node, representation) staticmethod

Set colorspace based on representation data.

Source code in client/ayon_openrv/plugins/load/openrv/load_frames.py
182
183
184
185
186
187
188
189
190
191
192
193
194
@staticmethod
def set_representation_colorspace(node: str, representation: dict) -> None:
    """Set colorspace based on representation data."""
    colorspace_data = representation.get("data", {}).get("colorspaceData")
    if colorspace_data:
        colorspace = colorspace_data["colorspace"]
        # TODO: Confirm colorspace is valid in current OCIO config
        #   otherwise errors will be spammed from OpenRV for invalid space
        group = rv.commands.nodeGroup(node)

        # Enable OCIO for the node and set the colorspace
        set_group_ocio_active_state(group, state=True)
        set_group_ocio_colorspace(group, colorspace)

update(container, context)

Update loaded container.

Source code in client/ayon_openrv/plugins/load/openrv/load_frames.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def update(self, container: dict, context: dict) -> None:
    """Update loaded container."""
    node = container["node"]
    filepath = rv.commands.sequenceOfFile(
        self.filepath_from_context(context),
    )[0]

    repre_entity = context["representation"]

    new_rep_name = os.path.basename(filepath)
    source_reps = rv.commands.sourceMediaReps(node)
    self.log.warning(f">> source_reps: {source_reps}")

    if new_rep_name not in source_reps:
        # change path
        rv.commands.addSourceMediaRep(
            node,
            new_rep_name,
            [filepath],
        )
    else:
        self.log.warning(">> new_rep_name already in source_reps")

    rv.commands.setActiveSourceMediaRep(
        node,
        new_rep_name,
    )
    source_rep_name = rv.commands.sourceMediaRep(node)
    self.log.info(f"New source_rep_name: {source_rep_name}")

    # update colorspace
    self.set_representation_colorspace(node, context["representation"])

    # add data for inventory manager
    rv.commands.setStringProperty(
        f"{node}.ayon.representation",
        [repre_entity["id"]],
        True,
    )
    rv.commands.reload()