Skip to content

load_texture

SubstanceLoadProjectImage

Bases: LoaderPlugin

Load Texture for project

Source code in client/ayon_substancedesigner/plugins/load/load_texture.py
 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
class SubstanceLoadProjectImage(load.LoaderPlugin):
    """Load Texture for project"""

    product_types = {"image", "textures"}
    representations = {"*"}

    label = "Load Texture"
    order = -10
    icon = "code-fork"
    color = "orange"


    @classmethod
    def get_options(cls, contexts):
        return [
            EnumDef(
                    "resource_loading_options",
                    label="Resource Loading Options",
                    items={
                        1: "Linked",
                        2: "CopiedAndLinked",
                        3: "BinaryEmbedded",
                    },
                    default=1
            ),
        ]

    def load(self, context, name, namespace, options):
        current_package = get_package_from_current_graph()
        filepath = self.filepath_from_context(context)
        resource_embed_method = options.get("resource_loading_options", 1)
        import_options = {
            "resource_loading_options": resource_embed_method
        }
        identifier = self.import_texture(
            filepath, context, current_package, resource_embed_method)
        imprint(
            current_package, name, namespace,
            context, loader=self, identifier=identifier,
            options=import_options
        )

    def update(self, container, context):
        # As the filepath for SD Resource file is read-only data.
        # the update function cannot directly set the textures
        # accordingly to the versions in the existing SD Resource
        # Therefore, new resource version of the bitmap would be
        # created when updating
        current_package = get_package_from_current_graph()
        filepath = self.filepath_from_context(context)
        resource_embed_method = int(container["resource_loading_options"])
        options = {
            "resource_loading_options": resource_embed_method
        }
        identifier = self.import_texture(
            filepath, context, current_package, resource_embed_method)
        imprint(
            current_package,
            container["name"],
            container.get("namespace", None),
            context,
            loader=self,
            identifier=identifier,
            options=options
        )

    def remove(self, container):
        # TODO: Supports the check across different packages if needed
        current_package = get_package_from_current_graph()
        for resource in current_package.getChildrenResources(True):
            if resource.getClassName() == "SDResourceBitmap":
                if resource.getIdentifier() == container["objectName"]:
                    resource.delete()
        remove_container_metadata(container)

    def import_texture(self, filepath, context,
                       current_package, resource_embed_method):
        project_name = context["project"]["name"]
        filename = os.path.splitext(os.path.basename(filepath))[0]
        # identifier would convert "." to "_", this makes sure
        # container data taking correct identifier value
        identifier = filename.replace(".", "_")
        if not has_resource_file(current_package):
            resource_folder = sd.api.sdresourcefolder.SDResourceFolder.sNew(
                current_package)
            resource_folder.setIdentifier(f"{project_name}_resources")
        else:
            resource_folder = get_resource_folder(current_package)
        bitmap_resource = sd.api.sdresourcebitmap.SDResourceBitmap.sNewFromFile(                # noqa
            resource_folder, filepath,
            sd.api.sdresource.EmbedMethod(resource_embed_method)
        )
        bitmap_resource.setIdentifier(identifier)

        return identifier