Subagent: Two-handed longsword clips

agent (cont.)

+6:10
  • Write~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    
    """Two-handed longsword clips for a whole character (tools/whole/build.py, WHOLE_STANCE=longsword).
    
      clips(vrm, shape) -> {"idle": [(t, pose), ...], "attack": [(t, pose), ...]}
    
    The sword is rigid on the right hand (outfit.sword); the left fist closes on the same grip SPAN further
    down, toward the pommel. A clip is a few keys of plain numbers (how low and turned the body is, where the
    lead foot is, where the right fist sits from the chest, where the blade points), eased between with
    Hermite curves and solved at every sample: feet planted or stepped with leg IK and kept flat, both hands
    gripped with duel.clear_grip (the elbows tried until the arms clear the body), the head turned back level
    toward the enemy. Solving every sample rather than blending a few solved poses keeps both fists on the
    grip and the hands on arcs between keys.
    
    Guard: a middle guard, left foot forward, hips bladed and sunk, the fists before the navel and the point
    up at the enemy's face. Attack: the blade drawn up and back over the right shoulder as the weight settles
    back, a descending diagonal cut from the right shoulder through the enemy with a lunge of the lead foot
    and the hips and shoulders turning the right side through, a follow-through low on the left, then a step
    back up into the guard.
    """
    import numpy as np
    from scipy.spatial.transform import Rotation
    
    import poses as P
    from clips import sink
    from rig import Rig, grip_points
    
    SWORD_FIST = (0.045, -0.018, 0.0)   # outfit.sword: the grip's axis in the right hand's frame
    SPAN = 0.095                        # the left fist this far down the grip from the right (duel.py)
    
    # A key: hips down (drop) and forward (fwd), the hips' and the shoulders' turn (degrees, + brings the right
    # side forward), forward lean, the lead (left) foot's step and lift, the right fist from the upper chest in
    # model space (+x her right, -z forward), the blade's direction, each elbow's preferred direction, and how
    # far down the gaze tips.
    GUARD = dict(drop=0.06, fwd=0.0, hip=-22.0, turn=-14.0, lean=6.0, lead=0.0, lift=0.0,
                 fist=(0.03, -0.20, -0.32), blade=(-0.05, 0.45, -0.89),
                 poleR=(0.7, -1.0, 0.3), poleL=(-0.8, -1.0, 0.2), gaze=-0.06)
    BREATH = dict(drop=0.075, lean=8.5, fist=(0.03, -0.212, -0.315), blade=(-0.05, 0.40, -0.91))   # the low end
    IDLE_LOOP, IDLE_STEP, BLADE_LAG = 2.0, 0.2, 0.1
    
    
    def key(**kw):
        return dict(GUARD, **kw)
    
    
    # (time, key, hold): a held key eases in and out (no velocity through it)
    ATTACK = [
        (0.0, key(), True),
        (0.2, key(drop=0.065, fwd=-0.02, hip=-26.0, turn=-26.0, lean=3.0, fist=(0.12, -0.04, -0.20),
                  blade=(0.12, 0.98, -0.12)), False),
        (0.32, key(drop=0.07, fwd=-0.05, hip=-30.0, turn=-38.0, lean=0.0, fist=(0.20, 0.08, -0.06),
                   blade=(0.30, 0.70, 0.65), poleR=(1.0, -0.6, 0.2)), False),
        (0.44, key(drop=0.08, fwd=-0.06, hip=-32.0, turn=-42.0, lean=-1.0, fist=(0.21, 0.10, -0.02),
                   blade=(0.32, 0.55, 0.77), poleR=(1.0, -0.6, 0.2)), True),
        (0.5, key(drop=0.08, fwd=0.06, hip=-4.0, turn=-10.0, lean=4.0, lead=0.15, lift=0.06, fist=(0.12, 0.12, -0.30),
                  blade=(0.15, 0.90, -0.40)), False),
        (0.56, key(drop=0.11, fwd=0.16, hip=10.0, turn=22.0, lean=14.0, lead=0.30, fist=(-0.02, -0.02, -0.52),
                   blade=(-0.30, -0.10, -0.95)), False),
        (0.62, key(drop=0.12, fwd=0.17, hip=13.0, turn=28.0, lean=16.0, lead=0.30, fist=(-0.14, -0.25, -0.45),
                   blade=(-0.50, -0.55, -0.60)), False),
        (0.68, key(drop=0.12, fwd=0.17, hip=14.0, turn=30.0, lean=16.0, lead=0.30, fist=(-0.22, -0.40, -0.28),
                   blade=(-0.55, -0.70, 0.45), poleR=(0.4, -1.0, -0.3)), False),
        (0.9, key(drop=0.11, fwd=0.16, hip=12.0, turn=27.0, lean=14.0, lead=0.30, fist=(-0.20, -0.36, -0.30),
                  blade=(-0.55, -0.62, 0.50), poleR=(0.4, -1.0, -0.3)), True),
        (1.05, key(drop=0.11, fwd=0.16, hip=12.0, turn=27.0, lean=14.0, lead=0.30, fist=(-0.20, -0.36, -0.30),
                   blade=(-0.55, -0.62, 0.50), poleR=(0.4, -1.0, -0.3)), True),
        (1.25, key(drop=0.08, fwd=0.06, hip=-8.0, turn=0.0, lean=9.0, lead=0.14, lift=0.04, fist=(-0.06, -0.28, -0.36),
                   blade=(-0.45, 0.10, -0.85)), False),
        (1.5, key(), True),
    ]
    SAMPLES = [0.0, 0.1, 0.2, 0.26, 0.32, 0.38, 0.44, 0.47, 0.5, 0.53, 0.56, 0.59, 0.62, 0.65, 0.68, 0.78, 0.9, 1.05,
               1.15, 1.25, 1.35, 1.42, 1.5]
    
    
    def hermite(keys, t):
        """The keys' numbers at time t: cubic Hermite, tangents from the neighbours (zero on held keys)."""
        ts = [k[0] for k in keys]
        i = max(0, min(len(keys) - 2, int(np.searchsorted(ts, t, side="right")) - 1))
        (t0, a, h0), (t1, b, h1) = keys[i], keys[i + 1]
        s = (t - t0) / (t1 - t0)
    
        def tan(j):
            if keys[j][2] or j == 0 or j == len(keys) - 1:
                return {n: 0 * np.asarray(v, float) for n, v in keys[j][1].items()}
            (tp, kp, _), (tn, kn, _) = keys[j - 1], keys[j + 1]
            return {n: (np.asarray(kn[n], float) - np.asarray(kp[n], float)) / (tn - tp) for n in kp}
        m0, m1 = tan(i), tan(i + 1)
        h00, h10, h01, h11 = 2 * s ** 3 - 3 * s * s + 1, s ** 3 - 2 * s * s + s, -2 * s ** 3 + 3 * s * s, s ** 3 - s * s
        dt = t1 - t0
        return {n: h00 * np.asarray(a[n], float) + h10 * dt * m0[n] + h01 * np.asarray(b[n], float) + h11 * dt * m1[n]
                for n in a}
    
    
    class Fighter:
        def __init__(self, vrm, shape=None):
            from checks import Body
            self.rig = Rig(vrm, shape, ortho=True)
            self.body = Body(vrm, shape)
            rig = self.rig
            self.base = P.merge(P.BATTLE_IDLE, P.fist("L"))
            W = rig.world(P.BUST)[rig.names["J_Bip_R_Hand"]]
            R = W[:3, :3] / np.linalg.norm(W[:3, :3], axis=0)
            self.ref = dict(ref_pose=P.BUST, fist_R=W[:3, 3] + R @ np.array(SWORD_FIST),
                            fist_L=grip_points(vrm, rig.shape, P.BUST)["L"]["point"])
            self.foot = {s: rig.pos(self.base, f"J_Bip_{s}_Foot") for s in "LR"}
            self.toe = {s: rig.pos(self.base, f"J_Bip_{s}_ToeBase") - self.foot[s] for s in "LR"}
            Wh = rig.world(P.BUST)[rig.names["J_Bip_C_Head"]][:3, :3]
            Q = self._rot(Wh)
            self.head_f, self.head_u = Q.T @ np.array([0, 0, -1.0]), Q.T @ np.array([0, 1.0, 0])
    
        @staticmethod
        def _rot(M):
            u, _, vt = np.linalg.svd(M[:3, :3])
            return u @ vt
    
        def sword(self, pose):
            """The grip's axis point under the right fist and the blade's direction, in model space."""
            rig = self.rig
            a = rig.held(pose, "J_Bip_R_Hand", P.BUST, self.ref["fist_R"])
            d = self._rot(rig.world(pose)[rig.names["J_Bip_R_Hand"]]) @ np.array([0, 0, -1.0])
            return a, d / np.linalg.norm(d)
    
        def flat(self, pose, side, yaw):
            """Turn the foot so heel to toe runs as in the base stance, turned `yaw` degrees (- toes out to her right)."""
            rig = self.rig
            cur = rig.pos(pose, f"J_Bip_{side}_ToeBase") - rig.pos(pose, f"J_Bip_{side}_Foot")
            return rig.rotate_world(pose, f"J_Bip_{side}_Foot", P.rot((0, 1, 0), yaw) @ np.eye(3) @ _between(cur, self.toe[side]))
    
        def look(self, pose, down, neck=0.4):
            """Turn the neck (a share) and the head so she faces -z (the enemy), level, the gaze tipped down a little."""
            rig = self.rig
            g = np.array([0.0, down, -1.0])
            g /= np.linalg.norm(g)
            up = np.array([0.0, 1.0, 0.0]) - g * g[1]
            up /= np.linalg.norm(up)
            want = np.c_[g, up, np.cross(g, up)]
            for bone, share in (("J_Bip_C_Neck", neck), ("J_Bip_C_Head", 1.0)):
                Q = self._rot(rig.world(pose)[rig.names["J_Bip_C_Head"]])
                f, u = Q @ self.head_f, Q @ self.head_u
                have = np.c_[f, u, np.cross(f, u)]
                R = Rotation.from_matrix(want @ have.T).as_rotvec() * share
                pose = rig.rotate_world(pose, bone, Rotation.from_rotvec(R).as_matrix())
            return pose
    
        def solve(self, k):
            from duel import clear_grip
            rig = self.rig
            twist = k["turn"] - k["hip"]
            pose = P.merge(self.base, {
                "J_Bip_C_Hips": P.move(0, -k["drop"], -k["fwd"], P.rot((0, 1, 0), k["hip"]) @ P.rot((1, 0, 0), -k["lean"] * 0.6)),
                "J_Bip_C_Spine": P.rot((0, 1, 0), twist * 0.55) @ P.rot((1, 0, 0), -k["lean"] * 0.25),
                "J_Bip_C_Chest": P.rot((0, 1, 0), twist * 0.45) @ P.rot((1, 0, 0), -k["lean"] * 0.15)})
            pose = rig.leg_ik(pose, "L", self.foot["L"] + np.array([-0.05, k["lift"], -0.10 - k["lead"]]), pole=(-0.1, 0, -1))
            pose = rig.leg_ik(pose, "R", self.foot["R"] + np.array([0.05, 0, 0.17]), pole=(0.3, -0.2, -1))
            pose = self.flat(pose, "L", 0.0)
            pose = self.flat(pose, "R", -35.0)
            fr = rig.pos(pose, "J_Bip_C_UpperChest") + np.asarray(k["fist"], float)
            D = np.asarray(k["blade"], float)
            D /= np.linalg.norm(D)
            pose = clear_grip(rig, self.body, pose, "R", self.ref, fr, D, tuple(k["poleR"]))
            a, d = self.sword(pose)
            pose = clear_grip(rig, self.body, pose, "L", self.ref, a - d * SPAN, d, tuple(k["poleL"]))
            return self.look(pose, float(k["gaze"]))
    
    
    def _between(a, b):
        from rig import rot_between
        return rot_between(a, b)
    
    
    def guard_at(t):
        """The guard's numbers at time t of the idle loop: the body sinks (clips.sink), the blade a beat behind."""
        k, kb = sink(t), sink(t - BLADE_LAG)
        out = dict(GUARD)
        for n in ("drop", "lean"):
            out[n] = GUARD[n] + (BREATH[n] - GUARD[n]) * k
        out["fist"] = tuple(np.asarray(GUARD["fist"]) + (np.asarray(BREATH["fist"]) - GUARD["fist"]) * k)
        out["blade"] = tuple(np.asarray(GUARD["blade"]) + (np.asarray(BREATH["blade"]) - GUARD["blade"]) * kb)
        return out
    
    
    def clips(vrm, shape=None, f=None):
        f = f or Fighter(vrm, shape)
        n = int(round(IDLE_LOOP / IDLE_STEP))
        idle = [(round(i * IDLE_STEP, 3), f.solve(guard_at(i * IDLE_STEP))) for i in range(n)]
        idle.append((IDLE_LOOP, idle[0][1]))
        attack = [(t, f.solve(hermite(ATTACK, t))) for t in SAMPLES]
        attack[0] = (0.0, f.solve(guard_at(0.0)))
        attack[-1] = (SAMPLES[-1], attack[0][1])
        return {"idle": idle, "attack": attack}
    

    result, 1 line

    File created successfully at: ~/Projects/lowmarch-fable52/tools/whole/clips2h.py (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    -     attack = [(t, f.solve(hermite(ATTACK, t))) for t in SAMPLES]
    -     attack[0] = (0.0, f.solve(guard_at(0.0)))
    -     attack[-1] = (SAMPLES[-1], attack[0][1])
    -     return {"idle": idle, "attack": attack}
    +     rest = guard_at(0.0)                                     # the attack leaves and rejoins the idle's first pose
    +     keys = [(ATTACK[0][0], rest, True)] + ATTACK[1:-1] + [(ATTACK[-1][0], rest, True)]
    +     attack = [(t, f.solve(hermite(keys, t))) for t in SAMPLES[1:-1]]
    +     attack = [(0.0, idle[0][1])] + attack + [(SAMPLES[-1], idle[0][1])]
    +     return {"idle": idle, "attack": attack}

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    -         return rig.rotate_world(pose, f"J_Bip_{side}_Foot", P.rot((0, 1, 0), yaw) @ np.eye(3) @ _between(cur, self.toe[side]))
    +         return rig.rotate_world(pose, f"J_Bip_{side}_Foot", P.rot((0, 1, 0), yaw) @ rot_between(cur, self.toe[side]))

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    - def _between(a, b):
    -     from rig import rot_between
    -     return rot_between(a, b)
    - 
    - 
    - 
    + 

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    - from rig import Rig, grip_points
    + from rig import Rig, grip_points, rot_between

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Write~/Projects/lowmarch-fable52/review/out/whole-clips/t.py

    input

    ~/Projects/lowmarch-fable52/review/out/whole-clips/t.py
    
    """Scratch checks on clips2h: arms in body, fists on the grip, blade/hands vs head+hair, pauldron, legs, ground."""
    import sys
    import time
    
    import numpy as np
    from scipy.spatial import cKDTree
    
    import h
    
    t0 = time.time()
    v, shape = h.load()
    sys.path.insert(0, f"{h.ROOT}/tools/whole")
    import importlib
    import clips2h
    import checks
    import poses as P
    import vroid_portrait as vp
    from vrm import bone_weight
    
    f = clips2h.Fighter(v, shape)
    t1 = time.time()
    cl = clips2h.clips(v, shape, f)
    print(f"solve {time.time() - t1:.1f}s (setup {t1 - t0:.1f}s)")
    head, bad, touch = checks.report("wren", cl, f.body)
    print(head)
    for it in bad:
        print("  ! " + it["text"])
    for it in touch:
        print("  ~ " + it["text"])
    
    rig = f.rig
    prims = [p for p in v.primitives() if "J" in p]
    mesh_ix = {m.get("name"): i for i, m in enumerate(v.j["meshes"])}
    skin_of = {n.get("mesh"): n.get("skin") for n in v.j["nodes"] if "mesh" in n}
    
    
    def posed(pose, sel, every=1):
        bp = vp.bone_pose(v, rig.shape, pose)
        jm = v.skin_matrices(bp)
        out = []
        for p in sel:
            s = skin_of[mesh_ix[p["mesh"]]]
            J, Wt, X = p["J"][::every], p["W"][::every], p["pos"][::every]
            M = (jm[s][J] * Wt[:, :, None, None]).sum(1)
            out.append(np.einsum("nij,nj->ni", M[:, :3, :3], X) + M[:, :3, 3])
        return np.concatenate(out)
    
    
    head_prims = [p for p in prims if vp.role_of(p["material"]) in ("face", "hair")]
    # hair dedupe: two hair prims share a mesh; the face mesh shares one too
    seen, hp = set(), []
    for p in head_prims:
        key = (p["mesh"], len(p["pos"]), vp.role_of(p["material"]))
        if key not in seen:
            seen.add(key)
            hp.append(p)
    body_p = [p for p in prims if vp.role_of(p["material"]) == "body"]
    w_sh = np.concatenate([bone_weight(p, ("L_Shoulder",)) for p in body_p])
    w_ua = np.concatenate([bone_weight(p, ("L_UpperArm",)) for p in body_p])
    w_leg = np.concatenate([bone_weight(p, ("UpperLeg", "LowerLeg")) for p in body_p])
    
    
    def hand_pts(pose, s):
        return np.array([rig.pos(pose, f"J_Bip_{s}_{b}") for b in ("Hand", "Index1", "Middle1", "Little1", "Middle3", "Thumb3")])
    
    
    def report(clip, keys):
        prev = None
        for t, pose in keys:
            a, d = f.sword(pose)
            lf = rig.held(pose, "J_Bip_L_Hand", P.BUST, f.ref["fist_L"])
            along = float((a - lf) @ d)
            off = float(np.linalg.norm((lf - a) - d * ((lf - a) @ d)))
            guard = a + d * (0.21 * 0.45 + 0.008)
            blade = guard + d[None] * np.linspace(0.0, 0.84, 24)[:, None]
            grip = a + d[None] * np.linspace(-0.13, 0.10, 6)[:, None]
            hands = np.r_[hand_pts(pose, "R"), hand_pts(pose, "L")]
            H = posed(pose, hp, every=3)
            th = cKDTree(H)
            db = th.query(blade)[0].min()
            dg = th.query(np.r_[grip, hands])[0].min()
            arms = np.array([rig.pos(pose, f"J_Bip_{s}_{b}") for s in "RL" for b in ("LowerArm",)])
            da = th.query(arms)[0].min()
            Bp, Bn, _ = f.body.posed(pose)
            pz = (w_sh > 0.3) | ((w_ua > 0.5) & (Bp[:, 1] > rig.pos(pose, "J_Bip_L_UpperArm")[1] - 0.04))
            pts = Bp[pz] + Bn[pz] * 0.055
            tp = cKDTree(pts)
            dp = min(tp.query(blade)[0].min(), tp.query(np.r_[grip, hand_pts(pose, "R")])[0].min())
            tl = cKDTree(Bp[w_leg > 0.5])
            dl = tl.query(blade)[0].min()
            tip = blade[-1, 1]
            feet = {s: (rig.pos(pose, f"J_Bip_{s}_Foot"), rig.pos(pose, f"J_Bip_{s}_ToeBase")) for s in "LR"}
            Wh = rig.world(pose)[rig.names["J_Bip_C_Hips"]][:3, :3]
            Q = f._rot(Wh)
            ext = {}
            for s in "LR":
                th_ = Q.T @ (rig.pos(pose, f"J_Bip_{s}_LowerLeg") - rig.pos(pose, f"J_Bip_{s}_UpperLeg"))
                ext[s] = np.degrees(np.arctan2(th_[2], -th_[1]))       # + thigh behind the pelvis' down line
            elb = {}
            for s in "RL":
                S, E, W_ = (rig.pos(pose, f"J_Bip_{s}_{b}") for b in ("UpperArm", "LowerArm", "Hand"))
                u1, u2 = S - E, W_ - E
                elb[s] = np.degrees(np.arccos(u1 @ u2 / np.linalg.norm(u1) / np.linalg.norm(u2)))
            cl_ = f.body.clearance(pose)
            worst = min(cl_, key=cl_.get)
            hd = rig.pos(pose, "J_Bip_C_Head")
            Qh = f._rot(rig.world(pose)[rig.names["J_Bip_C_Head"]])
            gz = Qh @ f.head_f
            drift = ""
            if prev is not None:
                drift = " drift " + " ".join(f"{s}{np.linalg.norm(feet[s][0] - prev[s][0]) * 100:.1f}" for s in "LR")
            prev = feet
            print(f"{clip} {t:4.2f} span {along * 100:5.1f} off {off * 100:4.1f} | head/hair: blade {db * 100:5.1f} grip+hands {dg * 100:5.1f} "
                  f"forearm {da * 100:5.1f} | paul {dp * 100:5.1f} | legs {dl * 100:5.1f} | tip y {tip:5.2f} "
                  f"| clear {worst} {cl_[worst] * 100:4.1f} | elbow R{elb['R']:4.0f} L{elb['L']:4.0f} | hipext L{ext['L']:4.0f} R{ext['R']:4.0f} "
                  f"| toe y L{feet['L'][1][1]:5.3f} R{feet['R'][1][1]:5.3f} ank L{feet['L'][0][1]:5.3f} | gaze {gz.round(2)} d {d.round(2)}{drift}")
    
    
    for clip, keys in cl.items():
        if len(sys.argv) > 1 and clip not in sys.argv[1:]:
            continue
        report(clip, keys)
    print(f"total {time.time() - t0:.0f}s")
    

    result, 1 line

    File created successfully at: ~/Projects/lowmarch-fable52/review/out/whole-clips/t.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 t.py 2>&1 | tail -50

    command

    cd ~/Projects/lowmarch-fable52/review/out/whole-clips && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 python3 t.py 2>&1 | tail -50

    result, 2 lines

    Command did not complete within its 120s timeout and was moved to the background (ID: bd9oiuzyh). Output is being written to: [redacted: private path]/tasks/bd9oiuzyh.output. You will be notified when it completes. It is running now: do not start a second copy while it runs. If it is no longer needed, stop it with TaskStop. No output had been read from it when it was moved. A program that buffers its output, or a pipe into one such as tail, can keep the output file empty until the command finishes. To check interim output, use Read on that file path.
    Session cwd remains ~/Projects/lowmarch-fable52; directory changes made by the backgrounded command do not apply to subsequent commands.
  • Bashsleep 100; cat [redacted: private path]/tasks/bd9oiuzyh.outputerror

    command

    sleep 100; cat [redacted: private path]/tasks/bd9oiuzyh.output

    result, error, 1 line

    <tool_use_error>Blocked: sleep 100 followed by: cat [redacted: private path]/tasks/bd9oiuzyh.output. To wait for a condition, use Monitor with an until-loop (e.g. `until <check>; do sleep 2; done`). To wait for a command you started, use run_in_background: true. Do not chain shorter sleeps to work around this block.</tool_use_error>
  • ToolSearchselect:Monitor

    input

    {
      "query": "select:Monitor",
      "max_results": 1
    }

    result

    no output

  • Bashuntil grep -q "total\|Error\|Traceback" [redacted: private path]/tasks/bd9oiuzyh.output; do sleep 3; done; cat [redacted: private path]/tasks/bd9oiuzyh.output

    command

    until grep -q "total\|Error\|Traceback" [redacted: private path]/tasks/bd9oiuzyh.output; do sleep 3; done; cat [redacted: private path]/tasks/bd9oiuzyh.output

    result, 52 lines

      ! wren, idle at 2.00 s: right upper arm 1.3 cm inside the body
      ! wren, attack at 0.00 s: right upper arm 1.3 cm inside the body
      ! wren, attack at 0.56 s: left upper arm 2.1 cm inside the body
      ! wren, attack at 0.59 s: left upper arm 1.0 cm inside the body
      ! wren, attack at 0.62 s: right upper arm 1.2 cm inside the body; left upper arm 0.8 cm from the body
      ! wren, attack at 0.65 s: right upper arm 4.0 cm inside the body; left upper arm 0.9 cm from the body
      ! wren, attack at 0.68 s: right upper arm 5.9 cm inside the body; left upper arm 1.3 cm inside the body
      ! wren, attack at 0.78 s: right upper arm 6.4 cm inside the body; left upper arm 2.8 cm inside the body
      ! wren, attack at 0.90 s: right upper arm 5.9 cm inside the body; left upper arm 2.6 cm inside the body
      ! wren, attack at 1.05 s: right upper arm 5.9 cm inside the body; left upper arm 2.6 cm inside the body
      ! wren, attack at 1.15 s: right upper arm 6.2 cm inside the body; left upper arm 0.7 cm inside the body
      ! wren, attack at 1.25 s: right upper arm 6.4 cm inside the body; left upper arm 1.5 cm from the body
      ! wren, attack at 1.35 s: right upper arm 5.6 cm inside the body
      ! wren, attack at 1.42 s: right upper arm 2.5 cm inside the body
      ! wren, attack at 1.50 s: right upper arm 1.3 cm inside the body
    idle 0.00 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.2 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -35 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.4  -0.91]
    idle 0.20 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.1 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -34 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [-0.05  0.4  -0.91] drift L0.0 R0.0
    idle 0.40 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.8 forearm  26.4 | paul  33.1 | legs  38.6 | tip y  1.36 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -33 R -12 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [-0.05  0.41 -0.91] drift L0.0 R0.0
    idle 0.60 span   9.5 off  0.2 | head/hair: blade  45.6 grip+hands  38.2 forearm  26.7 | paul  33.0 | legs  38.8 | tip y  1.39 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -31 R -11 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [-0.05  0.43 -0.9 ] drift L0.0 R0.0
    idle 0.80 span   9.5 off  0.2 | head/hair: blade  45.4 grip+hands  38.6 forearm  26.9 | paul  32.9 | legs  39.0 | tip y  1.41 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -30 R  -9 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [-0.05  0.44 -0.89] drift L0.0 R0.0
    idle 1.00 span   9.5 off  0.2 | head/hair: blade  45.3 grip+hands  38.7 forearm  26.9 | paul  32.9 | legs  39.0 | tip y  1.42 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -30 R  -9 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.45 -0.89] drift L0.0 R0.0
    idle 1.20 span   9.5 off  0.2 | head/hair: blade  45.3 grip+hands  38.6 forearm  26.8 | paul  32.9 | legs  39.0 | tip y  1.42 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -30 R  -9 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.45 -0.89] drift L0.0 R0.0
    idle 1.40 span   9.5 off  0.2 | head/hair: blade  45.4 grip+hands  38.0 forearm  26.4 | paul  33.1 | legs  38.7 | tip y  1.38 | clear R_UpperArm -1.2 | elbow R 135 L 107 | hipext L -33 R -12 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [-0.05  0.44 -0.9 ] drift L0.0 R0.0
    idle 1.60 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.2 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 107 | hipext L -35 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.4  -0.91] drift L0.0 R0.0
    idle 1.80 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.2 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -35 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.4  -0.91] drift L0.0 R0.0
    idle 2.00 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.2 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -35 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.4  -0.91] drift L0.0 R0.0
    attack 0.00 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.2 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -35 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.4  -0.91]
    attack 0.10 span   9.5 off  0.1 | head/hair: blade  35.5 grip+hands  35.6 forearm  25.8 | paul  27.1 | legs  40.6 | tip y  1.71 | clear R_UpperArm  1.9 | elbow R 107 L  98 | hipext L -32 R -13 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [ 0.01  0.72 -0.69] drift L0.0 R0.0
    attack 0.20 span   9.6 off  0.1 | head/hair: blade  15.6 grip+hands  19.7 forearm  17.9 | paul  19.3 | legs  47.3 | tip y  2.08 | clear L_UpperArm  2.4 | elbow R  70 L  81 | hipext L -28 R -12 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [ 0.12  0.99 -0.12] drift L0.0 R0.0
    attack 0.26 span   9.6 off  0.1 | head/hair: blade  11.9 grip+hands  13.6 forearm  15.4 | paul  17.8 | legs  48.8 | tip y  2.09 | clear R_LowerArm  2.7 | elbow R  53 L  71 | hipext L -26 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [0.23 0.93 0.3 ] drift L0.0 R0.0
    attack 0.32 span   9.5 off  0.1 | head/hair: blade  10.9 grip+hands  10.9 forearm  16.2 | paul  15.8 | legs  50.8 | tip y  1.92 | clear L_UpperArm  2.3 | elbow R  48 L  66 | hipext L -26 R -16 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [0.3  0.7  0.65] drift L0.0 R0.0
    attack 0.38 span   9.5 off  0.1 | head/hair: blade  10.8 grip+hands   9.8 forearm  15.3 | paul  14.6 | legs  51.6 | tip y  1.83 | clear L_UpperArm  2.5 | elbow R  47 L  66 | hipext L -26 R -18 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [0.32 0.58 0.75] drift L0.0 R0.0
    attack 0.44 span   9.5 off  0.1 | head/hair: blade  10.6 grip+hands   9.1 forearm  14.8 | paul  14.2 | legs  51.3 | tip y  1.79 | clear L_UpperArm  2.6 | elbow R  46 L  65 | hipext L -26 R -18 | toe y L0.057 R0.036 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [0.32 0.55 0.77] drift L0.0 R0.0
    attack 0.47 span   9.5 off  0.1 | head/hair: blade  13.3 grip+hands  12.3 forearm  19.6 | paul  17.6 | legs  57.2 | tip y  2.13 | clear L_UpperArm  4.5 | elbow R  50 L  66 | hipext L -39 R -11 | toe y L0.087 R0.036 ank L0.143 | gaze [-0.   -0.06 -1.  ] d [0.32 0.89 0.34] drift L6.4 R0.0
    attack 0.50 span   9.5 off  0.1 | head/hair: blade  24.1 grip+hands  20.4 forearm  27.7 | paul  20.7 | legs  65.4 | tip y  2.15 | clear L_UpperArm  3.0 | elbow R  78 L  82 | hipext L -51 R   6 | toe y L0.117 R0.036 ank L0.173 | gaze [-0.   -0.06 -1.  ] d [ 0.15  0.9  -0.4 ] drift L9.8 R0.0
    attack 0.53 span   9.4 off  0.1 | head/hair: blade  39.4 grip+hands  25.7 forearm  23.3 | paul  20.3 | legs  66.7 | tip y  1.70 | clear L_UpperArm  2.2 | elbow R 114 L 108 | hipext L -53 R  15 | toe y L0.091 R0.036 ank L0.147 | gaze [-0.   -0.06 -1.  ] d [-0.08  0.5  -0.86] drift L8.8 R0.0
    attack 0.56 span   9.1 off  0.3 | head/hair: blade  50.0 grip+hands  29.9 forearm  11.0 | paul  22.0 | legs  64.7 | tip y  1.03 | clear L_UpperArm -2.1 | elbow R 158 L 153 | hipext L -50 R  22 | toe y L0.057 R0.040 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.3  -0.1  -0.95] drift L7.4 R0.5
    attack 0.59 span   8.3 off  0.9 | head/hair: blade  53.0 grip+hands  32.1 forearm  14.9 | paul  22.3 | legs  54.4 | tip y  0.67 | clear L_UpperArm -1.0 | elbow R 168 L 164 | hipext L -52 R  23 | toe y L0.054 R0.042 ank L0.110 | gaze [ 0.   -0.06 -1.  ] d [-0.41 -0.36 -0.84] drift L1.0 R0.2
    attack 0.62 span   9.2 off  0.3 | head/hair: blade  54.8 grip+hands  33.8 forearm  20.6 | paul  25.5 | legs  39.4 | tip y  0.39 | clear R_UpperArm -1.2 | elbow R 167 L 130 | hipext L -54 R  23 | toe y L0.057 R0.039 ank L0.113 | gaze [-0.   -0.06 -1.  ] d [-0.52 -0.58 -0.63] drift L1.0 R0.3
    attack 0.65 span   9.4 off  0.2 | head/hair: blade  45.7 grip+hands  34.2 forearm  22.5 | paul  30.1 | legs  21.2 | tip y  0.14 | clear R_UpperArm -4.0 | elbow R 167 L 114 | hipext L -54 R  23 | toe y L0.057 R0.040 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.63 -0.77 -0.02] drift L0.0 R0.0
    attack 0.68 span   9.5 off  0.2 | head/hair: blade  39.0 grip+hands  33.1 forearm  21.6 | paul  37.1 | legs  11.8 | tip y  0.17 | clear R_UpperArm -5.9 | elbow R 168 L 119 | hipext L -54 R  23 | toe y L0.057 R0.040 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.55 -0.7   0.45] drift L0.0 R0.0
    attack 0.78 span   9.5 off  0.3 | head/hair: blade  38.1 grip+hands  35.1 forearm  21.8 | paul  38.0 | legs  11.1 | tip y  0.23 | clear R_UpperArm -6.4 | elbow R 168 L 131 | hipext L -52 R  23 | toe y L0.057 R0.041 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.53 -0.64  0.56] drift L0.0 R0.1
    attack 0.90 span   9.5 off  0.2 | head/hair: blade  39.9 grip+hands  37.1 forearm  23.1 | paul  37.6 | legs  13.6 | tip y  0.25 | clear R_UpperArm -5.9 | elbow R 168 L 133 | hipext L -49 R  23 | toe y L0.057 R0.042 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.57 -0.64  0.52] drift L0.0 R0.1
    attack 1.05 span   9.5 off  0.2 | head/hair: blade  39.9 grip+hands  37.1 forearm  23.1 | paul  37.6 | legs  13.6 | tip y  0.25 | clear R_UpperArm -5.9 | elbow R 168 L 133 | hipext L -49 R  23 | toe y L0.057 R0.042 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.57 -0.64  0.52] drift L0.0 R0.0
    attack 1.15 span   9.5 off  0.2 | head/hair: blade  46.1 grip+hands  34.1 forearm  26.1 | paul  34.5 | legs  21.8 | tip y  0.41 | clear R_UpperArm -6.2 | elbow R 168 L 110 | hipext L -52 R  13 | toe y L0.077 R0.036 ank L0.133 | gaze [-0.   -0.06 -1.  ] d [-0.85 -0.51 -0.16] drift L6.6 R0.6
    attack 1.25 span   9.4 off  0.2 | head/hair: blade  52.3 grip+hands  37.0 forearm  26.2 | paul  35.5 | legs  32.4 | tip y  1.02 | clear R_UpperArm -6.4 | elbow R 170 L 115 | hipext L -50 R   0 | toe y L0.097 R0.036 ank L0.153 | gaze [-0.   -0.06 -1.  ] d [-0.47  0.1  -0.88] drift L9.9 R0.0
    attack 1.35 span   9.5 off  0.1 | head/hair: blade  50.7 grip+hands  38.8 forearm  26.9 | paul  35.5 | legs  36.3 | tip y  1.19 | clear R_UpperArm -5.6 | elbow R 169 L 115 | hipext L -43 R  -8 | toe y L0.083 R0.036 ank L0.139 | gaze [-0.   -0.06 -1.  ] d [-0.25  0.27 -0.93] drift L7.5 R0.0
    attack 1.42 span   9.5 off  0.2 | head/hair: blade  47.8 grip+hands  38.2 forearm  26.6 | paul  34.2 | legs  37.8 | tip y  1.29 | clear R_UpperArm -2.5 | elbow R 146 L 110 | hipext L -38 R -12 | toe y L0.067 R0.036 ank L0.123 | gaze [ 0.   -0.06 -1.  ] d [-0.12  0.36 -0.93] drift L4.7 R0.0
    attack 1.50 span   9.5 off  0.2 | head/hair: blade  45.8 grip+hands  37.6 forearm  26.1 | paul  33.2 | legs  38.5 | tip y  1.34 | clear R_UpperArm -1.3 | elbow R 135 L 106 | hipext L -35 R -14 | toe y L0.057 R0.036 ank L0.113 | gaze [ 0.   -0.06 -1.  ] d [-0.05  0.4  -0.91] drift L2.4 R0.0
    total 552s
    
    [exited with code 0]

user

+16:05
<task-notification> <task-id>bd9oiuzyh</task-id> <tool-use-id>toolu_01B3188WP6AKcQVKt68PwesN</tool-use-id> <output-file> [redacted: private path] /tasks/bd9oiuzyh.output</output-file> <status>completed</status> <summary>Background command "Run first solve and checks" completed (exit code 0)</summary> </task-notification>

agent

+16:24
  • Bashcd ~/Projects/lowmarch-fable52/review/out/whole-clips && head -3 [redacted: private path]/tasks/bd9oiuzyh.output && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 timeout 600 python3 - <<'EOF'

    command

    cd ~/Projects/lowmarch-fable52/review/out/whole-clips && head -3 [redacted: private path]/tasks/bd9oiuzyh.output && OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 timeout 600 python3 - <<'EOF'
    import time, sys, h
    v,shape=h.load()
    sys.path.insert(0,h.ROOT+'/tools/whole')
    import clips2h, rig as R, duel, numpy as np
    f=clips2h.Fighter(v,shape)
    n={'grip':0,'world':0}
    og=R.Rig.grip; ow=R.Rig.world
    def g(self,*a,**k): n['grip']+=1; return og(self,*a,**k)
    def w(self,*a,**k): n['world']+=1; return ow(self,*a,**k)
    R.Rig.grip=g; R.Rig.world=w
    for t in (0.0,0.32,0.56,0.68):
        n.update(grip=0,world=0); t0=time.time()
        f.solve(clips2h.hermite(clips2h.ATTACK,t)); print(t, n, round(time.time()-t0,2))
    t0=time.time(); 
    for i in range(20): ow(f.rig,{})
    print('world', (time.time()-t0)/20)
    import cProfile, pstats
    cProfile.run('f.solve(clips2h.hermite(clips2h.ATTACK,0.0))','[redacted: private path]/scratchpad/prof')
    pstats.Stats('[redacted: private path]/scratchpad/prof').sort_stats('cumtime').print_stats(14)
    EOF

    result, 30 lines

      ! wren, idle at 2.00 s: right upper arm 1.3 cm inside the body
      ! wren, attack at 0.00 s: right upper arm 1.3 cm inside the body
      ! wren, attack at 0.56 s: left upper arm 2.1 cm inside the body
    0.0 {'grip': 9, 'world': 1294} 9.57
    0.32 {'grip': 6, 'world': 502} 3.85
    0.56 {'grip': 7, 'world': 1596} 14.35
    0.68 {'grip': 12, 'world': 2251} 26.86
    world 0.006126523017883301
    Tue Nov 10 19:27:12 2026    [redacted: private path]/scratchpad/prof
    
             14309838 function calls (14042190 primitive calls) in 15.667 seconds
    
       Ordered by: cumulative time
       List reduced from 189 to 14 due to restriction <14>
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000   15.667   15.667 {built-in method builtins.exec}
            1    0.000    0.000   15.667   15.667 <string>:1(<module>)
            1    0.000    0.000   15.666   15.666 ~/Projects/lowmarch-fable52/tools/whole/clips2h.py:143(solve)
            2    0.000    0.000   15.359    7.679 ~/Projects/lowmarch-fable52/tools/duel.py:96(clear_grip)
         1294    0.005    0.000   15.306    0.012 <stdin>:9(w)
         1294    0.011    0.000   15.301    0.012 ~/Projects/lowmarch-fable52/tools/rig.py:37(world)
            9    0.000    0.000   15.142    1.682 <stdin>:8(g)
            9    0.010    0.001   15.142    1.682 ~/Projects/lowmarch-fable52/tools/rig.py:120(grip)
         1303    0.569    0.000   11.477    0.009 ~/Projects/lowmarch-fable52/tools/vroid_portrait.py:173(bone_pose)
       160269    0.313    0.000    8.825    0.000 [redacted: private path]/numpy/core/numeric.py:2170(allclose)
       160271    1.127    0.000    7.488    0.000 [redacted: private path]/numpy/core/numeric.py:2249(isclose)
          238    0.007    0.000    5.622    0.024 ~/Projects/lowmarch-fable52/tools/rig.py:114(held)
          348    0.014    0.000    4.113    0.012 ~/Projects/lowmarch-fable52/tools/rig.py:43(rotate_world)
       160271    1.322    0.000    4.045    0.000 [redacted: private path]/numpy/core/numeric.py:2330(within_tol)
  • Bashcd ~/Projects/lowmarch-fable52 && sed -n 165,260p tools/vroid_portrait.py; grep -n "def node_world" -A25 tools/vrm.py; grep -n "def local_matrix" -A20 tools/vrm.py

    command

    cd ~/Projects/lowmarch-fable52 && sed -n 165,260p tools/vroid_portrait.py; grep -n "def node_world" -A25 tools/vrm.py; grep -n "def local_matrix" -A20 tools/vrm.py

    result, 143 lines

                   nose=0.0, nose_w=0.0,
                   eye=1.0, eye_tilt=0.0, iris=1.0, lash=1.0, mouth=1.0, arm_out=0.0, age_lines=0.0, blush=1.0, curl=0.0,
                   face_normals=1.0, hair_normals=0.0, shadow_cards=1.0, fangs=1.0, rest={})
    # hair is reshaped with the face: beards (built on the face before the face is sculpted) and side locks
    # must follow a wider jaw or a longer chin, or the skin comes through them
    SCULPT_ROLES = ("face", "body", "mouth", "brow", "line", "white", "iris", "highlight", "hair")
    
    
    def bone_pose(v, sh, base_pose):
        """Pose dict (bone -> 3x3) = base rotations combined with proportion scales. Each scaled bone
        resets its children to scale 1 except the head, whose children (hair bones) grow with it."""
        # Body proportions (all 1.0 = VRoid's): shoulders lengthens the clavicles (wider shoulders), arms
        # and legs thicken the limbs, torso broadens the waist-to-chest, leg_len lengthens thigh and shin
        # (the hips are lifted to keep the feet on the ground), hands scales the hands.
        a, lg, t = sh["arms"], sh["legs"], sh["torso"]
        spec = {"J_Bip_C_Head": ((sh["head"],) * 3, True),
                "J_Bip_C_Neck": ((sh["neck"], 1.0, sh["neck"]), False),
                "J_Bip_C_UpperChest": ((sh["chest"], 1.0, 1 + (sh["chest"] - 1) * 0.6), False),
                "J_Bip_C_Spine": ((1 + (t - 1) * 0.7, 1.0, 1 + (t - 1) * 0.5), False),
                "J_Bip_C_Chest": ((t, 1.0, 1 + (t - 1) * 0.7), False)}
        for side in "LR":
            spec[f"J_Bip_{side}_Shoulder"] = ((sh["shoulders"], 1.0, 1.0), False)
            for b in ("UpperArm", "LowerArm"):
                spec[f"J_Bip_{side}_{b}"] = ((1.0, a, a), False)          # arm bones run along x
            for b in ("UpperLeg", "LowerLeg"):
                spec[f"J_Bip_{side}_{b}"] = ((lg, sh["leg_len"], lg), False)  # leg bones run along y
            spec[f"J_Bip_{side}_Hand"] = ((sh["hands"],) * 3, True)
        spec = {k: v for k, v in spec.items() if not np.allclose(v[0], 1.0)}
        nodes = v.j["nodes"]
        parent = {c: i for i, n in enumerate(nodes) for c in n.get("children", [])}
        own = {}
    
        def own_of(i):
            if i in own:
                return own[i]
            nm = nodes[i].get("name")
            if nm in spec:
                own[i] = np.array(spec[nm][0], float)
            elif i in parent and nodes[parent[i]].get("name") in spec and spec[nodes[parent[i]]["name"]][1]:
                own[i] = own_of(parent[i])
            elif i in parent and (own_of(parent[i]) != 1).any() and nodes[parent[i]].get("name") not in spec:
                own[i] = own_of(parent[i])          # inherited chain (below the head)
            else:
                own[i] = np.ones(3)
            return own[i]
    
        def as4(r):
            r = np.asarray(r, float)
            if r.shape == (4, 4):
                return r
            m = np.eye(4)
            m[:3, :3] = r
            return m
        pose = {k: as4(r) for k, r in base_pose.items()}
        for i, n in enumerate(nodes):
            nm = n.get("name")
            if not nm:
                continue
            mine = own_of(i)
            par = own_of(parent[i]) if i in parent else np.ones(3)
            adj = mine / par
            if np.allclose(adj, 1):
                continue
            pose[nm] = pose.get(nm, np.eye(4)) @ np.diag(list(adj) + [1.0])
        if sh["shoulder_w"]:                        # wider shoulders by moving the arm joints out (no shear, unlike
            for side, sx in (("L", -1.0), ("R", 1.0)):  # `shoulders`, which scales the clavicle and skews a turned arm)
                mv = np.eye(4)
                mv[0, 3] = sx * sh["shoulder_w"]
                nm = f"J_Bip_{side}_UpperArm"
                pose[nm] = mv @ pose.get(nm, np.eye(4))
        if sh.get("curl"):                          # relaxed hands (degrees per finger joint) wherever the pose
            for side, sg in (("L", 1.0), ("R", -1.0)):  # leaves the fingers alone: an open hand reads as a paddle
                for f in ("Index", "Middle", "Ring", "Little"):
                    for j, kf in ((1, 0.6), (2, 1.0), (3, 0.8)):
                        nm = f"J_Bip_{side}_{f}{j}"
                        if nm in base_pose:
                            continue
                        a = np.radians(sg * sh["curl"] * kf * (1.0 + 0.12 * ("Index", "Middle", "Ring", "Little").index(f)))
                        r = np.eye(4)
                        r[:2, :2] = [[np.cos(a), -np.sin(a)], [np.sin(a), np.cos(a)]]
                        pose[nm] = pose.get(nm, np.eye(4)) @ r
        if sh.get("arm_out"):                       # arms held further from the body (degrees): a broad torso
            for side, sg in (("L", -1.0), ("R", 1.0)):  # needs room for hanging arms
                a = np.radians(sg * sh["arm_out"])
                r = np.eye(4)
                r[:2, :2] = [[np.cos(a), -np.sin(a)], [np.sin(a), np.cos(a)]]
                nm = f"J_Bip_{side}_UpperArm"
                pose[nm] = r @ pose.get(nm, np.eye(4))
        if sh["leg_len"] != 1.0:                    # longer legs: lift the hips so the feet stay on the ground
            W = v.node_world()
            names = v.nodes_by_name()
            leg = W[names["J_Bip_L_UpperLeg"]][1, 3] - W[names["J_Bip_L_Foot"]][1, 3]
            lift = np.eye(4)
            lift[1, 3] = (sh["leg_len"] - 1.0) * leg
            pose["J_Bip_C_Hips"] = lift @ pose.get("J_Bip_C_Hips", np.eye(4))
        if sh["height"] != 1.0:                     # the whole figure scaled from the hips (everything below them
    102:    def node_world(self, pose=None):
    103-        """World matrix of every node. pose: {node name: 3x3 rotation/scale or 4x4 adjustment
    104-        applied in the node's own frame on top of its rest local transform}."""
    105-        pose = pose or {}
    106-        nodes = self.j["nodes"]
    107-        parent = {}
    108-        for i, n in enumerate(nodes):
    109-            for c in n.get("children", []):
    110-                parent[c] = i
    111-        out = {}
    112-
    113-        def world(i):
    114-            if i not in out:
    115-                m = self.local_matrix(i, pose)
    116-                out[i] = world(parent[i]) @ m if i in parent else m
    117-            return out[i]
    118-
    119-        for i in range(len(nodes)):
    120-            world(i)
    121-        return out
    122-
    123-    # ---- geometry ----------------------------------------------------------------
    124-    def skin_matrices(self, pose):
    125-        """Per skin: (J,4,4) joint matrices = posed world @ inverse bind."""
    126-        rest, posed = self.node_world(), self.node_world(pose)
    127-        out = []
    74:    def local_matrix(self, i, pose=None):
    75-        """Local matrix of node i with an optional pose adjustment applied in the node's own frame:
    76-        a 3x3 (rotation / scale) or a 4x4 (with a translation offset)."""
    77-        n = self.j["nodes"][i]
    78-        if "matrix" in n:
    79-            m = np.array(n["matrix"], float).reshape(4, 4).T
    80-        else:
    81-            t = np.eye(4)
    82-            t[:3, 3] = n.get("translation", [0, 0, 0])
    83-            x, y, z, w = n.get("rotation", [0, 0, 0, 1])
    84-            r = np.eye(4)
    85-            r[:3, :3] = [[1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)],
    86-                         [2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)],
    87-                         [2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y)]]
    88-            m = t @ r @ np.diag(list(n.get("scale", [1, 1, 1])) + [1])
    89-        adj = (pose or {}).get(n.get("name"))
    90-        if adj is not None:
    91-            adj = np.asarray(adj, float)
    92-            if adj.shape == (3, 3):
    93-                a4 = np.eye(4)
    94-                a4[:3, :3] = adj
  • Bashcd ~/Projects/lowmarch-fable52 && sed -n 260,285p tools/vroid_portrait.py; cd review/out/whole-clips && python3 -c "

    command

    cd ~/Projects/lowmarch-fable52 && sed -n 260,285p tools/vroid_portrait.py; cd review/out/whole-clips && python3 -c "
    import pickle; print(pickle.load(open('shape.pkl','rb')))"

    result, 27 lines

        if sh["height"] != 1.0:                     # the whole figure scaled from the hips (everything below them
            W = v.node_world()                      # grows with them), lifted so the soles stay on the ground
            names = v.nodes_by_name()
            hip = W[names["J_Bip_C_Hips"]][1, 3] + (sh["leg_len"] - 1.0) * (
                W[names["J_Bip_L_UpperLeg"]][1, 3] - W[names["J_Bip_L_Foot"]][1, 3])
            if getattr(v, "_sole", None) is None:   # (reading every mesh is slow; this runs for every pose)
                v._sole = min(float(p_["pos"][:, 1].min()) for p_ in v.primitives() if len(p_["pos"]))
            sole = v._sole
            lift = np.eye(4)
            lift[1, 3] = (sh["height"] - 1.0) * (hip - sole)
            pose["J_Bip_C_Hips"] = lift @ pose.get("J_Bip_C_Hips", np.eye(4)) @ np.diag([sh["height"]] * 3 + [1.0])
        return pose
    
    
    def landmarks(prims, roles):
        def verts(*rs):
            return np.concatenate([p["pos"][np.unique(p["tri"])] for p, r in zip(prims, roles) if r in rs])
        iris = verts("iris")
        face = verts("face")
        body = verts("body")
        mid = face[np.abs(face[:, 0]) < 0.006]
        chin = mid[np.argmin(mid[:, 1])]
        eyeL, eyeR = iris[iris[:, 0] < 0].mean(0), iris[iris[:, 0] > 0].mean(0)
        eye_y = (eyeL[1] + eyeR[1]) / 2
        span = eye_y - chin[1]
        mouth = verts("mouth").mean(0)
    {'hands': 1.0, 'rest': {'Fcl_BRW_Angry': 0.3}, 'arm_out': 4.0}
  • Bashcd ~/Projects/lowmarch-fable52 && sed -n 94,101p tools/vrm.py; grep -n "^NEUTRAL" -A3 tools/vroid_portrait.py | head

    command

    cd ~/Projects/lowmarch-fable52 && sed -n 94,101p tools/vrm.py; grep -n "^NEUTRAL" -A3 tools/vroid_portrait.py | head

    result, 12 lines

                    a4[:3, :3] = adj
                    adj = a4
                m = m @ adj
            return m
    
        def parents(self):
            return {c: i for i, n in enumerate(self.j["nodes"]) for c in n.get("children", [])}
    
    162:NEUTRAL = dict(head=1.0, neck=1.0, chest=1.0, height=1.0,
    163-               shoulders=1.0, arms=1.0, torso=1.0, legs=1.0, leg_len=1.0, hands=1.0, shoulder_w=0.0,
    164-               face_len=0.0, jaw=0.0, chin_w=0.0, chin_len=0.0, cheek=0.0, brow_ridge=0.0,
    165-               nose=0.0, nose_w=0.0,

agent

+18:24

Now add a fast world with a self-check, a cheaper grip, and fix the arm problems.

  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    - class Fighter:
    -     def __init__(self, vrm, shape=None):
    -         from checks import Body
    -         self.rig = Rig(vrm, shape, ortho=True)
    + class FastRig(Rig):
    +     """Rig.world without vroid_portrait.bone_pose's walk over every node per call (most of a solve's time):
    +     where a shape only adds a fixed adjustment before each bone's pose (arm_out, shoulder_w; no scales or
    +     curl), the posed matrices are that adjustment times the pose, over cached rest matrices. Checked against
    +     the slow path on two poses; any mismatch keeps the slow path."""
    + 
    +     def __init__(self, vrm, shape=None):
    +         super().__init__(vrm, shape, ortho=True)
    +         nodes = vrm.j["nodes"]
    +         self._rest = [vrm.local_matrix(i, None) for i in range(len(nodes))]
    +         self._name = [n.get("name") for n in nodes]
    +         order, seen = [], set()
    + 
    +         def visit(i):
    +             if i in seen:
    +                 return
    +             if i in self._par:
    +                 visit(self._par[i])
    +             seen.add(i)
    +             order.append(i)
    +         for i in range(len(nodes)):
    +             visit(i)
    +         self._order = order
    +         self._pre = {k: P.as4(m) for k, m in self._bp(vrm, self.shape, {}).items()}
    +         self.fast = False
    +         probe = [P.BATTLE_IDLE, P.merge(P.BUST, {"J_Bip_C_Hips": P.move(0.01, -0.05, 0.02, P.rot((0, 1, 0), 20)),
    +                                                  "J_Bip_R_LowerArm": P.rot((1, 1, 0), 40)})]
    +         slow = [Rig.world(self, p) for p in probe]
    +         self.fast = True
    +         self.fast = all(max(np.abs(a[i] - b[i]).max() for i in a) < 1e-9
    +                         for a, b in zip(slow, (self.world(p) for p in probe)))
    + 
    +     def world(self, pose):
    +         if not self.fast:
    +             return Rig.world(self, pose)
    +         out = {}
    +         for i in self._order:
    +             nm = self._name[i]
    +             m = self._rest[i]
    +             adj = pose.get(nm)
    +             pre = self._pre.get(nm)
    +             if adj is not None:
    +                 adj = P.as4(adj)
    +                 m = m @ (pre @ adj if pre is not None else adj)
    +             elif pre is not None:
    +                 m = m @ pre
    +             out[i] = out[self._par[i]] @ m if i in self._par else m
    +         return out
    + 
    + 
    + def grip(rig, pose, side, ref, fist, D, pole, local=(0, 0, -1), iters=4):
    +     """rig.grip in a few world solves: aim the hand, then move the wrist by the fist's miss and re-solve the arm
    +     (analytic IK), a few rounds; the hand's aim is restored after each (the arm's turn carries it off)."""
    +     hd = f"J_Bip_{side}_Hand"
    +     T = np.asarray(fist, float)
    +     fr = ref[f"fist_{side}"]
    +     W0 = rig.world(P.BUST)[rig.names[hd]]
    +     off0 = fr - W0[:3, 3]                      # the fist from the wrist, in the reference pose
    +     R0 = W0[:3, :3]
    +     wrist = T - (rig.held(pose, hd, P.BUST, fr) - rig.pos(pose, hd))
    +     for _ in range(iters):
    +         pose = rig.arm_ik(pose, side, wrist, pole)
    +         W = rig.world(pose)[rig.names[hd]]
    +         pose = rig.rotate_world(pose, hd, rot_between(W[:3, :3] @ np.asarray(local, float), D))
    +         W = rig.world(pose)[rig.names[hd]]
    +         f = W[:3, :3] @ np.linalg.solve(R0, off0) + W[:3, 3]
    +         miss = T - f
    +         if np.linalg.norm(miss) < 1.5e-3:
    +             break
    +         wrist = W[:3, 3] + miss
    +     return pose
    + 
    + 
    + def clear_grip(rig, body, pose, side, ref, fist, D, poles, need=0.012):
    +     """grip, trying elbow directions in turn until the arm clears the body by `need` (else the clearest)."""
    +     limbs = [f"{side}_UpperArm", f"{side}_LowerArm", f"{side}_Hand"]
    +     best = None
    +     for pole in poles:
    +         p = grip(rig, pose, side, ref, fist, D, pole)
    +         cl = body.worst(p, limbs)
    +         if cl >= need:
    +             return p
    +         if best is None or cl > best[0]:
    +             best = (cl, p)
    +     return best[1]
    + 
    + 
    + class Fighter:
    +     def __init__(self, vrm, shape=None):
    +         from checks import Body
    +         self.rig = FastRig(vrm, shape)

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    -     def solve(self, k):
    -         from duel import clear_grip
    -         rig = self.rig
    +     def solve(self, k):
    +         rig = self.rig

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    -         pose = clear_grip(rig, self.body, pose, "R", self.ref, fr, D, tuple(k["poleR"]))
    -         a, d = self.sword(pose)
    -         pose = clear_grip(rig, self.body, pose, "L", self.ref, a - d * SPAN, d, tuple(k["poleL"]))
    +         pose = clear_grip(rig, self.body, pose, "R", self.ref, fr, D, [k["poleR"]] + POLES)
    +         a, d = self.sword(pose)
    +         mirror = np.array([-1.0, 1.0, 1.0])
    +         pose = clear_grip(rig, self.body, pose, "L", self.ref, a - d * SPAN, d,
    +                           [k["poleL"]] + [tuple(np.asarray(q) * mirror) for q in POLES])

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)
  • Edit~/Projects/lowmarch-fable52/tools/whole/clips2h.py

    input

    ~/Projects/lowmarch-fable52/tools/whole/clips2h.py
    - IDLE_LOOP, IDLE_STEP, BLADE_LAG = 2.0, 0.2, 0.1
    + IDLE_LOOP, IDLE_STEP, BLADE_LAG = 2.0, 0.2, 0.1
    + # elbow directions tried after a key's own when its arm meets the body (right arm; mirrored for the left)
    + POLES = [(1.0, -0.5, 0.0), (0.6, -1.0, -0.4), (1.0, -0.2, 0.4)]

    result, 1 line

    The file ~/Projects/lowmarch-fable52/tools/whole/clips2h.py has been updated successfully. (file state is current in your context — no need to Read it back)