#!/usr/bin/env python3 """Map identical animation frames to one PNG without changing frame timing.""" import argparse from hashlib import sha256 import json from pathlib import Path from practice_media import media_path ROOT = Path(__file__).resolve().parents[1] TABLE = ROOT / "src/practices/sun_salutation/sun_salutation_inventory.zi" def inventory(): manifest = json.loads((ROOT / "docs/sun-salutation-characters.json").read_text()) paths = [] for character in manifest["characters"]: for step, entry in enumerate(character["steps"], 1): for frame in range(1, entry["frames"] + 1): paths.append(media_path(character["directory"]) / f"{step:02d}-{frame:03d}.png") seen = {} sources = [] unique = [] for index, path in enumerate(paths): digest = sha256(path.read_bytes()).digest() if digest not in seen: seen[digest] = index unique.append(path) sources.append(seen[digest]) return paths, sources, unique def table_content(sources): lines = [ "// Generated by scripts/animation_inventory.py; do not edit.", "// Each original frame retains its timeline index and uses an identical PNG.", f"SUN_SALUTATION_STORED_FRAME_COUNT :: {len(set(sources))}", ] # Small declarations stay within the compiler's expression text capacity. for offset in range(0, len(sources), 512): chunk = sources[offset:offset + 512] values = ", ".join(str(value) for value in chunk) lines.append(f"sun_salutation_frame_sources_{offset // 512}: [{len(chunk)}]u16 = .[{values}];") lines.extend([ "", "SunSalutationStoredFrame :: (timeline_frame: s32) -> s32 {", f" if timeline_frame < 0 || timeline_frame >= {len(sources)} {{", " return 0", " }", ]) for offset in range(0, len(sources), 512): upper = min(offset + 512, len(sources)) lines.extend([ f" if timeline_frame < {upper} {{", f" return cast(s32)sun_salutation_frame_sources_{offset // 512}[timeline_frame - {offset}]", " }", ]) lines.extend([" return 0", "}", ""]) return "\n".join(lines) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--check", action="store_true") args = parser.parse_args() paths, sources, unique = inventory() content = table_content(sources) if args.check: if not TABLE.exists() or TABLE.read_text() != content: raise SystemExit("Animation inventory is stale; run python3 scripts/animation_inventory.py") else: TABLE.write_text(content) saved = sum(path.stat().st_size for index, path in enumerate(paths) if sources[index] != index) print(f"Animation: {len(paths)} timeline frames, {len(unique)} stored PNGs; " f"{saved:,} duplicate bytes excluded") if __name__ == "__main__": main()