A 3D model pipeline, pushed by Gemini 4 Carbon
agent (cont.)
+2:56read_file{"EndLine":250,"Path":"~/work/lowmarch/tools/hair.py","StartLine":1}
input
{ "EndLine": 250, "Path": "~/work/lowmarch/tools/hair.py", "StartLine": 1 }result, 251 lines
Showing lines 1-250 of 318 1: """The hair kit: hair built as locks combed over the skull, instead of borrowing a VRoid sample's hair 2: cards (portrait guide, lesson 14). 3: 4: HeadChart u = degrees round the head (0 front, +90 the character's right), v = degrees down from the 5: crown (0 top, 90 level with the skull's centre, ~130 the jaw): rays from the skull's centre 6: onto the head skin, smoothed into a shell (as plates.TorsoChart does for the body). 7: cap the hair's base volume: a shell over the scalp down to a hairline given as v(u). 8: lock one lock: a path of (u, v, lift) points over the head (lift = metres off the scalp), smoothed 9: and sampled, then swept with a flat lens-shaped section whose flat side lies along the head, 10: width tapering to a point. Past the chart's lower edge a path just continues in 3D. 11: Every piece gets UVs (around, along) and shares one strand texture, so the painted renderer draws 12: strand highlights along each lock; every piece rides the head bone. 13: """ 14: import math 15: 16: import numpy as np 17: 18: from plates import _Chart, _fill_nan 19: from scipy.ndimage import gaussian_filter 20: 21: 22: class HeadChart(_Chart): 23: def __init__(self, head, centre, du=4.0, dv=3.0, v_max=150.0, smooth=(1.0, 1.0)): 24: self.c = np.asarray(centre, float) 25: self.wrap = True 26: self.u0, self.du, self.v0, self.dv, self.ev = -180.0, du, 0.0, dv, 0.8 27: us = np.arange(-180.0, 180.0, du) 28: vs = np.arange(0.0, v_max + dv * 0.5, dv) 29: R = np.full((len(us), len(vs)), np.nan) 30: for i, u in enumerate(us): 31: for j, v in enumerate(vs): 32: o, d = self.ray(u, v) 33: R[i, j] = head.cast(o, d, rmax=0.3, far=True) 34: R = _fill_nan(R, True) 35: self.R = gaussian_filter(R, smooth, mode=("wrap", "nearest")) 36: 37: def ray(self, u, v): 38: u, v = np.broadcast_arrays(np.asarray(u, float), np.asarray(v, float)) 39: ur, vr = np.radians(u), np.radians(v) 40: d = np.stack([np.sin(vr) * np.sin(ur), np.cos(vr), -np.sin(vr) * np.cos(ur)], -1) 41: return np.broadcast_to(self.c, d.shape).copy(), d 42: 43: def centre(self, v=None): 44: return self.c 45: 46: @property 47: def L(self): 48: """Metres per degree of v (decals and seams measure plates on this chart in metres).""" 49: return float(np.mean(self.R)) * np.pi / 180 50: 51: def metric(self, u, v): 52: """(arc across, arc down) in metres, for seams and decals on helmets.""" 53: r = self.radius(u, v) 54: return np.stack([np.radians(u) * r * np.sin(np.radians(np.asarray(v, float))), np.radians(v) * r + 0 * np.asarray(u, float)], -1) 55: 56: 57: # ---- the shared strand texture: fine light and dark lines running along each lock ----------------- 58: def strand_texture(n=256, seed=3): 59: rng = np.random.default_rng(seed) 60: x = np.arange(n) 61: stripes = np.zeros(n) 62: for f, a in ((3, 0.18), (7, 0.12), (17, 0.10), (41, 0.06)): 63: stripes += a * np.sin(2 * np.pi * (f * x / n + rng.random())) 64: stripes += 0.08 * gaussian_filter(rng.standard_normal(n), 1.2, mode="wrap") 65: col = np.clip(0.62 + stripes, 0.25, 0.95) 66: along = 1.0 - 0.10 * np.linspace(0, 1, n)[:, None] # a touch darker toward the tip 67: img = (np.repeat(col[None, :], n, 0) * along * 255).astype(np.uint8) 68: return np.dstack([img, img, img, np.full((n, n), 255, np.uint8)]) 69: 70: 71: STRANDS = strand_texture() 72: 73: 74: def _smooth_path(P, n): 75: """Catmull-Rom through control points P (k, 3), sampled at n points.""" 76: P = np.asarray(P, float) 77: if len(P) == 2: 78: return P[0] + (P[1] - P[0]) * np.linspace(0, 1, n)[:, None] 79: Q = np.vstack([2 * P[0] - P[1], P, 2 * P[-1] - P[-2]]) 80: seg = len(P) - 1 81: out = [] 82: for t in np.linspace(0, seg, n): 83: i = min(int(t), seg - 1) 84: s = t - i 85: p0, p1, p2, p3 = Q[i], Q[i + 1], Q[i + 2], Q[i + 3] 86: out.append(0.5 * ((2 * p1) + (-p0 + p2) * s + (2 * p0 - 5 * p1 + 4 * p2 - p3) * s * s 87: + (-p0 + 3 * p1 - 3 * p2 + p3) * s ** 3)) 88: return np.array(out) 89: 90: 91: def lock(name, chart, path, width, thick=0.006, n=28, seg=10, taper=(0.45, 1.0), tip=0.04, flat_to=None, 92: role="hair", root_width=None, profile=None): 93: """A lock along `path`: (u, v, lift) points on the chart, or 3D points (arrays of length 3 with 94: a flag), mixed freely as ("uv", u, v, lift) / ("xyz", x, y, z). Width tapers from 95: taper[0] of the way along to a point at the end (tip: the last fraction kept a hair's width). 96: The flat side faces `flat_to` (a point the section looks away from; default the skull's centre).""" 97: from outfit import piece 98: pts = [] 99: for p in path: 100: if p[0] == "uv": 101: _, u, v, lift = p 102: pts.append(chart.point(u, v, off=lift)) 103: else: 104: pts.append(np.array(p[1:], float)) 105: C = _smooth_path(pts, n) 106: T = np.gradient(C, axis=0) 107: T /= np.maximum(np.linalg.norm(T, axis=1, keepdims=True), 1e-9) 108: away = C - np.asarray(flat_to if flat_to is not None else chart.c, float) 109: N = away - T * np.einsum("ij,ij->i", away, T)[:, None] 110: N /= np.maximum(np.linalg.norm(N, axis=1, keepdims=True), 1e-9) 111: Bv = np.cross(T, N) 112: t = np.linspace(0, 1, n) 113: w0 = width if root_width is None else root_width 114: w = np.where(t < 0.12, w0 + (width - w0) * t / 0.12, width) 115: k = np.clip((t - taper[0]) / max(taper[1] - taper[0], 1e-6), 0, 1) 116: w = w * (1 - k ** 1.6) + tip * 0.01 * k 117: if profile is not None: # a clump: profile(t) = width as a fraction of `width` 118: w = width * np.array([profile(x) for x in t]) + 0.0004 119: k = np.clip((t - 0.6) / 0.4, 0, 1) 120: th = thick * np.maximum(1 - k ** 1.3, 0.15) 121: a = np.linspace(0, 2 * np.pi, seg + 1) 122: G = (C[:, None] + Bv[:, None] * (w[:, None] / 2 * np.cos(a)[None, :])[..., None] 123: + N[:, None] * (th[:, None] / 2 * np.sin(a)[None, :])[..., None]) 124: from outfit import grid_mesh 125: pos, tri = grid_mesh(G) 126: pc = piece(name, role, pos, tri, outward_from=None) 127: from fitting import vertex_normals 128: fn = vertex_normals(pos, tri) 129: radial = (pos - np.repeat(C, seg + 1, 0)) 130: if (np.einsum("ij,ij->i", fn, radial) < 0).mean() > 0.5: 131: pc["tri"] = tri[:, ::-1] 132: pc["nrm"] = -fn 133: uu, vv = np.meshgrid(a / (2 * np.pi), t, indexing="xy") 134: pc["uv"] = np.stack([uu.ravel(), vv.ravel()], 1) 135: pc["strands"] = True 136: pc["group"] = "hair" 137: return pc 138: 139: 140: def cap(name, chart, hairline, lift=0.010, crown=0.006, nu=72, nv=26, role="hair"): 141: """The hair's base volume: a shell over the scalp from the crown down to hairline(u) (degrees of v), 142: lifted `lift` off the scalp plus `crown` more at the top.""" 143: from outfit import piece, grid_mesh 144: U = np.linspace(-180, 180, nu) 145: G = np.zeros((nu, nv, 3)) 146: UV = np.zeros((nu, nv, 2)) 147: for i, u in enumerate(U): 148: vmax = hairline(u) 149: for j, s in enumerate(np.linspace(0, 1, nv)): 150: v = vmax * s # from the crown itself (a gap there showed as a dot) 151: G[i, j] = chart.point(u, v, off=lift + crown * (1 - s) ** 2) 152: UV[i, j] = (i / (nu - 1) * 6, s) # strands run from the crown down (u repeats) 153: pos, tri = grid_mesh(G) 154: pc = piece(name, role, pos, tri, outward_from=chart.c) 155: pc["uv"] = UV.reshape(-1, 2) 156: pc["strands"] = True 157: pc["group"] = "hair" 158: return pc 159: 160: 161: # ---- hairstyles ----------------------------------------------------------------------------------- 162: def head_chart(fit, ear_x=None): 163: """The chart over this body's skull, centred in the head skin's bounding box. The ears are left 164: out (triangles further than ear_x to the side, by default 0.835 of the head's half width, 8.6 cm 165: on the VRoid heads): cast onto them, every lock passing the sides was pushed out past the ears and 166: the hair became a bowl.""" 167: from fitting import Surface 168: head = fit.head_surface() 169: P = head.pos[np.unique(head.tri)] 170: centre = (P.min(0) + P.max(0)) / 2 171: if ear_x is None: 172: ear_x = min(0.086, 0.835 * float(P[:, 0].max() - centre[0])) 173: cx = head.pos[head.tri].mean(1)[:, 0] 174: skull = Surface(head.pos, head.tri[np.abs(cx - centre[0]) < ear_x]) 175: return HeadChart(skull, centre) 176: 177: 178: def clump(t, widest=0.55, root=0.42, point=1.6): 179: """A bang's width along it, as a fraction of its widest: it grows out of the hair mass, is widest 180: past its middle, then comes to a point.""" 181: if t < widest: 182: return root + (1 - root) * math.sin(math.pi / 2 * t / widest) 183: return max(0.0, 1 - ((t - widest) / (1 - widest)) ** point) ** 0.9 184: 185: 186: def elaine(fit): 187: """Elaine's hair: a short bob with side-swept bangs, side locks framing the face, the back down to 188: the nape; her bun, braid and ribbon (outfit.updo) sit on top, and one upright strand curls forward 189: from the crown. 190: The bangs follow a study of Unicorn Overlord's character art (lesson 14): a few clumps of clearly 191: different widths and lengths fanning from a part off the centre, widest past their middle, a thin 192: strand or two between them; not a row of matching points (the user found seven of them jarring).""" 193: ch = head_chart(fit) 194: # the hairline: low at the nape (about the earlobe's bottom), and down in front of the ear over the 195: # temple (the base's untextured temple skin showed as a white triangle there) 196: out = [cap("hair_cap", ch, lambda u: float(np.interp(abs(u), [0, 40, 60, 75, 90, 110, 130, 150, 180], 197: [56, 62, 76, 100, 108, 110, 126, 140, 146])), 198: lift=0.007, crown=0.009)] 199: part = -24.0 # over her left eye 200: bangs = [ # name, path of (u, v, lift), widest width, (where widest, root fraction) 201: ("sweep", [(part + 4, 34, 0.006), (0, 60, 0.014), (20, 84, 0.017), (33, 96, 0.016)], 0.050, (0.50, 0.40)), 202: ("sweep_back", [(part + 12, 40, 0.005), (16, 64, 0.011), (40, 86, 0.013), (50, 95, 0.012)], 0.036, (0.55, 0.40)), 203: ("fall", [(part, 34, 0.006), (part - 1, 64, 0.016), (-10, 90, 0.019), (-5, 103, 0.018)], 0.030, (0.50, 0.45)), 204: ("left", [(part - 6, 36, 0.006), (part - 16, 64, 0.013), (-42, 88, 0.016), (-48, 97, 0.015)], 0.036, (0.55, 0.45)), 205: ("strand_r", [(part + 8, 42, 0.005), (8, 70, 0.018), (14, 99, 0.020)], 0.011, (0.35, 0.6)), 206: ("strand_l", [(-46, 48, 0.006), (-55, 76, 0.016), (-57, 98, 0.017)], 0.012, (0.35, 0.6)), 207: ] 208: for nm, path, w, (widest, root) in bangs: 209: out.append(lock(f"hair_bang_{nm}", ch, [("uv", *p) for p in path], width=w, thick=0.008, n=32, 210: profile=lambda t, a=widest, b=root: clump(t, a, b))) 211: # side locks: one in front of the ear down to the jaw, one over the ear's top 212: for s, side in ((1, "R"), (-1, "L")): 213: out.append(lock(f"hair_side{side}0", ch, [("uv", s * 50, 24, 0.008), ("uv", s * 68, 68, 0.012), 214: ("uv", s * 70, 108, 0.013), ("uv", s * 64, 140, 0.011)], 215: width=0.030, thick=0.007, taper=(0.5, 1.0), root_width=0.013)) 216: out.append(lock(f"hair_side{side}1", ch, [("uv", s * 84, 40, 0.005), ("uv", s * 100, 70, 0.012), 217: ("uv", s * 108, 104, 0.012), ("uv", s * 112, 122, 0.010)], 218: width=0.032, thick=0.008, taper=(0.55, 1.0), root_width=0.015)) 219: # the back: locks from the crown down to the nape, overlapping 220: for k, u in enumerate((112, 132, 152, 172, 192, 212, 232, 252)): 221: side = abs(u - 180) / 68 # 1 at the sides, 0 at the middle of the back 222: sw = (1 if u < 180 else -1) * 22 * side # sweep toward the nape as they fall 223: out.append(lock(f"hair_back{k}", ch, [("uv", u, 30, 0.005), ("uv", u + sw * 0.2, 62, 0.010 + 0.006 * (1 - side)), 224: ("uv", u + sw * 0.6, 98, 0.010 + 0.004 * (1 - side)), 225: ("uv", u + sw, 146 - 14 * side, 0.008)], 226: width=0.050, thick=0.009, taper=(0.75, 1.0), tip=0.3, root_width=0.020)) 227: # the upright strand: from the crown, up and curling forward; flat side to the side 228: root = ch.point(0, 6, off=0.010) 229: up = np.array([0, 1.0, 0]) 230: fwd = np.array([0, 0, -1.0]) 231: out.append(lock("hair_ahoge", ch, [("xyz", *root), ("xyz", *(root + up * 0.030 - fwd * 0.004)), 232: ("xyz", *(root + up * 0.055 + fwd * 0.012)), 233: ("xyz", *(root + up * 0.050 + fwd * 0.040)), 234: ("xyz", *(root + up * 0.036 + fwd * 0.052))], 235: width=0.016, thick=0.004, taper=(0.35, 1.0), flat_to=ch.c + np.array([-1.0, 0, 0]), n=24)) 236: for pc in out: 237: fit.bind(pc, rigid="J_Bip_C_Head") 238: return out 239: 240: 241: # ---- facial hair ----------------------------------------------------------------------------------------- 242: def _face_marks(ch, head, mouth=None): 243: """Chart angles of the chin (the front's lowest point) and the mouth: the mouth's own centre when 244: given (a long face puts it well over 13 degrees above the chin), else 13 degrees above the chin.""" 245: P = head.pos[np.unique(head.tri)] 246: front = P[(np.abs(P[:, 0] - ch.c[0]) < 0.012) & (P[:, 2] < ch.c[2] - 0.04)] 247: chin = front[np.argmin(front[:, 1])] 248: 249: def angle(q): 250: d = q - ch.c
agent
+5:23reasoning
Initial inspection of the core files reveals potential configuration mismatches related to file paths. Examining the export script next to check for conflicting settings that may overwrite the VRoid model. The suspicion is that an incorrect file path or export setting is the root cause of the observed issues.
read_file{"EndLine":450,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":1}
input
{ "EndLine": 450, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 1 }result, 451 lines
Showing lines 1-450 of 1407 1: """Render a VRoid model as a GBA-style portrait (96x80) in our palette. 2: 3: Pipeline: rasterize at SSx (vrm.rasterize) -> classify each pixel by material role -> shade in 4: palette tones (skin/hair/cloth bands from normals; eyes, brows, lashes from alpha-tested masks) 5: -> reduce to 1x with feature priority -> outline (silhouette, strand separations, jaw over neck) 6: -> hair shadow on the forehead. 7: 8: python3 tools/vroid_portrait.py MODEL.vrm [--yaw 35] [--png out.png] [--scale 6] 9: """ 10: import argparse 11: import math 12: import os 13: import sys 14: 15: import numpy as np 16: from PIL import Image 17: 18: HERE = os.path.dirname(os.path.abspath(__file__)) 19: sys.path.insert(0, HERE) 20: from vrm import VRM, rasterize, interpolate # noqa: E402 21: from head3d import rotation, SKIN, HAIR_TONES, COAT # noqa: E402 22: 23: SS = 4 24: EYES = {"w": "#f4fbff", "r": "#33264a", "s": "#4a5ca8", "t": "#8fb4e8"} 25: CAPE = {"U": "#3a1a2a", "V": "#6a2a3a", "W": "#9a3a4a", "X": "#c86070"} 26: METAL = {"6": "#3a3e58", "7": "#6a7090", "8": "#a8b0c8", "9": "#eef2fa"} 27: GOLD = {"G": "#8a5a1a", "H": "#d8a640", "I": "#fff0a0"} 28: LEATHER = {"u": "#4b2b21", "v": "#83502f", "x": "#b98351"} 29: DOUBLET = {"Q": "#14151d", "R": "#22242f", "S": "#333646", "T": "#4c5064"} # dark under-layer (never skin-like) 30: HERALDRY = {"F": "#6a6e7c", "J": "#a4a8b4", "N": "#d4d7de", "O": "#f1f2f5"} # cool white: borders, devices 31: GRASS = {"a": "#22401f", "b": "#36652c", "c": "#58923a", "d": "#93c257"} 32: DIRT = {"e": "#3e2a1e", "f": "#6e4a30", "i": "#9c7450"} 33: STONE = {"k": "#46465a", "l": "#76768a", "m": "#b2b2c0"} 34: PALETTE = {**SKIN, **HAIR_TONES, **COAT, **EYES, **CAPE, **METAL, **GOLD, **LEATHER, **DOUBLET, **HERALDRY, **GRASS, 35: **DIRT, **STONE} 36: 37: # material name fragment -> role (first match wins) 38: ROLES = [("EyeHighlight", "highlight"), ("EyeIris", "iris"), ("EyeWhite", "white"), ("EyeExtra", "skip"), 39: ("FaceEyeline", "line"), ("FaceEyelash", "line"), ("FaceMouthLine", "line"), ("FaceBrow", "brow"), ("FaceMouth", "mouth"), 40: ("Face_00_SKIN", "face"), ("Body_00_SKIN", "body"), ("HairBack", "hair"), ("_HAIR", "hair"), 41: ("CLOTH", "cloth")] 42: PRIORITY = {"o": 8, "r": 5, "s": 5, "t": 4, "w": 3, "A": 3, "1": 8} # features win the 4x4 vote 43: FACE_MARK = 0.70 # face-texture texels darker than this are painted lines (lip line, nose mark) 44: MARK_GROW = 4 # texels of dilation so a 1.5 mm painted line survives the 4x4 vote 45: LIP_DROP = 0.014 # UV distance below the lip line that gets the lower-lip shade 46: # VRoid face textures share one layout: marks only count inside the nose-and-mouth window 47: MARK_WINDOW = dict(u=(0.32, 0.68), v=(0.58, 0.86), nose_below=0.70) 48: 49: # High-resolution pixel rendering (2026-10-08): a 640x360 base screen (scales cleanly to 1080p/4K). 50: BUST_SIZE, BUST_EYE_CHIN, BUST_EYE_AT = (192, 160), 38.0, (92, 60) # portrait: canvas, eye-to-chin px, eye line 51: BATTLE_SIZE, BATTLE_PX_PER_M, BATTLE_GROUND_AT = (640, 360), 90.0, (420, 318) # battle: canvas, scale, feet 52: SUN = np.array([-0.30, 0.85, 0.45]) / np.linalg.norm([-0.30, 0.85, 0.45]) # scenery light (camera space) 53: LID_CORE = 70 # eyeline texels darker than this are the lash line 54: LIGHT = np.array([0.08, 0.06, 1.0]) / np.linalg.norm([0.08, 0.06, 1.0]) 55: SKIN_BANDS = [(0.80, "5"), (0.62, "4"), (0.38, "3"), (-9, "2")] 56: HAIR_BANDS = [(0.86, "E"), (0.66, "D"), (0.42, "C"), (-9, "B")] 57: CLOTH_BANDS = [(0.85, "P"), (0.6, "M"), (0.35, "L"), (-9, "K")] 58: # costume materials: role -> bands (metal has a narrow bright streak and hard darks) 59: MATERIALS = {"coat": CLOTH_BANDS, 60: "signature": CLOTH_BANDS, # the character's one colour (tabard, mantle, shield) 61: "doublet": [(0.85, "T"), (0.6, "S"), (0.35, "R"), (-9, "Q")], 62: "heraldry": [(0.85, "O"), (0.6, "N"), (0.35, "J"), (-9, "F")], 63: "cape": [(0.85, "X"), (0.6, "W"), (0.35, "V"), (-9, "U")], 64: "metal": [(0.975, "9"), (0.86, "8"), (0.55, "7"), (-9, "6")], 65: "gold": [(0.80, "I"), (0.45, "H"), (-9, "G")], 66: "leather": [(0.75, "x"), (0.4, "v"), (-9, "u")], 67: "boot": [(0.80, "x"), (0.45, "v"), (-9, "u")], 68: "glove": [(0.75, "x"), (0.4, "v"), (-9, "u")], 69: "steel": [(0.90, "9"), (0.70, "8"), (0.40, "7"), (-9, "6")], 70: "grass": [(0.90, "d"), (0.70, "c"), (0.40, "b"), (-9, "a")], 71: "dirt": [(0.70, "i"), (0.35, "f"), (-9, "e")], 72: "stone": [(0.80, "m"), (0.45, "l"), (-9, "k")]} 73: WORLD_ROLES = ("grass", "dirt", "stone") 74: DARKER = {c: tbl[min(i + 1, len(tbl) - 1)][1] for tbl in MATERIALS.values() for i, (_, c) in enumerate(tbl)} 75: FLIP = np.array([-1.0, 1.0, -1.0]) # VRoid faces -z; we want the face toward +z (the viewer) 76: 77: 78: def role_of(material): 79: for frag, role in ROLES: 80: if frag in material: 81: return role 82: return "cloth" 83: 84: 85: def bands(v, table): 86: out = np.full(v.shape, table[-1][1], "<U1") 87: for thr, ch in reversed(table): 88: out[v >= thr] = ch 89: return out 90: 91: 92: from poses import rot, BUST as BUST_POSE, BATTLE_IDLE # noqa: E402,F401 93: 94: 95: def repose(v, prims, ref, target): 96: """Move prims built in the `ref` pose to the `target` pose: each bone's change 97: W_target(j) @ W_ref(j)^-1, blended by the skin weights (bones missing from this skeleton, 98: like another model's hair bones, follow the head).""" 99: names = v.nodes_by_name() 100: Wr, Wt = v.node_world(ref), v.node_world(target) 101: cache = {} 102: 103: def delta(nm): 104: if nm not in cache: 105: i = names.get(nm, names["J_Bip_C_Head"]) 106: cache[nm] = Wt[i] @ np.linalg.inv(Wr[i]) 107: return cache[nm] 108: 109: for p in prims: 110: if "J" not in p: 111: continue 112: D = np.stack([delta(nm) for nm in p["jnames"]]) # (nj,4,4) 113: M = (D[p["J"]] * p["W"][:, :, None, None]).sum(1) # (N,4,4) 114: p["pos"] = np.einsum("nij,nj->ni", M[:, :3, :3], p["pos"]) + M[:, :3, 3] 115: n = np.einsum("nij,nj->ni", M[:, :3, :3], p["nrm"]) 116: p["nrm"] = n / np.maximum(np.linalg.norm(n, axis=1, keepdims=True), 1e-12) 117: 118: 119: def reduce_blocks(char, depth, pid, role, h, w): 120: """Majority vote over each SSxSS block, weighted by PRIORITY so thin features survive. Returns 121: the winning char per pixel plus the depth / primitive / role of its nearest sample.""" 122: blk = lambda a: a.reshape(h, SS, w, SS).transpose(0, 2, 1, 3).reshape(h * w, SS * SS) 123: cb, db, pb, rb = blk(char), blk(depth), blk(pid), blk(role) 124: codes, ci = np.unique(cb, return_inverse=True) 125: ci = ci.reshape(cb.shape) 126: nc = len(codes) 127: counts = np.bincount((np.arange(h * w)[:, None] * nc + ci).ravel(), minlength=h * w * nc).reshape(h * w, nc) 128: empty = np.nonzero(codes == "")[0] 129: filled = SS * SS - (counts[:, empty[0]] if len(empty) else 0) 130: score = counts * np.array([0 if c_ == "" else PRIORITY.get(c_, 1) for c_ in codes])[None] 131: best = np.argmax(score, 1) 132: ok = filled >= SS * SS / 2 133: sel = ci == best[:, None] 134: i = np.argmax(np.where(sel, db, -np.inf), 1) 135: r = np.arange(h * w) 136: out = np.where(ok, codes[best], "").astype("<U1") 137: dep = np.where(ok, db[r, i], -np.inf) 138: prim = np.where(ok, pb[r, i], -1) 139: rol = np.where(ok, rb[r, i], "") 140: return out.reshape(h, w), dep.reshape(h, w), prim.reshape(h, w), rol.reshape(h, w) 141: 142: 143: def camera_rotation(yaw, pitch): 144: """Model -> camera: turn the character by yaw (positive faces screen left), then look down on 145: it from `pitch` degrees above (a camera elevation, not a nod).""" 146: y, p = math.radians(yaw), math.radians(pitch) 147: Ry = np.array([[math.cos(y), 0, -math.sin(y)], [0, 1, 0], [math.sin(y), 0, math.cos(y)]]) 148: Rx = np.array([[1, 0, 0], [0, math.cos(p), -math.sin(p)], [0, math.sin(p), math.cos(p)]]) 149: return Rx @ Ry 150: 151: 152: def smooth(t): 153: t = np.clip(t, 0, 1) 154: return t * t * (3 - 2 * t) 155: 156: 157: # Archetype shape controls, in three layers (0 / 1 / {} = unchanged): 158: # 1. proportions through the skeleton: bone scales, the way VRoid varies builds 159: # 2. face sculpting: anatomical points move, anchors (eyes, ears, skull, neck) hold still, and a 160: # smooth RBF field fills in between 161: # 3. resting expression: VRoid's own shapes (hooded eyes, stern brows) held at a low weight 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, 166: 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, 167: face_normals=1.0, hair_normals=0.0, shadow_cards=1.0, fangs=1.0, rest={}) 168: # hair is reshaped with the face: beards (built on the face before the face is sculpted) and side locks 169: # must follow a wider jaw or a longer chin, or the skin comes through them 170: SCULPT_ROLES = ("face", "body", "mouth", "brow", "line", "white", "iris", "highlight", "hair") 171: 172: 173: def bone_pose(v, sh, base_pose): 174: """Pose dict (bone -> 3x3) = base rotations combined with proportion scales. Each scaled bone 175: resets its children to scale 1 except the head, whose children (hair bones) grow with it.""" 176: # Body proportions (all 1.0 = VRoid's): shoulders lengthens the clavicles (wider shoulders), arms 177: # and legs thicken the limbs, torso broadens the waist-to-chest, leg_len lengthens thigh and shin 178: # (the hips are lifted to keep the feet on the ground), hands scales the hands. 179: a, lg, t = sh["arms"], sh["legs"], sh["torso"] 180: spec = {"J_Bip_C_Head": ((sh["head"],) * 3, True), 181: "J_Bip_C_Neck": ((sh["neck"], 1.0, sh["neck"]), False), 182: "J_Bip_C_UpperChest": ((sh["chest"], 1.0, 1 + (sh["chest"] - 1) * 0.6), False), 183: "J_Bip_C_Spine": ((1 + (t - 1) * 0.7, 1.0, 1 + (t - 1) * 0.5), False), 184: "J_Bip_C_Chest": ((t, 1.0, 1 + (t - 1) * 0.7), False)} 185: for side in "LR": 186: spec[f"J_Bip_{side}_Shoulder"] = ((sh["shoulders"], 1.0, 1.0), False) 187: for b in ("UpperArm", "LowerArm"): 188: spec[f"J_Bip_{side}_{b}"] = ((1.0, a, a), False) # arm bones run along x 189: for b in ("UpperLeg", "LowerLeg"): 190: spec[f"J_Bip_{side}_{b}"] = ((lg, sh["leg_len"], lg), False) # leg bones run along y 191: spec[f"J_Bip_{side}_Hand"] = ((sh["hands"],) * 3, True) 192: spec = {k: v for k, v in spec.items() if not np.allclose(v[0], 1.0)} 193: nodes = v.j["nodes"] 194: parent = {c: i for i, n in enumerate(nodes) for c in n.get("children", [])} 195: own = {} 196: 197: def own_of(i): 198: if i in own: 199: return own[i] 200: nm = nodes[i].get("name") 201: if nm in spec: 202: own[i] = np.array(spec[nm][0], float) 203: elif i in parent and nodes[parent[i]].get("name") in spec and spec[nodes[parent[i]]["name"]][1]: 204: own[i] = own_of(parent[i]) 205: elif i in parent and (own_of(parent[i]) != 1).any() and nodes[parent[i]].get("name") not in spec: 206: own[i] = own_of(parent[i]) # inherited chain (below the head) 207: else: 208: own[i] = np.ones(3) 209: return own[i] 210: 211: def as4(r): 212: r = np.asarray(r, float) 213: if r.shape == (4, 4): 214: return r 215: m = np.eye(4) 216: m[:3, :3] = r 217: return m 218: pose = {k: as4(r) for k, r in base_pose.items()} 219: for i, n in enumerate(nodes): 220: nm = n.get("name") 221: if not nm: 222: continue 223: mine = own_of(i) 224: par = own_of(parent[i]) if i in parent else np.ones(3) 225: adj = mine / par 226: if np.allclose(adj, 1): 227: continue 228: pose[nm] = pose.get(nm, np.eye(4)) @ np.diag(list(adj) + [1.0]) 229: if sh["shoulder_w"]: # wider shoulders by moving the arm joints out (no shear, unlike 230: for side, sx in (("L", -1.0), ("R", 1.0)): # `shoulders`, which scales the clavicle and skews a turned arm) 231: mv = np.eye(4) 232: mv[0, 3] = sx * sh["shoulder_w"] 233: nm = f"J_Bip_{side}_UpperArm" 234: pose[nm] = mv @ pose.get(nm, np.eye(4)) 235: if sh.get("curl"): # relaxed hands (degrees per finger joint) wherever the pose 236: for side, sg in (("L", 1.0), ("R", -1.0)): # leaves the fingers alone: an open hand reads as a paddle 237: for f in ("Index", "Middle", "Ring", "Little"): 238: for j, kf in ((1, 0.6), (2, 1.0), (3, 0.8)): 239: nm = f"J_Bip_{side}_{f}{j}" 240: if nm in base_pose: 241: continue 242: a = np.radians(sg * sh["curl"] * kf * (1.0 + 0.12 * ("Index", "Middle", "Ring", "Little").index(f))) 243: r = np.eye(4) 244: r[:2, :2] = [[np.cos(a), -np.sin(a)], [np.sin(a), np.cos(a)]] 245: pose[nm] = pose.get(nm, np.eye(4)) @ r 246: if sh.get("arm_out"): # arms held further from the body (degrees): a broad torso 247: for side, sg in (("L", -1.0), ("R", 1.0)): # needs room for hanging arms 248: a = np.radians(sg * sh["arm_out"]) 249: r = np.eye(4) 250: r[:2, :2] = [[np.cos(a), -np.sin(a)], [np.sin(a), np.cos(a)]] 251: nm = f"J_Bip_{side}_UpperArm" 252: pose[nm] = r @ pose.get(nm, np.eye(4)) 253: if sh["leg_len"] != 1.0: # longer legs: lift the hips so the feet stay on the ground 254: W = v.node_world() 255: names = v.nodes_by_name() 256: leg = W[names["J_Bip_L_UpperLeg"]][1, 3] - W[names["J_Bip_L_Foot"]][1, 3] 257: lift = np.eye(4) 258: lift[1, 3] = (sh["leg_len"] - 1.0) * leg 259: pose["J_Bip_C_Hips"] = lift @ pose.get("J_Bip_C_Hips", np.eye(4)) 260: if sh["height"] != 1.0: # the whole figure scaled from the hips (everything below them 261: W = v.node_world() # grows with them), lifted so the soles stay on the ground 262: names = v.nodes_by_name() 263: hip = W[names["J_Bip_C_Hips"]][1, 3] + (sh["leg_len"] - 1.0) * ( 264: W[names["J_Bip_L_UpperLeg"]][1, 3] - W[names["J_Bip_L_Foot"]][1, 3]) 265: if getattr(v, "_sole", None) is None: # (reading every mesh is slow; this runs for every pose) 266: v._sole = min(float(p_["pos"][:, 1].min()) for p_ in v.primitives() if len(p_["pos"])) 267: sole = v._sole 268: lift = np.eye(4) 269: lift[1, 3] = (sh["height"] - 1.0) * (hip - sole) 270: pose["J_Bip_C_Hips"] = lift @ pose.get("J_Bip_C_Hips", np.eye(4)) @ np.diag([sh["height"]] * 3 + [1.0]) 271: return pose 272: 273: 274: def landmarks(prims, roles): 275: def verts(*rs): 276: return np.concatenate([p["pos"][np.unique(p["tri"])] for p, r in zip(prims, roles) if r in rs]) 277: iris = verts("iris") 278: face = verts("face") 279: body = verts("body") 280: mid = face[np.abs(face[:, 0]) < 0.006] 281: chin = mid[np.argmin(mid[:, 1])] 282: eyeL, eyeR = iris[iris[:, 0] < 0].mean(0), iris[iris[:, 0] > 0].mean(0) 283: eye_y = (eyeL[1] + eyeR[1]) / 2 284: span = eye_y - chin[1] 285: mouth = verts("mouth").mean(0) 286: band = mid[(mid[:, 1] > mouth[1] + 0.005) & (mid[:, 1] < eyeL[1] - 0.01)] 287: nose = band[np.argmin(band[:, 2])] 288: 289: skin = np.concatenate([face, body[body[:, 1] > chin[1] - 0.02]]) 290: 291: def window(pts, cond_y, cond_x=None): 292: for tol in (0.004, 0.008, 0.016): 293: ok = np.abs(pts[:, 1] - cond_y) < tol 294: if cond_x is not None: 295: ok &= cond_x(pts, tol) 296: if ok.any(): 297: return pts[ok] 298: raise ValueError("no landmark near y=%.3f" % cond_y) 299: 300: def edge(y, side, pts=face): 301: """Outermost face point at height y on one side (-1 = character's left, -x).""" 302: row = window(pts, y, lambda q, t: np.sign(q[:, 0]) == side) 303: return row[np.argmax(np.abs(row[:, 0]))] 304: 305: def front(x, y, pts=skin): 306: near = window(pts, y, lambda q, t: np.abs(q[:, 0] - x) < 1.5 * t) 307: return near[np.argmin(near[:, 2])] 308: 309: lm = dict(eyeL=eyeL, eyeR=eyeR, eye_y=eye_y, chin=chin, mouth=mouth, nose=nose, span=span) 310: for sd, side in (("L", -1), ("R", 1)): 311: lm["gonion" + sd] = edge(chin[1] + 0.30 * span, side) 312: lm["cheek" + sd] = edge(chin[1] + 0.72 * span, side) 313: cw = abs(edge(chin[1] + 0.12 * span, side)[0]) 314: lm["chinside" + sd] = front(side * 0.5 * cw, chin[1] + 0.12 * span) 315: lm["ridge" + sd] = front(side * abs(eyeL[0]), eye_y + 0.22 * span) 316: lm["wing" + sd] = front(side * 0.008, nose[1] + 0.06 * span) 317: head = body[(body[:, 1] > eye_y - 0.35 * span) & (body[:, 1] < eye_y + 0.1 * span)] 318: lm["ear" + sd] = head[np.argmax(side * head[:, 0])] 319: top = body[body[:, 1] > eye_y] 320: lm["crown"] = top[np.argmax(top[:, 1])] 321: lm["occiput"] = top[np.argmax(top[:, 2])] 322: lm["forehead"] = front(0.0, eye_y + 0.5 * span) 323: neck = body[np.abs(body[:, 1] - (chin[1] - 0.06)) < 0.006] 324: lm["neck_front"] = neck[np.argmin(neck[:, 2])] 325: lm["neck_back"] = neck[np.argmax(neck[:, 2])] 326: return lm 327: 328: 329: def sculpt(prims, roles, lm, sh, sigma=0.022): 330: """Anchored RBF displacement: move anatomical points, hold the anchors, interpolate.""" 331: ctrl, disp = [], [] 332: 333: def move(name, d): 334: ctrl.append(lm[name]); disp.append(np.broadcast_to(np.asarray(d, float), (3,)).copy()) 335: 336: for sd, side in (("L", -1), ("R", 1)): 337: out = np.array([side, 0, 0]) 338: move("gonion" + sd, out * 0.010 * sh["jaw"] + np.array([0, -0.003, 0]) * sh["jaw"]) 339: move("cheek" + sd, out * 0.006 * sh["cheek"]) 340: move("chinside" + sd, out * 0.006 * sh["chin_w"] + np.array([0, -0.006, -0.002]) * sh["chin_len"]) 341: move("ridge" + sd, np.array([0, -0.0015, -0.004]) * sh["brow_ridge"]) 342: move("wing" + sd, out * 0.004 * sh["nose_w"]) 343: move("ear" + sd, 0); move("eye" + sd, 0) 344: move("chin", np.array([0, -0.008, -0.003]) * sh["chin_len"]) 345: move("nose", np.array([0, -0.002, -0.004]) * sh["nose"]) 346: for name in ("crown", "occiput", "forehead", "neck_front", "neck_back", "mouth"): 347: move(name, 0) 348: C, D = np.array(ctrl), np.array(disp) 349: if not np.abs(D).any(): 350: return 351: phi = lambda A, B: np.exp(-(np.linalg.norm(A[:, None] - B[None], axis=-1) / sigma) ** 2) 352: Wt = np.linalg.solve(phi(C, C) + 1e-6 * np.eye(len(C)), D) 353: centre = (lm["eyeL"] + lm["eyeR"]) / 2 354: for p, r in zip(prims, roles): 355: if r not in SCULPT_ROLES: 356: continue 357: P = p["pos"] 358: near = np.linalg.norm(P - centre, axis=1) < 0.16 359: if near.any(): 360: P[near] += phi(P[near], C) @ Wt 361: 362: 363: def face_texture(prims, roles, v): 364: """The face texture's source image index (the face primitives share one), or None.""" 365: for p, r in zip(prims, roles): 366: if r == "face" and len(p["tri"]): 367: t = v.j["materials"][p["mi"]].get("pbrMetallicRoughness", {}).get("baseColorTexture") 368: if t is not None: 369: return v.j["textures"][t["index"]]["source"] 370: return None 371: 372: 373: def strip_blush(prims, roles, v, keep): 374: """VRoid's faces are painted with blush on the cheeks and an orange shadow round the eyes: young. 375: keep = 1 leaves it, 0 takes it back to the plain skin (dark painted marks, the lip line and the 376: nose, are left alone).""" 377: if keep >= 1: 378: return 379: src = face_texture(prims, roles, v) 380: if src is None: 381: return 382: tex = v.image(src).astype(float) 383: rgb = tex[..., :3] 384: lum = rgb.mean(-1) 385: opaque = tex[..., 3] > 200 386: base = np.median(rgb[opaque & (lum > np.percentile(lum[opaque], 40))], axis=0) 387: warm = (rgb[..., 0] - rgb[..., 2]) - (base[0] - base[2]) # redder than the skin 388: w = np.clip(warm / 35.0, 0, 1) * (lum > 0.72 * base.mean()) * opaque * (1 - keep) 389: rgb[:] = rgb * (1 - w[..., None]) + base * w[..., None] 390: v._img[src] = np.clip(tex, 0, 255).astype(np.uint8) 391: 392: 393: def face_normals(prims, roles, lm, amount, hair=0.0, forward=0.0, pull=0.5, neck=None): 394: """Shade the head as one simple rounded form instead of from its mesh (the "vertex skeleton normals" of 395: DillonGoo's Yin and Yang rigs): each normal points away from a short vertical line inside the head, pulled 396: halfway toward the line's middle, so cheeks, nose and jaw take clean shadow shapes that hold at every angle 397: instead of patches that jump as the head turns. The face, its brows, lines and mouth and the skull take it in 398: full (`amount`); the neck fades back to its own normals over 3 cm under the chin; hair takes `hair`, fading out 399: below the jaw. Then 5 rounds of smoothing over each mesh's neighbours, as the rig does. `forward`: where the51 more lines not shown.
read_file{"EndLine":250,"Path":"~/work/lowmarch/tools/cage.py","StartLine":1}
input
{ "EndLine": 250, "Path": "~/work/lowmarch/tools/cage.py", "StartLine": 1 }result, 251 lines
Showing lines 1-250 of 888 1: """VRoid bodies reshaped whole, their own head kept (spike, 2026-10-10). 2: 3: The head graft (tools/makehuman.py) joins an anime head to a realistic body, and the mass transfer 4: (tools/mass.py) carried MakeHuman's realistic muscle, many small forms, onto VRoid's smooth surface. Here a 5: VRoid model keeps its own head and its own mesh. Every change is broad and smooth, and every mesh follows 6: the rig by its skin weights, so the face and the body stay joined wherever they meet. 7: 8: Shaped by a reference body (spec["ref"], the way the armory's cage bodies are made): 9: 1. the skeleton goes onto the reference's joints: each limb bone turned and stretched from its joint to 10: the next, the spine as one piece from the hips' joint to the neck's (keeping VRoid's own spacing of 11: its short spine bones), the head, hands and feet scaled whole to the reference's sizes, the head 12: placed so its chin is at the reference's chin 13: 2. the rig's move is smoothed over a few centimetres (lbs_sigma): moving the thighs out from under a 14: pelvis that only stretches folds the skin where VRoid's weights change quickly 15: 3. wrap: from each body vertex a ray along its normal, both ways, to the reference's skin facing the same 16: way (a limb never takes the other limb's skin); the distance is smoothed over space as a number 17: (wrap_sigma: limbs, torso; a vector smoothed round a limb cancels itself) so only broad volumes come 18: across, never the reference's anatomy detail. The push goes along normals averaged over centimetres 19: (normal_sigma): pushed 10 cm along its own normals, any hollow tighter than that folds into a crease. 20: The head, hands and feet and the vertices the body shares with the face mesh stay where the rig put them 21: 4. forms (below) for the masses smoothing can't give (a brute's deltoids, trapezius, pecs), each standing 22: on the skin where a ray from its anchor leaves the body; then `relax` rounds of Taubin smoothing 23: The reference is a MakeHuman body (a cast entry's, or (name, MakeHuman spec)), fitted to the archetype by 24: tools/fit.py as usual: MakeHuman gives the range of builds, VRoid the head and the surface. spec["face"] puts a 25: VRoid sample's face on the base first (tools/heads.py), sized here like VRoid's own; its hairstyle's collision 26: spheres are fitted to the finished body, and the hair draped over them (tools/collide.py). 27: 28: Without a reference the spec's own controls shape it (the first try; on their own they made fat or boxy 29: bodies, but they stay for adjustments): base (a VRoid model under LOWMARCH_VROID) and, as fractions added 30: to 1 (0 = unchanged): 31: len {bone: x} along the bone ("UpperLeg" is both sides; "Spine", "Neck" ... the centre) 32: thick {bone: x} across it, every way (whole bones: a sharp change of weight shows as a step) 33: wide {bone: x} across it side to side (on top of thick; up and down on the arms) 34: deep {bone: x} across it front to back (on top of thick) 35: head, hand, foot the whole part with everything under it 36: rings {chain: {station: dict(t=, w=, d=)}} thickness (all ways, side to side, front to back) at named 37: stations along the torso, arms and legs (STATIONS), interpolated smoothly between them 38: swell {bone: metres}: the limb pushed out along its normals after the wrap, most halfway down the bone (the 39: pull fell 1.5 to 5 cm short of the references' mid-thighs, 2026-10-12; spec["fit"] with only=("thigh_p",) sets 40: it so each body's own thighs read where real people's do), and 41: forms: [dict(gate=bones, at=(bone, t), off=(out, up, fwd), face=(out, up, fwd), size=(out, up, fwd), h=metres), 42: ...]: an anchor t of the way from the bone's joint to the next, moved by off (metres; "out" is away from the 43: midline); the bump stands where a ray from it along `face` leaves the skin, h high there, with Gaussian 44: radii `size`, on skin weighted to the gate bones ("{s}" in a bone name makes one per side; a centre form 45: off the midline is a mirrored pair); height: metres, crown to sole (default: the base's). With 46: spec["fit"] the fitter moves CONTROLS until the archetype's ratios pass. 47: 48: python3 -I tools/cage.py [NAME ...] build these cast entries (default: every cage entry) 49: """ 50: import site 51: import sys 52: 53: sys.path.append(site.getusersitepackages()) # run with -I (it reads downloaded data): numpy is in the user site 54: import copy # noqa: E402 55: import hashlib # noqa: E402 56: import json # noqa: E402 57: import os # noqa: E402 58: import struct # noqa: E402 59: 60: import numpy as np # noqa: E402 61: import scipy.sparse as sp # noqa: E402 62: 63: HERE = os.path.dirname(os.path.abspath(__file__)) 64: sys.path[:0] = [HERE] 65: from vrm import VRM # noqa: E402 66: import collide # noqa: E402 67: 68: VROID = os.environ.get("LOWMARCH_VROID", os.path.expanduser("~/.local/share/lowmarch/vroid")) 69: OUT = os.path.join(VROID, "lowmarch_bodies", "cage") 70: CACHE = os.path.join(OUT, "fits.json") 71: FWD = np.array([0.0, 0.0, -1.0]) # VRoid models face -z, y up; the left side is -x 72: # each scaled bone and the joint its length runs to 73: NEXT = {"Hips": "Spine", "Spine": "Chest", "Chest": "UpperChest", "UpperChest": "Neck", "Neck": "Head", 74: "Shoulder": "UpperArm", "UpperArm": "LowerArm", "LowerArm": "Hand", "UpperLeg": "LowerLeg", "LowerLeg": "Foot"} 75: WHOLE = {"Head": "head", "Hand": "hand", "Foot": "foot"} # scaled whole, with everything under them 76: TORSO_CHAIN = ("Hips", "Spine", "Chest", "UpperChest") 77: # the rings' stations: u runs joint to joint along a limb (arm: upper-arm joint 0, elbow 1, wrist 2; leg: hip 78: # joint 0, knee 1, ankle 2) and up the torso from the hips' joint (0) to the neck's (1); the head's joint 79: # is always unchanged 80: STATIONS = {"torso": {"crotch": -0.25, "hips": -0.05, "waist": 0.3, "chest": 0.6, "shoulders": 0.82, "neck_base": 1.0, 81: "neck": 1.1}, 82: "arm": {"shoulder": 0.0, "biceps": 0.45, "elbow": 1.0, "forearm": 1.3, "wrist": 2.0}, 83: "leg": {"top": 0.0, "thigh": 0.4, "knee": 1.0, "calf": 1.3, "ankle": 2.0}} 84: 85: 86: def _bone(name): 87: """'J_Bip_L_UpperArm' -> 'UpperArm' (sides share a control), anything else -> None.""" 88: if not name.startswith("J_Bip_"): 89: return None 90: return name.split("_", 3)[3] 91: 92: 93: def _frame(a): 94: a = a / np.linalg.norm(a) 95: e1 = FWD - (FWD @ a) * a 96: e1 /= np.linalg.norm(e1) 97: return a, e1, np.cross(a, e1) 98: 99: 100: def _turn(a, b): 101: """The rotation taking unit vector a to unit vector b.""" 102: v, c = np.cross(a, b), float(a @ b) 103: if np.linalg.norm(v) < 1e-9: 104: return np.eye(3) 105: K = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]]) 106: return np.eye(3) + K + K @ K / (1 + c) 107: 108: 109: def rig(v, spec, ref=None): 110: """Every node's old and new rest position and the linear part of its map from old to new (a node's 111: map: p -> J_new + L (p - J_old)). ref: a reference body's joints ({name: position}) and whole-part 112: scales ({"head", "hand", "foot": factor}): the skeleton is put on its joints, each bone turned and 113: stretched from its own joint onto the next, instead of the spec's lengths.""" 114: nodes = v.j["nodes"] 115: W = v.node_world() 116: J = {i: W[i][:3, 3].copy() for i in range(len(nodes))} 117: par = v.parents() 118: names = {i: n.get("name", "") for i, n in enumerate(nodes)} 119: by_name = {n: i for i, n in names.items()} 120: Jn, L = {}, {} 121: 122: def g(key, b): 123: return spec.get(key, {}).get(b, 0.0) 124: 125: order = [] 126: seen = set() 127: 128: def visit(i): 129: if i in seen: 130: return 131: if i in par: 132: visit(par[i]) 133: seen.add(i) 134: order.append(i) 135: 136: for i in range(len(nodes)): 137: visit(i) 138: under_whole = set() # fingers, toes, eyes: they follow their part, never the reference 139: for i in order: 140: if i in par and (par[i] in under_whole or _bone(names[par[i]]) in WHOLE): 141: under_whole.add(i) 142: if ref is not None: # the torso stretches as one from the hips' joint to the neck's, keeping VRoid's own 143: h0, h1 = J[by_name["J_Bip_C_Hips"]], J[by_name["J_Bip_C_Neck"]] # spacing of its spine joints (one 144: r0, r1 = (np.asarray(ref["J"][n], float) for n in ("J_Bip_C_Hips", "J_Bip_C_Neck")) # short bone 145: a, e1, e2 = _frame(h1 - h0) # stretched on its own crumples the skin it carries) 146: k = np.linalg.norm(r1 - r0) / np.linalg.norm(h1 - h0) 147: L_torso = _turn(a, (r1 - r0) / np.linalg.norm(r1 - r0)) @ (k * np.outer(a, a) + np.outer(e1, e1) + np.outer(e2, e2)) 148: for i in order: 149: p = par.get(i) 150: b = _bone(names[i]) 151: spine = b in TORSO_CHAIN and b != "Hips" 152: if ref is not None and names[i] in ref["J"] and i not in under_whole and not spine: 153: Jn[i] = np.asarray(ref["J"][names[i]], float) 154: else: 155: Jn[i] = J[i] if p is None else Jn[p] + L[p] @ (J[i] - J[p]) 156: if ref is not None and b in TORSO_CHAIN: 157: L[i] = L_torso 158: elif b in WHOLE: 159: L[i] = (ref["scale"][WHOLE[b]] if ref else 1.0) * (1.0 + spec.get(WHOLE[b], 0.0)) * np.eye(3) 160: elif b in NEXT: 161: side = names[i].split("_")[2] 162: nxt = by_name.get(f"J_Bip_{side}_{NEXT[b]}") 163: a, e1, e2 = _frame(J[nxt] - J[i]) 164: if ref is not None: 165: r = np.asarray(ref["J"][names[nxt]], float) - Jn[i] 166: k = np.linalg.norm(r) / np.linalg.norm(J[nxt] - J[i]) 167: L[i] = _turn(a, r / np.linalg.norm(r)) @ (k * np.outer(a, a) + np.outer(e1, e1) + np.outer(e2, e2)) 168: else: 169: t = 1.0 + g("thick", b) 170: L[i] = ((1.0 + g("len", b)) * np.outer(a, a) + t * (1.0 + g("deep", b)) * np.outer(e1, e1) 171: + t * (1.0 + g("wide", b)) * np.outer(e2, e2)) 172: elif names[i].endswith("Bust1") and p is not None: # VRoid's own bust bones, scaled whole (its slider) 173: L[i] = (1.0 + spec.get("bust", 0.0)) * L[p] 174: else: 175: L[i] = L[p] if p is not None else np.eye(3) 176: return J, Jn, L, names 177: 178: 179: def _mesh_data(v, mi): 180: mesh = v.j["meshes"][mi] 181: at = mesh["primitives"][0]["attributes"] 182: skin = next(n.get("skin") for n in v.j["nodes"] if n.get("mesh") == mi) 183: joints = v.j["skins"][skin]["joints"] 184: tri = np.concatenate([v.accessor(p["indices"]).reshape(-1, 3).astype(int) for p in mesh["primitives"]]) 185: mats = [v.j["materials"][p["material"]]["name"] for p in mesh["primitives"]] 186: per = [np.unique(v.accessor(p["indices"]).astype(int)) for p in mesh["primitives"]] 187: return dict(at=at, skin=skin, joints=joints, tri=tri, mats=mats, per=per, 188: P=v.accessor(at["POSITION"]).astype(np.float64), N=v.accessor(at["NORMAL"]).astype(np.float64), 189: Ji=v.accessor(at["JOINTS_0"]).astype(int), Wt=v.accessor(at["WEIGHTS_0"]).astype(np.float64), 190: targets=mesh["primitives"][0].get("targets", [])) 191: 192: 193: def _weld(P): 194: """VRoid 2 splits vertices along texture seams: one id per position.""" 195: _, wid = np.unique(np.round(P, 5), axis=0, return_inverse=True) 196: return wid.ravel() 197: 198: 199: def _adjacency(tri, n): 200: i = np.concatenate([tri[:, 0], tri[:, 1], tri[:, 2], tri[:, 1], tri[:, 2], tri[:, 0]]) 201: j = np.concatenate([tri[:, 1], tri[:, 2], tri[:, 0], tri[:, 0], tri[:, 1], tri[:, 2]]) 202: A = sp.coo_matrix((np.ones(len(i)), (i, j)), shape=(n, n)).tocsr() 203: A.data[:] = 1.0 204: deg = np.asarray(A.sum(1)).ravel() 205: return sp.diags(1.0 / np.maximum(deg, 1)) @ A 206: 207: 208: def _smooth(X, M, rounds, keep=None): 209: for _ in range(rounds): 210: Y = 0.5 * X + 0.5 * (M @ X) 211: if keep is not None: 212: Y[keep] = X[keep] 213: X = Y 214: return X 215: 216: 217: def _face_normals(P, tri, n): 218: N = np.zeros((n, 3)) 219: fn = np.cross(P[tri[:, 1]] - P[tri[:, 0]], P[tri[:, 2]] - P[tri[:, 0]]) 220: for k in range(3): 221: np.add.at(N, tri[:, k], fn) 222: return N / np.maximum(np.linalg.norm(N, axis=1, keepdims=True), 1e-12) 223: 224: 225: def _weights(md, names, bones): 226: hit = np.array([any(names[j].endswith(b) for b in bones) for j in md["joints"]]) 227: return (hit[md["Ji"]] * md["Wt"]).sum(1) 228: 229: 230: def _pchip(stations, values, u): 231: from scipy.interpolate import PchipInterpolator 232: o = np.argsort(stations) 233: x, y = np.asarray(stations, float)[o], np.asarray(values, float)[o] 234: if len(x) == 1: 235: return np.full(len(u), y[0]) 236: return PchipInterpolator(x, y, extrapolate=False)(np.clip(u, x[0], x[-1])) 237: 238: 239: def _polyline(P, pts): 240: """Each point's place along a chain of joints (u: 0 at the first, 1 at the next ...; the ends run on past 241: their joints), the nearest point on the chain, and the segment's axis.""" 242: best = np.full(len(P), np.inf) 243: u = np.zeros(len(P)) 244: near = np.zeros_like(P) 245: ax = np.zeros_like(P) 246: for k in range(len(pts) - 1): 247: a, b = pts[k], pts[k + 1] 248: d = b - a 249: t = (P - a) @ d / (d @ d) 250: t = np.clip(t, -np.inf if k == 0 else 0.0, np.inf if k == len(pts) - 2 else 1.0)