|  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 | class WrapCreator(TrayPublishCreator):
    """"Parses Wrap workfile, looks for all writer nodes names starting `AYON_`
    Expected format of write node name:
        `AYON_productName_extension` - eg `AYON_modelMain_abc`
    Triggers separate creators inheriting from 'WrapProductBaseCreator' which
    provides different icons and separation into product_types blocks.
    """
    # no API for Wrap, could get only triggered in Traypublisher
    host_name = "traypublisher"
    identifier = "wrap"
    product_type = None
    label = "Wrap"
    default_variant = "Main"
    extensions = ["wrap"]
    implemented_product_types = [
        "model", "dataTransform", "dataPD", "render", "image"
    ]
    def create(
        self, product_name: str, instance_data: dict, pre_create_data: dict
    ):
        file_paths = pre_create_data.get("filepath")
        if not file_paths:
            return
        for file_info in file_paths:
            instance_data = copy.deepcopy(instance_data)
            file_name = file_info["filenames"][0]
            workfile_path = os.path.join(file_info["directory"], file_name)
            wrap_instance_data = {"workfile_path": workfile_path}
            with open(workfile_path, "r") as fp:
                content = json.load(fp)
                for node_name, node in content["nodes"].items():
                    if not node_name.startswith("AYON_"):
                        continue
                    product_name = self._get_product_name(node_name)
                    product_type = self._get_product_type(
                        node_name, product_name)
                    wrap_instance_data["nodeId"] = node["nodeId"]
                    wrap_instance_data["nodeName"] = node_name
                    wrap_instance_data["output_file_path"] = (
                        node["params"]["fileName"]["value"])
                    instance_data["wrap"] = wrap_instance_data
                    self._create_instance(instance_data, product_name,
                                          product_type)
            instance_data = self._create_workfile_instance(
                instance_data, workfile_path)
    def _create_workfile_instance(self, instance_data, workfile_path):
        instance_data = copy.deepcopy(instance_data)
        instance_data["workfile_path"] = workfile_path
        product_type = "workfile"
        product_name = "workfileMain"
        self._create_instance(instance_data, product_name,
                              product_type)
        return instance_data
    def _create_instance(self, instance_data, product_name, product_type):
        creator_identifier = f"wrap_{product_type}"
        wrap_creator = self.create_context.creators[
            creator_identifier]
        _new_instance = wrap_creator.create(
            product_name,
            instance_data,
        )
    def _get_product_name(self, node_name):
        """Parses node name by '_'
        Raises:
            (CreatorError) - if name doesn't match expected format
        """
        node_parts = node_name.split("_")
        if len(node_parts) != 3:
            raise CreatorError(
                f"'{node_name} doesn't match to "
                "expected write node format "
                "'AYON_productName_extension'"
            )
        product_name = node_parts[1]
        return product_name
    def _get_product_type(self, node_name, product_name):
        """Queries product type from list of implemented
        Node name contains full product_name, which must be stripped to get
        product_type.
        Raises:
            (CreatorError) - if name doesn't match expected format
        """
        product_type = None
        for impl_product_type in self.implemented_product_types:
            if product_name.startswith(impl_product_type):
                product_type = impl_product_type
                break
        if not product_type:
            raise CreatorError(
                f"'{node_name} doesn't match to values from "
                f"'{self.implemented_product_types}'"
            )
        return product_type
    def get_instance_attr_defs(self):
        return [
            FileDef(
                "filepath",
                folders=False,
                single_item=False,
                extensions=self.extensions,
                allow_sequences=False,
                label="Filepath",
                hidden=True
            )
        ]
    def get_pre_create_attr_defs(self):
        # Use same attributes as for instance attributes
        return [
            FileDef(
                "filepath",
                folders=False,
                single_item=False,
                extensions=self.extensions,
                allow_sequences=False,
                label="Filepath"
            )
        ]
    def get_detail_description(self):
        return """# Create instances from Wrap Save nodes
        Looks for writer nodes with names starting by `AYON_`. Tries to match
        them to final 'product_type' by values in 
        `self.implemented_product_types`. 
        Eg. 'AYON_modelMain_abc' name produces instance of 'model' family with
        'renderMain' product name.
        """
    def get_icon(self):
        return "fa.file"
 |