Skip to content

copy_file

CopyFileActionPlugin

Bases: LoaderActionPlugin

Copy published file path to clipboard

Source code in client/ayon_core/plugins/loader/copy_file.py
 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
class CopyFileActionPlugin(LoaderActionPlugin):
    """Copy published file path to clipboard"""
    identifier = "core.copy-action"

    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
            )

        output = []
        if not repres:
            return output

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

        for repre_name, repre_ids in repre_ids_by_name.items():
            repre_id = next(iter(repre_ids), None)
            if not repre_id:
                continue
            output.append(
                LoaderActionItem(
                    label=repre_name,
                    order=32,
                    group_label="Copy file path",
                    data={
                        "representation_id": repre_id,
                        "action": "copy-path",
                    },
                    icon={
                        "type": "material-symbols",
                        "name": "content_copy",
                        "color": "#999999",
                    }
                )
            )
            output.append(
                LoaderActionItem(
                    label=repre_name,
                    order=33,
                    group_label="Copy file",
                    data={
                        "representation_id": repre_id,
                        "action": "copy-file",
                    },
                    icon={
                        "type": "material-symbols",
                        "name": "file_copy",
                        "color": "#999999",
                    }
                )
            )
        return output

    def execute_action(
        self,
        selection: LoaderActionSelection,
        data: dict,
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        from qtpy import QtWidgets, QtCore

        action = data["action"]
        repre_id = data["representation_id"]
        repre = next(iter(selection.entities.get_representations({repre_id})))
        path = get_representation_path_with_anatomy(
            repre, selection.get_project_anatomy()
        )
        self.log.info(f"Added file path to clipboard: {path}")

        clipboard = QtWidgets.QApplication.clipboard()
        if not clipboard:
            return LoaderActionResult(
                "Failed to copy file path to clipboard.",
                success=False,
            )

        if action == "copy-path":
            # Set to Clipboard
            clipboard.setText(os.path.normpath(path))

            return LoaderActionResult(
                "Path stored to clipboard...",
                success=True,
            )

        # Build mime data for clipboard
        data = QtCore.QMimeData()
        url = QtCore.QUrl.fromLocalFile(path)
        data.setUrls([url])

        # Set to Clipboard
        clipboard.setMimeData(data)

        return LoaderActionResult(
            "File added to clipboard...",
            success=True,
        )