Skip to content

open_in_xstudio

OpenInXStudio

Bases: LoaderActionPlugin

Open Image Sequence or Video with system default

Source code in client/ayon_xstudio/plugins/loader/open_in_xstudio.py
 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
class OpenInXStudio(LoaderActionPlugin):
    """Open Image Sequence or Video with system default"""
    identifier = "xstudio.open-in-xstudio"

    _executable_cache = XStudioExecutableCache()
    extensions = set(IMAGE_EXTENSIONS) | set(VIDEO_EXTENSIONS)

    @classmethod
    def get_xstudio_path(cls) -> Optional[str]:
        """Get the path to the xStudio executable.

        Returns:
            str: The path to the xStudio executable.
        """
        return cls._executable_cache.get_path()

    def get_action_items(
        self, selection: LoaderActionSelection
    ) -> list[LoaderActionItem]:
        if not self.get_xstudio_path():
            return []

        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 []

        repre_ids_by_name = collections.defaultdict(set)
        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}"
                if ext not in self.extensions:
                    continue
                name = repre["name"]
                repre_ids_by_name[name].add(repre["id"])

        if not repre_ids_by_name:
            return []

        return [
            LoaderActionItem(
                label=repre_name,
                group_label="Open in xStudio",
                order=-10,
                data={"representation_id": next(iter(repre_ids))},
                icon={
                    "type": "ayon_url",
                    "url": get_base_xstudio_icon_url(),
                },
            )
            for repre_name, repre_ids in repre_ids_by_name.items()
            if len(repre_ids) == 1
        ]

    def execute_action(
        self,
        selection: LoaderActionSelection,
        data: dict[str, Any],
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        executable = self.get_xstudio_path()
        if not executable:
            return LoaderActionResult(
                "Couldn't find xStudio executable.",
                success=False,
            )

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

        repre_path = get_representation_path_with_anatomy(
            repre, selection.get_project_anatomy()
        )
        if not repre_path:
            return LoaderActionResult(
                "Failed to fill representation path...",
                success=False,
            )

        if not os.path.exists(repre_path):
            return LoaderActionResult(
                "File to open was not found...",
                success=False,
            )

        fdir, fname = os.path.split(repre_path)
        pattern = clique.PATTERNS["frames"]
        files = os.listdir(fdir)
        collections: list[Any]
        remainder: list[str]
        collections, remainder = clique.assemble(
            files, patterns=[pattern], minimum_items=1
        )
        first_image: Optional[str] = None
        for other_file in remainder:
            if other_file == fname:
                first_image = other_file
                break

        if not first_image:
            seq = collections[0]
            # NOTE: clique padding == 0 when the seq number does not start
            # with 0. "1001" has a padding of 0 and "0101" has a padding of 1.
            # we output the seq path with hashes.
            idxs = list(seq.indexes)
            pad = "#" * (seq.padding + len(str(idxs[0])))
            first_image = (
                f"{seq.head}{pad}{seq.tail}={idxs[0]}-{idxs[-1]}"
            )

        filepath = os.path.normpath(os.path.join(fdir, first_image))
        self.log.info("Opening xStudio with : %s", filepath)

        cmd: list[str] = [
            # xStudio path
            str(executable),
            # PATH TO COMPONENT
            filepath,
        ]
        # Run XStudio with these commands
        run_detached_process(cmd)
        # Keep process in memory for some time
        time.sleep(0.1)

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

get_xstudio_path() classmethod

Get the path to the xStudio executable.

Returns:

Name Type Description
str Optional[str]

The path to the xStudio executable.

Source code in client/ayon_xstudio/plugins/loader/open_in_xstudio.py
31
32
33
34
35
36
37
38
@classmethod
def get_xstudio_path(cls) -> Optional[str]:
    """Get the path to the xStudio executable.

    Returns:
        str: The path to the xStudio executable.
    """
    return cls._executable_cache.get_path()