Encountering Issues with Datasmith Import of 3DXML/STP Files via Python Script

I’ve been working on a Python script to automate the importation of 3DXML and STP files into Unreal Engine using Datasmith. The core of the import process is handled by the datasmith_importer function, which successfully creates the scene and imports geometry and materials.

def datasmith_importer(file_name, file_path, destination_folder):
    # Level Directory
    level_directory = f'{destination_folder}/{file_name}/Level/{file_name}'

    # Create new level
    unreal.get_editor_subsystem(unreal.LevelEditorSubsystem).new_level(level_directory)

    # Create DataSmith scene
    datasmith_scene = unreal.DatasmithSceneElement.construct_datasmith_scene_from_file(file_path)

    # Configure datasmith base options
    base_options = datasmith_scene.get_options().base_options
    base_options.scene_handling = unreal.DatasmithImportScene.CURRENT_LEVEL
    base_options.static_mesh_options.generate_lightmap_u_vs = False
    base_options.include_material = True

    # Configure datasmith tessellation options (None for 3DXML files)
    tessellation_options = datasmith_scene.get_options(unreal.DatasmithCommonTessellationOptions)
    if tessellation_options:
        tessellation_options.options.chord_tolerance = chord_tolerance
        tessellation_options.options.max_edge_length = max_edge_length
        tessellation_options.options.normal_tolerance = normal_tolerance
        tessellation_options.options.stitching_technique = unreal.DatasmithCADStitchingTechnique.STITCHING_NONE

    # Import the scene into the current level
    result = datasmith_scene.import_scene(destination_folder)

    # Save imported static meshes and materials assets
    for static_mesh in result.imported_meshes:
        unreal.EditorAssetLibrary.save_loaded_asset(static_mesh)
        for i in range(static_mesh.get_num_sections(0)):
            material_interface = static_mesh.get_material(i)
            unreal.EditorAssetLibrary.save_loaded_asset(material_interface)

    # Save level
    unreal.get_editor_subsystem(unreal.LevelEditorSubsystem).save_all_dirty_levels()

The script works flawlessly when executed via the “Tools/Execute Python Script” option in Unreal Engine. However, when I call the same script from PowerShell using the following command:

Start-Process $engine -ArgumentList "`"$project`" -run=pythonscript -script=`"$script`""

The importation process completes, but the imported materials are missing their references.
.Any insights or suggestions would be greatly appreciated. Thank you!

I wasn’t actually saving the materials parent…

This fixed it:

    # Save imported static meshes and materials assets
    for static_mesh in result.imported_meshes:
        unreal.EditorAssetLibrary.save_loaded_asset(static_mesh)

        for i in range(static_mesh.get_num_sections(0)):
            material_interface = static_mesh.get_material(i)
            unreal.EditorAssetLibrary.save_loaded_asset(material_interface)

            material_parent = material_interface.parent
            unreal.EditorAssetLibrary.save_loaded_asset(material_parent)