rinkplanner/tools/promote_rotoscope.py

160 lines
5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Übernimmt einen Rotoscoping-Export in das Asset-Manifest.
Liest die vom Blender-Export geschriebene JSON-Metadatendatei, aktualisiert
den Skater-Eintrag und synchronisiert das GLB in das Godot-Projekt. Mit
--publish wird es zusätzlich auf den FTP-Spiegel geladen.
"""
import argparse
import json
import os
import subprocess
import sys
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MANIFEST = os.path.join(REPO_ROOT, "rinkplanner", "assets", "asset_manifest.json")
def documents_dir() -> str:
"""Dokumente-Ordner des Benutzers; der Name hängt von der Sprache ab."""
home = os.path.expanduser("~")
try:
result = subprocess.run(
["xdg-user-dir", "DOCUMENTS"],
capture_output=True,
text=True,
check=False,
)
except OSError:
result = None
if result is not None and result.returncode == 0:
candidate = result.stdout.strip()
# xdg-user-dir liefert $HOME, wenn kein Ordner konfiguriert ist.
if candidate and candidate != home and os.path.isdir(candidate):
return candidate
for name in ("Dokumente", "Documents"):
candidate = os.path.join(home, name)
if os.path.isdir(candidate):
return candidate
return os.path.join(home, "Documents")
WORKBENCH = os.environ.get("RINKPLANNER_WORKBENCH") or os.path.join(
documents_dir(), "workbench_rinkplanner"
)
ASSET_ID = "character.skater.figure"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Rotoscoping-Clips ins Asset-Manifest übernehmen."
)
parser.add_argument("--glb", required=True)
parser.add_argument("--version", type=int, default=0)
parser.add_argument("--manifest", default=MANIFEST)
parser.add_argument("--workbench", default=WORKBENCH)
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--publish", action="store_true")
parser.add_argument("--no-sync", action="store_true")
return parser.parse_args()
def load_json(path: str) -> dict:
with open(path, encoding="utf-8") as handle:
return json.load(handle)
def main() -> int:
args = parse_args()
glb_path = os.path.abspath(args.glb)
sidecar_path = os.path.splitext(glb_path)[0] + ".json"
if not os.path.isfile(glb_path):
print("GLB fehlt: " + glb_path, file=sys.stderr)
return 1
if not os.path.isfile(sidecar_path):
print("Metadatendatei fehlt: " + sidecar_path, file=sys.stderr)
return 1
workbench = os.path.abspath(args.workbench)
library = os.path.join(workbench, "library", "characters")
if not glb_path.startswith(library + os.sep):
print(
"Das GLB muss unter %s liegen, nicht unter %s."
% (library, glb_path),
file=sys.stderr,
)
return 1
sidecar = load_json(sidecar_path)
manifest = load_json(args.manifest)
assets = manifest.get("assets", [])
entry = next(
(asset for asset in assets if asset.get("id") == ASSET_ID),
None,
)
if entry is None:
print("Asset %s fehlt im Manifest." % ASSET_ID, file=sys.stderr)
return 1
version = args.version or int(entry.get("version", 0)) + 1
if version < 1:
print("Ungültige Version.", file=sys.stderr)
return 1
filename = os.path.basename(glb_path)
clips = sidecar.get("clip_names", [])
if not clips:
print("Der Export enthält keine Clips.", file=sys.stderr)
return 1
entry["version"] = version
entry["source"] = os.path.relpath(glb_path, workbench).replace(os.sep, "/")
entry["target"] = "assets/external/characters/" + filename
entry["remote"] = "characters/" + filename
entry["sha256"] = sidecar["sha256"]
entry["bytes"] = int(sidecar["bytes"])
if sidecar.get("source_scene"):
entry["source_scene"] = sidecar["source_scene"]
entry["animations"] = clips
entry["notes"] = "Rotoscoping-Export mit %d Clip(s)." % len(clips)
print(
json.dumps(
{
"id": ASSET_ID,
"version": version,
"file": filename,
"clips": clips,
"sha256": entry["sha256"],
"bytes": entry["bytes"],
"target": entry["target"],
},
ensure_ascii=False,
indent=2,
)
)
if args.dry_run:
print("DRY-RUN: Manifest und Sync nicht verändert.")
return 0
with open(args.manifest, "w", encoding="utf-8") as handle:
json.dump(manifest, handle, ensure_ascii=False, indent=2)
handle.write("\n")
if args.no_sync:
print("SYNC: übersprungen (--no-sync).")
return 0
sync_script = os.path.join(REPO_ROOT, "tools", "sync_assets.sh")
subprocess.run([sync_script, "--sync"], check=True)
if args.publish:
subprocess.run([sync_script, "--publish"], check=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())