Subagent: Two-handed longsword clips

agent (cont.)

+0:09
  • Bashcat tools/checks.py && grep -n "def sword" -A80 tools/outfit.py | head -120 && grep -n "def side_of" -A10 tools/outfit.py && ls tools/whole/notes && grep -n "clips\|stance\|ref_pose\|fist" review/vroid-02/build.py | head -60

    command

    cat tools/checks.py && grep -n "def sword" -A80 tools/outfit.py | head -120 && grep -n "def side_of" -A10 tools/outfit.py && ls tools/whole/notes && grep -n "clips\|stance\|ref_pose\|fist" review/vroid-02/build.py | head -60

    result, 404 lines

    """Automatic checks on a character's poses, measured rather than eyeballed.
    
    Clearance: does an arm pass through the torso? Each arm segment (upper arm, forearm, hand) is a
    capsule: its bone's centre line, with the limb's thickness measured from the body mesh. The torso is
    VRoid's skinned body mesh, posed by the same bone_pose the exporter uses; a point's signed distance
    is taken against the nearest torso vertex's normal. Clearance = that distance minus the limb's
    radius: negative means the limb's surface is inside the torso skin by that much.
    
    Measuring centre lines, not skin vertices, keeps the skinning's squash where a hanging arm meets the
    ribs from reading as a problem; the upper arm's first half is skipped (it joins the torso there, and
    a raised arm's shoulder end sits under the shoulder plate by design).
    
    Skin under a costume is culled, but plate stands off the body by a centimetre or two on both the
    torso and the arm, so "touching" starts a little outside the skin.
    
      b = Body(vrm, shape)
      b.clearance(pose) -> {limb: clearance in metres}
      report(name, {clip: [(t, pose)]}, b) -> (summary line, [problem lines], [touching lines])
    """
    import numpy as np
    from scipy.spatial import cKDTree
    
    import vroid_portrait as vp
    from vrm import bone_weight
    
    SEGMENTS = {f"{s}_{a}": (f"J_Bip_{s}_{a}", f"J_Bip_{s}_{b}", t0)
                for s in "RL" for a, b, t0 in (("UpperArm", "LowerArm", 0.5), ("LowerArm", "Hand", 0.0),
                                               ("Hand", "Middle3", 0.0))}
    THROUGH = -0.01        # the limb is more than this far inside the torso skin: it shows through the armour
    TOUCH = 0.015          # closer than this: armour on armour
    
    
    class Body:
        def __init__(self, vrm, shape=None):
            self.v = vrm
            self.shape = dict(vp.NEUTRAL, **(shape or {}))
            body = [p for p in vrm.primitives() if vp.role_of(p["material"]) == "body" and "J" in p]
            mesh_ix = {m.get("name"): i for i, m in enumerate(vrm.j["meshes"])}
            skin_of = {n.get("mesh"): n.get("skin") for n in vrm.j["nodes"] if "mesh" in n}
            self.parts = [(p, skin_of[mesh_ix[p["mesh"]]]) for p in body]
            off = np.cumsum([0] + [len(p["pos"]) for p in body])
            self.tri = np.concatenate([p["tri"] + off[k] for k, p in enumerate(body)])
            used = np.zeros(off[-1], bool)
            used[self.tri.ravel()] = True                     # VRoid meshes carry unused vertices (no normal)
            w = lambda m: np.concatenate([bone_weight(p, m) for p in body])
            self.torso = (w(("Spine", "Chest", "Bust", "Hips")) > 0.6) & used
            self.names = vrm.nodes_by_name()
            # each segment's radius: the median distance of its vertices from its centre line, unposed
            P, _, W = self.posed({})
            self.radius = {}
            for limb, (a, b, _) in SEGMENTS.items():
                m = (w((limb,)) > 0.6) & used
                A, B = W[self.names[a]][:3, 3], W[self.names[b]][:3, 3]
                self.radius[limb] = float(np.median(_seg_dist(P[m], A, B))) if m.any() else 0.03
    
        def posed(self, pose):
            """Body vertices, vertex normals and node world matrices in `pose` (model space)."""
            bp = vp.bone_pose(self.v, self.shape, pose)
            jm = self.v.skin_matrices(bp)
            P = []
            for p, s in self.parts:
                M = (jm[s][p["J"]] * p["W"][:, :, None, None]).sum(1)
                P.append(np.einsum("nij,nj->ni", M[:, :3, :3], p["pos"]) + M[:, :3, 3])
            P = np.concatenate(P)
            T = self.tri
            fn = np.cross(P[T[:, 1]] - P[T[:, 0]], P[T[:, 2]] - P[T[:, 0]])
            N = np.zeros_like(P)
            for j in range(3):
                np.add.at(N, T[:, j], fn)
            N /= np.linalg.norm(N, axis=1, keepdims=True) + 1e-12
            return P, N, self.v.node_world(bp)
    
        def clearance(self, pose, limbs=None):
            P, N, W = self.posed(pose)
            TP, TN = P[self.torso], N[self.torso]
            tree = cKDTree(TP)
            out = {}
            for limb, (a, b, t0) in SEGMENTS.items():
                if limbs is not None and limb not in limbs:
                    continue
                A, B = W[self.names[a]][:3, 3], W[self.names[b]][:3, 3]
                q = A + np.linspace(t0, 1, 12)[:, None] * (B - A)
                d, j = tree.query(q)
                s = np.einsum("ij,ij->i", q - TP[j], TN[j])
                # inside only when the point lies along the inward normal: a raised arm far above the
                # shoulder can have the downward-facing armpit skin as its nearest torso vertex (the
                # shoulder top belongs to the Shoulder bones, not the torso), which reads as "behind" it
                s = np.where((s < 0) & (-s < 0.5 * d), d, s)
                out[limb] = float(s.min()) - self.radius[limb]
            return out
    
        def worst(self, pose, limbs=None):
            """The smallest clearance over `limbs` (every arm segment by default), for a pose search."""
            return min(self.clearance(pose, limbs).values())
    
    
    def _seg_dist(X, A, B):
        d = B - A
        t = np.clip((X - A) @ d / (d @ d), 0, 1)
        return np.linalg.norm(X - (A + t[:, None] * d), axis=1)
    
    
    def _say(limb, c):
        side, seg = limb.split("_")
        name = {"UpperArm": "upper arm", "LowerArm": "forearm", "Hand": "hand"}[seg]
        where = f"{-c * 100:.1f} cm inside the body" if c < 0 else f"{c * 100:.1f} cm from the body"
        return f"{'right' if side == 'R' else 'left'} {name} {where}"
    
    
    def report(name, clips, body):
        """Keyframes where an arm goes through the torso (problems) or rests on it (touching): each a
        dict(clip, t, hits=[(limb, clearance)], text)."""
        bad, touch, n = [], [], 0
        for clip, keys in clips.items():
            for t, pose in keys:
                n += 1
                cl = body.clearance(pose)
                hits = sorted((c, k) for k, c in cl.items() if c < TOUCH)
                if not hits:
                    continue
                item = dict(clip=clip, t=round(t, 3), hits=[(k, round(c, 4)) for c, k in hits],
                            text=f"{name}, {clip} at {t:.2f} s: " + "; ".join(_say(k, c) for c, k in hits))
                (bad if hits[0][0] < THROUGH else touch).append(item)
        head = (f"{name}: {n} keyframes checked; " +
                (f"{len(bad)} with an arm through the body" if bad else "no arm through the body") +
                (f", {len(touch)} with an arm resting on it" if touch else ""))
        return head, bad, touch
    
    
    # ---- layers: an inner garment showing through the plate over it --------------------------------------
    INNER = ("dress", "doublet", "trousers")         # garment groups worn under plate (skirts and capes excluded)
    REACH = 0.03                                     # a garment point further than this from a plate isn't tested
    
    
    def _rays_hit(O, D, T, rmax, k=48):
        """For rays O + t D (M of them) against triangles T (N, 3, 3): the nearest hit distance, nan if none
        within rmax (Moller-Trumbore). Each ray is tested against the k triangles whose centres lie
        nearest its midpoint (a short ray only meets triangles near it)."""
        out = np.full(len(O), np.nan)
        if not len(O):
            return out
        kk = min(k, len(T))
        _, idx = cKDTree(T.mean(1)).query(O + D * rmax * 0.5, k=kk)
        idx = idx.reshape(len(O), kk)
        Tk = T[idx]                                          # (M, k, 3, 3)
        a, e1, e2 = Tk[:, :, 0], Tk[:, :, 1] - Tk[:, :, 0], Tk[:, :, 2] - Tk[:, :, 0]
        d = D[:, None]
        h = np.cross(d, e2)
        det = (e1 * h).sum(-1)
        ok = np.abs(det) > 1e-12
        inv = np.where(ok, 1 / np.where(ok, det, 1), 0)
        sv = O[:, None] - a
        u = (sv * h).sum(-1) * inv
        q = np.cross(sv, e1)
        v = (d * q).sum(-1) * inv
        t = (e2 * q).sum(-1) * inv
        hit = ok & (u >= 0) & (v >= 0) & (u + v <= 1) & (t > 1e-5) & (t < rmax)
        return np.where(hit.any(1), np.where(hit, t, np.inf).min(1), np.nan)
    
    
    EDGE = 0.006                                     # hits this close to a plate's outline are its rim, not its face
    
    
    def _part(name):
        """Which part of the body a bone belongs to: torso, an arm or a leg (by side)."""
        for side in "LR":
            if f"_{side}_" in name:
                if any(k in name for k in ("UpperLeg", "LowerLeg", "Foot", "ToeBase")):
                    return "leg" + side
                return "arm" + side
        return "torso"
    
    
    def _parts(pc):
        """Each vertex's body part, from its strongest bone."""
        names = np.array([_part(n) for n in pc["jnames"]])
        return names[pc["J"][np.arange(len(pc["J"])), np.argmax(pc["W"], 1)]]
    
    
    def _outline(pc):
        """Indices of a piece's outline vertices: open edges, plus creases sharper than 60 degrees (the rim
        of a plate with thickness, whose walls meet its faces there)."""
        P, T = pc["pos"], pc["tri"]
        _, rep = np.unique(np.round(P / 1e-5).astype(np.int64), axis=0, return_inverse=True)
        rep = rep.ravel()
        fn = np.cross(P[T[:, 1]] - P[T[:, 0]], P[T[:, 2]] - P[T[:, 0]])
        fn /= np.maximum(np.linalg.norm(fn, axis=1, keepdims=True), 1e-12)
        edges = {}
        for f, t in enumerate(T):
            for a, b in ((t[0], t[1]), (t[1], t[2]), (t[2], t[0])):
                edges.setdefault((min(rep[a], rep[b]), max(rep[a], rep[b])), []).append((f, a, b))
        out = set()
        for faces in edges.values():
            if len(faces) == 1 or any(fn[faces[0][0]] @ fn[g[0]] < 0.5 for g in faces[1:]):
                for _, a, b in faces:
                    out.update((a, b))
        return np.array(sorted(out), dtype=np.int64)
    
    
    class Layers:
        """Pieces as built (reference pose), posed through the same skeleton: the garments (wrapped
        pieces in INNER groups; trims, tubes and collars are left out) against every plate (group
        "plate", not rolls, rivets or ridges), each garment point only against plates on the same part
        of the body (a sleeve hanging beside a breastplate is meant to be outside it). A garment point
        shows through when a ray from it inward, along its own normal, meets the plate's face within
        REACH (not its rim): the plate is then under it. (Distance from the body can't tell: where a
        plate bridges a hollow, the cloth in the hollow is further from the nearest body point.)
        A piece can also name what covers it, cloth or plate: pc["covered_by"] = [names] (an underskirt
        under an overskirt) adds those pairs."""
    
        def __init__(self, portrait, body):
            self.v, self.body = portrait.v, body
            self.shape = body.shape
            self.ref = portrait.pose_ref
            pcs = [p for p in portrait.prims if p.get("material", "").startswith("OUTFIT_") and "J" in p]
            inner = [p for p in pcs if p.get("group") in INNER and "loops" in p]
            plates = [p for p in pcs if p.get("group") == "plate"
                      and not any(k in p["material"] for k in ("roll", "rivet", "ridge"))]
            by_name = {p["material"].replace("OUTFIT_", ""): p for p in pcs}
            pairs = [(g, o) for g in inner for o in plates]
            pairs += [(g, by_name[n]) for g in pcs for n in g.get("covered_by", ()) if n in by_name]
            parts, edges, part_of = {}, {}, {}
            for g, o in pairs:
                parts.setdefault(id(g), _parts(g))
                if id(o) not in edges:
                    k, c = np.unique(_parts(o), return_counts=True)
                    part_of[id(o)], edges[id(o)] = k[np.argmax(c)], _outline(o)
            self.pairs = [(g, parts[id(g)], o, part_of[id(o)], edges[id(o)]) for g, o in pairs]
            self.names = self.v.nodes_by_name()
    
        def _skin(self, pose):
            """Per joint name: pose world @ inverse reference world (pieces are built in the reference pose)."""
            W = self.v.node_world(vp.bone_pose(self.v, self.shape, pose))
            W0 = self.v.node_world(self.ref)
            return lambda jnames: np.stack([W[self.names[n]] @ np.linalg.inv(W0[self.names[n]]) if n in self.names
                                            else np.eye(4) for n in jnames])
    
        def check(self, pose=None):
            """[(garment, plate, points showing, how far past the plate in metres)] for one pose (None:
            the reference pose)."""
            skin = self._skin(pose) if pose is not None else None
            cache = {}
    
            def get(pc):
                if skin is None:
                    return pc["pos"], pc["nrm"]
                M = cache.setdefault(id(pc["jnames"]), skin(pc["jnames"]))
                X = (M[pc["J"]] * pc["W"][:, :, None, None]).sum(1)
                P = np.einsum("nij,nj->ni", X[:, :3, :3], pc["pos"]) + X[:, :3, 3]
                N = np.einsum("nij,nj->ni", X[:, :3, :3], pc["nrm"])
                return P, N / np.maximum(np.linalg.norm(N, axis=1, keepdims=True), 1e-12)
    
            outer, inner, out = {}, {}, []
            for pc, parts, plate, part, edge in self.pairs:
                if id(plate) not in outer:
                    Q, _ = get(plate)
                    outer[id(plate)] = (Q, Q[plate["tri"]], cKDTree(Q[edge]) if len(edge) else None)
                if id(pc) not in inner:
                    inner[id(pc)] = get(pc)
                Q, T, rim = outer[id(plate)]
                X, N = inner[id(pc)]
                lo, hi = Q.min(0) - REACH, Q.max(0) + REACH
                near = np.all((X > lo) & (X < hi), axis=1) & (parts == part)
                if not near.any():
                    continue
                t = _rays_hit(X[near], -N[near], T, REACH)
                show = np.isfinite(t)
                if rim is not None and show.any():
                    at = X[near][show] - N[near][show] * t[show][:, None]
                    keep = rim.query(at)[0] > EDGE
                    idx = np.nonzero(show)[0]
                    show[idx[~keep]] = False
                if show.sum() >= 5:                          # (how deep: of the points counted, not the rim's)
                    out.append((pc["material"].replace("OUTFIT_", ""), plate["material"].replace("OUTFIT_", ""),
                                int(show.sum()), float(np.nanmax(t[show]))))
            return out
    
    
    def layer_report(name, clips, layers):
        """The reference pose (a modelling problem), then each keyframe for what is new in motion: a
        garment and plate pair not already showing in the reference pose, or showing 5 mm further.
        dict(summary, items=[dict(clip, t, hits, text)])."""
        items = []
        ref = {(g, p): d for g, p, _, d in layers.check(None)}
        if ref:
            hits = sorted(((g, p, n, d) for g, p, n, d in layers.check(None)), key=lambda h: -h[3])
            items.append(dict(clip="reference pose", t=0.0, hits=hits, text=f"{name}, reference pose: " + "; ".join(
                f"{g} through {p} (up to {d * 1000:.0f} mm)" for g, p, n, d in hits[:4])))
        for clip, keys in clips.items():
            for t, pose in keys:
                hits = [h for h in layers.check(pose) if h[3] > ref.get((h[0], h[1]), -1.0) + 0.005]
                if hits:
                    hits.sort(key=lambda h: -h[3])
                    items.append(dict(clip=clip, t=round(t, 3), hits=hits, text=f"{name}, {clip} at {t:.2f} s: " + "; ".join(
                        f"{g} through {p} ({n} points, up to {d * 1000:.0f} mm)" for g, p, n, d in hits[:3])))
        moving = [it for it in items if it["clip"] != "reference pose"]
        n = sum(len(k) for k in clips.values())
        summary = (f"{name}: plate over garments: " + ("shows through in the reference pose, " if ref else "clean as built, ")
                   + (f"and {len(moving)} of {n} keyframes add to it" if moving else f"nothing new in {n} keyframes"))
        return dict(summary=summary, items=items)
    517:def sword(fit, hand="R", blade_len=0.80, blade_w=0.042, grip_len=0.15, hilt="gold", grip="leather"):
    518-    """A one-handed sword held in the fist. In the hand's frame the fingers run along +x (right
    519-    hand), the palm faces -y and the fist's hole runs front-back (z): the blade leaves the thumb
    520-    side, pointing forward. Rigid on the hand bone."""
    521-    M = fit.world[f"J_Bip_{hand}_Hand"]
    522-    R, o = M[:3, :3], M[:3, 3]
    523-    R = R / np.linalg.norm(R, axis=0)               # the hand bone may be scaled (body proportions)
    524-    sx = 1 if hand == "R" else -1
    525-    fist = o + R @ np.array([sx * 0.045, -0.018, 0.0])
    526-    fwd = R @ np.array([0, 0, -1.0])
    527-    side = R @ np.array([sx * 1.0, 0, 0])
    528-    flat = np.cross(fwd, side)
    529-    out = [tube("grip", grip, fist - fwd * grip_len * 0.55, fwd, grip_len, 0.0135),
    530-           ball("pommel", hilt, fist - fwd * (grip_len * 0.55 + 0.012), 0.02)]
    531-    guard_c = fist + fwd * (grip_len * 0.45 + 0.008)
    532-    out.append(box("guard", hilt, guard_c, (side, flat, fwd), (0.095, 0.014, 0.012)))
    533-    base = guard_c + fwd * 0.012
    534-    rows = 12
    535-    P = np.zeros((5, rows, 3))
    536-    for j, t in enumerate(np.linspace(0, 1, rows)):
    537-        taper = 1.0 if t < 0.82 else max(0.0, (1 - t) / 0.18)
    538-        w = blade_w / 2 * (1 - 0.15 * t) * taper
    539-        th = 0.0045 * taper
    540-        c = base + fwd * blade_len * t
    541-        P[:, j] = [c + side * w, c + flat * th, c - side * w, c - flat * th, c + side * w]
    542-    pos, tri = grid_mesh(P)
    543-    out.append(piece("blade", "steel", pos, tri, outward_from=base + fwd * blade_len / 2, axis=fwd))
    544-    out.append(ribbon("fuller", "metal", [base + fwd * blade_len * t + flat * 0.0047 for t in np.linspace(0.04, 0.7, 10)],
    545-                      width=blade_w * 0.18, lift=0.0005, normals=np.tile(flat, (10, 1))))
    546-    for pc in out:
    547-        fit.bind(pc, rigid=f"J_Bip_{hand}_Hand")
    548-    return out
    549-
    550-
    551-# ---- plate kit (Unicorn Overlord study, docs/study.md): lames, shells, caps with fan wings ------
    552-def pipe(name, role, curve, r=0.0026, seg=6, closed=False):
    553-    """A thin tube along a polyline: the rolled edge of a plate, piping, a cord."""
    554-    C = np.asarray(curve, float)
    555-    if closed:
    556-        C = np.concatenate([C, C[:1]])
    557-    t = np.gradient(C, axis=0)
    558-    t /= np.maximum(np.linalg.norm(t, axis=1, keepdims=True), 1e-9)
    559-    t0 = t[np.argmax(np.linalg.norm(t, axis=1) > 0.5)]          # first non-degenerate tangent
    560-    n = np.cross(t0, [0, 1, 0])
    561-    if np.linalg.norm(n) < 1e-6:
    562-        n = np.cross(t0, [1, 0, 0])
    563-    n /= max(np.linalg.norm(n), 1e-9)
    564-    N = np.zeros_like(C)
    565-    for i in range(len(C)):                      # parallel transport: the tube doesn't twist
    566-        n = n - t[i] * (n @ t[i])
    567-        n /= max(np.linalg.norm(n), 1e-9)
    568-        N[i] = n
    569-    B = np.cross(t, N)
    570-    ang = np.linspace(0, 2 * math.pi, seg + 1)
    571-    P = C[:, None] + r * (np.cos(ang)[None, :, None] * N[:, None] + np.sin(ang)[None, :, None] * B[:, None])
    572-    pos, tri = grid_mesh(P)
    573-    nrm = ((P - C[:, None]) / r).reshape(-1, 3)
    574-    if (vertex_normals(pos, tri) * nrm).sum(1).mean() < 0:
    575-        tri = tri[:, ::-1]
    576-    return piece(name, role, pos, tri, nrm=nrm)
    577-
    578-
    579-def limb_surface(fit, bones, side=None):
    580-    """The skin of one limb (bones matched by name), optionally only one side of the body, so ray
    581-    casts out of a bone can't reach the torso or the other limb."""
    582-    T = fit.tris_for(bones, min_w=0.35)
    583-    if side:
    584-        cx = fit.pos[T][:, :, 0].mean(1)
    585-        T = T[cx < 0.01] if side == "L" else T[cx > -0.01]
    586-    return Surface(fit.pos, T)
    587-
    588-
    589-def side_of(pc, side):
    590-    """A piece's triangles on one side of the body (the character's left is -x)."""
    591-    cx = pc["pos"][pc["tri"]][:, :, 0].mean(1)
    592-    return Surface(pc["pos"], pc["tri"][cx < 0.0] if side == "L" else pc["tri"][cx > 0.0])
    593-
    594-
    595-def limb_lames(fit, name, role, over, top, bottom, lames, arc=(-180, 180), front=(0, 0, -1), thickness=0.0035,
    596-               seg=30, rows=5, rmax=0.13):
    597-    """Bands of plate around a limb: each lame covers fractions (s0, s1) of the segment top->bottom
    589:def side_of(pc, side):
    590-    """A piece's triangles on one side of the body (the character's left is -x)."""
    591-    cx = pc["pos"][pc["tri"]][:, :, 0].mean(1)
    592-    return Surface(pc["pos"], pc["tri"][cx < 0.0] if side == "L" else pc["tri"][cx > 0.0])
    593-
    594-
    595-def limb_lames(fit, name, role, over, top, bottom, lames, arc=(-180, 180), front=(0, 0, -1), thickness=0.0035,
    596-               seg=30, rows=5, rmax=0.13):
    597-    """Bands of plate around a limb: each lame covers fractions (s0, s1) of the segment top->bottom
    598-    and sits off `over` (ray cast out of the bone axis) by an offset that grows toward its lower
    599-    rim. Lames are stacked like roof tiles: the upper one's rim sits outside the next one's top.
    7:  <name>.glb.json     each character, skinned, in the reference (bust) pose, with animation clips:
    40:import clips  # noqa: E402
    54:    # the armory: bodies to assemble characters from, T-posed and bare (no clips, no checks): VRoid's base shaped by
    99:        style, length = (c["beard"], 0.0) if isinstance(c["beard"], str) else c["beard"]
    189:    """The rigged character (reference pose) with its clips, its cameras, the live renderer's data and
    199:        cl = clips.clips(p.v, c["shape"], c.get("stance", "sword"))
    204:    size = export_glb.export_skinned(p, pal, path, clips=cl)
    236:    if isinstance(ref, str):

    4 more lines not shown.

  • Bashsed -n 180,260p review/vroid-02/build.py && grep -n "pose_ref\|ref_pose" -r tools/*.py review/vroid-02/*.py | head -30 && ls tools/whole/notes && cat tools/whole/notes/* | head -80

    command

    sed -n 180,260p review/vroid-02/build.py && grep -n "pose_ref\|ref_pose" -r tools/*.py review/vroid-02/*.py | head -30 && ls tools/whole/notes && cat tools/whole/notes/* | head -80

    result, 104 lines

        return out
    
    
    def wrap_json(path):
        json.dump({"glb": base64.b64encode(open(path, "rb").read()).decode()}, open(path + ".json", "w"))
        os.remove(path)
    
    
    def character(args):
        """The rigged character (reference pose) with its clips, its cameras, the live renderer's data and
        its checks."""
        name, clash = args
        p, c = portrait(name, "bust")
        p.paint = c.get("paint")
        pal = dict(vp.PALETTE, **c["colors"])
        cam_bust, cam_battle = p.frame("bust"), p.frame("battle")
        if ARMORY:                                          # nothing moves: every clip holds the T-pose
            cl = {k: [(0.0, {}), (1.0, {})] for k in ("idle", "attack", "apose")}
        else:
            cl = clips.clips(p.v, c["shape"], c.get("stance", "sword"))
            if clash:
                cl["clash"] = clash
            cl["apose"] = [(0.0, APOSE), (1.0, APOSE)]
        path = f"{OUT}/{name}.glb"
        size = export_glb.export_skinned(p, pal, path, clips=cl)
        wrap_json(path)
        save_geometry(name, p)
        if ARMORY:                                          # the armory's checks are proportions (tools/proportions.py)
            return dict(name=name, size=size, cams=dict(bust=cam_bust, battle=cam_battle),
                        info=dict(export_glb.scene_info(p, pal), springs=springs(p.v)), checks=proportion_checks(name, c))
        body = checks.Body(p.v, c["shape"])
        keyed = {k: v for k, v in cl.items() if k != "apose"}
        head, bad, touch = checks.report(name, keyed, body)
        layers = checks.layer_report(name, keyed, checks.Layers(p, body))
        return dict(name=name, size=size, cams=dict(bust=cam_bust, battle=cam_battle), info=export_glb.scene_info(p, pal),
                    checks=dict(summary=head, through=bad, touching=touch, layers=layers))
    
    
    def save_geometry(name, p):
        """Every piece's vertices as built (reference pose), for tools/regress.py to compare builds."""
        os.makedirs(GEOM, exist_ok=True)
        arrays, seen = {}, {}
        for q in p.prims:
            key = q["material"]
            seen[key] = seen.get(key, 0) + 1
            if seen[key] > 1:
                key = f"{key}#{seen[key]}"
            arrays[key] = q["pos"][np.unique(q["tri"])].astype(np.float32) if len(q["tri"]) else q["pos"].astype(np.float32)
        np.savez_compressed(os.path.join(GEOM, f"{name}.npz"), **arrays)
    
    
    def _fit_of(c):
        """The fit a body was made to (its own, or its cage reference's): the ratios it skips (the heavy targets'
        widths were measured on armour), ranges of its own, its figure."""
        spec = c.get("mh") or {}
        ref = (c.get("cage") or {}).get("ref")
        if isinstance(ref, str):
            spec = CAST[ref].get("mh", {})
        elif isinstance(ref, (list, tuple)):
            spec = ref[1]
        return spec.get("fit", {})
    
    
    def _nth(p):
        n = int(round(p))
        return f"{n}{'th' if 10 <= n % 100 <= 20 else {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')}"
    
    
    def proportion_checks(name, c):
        """The model file against its archetype's targets (docs/proportions.md): a summary, and a line per ratio
        outside its range (fails first); then its girths against real people of its height and waist
        (tools/anthro.py), a line for each past the 5th or 95th percentile."""
        import anthro
        import proportions as PR
        path = model_path(name, c)
        m = PR.measure(path)
        arch = c.get("archetype") or PR.best_archetype(m, c.get("sex"))
        t = PR.targets(arch, c.get("sex"))
        want = _fit_of(c)
        t.update({k: tuple(r) for k, r in want.get("ranges", {}).items()})
        t.update({k: tuple(r) for k, r in c.get("ranges", {}).items()})       # the body's own (cast.py CHECK_RANGES)
    tools/checks.py:214:        self.ref = portrait.pose_ref
    tools/clips.py:105:    ref = dict(ref_pose=P.BUST, fist_R=f["R"], fist_L=f["L"])
    tools/rig.py:114:    def held(self, pose, bone, ref_pose, point):
    tools/rig.py:117:        M = self.world(pose)[i] @ np.linalg.inv(self.world(ref_pose)[i])
    tools/rig.py:120:    def grip(self, pose, side, ref_pose, fist_ref, fist_target, blade_dir, pole, local_blade=(0, 0, -1), iters=14):
    tools/rig.py:129:        wrist = T - (self.held(pose, hd, ref_pose, fist_ref) - self.pos(pose, hd))
    tools/rig.py:137:                f = self.held(pose, hd, ref_pose, fist_ref)
    tools/rig.py:140:            miss = np.linalg.norm(T - self.held(pose, hd, ref_pose, fist_ref))
    tools/rig.py:151:def grip_points(vrm, shape, ref_pose, fingers=("Index", "Middle", "Ring", "Little")):
    tools/rig.py:153:    space in `ref_pose` (the hand doesn't move when the fingers curl, so a weapon built there and bound
    tools/rig.py:168:        W = vrm.node_world(vp.bone_pose(vrm, sh, P.merge(ref_pose, P.fist(side))))
    tools/export_glb.py:402:    ref = portrait.pose_ref
    tools/duel.py:14:  duel_clips({name: (vrm, shape, ref_pose, sword_info)}, cast) -> {name: [(t, pose)...]}, placements
    tools/duel.py:102:        p = rig.grip(pose, side, c["ref_pose"], c[f"fist_{side}"], fist_m, blade_m, pole=pole)
    tools/duel.py:112:    """chars: {name: dict(vrm, shape, ref_pose, fist_R, fist_L)} (fists: reference-pose model points on
    tools/duel.py:135:                pose = rig.grip(pose, "R", c["ref_pose"], c["fist_R"], fist_m, blade_m, pole=(0.7, -1.0, 0.35))
    review/vroid-02/duel_check.py:35:        out[n] = dict(vrm=v, shape=c["shape"], ref_pose=P.BUST, fist_R=f["R"], fist_L=f["L"])
    tools/vroid_portrait.py:907:                 mouth="curve", world=None, ref_pose=BUST_POSE, hair_trim=None):
    tools/vroid_portrait.py:921:        pose = bone_pose(self.v, sh, ref_pose)
    tools/vroid_portrait.py:939:        self.pose_ref = pose
    tools/vroid_portrait.py:959:            hp = hv.primitives(None, ref_pose)
    tools/vroid_portrait.py:990:            fit.grip = rig.grip_points(self.v, sh, ref_pose)  # where each fist closes (weapons sit there)
    cat: 'tools/whole/notes/*': No such file or directory
  • Bashgrep -n "fit.grip\|def sword\|sword(fit" tools/*.py | head; grep -n "BUST_POSE =" -A3 tools/vroid_portrait.py; ls -la tools/whole/notes; git log --oneline -3 -- tools/whole/

    command

    grep -n "fit.grip\|def sword\|sword(fit" tools/*.py | head; grep -n "BUST_POSE =" -A3 tools/vroid_portrait.py; ls -la tools/whole/notes; git log --oneline -3 -- tools/whole/

    result, 12 lines

    tools/mobs.py:165:    out += O.sword(fit, "R", blade_len=0.70, hilt="metal")
    tools/vroid_portrait.py:990:            fit.grip = rig.grip_points(self.v, sh, ref_pose)  # where each fist closes (weapons sit there)
    tools/outfit.py:517:def sword(fit, hand="R", blade_len=0.80, blade_w=0.042, grip_len=0.15, hilt="gold", grip="leather"):
    tools/outfit.py:1029:    out += sword(fit, "R", blade_len=0.84, blade_w=0.046, grip_len=0.21, hilt="gold", grip="signature")
    tools/outfit.py:1106:    out += sword(fit, "R")
    tools/outfit.py:1263:    out += sword(fit, "R", hilt="metal")
    total 8
    drwx------ 2 severian severian 4096 Nov 10 19:08 .
    drwx------ 4 severian severian 4096 Nov 10 19:08 ..
    c43c63d Whole frame: a stance hook for two-handed clips (tools/whole/clips2h.py)
    ede247d Whole frame: default placement faces the two fighters at each other
    34a972d Whole frame: dress an armory body with the old kit, shoot one composed battle frame
  • Bashsed -n 1,60p review/vroid-02/build.py && grep -n "def portrait" -A30 review/vroid-02/build.py && grep -n "def model_path" -A12 review/vroid-02/build.py && grep -n "^BUST_POSE\|BUST_POSE =" tools/vroid_portrait.py

    command

    sed -n 1,60p review/vroid-02/build.py && grep -n "def portrait" -A30 review/vroid-02/build.py && grep -n "def model_path" -A12 review/vroid-02/build.py && grep -n "^BUST_POSE\|BUST_POSE =" tools/vroid_portrait.py

    result, 104 lines

    """Build the model-viewer review page for the cast on stage, and their duel.
    
      python3 review/vroid-02/build.py              every character, the ground and the page
      python3 review/vroid-02/build.py elaine       just Elaine (the others stay as last built)
    
    Writes to review/out/vroid-02/site/:
      <name>.glb.json     each character, skinned, in the reference (bust) pose, with animation clips:
                          idle, attack, clash (the two-character sword bind) and apose (arms out, for
                          checking a costume where hanging arms would hide it). Artifact hosting serves
                          .json but not .glb, so the model is base64-wrapped.
      plains.glb.json     the battle ground (static)
      camera.json         per character: the cameras for both views plus what the live renderer needs
                          (palette roles, lights, colliders, cloth grids); the duel's stage camera and
                          where each fighter stands
      checks.json         per character: the automatic checks (tools/checks.py) over every keyframe: arms
                          through the body, and garments showing through plate
    and review/out/vroid-02/geom/<name>.npz (every piece as built), which tools/regress.py compares with the
    last published version (snapshot.py) after each build.
    then copies index.html next to them. The duel's poses are cached (they take about a minute to
    solve) and re-solved when the rig, the duel or the cast entries change.
    """
    import base64
    import hashlib
    import json
    import os
    import pickle
    import shutil
    import sys
    from concurrent.futures import ProcessPoolExecutor
    
    import numpy as np
    
    HERE = os.path.dirname(os.path.abspath(__file__))
    ROOT = os.path.dirname(os.path.dirname(HERE))
    sys.path[:0] = [os.path.join(ROOT, "tools"), os.path.join(ROOT, "art", "cast"), HERE]
    import vroid_portrait as vp  # noqa: E402
    import poses as P  # noqa: E402
    import terrain  # noqa: E402
    import export_glb  # noqa: E402
    import clips  # noqa: E402
    import duel  # noqa: E402
    import checks  # noqa: E402
    from duel_check import chars_for  # noqa: E402
    from cast import CAST  # noqa: E402
    
    VROID = os.environ.get("LOWMARCH_VROID", os.path.expanduser("[redacted: private path]/vroid"))
    # which review page: the cast viewer (vroid-02), or a workshop page on a smaller roster (its own artifact)
    SITE = os.environ.get("LOWMARCH_SITE", "vroid-02")
    WORKSHOPS = {
        "mh-workshop": dict(
            title="Body workshop", eyebrow="Lowmarch · MakeHuman bodies, VRoid heads",
            roster=["mob_mh_youth", "mob_mh_veteran", "mob_mh_brute", "mob_mh_f_youth", "mob_mh_f_veteran", "mob_mh_f_strong"],
            notes=os.path.join(HERE, "mh_workshop_notes.html")),
        # the armory: bodies to assemble characters from, T-posed and bare (no clips, no checks): VRoid's base shaped by
        # MakeHuman references (art/cast/cast.py ARMORY), which are built but never shown
        "armory": dict(
            title="Armory", eyebrow="Lowmarch · VRoid bodies shaped by MakeHuman, T-posed for inspection",
            roster=[n for n in CAST if n.startswith("arm_")], armory=True,
            notes=os.path.join(HERE, "armory_notes.html")),
        "base-spike": dict(
    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)
    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"])
  • Bashgrep -n "def arm_room" -A25 review/vroid-02/build.py | head -40 && grep -n "class Portrait" -A5 tools/vroid_portrait.py && grep -n "self.v = " tools/vroid_portrait.py | head

    command

    grep -n "def arm_room" -A25 review/vroid-02/build.py | head -40 && grep -n "class Portrait" -A5 tools/vroid_portrait.py && grep -n "self.v = " tools/vroid_portrait.py | head

    result, 33 lines

    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)."""
    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
    905:class Portrait:
    906-    def __init__(self, path, morphs=None, pose=BUST_POSE, shape=None, hair_from=None, costume=None,
    907-                 mouth="curve", world=None, ref_pose=BUST_POSE, hair_trim=None):
    908-        """mouth: how the closed mouth is drawn. 'texture' = VRoid's painted lip line, thickened;
    909-        'texture+lip' = that plus a lower-lip shade; 'drawn' = an FE8-style mouth between the
    910-        projected mouth corners; 'none' = geometry only (the line disappears when closed)."""
    912:        self.v = VRM(path)
  • Bashls review/out/ | head -30; ls review/out/whole 2>/dev/null; find review/out -maxdepth 3 -name "*.png" -newer tools/whole/frame.json 2>/dev/null | head

    command

    ls review/out/ | head -30; ls review/out/whole 2>/dev/null; find review/out -maxdepth 3 -name "*.png" -newer tools/whole/frame.json 2>/dev/null | head

    result, 5 lines

    vroid-02
    whole
    whole-clips
    whole-lens
    site
  • Bashmkdir -p review/out/whole-clips/shots && python3 - <<'EOF'error

    command

    mkdir -p review/out/whole-clips/shots && python3 - <<'EOF'
    import json
    s=json.load(open('tools/whole/frame.json'))
    s.update(w=960,h=540,scale=1)
    s['clips']={"wren":["idle",0.0],"mob_soldier":["idle",0.0]}
    json.dump(s,open('review/out/whole-clips/shots/base.json','w'),indent=1)
    EOF
    time WHOLE_SITE=review/out/whole-clips/site python3 tools/whole/shoot.py review/out/whole-clips/shots/base.json review/out/whole-clips/shots/base.png

    result, error, 6 lines

    Exit code 1
    FAILED 
    
    real	0m2.940s
    user	0m1.087s
    sys	0m0.890s
  • Bashls -la /snap/bin/chromium; /snap/bin/chromium --headless=new --dump-dom about:blank 2>&1 | tail -5

    command

    ls -la /snap/bin/chromium; /snap/bin/chromium --headless=new --dump-dom about:blank 2>&1 | tail -5

    result, 6 lines

    lrwxrwxrwx 1 root root 13 Nov  9 11:57 /snap/bin/chromium -> /usr/bin/snap
    [71580:71750:1110/191054.896065:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [71580:71580:1110/191054.902566:ERROR:dbus/object_proxy.cc:572] Failed to call method: org.freedesktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type: 
    [71580:71750:1110/191054.902679:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [71580:71580:1110/191054.911684:ERROR:dbus/object_proxy.cc:572] Failed to call method: org.freedesktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type: 
    <html><head></head><body></body></html>
  • Bashpython3 - <<'EOF' 2>&1 | tail -30

    command

    python3 - <<'EOF' 2>&1 | tail -30
    import sys,json,re,subprocess,os,time,socket,urllib.parse
    sys.argv=['x']
    sys.path.insert(0,'tools/whole')
    os.environ['WHOLE_SITE']='review/out/whole-clips/site'
    import importlib.util
    spec=importlib.util.spec_from_file_location('shoot','tools/whole/shoot.py'); sh=importlib.util.module_from_spec(spec); spec.loader.exec_module(sh)
    orig=subprocess.run
    def run(*a,**k):
        r=orig(*a,**k)
        m=re.search(r'id="sheetOut"[^>]*>(.{0,300})',r.stdout,re.S)
        print('SHEETOUT', m.group(0)[:400] if m else None)
        print('STDERR', r.stderr[-1500:])
        print('LEN', len(r.stdout))
        return r
    subprocess.run=run
    try:
        sh.shoot(json.load(open('review/out/whole-clips/shots/base.json')),'review/out/whole-clips/shots/base.png')
    except SystemExit as e: print('EXIT',e)
    EOF

    result, 20 lines

    SHEETOUT id="sheetOut" aria-hidden="true"></textarea>
    
    <script type="importmap">
    { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
                   "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/" } }
    </script>
    <script type="module">
    import * as THREE from "three";
    import { 
    STDERR esktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type: 
    [72349:72525:1110/191104.260985:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [72349:72349:1110/191104.273426:ERROR:dbus/object_proxy.cc:572] Failed to call method: org.freedesktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type: 
    [72349:72525:1110/191104.273627:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [72349:72349:1110/191104.275181:ERROR:dbus/object_proxy.cc:572] Failed to call method: org.freedesktop.DBus.Properties.GetAll: object_path= /org/freedesktop/UPower/devices/DisplayDevice: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files
    [72349:72349:1110/191104.290245:ERROR:dbus/object_proxy.cc:572] Failed to call method: org.freedesktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type: 
    [72349:72525:1110/191104.290566:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [72349:72349:1110/191104.294854:ERROR:dbus/object_proxy.cc:572] Failed to call method: org.freedesktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type: 
    
    LEN 112509
    EXIT FAILED
  • Bashcurl -sI https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js | head -3; echo rc=$?

    command

    curl -sI https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js | head -3; echo rc=$?

    result, 4 lines

    HTTP/2 200 
    date: Tue, 10 Nov 2026 11:11:11 GMT
    content-type: application/javascript; charset=utf-8
    rc=0
  • Bashtime WHOLE_SITE=$PWD/review/out/whole-clips/site python3 tools/whole/shoot.py review/out/whole-clips/shots/base.json review/out/whole-clips/shots/base.png

    command

    time WHOLE_SITE=$PWD/review/out/whole-clips/site python3 tools/whole/shoot.py review/out/whole-clips/shots/base.json review/out/whole-clips/shots/base.png

    result, 5 lines

    review/out/whole-clips/shots/base.png 6s
    
    real	0m6.427s
    user	0m1.174s
    sys	0m1.283s
  • Read~/Projects/lowmarch-fable52/review/out/whole-clips/shots/base.png

    input

    {
      "file_path": "~/Projects/lowmarch-fable52/review/out/whole-clips/shots/base.png"
    }

    result

    no output

    base.png
    base.png
  • Bashgrep -n "clips\|interpolation\|STEP\|CUBIC" tools/export_glb.py | head -30

    command

    grep -n "clips\|interpolation\|STEP\|CUBIC" tools/export_glb.py | head -30

    result, 6 lines

    273:# ---- skinned export: one rigged character + animation clips --------------------------------------------------------
    380:def export_skinned(portrait, palette, path, clips=None, max_tex=512):
    383:    clips: {name: [(time, pose dict), ...]} -> glTF animations (rotation, plus translation where a
    528:    for name, keys in (clips or {}).items():
    545:                samplers.append(dict(input=tacc, output=add_acc(Q, "VEC4", 5126), interpolation="LINEAR"))
    548:                samplers.append(dict(input=tacc, output=add_acc(T, "VEC3", 5126), interpolation="LINEAR"))
  • Bashsed -n 520,560p tools/export_glb.py

    command

    sed -n 520,560p tools/export_glb.py

    result, 41 lines

                mat["extras"] = dict(decal=True)
            materials.append(mat)
            meshes.append(dict(name=group_of(p, role), primitives=[dict(attributes=attrs,
                         indices=add_acc(inv.reshape(-1).astype(np.uint32), "SCALAR", 5125, 34963), material=len(materials) - 1)]))
            mesh_nodes.append(len(gnodes))
            gnodes.append(dict(name=f"{group_of(p, role)}/{p['material']}", mesh=len(meshes) - 1, skin=0,
                               extras=dict(group=group_of(p, role), role=role, material=p["material"])))
        # animations
        for name, keys in (clips or {}).items():
            times = np.array([t for t, _ in keys], np.float32)
            tacc = add_acc(times.reshape(-1, 1), "SCALAR", 5126, minmax=True)
            samplers, channels = [], []
            for i in order:
                mats = [F4 @ v.local_matrix(i, vp.bone_pose(v, portrait.shape, pose)) @ F4 for _, pose in keys]
                trs = [_trs(m) for m in mats]
                T = np.array([a for a, _, _ in trs], np.float32)
                Q = np.array([b for _, b, _ in trs], np.float32)
                for k in range(1, len(Q)):                      # keep quaternions on one hemisphere
                    if np.dot(Q[k], Q[k - 1]) < 0:
                        Q[k] = -Q[k]
                t0, q0, _ = _trs(local_ref[i])
                if np.dot(Q[0], q0) < 0:
                    q0 = -q0
                # a channel whenever the clip leaves the reference pose, even if it then holds still
                if np.abs(Q - q0).max() > 1e-5:
                    samplers.append(dict(input=tacc, output=add_acc(Q, "VEC4", 5126), interpolation="LINEAR"))
                    channels.append(dict(sampler=len(samplers) - 1, target=dict(node=node_of[i], path="rotation")))
                if np.abs(T - t0).max() > 1e-5:
                    samplers.append(dict(input=tacc, output=add_acc(T, "VEC3", 5126), interpolation="LINEAR"))
                    channels.append(dict(sampler=len(samplers) - 1, target=dict(node=node_of[i], path="translation")))
            anims.append(dict(name=name, samplers=samplers, channels=channels))
        root = len(gnodes)
        gnodes.append(dict(name="character", children=roots + mesh_nodes))
        gltf = dict(asset=dict(version="2.0", generator="lowmarch export_skinned"), scene=0, scenes=[dict(nodes=[root])],
                    nodes=gnodes, meshes=meshes, materials=materials, skins=[skin], accessors=accessors, bufferViews=views,
                    buffers=[dict(byteLength=len(bin_))], samplers=[dict(magFilter=9729, minFilter=9987)])
        if textures:
            gltf.update(textures=textures, images=images)
        if anims:
            gltf["animations"] = anims
        js = json.dumps(gltf, separators=(",", ":")).encode()
  • Write~/Projects/lowmarch-fable52/review/out/whole-clips/h.py

    input

    ~/Projects/lowmarch-fable52/review/out/whole-clips/h.py
    
    """Scratch harness: wren's vrm and shape as tools/whole/build.py builds them."""
    import importlib.util
    import os
    import sys
    import time
    
    ROOT = "~/Projects/lowmarch-fable52"
    os.environ.setdefault("LOWMARCH_SITE", "whole-clips")
    os.environ.setdefault("WHOLE_STANCE", "longsword")
    sys.argv = [sys.argv[0]]
    spec = importlib.util.spec_from_file_location("wholebuild", f"{ROOT}/tools/whole/build.py")
    wb = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(wb)
    import pickle
    
    from vrm import VRM  # noqa: E402
    
    CACHE = f"{ROOT}/review/out/whole-clips/shape.pkl"
    
    
    def load():
        c = wb.CAST["wren"]
        if os.path.exists(CACHE):
            shape = pickle.load(open(CACHE, "rb"))
        else:
            shape = dict(c["shape"])
            if "arm_out" not in shape:
                shape["arm_out"] = wb.build.arm_room("wren", c)
            pickle.dump(shape, open(CACHE, "wb"))
        v = VRM(wb.build.model_path("wren", c))
        return v, shape
    

    result, 1 line

    File created successfully at: ~/Projects/lowmarch-fable52/review/out/whole-clips/h.py (file state is current in your context — no need to Read it back)
  • Bashcd ~/Projects/lowmarch-fable52/review/out/whole-clips && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 python3 - <<'EOF'

    command

    cd ~/Projects/lowmarch-fable52/review/out/whole-clips && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 python3 - <<'EOF'
    import time; t=time.time()
    import h
    v,shape=h.load(); print('load',time.time()-t, shape.get('arm_out'))
    import numpy as np, poses as P
    from rig import Rig, grip_points
    import vroid_portrait as vp
    print('BUST same', all(np.allclose(np.asarray(vp.BUST_POSE[k]), np.asarray(P.BUST[k])) for k in P.BUST) and set(vp.BUST_POSE)==set(P.BUST))
    rig=Rig(v,shape)
    t=time.time()
    for b in ["J_Bip_C_Hips","J_Bip_C_Head","J_Bip_R_UpperArm","J_Bip_L_UpperArm","J_Bip_R_Hand","J_Bip_L_Foot","J_Bip_R_Foot","J_Bip_C_UpperChest","J_Bip_C_Neck"]:
        print(b, rig.pos(P.BUST,b).round(3))
    print('pos time', time.time()-t)
    g=grip_points(v,shape,P.BUST)
    W=rig.world(P.BUST)[rig.names["J_Bip_R_Hand"]]
    R=W[:3,:3]/np.linalg.norm(W[:3,:3],axis=0)
    sw=W[:3,3][redacted: email]([0.045,-0.018,0])
    print('grip R', g['R']['point'].round(4), 'sword fist', sw.round(4), 'fwd', (R@[0,0,-1]).round(3), 'axis', g['R']['axis'].round(3))
    print('grip L', g['L']['point'].round(4))
    t=time.time(); rig.world(P.BUST); print('world', time.time()-t)
    t=time.time()
    p=rig.grip(P.BATTLE_IDLE,"R",P.BUST,sw,np.array([0.05,1.0,-0.3]),(0,0.5,-1),(1,-1,0))
    print('grip time', time.time()-t)
    from checks import Body
    t=time.time(); b=Body(v,shape); print('body', time.time()-t)
    t=time.time(); print(b.clearance(p)); print('clear', time.time()-t)
    EOF

    result, 19 lines

    load 0.8253927230834961 4.0
    BUST same True
    J_Bip_C_Hips [ 0.     0.913 -0.022]
    J_Bip_C_Head [-0.     1.464 -0.039]
    J_Bip_R_UpperArm [ 0.147  1.338 -0.019]
    J_Bip_L_UpperArm [-0.147  1.338 -0.019]
    J_Bip_R_Hand [ 0.278  0.883 -0.019]
    J_Bip_L_Foot [-0.098  0.103 -0.021]
    J_Bip_R_Foot [ 0.098  0.103 -0.021]
    J_Bip_C_UpperChest [-0.     1.255 -0.032]
    J_Bip_C_Neck [0.    1.425 0.002]
    pos time 0.08374953269958496
    grip R [ 0.2825  0.8134 -0.0135] sword fist [ 0.2727  0.8344 -0.0195] fwd [ 0.  0. -1.] axis [0. 0. 1.]
    grip L [-0.2825  0.8134 -0.0135]
    world 0.0063588619232177734
    grip time 0.4106018543243408
    body 0.04828834533691406
    {'R_UpperArm': 0.006649652637906456, 'R_LowerArm': 0.062460014430657324, 'R_Hand': 0.13784047839510652, 'L_UpperArm': 0.011108825064230825, 'L_LowerArm': 0.06488113254643793, 'L_Hand': 0.07581261224460584}
    clear 0.031136035919189453
  • Bashcd ~/Projects/lowmarch-fable52/review/out/whole-clips && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 python3 - <<'EOF'

    command

    cd ~/Projects/lowmarch-fable52/review/out/whole-clips && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 python3 - <<'EOF'
    import h
    v,shape=h.load()
    import vroid_portrait as vp, numpy as np
    for p in v.primitives():
        print(p['material'], vp.role_of(p['material']), len(p['pos']), 'J' in p, p.get('mesh'))
    names=v.nodes_by_name()
    print([n for n in names if 'Foot' in n or 'Toe' in n or 'Shoulder' in n])
    EOF

    result, 12 lines

    N00_000_00_FaceMouth_00_FACE (Instance) mouth 4286 True Face (merged)(Clone).baked
    N00_000_00_EyeIris_00_EYE (Instance) iris 4286 True Face (merged)(Clone).baked
    N00_000_00_EyeHighlight_00_EYE (Instance) highlight 4286 True Face (merged)(Clone).baked
    N00_000_00_Face_00_SKIN (Instance) face 4286 True Face (merged)(Clone).baked
    N00_000_00_EyeWhite_00_EYE (Instance) white 4286 True Face (merged)(Clone).baked
    N00_000_00_FaceBrow_00_FACE (Instance) brow 4286 True Face (merged)(Clone).baked
    N00_000_00_FaceEyelash_00_FACE (Instance) line 4286 True Face (merged)(Clone).baked
    N00_000_00_FaceEyeline_00_FACE (Instance) line 4286 True Face (merged)(Clone).baked
    N00_000_00_Body_00_SKIN (Instance) body 7338 True Body (merged).baked
    N00_000_Hair_00_HAIR lm hair 23222 True Hair (lowmarch heads.py)
    N00_000_00_HairBack_00_HAIR lm hair 23222 True Hair (lowmarch heads.py)
    ['J_Bip_L_Shoulder', 'J_Bip_R_Shoulder', 'J_Bip_L_Foot', 'J_Bip_L_ToeBase', 'J_Bip_R_Foot', 'J_Bip_R_ToeBase']
  • Bashsed -n 1,40p tools/vrm.py | head -50; grep -n "def bone_weight" -A12 tools/vrm.py; python3 -c "import scipy; print(scipy.__version__)"

    command

    sed -n 1,40p tools/vrm.py | head -50; grep -n "def bone_weight" -A12 tools/vrm.py; python3 -c "import scipy; print(scipy.__version__)"

    result, 48 lines

    """VRM / glTF-binary loader and a small software rasterizer, no dependencies beyond numpy + PIL.
    
    VRoid models are our 3D base (CC0 samples, see docs/vroid.md). This module reads them and
    rasterizes triangles into per-pixel buffers (depth, primitive id, normal, uv, local position) that
    the portrait renderer shades in our palette. Nothing from the model's textures is copied into the
    game except through those buffers (eye/brow/lash shapes come from alpha-tested texture masks).
    """
    import io
    import json
    import struct
    
    import numpy as np
    from PIL import Image
    
    _CT = {5120: np.int8, 5121: np.uint8, 5122: np.int16, 5123: np.uint16, 5125: np.uint32, 5126: np.float32}
    _NC = {"SCALAR": 1, "VEC2": 2, "VEC3": 3, "VEC4": 4, "MAT4": 16}
    
    
    class VRM:
        def __init__(self, path):
            b = open(path, "rb").read()
            magic, _, _ = struct.unpack("<4sII", b[:12])
            assert magic == b"glTF", path
            jlen, _ = struct.unpack("<II", b[12:20])
            self.j = json.loads(b[20:20 + jlen])
            off = 20 + jlen
            blen, _ = struct.unpack("<II", b[off:off + 8])
            self.bin = b[off + 8:off + 8 + blen]
            self._img = {}
            self.meta = self.j.get("extensions", {}).get("VRM", {}).get("meta", {})
    
        # ---- raw data --------------------------------------------------------------
        def accessor(self, i):
            a = self.j["accessors"][i]
            dt, nc = _CT[a["componentType"]], _NC[a["type"]]
            n = a["count"]
            if "bufferView" not in a:
                return np.zeros((n, nc), np.float32)
            bv = self.j["bufferViews"][a["bufferView"]]
            start = bv.get("byteOffset", 0) + a.get("byteOffset", 0)
    268:def bone_weight(prim, match):
    269-    """Per-vertex total skin weight on bones whose name contains any of `match` (substrings)."""
    270-    if "J" not in prim:
    271-        return np.zeros(len(prim["pos"]))
    272-    names = prim["jnames"]
    273-    hit = np.array([any(m in n for m in match) for n in names])
    274-    return (hit[prim["J"]] * prim["W"]).sum(1)
    1.15.3