#!/usr/bin/env python3 """Import the façade module kit into Unreal: /Game/Hollywood/Kit/*. Pairs with `Tools/blender/facade_kit_build.py`. Each piece is imported as its own static mesh at the module's origin (bottom-front centre), so a building can place a piece by bay index and storey index with a single offset. UnrealEditor-Cmd -run=pythonscript \ -script=Tools/unreal/ue_import_facade_kit.py -nullrhi -unattended -nopause -stdout Result file: /tmp/gigline/kit-import.txt (the commandlet swallows print()). """ from __future__ import annotations import os import sys import unreal _HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(_HERE)) # tools root, for _common sys.path.insert(0, _HERE) # siblings in this folder (ue_common, ue_placement) import ue_common # noqa: E402 SRC = "/tmp/gigline/kit" DEST = "/Game/Hollywood/Kit" RESULT = "/tmp/gigline/kit-import.txt" lines: list[str] = [] def log(message: str) -> None: lines.append(message) print(message) def main() -> None: if not os.path.isdir(SRC): log(f"MISSING source directory {SRC} — run Tools/blender/facade_kit_build.py first") return files = sorted(f for f in os.listdir(SRC) if f.lower().endswith(".fbx")) if not files: log(f"no FBX files in {SRC}") return # Drop kit assets that the current build no longer produces (the pieces were # renamed to follow the SM_ convention), so the folder cannot accumulate two # generations of the same piece. expected = {os.path.splitext(f)[0] for f in files} for existing in unreal.EditorAssetLibrary.list_assets(DEST, recursive=False): name = existing.split("/")[-1].split(".")[0] if name not in expected: unreal.EditorAssetLibrary.delete_asset(f"{DEST}/{name}") log(f"removed stale kit asset {name}") for filename in files: asset_name = os.path.splitext(filename)[0] task = unreal.AssetImportTask() task.filename = os.path.join(SRC, filename) task.destination_path = DEST task.destination_name = asset_name task.automated = True task.save = True task.replace_existing = True ui = unreal.FbxImportUI() ui.import_mesh = True ui.import_materials = True ui.import_textures = False ui.import_as_skeletal = False smd = ui.static_mesh_import_data smd.import_uniform_scale = 1.0 smd.combine_meshes = True smd.convert_scene_unit = False smd.force_front_x_axis = False smd.normal_import_method = unreal.FBXNormalImportMethod.FBXNIM_IMPORT_NORMALS # Kit pieces are thin boxes and panes: complex-as-simple keeps the # collision on the real faces instead of a fat convex hull that would # fence off the shopfront glazing. smd.auto_generate_collision = False task.options = ui unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task]) mesh = unreal.EditorAssetLibrary.load_asset(f"{DEST}/{asset_name}") if mesh is None: log(f"{asset_name}: IMPORT FAILED") continue # The FBX import also creates an M_Kit_* material per piece. The demo # assigns real site materials at assembly time, so those are clutter. for slot in mesh.get_editor_property("static_materials"): mat = slot.get_editor_property("material_interface") if mat and mat.get_name().startswith("M_Kit_"): unreal.EditorAssetLibrary.delete_asset(mat.get_path_name().split(".")[0]) unreal.GigLineAssetTools.set_complex_collision_as_simple(mesh, True) unreal.EditorAssetLibrary.save_loaded_asset(mesh) box = mesh.get_bounding_box() mn, mx = box.min, box.max log(f"{asset_name:20} size=({mx.x - mn.x:6.1f}, {mx.y - mn.y:6.1f}, " f"{mx.z - mn.z:6.1f}) cm min=({mn.x:6.1f}, {mn.y:6.1f}, {mn.z:6.1f}) " f"collision={unreal.GigLineAssetTools.get_collision_complexity(mesh)}") log(f"imported {len(files)} kit piece(s) into {DEST}") try: main() except Exception as exc: # noqa: BLE001 - surface the failure in the result file import traceback log(f"FAILED: {type(exc).__name__}: {exc}") log(traceback.format_exc()) os.makedirs(os.path.dirname(RESULT), exist_ok=True) with open(RESULT, "w", encoding="utf-8") as handle: handle.write("\n".join(lines) + "\n")