#!/usr/bin/env python3
"""Convert the CC0 test-body GLB into an FBX with Blender.

The committed fixture tests/fixtures/bodies/test-body-male.fbx is the output
of this script. Re-run it when the generator changes:

    blender --background --python scripts/convert_test_body_to_fbx.py

Blender is only a fixture tool. The application does not call Blender.
"""

import sys
from pathlib import Path

import bpy

ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "tests" / "fixtures" / "bodies" / "test-body-male.glb"
TARGET = ROOT / "tests" / "fixtures" / "bodies" / "test-body-male.fbx"


def main() -> None:
    bpy.ops.wm.read_factory_settings(use_empty=True)
    bpy.ops.import_scene.gltf(filepath=str(SOURCE))
    for obj in list(bpy.data.objects):
        if not obj.name.startswith("test-body"):
            bpy.data.objects.remove(obj, do_unlink=True)
    actions = [action.name for action in bpy.data.actions]
    armatures = [obj.name for obj in bpy.data.objects if obj.type == "ARMATURE"]
    meshes = [obj.name for obj in bpy.data.objects if obj.type == "MESH"]
    print("actions", actions)
    print("armatures", armatures)
    print("meshes", meshes)
    bpy.ops.export_scene.fbx(
        filepath=str(TARGET),
        use_selection=False,
        object_types={"ARMATURE", "MESH"},
        add_leaf_bones=False,
        bake_anim=True,
        bake_anim_use_all_actions=True,
        bake_anim_use_nla_strips=True,
        bake_anim_force_startend_keying=True,
        path_mode="AUTO",
        embed_textures=False,
        axis_forward="-Z",
        axis_up="Y",
        apply_scale_options="FBX_SCALE_UNITS",
    )
    print(f"wrote {TARGET} ({TARGET.stat().st_size} bytes)")


if __name__ == "__main__":
    main()
