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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 | class BaseMediaLoader(load.LoaderPlugin):
"""Base class for the media loaders."""
skip_discovery = True # mark as base class
product_base_types: ClassVar[set] = {"*"}
product_types = product_base_types
representations: ClassVar[set] = {"*"}
order = 0
def load(
self,
context: dict,
name: str | None = None,
namespace: str | None = None,
options: dict | None = None,
) -> None:
"""Load frames or video into OpenRV."""
filepath = self.filepath_from_context(context)
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,
context
)
# update colorspace
self.set_representation_colorspace(node, context["representation"])
imprint_container(
node,
name=name,
namespace=namespace,
context=context,
loader=self.__class__.__name__,
)
rv.commands.sendInternalEvent(
"ayon-source-loaded", str(node), self.__class__.__name__
)
def _finalize_loaded_node(self, loaded_node, rep_name, filepath, context):
"""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 version 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.
context (dict[str, dict[str, Any]]): The context of the loaded
representation.
Returns:
str: The node that was loaded.
"""
node = loaded_node
self._add_source_media_rep(node, rep_name, filepath)
rv.commands.setActiveSourceMediaRep(
loaded_node,
rep_name,
)
switch_node = rv.commands.sourceMediaRepSwitchNode(loaded_node)
for (
source_node_name,
source_node,
) in rv.commands.sourceMediaRepsAndNodes(switch_node):
node_type = rv.commands.nodeType(source_node)
node_group = rv.commands.nodeGroup(source_node)
# We are removing the first added source 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_group)
else:
node = source_node
break
# Rename the switch node and group as well for better debugging
folder = context["folder"]
product = context["product"]
label = f"{folder['path']} > {product['name']}"
rv.extra_commands.setUIName(
switch_node,
f"{label} Switch"
)
rv.extra_commands.setUIName(
rv.commands.nodeGroup(switch_node),
f"{label} Switch Group"
)
rv.commands.reload()
return node
def _add_source_media_rep(self, node, rep_name, filepath):
"""Add source media representation to the node."""
# Add source to the media rep to add it to the `switch node`
added_source = rv.commands.addSourceMediaRep(
node,
rep_name,
[filepath],
)
# Avoid long names on the added source media rep
rv.extra_commands.setUIName(
rv.commands.nodeGroup(added_source),
rep_name
)
def update(self, container, context):
node = container["node"]
filepath = self.filepath_from_context(context)
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:
# add version to the switch group if it's not there yet
self._add_source_media_rep(node, new_rep_name, filepath)
else:
self.log.warning(
f"New rep name already in source_reps: {new_rep_name}"
)
# activate the new version in the switch group
rv.commands.setActiveSourceMediaRep(
node,
new_rep_name,
)
source_rep_name = rv.commands.sourceMediaRep(node)
self.log.debug(f"New source_rep_name: {source_rep_name}")
# update colorspace
representation = context["representation"]
self.set_representation_colorspace(node, 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 (
source_node_name,
source_node
) in rv.commands.sourceMediaRepsAndNodes(switch_node):
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)
# 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)
rv.commands.reload()
def switch(self, container, context):
self.update(container, context)
def set_representation_colorspace(self, node, representation):
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
self.log.info(f"Setting colorspace: {colorspace}")
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)
|