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
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
292
293
294
295
296
297
298
299 | class OpenRVAddon(AYONAddon, IHostAddon, IPluginPaths):
name = "openrv"
host_name = "openrv"
version = __version__
def get_plugin_paths(self):
return {}
def get_create_plugin_paths(self, host_name):
if host_name != self.host_name:
return []
plugins_dir = os.path.join(OPENRV_ROOT_DIR, "plugins")
return [os.path.join(plugins_dir, "create")]
def get_publish_plugin_paths(self, host_name):
if host_name != self.host_name:
return []
plugins_dir = os.path.join(OPENRV_ROOT_DIR, "plugins")
return [os.path.join(plugins_dir, "publish")]
def get_load_plugin_paths(self, host_name):
loaders_dir = os.path.join(OPENRV_ROOT_DIR, "plugins", "load")
if host_name != self.host_name:
# Other hosts and tray browser
return [os.path.join(loaders_dir, "global")]
# inside OpenRV
return [os.path.join(loaders_dir, "openrv")]
def get_loader_action_plugin_paths(self, host_name=None):
if host_name != self.host_name:
return []
return [
os.path.join(OPENRV_ROOT_DIR, "plugins", "load_actions")
]
def add_implementation_envs(self, env, app):
"""Modify environments to contain all required for implementation."""
# Set default environments if are not set via settings
defaults = {
"AYON_LOG_NO_COLORS": "True"
}
for key, value in defaults.items():
if not env.get(key):
env[key] = value
def get_launch_hook_paths(self, app):
if app.host_name != self.host_name:
return []
return [
os.path.join(OPENRV_ROOT_DIR, "hooks")
]
def get_workfile_extensions(self):
return [".rv"]
def cli(self, addon_click_group):
main_group = click_wrap.group(
self._cli_main, name=self.name, help="OpenRV addon"
)
(
main_group.command(
self._cli_open_workfile,
name="open-workfile",
help="Open a published RV workfile in OpenRV",
)
.option("--project", required=True, help="Project name")
.option(
"--representation",
required=True,
help="Published RV workfile representation id",
)
.option(
"--app",
required=False,
default=None,
help="OpenRV app variant full name (e.g. openrv/2025)",
)
)
(
main_group.command(
self._cli_open_representation,
name="open-representation",
help=(
"Open a published RV workfile or media representation"
" in OpenRV"
),
)
.option("--project", required=True, help="Project name")
.option(
"--representation",
required=True,
help="Published representation id",
)
.option(
"--app",
required=False,
default=None,
help="OpenRV app variant full name (e.g. openrv/2025)",
)
)
addon_click_group.add_command(main_group.to_click_obj())
def _cli_main(self):
ensure_addons_are_process_ready(
addon_name=self.name,
addon_version=self.version,
)
def _cli_open_workfile(
self,
project: str,
app: str,
representation: str
):
project_name = project
repre_entity = ayon_api.get_representation_by_id(
project_name,
representation,
)
if repre_entity is None:
raise RuntimeError(
"Could not find representation by the provided id."
)
repre_path = get_representation_path(project_name, repre_entity)
folder_path, task_name = (
self._get_launch_context_for_representation(
project_name, repre_entity
)
)
self._launch_openrv(
project_name,
folder_path,
task_name,
app_name=app,
workfile_path=repre_path,
# After opening unset the session filename to avoid user
# accidentally saving into the published file.
unset_session_filename=True,
)
def _cli_open_representation(
self,
project: str,
app: str,
representation: str,
):
project_name = project
repre_entity = ayon_api.get_representation_by_id(
project_name,
representation,
)
if repre_entity is None:
raise RuntimeError(
"Could not find representation by the provided id."
)
folder_path, task_name = (
self._get_launch_context_for_representation(
project_name, repre_entity
)
)
if repre_entity["name"] == "rv":
repre_path = get_representation_path(project_name, repre_entity)
self._launch_openrv(
project_name,
folder_path,
task_name,
app_name=app,
workfile_path=repre_path,
unset_session_filename=True,
)
else:
self._launch_openrv(
project_name,
folder_path,
task_name,
app_name=app,
representation_id=representation,
)
def _get_launch_context_for_representation(
self,
project_name: str,
representation: dict[str, str],
) -> tuple[str, str]:
version = ayon_api.get_version_by_id(
project_name,
representation["versionId"],
fields={"productId", "taskId"},
)
if not version:
raise RuntimeError(
f"Could not resolve parent version for representation "
f"'{representation['id']}'."
)
product = ayon_api.get_product_by_id(
project_name,
version["productId"],
fields={"folderId"},
)
if not product:
raise RuntimeError(
f"Could not resolve parent product for representation "
f"'{representation['id']}'."
)
folder = ayon_api.get_folder_by_id(
project_name,
product["folderId"],
fields={"path"},
)
folder_path = folder.get("path") if folder else None
if not folder_path:
raise RuntimeError(
f"Could not resolve folder path for representation "
f"'{representation['id']}'."
)
task_name = None
task_id = version.get("taskId")
if task_id:
task = ayon_api.get_task_by_id(
project_name,
task_id,
fields={"name"},
)
if task:
task_name = task.get("name")
return folder_path, task_name
def _launch_openrv(
self,
project_name: str,
folder_path: str,
task_name: str,
*,
app_name: str | None = None,
workfile_path: str | None = None,
unset_session_filename: bool = False,
representation_id: str | None = None,
):
from ayon_applications import ApplicationManager
app_manager = ApplicationManager()
if not app_name:
openrv_app = app_manager.find_latest_available_variant_for_group(
"openrv"
)
if not openrv_app:
raise RuntimeError(
"No configured OpenRV found in Applications. Ask admin "
"to configure it in "
"ayon+settings://applications/applications/openrv."
)
app_name = openrv_app.full_name
# Directly after opening the workfile, unset the session so the user
# won't accidentally overwrite the passed workfile.
env = os.environ.copy()
if unset_session_filename:
env["AYON_RV_UNSET_SESSION"] = "1"
launch_kwargs = dict(
project_name=project_name,
folder_path=folder_path,
task_name=task_name,
workfile_path=workfile_path,
env=env,
)
# Used by prelaunch hook to load on launch
if representation_id is not None:
launch_kwargs["representation_ids"] = [representation_id]
launch_kwargs["start_last_workfile"] = False
app_manager.launch(app_name, **launch_kwargs)
|