Skip to content

load_staticmesh_abc

Loader for Static Mesh alembics.

StaticMeshAlembicLoader

Bases: Loader

Load Unreal StaticMesh from Alembic

Source code in client/ayon_unreal/plugins/load/load_staticmesh_abc.py
 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
class StaticMeshAlembicLoader(plugin.Loader):
    """Load Unreal StaticMesh from Alembic"""

    product_types = {"model", "staticMesh"}
    label = "Import Alembic Static Mesh"
    representations = {"abc"}
    icon = "cube"
    color = "orange"

    abc_conversion_preset = "maya"
    loaded_asset_dir = "{folder[path]}/{product[name]}_{version[version]}"
    show_dialog = False


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

    @classmethod
    def get_options(cls, contexts):
        return [
            EnumDef(
                "abc_conversion_preset",
                label="Alembic Conversion Preset",
                items={
                    "3dsmax": "3dsmax",
                    "maya": "maya",
                    "custom": "custom"
                },
                default=cls.abc_conversion_preset
            ),
            EnumDef(
                "abc_material_settings",
                label="Alembic Material Settings",
                items={
                    "no_material": "Do not apply materials",
                    "create_materials": "Create matarials by face sets",
                    "find_materials": "Search matching materials by face sets",
                },
                default="no_materials"
            ),
            BoolDef(
                "merge_meshes",
                label="Merge Meshes",
                default=True
            )
        ]

    @staticmethod
    def get_task(filename, asset_dir, asset_name, replace, loaded_options):
        task = unreal.AssetImportTask()
        options = unreal.AbcImportSettings()
        sm_settings = unreal.AbcStaticMeshSettings()
        mat_settings = unreal.AbcMaterialSettings()
        conversion_settings = unreal.AbcConversionSettings()

        task.set_editor_property('filename', filename)
        task.set_editor_property('destination_path', asset_dir)
        task.set_editor_property('destination_name', asset_name)
        task.set_editor_property('replace_existing', replace)
        task.set_editor_property(
            'automated', not loaded_options.get("show_dialog"))
        task.set_editor_property('save', True)

        # set import options here
        # Unreal 4.24 ignores the settings. It works with Unreal 4.26
        options.set_editor_property(
            'import_type', unreal.AlembicImportType.STATIC_MESH)

        sm_settings.set_editor_property(
            'merge_meshes', loaded_options.get("merge_meshes", True))

        if loaded_options.get("abc_material_settings") == "create_materials":
            mat_settings.set_editor_property("create_materials", True)
            mat_settings.set_editor_property("find_materials", False)
        elif loaded_options.get("abc_material_settings") == "find_materials":
            mat_settings.set_editor_property("create_materials", False)
            mat_settings.set_editor_property("find_materials", True)
        else:
            mat_settings.set_editor_property("create_materials", False)
            mat_settings.set_editor_property("find_materials", False)


        if not loaded_options.get("default_conversion"):
            conversion_settings = None
            abc_conversion_preset = loaded_options.get("abc_conversion_preset")
            if abc_conversion_preset == "maya":
                if UNREAL_VERSION.major >= 5 and UNREAL_VERSION.minor >= 4:
                    conversion_settings = unreal.AbcConversionSettings(
                        preset=unreal.AbcConversionPreset.MAYA)
                else:
                    conversion_settings = unreal.AbcConversionSettings(
                        preset=unreal.AbcConversionPreset.CUSTOM,
                        flip_u=False, flip_v=True,
                        rotation=[90.0, 0.0, 0.0],
                        scale=[1.0, -1.0, 1.0])
            elif abc_conversion_preset == "3dsmax":
                if UNREAL_VERSION.major >= 5:
                    conversion_settings = unreal.AbcConversionSettings(
                        preset=unreal.AbcConversionPreset.MAX)
                else:
                    conversion_settings = unreal.AbcConversionSettings(
                        preset=unreal.AbcConversionPreset.CUSTOM,
                        flip_u=False, flip_v=True,
                        rotation=[0.0, 0.0, 0.0],
                        scale=[1.0, -1.0, 1.0])
            else:
                data = get_current_project_settings()
                preset = (
                    data["unreal"]["import_settings"]["custom"]
                )
                conversion_settings = unreal.AbcConversionSettings(
                    preset=unreal.AbcConversionPreset.CUSTOM,
                    flip_u=preset["flip_u"],
                    flip_v=preset["flip_v"],
                    rotation=[
                        preset["rot_x"],
                        preset["rot_y"],
                        preset["rot_z"]
                    ],
                    scale=[
                        preset["scl_x"],
                        preset["scl_y"],
                        preset["scl_z"]
                    ]
                )
            options.conversion_settings = conversion_settings

        options.static_mesh_settings = sm_settings
        options.material_settings = mat_settings
        task.options = options

        return task

    def import_and_containerize(
        self, filepath, asset_dir, asset_name, container_name,
        loaded_options, asset_path=None
    ):
        task = None
        if asset_path:
            loaded_asset_dir = unreal.Paths.split(asset_path)[0]
            task = self.get_task(
                filepath, loaded_asset_dir, asset_name, True, loaded_options)
        else:
            if not unreal.EditorAssetLibrary.does_asset_exist(
                f"{asset_dir}/{asset_name}"):
                    task = self.get_task(
                        filepath, asset_dir, asset_name, False, loaded_options)

        unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
        if not unreal.EditorAssetLibrary.does_asset_exist(
            f"{asset_dir}/{container_name}"):
                # Create Asset Container
                create_container(container=container_name, path=asset_dir)

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

    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.
            data (dict): Those would be data to be imprinted.

        Returns:
            list(str): list of container content
        """
        # Create directory for asset and Ayon container
        folder_path = context["folder"]["path"]

        suffix = "_CON"
        path = self.filepath_from_context(context)
        ext = os.path.splitext(path)[-1].lstrip(".")
        asset_root, asset_name = format_asset_directory(context, self.loaded_asset_dir)
        loaded_options = {
            "default_conversion": options.get("default_conversion", False),
            "abc_conversion_preset": options.get(
                "abc_conversion_preset", self.abc_conversion_preset),
            "abc_material_settings": options.get("abc_material_settings", "no_material"),
            "merge_meshes": options.get("merge_meshes", True),
            "show_dialog": options.get("show_dialog", self.show_dialog),
        }

        tools = unreal.AssetToolsHelpers().get_asset_tools()
        asset_dir, container_name = tools.create_unique_asset_name(
            asset_root, suffix=f"_{ext}")

        container_name += suffix
        asset_path = has_asset_directory_pattern_matched(
            asset_name, asset_dir, name, extension=ext)
        if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
            unreal.EditorAssetLibrary.make_directory(asset_dir)
            self.import_and_containerize(path, asset_dir, asset_name,
                                         container_name, loaded_options,
                                         asset_path=asset_path)

        product_type = context["product"]["productType"]
        self.imprint(
            folder_path,
            asset_dir,
            container_name,
            asset_name,
            context["representation"],
            product_type,
            context["project"]["name"]
        )
        if asset_path:
            unreal.EditorAssetLibrary.rename_asset(
                f"{asset_path}",
                f"{asset_dir}/{asset_name}.{asset_name}"
            )
        asset_content = unreal.EditorAssetLibrary.list_assets(
            asset_dir, recursive=True, include_folder=False
        )
        for a in asset_content:
            unreal.EditorAssetLibrary.save_asset(a)

        return asset_content

    def update(self, container, context):
        folder_path = context["folder"]["path"]
        product_type = context["product"]["productType"]
        repre_entity = context["representation"]

        # Create directory for asset and Ayon container
        suffix = "_CON"
        path = get_representation_path(repre_entity)
        ext = os.path.splitext(path)[-1].lstrip(".")
        asset_root, asset_name = format_asset_directory(context, self.loaded_asset_dir)
        tools = unreal.AssetToolsHelpers().get_asset_tools()
        asset_dir, container_name = tools.create_unique_asset_name(
            asset_root, suffix=f"_{ext}")

        container_name += suffix
        if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
            unreal.EditorAssetLibrary.make_directory(asset_dir)
        loaded_options = {
            "default_conversion": False,
            "abc_conversion_preset": self.abc_conversion_preset
        }
        self.import_and_containerize(path, asset_dir, asset_name,
                                     container_name, loaded_options)

        self.imprint(
            folder_path,
            asset_dir,
            container_name,
            asset_name,
            repre_entity,
            product_type,
            context["project"]["name"]
        )

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

        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
data dict

Those would be data to be imprinted.

required

Returns:

Name Type Description
list str

list of container content

Source code in client/ayon_unreal/plugins/load/load_staticmesh_abc.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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.
        data (dict): Those would be data to be imprinted.

    Returns:
        list(str): list of container content
    """
    # Create directory for asset and Ayon container
    folder_path = context["folder"]["path"]

    suffix = "_CON"
    path = self.filepath_from_context(context)
    ext = os.path.splitext(path)[-1].lstrip(".")
    asset_root, asset_name = format_asset_directory(context, self.loaded_asset_dir)
    loaded_options = {
        "default_conversion": options.get("default_conversion", False),
        "abc_conversion_preset": options.get(
            "abc_conversion_preset", self.abc_conversion_preset),
        "abc_material_settings": options.get("abc_material_settings", "no_material"),
        "merge_meshes": options.get("merge_meshes", True),
        "show_dialog": options.get("show_dialog", self.show_dialog),
    }

    tools = unreal.AssetToolsHelpers().get_asset_tools()
    asset_dir, container_name = tools.create_unique_asset_name(
        asset_root, suffix=f"_{ext}")

    container_name += suffix
    asset_path = has_asset_directory_pattern_matched(
        asset_name, asset_dir, name, extension=ext)
    if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
        unreal.EditorAssetLibrary.make_directory(asset_dir)
        self.import_and_containerize(path, asset_dir, asset_name,
                                     container_name, loaded_options,
                                     asset_path=asset_path)

    product_type = context["product"]["productType"]
    self.imprint(
        folder_path,
        asset_dir,
        container_name,
        asset_name,
        context["representation"],
        product_type,
        context["project"]["name"]
    )
    if asset_path:
        unreal.EditorAssetLibrary.rename_asset(
            f"{asset_path}",
            f"{asset_dir}/{asset_name}.{asset_name}"
        )
    asset_content = unreal.EditorAssetLibrary.list_assets(
        asset_dir, recursive=True, include_folder=False
    )
    for a in asset_content:
        unreal.EditorAssetLibrary.save_asset(a)

    return asset_content