Skip to content

open_in_djv

OpenInDJV

Bases: LoaderActionPlugin

Open Image Sequence or Video with system default

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

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

    @classmethod
    def get_djv_path(cls):
        return cls._executable_cache.get_path()

    def get_action_items(
        self, selection: LoaderActionSelection
    ) -> list[LoaderActionItem]:
        if not self.get_djv_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 DJV",
                order=30,
                data={"representation_id": next(iter(repre_ids))},
                icon={
                    "type": "awesome-font",
                    "name": "fa.play-circle",
                    "color": "orange",
                }
            )
            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]:
        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,
            )

        directory = os.path.dirname(repre_path)
        pattern = clique.PATTERNS["frames"]
        files = os.listdir(directory)
        collections, remainder = clique.assemble(
            files,
            patterns=[pattern],
            minimum_items=1
        )

        first_image = repre_path
        if not remainder:
            sequence = collections[0]
            first_image = list(sequence)[0]
        filepath = os.path.normpath(os.path.join(directory, first_image))

        self.log.info(f"Opening '{filepath}'")

        executable = self.get_djv_path()
        if not executable:
            return LoaderActionResult(
                "Couldn't find DJV executable.",
                success=False,
            )

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

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