Datasmith Importer for .udatasmith and export for .stl using python script

Hello Everyone,

I have a python script which imports the .udatasmith into a new level in unreal and exports that new level to .stl. The script is as follows:

import unreal

file_to_import = "C:\\Users\\IRESC_ASUS_2\\Downloads\\PLO_20221206.udatasmith"
final_stl_file = "C:\\Users\\IRESC_ASUS_2\\Downloads\\my_filename.stl"

ds_scene_in_memory = unreal.DatasmithSceneElement.construct_datasmith_scene_from_file(file_to_import)

if ds_scene_in_memory is None:
    print ("Scene loading failed.")
    quit()

# Modify the data in the scene to filter out or combine elements...

# Remove any mesh whose name includes a certain keyword.
remove_keyword = "exterior"      # we'll remove any actors with this string in their names.
meshes_to_skip = set([])         # we'll use this set to temporarily store the meshes we don't need.

# Remove from the scene any mesh actors whose names match the string set above.
for mesh_actor in ds_scene_in_memory.get_all_mesh_actors():
    actor_label = mesh_actor.get_label()
    if remove_keyword in actor_label:
        print("removing actor named: " + actor_label)
        # add this actor's mesh asset to the list of meshes to skip
        mesh = mesh_actor.get_mesh_element()
        meshes_to_skip.add(mesh)
        ds_scene_in_memory.remove_mesh_actor(mesh_actor)

# Remove all the meshes we don't need to import.
for mesh in meshes_to_skip:
    mesh_name = mesh.get_element_name()
    print("removing mesh named " + mesh_name)
    ds_scene_in_memory.remove_mesh(mesh)

# Set import options.
import_options = ds_scene_in_memory.get_options(unreal.DatasmithImportOptions)
import_options.base_options.scene_handling = unreal.DatasmithImportScene.NEW_LEVEL

# Finalize the process by creating assets and actors.

# Your destination folder must start with /Game/
result = ds_scene_in_memory.import_scene("/Game/MyStudioScene")

if not result.import_succeed:
    print ("Importing failed.")
    quit()

# Clean up the Datasmith Scene.
ds_scene_in_memory.destroy_scene()
print ("Custom import process complete!")

# merge the actors into one object
all_actors = unreal.EditorLevelLibrary.get_all_level_actors()
merge_options = unreal.EditorScriptingMergeStaticMeshActorsOptions()
# look for the unreal.MeshMergingSettings class to see what options you can set in here
merge_options.base_package_name = "/Game/NEW_MESH"
new_mesh_actor = unreal.EditorLevelLibrary.merge_static_mesh_actors(all_actors, merge_options)

# load the merged asset
loaded_asset = unreal.EditorAssetLibrary.load_asset("/Game/NEW_MESH")

# set up the STL export options
task = unreal.AssetExportTask()
task.object = loaded_asset # the asset to export
task.filename = final_stl_file # the filename to export as
task.automated = True # don't display the export options dialog
task.replace_identical = True # always overwrite the output
task.options = unreal.LevelExporterSTL()

# export!
result = unreal.Exporter.run_asset_export_task(task)

print("Export complete!")
for error_msg in task.errors:
 unreal.log_error("{}".format(error_msg))

When I running this script I am getting error that no stl exporter is found. I have no errors when I export the files as .fbx or .obj.

We can manually export the file as .stl when we use File->Export All->Save type as .stl as shown in below figure

My doubt is if we can export as .stl mannually why can’t we perform using a script or Am I doing something wrong here. Ther version of unreal which I am using is 5.1 built from source.

Thanks for the help!