Skip to content

open_file

OpenFileAction

Bases: LoaderActionPlugin

Open Image Sequence or Video with system default

Source code in client/ayon_core/plugins/loader/open_file.py
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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
class OpenFileAction(LoaderActionPlugin):
    """Open Image Sequence or Video with system default"""
    identifier = "core.open-file"

    def get_action_items(
        self, selection: LoaderActionSelection
    ) -> list[LoaderActionItem]:
        repres = []
        if selection.selected_type == "representation":
            repres = selection.entities.get_representations(
                selection.selected_ids
            )

        if selection.selected_type == "version":
            repres = selection.entities.get_versions_representations(
                selection.selected_ids
            )

        if not repres:
            return []

        repres_by_ext = collections.defaultdict(list)
        for repre in repres:
            repre_context = repre.get("context")
            if not repre_context:
                continue
            ext = repre_context.get("ext")
            if not ext:
                path = repre["attrib"].get("path")
                if path:
                    ext = os.path.splitext(path)[1]

            if ext:
                ext = ext.lower()
                if not ext.startswith("."):
                    ext = f".{ext}"
                repres_by_ext[ext.lower()].append(repre)

        if not repres_by_ext:
            return []

        filtered_exts = filter_supported_exts(set(repres_by_ext))

        repre_ids_by_name = collections.defaultdict(set)
        for ext in filtered_exts:
            for repre in repres_by_ext[ext]:
                repre_ids_by_name[repre["name"]].add(repre["id"])

        return [
            LoaderActionItem(
                label=repre_name,
                group_label="Open file",
                order=30,
                data={"representation_ids": list(repre_ids)},
                icon={
                    "type": "material-symbols",
                    "name": "file_open",
                    "color": "#ffffff",
                }
            )
            for repre_name, repre_ids in repre_ids_by_name.items()
        ]

    def execute_action(
        self,
        selection: LoaderActionSelection,
        data: dict[str, Any],
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        path = None
        repre_path = None
        repre_ids = data["representation_ids"]
        for repre in selection.entities.get_representations(repre_ids):
            repre_path = get_representation_path_with_anatomy(
                repre, selection.get_project_anatomy()
            )
            if os.path.exists(repre_path):
                path = repre_path
                break

        if path is None:
            if repre_path is None:
                return LoaderActionResult(
                    "Failed to fill representation path...",
                    success=False,
                )
            return LoaderActionResult(
                "File to open was not found...",
                success=False,
            )

        self.log.info(f"Opening: {path}")

        open_file(path)

        return LoaderActionResult(
            "File was opened...",
            success=True,
        )

open_file(filepath)

Open file with system default executable

Source code in client/ayon_core/plugins/loader/open_file.py
252
253
254
255
256
257
258
259
def open_file(filepath: str) -> None:
    """Open file with system default executable"""
    if sys.platform.startswith("darwin"):
        subprocess.call(("open", filepath))
    elif os.name == "nt":
        os.startfile(filepath)
    elif os.name == "posix":
        subprocess.call(("xdg-open", filepath))