A 3D model pipeline, pushed by Gemini 4 Argon
agent (cont.)
+14:43read_file{"EndLine":140,"Path":"~/work/lowmarch/review/vroid-02/build.py","StartLine":1}
input
{ "EndLine": 140, "Path": "~/work/lowmarch/review/vroid-02/build.py", "StartLine": 1 }result, 141 lines
Showing lines 1-140 of 391 1: """Build the model-viewer review page for the cast on stage, and their duel. 2: 3: python3 review/vroid-02/build.py every character, the ground and the page 4: python3 review/vroid-02/build.py elaine just Elaine (the others stay as last built) 5: 6: Writes to review/out/vroid-02/site/: 7: <name>.glb.json each character, skinned, in the reference (bust) pose, with animation clips: 8: idle, attack, clash (the two-character sword bind) and apose (arms out, for 9: checking a costume where hanging arms would hide it). Artifact hosting serves 10: .json but not .glb, so the model is base64-wrapped. 11: plains.glb.json the battle ground (static) 12: camera.json per character: the cameras for both views plus what the live renderer needs 13: (palette roles, lights, colliders, cloth grids); the duel's stage camera and 14: where each fighter stands 15: checks.json per character: the automatic checks (tools/checks.py) over every keyframe: arms 16: through the body, and garments showing through plate 17: and review/out/vroid-02/geom/<name>.npz (every piece as built), which tools/regress.py compares with the 18: last published version (snapshot.py) after each build. 19: then copies index.html next to them. The duel's poses are cached (they take about a minute to 20: solve) and re-solved when the rig, the duel or the cast entries change. 21: """ 22: import base64 23: import hashlib 24: import json 25: import os 26: import pickle 27: import shutil 28: import sys 29: from concurrent.futures import ProcessPoolExecutor 30: 31: import numpy as np 32: 33: HERE = os.path.dirname(os.path.abspath(__file__)) 34: ROOT = os.path.dirname(os.path.dirname(HERE)) 35: sys.path[:0] = [os.path.join(ROOT, "tools"), os.path.join(ROOT, "art", "cast"), HERE] 36: import vroid_portrait as vp # noqa: E402 37: import poses as P # noqa: E402 38: import terrain # noqa: E402 39: import export_glb # noqa: E402 40: import clips # noqa: E402 41: import duel # noqa: E402 42: import checks # noqa: E402 43: from duel_check import chars_for # noqa: E402 44: from cast import CAST # noqa: E402 45: 46: VROID = os.environ.get("LOWMARCH_VROID", os.path.expanduser("~/.local/share/lowmarch/vroid")) 47: # which review page: the cast viewer (vroid-02), or a workshop page on a smaller roster (its own artifact) 48: SITE = os.environ.get("LOWMARCH_SITE", "vroid-02") 49: WORKSHOPS = { 50: "mh-workshop": dict( 51: title="Body workshop", eyebrow="Lowmarch · MakeHuman bodies, VRoid heads", 52: roster=["mob_mh_youth", "mob_mh_veteran", "mob_mh_brute", "mob_mh_f_youth", "mob_mh_f_veteran", "mob_mh_f_strong"], 53: notes=os.path.join(HERE, "mh_workshop_notes.html")), 54: # the armory: bodies to assemble characters from, T-posed and bare (no clips, no checks): VRoid's base shaped by 55: # MakeHuman references (art/cast/cast.py ARMORY), which are built but never shown 56: "armory": dict( 57: title="Armory", eyebrow="Lowmarch · VRoid bodies shaped by MakeHuman, T-posed for inspection", 58: roster=[n for n in CAST if n.startswith("arm_")], armory=True, 59: notes=os.path.join(HERE, "armory_notes.html")), 60: "base-spike": dict( 61: title="Base spike", eyebrow="Lowmarch · which base model: MakeHuman, VRoid heads, Quaternius", 62: roster=[n for n in CAST if n.startswith("spk_")], 63: notes=os.path.join(HERE, "base_spike_notes.html")), 64: } 65: WORKSHOP_ONLY = {n for w in WORKSHOPS.values() for n in w["roster"]} - {"mob_mh_youth", "mob_mh_veteran", "mob_mh_brute"} 66: OUT = os.path.join(ROOT, "review", "out", SITE, "site") 67: CACHE = os.path.join(ROOT, "review", "out", "vroid-02", "cache") 68: GEOM = os.path.join(ROOT, "review", "out", "vroid-02", "geom") # per-piece geometry of the last build 69: ORDER = {"player": "aldric", "opponent": "elaine"} 70: # the armory's skin picker (cast.SKINS): people light to deep, then fantasy 71: SKIN_PICKER = [("People", [("porcelain", "Porcelain"), ("ivory", "Ivory"), ("peach", "Peach"), ("sand", "Sand"), 72: ("honey", "Honey"), ("olive_mid", "Olive"), ("amber", "Amber"), ("caramel", "Caramel"), 73: ("chestnut", "Chestnut"), ("umber", "Umber"), ("mahogany", "Mahogany"), ("ebony", "Ebony")]), 74: ("Fantasy", [("drow_ash", "Dark elf, ash"), ("drow_slate", "Dark elf, slate"), ("ashen", "Ashen"), 75: ("orc", "Orc"), ("orc_deep", "Orc, deep"), ("sea", "Sea"), ("infernal", "Infernal")])] 76: LABELS = {"aldric": "Aldric", "elaine": "Elaine", "saber": "Saber (study)"} # saber: a benchmark, not for the game 77: MOBS = [k for k in CAST if k.startswith("mob_") and k not in WORKSHOP_ONLY] # common enemies: built, stood in the lineup 78: # MakeHuman-based units stand first, next to the VRoid soldier they are compared with 79: MOBS = sorted(MOBS, key=lambda k: (0 if k == "mob_soldier" else 1 if CAST[k].get("mh") else 2)) 80: LABELS.update({k: CAST[k].get("label", k[4:].title()) for k in MOBS}) 81: if SITE in WORKSHOPS: # a workshop page: only its roster, in its order, no duel 82: MOBS = list(WORKSHOPS[SITE]["roster"]) 83: LABELS = {k: CAST[k].get("label", k[4:].title()) for k in MOBS} 84: # arms raised 35 degrees from the T-pose: shows the waist, the sides and the armpits 85: APOSE = P.merge(P.BUST, {"J_Bip_L_UpperArm": P.rot((0, 0, 1), -37), "J_Bip_R_UpperArm": P.rot((0, 0, 1), 37), 86: "J_Bip_L_Shoulder": P.rot((0, 0, 1), -6), "J_Bip_R_Shoulder": P.rot((0, 0, 1), 6)}) 87: 88: 89: def costume_of(c): 90: """The cast entry's costume, plus kit hair (hair_style) and facial hair (beard: a style, or 91: (style, length)) if it has them.""" 92: import outfit 93: import hair 94: base = outfit.COSTUMES[c.get("costume") or "lord"] 95: extra = [] 96: if c.get("hair_style"): 97: extra.append(lambda fit: getattr(hair, c["hair_style"])(fit)) 98: if c.get("beard"): 99: style, length = (c["beard"], 0.0) if isinstance(c["beard"], str) else c["beard"] 100: extra.append(lambda fit: hair.beard(fit, style, length=length)) 101: if not extra: 102: return base 103: return lambda fit: base(fit) + [pc for f in extra for pc in f(fit)] 104: 105: 106: def model_path(name, c): 107: """A VRoid model under LOWMARCH_VROID, or a converted MakeHuman or Quaternius body, or a reshaped VRoid one 108: (tools/makehuman.py, tools/quaternius.py, tools/mass.py, tools/cage.py, run first with `python3 -I`: they 109: read downloaded data).""" 110: if c.get("mass"): 111: import mass 112: p = mass.path_for(name, c["mass"]) 113: if not os.path.exists(p): 114: raise SystemExit(f"{name}: no massed VRoid body; run python3 -I tools/mass.py first") 115: return p 116: if c.get("cage"): 117: import cage 118: p = cage.path_for(name, c["cage"]) 119: if not os.path.exists(p): 120: raise SystemExit(f"{name}: no cage body; run python3 -I tools/cage.py first") 121: return p 122: if c.get("qt"): 123: import quaternius 124: p = quaternius.path_for(name, c["qt"]) 125: if not os.path.exists(p): 126: raise SystemExit(f"{name}: no converted Quaternius body; run python3 -I tools/quaternius.py first") 127: return p 128: if not c.get("mh"): 129: return os.path.join(VROID, c["model"]) 130: import makehuman 131: p = makehuman.path_for(name, c["mh"]) 132: if not os.path.exists(p): 133: raise SystemExit(f"{name}: no converted MakeHuman body; run python3 -I tools/makehuman.py first") 134: return p 135: 136: 137: def arm_room(name, c, need=0.006, most=16): 138: """The smallest outward arm angle (degrees, shape["arm_out"]) at which the hanging arms clear the 139: body by `need` in the reference pose and the idle: broad bodies otherwise sink their arms into 140: their sides. MakeHuman bodies only for now (the VRoid cast was posed by hand)."""read_file{"EndLine":391,"Path":"~/work/lowmarch/review/vroid-02/build.py","StartLine":140}
input
{ "EndLine": 391, "Path": "~/work/lowmarch/review/vroid-02/build.py", "StartLine": 140 }result, 253 lines
Showing lines 140-391 of 391 140: their sides. MakeHuman bodies only for now (the VRoid cast was posed by hand).""" 141: import checks 142: from vrm import VRM 143: v = VRM(model_path(name, c)) 144: limbs = [f"{s}_{a}" for s in "LR" for a in ("UpperArm", "LowerArm")] 145: for deg in range(0, most + 1, 2): 146: b = checks.Body(v, dict(c["shape"], arm_out=float(deg))) 147: if min(b.worst(pose, limbs) for pose in (P.BUST, P.BATTLE_IDLE)) >= need: 148: return float(deg) 149: return float(most) 150: 151: 152: ARMORY = bool(WORKSHOPS.get(SITE, {}).get("armory")) # T-posed and bare: the reference pose is the T-pose 153: 154: 155: def portrait(name, view): 156: c = CAST[name] 157: if (c.get("mh") or c.get("qt") or c.get("mass") or c.get("cage")) and "arm_out" not in c["shape"] and not ARMORY: 158: c["shape"]["arm_out"] = arm_room(name, c) 159: return vp.Portrait(model_path(name, c), shape=c["shape"], 160: hair_from=os.path.join(VROID, c["hair"]) if c.get("hair") else None, 161: costume=costume_of(c), pose={} if ARMORY else vp.BUST_POSE, hair_trim=c.get("hair_trim"), 162: world=terrain.plains() if view == "battle" else None), c 163: 164: 165: def springs(v): 166: """The model's spring bones (VRM 0.x secondaryAnimation: VRoid's women's base has its breasts', a hairstyle its 167: hair's, tools/heads.py): each group's root bones, settings and collision spheres (on a bone, offset in its own 168: frame, before the export's turn), for the viewer's physics.""" 169: sa = v.j.get("extensions", {}).get("VRM", {}).get("secondaryAnimation", {}) 170: nodes = v.j["nodes"] 171: spheres = [[dict(bone=nodes[cg["node"]]["name"], offset=[c["offset"]["x"], c["offset"]["y"], c["offset"]["z"]], 172: radius=c["radius"]) for c in cg.get("colliders", [])] for cg in sa.get("colliderGroups", [])] 173: out = [] 174: for g in sa.get("boneGroups", []): 175: gd = g.get("gravityDir", {"x": 0, "y": -1, "z": 0}) 176: out.append(dict(bones=[nodes[b]["name"] for b in g["bones"]], stiffness=g.get("stiffiness", 1.0), 177: gravity=g.get("gravityPower", 0.0), gravity_dir=[gd["x"], gd["y"], gd["z"]], 178: drag=g.get("dragForce", 0.4), radius=g.get("hitRadius", 0.02), comment=g.get("comment", ""), 179: colliders=[c for k in g.get("colliderGroups", []) if k < len(spheres) for c in spheres[k]])) 180: return out 181: 182: 183: def wrap_json(path): 184: json.dump({"glb": base64.b64encode(open(path, "rb").read()).decode()}, open(path + ".json", "w")) 185: os.remove(path) 186: 187: 188: def character(args): 189: """The rigged character (reference pose) with its clips, its cameras, the live renderer's data and 190: its checks.""" 191: name, clash = args 192: p, c = portrait(name, "bust") 193: p.paint = c.get("paint") 194: pal = dict(vp.PALETTE, **c["colors"]) 195: cam_bust, cam_battle = p.frame("bust"), p.frame("battle") 196: if ARMORY: # nothing moves: every clip holds the T-pose 197: cl = {k: [(0.0, {}), (1.0, {})] for k in ("idle", "attack", "apose")} 198: else: 199: cl = clips.clips(p.v, c["shape"], c.get("stance", "sword")) 200: if clash: 201: cl["clash"] = clash 202: cl["apose"] = [(0.0, APOSE), (1.0, APOSE)] 203: path = f"{OUT}/{name}.glb" 204: size = export_glb.export_skinned(p, pal, path, clips=cl) 205: wrap_json(path) 206: save_geometry(name, p) 207: if ARMORY: # the armory's checks are proportions (tools/proportions.py) 208: return dict(name=name, size=size, cams=dict(bust=cam_bust, battle=cam_battle), 209: info=dict(export_glb.scene_info(p, pal), springs=springs(p.v)), checks=proportion_checks(name, c)) 210: body = checks.Body(p.v, c["shape"]) 211: keyed = {k: v for k, v in cl.items() if k != "apose"} 212: head, bad, touch = checks.report(name, keyed, body) 213: layers = checks.layer_report(name, keyed, checks.Layers(p, body)) 214: return dict(name=name, size=size, cams=dict(bust=cam_bust, battle=cam_battle), info=export_glb.scene_info(p, pal), 215: checks=dict(summary=head, through=bad, touching=touch, layers=layers)) 216: 217: 218: def save_geometry(name, p): 219: """Every piece's vertices as built (reference pose), for tools/regress.py to compare builds.""" 220: os.makedirs(GEOM, exist_ok=True) 221: arrays, seen = {}, {} 222: for q in p.prims: 223: key = q["material"] 224: seen[key] = seen.get(key, 0) + 1 225: if seen[key] > 1: 226: key = f"{key}#{seen[key]}" 227: arrays[key] = q["pos"][np.unique(q["tri"])].astype(np.float32) if len(q["tri"]) else q["pos"].astype(np.float32) 228: np.savez_compressed(os.path.join(GEOM, f"{name}.npz"), **arrays) 229: 230: 231: def _fit_of(c): 232: """The fit a body was made to (its own, or its cage reference's): the ratios it skips (the heavy targets' 233: widths were measured on armour), ranges of its own, its figure.""" 234: spec = c.get("mh") or {} 235: ref = (c.get("cage") or {}).get("ref") 236: if isinstance(ref, str): 237: spec = CAST[ref].get("mh", {}) 238: elif isinstance(ref, (list, tuple)): 239: spec = ref[1] 240: return spec.get("fit", {}) 241: 242: 243: def _nth(p): 244: n = int(round(p)) 245: return f"{n}{'th' if 10 <= n % 100 <= 20 else {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')}" 246: 247: 248: def proportion_checks(name, c): 249: """The model file against its archetype's targets (docs/proportions.md): a summary, and a line per ratio 250: outside its range (fails first); then its girths against real people of its height and waist 251: (tools/anthro.py), a line for each past the 5th or 95th percentile.""" 252: import anthro 253: import proportions as PR 254: path = model_path(name, c) 255: m = PR.measure(path) 256: arch = c.get("archetype") or PR.best_archetype(m, c.get("sex")) 257: t = PR.targets(arch, c.get("sex")) 258: want = _fit_of(c) 259: t.update({k: tuple(r) for k, r in want.get("ranges", {}).items()}) 260: t.update({k: tuple(r) for k, r in c.get("ranges", {}).items()}) # the body's own (cast.py CHECK_RANGES) 261: head = (c.get("cage") or {}).get("face") or {} 262: face = head.get("fit", {}).get("archetype") or head.get("archetype") 263: if face: # its face is judged by its face's archetype (tools/heads.py) 264: for k in ("eye_frac", "eye_open_CE", "iris_gap_CE", "face_w_CE"): 265: t.pop(k, None) 266: if k in PR.TARGETS[face]: 267: t[k] = PR.TARGETS[face][k] 268: # a face left as VRoid built it (no fit, 2026-10-13) has its ratios against the study's paintings reported, not 269: # judged: held to them, faces were stretched and read too sharp from the side 270: reported = ("H_CE", "eye_frac", "eye_open_CE", "iris_gap_CE", "face_w_CE") if head and "fit" not in head else () 271: for k in want.get("skip", ()): 272: t.pop(k, None) 273: for k in ("hand", "foot"): # judged against real hands and feet below instead 274: if want.get("anthro") and f"{k}_p" not in want.get("skip", ()): 275: t.pop(f"{k}_CE", None) 276: rows, faces = [], [] 277: for k, rng in t.items(): 278: v = m.get(k) 279: verdict = PR.judge(v, rng) 280: if verdict in ("warn", "FAIL") and k in reported: 281: faces.append(dict(verdict="face", text=f"{PR.RATIOS.get(k, k)}: {v:.2f}, the study's {rng[0]:g} to {rng[1]:g}")) 282: elif verdict in ("warn", "FAIL"): 283: rows.append(dict(verdict=verdict, text=f"{PR.RATIOS.get(k, k)}: {v:.2f}, target {rng[0]:g} to {rng[1]:g}" 284: + (" (fail)" if verdict == "FAIL" else " (near)"))) 285: rows.sort(key=lambda r: r["verdict"] != "FAIL") 286: n_fail = sum(r["verdict"] == "FAIL" for r in rows) 287: judged = [k for k in t if m.get(k) is not None and k not in reported] 288: summary = (f"{name}: {arch}, {len(judged) - len(rows)} of {len(judged)} ratios in range" 289: + (f", {n_fail} out" if n_fail else "") + (f", {len(rows) - n_fail} near" if len(rows) > n_fail else "")) 290: a = anthro.assess(path, c.get("sex"), name.endswith("_old")) 291: am = a.pop("_m") 292: who = f"{'women' if c.get('sex') == 'f' else 'men'} {am['stature'] / 100:.2f} m tall with a {am['waist']:.0f} cm waist" 293: real = [] 294: for k, (v, med, pct) in a.items(): 295: if k == "build": 296: continue 297: if np.isfinite(pct) and not 5 <= pct <= 95: 298: real.append(dict(verdict="real", text=f"Real bodies: {anthro.MEASURES[k][3]}: {v:.1f} cm, the {_nth(pct)} percentile" 299: f" of {who} (their median {med:.1f})")) 300: rows += real + faces 301: if real: 302: summary += f"; {len(real)} girths past the 5th to 95th percentile of real bodies" 303: m.update({f"real_{k}": v[0] for k, v in a.items()}) 304: m.update({f"real_{k}_pct": v[2] for k, v in a.items()}) 305: clean = {k: (None if v is None or not np.isfinite(v) else round(float(v), 4)) for k, v in m.items() 306: if isinstance(v, (int, float, np.floating))} # JSON has no NaN 307: return dict(summary=summary, through=[], touching=[], layers=None, props=rows, measures=clean) 308: 309: 310: def scenery(name): 311: p, c = portrait(name, "battle") 312: path = f"{OUT}/plains.glb" 313: size = export_glb.export(p, dict(vp.PALETTE, **c["colors"]), path, only_world=True) 314: wrap_json(path) 315: return size 316: 317: 318: def duel_cached(): 319: """The clash clip for both fighters, solved once per change to what it depends on.""" 320: src = "".join(open(os.path.join(ROOT, "tools", f)).read() for f in ("duel.py", "rig.py", "checks.py", "poses.py")) 321: key = hashlib.sha1((src + repr([CAST[n] for n in ORDER.values()])).encode()).hexdigest()[:16] 322: path = os.path.join(CACHE, f"duel_{key}.pkl") 323: if os.path.exists(path): 324: return pickle.load(open(path, "rb")) 325: print("solving the duel (about a minute)...", flush=True) 326: res = duel.duel_clips(chars_for(ORDER.values()), ORDER) 327: os.makedirs(CACHE, exist_ok=True) 328: pickle.dump(res, open(path, "wb")) 329: return res 330: 331: 332: def as_list(o): 333: return o.tolist() if isinstance(o, np.ndarray) else o 334: 335: 336: if __name__ == "__main__": 337: names = sys.argv[1:] or list(LABELS) 338: os.makedirs(OUT, exist_ok=True) 339: ws = WORKSHOPS.get(SITE) 340: clash, place = ({}, {}) if ws else duel_cached() 341: cam_path, chk_path = f"{OUT}/camera.json", f"{OUT}/checks.json" 342: old = json.load(open(cam_path)) if os.path.exists(cam_path) and sys.argv[1:] else {} 343: old_chk = json.load(open(chk_path)) if os.path.exists(chk_path) and sys.argv[1:] else {} 344: with ProcessPoolExecutor(4) as ex: 345: fc = [ex.submit(character, (n, clash.get(n))) for n in names] 346: fs = ex.submit(scenery, names[0]) if not sys.argv[1:] or not os.path.exists(f"{OUT}/plains.glb.json") else None 347: built = {r["name"]: r for r in (f.result() for f in fc)} 348: if fs: 349: print(f"plains.glb {fs.result() / 1e6:.2f} MB") 350: for n, r in built.items(): 351: print(f"{n}.glb {r['size'] / 1e6:.1f} MB") 352: print(" " + r["checks"]["summary"]) 353: if r["checks"]["layers"]: 354: print(" " + r["checks"]["layers"]["summary"]) 355: for item in r["checks"].get("props", []): 356: print(" " + {"FAIL": "! ", "real": "· "}.get(item["verdict"], "~ ") + item["text"]) 357: for item in r["checks"]["through"]: 358: print(" ! " + item["text"]) 359: prev = {c["name"]: c for c in old.get("characters", [])} 360: entries = [] 361: for n in LABELS: # keep the cast order; unbuilt characters as they were 362: if n in built: 363: r = built[n] 364: entries.append(dict(name=n, label=LABELS[n], info=r["info"], cams=r["cams"], 365: **({"group": CAST[n]["group"]} if CAST[n].get("group") else {}))) 366: elif n in prev: # as last built, with the cast's current label and group 367: e = {k: v for k, v in prev[n].items() if k != "group"} 368: e["label"] = LABELS[n] 369: if CAST[n].get("group"): 370: e["group"] = CAST[n]["group"] 371: entries.append(e) 372: cam_battle = entries[0]["cams"]["battle"] 373: duel_cam = dict(cam_battle, pivot_px=[cam_battle["w"] // 2, cam_battle["pivot_px"][1]]) 374: out = dict(characters=entries, duel=dict(order=ORDER, place=place, cam=duel_cam, clip="clash"), 375: lineup=dict(order=[] if ARMORY else [n for n in MOBS if any(e["name"] == n for e in entries)], spacing=0.95)) 376: if ws: # the page's title and notes; no duel view 377: out["page"] = dict(title=ws["title"], eyebrow=ws["eyebrow"], notes=open(ws["notes"]).read(), duel=False, 378: armory=bool(ws.get("armory"))) 379: if ws.get("armory"): # the skin picker: every tone of the range, with its ramp 380: import cast 381: out["page"]["skins"] = [dict(name=n, label=lab, group=grp, ramp=export_glb.skin_ramp(cast.SKINS[n])) 382: for grp, tones in SKIN_PICKER for n, lab in tones] 383: json.dump(out, open(cam_path, "w"), indent=1, default=as_list) 384: chk = dict(old_chk, **{n: r["checks"] for n, r in built.items()}) 385: json.dump(chk, open(chk_path, "w"), indent=1) 386: shutil.copy(os.path.join(HERE, "index.html"), OUT) 387: import regress # what changed since the last published version 388: for n in built: 389: for line in regress.report(n): 390: print(line) 391: print("->", OUT)
agent
+14:59reasoning
Initial observation: review/vroid-02/build.py and art/cast/cast.py seem well-structured. Examining art/cast/cast.py at lines 305-329, which appears to describe character data.
Initial suspicion was that the character data was well-structured, but inconsistencies emerged when comparing character definitions. The aldric definition uses an outdated model with bone-scale hacks, while elaine relies on a procedural hair generation. This suggests a potential lack of standardization in how characters are built. Further investigation of the arm_m_heroic definition is needed.
read_file{"EndLine":642,"Path":"~/work/lowmarch/tools/cage.py","StartLine":600}
input
{ "EndLine": 642, "Path": "~/work/lowmarch/tools/cage.py", "StartLine": 600 }result, 44 lines
Showing lines 600-642 of 888 600: o, sz = f.get("off", (0, 0, 0)), f["size"] 601: # a sided form is one per side; a centre form off the midline is a mirrored pair, on it a single one 602: for s, out in ((("L", -1.0), ("R", 1.0)) if sided or o[0] else (("C", 1.0),)): 603: side = s if sided else "C" 604: gate = _weights(md, names, tuple(x.format(s=side) for x in f["gate"])) 605: bone, t = f["at"] 606: full = "J_Bip_" + bone.format(s=side) # "{s}_UpperArm", "C_Spine" 607: b = _bone(full) 608: j0 = Jn[by_name[full]] 609: j1 = Jn[by_name[full[:-len(b)] + NEXT[b]]] if b in NEXT else j0 610: c = j0 + t * (j1 - j0) 611: c = np.array([c[0] if sided else 0.0, c[1], c[2]]) + np.array([out * o[0], o[1], -o[2]]) 612: d = np.array(f["face"], float) * np.array([out, 1.0, -1.0]) 613: d /= np.linalg.norm(d) 614: # the bump stands on the skin where a ray from the anchor along `face` leaves the body, so `h` is 615: # the height it adds there (centred on the joint, deep inside a big body, it added a third of it) 616: rel = P - c 617: along = rel @ d 618: perp = np.linalg.norm(rel - along[:, None] * d, axis=1) 619: ok = (along > 0) & (nv @ d > 0.3) & (gate > 0.2) 620: if not ok.any(): 621: continue 622: s0 = P[np.nonzero(ok)[0][np.argmin(perp[ok])]] 623: q = (P - s0) * np.array([out, 1.0, -1.0]) 624: r2 = (q[:, 0] / sz[0]) ** 2 + (q[:, 1] / sz[1]) ** 2 + (q[:, 2] / sz[2]) ** 2 625: amp += f["h"] * gate * np.exp(-0.5 * r2) * np.clip(nv @ d + 0.5, 0, 1) # never the far side 626: still = _weights(md, names, ("C_Head",)) > 0.02 627: # the neck's top, where the face mesh meets the body, stays exactly where the rig put it 628: neck = Jn[by_name["J_Bip_C_Neck"]] 629: head = Jn[by_name["J_Bip_C_Head"]] 630: fade = np.clip((head[1] - P[:, 1]) / max(head[1] - neck[1], 1e-6), 0, 1) 631: amp *= fade 632: aw = np.zeros(n) 633: np.add.at(aw, wid, amp) 634: aw /= np.bincount(wid, minlength=n) 635: st = np.zeros(n, bool) 636: np.logical_or.at(st, wid, still) 637: aw[st] = 0 638: aw = _smooth(aw, M, spec.get("smooth", 3), keep=st) 639: Gf = np.zeros((n, 6)) 640: np.add.at(Gf, wid, _groups([names[j] for j in md["joints"]], md["Ji"], md["Wt"])) 641: Gf /= np.bincount(wid, minlength=n)[:, None] 642: hold = st | shared | (Gf[:, 5] > 0.5)read_file{"EndLine":888,"Path":"~/work/lowmarch/tools/cage.py","StartLine":800}
input
{ "EndLine": 888, "Path": "~/work/lowmarch/tools/cage.py", "StartLine": 800 }result, 90 lines
Showing lines 800-888 of 888 800: open(tmp, "wb").write(data) 801: os.replace(tmp, path) 802: return len(data) 803: 804: 805: # ---- fitting (tools/fit.py's loop, with this builder's controls) 806: def _c(*paths): 807: return tuple(tuple(p.split(".")) for p in paths) 808: 809: 810: CONTROLS = { # ratio: [(spec paths moved together, bounds, first step)] 811: "crotch_H": [(_c("len.UpperLeg", "len.LowerLeg"), (-0.45, 0.4), 0.1)], 812: "heads": [(_c("head"), (-0.3, 0.6), 0.1)], 813: "neck_hd": [(_c("len.Neck"), (-0.6, 0.6), 0.15)], 814: "shoulder_hd": [(_c("len.Shoulder"), (-0.5, 3.0), 0.4)], 815: "hip_hd": [(_c("wide.Hips"), (-0.3, 1.2), 0.15)], 816: "waist_hd": [(_c("rings.torso.waist.w"), (-0.3, 1.5), 0.15)], 817: "arm_hd": [(_c("len.UpperArm", "len.LowerArm"), (-0.4, 0.5), 0.1)], 818: "hand_CE": [(_c("hand"), (-0.3, 0.6), 0.1)], 819: "foot_CE": [(_c("foot"), (-0.3, 0.6), 0.1)], 820: "cup_p": [(_c("bust_shape.inflate"), (0.0, 0.04), 0.008)], # VRoid's breasts inflated (tools/anthro.py's cup) 821: "thigh_p": [(_c("swell.UpperLeg"), (-0.01, 0.03), 0.004)], # mid-thigh (tools/anthro.py), metres along normals 822: } 823: 824: 825: def probe(spec, anthro=None): 826: import fit 827: os.makedirs(OUT, exist_ok=True) 828: p = os.path.join(OUT, f".probe_{os.getpid()}.vrm") 829: write(p, *build(spec)) 830: try: 831: return fit.measure(p, anthro) 832: finally: 833: os.remove(p) 834: 835: 836: def _key(spec, hair=True): 837: src = "".join(open(os.path.join(HERE, f)).read() for f in ("cage.py", "fit.py", "proportions.py", "makehuman.py", 838: "anthro.py", "heads.py", "vroid_portrait.py") 839: + (("collide.py",) if hair else ())) 840: ref = spec.get("ref") 841: if isinstance(ref, str): # a cast entry's reference: its spec too (a refitted reference left the body stale) 842: sys.path[:0] = [os.path.join(os.path.dirname(HERE), "art", "cast")] 843: from cast import CAST 844: src += json.dumps(CAST[ref]["mh"], sort_keys=True) 845: return hashlib.sha1((src + json.dumps(spec, sort_keys=True)).encode()).hexdigest()[:16] 846: 847: 848: def fitted(spec, log=None): 849: """spec with its proportion controls fitted to spec["fit"]'s archetype (cached).""" 850: if "fit" not in spec: 851: return spec 852: import fit 853: k = _key(spec, hair=False) # the hair's collision (collide.py) never moves the body 854: try: 855: cache = json.load(open(CACHE)) 856: except (OSError, ValueError): 857: cache = {} 858: if k not in cache: 859: s, _ = fit.fit(spec, log, probe=probe, controls=CONTROLS, head_step=False, rescale=False) 860: try: 861: cache = json.load(open(CACHE)) 862: except (OSError, ValueError): 863: cache = {} 864: cache[k] = s 865: os.makedirs(OUT, exist_ok=True) 866: tmp = CACHE + f".{os.getpid()}" 867: json.dump(cache, open(tmp, "w"), indent=1, sort_keys=True) 868: os.replace(tmp, CACHE) 869: return cache[k] 870: 871: 872: def path_for(name, spec): 873: return os.path.join(OUT, f"{name}_{_key(spec)}.vrm") 874: 875: 876: def ensure(name, spec, log=None): 877: p = path_for(name, spec) 878: if not os.path.exists(p): 879: write(p, *build(fitted(copy.deepcopy(spec), log))) 880: return p 881: 882: 883: if __name__ == "__main__": 884: sys.path[:0] = [os.path.join(os.path.dirname(HERE), "art", "cast")] 885: from cast import CAST 886: todo = sys.argv[1:] or [n for n, c in CAST.items() if c.get("cage")] 887: for name in todo: 888: print(name, ensure(name, CAST[name]["cage"], print))