Skip to content

load_file

FileLoader

Bases: LoaderPlugin

Load images

Stores the imported asset in a container named after the asset.

Source code in client/ayon_wrap/plugins/load/load_file.py
 12
 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
class FileLoader(LoaderPlugin):
    """Load images

    Stores the imported asset in a container named after the asset.
    """
    label = "Load file"

    # TODO
    families = ["image",
                "plate",
                "render",
                "prerender",
                "review",
                "model"]
    representations = ["*"]

    def load(self, context, name=None, namespace=None, data=None):
        # Loader currently not implemented, only SceneInventory
        raise NotImplementedError

    def update(self, container, representation):
        """ Switch asset or change version """
        context = get_current_context()

        nodeId = container["nodeId"]
        with open(container["namespace"], "r") as fp:
            content = json.load(fp)

            metadata = (content.get("metadata", {})
                               .get("AYON_NODE_METADATA", []))
            final_metadata = []
            for item in metadata:
                if item["id"] != AVALON_CONTAINER_ID:
                    final_metadata.append(item)
                    continue
                if item["nodeId"] != nodeId:
                    final_metadata.append(item)
                    continue

                updated = self._update_placeholder_string(container,
                                                          representation)
                repre, filled_value = fill_placeholder(updated,
                                                       container["namespace"],
                                                       context)
                item["representation"] = repre["id"]
                item["original_value"] = updated
                item["name"] = os.path.basename(filled_value)
                item["version"] = repre["context"]["version"]

                final_metadata.append(item)

                for node in content["nodes"].values():
                    if node["nodeId"] != nodeId:
                        continue
                    node["params"]["fileName"]["value"] = filled_value

        self._save_metadata(container["namespace"], content, final_metadata)

    def _update_placeholder_string(self, container, representation):
        orig_token_values = get_token_and_values(
            container["original_value"])
        orig_token_values["version"] = \
            str(representation["context"]["version"])
        orig_token_values["asset_name"] = \
            representation["context"]["asset"]
        orig_token_values["ext"] = representation["context"]["ext"]
        orig_token_values["product_name"] = representation["context"]["subset"]
        updated = ".".join(list(orig_token_values.values()))
        return updated

    def remove(self, container):
        """
            Removes element from scene: deletes layer + removes from Headline
        Args:
            container (dict): container to be removed - used to get layer_id
        """
        nodeId = container["nodeId"]
        with open(container["namespace"], "r") as fp:
            content = json.load(fp)

            metadata = (content.get("metadata", {})
                        .get("AYON_NODE_METADATA", []))
            final_metadata = []
            for item in metadata:
                if item["id"] != AVALON_CONTAINER_ID:
                    final_metadata.append(item)
                    continue
                if item["nodeId"] != nodeId:
                    final_metadata.append(item)
                    continue

        final_nodes = {}
        for node_name, node in content["nodes"].items():
            if node["nodeId"] == nodeId:
                node["params"]["fileName"]["value"] = ""

            final_nodes[node_name] = node
        content["nodes"] = final_nodes

        self._save_metadata(container["namespace"], content, final_metadata)

    def switch(self, container, representation):
        self.update(container, representation)

    def _save_metadata(self, workfile_path, content, final_metadata):
        content["metadata"]["AYON_NODE_METADATA"] = final_metadata
        with open(workfile_path, "w") as fp:
            json.dump(content, fp, indent=4)

remove(container)

Removes element from scene: deletes layer + removes from Headline

Args: container (dict): container to be removed - used to get layer_id

Source code in client/ayon_wrap/plugins/load/load_file.py
 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
def remove(self, container):
    """
        Removes element from scene: deletes layer + removes from Headline
    Args:
        container (dict): container to be removed - used to get layer_id
    """
    nodeId = container["nodeId"]
    with open(container["namespace"], "r") as fp:
        content = json.load(fp)

        metadata = (content.get("metadata", {})
                    .get("AYON_NODE_METADATA", []))
        final_metadata = []
        for item in metadata:
            if item["id"] != AVALON_CONTAINER_ID:
                final_metadata.append(item)
                continue
            if item["nodeId"] != nodeId:
                final_metadata.append(item)
                continue

    final_nodes = {}
    for node_name, node in content["nodes"].items():
        if node["nodeId"] == nodeId:
            node["params"]["fileName"]["value"] = ""

        final_nodes[node_name] = node
    content["nodes"] = final_nodes

    self._save_metadata(container["namespace"], content, final_metadata)

update(container, representation)

Switch asset or change version

Source code in client/ayon_wrap/plugins/load/load_file.py
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
def update(self, container, representation):
    """ Switch asset or change version """
    context = get_current_context()

    nodeId = container["nodeId"]
    with open(container["namespace"], "r") as fp:
        content = json.load(fp)

        metadata = (content.get("metadata", {})
                           .get("AYON_NODE_METADATA", []))
        final_metadata = []
        for item in metadata:
            if item["id"] != AVALON_CONTAINER_ID:
                final_metadata.append(item)
                continue
            if item["nodeId"] != nodeId:
                final_metadata.append(item)
                continue

            updated = self._update_placeholder_string(container,
                                                      representation)
            repre, filled_value = fill_placeholder(updated,
                                                   container["namespace"],
                                                   context)
            item["representation"] = repre["id"]
            item["original_value"] = updated
            item["name"] = os.path.basename(filled_value)
            item["version"] = repre["context"]["version"]

            final_metadata.append(item)

            for node in content["nodes"].values():
                if node["nodeId"] != nodeId:
                    continue
                node["params"]["fileName"]["value"] = filled_value

    self._save_metadata(container["namespace"], content, final_metadata)