Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
feat: cut effect lifetimes through shared single-play pipeline
Soohong Kimd833ae955c28
diff --git a/sprite_gen/video/effect.py b/sprite_gen/video/effect.pyindex 080cd6d..ebcb787 100644--- a/sprite_gen/video/effect.py+++ b/sprite_gen/video/effect.py@@ -72,3 +72,24 @@ def empty_end(canvas_path: Path, out: Path) -> Path:blank = Image.new("RGB", source.size, (0, 0, 0))blank.save(out)return out+++def frame_masses(files: list[Path]) -> np.ndarray:+ """Full-resolution emitted coverage; thumbnails can erase a dim isolated spark."""+ values = []+ for path in files:+ with Image.open(path) as image:+ values.append(float(np.asarray(image.convert("RGBA").getchannel("A"), dtype=np.float64).mean() / 255))+ return np.array(values)+++def lifetime_ending(D: np.ndarray, masses: np.ndarray, fps: float) -> tuple[int, int, dict[str, Any]]:+ """The same single-play cutter's terminal condition: visible light followed by actual emptiness."""+ active = np.flatnonzero(masses > 0)+ if not len(active):+ raise ValueError("lifetime is entirely empty; no effect appeared")+ if masses[-1] > 0:+ raise ValueError("lifetime has no empty end frame; regenerate with an empty black end-frame pin")+ start, end = max(0, int(active[0]) - 1), int(active[-1]) + 1+ return start, end, {"kind": "empty", "frames": len(masses) - end,+ "start": end, "peak_frame": int(np.argmax(masses)), "alpha_max": 0}diff --git a/sprite_gen/video/loop.py b/sprite_gen/video/loop.pyindex aea2b94..69e6d30 100644--- a/sprite_gen/video/loop.py+++ b/sprite_gen/video/loop.py@@ -48,7 +48,7 @@ from sprite_gen._deps import npfrom sprite_gen.spec.runio import atomic_write_textfrom sprite_gen.util.gif_utils import save_clean_giffrom sprite_gen.util.resample import resize_cell-from sprite_gen.video import motion_anchor, auto_motion, local_cycle, gait_fallback, nonloop+from sprite_gen.video import motion_anchor, auto_motion, local_cycle, gait_fallback, nonloop, effectfrom sprite_gen.video import held as held_modfrom sprite_gen.video import legs as legs_modfrom sprite_gen.video import period as period_mod@@ -76,7 +76,7 @@ ONE_SHOT_MIN_MOVED = 0.4ONE_SHOT_MIN_COHERENCE = 3.0 # departure must exceed three ordinary playback stepsONE_SHOT_MIN_ACTIVE = 4 # isolated keying/jitter spikes are not a performed actionONE_SHOT_PAD = 2 # one-shot: rest frames kept on each side of the excursion so the seam is rest -> rest-CYCLE_MODES = ("auto", "periodic", "one-shot", "fixed", "pinned", "once")+CYCLE_MODES = ("auto", "periodic", "one-shot", "fixed", "pinned", *nonloop.KINDS)# --cycle pinned: the clip was asked to end on its first frame (first-last mode). A re-rendered# first frame is never byte-identical to its source; on the analysis thumbnail (mean absolute# RGBA difference, 0..1) it lands within this of it, well under one frame of visible motion.@@ -678,7 +678,8 @@ def build_strip(frames: list[Image.Image], *, max_cells: int = STRIP_MAX_CELLS,`max_cells`: the crop is measured over every cycle frame first, then as many evenlyspaced frames as fit are kept. Subsampling is recorded in the meta, never silent."""L = len(frames)- boxes = [im.getchannel("A").point(lambda v: 255 if v >= 8 else 0).getbbox() for im in frames]+ lifetime = kind == "lifetime"+ boxes = [(im.getchannel("A") if lifetime else im.getchannel("A").point(lambda v: 255 if v >= 8 else 0)).getbbox() for im in frames]boxes = [b for b in boxes if b]if not boxes:raise SystemExit("video-loop: every cycle frame is fully transparent")@@ -686,9 +687,9 @@ def build_strip(frames: list[Image.Image], *, max_cells: int = STRIP_MAX_CELLS,# edge (video-frames --allow-subject-edge-contact) still gets its 8 transparent columns,# so no cell corner is ever the subject. `crop` fills outside the frame with alpha 0.left = min(b[0] for b in boxes) - 8- top = max(0, min(b[1] for b in boxes) - 8)+ top = min(b[1] for b in boxes) - 8 if lifetime else max(0, min(b[1] for b in boxes) - 8)right = max(b[2] for b in boxes) + 8- bottom = max(b[3] for b in boxes)+ bottom = max(b[3] for b in boxes) + (8 if lifetime else 0)# body_h = the STANDING height: the tallest frame whose feet touch the floor. A median# over the whole cycle undercounts a jump (crouch + airborne frames dominate) and then# over-scales it — the 2026-09-09 hero read 22 % taller than the walk beside it.@@ -698,7 +699,8 @@ def build_strip(frames: list[Image.Image], *, max_cells: int = STRIP_MAX_CELLS,# first frame, or the cut's first — clip frame `standing_frame` — where the cut is filmed at# another size than it). The tallest grounded frame counts whatever is raised overhead — an attack's# windup lifts the weapon above the head — and would shrink that state against the others.- body_src = standing_src if standing_src is not None else max(grounded)+ body_src = (max(b[3] for b in boxes) - min(b[1] for b in boxes) if lifetime else+ standing_src if standing_src is not None else max(grounded))# max_height is a ceiling on the CELL; an explicit body_height is a target for the BODY.# Without one, nothing is ever scaled up. With one, a state whose source body is SHORTER# than the request has to grow — clamping to 1.0 first made the option a downward clamp@@ -718,8 +720,8 @@ def build_strip(frames: list[Image.Image], *, max_cells: int = STRIP_MAX_CELLS,cell_src_w = lead + trailelse:cell_src_w = right - left- w = round(cell_src_w * scale)- h = round((bottom - top) * scale)+ w = max(1, round(cell_src_w * scale))+ h = max(1, round((bottom - top) * scale))cap = max(1, min(max_cells, max_width // max(1, w)))idx = list(range(L))if L > cap:@@ -767,6 +769,10 @@ def build_strip(frames: list[Image.Image], *, max_cells: int = STRIP_MAX_CELLS,}if standing_src is not None and standing_frame:meta["body_ref_frame"] = standing_frame # the clip frame the standing height was read on+ if lifetime:+ meta.update(blend="add", body_ref="effect-extent",+ anchor=[(frames[0].width / 2 - left) * w / (right - left),+ (frames[0].height / 2 - top) * h / (bottom - top)])if feet:# the spec asset loader reads a sidecar `anchor` as the [x, y] pivot (default:# bottom-centre); with feet alignment the true pivot is the foot line at the@@ -1116,6 +1122,11 @@ def run_loop(steps: int | None = None,) -> dict[str, Any]:state = state.strip().lower() if state else state+ if state == effect.STATE:+ if cycle_mode == "auto":+ cycle_mode = "lifetime"+ elif cycle_mode != "lifetime":+ raise SystemExit("video-loop: an effect requires --cycle lifetime")if state in nonloop.STATES:if cycle_mode == "auto":cycle_mode = "once"@@ -1177,7 +1188,7 @@ def run_loop(# The clip's steps as keyed, before `--anchor motion-auto` reads its own distance or moves a frame:# the hold is read on them, over the clip and over the cut (sprite_gen/video/held.py).clip_steps = [float(D[k, k + 1]) for k in range(n - 1)]- masses = frame_masses(files)+ masses = effect.frame_masses(files) if cycle_mode == "lifetime" else frame_masses(files)periodic_attempt: dict[str, Any] | None = Nonetarget = (report_path or (out_dir / f"{name}.loop.report.json")).expanduser().resolve()report_base = {@@ -1269,6 +1280,9 @@ def run_loop(cycle = nonloop.select_cut(D, masses, fps=fps,ending=partial(nonloop.hold_ending, boxes=boxes, state=state))nonloop.validate_body(source_frames[cycle["start"]:cycle["end_frame"] + 1])+ elif cycle_mode == "lifetime":+ cycle = nonloop.select_cut(D, masses, fps=fps, kind=cycle_mode,+ ending=effect.lifetime_ending)elif cycle_mode == "fixed":if start is None or length is None:raise SystemExit("video-loop: --cycle fixed needs --start and --length")@@ -1340,7 +1354,7 @@ def run_loop(# Motion review is pixel preserving: cleanup belongs to the keyed input (`video-frames` erases# the specks far from the body). The other cuts erase every loose piece of solid pixels under# the speck size, wherever it is: the outline's islands and a drawn shadow as well.- if anchor not in ("motion", "motion-auto"):+ if anchor not in ("motion", "motion-auto") and cycle["kind"] != "lifetime":scrubbed += _scrub(im)im, d = drop_specks(im, alpha_over=16, diagonal=False, apart=0)specks += d@@ -1369,7 +1383,7 @@ def run_loop(write_loop_report(target, {**report_base, "status": "failed", "error": error, "cycle": cycle})raise SystemExit(error) from excreport_base["motion_anchor"] = motion- if anchor in ("none", "body"):+ if anchor in ("none", "body") and cycle["kind"] != "lifetime":from sprite_gen.video.source import RECIPEreport_base["source_projection"] = {"recipe": RECIPE, "wrap_dx_px": wrap_dx,@@ -1482,7 +1496,7 @@ def run_loop(if abs(report_base["cut_size"]["change"]) >= gait_fallback.SIZE_HOLD_MIN:standing_frame = istrip_of = partial(build_strip, max_height=strip_height, cycle_seconds=cycle_seconds, body_height=body_height, anchor="feet" if anchor == "feet" else "none", kind=str(cycle.get("kind") or "periodic"),- standing_src=standing_height(files[standing_frame], standing_frame) if body_height is not None else None,+ standing_src=standing_height(files[standing_frame], standing_frame) if body_height is not None and cycle["kind"] != "lifetime" else None,standing_frame=standing_frame)strip, strip_meta = strip_of(frames)# The cut as its seam is read: each cell given way to the frame beside it as filmed (`seam_as_filmed`).@@ -1578,7 +1592,8 @@ def run_loop("resampled_inner_mean_adjacent": resampled_adjacent,"resampled_seam_ratio": seam_ratio})raise SystemExit(error)- save_clean_gif(pick, gif_path, duration_ms=delay_ms, loop=None if single_play else 0, alpha_threshold=128)+ save_clean_gif(pick, gif_path, duration_ms=delay_ms, loop=None if single_play else 0,+ alpha_threshold=0 if cycle["kind"] == "lifetime" else 128)write_webp(pick, webp_path, delay_ms=delay_ms, workdir=out_dir / ".webp-frames", **({"loop": 1} if single_play else {}))shutil.rmtree(out_dir / ".webp-frames", ignore_errors=True)# A filmed ghost given way to the frame beside it is that frame shown twice in a row: one frame of the GIF or WebP