ledcube3d/Scripts/CreateSambaDancingSublevel.py

108 lines
4.5 KiB
Python

import unreal
FBX_PATH = r"C:\Users\post\Downloads\Samba Dancing.fbx"
SOURCE_DIR = "/Game/LedVolume/Sources/SambaDancing"
MAP_PATH = "/Game/LedVolume/Maps/L_SambaDancing_Sublevel"
SOURCE_TAG = "LedVoxelSource"
def ensure_directory(path):
if not unreal.EditorAssetLibrary.does_directory_exist(path):
unreal.EditorAssetLibrary.make_directory(path)
def set_prop(obj, name, value):
obj.set_editor_property(name, value)
def import_fbx():
ensure_directory("/Game/LedVolume")
ensure_directory("/Game/LedVolume/Sources")
ensure_directory(SOURCE_DIR)
task = unreal.AssetImportTask()
task.set_editor_property("filename", FBX_PATH)
task.set_editor_property("destination_path", SOURCE_DIR)
task.set_editor_property("automated", True)
task.set_editor_property("replace_existing", True)
task.set_editor_property("save", True)
options = unreal.FbxImportUI()
options.set_editor_property("import_mesh", True)
options.set_editor_property("import_as_skeletal", True)
options.set_editor_property("import_animations", True)
options.set_editor_property("mesh_type_to_import", unreal.FBXImportType.FBXIT_SKELETAL_MESH)
options.set_editor_property("create_physics_asset", False)
task.set_editor_property("options", options)
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
imported = [unreal.EditorAssetLibrary.load_asset(path) for path in task.get_editor_property("imported_object_paths")]
imported = [asset for asset in imported if asset]
skeletal_mesh = next((asset for asset in imported if isinstance(asset, unreal.SkeletalMesh)), None)
animation = next((asset for asset in imported if isinstance(asset, unreal.AnimSequence)), None)
if not skeletal_mesh:
assets = unreal.EditorAssetLibrary.list_assets(SOURCE_DIR, recursive=True, include_folder=False)
loaded = [unreal.EditorAssetLibrary.load_asset(path) for path in assets]
skeletal_mesh = next((asset for asset in loaded if isinstance(asset, unreal.SkeletalMesh)), None)
animation = next((asset for asset in loaded if isinstance(asset, unreal.AnimSequence)), None)
if not skeletal_mesh:
raise RuntimeError("Samba FBX import did not produce a SkeletalMesh.")
return skeletal_mesh, animation
def clear_current_level():
for actor in unreal.EditorLevelLibrary.get_all_level_actors():
if not isinstance(actor, unreal.WorldSettings):
unreal.EditorLevelLibrary.destroy_actor(actor)
def create_level(skeletal_mesh, animation):
ensure_directory("/Game/LedVolume/Maps")
if unreal.EditorAssetLibrary.does_asset_exist(MAP_PATH):
unreal.EditorLevelLibrary.load_level(MAP_PATH)
else:
unreal.EditorLevelLibrary.new_level(MAP_PATH)
clear_current_level()
actor = unreal.EditorLevelLibrary.spawn_actor_from_class(unreal.SkeletalMeshActor, unreal.Vector(0, 0, 0), unreal.Rotator(0, 0, 0))
actor.set_actor_label("LED_Source_Samba_Dancing")
actor.tags = [unreal.Name(SOURCE_TAG)]
component = actor.get_editor_property("skeletal_mesh_component")
set_prop(component, "skeletal_mesh_asset", skeletal_mesh)
set_prop(component, "relative_location", unreal.Vector(0, 0, 0))
set_prop(component, "relative_rotation", unreal.Rotator(0, 0, 0))
set_prop(component, "relative_scale3d", unreal.Vector(1.0, 1.0, 1.0))
set_prop(component, "visibility_based_anim_tick_option", unreal.VisibilityBasedAnimTickOption.ALWAYS_TICK_POSE_AND_REFRESH_BONES)
if animation:
component.set_animation_mode(unreal.AnimationMode.ANIMATION_SINGLE_NODE)
component.set_animation(animation)
component.play(True)
unreal.log(f"Samba animation assigned: {animation.get_path_name()}")
else:
unreal.log_warning("No AnimSequence imported from Samba FBX; level will contain the skeletal mesh pose only.")
light = unreal.EditorLevelLibrary.spawn_actor_from_class(unreal.DirectionalLight, unreal.Vector(-250, -350, 500), unreal.Rotator(-45, 30, 0))
light.set_actor_label("Samba_Source_Preview_Light")
camera = unreal.EditorLevelLibrary.spawn_actor_from_class(unreal.CineCameraActor, unreal.Vector(260, -420, 220), unreal.Rotator(-18, 34, 0))
camera.set_actor_label("Samba_Source_Preview_Camera")
unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True)
unreal.log(f"Created Samba sublevel: {MAP_PATH}")
def main():
skeletal_mesh, animation = import_fbx()
create_level(skeletal_mesh, animation)
if __name__ == "__main__":
main()