107 lines
4.2 KiB
Python
107 lines
4.2 KiB
Python
import json
|
||
import os
|
||
|
||
import unreal
|
||
|
||
|
||
SOURCE_MAP = "/Game/MrPlow/Maps/VehicleImports/Lvl_ModularContract_Kamaz_Auto"
|
||
DEST_MAP = "/Game/MrPlow/Maps/VehicleImports/Lvl_KAMAZ_CR_VisualVerify"
|
||
FBX_PATH = r"C:\Users\post\Documents\mrPlow\mrPlow58\VehicleExports\KAMAZ_CR\FBX\SM_KAMAZ_CR_BaseVisual.fbx"
|
||
DEST_ASSET_PATH = "/Game/MrPlow/Vehicles/KAMAZ_CR"
|
||
DEST_ASSET_NAME = "SM_KAMAZ_CR_BaseVisual"
|
||
REPORT_PATH = r"C:\Users\post\Documents\mrPlow\mrPlow58\VehicleExports\KAMAZ_CR\unreal_kamaz_cr_visual_level_report.json"
|
||
|
||
|
||
def import_static_mesh():
|
||
task = unreal.AssetImportTask()
|
||
task.filename = FBX_PATH
|
||
task.destination_path = DEST_ASSET_PATH
|
||
task.destination_name = DEST_ASSET_NAME
|
||
task.automated = True
|
||
task.save = True
|
||
task.replace_existing = True
|
||
|
||
options = unreal.FbxImportUI()
|
||
options.import_mesh = True
|
||
options.import_as_skeletal = False
|
||
options.import_materials = True
|
||
options.import_textures = True
|
||
options.create_physics_asset = False
|
||
|
||
mesh_data = options.static_mesh_import_data
|
||
mesh_data.combine_meshes = True
|
||
mesh_data.generate_lightmap_u_vs = True
|
||
mesh_data.auto_generate_collision = True
|
||
mesh_data.convert_scene = True
|
||
mesh_data.convert_scene_unit = True
|
||
|
||
task.options = options
|
||
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
|
||
return "{}/{}.{}".format(DEST_ASSET_PATH, DEST_ASSET_NAME, DEST_ASSET_NAME)
|
||
|
||
|
||
def duplicate_and_place(mesh_path):
|
||
if unreal.EditorAssetLibrary.does_asset_exist(DEST_MAP):
|
||
unreal.EditorAssetLibrary.delete_asset(DEST_MAP)
|
||
if not unreal.EditorAssetLibrary.does_asset_exist(SOURCE_MAP):
|
||
raise RuntimeError("Source map missing: {}".format(SOURCE_MAP))
|
||
if not unreal.EditorAssetLibrary.duplicate_asset(SOURCE_MAP, DEST_MAP):
|
||
raise RuntimeError("Could not duplicate map to {}".format(DEST_MAP))
|
||
|
||
unreal.EditorLevelLibrary.load_level(DEST_MAP)
|
||
|
||
# Remove old placed vehicle/pawn actors from the duplicated test map.
|
||
removed = []
|
||
for actor in list(unreal.EditorLevelLibrary.get_all_level_actors()):
|
||
actor_name = actor.get_name()
|
||
class_name = actor.get_class().get_name()
|
||
if "Kamaz" in actor_name or "KAMAZ" in actor_name or "Pawn" in actor_name or "Vehicle" in class_name:
|
||
if "Landscape" not in class_name and "PlayerStart" not in class_name:
|
||
removed.append({"name": actor_name, "class": class_name})
|
||
unreal.EditorLevelLibrary.destroy_actor(actor)
|
||
|
||
mesh = unreal.EditorAssetLibrary.load_asset(mesh_path)
|
||
if not mesh:
|
||
raise RuntimeError("Imported mesh missing: {}".format(mesh_path))
|
||
|
||
actor = unreal.EditorLevelLibrary.spawn_actor_from_object(mesh, unreal.Vector(0.0, 0.0, 34.0), unreal.Rotator(0.0, 0.0, 0.0))
|
||
actor.set_actor_label("VERIFY_KAMAZ-53605-3950-48A5_BaseVisual")
|
||
actor.set_actor_scale3d(unreal.Vector(1.0, 1.0, 1.0))
|
||
|
||
text = unreal.EditorLevelLibrary.spawn_actor_from_class(unreal.TextRenderActor, unreal.Vector(0.0, -360.0, 250.0), unreal.Rotator(0.0, 0.0, 0.0))
|
||
text.set_actor_label("VERIFY_Label_KAMAZ_CR")
|
||
text_comp = text.get_component_by_class(unreal.TextRenderComponent)
|
||
if text_comp:
|
||
text_comp.set_text("КАМАЗ-53605-3950-48(A5) base visual")
|
||
text_comp.set_horizontal_alignment(unreal.HorizontalTextAligment.EHTA_CENTER)
|
||
text_comp.set_world_size(45.0)
|
||
|
||
unreal.EditorLevelLibrary.save_current_level()
|
||
unreal.EditorAssetLibrary.save_loaded_asset(mesh)
|
||
unreal.EditorAssetLibrary.save_asset(DEST_MAP, only_if_is_dirty=False)
|
||
return removed
|
||
|
||
|
||
def main():
|
||
result = {
|
||
"ok": False,
|
||
"fbx": FBX_PATH,
|
||
"meshAsset": None,
|
||
"map": DEST_MAP,
|
||
"removedActors": [],
|
||
}
|
||
try:
|
||
mesh_path = import_static_mesh()
|
||
result["meshAsset"] = mesh_path
|
||
result["removedActors"] = duplicate_and_place(mesh_path)
|
||
result["ok"] = True
|
||
except Exception as exc:
|
||
result["error"] = str(exc)
|
||
|
||
os.makedirs(os.path.dirname(REPORT_PATH), exist_ok=True)
|
||
with open(REPORT_PATH, "w", encoding="utf-8") as report:
|
||
json.dump(result, report, ensure_ascii=False, indent=2)
|
||
unreal.log("KAMAZ_CR visual level report: {}".format(json.dumps(result, ensure_ascii=False)))
|
||
|
||
|
||
main()
|