A 3D model pipeline, pushed by Gemini 4 Argon
agent (cont.)
+3:19read_file{"EndLine":300,"Path":"~/work/lowmarch/tools/rig.py","StartLine":1}
input
{ "EndLine": 300, "Path": "~/work/lowmarch/tools/rig.py", "StartLine": 1 }result, 191 lines
Showing lines 1-190 of 190 1: """Small rig tools for authoring poses on a VRoid skeleton: rotate a bone in world space, two-bone 2: leg and arm IK, aiming a held prop (the sword) at a direction, and gripping a sword so its blade 3: runs through a given point. 4: 5: Poses here are the same dicts as tools/poses.py (bone name -> 3x3 or 4x4 adjustment in the bone's 6: own frame). Everything is solved in model space (VRoid: the character faces -z, y up). 7: """ 8: import numpy as np 9: 10: from poses import as4 11: 12: 13: def rot_between(a, b): 14: a = np.asarray(a, float) / np.linalg.norm(a) 15: b = np.asarray(b, float) / np.linalg.norm(b) 16: v, c = np.cross(a, b), float(np.dot(a, b)) 17: if c < -0.999999: 18: axis = np.cross(a, [1, 0, 0]) if abs(a[0]) < 0.9 else np.cross(a, [0, 1, 0]) 19: axis /= np.linalg.norm(axis) 20: return 2 * np.outer(axis, axis) - np.eye(3) 21: vx = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]]) 22: return np.eye(3) + vx + vx @ vx / (1 + c) 23: 24: 25: class Rig: 26: def __init__(self, vrm, shape=None, ortho=False): 27: """ortho: rotate bones through the parent's pure rotation (exact with scaled bones, see 28: rotate_world). Off by default so clips authored before it (Aldric's, frozen) stay identical.""" 29: import vroid_portrait as vp 30: self.v = vrm 31: self.shape = dict(vp.NEUTRAL, **(shape or {})) 32: self.names = vrm.nodes_by_name() 33: self._bp = vp.bone_pose 34: self.ortho = ortho 35: self._par = vrm.parents() 36: 37: def world(self, pose): 38: return self.v.node_world(self._bp(self.v, self.shape, pose)) 39: 40: def pos(self, pose, bone): 41: return self.world(pose)[self.names[bone]][:3, 3] 42: 43: def rotate_world(self, pose, bone, R): 44: """Pre-multiply the bone's world rotation by R (about its joint) by editing its local pose. 45: With proportion scales on the skeleton the bone's world matrix is not a rotation; the 46: default (legacy) form then leaks shear into the pose and an iterated solve can diverge. 47: ortho mode conjugates by the pure rotation of the frame the adjustment lives in.""" 48: W = self.world(pose) 49: i = self.names[bone] 50: cur = W[i][:3, :3] 51: adj_old = as4(pose.get(bone, np.eye(3))) 52: if self.ortho: 53: par = self._par.get(i) 54: base = (W[par] if par is not None else np.eye(4)) @ self.v.local_matrix(i, None) 55: u, _, vt = np.linalg.svd(base[:3, :3]) 56: Q = u @ vt # nearest rotation 57: out = dict(pose) 58: a = adj_old.copy() 59: a[:3, :3] = Q.T @ R @ Q @ adj_old[:3, :3] 60: out[bone] = a 61: return out 62: base = cur @ np.linalg.inv(adj_old[:3, :3]) # world rotation without this bone's adjustment 63: new_rot = np.linalg.inv(base) @ (R @ cur) 64: out = dict(pose) 65: a = adj_old.copy() 66: a[:3, :3] = new_rot 67: out[bone] = a 68: return out 69: 70: def leg_ik(self, pose, side, target, pole=(0, 0, -1)): 71: """Bend the leg so the ankle lands on `target`, knee toward `pole` (forward).""" 72: up, lo, ft = f"J_Bip_{side}_UpperLeg", f"J_Bip_{side}_LowerLeg", f"J_Bip_{side}_Foot" 73: H, K, A = self.pos(pose, up), self.pos(pose, lo), self.pos(pose, ft) 74: l1, l2 = np.linalg.norm(K - H), np.linalg.norm(A - K) 75: T = np.asarray(target, float) 76: d = np.clip(np.linalg.norm(T - H), 1e-4, l1 + l2 - 1e-4) 77: u = (T - H) / np.linalg.norm(T - H) 78: w = np.asarray(pole, float) - u * np.dot(pole, u) 79: w /= np.linalg.norm(w) 80: ca = np.clip((l1 * l1 + d * d - l2 * l2) / (2 * l1 * d), -1, 1) 81: K2 = H + l1 * (ca * u + np.sqrt(1 - ca * ca) * w) 82: pose = self.rotate_world(pose, up, rot_between(K - H, K2 - H)) 83: K, A = self.pos(pose, lo), self.pos(pose, ft) 84: return self.rotate_world(pose, lo, rot_between(A - K, H + u * d - K)) 85: 86: def plant_feet(self, pose, reference): 87: """Keep both ankles where they are in `reference` (feet stay on the ground while the hips move).""" 88: for side in "LR": 89: pose = self.leg_ik(pose, side, self.pos(reference, f"J_Bip_{side}_Foot")) 90: return pose 91: 92: def aim(self, pose, bone, local_dir, target_dir): 93: """Turn `bone` so its local direction (e.g. the sword blade, hand -z) points along target_dir.""" 94: W = self.world(pose)[self.names[bone]][:3, :3] 95: cur = W @ np.asarray(local_dir, float) 96: return self.rotate_world(pose, bone, rot_between(cur, target_dir)) 97: 98: def arm_ik(self, pose, side, target, pole): 99: """Bend the arm so the wrist lands on `target`, elbow toward `pole`.""" 100: up, lo, hd = f"J_Bip_{side}_UpperArm", f"J_Bip_{side}_LowerArm", f"J_Bip_{side}_Hand" 101: S, E, W = self.pos(pose, up), self.pos(pose, lo), self.pos(pose, hd) 102: l1, l2 = np.linalg.norm(E - S), np.linalg.norm(W - E) 103: T = np.asarray(target, float) 104: d = np.clip(np.linalg.norm(T - S), abs(l1 - l2) + 1e-3, l1 + l2 - 1e-4) 105: u = (T - S) / np.linalg.norm(T - S) 106: w = np.asarray(pole, float) - u * np.dot(pole, u) 107: w /= np.linalg.norm(w) 108: ca = np.clip((l1 * l1 + d * d - l2 * l2) / (2 * l1 * d), -1, 1) 109: E2 = S + l1 * (ca * u + np.sqrt(1 - ca * ca) * w) 110: pose = self.rotate_world(pose, up, rot_between(E - S, E2 - S)) 111: E, W = self.pos(pose, lo), self.pos(pose, hd) 112: return self.rotate_world(pose, lo, rot_between(W - E, S + u * d - E)) 113: 114: def held(self, pose, bone, ref_pose, point): 115: """Where a point attached to `bone` (given in the reference pose, model space) is in `pose`.""" 116: i = self.names[bone] 117: M = self.world(pose)[i] @ np.linalg.inv(self.world(ref_pose)[i]) 118: return M[:3, :3] @ np.asarray(point, float) + M[:3, 3] 119: 120: def grip(self, pose, side, ref_pose, fist_ref, fist_target, blade_dir, pole, local_blade=(0, 0, -1), iters=14): 121: """Put the fist (a point on the hand, from the reference pose) on `fist_target` with the hand's 122: `local_blade` axis along `blade_dir`. An analytic arm IK gets close (elbow toward `pole`); 123: then cyclic corrections that measure the real fist after every step (aim the hand, swing 124: the forearm, swing the upper arm) finish it, which converges even when proportion scales 125: shear the arm and the analytic solve alone undershoots.""" 126: hd, lo, up = f"J_Bip_{side}_Hand", f"J_Bip_{side}_LowerArm", f"J_Bip_{side}_UpperArm" 127: T = np.asarray(fist_target, float) 128: D = np.asarray(blade_dir, float) / np.linalg.norm(blade_dir) 129: wrist = T - (self.held(pose, hd, ref_pose, fist_ref) - self.pos(pose, hd)) 130: pose = self.arm_ik(pose, side, wrist, pole) 131: for _ in range(iters): 132: for _ in range(2): 133: M = self.world(pose)[self.names[hd]][:3, :3] 134: pose = self.rotate_world(pose, hd, rot_between(M @ np.asarray(local_blade, float), D)) 135: for bone in (lo, up): 136: j = self.pos(pose, bone) 137: f = self.held(pose, hd, ref_pose, fist_ref) 138: if np.linalg.norm(f - j) > 1e-4 and np.linalg.norm(T - j) > 1e-4: 139: pose = self.rotate_world(pose, bone, rot_between(f - j, T - j)) 140: miss = np.linalg.norm(T - self.held(pose, hd, ref_pose, fist_ref)) 141: M = self.world(pose)[self.names[hd]][:3, :3] 142: bd = M @ np.asarray(local_blade, float) 143: if miss < 2e-3 and np.dot(bd / np.linalg.norm(bd), D) > 0.9995: 144: break 145: for _ in range(2): # the blade direction is what reads; finish on it 146: M = self.world(pose)[self.names[hd]][:3, :3] 147: pose = self.rotate_world(pose, hd, rot_between(M @ np.asarray(local_blade, float), D)) 148: return pose 149: 150: 151: def grip_points(vrm, shape, ref_pose, fingers=("Index", "Middle", "Ring", "Little")): 152: """Where each fist closes: the centre of the hole the curled fingers make (poses.fist), in model 153: space in `ref_pose` (the hand doesn't move when the fingers curl, so a weapon built there and bound 154: to the hand sits in the fist whenever the fingers are curled). Each finger's joints and fingertip 155: (the last segment again, bent by its own joint) are fitted with a circle in the plane across the 156: hole (the hand's local z runs through it, palm to fingertips in x and y); the hole's centre is the 157: mean of the fingers' circle centres. A fixed offset from the wrist, tuned on one hand, missed on 158: larger or differently made hands (the shaft ran over the knuckles, the fingers closed under it). 159: Returns {side: dict(point, axis, radius)}: radius is the finger-centreline circle's (the shaft's 160: radius plus a finger's half-thickness fits).""" 161: import vroid_portrait as vp 162: import poses as P 163: sh = dict(vp.NEUTRAL, **(shape or {})) 164: names = vrm.nodes_by_name() 165: out = {} 166: for side in "LR": 167: sx = 1.0 if side == "R" else -1.0 168: W = vrm.node_world(vp.bone_pose(vrm, sh, P.merge(ref_pose, P.fist(side)))) 169: H = W[names[f"J_Bip_{side}_Hand"]] 170: R = H[:3, :3] / np.linalg.norm(H[:3, :3], axis=0) 171: o, e1, e2, ax = H[:3, 3], R[:, 0] * sx, R[:, 1], R[:, 2] 172: cs, axial, rads = [], [], [] 173: for f in fingers: 174: ks = [names.get(f"J_Bip_{side}_{f}{k}") for k in (1, 2, 3)] 175: if any(k is None for k in ks): 176: continue 177: js = [W[k][:3, 3] for k in ks] 178: M3 = W[ks[2]][:3, :3] / np.linalg.norm(W[ks[2]][:3, :3], axis=0) 179: pts = js + [js[2] + M3 @ np.array([sx, 0.0, 0.0]) * 0.85 * np.linalg.norm(js[2] - js[1])] 180: Q = np.array([[(p - o) @ e1, (p - o) @ e2] for p in pts]) 181: A = np.c_[2 * Q, np.ones(len(Q))] # algebraic circle fit 182: b = (Q ** 2).sum(1) 183: cx, cy, c0 = np.linalg.lstsq(A, b, rcond=None)[0] 184: cs.append((cx, cy)) 185: rads.append(float(np.sqrt(c0 + cx * cx + cy * cy))) 186: axial.append(float((js[0] - o) @ ax)) 187: c = np.mean(cs, 0) 188: out[side] = dict(point=o + c[0] * e1 + c[1] * e2 + np.mean(axial) * ax, axis=ax, radius=float(np.mean(rads)), 189: centres=[o + x * e1 + y * e2 + a * ax for (x, y), a in zip(cs, axial)]) 190: return out
agent
+3:25reasoning
arm_f_*.obj files it's an object loaded with load_obj(...).
2. It calls makehuman.apply_cage(...) with various settings: The cage is a simple cube in most cases with only 3x3x3 vertices. The settings are mostly about how many vertices in the "reference mesh" to process at a time.
3. Finally it calls makehuman.export_obj(...) with spec["ref"] (the MakeHuman body or an obj).
The primary function seems to apply a deformation to the provided mesh, but the details require more study. Initial hypothesis is that cage.py applies a pose to the arm model.
read_file{"EndLine":250,"Path":"~/work/lowmarch/tools/makehuman.py","StartLine":1}
input
{ "EndLine": 250, "Path": "~/work/lowmarch/tools/makehuman.py", "StartLine": 1 }result, 251 lines
Showing lines 1-250 of 935 1: """MakeHuman bodies as bases: CC0 assets (base mesh, morph targets, default rig and weights, from the 2: MPFB2 repository's data folder; see docs/makehuman.md) turned into a VRM-shaped glTF file that the 3: rest of the pipeline reads exactly like a VRoid model. 4: 5: The conversion: 6: 1. the base mesh (decimetres, facing +z) plus macro targets (gender, age, muscle, weight, height, 7: proportions; blended as MakeHuman blends them) and any named targets (face, body parts) 8: 2. joints from the morphed mesh's joint helpers (the default rig's head positions), weights from 9: the default rig, both mapped onto VRoid's bone names (J_Bip_*) 10: 3. straightened into VRoid's rest pose: arms horizontal along x with the palms down, legs straight 11: 4. turned to face -z, in metres, feet on the ground; every bone's rest rotation is identity, as 12: poses.py assumes 13: 5. split into the parts the pipeline names: body skin, the face mask, and stylised eyes (a white, 14: an iris cap with a painted iris, a highlight), the lash line, brows and a lip line, which the 15: painted renderer draws like VRoid's 16: Hair comes from the hair kit (tools/hair.py). 17: 18: With spec["head"] = a VRoid model (e.g. "base_male/Base_Male.vrm") the MakeHuman head is replaced by 19: that model's: face, eyes, brows, mouth, expression shapes, textures and hair, plus its skin from 20: mid-neck up, scaled to this body's head (spec["head_scale"] on top of the measured ratio) and 21: stitched to the MakeHuman neck (graft()). The body is MakeHuman's, the face stays anime. 22: 23: python3 -I tools/makehuman.py convert every cast entry with an `mh` spec (cached by spec) 24: """ 25: import site 26: import sys 27: 28: sys.path.append(site.getusersitepackages()) # run with -I (it reads downloaded data): numpy is in the user site 29: import collections # noqa: E402 30: import gzip 31: import hashlib # noqa: E402 32: import io # noqa: E402 33: import json # noqa: E402 34: import math # noqa: E402 35: import os # noqa: E402 36: import struct # noqa: E402 37: 38: import numpy as np # noqa: E402 39: from scipy.spatial import cKDTree # noqa: E402 40: 41: DATA = os.environ.get("LOWMARCH_MAKEHUMAN", os.path.expanduser("~/.local/share/lowmarch/makehuman/mpfb2/src/mpfb/data")) 42: VROID = os.environ.get("LOWMARCH_VROID", os.path.expanduser("~/.local/share/lowmarch/vroid")) 43: OUT = os.path.expanduser("~/.local/share/lowmarch/makehuman/out") 44: FLIP = np.array([-1.0, 1.0, -1.0]) 45: 46: # ---- the base mesh and targets ---------------------------------------------------------------------------- 47: _base = None 48: 49: 50: def base(): 51: global _base 52: if _base is None: 53: V, VT, groups, fuv, cur = [], [], collections.defaultdict(list), collections.defaultdict(list), None 54: for line in open(os.path.join(DATA, "3dobjs", "base.obj")): 55: if line.startswith("v "): 56: V.append([float(x) for x in line.split()[1:4]]) 57: elif line.startswith("vt "): 58: VT.append([float(x) for x in line.split()[1:3]]) 59: elif line.startswith("g "): 60: cur = line.split()[1] 61: elif line.startswith("f "): 62: tok = [t.split("/") for t in line.split()[1:]] 63: groups[cur].append([int(t[0]) - 1 for t in tok]) 64: fuv[cur].append([int(t[1]) - 1 if len(t) > 1 and t[1] else -1 for t in tok]) 65: _base = dict(V=np.array(V), VT=np.array(VT), groups=dict(groups), fuv=dict(fuv)) 66: return _base 67: 68: 69: _targets = {} 70: 71: 72: def target(rel): 73: """(indices, offsets) of a target file, rel like 'chin/chin-width-incr'.""" 74: if rel not in _targets: 75: path = os.path.join(DATA, "targets", rel + ".target.gz") 76: idx, d = [], [] 77: for line in gzip.open(path, "rt"): 78: p = line.split() 79: if len(p) == 4 and not line.startswith("#"): 80: idx.append(int(p[0])) 81: d.append([float(x) for x in p[1:]]) 82: _targets[rel] = (np.array(idx, int), np.array(d).reshape(-1, 3)) 83: return _targets[rel] 84: 85: 86: def _two(x, lo, mid, hi): 87: """Weights of a slider with three anchors (0, 0.5, 1): {name: weight}.""" 88: x = float(np.clip(x, 0, 1)) 89: return {lo: 1 - x * 2, mid: x * 2} if x < 0.5 else {mid: 2 - x * 2, hi: x * 2 - 1} 90: 91: 92: def macro_weights(g=0.5, age=0.5, muscle=0.5, weight=0.5, height=0.5, proportions=0.5, race=None, cup=0.5, firmness=0.5): 93: """The macro targets and their weights for these slider values (0..1, MakeHuman's scales). cup and firmness: 94: the breasts' size and firmness (MakeHuman's own breast macros, made for each sex, age, muscle and weight; 95: 0.5 is the base mesh's).""" 96: G = {"female": 1 - g, "male": g} 97: a = float(np.clip(age, 0, 1)) 98: if a < 0.1875: 99: A = {"baby": 1 - a / 0.1875, "child": a / 0.1875} 100: elif a < 0.5: 101: t = (a - 0.1875) / 0.3125 102: A = {"child": 1 - t, "young": t} 103: else: 104: t = (a - 0.5) / 0.5 105: A = {"young": 1 - t, "old": t} 106: M = _two(muscle, "minmuscle", "averagemuscle", "maxmuscle") 107: W = _two(weight, "minweight", "averageweight", "maxweight") 108: H = {"minheight": max(0.0, (0.5 - height) * 2), "maxheight": max(0.0, (height - 0.5) * 2)} 109: P = {"uncommonproportions": max(0.0, (0.5 - proportions) * 2), "idealproportions": max(0.0, (proportions - 0.5) * 2)} 110: R = race or {"african": 1 / 3, "asian": 1 / 3, "caucasian": 1 / 3} 111: C = _two(cup, "mincup", "averagecup", "maxcup") 112: F = _two(firmness, "minfirmness", "averagefirmness", "maxfirmness") 113: out = [] 114: for gn, gw in G.items(): 115: for an, aw in A.items(): 116: for rn, rw in R.items(): 117: if gw * aw * rw > 1e-4: 118: out.append((f"macrodetails/{rn}-{gn}-{an}", gw * aw * rw)) 119: for mn, mw in M.items(): 120: for wn, ww in W.items(): 121: base_w = gw * aw * mw * ww 122: if base_w < 1e-4: 123: continue 124: out.append((f"macrodetails/universal-{gn}-{an}-{mn}-{wn}", base_w)) 125: for hn, hw in H.items(): 126: if hw > 1e-4: 127: out.append((f"macrodetails/height/{gn}-{an}-{mn}-{wn}-{hn}", base_w * hw)) 128: for pn, pw in P.items(): 129: if pw > 1e-4: 130: out.append((f"macrodetails/proportions/{gn}-{an}-{mn}-{wn}-{pn}", base_w * pw)) 131: for cn, cw in C.items(): 132: for fn, fw in F.items(): 133: if cw * fw > 1e-4 and (cn, fn) != ("averagecup", "averagefirmness"): 134: out.append((f"breast/{gn}-{an}-{mn}-{wn}-{cn}-{fn}", base_w * cw * fw)) 135: return out 136: 137: 138: def morphed(spec): 139: """The base mesh's vertices (decimetres) with the spec's macros and targets applied. Named targets 140: take a value in -1..1 for a decr/incr pair ('chin/chin-width': 0.5), or 0..1 for a single target; 141: a name starting 'l-' is applied to both sides.""" 142: V = base()["V"].copy() 143: m = spec.get("macro", {}) 144: for rel, w in macro_weights(**m): 145: if os.path.exists(os.path.join(DATA, "targets", rel + ".target.gz")): 146: i, d = target(rel) 147: V[i] += w * d 148: for name, val in spec.get("targets", {}).items(): 149: names = [name, name.replace("l-", "r-", 1)] if os.path.basename(name).startswith("l-") else [name] 150: for nm in names: 151: for suffix, sgn in (("-incr", 1), ("-decr", -1), ("-up", 1), ("-down", -1), ("-out", 1), ("-in", -1)): 152: path = os.path.join(DATA, "targets", nm + suffix + ".target.gz") 153: if os.path.exists(path) and val * sgn > 0: 154: i, d = target(nm + suffix) 155: V[i] += abs(val) * d 156: break 157: else: 158: if os.path.exists(os.path.join(DATA, "targets", nm + ".target.gz")): 159: i, d = target(nm) 160: V[i] += val * d 161: return V 162: 163: 164: # ---- the rig: MakeHuman's default bones onto VRoid's names -------------------------------------------------- 165: FINGER = {1: "Thumb", 2: "Index", 3: "Middle", 4: "Ring", 5: "Little"} 166: 167: 168: def vroid_bone(mh): 169: """The VRoid bone a MakeHuman default-rig bone's weights go to.""" 170: side = "L" if mh.endswith(".L") else "R" if mh.endswith(".R") else None 171: b = mh.split(".")[0] 172: if b in ("root", "spine05") or b == "pelvis": 173: return "J_Bip_C_Hips" 174: if b == "spine04": 175: return "J_Bip_C_Spine" 176: if b in ("spine03", "spine02", "breast"): 177: return "J_Bip_C_Chest" 178: if b == "spine01": 179: return "J_Bip_C_UpperChest" 180: if b.startswith("neck"): 181: return "J_Bip_C_Neck" 182: if side: 183: if b in ("clavicle", "shoulder01"): 184: return f"J_Bip_{side}_Shoulder" 185: if b.startswith("upperarm"): 186: return f"J_Bip_{side}_UpperArm" 187: if b.startswith("lowerarm"): 188: return f"J_Bip_{side}_LowerArm" 189: if b == "wrist" or b.startswith("metacarpal"): 190: return f"J_Bip_{side}_Hand" 191: if b.startswith("finger"): 192: n, k = b[6:].split("-") 193: return f"J_Bip_{side}_{FINGER[int(n)]}{k}" 194: if b.startswith("upperleg"): 195: return f"J_Bip_{side}_UpperLeg" 196: if b.startswith("lowerleg"): 197: return f"J_Bip_{side}_LowerLeg" 198: if b == "foot": 199: return f"J_Bip_{side}_Foot" 200: if b.startswith("toe"): 201: return f"J_Bip_{side}_ToeBase" 202: return "J_Bip_C_Head" 203: 204: 205: # VRoid bones: name -> (parent, the MakeHuman bone whose head is this joint) 206: BONES = {"J_Bip_C_Hips": (None, "spine05"), "J_Bip_C_Spine": ("J_Bip_C_Hips", "spine04"), 207: "J_Bip_C_Chest": ("J_Bip_C_Spine", "spine03"), "J_Bip_C_UpperChest": ("J_Bip_C_Chest", "spine01"), 208: "J_Bip_C_Neck": ("J_Bip_C_UpperChest", "neck01"), "J_Bip_C_Head": ("J_Bip_C_Neck", "head")} 209: for _s, _m in (("L", ".L"), ("R", ".R")): 210: BONES.update({f"J_Bip_{_s}_Shoulder": ("J_Bip_C_UpperChest", "clavicle" + _m), 211: f"J_Bip_{_s}_UpperArm": (f"J_Bip_{_s}_Shoulder", "upperarm01" + _m), 212: f"J_Bip_{_s}_LowerArm": (f"J_Bip_{_s}_UpperArm", "lowerarm01" + _m), 213: f"J_Bip_{_s}_Hand": (f"J_Bip_{_s}_LowerArm", "wrist" + _m), 214: f"J_Bip_{_s}_UpperLeg": ("J_Bip_C_Hips", "upperleg01" + _m), 215: f"J_Bip_{_s}_LowerLeg": (f"J_Bip_{_s}_UpperLeg", "lowerleg01" + _m), 216: f"J_Bip_{_s}_Foot": (f"J_Bip_{_s}_LowerLeg", "foot" + _m), 217: f"J_Bip_{_s}_ToeBase": (f"J_Bip_{_s}_Foot", "toe3-1" + _m), 218: f"J_Adj_{_s}_FaceEye": ("J_Bip_C_Head", "eye" + _m)}) 219: for _n, _f in FINGER.items(): 220: for _k in (1, 2, 3): 221: BONES[f"J_Bip_{_s}_{_f}{_k}"] = (f"J_Bip_{_s}_Hand" if _k == 1 else f"J_Bip_{_s}_{_f}{_k - 1}", f"finger{_n}-{_k}{_m}") 222: ARM = ["UpperArm", "LowerArm", "Hand"] + [f"{f}{k}" for f in FINGER.values() for k in (1, 2, 3)] 223: LEG = ["UpperLeg", "LowerLeg", "Foot", "ToeBase"] 224: 225: 226: def _rig_point(spec, V, groups): 227: if spec["strategy"] == "CUBE": 228: return V[np.unique(np.concatenate([np.array(f) for f in groups[spec["cube_name"]]]))].mean(0) 229: if spec["strategy"] == "VERTEX": 230: return V[spec["vertex_index"]] 231: return V[spec["vertex_indices"]].mean(0) 232: 233: 234: def _rot(a, b): 235: a, b = a / np.linalg.norm(a), b / np.linalg.norm(b) 236: v, c = np.cross(a, b), float(a @ b) 237: if c < -0.9999: 238: return -np.eye(3) 239: vx = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]]) 240: return np.eye(3) + vx + vx @ vx / (1 + c) 241: 242: 243: def _about(R, p): 244: T = np.eye(4) 245: T[:3, :3] = R 246: T[:3, 3] = p - R @ p 247: return T 248: 249: 250: # ---- the stylised eye: a painted iris texture -----------------------------------------------------------------
agent
+3:33reasoning
The description indicates the tool prepares a model for VRoid. The key actions seem to be straightening the pose to a specific rest state and orienting it to face a particular direction within a defined coordinate system. This suggests a transformation of the model's initial pose and orientation to meet the requirements of the target VRoid environment.
The core concept is that VRoid's rest pose has identity bone rotations. MakeHuman's rest pose differs, requiring straightening. The pose={} used for LOWMARCH_ARMORY=1 in the review site was to show bodies in the T-pose for proportion inspection, directly representing VRoid's rest pose. Further investigation is needed regarding the potential issues arising from using different bodies with different poses.
The conversion process likely straightens the MakeHuman model to match VRoid's T-pose, confirming the significance of identity bone rotations. Investigating the codebase reveals that art/cast/cast.py defines how arm entries interact with dressing tools, with the assumption that shape information is already defined within VRM files due to cage.py processing. This suggests that proportion changes are already handled by an initial transformation process.
The project's development timeline indicates a focus on creating VRM-compatible body variations based on a CC0 VRoid 1.29 base with face swaps and hair. Core tools were developed, including cage.py, fit.py, anthro.py, and others, to generate VRM files with standard bone structures and identity rest rotations. These generated VRM files confirm the critical importance of standard bone structures and identity rotations.
read_file{"EndLine":450,"Path":"~/work/lowmarch/tools/outfit.py","StartLine":151}
input
{ "EndLine": 450, "Path": "~/work/lowmarch/tools/outfit.py", "StartLine": 151 }result, 301 lines
Showing lines 151-450 of 1272 151: nrm = nrm - t * np.einsum("ij,ij->i", nrm, t)[:, None] 152: nrm /= np.maximum(np.linalg.norm(nrm, axis=1, keepdims=True), 1e-9) 153: side = np.cross(t, nrm) 154: P = np.stack([C - side * width / 2 + nrm * lift, C + side * width / 2 + nrm * lift], 1) 155: pos, tri = grid_mesh(P) 156: vn = np.repeat(nrm, 2, axis=0) 157: if (vertex_normals(pos, tri) * vn).sum(1).mean() < 0: 158: tri = tri[:, ::-1] 159: return piece(name, role, pos, tri, nrm=vn) 160: 161: 162: # ---- body fit ----------------------------------------------------------------------- 163: class Fit: 164: """What a costume is built against: the body skin (welded, measurable) and the bones, all in 165: the reference pose, plus the skin weights so pieces can be bound to the skeleton.""" 166: 167: def __init__(self, vrm, pose, prims, roles): 168: self._prims, self._roles = prims, roles 169: W = vrm.node_world(pose) 170: names = vrm.nodes_by_name() 171: self.world = {k: W[i].copy() for k, i in names.items() if k} 172: self.bone = {k: m[:3, 3].copy() for k, m in self.world.items()} 173: skin = [p for p, r in zip(prims, roles) if r == "body"] 174: self.body = skin[0] 175: self.pos = self.body["pos"] 176: self.J, self.W, self.jnames = self.body["J"], self.body["W"], self.body["jnames"] 177: self.tri = weld(self.pos, np.concatenate([p["tri"] for p in skin])) 178: self.surf = Surface(self.pos, self.tri) 179: self.skin = self.pos[np.unique(self.tri)] 180: # MakeHuman bodies (tools/makehuman.py) have real muscle relief; VRoid's limbs are smooth tubes 181: self.cloth_hull = vrm.j.get("asset", {}).get("generator", "").startswith("lowmarch makehuman") \ 182: and not os.environ.get("LOWMARCH_NO_HULL") 183: 184: def head_surface(self): 185: """The head's skin (face and scalp), for charts over the skull (tools/hair.py).""" 186: from vrm import bone_weight 187: pos, tri, off = [], [], 0 188: for p, r in zip(self._prims, self._roles): 189: if r in ("face", "body") and "J" in p and len(p["tri"]): 190: w = bone_weight(p, ("Head",)) 191: T = p["tri"][(w[p["tri"]] > 0.5).all(1)] 192: if len(T): 193: pos.append(p["pos"]) 194: tri.append(T + off) 195: off += len(p["pos"]) 196: return Surface(np.concatenate(pos), np.concatenate(tri)) 197: 198: def weight(self, bones): 199: hit = np.array([any(b in n for b in bones) for n in self.jnames]) 200: return (hit[self.J] * self.W).sum(1) 201: 202: def tris_for(self, bones, min_w=0.5, exclude=(), y_min=None, y_max=None): 203: w = self.weight(bones) 204: if exclude: 205: w = w - self.weight(exclude) 206: T = self.tri[(w[self.tri] >= min_w).all(1)] 207: tp = self.pos[T] 208: sel = np.ones(len(T), bool) 209: if y_min is not None: 210: sel &= tp[:, :, 1].min(1) > y_min 211: if y_max is not None: 212: sel &= tp[:, :, 1].max(1) < y_max 213: return T[sel] 214: 215: def bind(self, pc, allow=None, fallback=None, rigid=None, blend=None, surface=None): 216: """Attach skin weights to a piece: copied from the body under it (optionally limited to 217: some bones), or rigid on one bone / a fixed blend.""" 218: if pc is None: 219: return None 220: n = len(pc["pos"]) 221: if rigid or blend: 222: pc["J"], pc["W"] = rigid_weights(n, self.jnames, rigid, blend) 223: else: 224: pc["J"], pc["W"] = transfer_weights(pc["pos"], None, self.J, self.W, surface=surface or self.surf, 225: allow=allow, jnames=self.jnames, fallback=fallback) 226: pc["jnames"] = self.jnames 227: return pc 228: 229: 230: def without(pc, bones, max_w=0.3): 231: """A piece's triangles away from some bones, as (pos, tri) for merge_surfaces: a bodice's torso 232: without its sleeves. Casting a waist ring onto a garment with its sleeves in it catches the 233: hanging arms at the sides (the reference pose has the arms down beside the hips).""" 234: hit = np.array([any(b in n for b in bones) for n in pc["jnames"]]) 235: w = (hit[pc["J"]] * pc["W"]).sum(1) 236: return pc["pos"], pc["tri"][(w[pc["tri"]] < max_w).all(1)] 237: 238: 239: def inherit(child, parent): 240: """Trim and hardware take the weights of the nearest vertex of the piece they sit on.""" 241: _, i = cKDTree(parent["pos"]).query(child["pos"]) 242: child["J"], child["W"], child["jnames"] = parent["J"][i], parent["W"][i], parent["jnames"] 243: return child 244: 245: 246: def near_normals(pc, pts): 247: return pc["nrm"][cKDTree(pc["pos"]).query(pts)[1]] 248: 249: 250: # ---- garments ------------------------------------------------------------------------ 251: def wrap(fit, name, role, bones, offset, min_w=0.5, exclude=(), y_min=None, y_max=None, subdiv=0, 252: base=None, hems=(), smooth=15, cut=None, refloor=False, clamp=False): 253: """A garment shrink-wrapped onto the body region that follows `bones`. cut: (normal, d), keep only 254: the skin below that plane (n.p < d, give or take a centimetre; pair it with a hem on the same plane). 255: refloor: after the hems, push out whatever a hem pulled into the skin (a hem moves its edge straight 256: along the plane's normal, which sinks it into a sloping hip; off by default so finished costumes 257: don't drift). clamp: every vertex past a hem's plane goes onto it, not just the edge's (pass y_min 258: and y_max a few centimetres past the hems: large skin triangles crossing a hem line otherwise leave 259: notches as deep as a triangle).""" 260: T = fit.tris_for(bones, min_w, exclude, y_min, y_max) 261: if cut is not None: 262: n_ = np.asarray(cut[0], float) / np.linalg.norm(cut[0]) 263: T = T[(fit.pos[T] @ n_).max(1) < cut[1] + 0.01] 264: if not len(T): 265: return None 266: pos, tri, used = compact(fit.pos, T) 267: levels = [] 268: for _ in range(subdiv): 269: pos, tri, par = subdivide(pos, tri, with_parents=True) 270: levels.append(par) 271: out = shrink_wrap(pos, tri, base or fit.surf, offset, smooth=smooth) 272: for n, d in hems: 273: out = flatten_hem(out, tri, n, d) 274: if clamp: 275: n_ = np.asarray(n, float) / np.linalg.norm(n) 276: over = out @ n_ - d 277: out = out - np.outer(np.maximum(over, 0.0), n_) 278: if refloor and hems: 279: surf = base or fit.surf 280: d2, cp2, ti2 = surf.query(out) 281: low = d2 < offset * 0.75 282: out[low] = cp2[low] + surf.fn[ti2[low]] * offset * 0.75 283: pc = piece(name, role, out, tri) 284: # weights come from the exact skin each vertex grew from: copying from the nearest skin instead 285: # let the inside of one boot pick up the other leg's bones 286: pc["J"], pc["W"] = blend_weights(fit.J[used], fit.W[used], int(fit.J.max()) + 1, levels) 287: pc["jnames"] = fit.jnames 288: if getattr(fit, "cloth_hull", False): # a body with muscle relief: sleeves span the hollows 289: hw = lambda b: (np.array([b == n for n in fit.jnames])[pc["J"]] * pc["W"]).sum(1) 290: axes = [] 291: for sd in ("L", "R"): 292: for b0, b1 in (("UpperArm", "LowerArm"), ("LowerArm", "Hand"), ("UpperLeg", "LowerLeg"), ("LowerLeg", "Foot"), 293: ("Foot", "ToeBase")): # (boots over the foot: no toes showing) 294: n0, n1 = f"J_Bip_{sd}_{b0}", f"J_Bip_{sd}_{b1}" 295: if n0 in fit.bone and n1 in fit.bone: 296: m = hw(n0) + (hw(n1) if b0 == "Foot" else 0.0) # the foot's segment takes the toes too 297: axes.append((m >= 0.6, fit.bone[n0], fit.bone[n1], b0 == "Foot")) 298: pc["pos"] = limb_hull(pc["pos"], tri, axes) 299: pc["nrm"] = vertex_normals(pc["pos"], tri) 300: pc["loops"] = boundary_loops(tri) 301: return pc 302: 303: 304: def loop_trim(name, role, garment, pick, width=0.008, lift=0.0015): 305: """Trim along one of a garment's open edges: the loop that scores highest under `pick`.""" 306: loops = garment.get("loops") or [] 307: if not loops: 308: return None 309: best = max(loops, key=lambda L: pick(garment["pos"][L])) 310: return inherit(ribbon(name, role, garment["pos"][best], width, lift, normals=garment["nrm"][best], closed=True), garment) 311: 312: 313: def slice_line(pc, axis=0, value=0.0, keep=lambda p: True): 314: """Points where a plane (coordinate `axis` == value) cuts a piece, sorted top to bottom.""" 315: P = pc["pos"][pc["tri"]] 316: s = P[:, :, axis] - value 317: out = [] 318: for i, j in ((0, 1), (1, 2), (2, 0)): 319: a, b = s[:, i], s[:, j] 320: x = (a > 0) != (b > 0) 321: t = a[x] / (a[x] - b[x]) 322: out.append(P[x, i] + (P[x, j] - P[x, i]) * t[:, None]) 323: pts = np.concatenate(out) 324: pts = np.array([p for p in pts if keep(p)]) 325: return pts[np.argsort(-pts[:, 1])] if len(pts) else pts 326: 327: 328: def collar(fit, name, role, height_back=0.044, height_front=0.028, gap_deg=50, clear=(0.009, 0.015), 329: thickness=0.0035, seg=40, rows=6): 330: """A standing collar fitted to the neck: radius from ray casts to the neck skin plus clearance 331: (flaring toward the top), taller at the back, base lower at the front (the jugular notch).""" 332: nk = fit.bone["J_Bip_C_Neck"] 333: half = math.radians(180 - gap_deg / 2) 334: ang = np.linspace(-half, half, seg) # 0 = back (+z), +-pi = front (-z) 335: neck = Surface(fit.pos, fit.tris_for(("Neck", "UpperChest", "Head", "Shoulder"), min_w=0.3)) 336: P = np.zeros((seg, rows, 3)) 337: for i, a in enumerate(ang): 338: d = np.array([math.sin(a), 0, math.cos(a)]) 339: front = (1 - math.cos(a)) / 2 # 0 back .. 1 front 340: base_y = nk[1] - 0.010 - 0.012 * front 341: h = height_back + (height_front - height_back) * front 342: for j, t in enumerate(np.linspace(0, 1, rows)): 343: o = np.array([nk[0], base_y + h * t, nk[2]]) 344: r = neck.cast(o, d, rmax=0.2, far=False) 345: r = 0.05 if np.isnan(r) else r 346: P[i, j] = o + d * (r + clear[0] + (clear[1] - clear[0]) * t) 347: pos, tri, outer = thick_grid(P, thickness, outward_from=nk, axis=[0, 1, 0]) 348: pc = piece(name, role, pos, tri) 349: t = np.clip((pos[:, 1] - (nk[1] - 0.022)) / height_back, 0, 1) 350: fit.bind(pc, blend={"J_Bip_C_UpperChest": 0.5, "J_Bip_C_Neck": 0.5}) 351: pc["W"][:, 0], pc["W"][:, 1] = 1 - 0.7 * t, 0.7 * t # base on the chest, top mostly on the neck 352: return pc, outer[:, -1], (outer[0], outer[-1]) 353: 354: 355: def pauldron(fit, name, role, over, side="L", lames=((0, 62, 0.012, 0.016), (55, 78, 0.010, 0.013), 356: (72, 92, 0.008, 0.011)), spread=135, thickness=0.004, seg=30, rows=7): 357: """Overlapping lames over the shoulder. Each lame's shape comes from ray casts out of the 358: deltoid to the surface it covers (`over`: the tunic, plus the cape if it runs under), plus an 359: offset that grows toward the rim. Lower lames tuck under the one above. Open on the neck side.""" 360: sh = fit.bone[f"J_Bip_{side}_UpperArm"] 361: el = fit.bone[f"J_Bip_{side}_LowerArm"] 362: down = (el - sh) / np.linalg.norm(el - sh) 363: up = -down 364: sx = -1 if side == "L" else 1 365: outw = np.array([sx, 0, 0.0]) - down * (np.array([sx, 0, 0.0]) @ down) 366: outw /= np.linalg.norm(outw) 367: fwd = np.cross(down, outw) 368: fwd = -fwd if fwd[2] > 0 else fwd 369: C = sh + down * 0.015 370: pole = up * math.cos(math.radians(28)) + outw * math.sin(math.radians(28)) 371: pole /= np.linalg.norm(pole) 372: e1 = fwd - pole * (fwd @ pole) 373: e1 /= np.linalg.norm(e1) 374: e2 = np.cross(pole, e1) 375: e2 = -e2 if e2 @ outw < 0 else e2 376: ph = np.linspace(-math.radians(spread), math.radians(spread), seg) 377: pieces, rims = [], [] 378: for k, (t0, t1, o0, o1) in enumerate(lames): 379: G = np.zeros((seg, rows, 3)) 380: for j, th in enumerate(np.radians(np.linspace(t0, t1, rows))): 381: for i, p in enumerate(ph): 382: d = pole * math.cos(th) + math.sin(th) * (e2 * math.cos(p) + e1 * math.sin(p)) 383: r = over.cast(C, d, rmax=0.25, far=True) 384: r = 0.06 if np.isnan(r) else r 385: G[i, j] = C + d * (r + o0 + (o1 - o0) * j / (rows - 1)) 386: pos, tri, outer = thick_grid(G, thickness, outward_from=C) 387: pc = piece(f"{name}{k}", role, pos, tri) 388: if k == 0: 389: fit.bind(pc, blend={f"J_Bip_{side}_UpperArm": 0.7, f"J_Bip_{side}_Shoulder": 0.3}) 390: else: 391: fit.bind(pc, rigid=f"J_Bip_{side}_UpperArm") 392: pieces.append(pc) 393: rims.append(outer[:, -1]) 394: return pieces, rims 395: 396: 397: def drape(P0, collide, clearance, pinned, steps=90, iters=10, gravity=0.0025, bend=0.25, closed=False): 398: """A small position-based cloth solve on a (U, V, 3) grid: gravity, stretch and bend 399: constraints taken from the starting shape, pinned points, and collision that keeps every 400: point `clearance` (per row) outside the `collide` surface. closed: the U direction wraps 401: around (a skirt), so the last column is linked to the first.""" 402: U, V = P0.shape[:2] 403: P = P0.reshape(-1, 3).copy() 404: idx = np.arange(U * V).reshape(U, V) 405: nxt = np.roll(idx, -1, 0) if closed else idx[1:, :] 406: nx2 = np.roll(idx, -2, 0) if closed else idx[2:, :] 407: links = np.concatenate([np.stack([(idx if closed else idx[:-1, :]).ravel(), nxt.ravel()], 1), 408: np.stack([idx[:, :-1].ravel(), idx[:, 1:].ravel()], 1)]) 409: blinks = np.concatenate([np.stack([(idx if closed else idx[:-2, :]).ravel(), nx2.ravel()], 1), 410: np.stack([idx[:, :-2].ravel(), idx[:, 2:].ravel()], 1)]) 411: rest = np.linalg.norm(P[links[:, 0]] - P[links[:, 1]], axis=1) 412: brest = np.linalg.norm(P[blinks[:, 0]] - P[blinks[:, 1]], axis=1) 413: fixed = pinned.reshape(-1) 414: pin = P[fixed].copy() 415: clr = np.broadcast_to(np.asarray(clearance, float)[None, :], (U, V)).reshape(-1) 416: 417: def solve(L, R0, k): 418: """Jacobi step: every constraint proposes a correction, each point moves by the average 419: of its proposals (stable with many constraints per point).""" 420: a, b = P[L[:, 0]], P[L[:, 1]] 421: d = b - a 422: ln = np.maximum(np.linalg.norm(d, axis=1), 1e-9) 423: corr = (d * ((ln - R0) / ln)[:, None]) * 0.5 * k 424: acc = np.zeros_like(P) 425: cnt = np.zeros(len(P)) 426: np.add.at(acc, L[:, 0], corr) 427: np.add.at(acc, L[:, 1], -corr) 428: np.add.at(cnt, L[:, 0], 1) 429: np.add.at(cnt, L[:, 1], 1) 430: P[:] += acc / np.maximum(cnt, 1)[:, None] * 1.6 431: 432: for _ in range(steps): 433: P[:, 1] -= gravity 434: for _ in range(iters): 435: solve(links, rest, 1.0) 436: solve(blinks, brest, bend) 437: P[fixed] = pin 438: d, cp, ti = collide.query(P) 439: low = d < clr 440: P[low] = cp[low] + collide.fn[ti[low]] * clr[low][:, None] 441: P[fixed] = pin 442: return P.reshape(U, V, 3) 443: 444: 445: def cape(fit, name, role, under, length=1.0, phi=0.55, seg=44, rows=18, top_clear=0.003, 446: clear=(0.010, 0.045), folds=7, fold_depth=0.010): 447: """A cape whose top edge lies on the tunic around the base of the neck, clasped at the 448: collarbones, then settled by the cloth solve so it rests on the shoulders and falls clear.""" 449: nk = fit.bone["J_Bip_C_Neck"] 450: ph = np.linspace(-phi, phi, seg) * math.pi # 0 = straight back