| 1 | """Storage transport, PNG codec, diff metrics, synthetic plates, and Fusion .comp primitives.""" |
| 2 | |
| 3 | import math |
| 4 | import struct |
| 5 | import subprocess |
| 6 | import sys |
| 7 | import zlib |
| 8 | from pathlib import Path |
| 9 | from typing import Any, Dict, List, Optional, Tuple |
| 10 | |
| 11 | REPO = Path(__file__).resolve().parents[2] |
| 12 | RENDER_CHECK = REPO / "target/release/render-check" |
| 13 | SMB_ROOT = Path("/Volumes/clover/Project/CloverCompositor/probes/diff_fuzz") |
| 14 | REMOTE_ROOT = "/srv/clover/Project/CloverCompositor/probes/diff_fuzz" |
| 15 | CONTAINER_ROOT = "/Volumes/clover/Project/CloverCompositor/probes/diff_fuzz" |
| 16 | LOCAL_FALLBACK = Path("/tmp/fission_diff_fuzz") |
| 17 | |
| 18 | # --------------------------------------------------------------------------- |
| 19 | # Storage / SG Transport Helpers |
| 20 | # --------------------------------------------------------------------------- |
| 21 | |
| 22 | |
| 23 | def use_smb() -> bool: |
| 24 | return Path("/Volumes/clover/Project/CloverCompositor").exists() |
| 25 | |
| 26 | |
| 27 | def local_root() -> Path: |
| 28 | return SMB_ROOT if use_smb() else LOCAL_FALLBACK |
| 29 | |
| 30 | |
| 31 | def ensure_dirs() -> Path: |
| 32 | root = local_root() |
| 33 | for sub in ("assets", "comps", "gt", "ours"): |
| 34 | (root / sub).mkdir(parents=True, exist_ok=True) |
| 35 | return root |
| 36 | |
| 37 | |
| 38 | def sync_asset_to_remote(local_path: Path) -> None: |
| 39 | if not use_smb(): |
| 40 | subprocess.run( |
| 41 | ["sg", "upload", str(local_path), f"{REMOTE_ROOT}/assets/{local_path.name}"], |
| 42 | check=True, |
| 43 | timeout=60, |
| 44 | ) |
| 45 | |
| 46 | |
| 47 | def ensure_render_check() -> Path: |
| 48 | if RENDER_CHECK.exists(): |
| 49 | return RENDER_CHECK |
| 50 | print(f"[build] {RENDER_CHECK} missing; building render-check --release...", file=sys.stderr) |
| 51 | subprocess.run( |
| 52 | ["cargo", "build", "--release", "-p", "engine", "--bin", "render-check"], |
| 53 | cwd=str(REPO), |
| 54 | check=True, |
| 55 | timeout=600, |
| 56 | ) |
| 57 | return RENDER_CHECK |
| 58 | |
| 59 | |
| 60 | # --------------------------------------------------------------------------- |
| 61 | # Pure Python Stdlib PNG Encoder / Decoder & Diff Metrics |
| 62 | # --------------------------------------------------------------------------- |
| 63 | |
| 64 | |
| 65 | def write_png_rgba(path: Path, w: int, h: int, rgba_bytes: bytes) -> None: |
| 66 | def chunk(tag: bytes, data: bytes) -> bytes: |
| 67 | return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF) |
| 68 | |
| 69 | raw = bytearray() |
| 70 | stride = w * 4 |
| 71 | for y in range(h): |
| 72 | raw.append(0) |
| 73 | raw.extend(rgba_bytes[y * stride : (y + 1) * stride]) |
| 74 | |
| 75 | ihdr = struct.pack(">IIBBBBB", w, h, 8, 6, 0, 0, 0) |
| 76 | png = ( |
| 77 | b"\x89PNG\r\n\x1a\n" |
| 78 | + chunk(b"IHDR", ihdr) |
| 79 | + chunk(b"IDAT", zlib.compress(bytes(raw), level=6)) |
| 80 | + chunk(b"IEND", b"") |
| 81 | ) |
| 82 | path.parent.mkdir(parents=True, exist_ok=True) |
| 83 | if path.exists(): |
| 84 | try: |
| 85 | if path.read_bytes() == png: |
| 86 | return |
| 87 | except OSError: |
| 88 | pass |
| 89 | path.write_bytes(png) |
| 90 | |
| 91 | |
| 92 | def _paeth(a: int, b: int, c: int) -> int: |
| 93 | p = a + b - c |
| 94 | pa = abs(p - a) |
| 95 | pb = abs(p - b) |
| 96 | pc = abs(p - c) |
| 97 | if pa <= pb and pa <= pc: |
| 98 | return a |
| 99 | if pb <= pc: |
| 100 | return b |
| 101 | return c |
| 102 | |
| 103 | |
| 104 | def read_png_rgba(path: Path) -> Tuple[int, int, bytes]: |
| 105 | data = path.read_bytes() |
| 106 | if data[:8] != b"\x89PNG\r\n\x1a\n": |
| 107 | raise ValueError(f"Not a PNG file: {path}") |
| 108 | pos = 8 |
| 109 | w = h = bit_depth = color_type = interlace = 0 |
| 110 | idat = bytearray() |
| 111 | while pos + 8 <= len(data): |
| 112 | length = struct.unpack(">I", data[pos : pos + 4])[0] |
| 113 | tag = data[pos + 4 : pos + 8] |
| 114 | chunk_data = data[pos + 8 : pos + 8 + length] |
| 115 | pos += 12 + length |
| 116 | if tag == b"IHDR": |
| 117 | w, h, bit_depth, color_type, _comp, _filt, interlace = struct.unpack(">IIBBBBB", chunk_data) |
| 118 | elif tag == b"IDAT": |
| 119 | idat.extend(chunk_data) |
| 120 | elif tag == b"IEND": |
| 121 | break |
| 122 | |
| 123 | if interlace != 0: |
| 124 | raise ValueError(f"Interlaced PNG unsupported in pure-stdlib reader: {path}") |
| 125 | channels_map = {0: 1, 2: 3, 4: 2, 6: 4} |
| 126 | if color_type not in channels_map or bit_depth not in (8, 16): |
| 127 | raise ValueError(f"Unsupported PNG format (color={color_type}, depth={bit_depth}): {path}") |
| 128 | |
| 129 | ch = channels_map[color_type] |
| 130 | bypp = ch * (bit_depth // 8) |
| 131 | row_bytes = w * bypp |
| 132 | raw = zlib.decompress(bytes(idat)) |
| 133 | if len(raw) < h * (row_bytes + 1): |
| 134 | raise ValueError(f"Truncated PNG IDAT in {path}") |
| 135 | |
| 136 | prev = bytearray(row_bytes) |
| 137 | out = bytearray(w * h * 4) |
| 138 | idx_in = 0 |
| 139 | for y in range(h): |
| 140 | ftype = raw[idx_in] |
| 141 | idx_in += 1 |
| 142 | scan = bytearray(raw[idx_in : idx_in + row_bytes]) |
| 143 | idx_in += row_bytes |
| 144 | if ftype == 1: |
| 145 | for i in range(bypp, row_bytes): |
| 146 | scan[i] = (scan[i] + scan[i - bypp]) & 0xFF |
| 147 | elif ftype == 2: |
| 148 | for i in range(row_bytes): |
| 149 | scan[i] = (scan[i] + prev[i]) & 0xFF |
| 150 | elif ftype == 3: |
| 151 | for i in range(row_bytes): |
| 152 | a = scan[i - bypp] if i >= bypp else 0 |
| 153 | scan[i] = (scan[i] + ((a + prev[i]) >> 1)) & 0xFF |
| 154 | elif ftype == 4: |
| 155 | for i in range(row_bytes): |
| 156 | a = scan[i - bypp] if i >= bypp else 0 |
| 157 | b = prev[i] |
| 158 | c = prev[i - bypp] if i >= bypp else 0 |
| 159 | scan[i] = (scan[i] + _paeth(a, b, c)) & 0xFF |
| 160 | elif ftype != 0: |
| 161 | raise ValueError(f"Unknown PNG filter {ftype} in {path}") |
| 162 | prev = scan |
| 163 | |
| 164 | out_row = y * w * 4 |
| 165 | if bit_depth == 8: |
| 166 | if ch == 4: |
| 167 | out[out_row : out_row + w * 4] = scan |
| 168 | elif ch == 3: |
| 169 | for x in range(w): |
| 170 | si = x * 3 |
| 171 | di = out_row + x * 4 |
| 172 | out[di] = scan[si] |
| 173 | out[di + 1] = scan[si + 1] |
| 174 | out[di + 2] = scan[si + 2] |
| 175 | out[di + 3] = 255 |
| 176 | elif ch == 2: |
| 177 | for x in range(w): |
| 178 | si = x * 2 |
| 179 | di = out_row + x * 4 |
| 180 | v = scan[si] |
| 181 | out[di] = v |
| 182 | out[di + 1] = v |
| 183 | out[di + 2] = v |
| 184 | out[di + 3] = scan[si + 1] |
| 185 | else: |
| 186 | for x in range(w): |
| 187 | di = out_row + x * 4 |
| 188 | v = scan[x] |
| 189 | out[di] = v |
| 190 | out[di + 1] = v |
| 191 | out[di + 2] = v |
| 192 | out[di + 3] = 255 |
| 193 | else: |
| 194 | # 16-bit big endian -> 8-bit |
| 195 | for x in range(w): |
| 196 | si = x * bypp |
| 197 | di = out_row + x * 4 |
| 198 | vals = [((scan[si + k * 2] << 8) | scan[si + k * 2 + 1]) >> 8 for k in range(ch)] |
| 199 | if ch == 4: |
| 200 | out[di : di + 4] = bytes(vals) |
| 201 | elif ch == 3: |
| 202 | out[di : di + 4] = bytes((vals[0], vals[1], vals[2], 255)) |
| 203 | elif ch == 2: |
| 204 | out[di : di + 4] = bytes((vals[0], vals[0], vals[0], vals[1])) |
| 205 | else: |
| 206 | out[di : di + 4] = bytes((vals[0], vals[0], vals[0], 255)) |
| 207 | return w, h, bytes(out) |
| 208 | |
| 209 | |
| 210 | def compute_diff_metrics(ours_path: Path, gt_path: Path) -> Dict[str, Any]: |
| 211 | if not ours_path.exists(): |
| 212 | return {"error": f"missing ours: {ours_path}", "mae_pct": 100.0, "mae_255": 255.0, "rmse_255": 255.0} |
| 213 | if not gt_path.exists(): |
| 214 | return {"error": f"missing gt: {gt_path}", "mae_pct": 100.0, "mae_255": 255.0, "rmse_255": 255.0} |
| 215 | |
| 216 | ow, oh, ob = read_png_rgba(ours_path) |
| 217 | gw, gh, gb = read_png_rgba(gt_path) |
| 218 | size_mismatch = (ow != gw) or (oh != gh) |
| 219 | |
| 220 | # Compare over overlapping or resized if mismatched, matching render-check's RGB metric |
| 221 | cw = min(ow, gw) |
| 222 | ch = min(oh, gh) |
| 223 | if size_mismatch: |
| 224 | # Bilinear sample gt onto ow x oh just like render-check, but still flag size_mismatch! |
| 225 | sum_abs = 0 |
| 226 | sum_sq = 0 |
| 227 | max_diff = 0 |
| 228 | for y in range(oh): |
| 229 | gy = min(gh - 1, int((y + 0.5) * gh / oh)) |
| 230 | for x in range(ow): |
| 231 | gx = min(gw - 1, int((x + 0.5) * gw / ow)) |
| 232 | oi = (y * ow + x) * 4 |
| 233 | gi = (gy * gw + gx) * 4 |
| 234 | for c in range(3): |
| 235 | d = abs(ob[oi + c] - gb[gi + c]) |
| 236 | sum_abs += d |
| 237 | sum_sq += d * d |
| 238 | if d > max_diff: |
| 239 | max_diff = d |
| 240 | n = ow * oh * 3 |
| 241 | else: |
| 242 | sum_abs = 0 |
| 243 | sum_sq = 0 |
| 244 | max_diff = 0 |
| 245 | n = ow * oh * 3 |
| 246 | for i in range(0, ow * oh * 4, 4): |
| 247 | d0 = abs(ob[i] - gb[i]) |
| 248 | d1 = abs(ob[i + 1] - gb[i + 1]) |
| 249 | d2 = abs(ob[i + 2] - gb[i + 2]) |
| 250 | sum_abs += d0 + d1 + d2 |
| 251 | sum_sq += d0 * d0 + d1 * d1 + d2 * d2 |
| 252 | m = max(d0, d1, d2) |
| 253 | if m > max_diff: |
| 254 | max_diff = m |
| 255 | |
| 256 | mae_255 = sum_abs / max(1, n) |
| 257 | rmse_255 = math.sqrt(sum_sq / max(1, n)) |
| 258 | mae_pct = mae_255 / 2.55 |
| 259 | return { |
| 260 | "mae_255": round(mae_255, 4), |
| 261 | "mae_pct": round(mae_pct, 4), |
| 262 | "rmse_255": round(rmse_255, 4), |
| 263 | "max_diff": max_diff, |
| 264 | "dims_ours": [ow, oh], |
| 265 | "dims_gt": [gw, gh], |
| 266 | "size_mismatch": size_mismatch, |
| 267 | } |
| 268 | |
| 269 | |
| 270 | # --------------------------------------------------------------------------- |
| 271 | # Synthetic Test Plate & Geometry Generators |
| 272 | # --------------------------------------------------------------------------- |
| 273 | |
| 274 | |
| 275 | def _hsv_to_rgb(h: float, s: float, v: float) -> Tuple[float, float, float]: |
| 276 | h = (h % 1.0) * 6.0 |
| 277 | i = int(h) |
| 278 | f = h - i |
| 279 | p = v * (1.0 - s) |
| 280 | q = v * (1.0 - s * f) |
| 281 | t = v * (1.0 - s * (1.0 - f)) |
| 282 | return [(v, t, p), (q, v, p), (p, v, t), (p, q, v), (t, p, v), (v, p, q)][i % 6] |
| 283 | |
| 284 | |
| 285 | def _clamp8(v: float) -> int: |
| 286 | return max(0, min(255, int(round(v * 255.0)))) |
| 287 | |
| 288 | |
| 289 | def generate_assets() -> Dict[str, str]: |
| 290 | """Generates all synthetic test plates & cube.obj under probes/diff_fuzz/assets/.""" |
| 291 | root = ensure_dirs() |
| 292 | assets_dir = root / "assets" |
| 293 | |
| 294 | # 1. plate_color.png (256x256): HSV + gray/primary ramps + geometric edges + alpha gradient |
| 295 | p_color = assets_dir / "plate_color.png" |
| 296 | w, h = 256, 256 |
| 297 | buf = bytearray(w * h * 4) |
| 298 | for y in range(h): |
| 299 | ny = y / (h - 1) |
| 300 | for x in range(w): |
| 301 | nx = x / (w - 1) |
| 302 | r, g, b, a = 0.0, 0.0, 0.0, 1.0 |
| 303 | if ny < 0.40: |
| 304 | r, g, b = _hsv_to_rgb(nx, ny / 0.40, 0.90) |
| 305 | elif ny < 0.60: |
| 306 | if nx < 0.5: |
| 307 | g0 = nx * 2.0 |
| 308 | r, g, b = g0, g0, g0 |
| 309 | else: |
| 310 | bar = int((nx - 0.5) * 12.0) % 6 |
| 311 | r, g, b = [ |
| 312 | (0.95, 0.15, 0.15), |
| 313 | (0.15, 0.90, 0.20), |
| 314 | (0.15, 0.30, 0.95), |
| 315 | (0.95, 0.90, 0.15), |
| 316 | (0.15, 0.90, 0.95), |
| 317 | (0.95, 0.15, 0.90), |
| 318 | ][bar] |
| 319 | elif ny < 0.82: |
| 320 | # Sharp geometric edges (rotated diamond + circle + stripes) |
| 321 | r, g, b = 0.12, 0.14, 0.18 |
| 322 | dx, dy = abs(nx - 0.30), abs(ny - 0.71) |
| 323 | if dx + dy < 0.11: |
| 324 | r, g, b = 0.96, 0.52, 0.12 |
| 325 | if math.hypot(nx - 0.72, ny - 0.71) < 0.09: |
| 326 | r, g, b = 0.20, 0.85, 0.95 |
| 327 | if ((x + y) // 6) % 2 == 0 and 0.45 < nx < 0.58: |
| 328 | r, g, b = 0.92, 0.92, 0.92 |
| 329 | else: |
| 330 | r, g, b = _hsv_to_rgb(nx, 0.85, 0.92) |
| 331 | a = max(0.0, min(1.0, 1.0 - (ny - 0.82) / 0.18)) |
| 332 | idx = (y * w + x) * 4 |
| 333 | buf[idx] = _clamp8(r) |
| 334 | buf[idx + 1] = _clamp8(g) |
| 335 | buf[idx + 2] = _clamp8(b) |
| 336 | buf[idx + 3] = _clamp8(a) |
| 337 | write_png_rgba(p_color, w, h, bytes(buf)) |
| 338 | sync_asset_to_remote(p_color) |
| 339 | |
| 340 | # 2. plate_fg.png (256x256): semi-transparent shapes, fine diagonal stripes, soft radial falloff |
| 341 | p_fg = assets_dir / "plate_fg.png" |
| 342 | buf = bytearray(w * h * 4) |
| 343 | for y in range(h): |
| 344 | ny = y / (h - 1) |
| 345 | for x in range(w): |
| 346 | nx = x / (w - 1) |
| 347 | r, g, b, a = 0.0, 0.0, 0.0, 0.0 |
| 348 | # Soft radial falloff orb (top-left) |
| 349 | d_orb = math.hypot(nx - 0.28, ny - 0.28) |
| 350 | if d_orb < 0.22: |
| 351 | t = 1.0 - d_orb / 0.22 |
| 352 | r, g, b = 0.95 * t + 0.2 * (1.0 - t), 0.35 + 0.55 * math.sin(d_orb * 40.0) ** 2, 0.90 * (1.0 - 0.4 * t) |
| 353 | a = t * t * (3.0 - 2.0 * t) |
| 354 | # Fine diagonal stripes block (top-right) |
| 355 | if 0.54 <= nx <= 0.90 and 0.10 <= ny <= 0.44: |
| 356 | stripe = ((x - y) // 2) % 2 == 0 |
| 357 | r, g, b = (0.95, 0.85, 0.15) if stripe else (0.15, 0.22, 0.55) |
| 358 | a = 0.85 if stripe else 0.55 |
| 359 | # Overlapping semi-transparent discs (bottom-left) |
| 360 | d1 = math.hypot(nx - 0.24, ny - 0.72) < 0.14 |
| 361 | d2 = math.hypot(nx - 0.36, ny - 0.72) < 0.14 |
| 362 | if d1 and d2: |
| 363 | r, g, b, a = 0.85, 0.55, 0.95, 0.75 |
| 364 | elif d1: |
| 365 | r, g, b, a = 0.10, 0.85, 0.90, 0.55 |
| 366 | elif d2: |
| 367 | r, g, b, a = 0.95, 0.20, 0.65, 0.55 |
| 368 | # Asymmetric crisp L-marker (bottom-right) |
| 369 | if (0.62 <= nx <= 0.68 and 0.58 <= ny <= 0.86) or (0.62 <= nx <= 0.86 and 0.80 <= ny <= 0.86): |
| 370 | r, g, b, a = 0.98, 0.98, 0.98, 1.0 |
| 371 | idx = (y * w + x) * 4 |
| 372 | buf[idx] = _clamp8(r) |
| 373 | buf[idx + 1] = _clamp8(g) |
| 374 | buf[idx + 2] = _clamp8(b) |
| 375 | buf[idx + 3] = _clamp8(a) |
| 376 | write_png_rgba(p_fg, w, h, bytes(buf)) |
| 377 | sync_asset_to_remote(p_fg) |
| 378 | |
| 379 | # 3. plate_small.png (160x120): mismatched resolution test plate |
| 380 | p_small = assets_dir / "plate_small.png" |
| 381 | sw, sh = 160, 120 |
| 382 | sbuf = bytearray(sw * sh * 4) |
| 383 | for y in range(sh): |
| 384 | ny = y / (sh - 1) |
| 385 | for x in range(sw): |
| 386 | nx = x / (sw - 1) |
| 387 | r, g, b, a = 0.18, 0.22, 0.30, 0.95 |
| 388 | if x < 5 or x >= sw - 5 or y < 5 or y >= sh - 5: |
| 389 | if y < 5: |
| 390 | r, g, b, a = 0.95, 0.20, 0.20, 1.0 |
| 391 | elif y >= sh - 5: |
| 392 | r, g, b, a = 0.20, 0.90, 0.25, 1.0 |
| 393 | elif x < 5: |
| 394 | r, g, b, a = 0.20, 0.40, 0.95, 1.0 |
| 395 | else: |
| 396 | r, g, b, a = 0.95, 0.85, 0.15, 1.0 |
| 397 | else: |
| 398 | if ((x // 10) + (y // 10)) % 2 == 0: |
| 399 | r, g, b = 0.72, 0.76, 0.82 |
| 400 | if abs(nx - ny) < 0.04 or abs(nx - (1.0 - ny)) < 0.04: |
| 401 | r, g, b = 0.95, 0.35, 0.10 |
| 402 | d_c = math.hypot(nx - 0.5, ny - 0.5) |
| 403 | if d_c < 0.22: |
| 404 | a = max(0.2, 1.0 - (0.22 - d_c) / 0.22 * 0.7) |
| 405 | idx = (y * sw + x) * 4 |
| 406 | sbuf[idx] = _clamp8(r) |
| 407 | sbuf[idx + 1] = _clamp8(g) |
| 408 | sbuf[idx + 2] = _clamp8(b) |
| 409 | sbuf[idx + 3] = _clamp8(a) |
| 410 | write_png_rgba(p_small, sw, sh, bytes(sbuf)) |
| 411 | sync_asset_to_remote(p_small) |
| 412 | |
| 413 | # 4. plate_bump.png (256x256): grayscale heightmap / bump test texture |
| 414 | p_bump = assets_dir / "plate_bump.png" |
| 415 | bbuf = bytearray(w * h * 4) |
| 416 | for y in range(h): |
| 417 | ny = y / (h - 1) |
| 418 | for x in range(w): |
| 419 | nx = x / (w - 1) |
| 420 | d = math.hypot(nx - 0.5, ny - 0.5) |
| 421 | dome = max(0.0, 1.0 - (d / 0.38) ** 2) ** 0.5 if d < 0.38 else 0.0 |
| 422 | ripple = 0.5 + 0.5 * math.sin(d * 48.0) |
| 423 | step = 0.25 * math.floor(nx * 5.0) / 4.0 |
| 424 | val = max(0.0, min(1.0, 0.50 * dome + 0.30 * ripple + 0.20 * step)) |
| 425 | v8 = _clamp8(val) |
| 426 | idx = (y * w + x) * 4 |
| 427 | bbuf[idx] = v8 |
| 428 | bbuf[idx + 1] = v8 |
| 429 | bbuf[idx + 2] = v8 |
| 430 | bbuf[idx + 3] = 255 |
| 431 | write_png_rgba(p_bump, w, h, bytes(bbuf)) |
| 432 | sync_asset_to_remote(p_bump) |
| 433 | |
| 434 | # 5. cube.obj: small valid triangulated OBJ mesh |
| 435 | p_obj = assets_dir / "cube.obj" |
| 436 | obj_text = """# Synthetic unit cube for SurfaceFBXMesh differential testing |
| 437 | o UnitCube |
| 438 | v -0.5 -0.5 0.5 |
| 439 | v 0.5 -0.5 0.5 |
| 440 | v 0.5 0.5 0.5 |
| 441 | v -0.5 0.5 0.5 |
| 442 | v -0.5 -0.5 -0.5 |
| 443 | v 0.5 -0.5 -0.5 |
| 444 | v 0.5 0.5 -0.5 |
| 445 | v -0.5 0.5 -0.5 |
| 446 | vt 0.0 0.0 |
| 447 | vt 1.0 0.0 |
| 448 | vt 1.0 1.0 |
| 449 | vt 0.0 1.0 |
| 450 | vn 0.0 0.0 1.0 |
| 451 | vn 0.0 0.0 -1.0 |
| 452 | vn 1.0 0.0 0.0 |
| 453 | vn -1.0 0.0 0.0 |
| 454 | vn 0.0 1.0 0.0 |
| 455 | vn 0.0 -1.0 0.0 |
| 456 | f 1/1/1 2/2/1 3/3/1 |
| 457 | f 1/1/1 3/3/1 4/4/1 |
| 458 | f 6/1/2 5/2/2 8/3/2 |
| 459 | f 6/1/2 8/3/2 7/4/2 |
| 460 | f 2/1/3 6/2/3 7/3/3 |
| 461 | f 2/1/3 7/3/3 3/4/3 |
| 462 | f 5/1/4 1/2/4 4/3/4 |
| 463 | f 5/1/4 4/3/4 8/4/4 |
| 464 | f 4/1/5 3/2/5 7/3/5 |
| 465 | f 4/1/5 7/3/5 8/4/5 |
| 466 | f 5/1/6 6/2/6 2/3/6 |
| 467 | f 5/1/6 2/3/6 1/4/6 |
| 468 | """ |
| 469 | if not p_obj.exists() or p_obj.read_text() != obj_text: |
| 470 | p_obj.write_text(obj_text) |
| 471 | sync_asset_to_remote(p_obj) |
| 472 | |
| 473 | return { |
| 474 | "color": f"{CONTAINER_ROOT}/assets/plate_color.png", |
| 475 | "fg": f"{CONTAINER_ROOT}/assets/plate_fg.png", |
| 476 | "small": f"{CONTAINER_ROOT}/assets/plate_small.png", |
| 477 | "bump": f"{CONTAINER_ROOT}/assets/plate_bump.png", |
| 478 | "cube_obj": f"{CONTAINER_ROOT}/assets/cube.obj", |
| 479 | } |
| 480 | |
| 481 | |
| 482 | # --------------------------------------------------------------------------- |
| 483 | # Fusion .comp Serialization Primitives |
| 484 | # --------------------------------------------------------------------------- |
| 485 | |
| 486 | |
| 487 | def step_spline(name: str, frame_values: Dict[int, float]) -> str: |
| 488 | kfs = [f"\t\t\t\t[{f}] = {{ {v!r}, Flags = {{ StepIn = true }} }}," for f, v in sorted(frame_values.items())] |
| 489 | return f"\t\t{name} = BezierSpline {{\n\t\t\tKeyFrames = {{\n" + "\n".join(kfs) + "\n\t\t\t},\n\t\t}," |
| 490 | |
| 491 | |
| 492 | def linear_spline(name: str, frame_values: Dict[int, float]) -> str: |
| 493 | kfs = [f"\t\t\t\t[{f}] = {{ {v!r}, Flags = {{ Linear = true }} }}," for f, v in sorted(frame_values.items())] |
| 494 | return f"\t\t{name} = BezierSpline {{\n\t\t\tKeyFrames = {{\n" + "\n".join(kfs) + "\n\t\t\t},\n\t\t}," |
| 495 | |
| 496 | |
| 497 | def lut_bezier(name: str, pts: List[Tuple[float, float]]) -> str: |
| 498 | kfs = "\n".join(f"\t\t\t\t\t[{x}] = {{ {y} }}," for x, y in pts) |
| 499 | return ( |
| 500 | f"\t\t{name} = LUTBezier {{\n" |
| 501 | f"\t\t\tSmoothEndsTransition = true,\n" |
| 502 | f"\t\t\tKeyColorSplines = {{\n" |
| 503 | f"\t\t\t\t[0] = {{\n{kfs}\n\t\t\t\t}}\n" |
| 504 | f"\t\t\t}},\n\t\t}}," |
| 505 | ) |
| 506 | |
| 507 | |
| 508 | def make_loader(name: str, container_path: str, post_mult: int = 1, depth: Optional[int] = None) -> str: |
| 509 | extra = f"\n\t\t\t\tDepth = Input {{ Value = {depth}, }}," if depth is not None else "" |
| 510 | return f"""\t\t{name} = Loader {{ |
| 511 | \t\t\tClips = {{ |
| 512 | \t\t\t\tClip {{ |
| 513 | \t\t\t\t\tID = "Clip1", |
| 514 | \t\t\t\t\tFilename = "{container_path}", |
| 515 | \t\t\t\t\tFormatID = "PNGFormat", |
| 516 | \t\t\t\t\tLength = 1, |
| 517 | \t\t\t\t\tLengthSetManually = true, |
| 518 | \t\t\t\t\tTrimIn = 0, |
| 519 | \t\t\t\t\tTrimOut = 0, |
| 520 | \t\t\t\t\tExtendFirst = 0, |
| 521 | \t\t\t\t\tExtendLast = 0, |
| 522 | \t\t\t\t\tLoop = 1, |
| 523 | \t\t\t\t\tStartFrame = -1, |
| 524 | \t\t\t\t\tGlobalStart = 0, |
| 525 | \t\t\t\t\tGlobalEnd = 1000, |
| 526 | \t\t\t\t}}, |
| 527 | \t\t\t}}, |
| 528 | \t\t\tInputs = {{ |
| 529 | \t\t\t\t["Clip1.PNGFormat.PostMultiply"] = Input {{ Value = {post_mult}, }},{extra} |
| 530 | \t\t\t}}, |
| 531 | \t\t}},""" |
| 532 | |
| 533 | |
| 534 | def make_saver(src_op: str, prefix: str) -> str: |
| 535 | return f"""\t\tSaver1 = Saver {{ |
| 536 | \t\t\tInputs = {{ |
| 537 | \t\t\t\tClip = Input {{ |
| 538 | \t\t\t\t\tValue = Clip {{ |
| 539 | \t\t\t\t\t\tFilename = "{CONTAINER_ROOT}/gt/{prefix}_0000.png", |
| 540 | \t\t\t\t\t\tFormatID = "PNGFormat", |
| 541 | \t\t\t\t\t\tLength = 0, |
| 542 | \t\t\t\t\t\tSaving = true, |
| 543 | \t\t\t\t\t\tTrimIn = 0, |
| 544 | \t\t\t\t\t\tExtendFirst = 0, |
| 545 | \t\t\t\t\t\tExtendLast = 0, |
| 546 | \t\t\t\t\t\tLoop = 1, |
| 547 | \t\t\t\t\t\tAspectMode = 0, |
| 548 | \t\t\t\t\t\tDepth = 0, |
| 549 | \t\t\t\t\t\tGlobalStart = -2000000000, |
| 550 | \t\t\t\t\t\tGlobalEnd = 0, |
| 551 | \t\t\t\t\t}}, |
| 552 | \t\t\t\t}}, |
| 553 | \t\t\t\tOutputFormat = Input {{ Value = FuID {{ "PNGFormat" }}, }}, |
| 554 | \t\t\t\tInput = Input {{ SourceOp = "{src_op}", Source = "Output", }}, |
| 555 | \t\t\t}}, |
| 556 | \t\t}},""" |
| 557 | |
| 558 | |
| 559 | def make_comp(tools_body: str, end_frame: int, w: int = 256, h: int = 256, start_frame: int = 0) -> str: |
| 560 | return f"""Composition {{ |
| 561 | \tCurrentTime = {start_frame}, |
| 562 | \tRenderRange = {{ {start_frame}, {end_frame} }}, |
| 563 | \tGlobalRange = {{ {start_frame}, {end_frame} }}, |
| 564 | \tCurrentID = 500, |
| 565 | \tHiQ = true, |
| 566 | \tPlaybackUpdateMode = 0, |
| 567 | \tVersion = "Fusion Studio 21.1 build 23", |
| 568 | \tTools = ordered() {{ |
| 569 | {tools_body} |
| 570 | \t}}, |
| 571 | \tPrefs = {{ |
| 572 | \t\tComp = {{ |
| 573 | \t\t\tFrameFormat = {{ |
| 574 | \t\t\t\tWidth = {w}, |
| 575 | \t\t\t\tHeight = {h}, |
| 576 | \t\t\t\tRate = 24, |
| 577 | \t\t\t}}, |
| 578 | \t\t}}, |
| 579 | \t}}, |
| 580 | }} |
| 581 | """ |
| 582 | |
| 583 | |
| 584 | def build_switch_chain( |
| 585 | cases: List[Tuple[int, str, str]], |
| 586 | bg_name: str, |
| 587 | prefix_tag: str, |
| 588 | ) -> Tuple[List[str], List[str], str]: |
| 589 | """Chains Merge switches so frame f outputs op_name for case (f, label, op_name).""" |
| 590 | nodes: List[str] = [] |
| 591 | splines: List[str] = [] |
| 592 | prev = bg_name |
| 593 | frames = [c[0] for c in cases] |
| 594 | for f, _label, op_name in cases: |
| 595 | bl = {fr: (1.0 if fr == f else 0.0) for fr in frames} |
| 596 | sname = f"{prefix_tag}_{f}_Blend" |
| 597 | splines.append(step_spline(sname, bl)) |
| 598 | sw_name = f"{prefix_tag}_Sw_{f}" |
| 599 | nodes.append( |
| 600 | f"""\t\t{sw_name} = Merge {{ |
| 601 | \t\t\tInputs = {{ |
| 602 | \t\t\t\tBlend = Input {{ SourceOp = "{sname}", Source = "Value", }}, |
| 603 | \t\t\t\tBackground = Input {{ SourceOp = "{prev}", Source = "Output", }}, |
| 604 | \t\t\t\tForeground = Input {{ SourceOp = "{op_name}", Source = "Output", }}, |
| 605 | \t\t\t}}, |
| 606 | \t\t}},""" |
| 607 | ) |
| 608 | prev = sw_name |
| 609 | return nodes, splines, prev |
| 610 | |
| 611 | |