#!/usr/bin/env python3 """glb_merge_anims.py — stdlib-only merger of single-clip GLBs (Meshy outputs) into one multi-clip GLB. No Blender required. Designed for Meshy: rigged.glb (mesh+skeleton from /rigging) + N animation GLBs from the same rig task (identical node names). Animations are remapped into the base file by NODE NAME, so it also works across re-exports as long as bone names match. Includes the two mandatory Meshy post-fixes from the skill: * root-bone baked-scale bug: if a clip's root scale channel != 1.0 (observed 1.176 on idle), scale values are rewritten to 1.0 and the same clip's root translation is divided by the factor; * materials forced to alphaMode OPAQUE + doubleSided (the "inverted normals" lookalike). Usage: python3 glb_merge_anims.py base.glb walk.glb:Walk run.glb:Run \ idle.glb:Idle attack.glb:Attack out.glb Verify the result with glb_inspect.py (clip count, skins>=1, root scale 1.0). """ import json import struct import sys MAGIC = 0x46546C67 CHUNK_JSON = 0x4E4F534A CHUNK_BIN = 0x004E4942 COMP_SIZE = {5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4} TYPE_COUNT = {"SCALAR": 1, "VEC2": 2, "VEC3": 3, "VEC4": 4, "MAT4": 16} SCALE_TOL = 0.02 def read_glb(path): with open(path, "rb") as f: data = f.read() magic, version, _ = struct.unpack_from(" 1.0") base_bin[start: start + n * 4] = f32_bytes([1.0] * n) acc.pop("min", None); acc.pop("max", None) for ch2 in anim["channels"]: if (ch2["target"]["path"] == "translation" and ch2["target"]["node"] == ch["target"]["node"]): smp2 = anim["samplers"][ch2["sampler"]] acc2 = base["accessors"][smp2["output"]] bv2 = base["bufferViews"][acc2["bufferView"]] s2 = bv2.get("byteOffset", 0) + acc2.get("byteOffset", 0) n2 = acc2["count"] * 3 tv = f32_list(bytes(base_bin[s2: s2 + n2 * 4])) base_bin[s2: s2 + n2 * 4] = f32_bytes([v / mean for v in tv]) acc2.pop("min", None); acc2.pop("max", None) print(f" translation of same root divided by {mean:.4f}") def main(): if len(sys.argv) < 4: raise SystemExit(__doc__) base_path, out_path = sys.argv[1], sys.argv[-1] base, base_bin = read_glb(base_path) print(f"base: {base_path} ({len(base.get('animations', []))} clips, " f"{len(base.get('skins', []))} skins)") for spec in sys.argv[2:-1]: if ":" in spec: path, name = spec.rsplit(":", 1) else: path, name = spec, spec.rsplit("/", 1)[-1].rsplit(".", 1)[0] donor, donor_bin = read_glb(path) print(f"donor: {path} -> '{name}'") merge_clip(base, base_bin, donor, donor_bin, name) fix_root_scale(base, base_bin) for m in base.get("materials", []): m["alphaMode"] = "OPAQUE" m.pop("alphaCutoff", None) m["doubleSided"] = True write_glb(out_path, base, base_bin) print(f"done: {out_path} ({len(base.get('animations', []))} clips)") if __name__ == "__main__": main()