LifePlay-Revival/Tools/HairImportFbx.py
2026-10-02 19:02:33 +02:00

54 lines
2.7 KiB
Python

# Protocole de conversion d'une coiffure (Docs/Migration/13_Protocole_Coiffure.md), etape 2 : import du FBX.
#
# Importe les maillages d'un FBX en maillages statiques separes (sans materiaux ni textures) dans un dossier de travail,
# puis affiche pour chacun : nom, boite englobante, triangles, emplacements de materiaux. Ces informations servent a
# choisir le maillage de la coiffure (-Static) et la tete de reference (-FitHead) de LPHairFromStaticMesh.
#
# Usage :
# UnrealEditor-Cmd.exe <projet> -run=pythonscript "-script=<projet>/Tools/HairImportFbx.py fbx=<fichier.fbx> dest=/Game/Heavy/New5/Hairs/_Work" -unattended -nopause -nosplash
# Sortie : lignes "HAIRIMPORT" du journal.
import sys
import unreal
args = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a)
fbx = args.get("fbx")
dest = args.get("dest", "/Game/Heavy/New5/Hairs/_Work")
if not fbx:
unreal.log_error("HAIRIMPORT usage : fbx=<fichier.fbx> [dest=/Game/...]")
else:
task = unreal.AssetImportTask()
task.filename = fbx.replace("\\", "/")
task.destination_path = dest
task.automated = True
task.replace_existing = True
task.save = True
ui = unreal.FbxImportUI()
ui.import_mesh = True
ui.import_as_skeletal = False
ui.import_materials = False
ui.import_textures = False
ui.mesh_type_to_import = unreal.FBXImportType.FBXIT_STATIC_MESH
ui.static_mesh_import_data.combine_meshes = False
ui.static_mesh_import_data.auto_generate_collision = False
task.options = ui
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
count = 0
meshes = []
for path in task.imported_object_paths:
asset = unreal.load_asset(path)
if isinstance(asset, unreal.StaticMesh):
box = asset.get_bounding_box()
size = box.max - box.min
slots = [str(m.material_slot_name) for m in asset.static_materials]
unreal.log_warning("HAIRIMPORT %s : %d triangles, taille X=%.1f Y=%.1f Z=%.1f cm, min Z=%.1f max Z=%.1f, emplacements %s"
% (path, asset.get_num_triangles(0), size.x, size.y, size.z, box.min.z, box.max.z, slots))
meshes.append({"path": path.split(".")[0], "name": asset.get_name(), "triangles": asset.get_num_triangles(0),
"min": [box.min.x, box.min.y, box.min.z], "max": [box.max.x, box.max.y, box.max.z], "slots": slots})
count += 1
unreal.log_warning("HAIRIMPORT %d maillage(s) statique(s) importe(s) dans %s" % (count, dest))
# Rapport lisible par Tools/HairPipeline.ps1 (option report=<fichier.json>).
if args.get("report"):
import json
with open(args["report"], "w", encoding="utf-8") as f:
json.dump({"fbx": fbx, "dest": dest, "meshes": meshes}, f, indent=1)