#!/usr/bin/env python3 """Render animations.json to contact sheets: one row per animation, one column per frame. python3 tools/preview_animations.py OUTDIR [--dark] [--only name,name] [--json PATH] [--streak K|calm] [--number N] [--every] writes OUTDIR/sheet00.png, ... (10 animations each), calm00.png, ... (every other frame), star.png, and streakK.png per streak level + streak_calm.png (every other frame; --every: all 24, in two rows), each with its " days!" caption under it (from the frame its number first shows), fitted into the cell as the app fits it on screen. --json: read this animations.json (default shared/animations.json), e.g. one made with tools/gen_animations.py --level K --out DIR. --streak K|calm: only that streak level's sheet. --number N: the streak number drawn (default the level's own; calm: 21). Try --number 465 for the widest it gets. """ import json, os, sys os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from routine.qt.QtGui import QColor, QGuiApplication, QImage, QPainter, QPen from routine.gui.anim_paint import bounds, caption_h, caption_place, caption_start, fit, paint_caption, paint_frame def arg(name, default=None): return sys.argv[sys.argv.index(name) + 1] if name in sys.argv else default app = QGuiApplication(sys.argv) root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) data = json.load(open(arg("--json", os.path.join(root, "shared", "animations.json")))) out = sys.argv[1] os.makedirs(out, exist_ok=True) only = arg("--only").split(",") if "--only" in sys.argv else None streak_only = arg("--streak") bg = QColor(48, 48, 48) if "--dark" in sys.argv else QColor(239, 239, 239) U, CW, CH = 14, 150, 150 def sheet(anims, path, cols=12, number=None, cw=None, ch=None): CW, CH = cw or globals()["CW"], ch or globals()["CH"] img = QImage(CW * cols + 110, CH * len(anims), QImage.Format.Format_RGB32) img.fill(bg) p = QPainter(img) for r, a in enumerate(anims): p.setPen(QColor("#888")) p.drawText(6, r * CH + 20, a["name"]) cap = None if number is not None: # the caption, and the whole of it fitted in the cell ch = a.get("_caption_h") or caption_h(a) cap = caption_place(bounds(a, number), number, False, ch) ub = cap[2] u = min(U, (CW - 8) / (ub[2] - ub[0]), (CH - 8) / (ub[3] - ub[1])) first = a.get("_first", 0) for c, fr in enumerate(a["frames"][:cols]): ox, oy = 110 + c * CW, r * CH p.setClipping(False) p.setPen(QColor("#bbb")); p.drawRect(ox, oy, CW - 1, CH - 1) p.setClipRect(ox, oy, CW, CH) bx, by = ox + CW // 3, oy + CH - 40 if cap is None: p.setPen(QPen(QColor("#666"), 1)); p.drawRect(int(bx - U / 2), int(by - U / 2), U, U) paint_frame(p, fr, data["colors"][a["color"]], bx, by, U, data["colors"], number) continue bx = ox + fit(bx - ox, ub[0] * u, ub[2] * u, CW, 4) by = oy + fit(by - oy, ub[1] * u, ub[3] * u, CH, 4) p.setPen(QPen(QColor("#666"), 1)); p.drawRect(int(bx - u / 2), int(by - u / 2), int(u), int(u)) paint_frame(p, fr, data["colors"][a["color"]], bx, by, u, data["colors"], number) paint_caption(p, number, data["colors"][a["color"]], bx, by, u, cap[:2], first + c * a.get("_step", 1), ch, a.get("_caption_start", 0)) p.end() img.save(path) print("saved", path) def streak_sheet(anims, path, number): if "--every" in sys.argv: rows = [] for a in anims: c = {"_caption_h": caption_h(a), "_caption_start": caption_start(a)} rows += [dict(a, frames=a["frames"][:12], **c), dict(a, name=a["name"] + " 13-24", frames=a["frames"][12:], _first=12, **c)] else: rows = [dict(a, frames=a["frames"][::2], _step=2, _caption_h=caption_h(a), _caption_start=caption_start(a)) for a in anims] sheet(rows, path, number=number, cw=170, ch=200) # streak ones are bigger if streak_only is None: anims = [a for a in data["animations"] if not only or a["name"] in only] for k in range(0, len(anims), 10): sheet(anims[k:k + 10], os.path.join(out, "sheet%02d.png" % (k // 10))) calm = [a for a in data.get("calm", []) if not only or a["name"] in only] for k in range(0, len(calm), 10): sheet([dict(a, frames=a["frames"][::2]) for a in calm[k:k + 10]], os.path.join(out, "calm%02d.png" % (k // 10))) if "star" in data and not only: st = data["star"] sheet([dict(st, frames=st["frames"][:12]), dict(st, name="star 13-24", frames=st["frames"][12:])], os.path.join(out, "star.png")) streak = data.get("streak") or {} for k, anims in enumerate(streak.get("sets", [])): anims = [a for a in anims if not only or a["name"] in only] if anims and streak_only in (None, str(k)): streak_sheet(anims, os.path.join(out, "streak%d.png" % k), int(arg("--number", streak["levels"][k]))) calm = [a for a in streak.get("calm", []) if not only or a["name"] in only] if calm and streak_only in (None, "calm"): streak_sheet(calm, os.path.join(out, "streak_calm.png"), int(arg("--number", 21)))