Skip to content

open_in_djv

DJVViewAction

Bases: LocalAction

Launch DJVView action.

Source code in client/ayon_djv/plugins/ftrack/open_in_djv.py
 13
 14
 15
 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
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 DJVViewAction(LocalAction):
    """Launch DJVView action."""
    identifier = "djvview-launch-action"
    label = "DJV View"
    description = "DJV View Launcher"
    icon = get_djv_icon_url()

    type = "Application"

    allowed_types = {
        ext.lstrip(".")
        for ext in set(IMAGE_EXTENSIONS) | set(VIDEO_EXTENSIONS)
    }
    _executable_cache = DJVExecutableCache()

    def discover(self, session, entities, event):
        """Return available actions based on *event*. """
        selection = event["data"].get("selection", [])
        if len(selection) != 1:
            return False

        entityType = selection[0].get("entityType", None)
        if entityType not in ["assetversion", "task"]:
            return False

        return self._executable_cache.get_path() is not None

    def interface(self, session, entities, event):
        if event["data"].get("values", {}):
            return

        entity = entities[0]
        versions = []

        entity_type = entity.entity_type.lower()
        if entity_type == "assetversion":
            if (
                entity[
                    "components"
                ][0]["file_type"][1:] in self.allowed_types
            ):
                versions.append(entity)
        else:
            master_entity = entity
            if entity_type == "task":
                master_entity = entity["parent"]

            for asset in master_entity["assets"]:
                for version in asset["versions"]:
                    # Get only AssetVersion of selected task
                    if (
                        entity_type == "task" and
                        version["task"]["id"] != entity["id"]
                    ):
                        continue
                    # Get only components with allowed type
                    filetype = version["components"][0]["file_type"]
                    if filetype[1:] in self.allowed_types:
                        versions.append(version)

        if len(versions) < 1:
            return {
                "success": False,
                "message": "There are no Asset Versions to open."
            }

        path = self._executable_cache.get_path()
        if not path:
            return {
                "success": False,
                "message": "Couldn't find DJV executable."
            }

        version_items = []
        base_label = "v{0} - {1} - {2}"
        default_component = None
        last_available = None
        select_value = None
        for version in versions:
            for component in version["components"]:
                label = base_label.format(
                    str(version["version"]).zfill(3),
                    version["asset"]["type"]["name"],
                    component["name"]
                )

                try:
                    location = component[
                        "component_locations"
                    ][0]["location"]
                    file_path = location.get_filesystem_path(component)
                except Exception:
                    file_path = component[
                        "component_locations"
                    ][0]["resource_identifier"]

                if os.path.isdir(os.path.dirname(file_path)):
                    last_available = file_path
                    if component["name"] == default_component:
                        select_value = file_path
                    version_items.append(
                        {"label": label, "value": file_path}
                    )

        if len(version_items) == 0:
            return {
                "success": False,
                "message": (
                    "There are no Asset Versions with accessible path."
                )
            }

        item = {
            "label": "Items to view",
            "type": "enumerator",
            "name": "path",
            "data": sorted(
                version_items,
                key=itemgetter("label"),
                reverse=True
            )
        }
        if select_value is not None:
            item["value"] = select_value
        else:
            item["value"] = last_available

        return {"items": [item]}

    def launch(self, session, entities, event):
        """Callback method for DJVView action."""

        # Launching application
        event_values = event["data"].get("values")
        if not event_values:
            return

        executable = self._executable_cache.get_path()
        if not executable:
            return {
                "success": False,
                "message": "Couldn't find DJV executable."
            }

        filpath = os.path.normpath(event_values["path"])

        cmd = [
            # DJV path
            str(executable),
            # PATH TO COMPONENT
            filpath
        ]
        self.log.info(f"Opening: {cmd}")

        try:
            # Run DJV with these commands
            run_detached_process(cmd)

        except FileNotFoundError:
            return {
                "success": False,
                "message": "File \"{}\" was not found.".format(
                    os.path.basename(filpath)
                )
            }

        return True

discover(session, entities, event)

Return available actions based on event.

Source code in client/ayon_djv/plugins/ftrack/open_in_djv.py
28
29
30
31
32
33
34
35
36
37
38
def discover(self, session, entities, event):
    """Return available actions based on *event*. """
    selection = event["data"].get("selection", [])
    if len(selection) != 1:
        return False

    entityType = selection[0].get("entityType", None)
    if entityType not in ["assetversion", "task"]:
        return False

    return self._executable_cache.get_path() is not None

launch(session, entities, event)

Callback method for DJVView action.

Source code in client/ayon_djv/plugins/ftrack/open_in_djv.py
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
def launch(self, session, entities, event):
    """Callback method for DJVView action."""

    # Launching application
    event_values = event["data"].get("values")
    if not event_values:
        return

    executable = self._executable_cache.get_path()
    if not executable:
        return {
            "success": False,
            "message": "Couldn't find DJV executable."
        }

    filpath = os.path.normpath(event_values["path"])

    cmd = [
        # DJV path
        str(executable),
        # PATH TO COMPONENT
        filpath
    ]
    self.log.info(f"Opening: {cmd}")

    try:
        # Run DJV with these commands
        run_detached_process(cmd)

    except FileNotFoundError:
        return {
            "success": False,
            "message": "File \"{}\" was not found.".format(
                os.path.basename(filpath)
            )
        }

    return True

register(session)

Register hooks.

Source code in client/ayon_djv/plugins/ftrack/open_in_djv.py
182
183
184
185
def register(session):
    """Register hooks."""

    DJVViewAction(session).register()