"""Runs scripts/bvh-to-fbx.py in the real Blender the daemon runs (VILI_BLENDER), with the environment the daemon builds for it (scripts/vili-daemon.mjs exportEnv). The recipe's test-worker step runs this against the unpacked blender tree; there is no skip: without VILI_BLENDER it errors. VILI_BLENDER= python3.12 -m unittest scripts/test_bvh_to_fbx.py """ import importlib.util import json import os import pathlib import re import struct import subprocess import tempfile import unittest BLENDER = os.environ["VILI_BLENDER"] SCRIPT = pathlib.Path(__file__).resolve().with_name("bvh-to-fbx.py") # Reads an FBX back into a factory scene and reports what the avatar cut will see. With a BVH as its second # argument it also imports that BVH the way Blender itself does (native Euler order), as the reference pose. INSPECT = r''' import bpy, json, sys argv = sys.argv[sys.argv.index("--") + 1:] fbx = argv[0] bpy.ops.wm.read_factory_settings(use_empty=True) bpy.ops.import_scene.fbx(filepath=fbx) scene = bpy.context.scene arm = next(o for o in scene.objects if o.type == "ARMATURE") def world(name, frame): scene.frame_set(frame) return (arm.matrix_world @ arm.pose.bones[name].matrix).translation.copy() out = { "bones": [b.name for b in arm.data.bones], "scale": list(arm.scale), "hips_rest": arm.data.bones["Hips"].head_local.z * arm.scale.z, } action = arm.animation_data.action if arm.animation_data else None if action is not None: out["action"] = action.name first, last = (int(f) for f in action.frame_range) out["frame_range"] = [first, last] # The importer starts the take a frame late: sample i is at frame first + 1 + i, and the range ends before the # last key. Read two frames past it. frames = range(first, last + 3) out["hips_travel"] = max((world("Hips", f) - world("Hips", first)).length for f in frames) out["hips_mid"] = (world("Hips", first + 1 + 15) - world("Hips", first)).length out["hips_curve"] = [list(world("Hips", first + 1 + i)) for i in range(31)] out["root_wander"] = max((world("Root", f) - world("Root", first)).length for f in frames) out["head_from_hips"] = list(world("Head", first + 1 + 15) - world("Hips", first + 1 + 15)) if len(argv) > 1: bpy.ops.wm.read_factory_settings(use_empty=True) bpy.ops.import_anim.bvh(filepath=argv[1], global_scale=0.01, axis_forward="-Z", axis_up="Y", rotate_mode="NATIVE", use_fps_scale=False, update_scene_fps=True, frame_start=1) scene = bpy.context.scene arm = bpy.context.view_layer.objects.active out["reference_head_from_hips"] = list(world("Head", 1 + 15) - world("Hips", 1 + 15)) out["reference_hips_curve"] = [list(world("Hips", 1 + i)) for i in range(31)] print("INSPECT " + json.dumps(out)) ''' def export_env(): env = { "HOME": "/nonexistent", "PYTHONHASHSEED": "0", "PYTHONNOUSERSITE": "1", "LD_LIBRARY_PATH": os.path.join(os.path.dirname(BLENDER), "lib"), } for name in ("PATH", "LANG"): if name in os.environ: env[name] = os.environ[name] return env def blender(*argv, env=None): return subprocess.run( [BLENDER, "-b", "--factory-startup", "--python-use-system-env", "--python-exit-code", "1", *argv], env=export_env() if env is None else env, capture_output=True, text=True, timeout=600, ) def convert(bvh, fbx, clip, env=None): return blender("--python", str(SCRIPT), "--", str(bvh), str(fbx), clip, env=env) def result(run): line = next(line for line in run.stdout.splitlines() if line.startswith("VILI-FBX ")) return json.loads(line[len("VILI-FBX "):]) def unit_scale_factor(fbx): """GlobalSettings' UnitScaleFactor from a binary FBX: the property is four strings, then a double.""" raw = pathlib.Path(fbx).read_bytes() position = raw.index(b"UnitScaleFactor") + len(b"UnitScaleFactor") for _ in range(3): # type "double", label "Number", flags "" assert raw[position:position + 1] == b"S" position += 5 + struct.unpack("