I am trying to write a python script that will import assets into unreal, spawn the static meshes into an empty map, combine the static mesh actors into a single actor and then save the combined actor as a new asset. I have attached the script below.
When I run the functions in the script from the unreal Editor, i can see the assets merging and the script working. But when I run UnrealEditor-Cmd.exe -run=pythonscript -script=merging-script.py
, the mesh merging does not seem to be happening. I have attached the logs below. I am using UE 5.2.1. I don’t know if i am missing something or is it only an in-editor thing. Please Help.
Merging-script.py
def buildImportTask(filename, destination):
task = unreal.AssetImportTask()
task.automated = True
task.destination_path = destination
task.filename = filename
task.save = True
return task
def mergeStaticMesh(destinationPath):
actors = unreal.EditorLevelLibrary.get_all_level_actors()
actors_to_merge = [a for a in actors if a.__class__ == unreal.StaticMeshActor]
print("before",actors_to_merge)
meshMergeOptions = unreal.MergeStaticMeshActorsOptions()
meshMergeOptions.base_package_name = destinationPath
meshMergeOptions.spawn_merged_actor = True
meshMergeOptions.destroy_source_actors = True
meshMergeOptions.new_actor_label = "SM_MERGED_StaticMeshActor"
meshMergeOptions.rename_components_from_source = True
meshMergeOptions.mesh_merging_settings.create_merged_material = False
merged_actor = unreal.EditorLevelLibrary.merge_static_mesh_actors(actors_to_merge, meshMergeOptions)
print(merged_actor)
actors = unreal.EditorLevelLibrary.get_all_level_actors()
actors_to_merge = [a for a in actors if a.__class__ == unreal.StaticMeshActor]
print("after",actors_to_merge)
return merged_actor
task = buildImportTask(filename,destinationPath)
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
importedAssets = task.get_objects()
for asset in importedAssets:
if (asset.get_default_object().get_name() == "Default__StaticMesh"):
actor_location = unreal.Vector(0.0,0.0,0.0)
actor_rotation = unreal.Rotator(0.0,0.0,0.0)
unreal.EditorLevelLibrary.spawn_actor_from_object(mesh, actor_location, actor_rotation)
merged_actor = mergeStaticMesh(destinationPath+"_merged")
Logs
LogPython: before [<Object '/Temp/Untitled_0.Untitled:PersistentLevel.StaticMeshActor_0' (0x000008B6068E8C00) Class 'StaticMeshActor'>, <Object '/Temp/Untitled_0.Untitled:PersistentLevel.StaticMeshActor_1' (0x000008B6068E8700) Class 'StaticMeshActor'>, <Object '/Temp/Untitled_0.Untitled:PersistentLevel.StaticMeshActor_2' (0x000008B6068E7D00) Class 'StaticMeshActor'>]
LogPython: Warning: C:/Users//Documents/Unreal Projects/Scripts/script.py:158: DeprecationWarning: EditorLevelLibrary: Function 'merge_static_mesh_actors' on 'EditorLevelLibrary' is deprecated: The Editor Scripting Utilities Plugin is deprecated - Use the function in Static Mesh Editor Subsystem
merged_actor = unreal.EditorLevelLibrary.merge_static_mesh_actors(actors_to_merge, meshMergeOptions)
LogPython: None
LogPython: Warning: C:/Users//Documents/Unreal Projects/Scripts/script.py:161: DeprecationWarning: EditorLevelLibrary: Function 'get_all_level_actors' on 'EditorLevelLibrary' is deprecated: The Editor Scripting Utilities Plugin is deprecated - Use the function in Editor Actor Utilities Subsystem
actors = unreal.EditorLevelLibrary.get_all_level_actors()
LogPython: after [<Object '/Temp/Untitled_0.Untitled:PersistentLevel.StaticMeshActor_0' (0x000008B6068E8C00) Class 'StaticMeshActor'>, <Object '/Temp/Untitled_0.Untitled:PersistentLevel.StaticMeshActor_1' (0x000008B6068E8700) Class 'StaticMeshActor'>, <Object '/Temp/Untitled_0.Untitled:PersistentLevel.StaticMeshActor_2' (0x000008B6068E7D00) Class 'StaticMeshActor'>]