Skip to content

open_rv_session

OpenRVSessionAction

Bases: LoaderActionPlugin

Open selected RV representation as an RV session.

Source code in client/ayon_openrv/plugins/load_actions/open_rv_session.py
 16
 17
 18
 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
class OpenRVSessionAction(LoaderActionPlugin):
    """Open selected RV representation as an RV session."""

    identifier = "openrv.open-rv-session"

    def get_action_items(
        self,
        selection: LoaderActionSelection,
    ) -> list[LoaderActionItem]:
        items = []
        for representation in self._get_selected_representations(selection):
            if representation["name"].lower() != "rv":
                continue

            items.append(
                LoaderActionItem(
                    label="Open RV session",
                    order=0,
                    data={"representation_id": representation["id"]},
                    icon={
                        "type": "material-symbols",
                        "name": "live_tv",
                        "color": "#FFA500",
                    },
                )
            )
        return items

    def execute_action(
        self,
        selection: LoaderActionSelection,
        data: dict[str, Any],
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        representation_id = data.get("representation_id")
        if not representation_id:
            return LoaderActionResult(
                "Missing representation id.",
                success=False,
            )

        representation = next(
            iter(selection.entities.get_representations({representation_id})),
            None,
        )
        if not representation:
            return LoaderActionResult(
                "Failed to find representation in AYON.",
                success=False,
            )

        workfile_path = get_representation_path_with_anatomy(
            representation,
            selection.project_anatomy,
        )
        if not workfile_path:
            return LoaderActionResult(
                "Failed to resolve representation path.",
                success=False,
            )
        if not os.path.exists(workfile_path):
            return LoaderActionResult(
                f"RV session file was not found: {workfile_path}",
                success=False,
            )

        # Open workfile
        host = registered_host()
        host.open_workfile(workfile_path)
        # Unset session to avoid user saving into it
        rv.commands.setSessionFileName("")

        return LoaderActionResult("Opened RV session in OpenRV.", success=True)

    def _get_selected_representations(
        self,
        selection: LoaderActionSelection,
    ) -> list[dict[str, Any]]:
        if selection.selected_type == "representation":
            return selection.entities.get_representations(
                selection.selected_ids
            )
        if selection.selected_type == "version":
            return selection.entities.get_versions_representations(
                selection.selected_ids
            )
        return []