Skip to content

load_uasset

Load UAsset.

UAssetLoader

Bases: Loader

Load UAsset.

Source code in client/ayon_unreal/plugins/load/load_uasset.py
 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
class UAssetLoader(plugin.Loader):
    """Load UAsset."""

    product_types = {"uasset"}
    label = "Load UAsset"
    representations = {"uasset"}
    icon = "cube"
    color = "orange"

    extension = "uasset"

    loaded_asset_dir = "{folder[path]}/{product[name]}_{version[version]}"
    loaded_asset_name = "{folder[name]}_{product[name]}_{version[version]}_{representation[name]}"       # noqa

    @classmethod
    def apply_settings(cls, project_settings):
        super(UAssetLoader, cls).apply_settings(project_settings)
        # Apply import settings
        unreal_settings = project_settings["unreal"]["import_settings"]
        cls.loaded_asset_dir = unreal_settings["loaded_asset_dir"]

    def load(self, context, name, namespace, options):
        """Load and containerise representation into Content Browser.

        Args:
            context (dict): application context
            name (str): Product name
            namespace (str): in Unreal this is basically path to container.
                             This is not passed here, so namespace is set
                             by `containerise()` because only then we know
                             real path.
            options (dict): Those would be data to be imprinted. This is not
                used now, data are imprinted by `containerise()`.

        Returns:
            list(str): list of container content
        """

        # Create directory for asset and Ayon container
        folder_path = context["folder"]["path"]
        suffix = "_CON"
        asset_root, asset_name = unreal_pipeline.format_asset_directory(
            context, self.loaded_asset_dir, self.loaded_asset_name
        )
        tools = unreal.AssetToolsHelpers().get_asset_tools()
        asset_dir, container_name = tools.create_unique_asset_name(
            asset_root, suffix=""
        )
        container_name = f"{container_name}_{suffix}"

        if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
            unreal.EditorAssetLibrary.make_directory(asset_dir)

        destination_path = parsing_to_absolute_directory(asset_dir)

        path = self.filepath_from_context(context)

        shutil.copy(path, f"{destination_path}/{asset_name}")

        if not unreal.EditorAssetLibrary.does_asset_exist(
            f"{asset_dir}/{container_name}"):
                # Create Asset Container
                unreal_pipeline.create_container(
                    container=container_name, path=asset_dir)

        data = {
            "schema": "ayon:container-2.0",
            "id": AYON_CONTAINER_ID,
            "namespace": asset_dir,
            "folder_path": folder_path,
            "container_name": container_name,
            "asset_name": asset_name,
            "loader": str(self.__class__.__name__),
            "representation": context["representation"]["id"],
            "parent": context["representation"]["versionId"],
            "product_type": context["product"]["productType"],
            # TODO these should be probably removed
            "asset": folder_path,
            "family": context["product"]["productType"],
            "project_name": context["project"]["name"]
        }

        unreal_pipeline.imprint(f"{asset_dir}/{container_name}", data)

        asset_content = unreal.EditorAssetLibrary.list_assets(
            asset_dir, recursive=True, include_folder=True
        )

        for a in asset_content:
            unreal.EditorAssetLibrary.save_asset(a)

        return asset_content

    def update(self, container, context):
        ar = unreal.AssetRegistryHelpers.get_asset_registry()

        asset_dir = container["namespace"]
        repre_entity = context["representation"]

        asset_content = unreal.EditorAssetLibrary.list_assets(
            asset_dir, recursive=False, include_folder=True
        )

        for asset in asset_content:
            obj = ar.get_asset_by_object_path(asset).get_asset()
            if obj.get_class().get_name() != "AyonAssetContainer":
                unreal.EditorAssetLibrary.delete_asset(asset)

        update_filepath = self.filepath_from_context(context)
        new_asset_name = os.path.basename(update_filepath)
        if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
            unreal.EditorAssetLibrary.make_directory(asset_dir)

        destination_path = parsing_to_absolute_directory(asset_dir)

        shutil.copy(
            update_filepath, f"{destination_path}/{new_asset_name}"
        )

        container_path = f'{container["namespace"]}/{container["objectName"]}'
        # update metadata
        unreal_pipeline.imprint(
            container_path,
            {
                "asset_name": new_asset_name,
                "representation": repre_entity["id"],
                "parent": repre_entity["versionId"],
                "project_name": context["project"]["name"]
            }
        )

        asset_content = unreal.EditorAssetLibrary.list_assets(
            asset_dir, recursive=True, include_folder=True
        )

        for a in asset_content:
            unreal.EditorAssetLibrary.save_asset(a)

    def remove(self, container):
        path = container["namespace"]
        if unreal.EditorAssetLibrary.does_directory_exist(path):
            unreal.EditorAssetLibrary.delete_directory(path)

load(context, name, namespace, options)

Load and containerise representation into Content Browser.

Parameters:

Name Type Description Default
context dict

application context

required
name str

Product name

required
namespace str

in Unreal this is basically path to container. This is not passed here, so namespace is set by containerise() because only then we know real path.

required
options dict

Those would be data to be imprinted. This is not used now, data are imprinted by containerise().

required

Returns:

Name Type Description
list str

list of container content

Source code in client/ayon_unreal/plugins/load/load_uasset.py
 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
def load(self, context, name, namespace, options):
    """Load and containerise representation into Content Browser.

    Args:
        context (dict): application context
        name (str): Product name
        namespace (str): in Unreal this is basically path to container.
                         This is not passed here, so namespace is set
                         by `containerise()` because only then we know
                         real path.
        options (dict): Those would be data to be imprinted. This is not
            used now, data are imprinted by `containerise()`.

    Returns:
        list(str): list of container content
    """

    # Create directory for asset and Ayon container
    folder_path = context["folder"]["path"]
    suffix = "_CON"
    asset_root, asset_name = unreal_pipeline.format_asset_directory(
        context, self.loaded_asset_dir, self.loaded_asset_name
    )
    tools = unreal.AssetToolsHelpers().get_asset_tools()
    asset_dir, container_name = tools.create_unique_asset_name(
        asset_root, suffix=""
    )
    container_name = f"{container_name}_{suffix}"

    if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
        unreal.EditorAssetLibrary.make_directory(asset_dir)

    destination_path = parsing_to_absolute_directory(asset_dir)

    path = self.filepath_from_context(context)

    shutil.copy(path, f"{destination_path}/{asset_name}")

    if not unreal.EditorAssetLibrary.does_asset_exist(
        f"{asset_dir}/{container_name}"):
            # Create Asset Container
            unreal_pipeline.create_container(
                container=container_name, path=asset_dir)

    data = {
        "schema": "ayon:container-2.0",
        "id": AYON_CONTAINER_ID,
        "namespace": asset_dir,
        "folder_path": folder_path,
        "container_name": container_name,
        "asset_name": asset_name,
        "loader": str(self.__class__.__name__),
        "representation": context["representation"]["id"],
        "parent": context["representation"]["versionId"],
        "product_type": context["product"]["productType"],
        # TODO these should be probably removed
        "asset": folder_path,
        "family": context["product"]["productType"],
        "project_name": context["project"]["name"]
    }

    unreal_pipeline.imprint(f"{asset_dir}/{container_name}", data)

    asset_content = unreal.EditorAssetLibrary.list_assets(
        asset_dir, recursive=True, include_folder=True
    )

    for a in asset_content:
        unreal.EditorAssetLibrary.save_asset(a)

    return asset_content

UMapLoader

Bases: UAssetLoader

Load Level.

Source code in client/ayon_unreal/plugins/load/load_uasset.py
177
178
179
180
181
182
183
184
class UMapLoader(UAssetLoader):
    """Load Level."""

    product_types = {"uasset"}
    label = "Load Level"
    representations = {"umap"}

    extension = "umap"

parsing_to_absolute_directory(asset_dir)

Converting unreal relative directory to absolute directory

Parameters:

Name Type Description Default
asset_dir str

asset dir

required

Returns:

Name Type Description
str

absolute asset dir

Source code in client/ayon_unreal/plugins/load/load_uasset.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def parsing_to_absolute_directory(asset_dir):
    """Converting unreal relative directory to
    absolute directory

    Args:
        asset_dir (str): asset dir

    Returns:
        str: absolute asset dir
    """
    if unreal_pipeline.AYON_ROOT_DIR in asset_dir:
        return asset_dir.replace(
            "/Game", Path(unreal.Paths.project_content_dir()).as_posix(),
        1)
    else:
        absolute_path = os.path.join(
            unreal.Paths.project_plugins_dir(), asset_dir)
        return os.path.normpath(absolute_path)