A 3D model pipeline, pushed by Gemini 4 Carbon
agent
+1:11:45reasoning
The current script attempts to pass hand grip frame data directly, a potential source of mismatch with the expected hand pose. Addressing this, the script will now use the output from the hand frame function directly, ensuring the correct data is used for posing. The pcs_arr variable initialization requires correction, the data type needs to be specified to avoid the 'object' dtype, which could lead to unexpected behavior.
Focusing on the Blender script, specifically lines 954 and 1036-1071 of tools/blender_sculpt_cast.py, the orientation of the gauntlet and its placement on the hand have been examined, including the influence of arm pose on the back-of-hand orientation. The heel and boot definitions are under review. A discrepancy between J_Bip_L_ToeBase and J_Bip_L_Foot in vroid_portrait.py has surfaced, where Y in Blender is -Z in VRM.
read_file{"EndLine":75,"Path":"~/work/lowmarch/tools/mobs.py","StartLine":1}
input
{ "EndLine": 75, "Path": "~/work/lowmarch/tools/mobs.py", "StartLine": 1 }result, 76 lines
Showing lines 1-75 of 311 1: """Common enemy units: one costume per class, built from gear.py on any base body. Generic but detailed: 2: layered padding, mail and plate, riveted straps, an army colour on the cloth and shields (portrait 3: guide, lesson 15). Pair each with a build and a face from gear.BUILDS / gear.FACES in the cast. 4: """ 5: import numpy as np 6: 7: import outfit as O 8: import gear as G 9: from fitting import Surface, merge_surfaces 10: from vrm import bone_weight 11: 12: 13: def _base(fit, shirt="padding", legs="cloth", boots="boot", gloves="glove", knee=None, shirt_subdiv=0, boot_top=None): 14: """Under layers every class shares: a shirt with sleeves to the wrist, trousers, boots to below 15: the knee (or to boot_top: above the knee, they take the thigh too), gloves. Returns the pieces and 16: the shirt (what coats are fitted over).""" 17: B = fit.bone 18: nk, hips = B["J_Bip_C_Neck"], B["J_Bip_C_Hips"] 19: knee = B["J_Bip_L_LowerLeg"][1] if knee is None else knee 20: 21: def cuff(side): 22: a, h = B[f"J_Bip_{side}_LowerArm"], B[f"J_Bip_{side}_Hand"] 23: n = (h - a) / np.linalg.norm(h - a) 24: return n, n @ (h - n * 0.02) 25: torso = ("Spine", "Chest", "UpperChest", "Bust", "Shoulder", "UpperArm", "LowerArm", "Neck", "Hips") 26: # the neckline: a straight hem, lower at the throat than at the back of the neck (a level cut of 27: # whole triangles left a saw-tooth edge, and a big trapezius showed above it) 28: nh = np.array([0.0, 1.0, -0.3]) / np.linalg.norm([0.0, 1.0, -0.3]) 29: neckline = (nh, float(nh @ (nk + np.array([0.0, 0.012, 0.0])))) 30: s = O.wrap(fit, "shirt", shirt, torso, offset=0.006, exclude=("UpperLeg",), y_min=hips[1] - 0.04, 31: y_max=nk[1] + 0.05, cut=neckline, hems=[cuff("L"), cuff("R"), neckline], subdiv=shirt_subdiv) 32: t = O.wrap(fit, "trousers", legs, ("Hips", "UpperLeg", "LowerLeg"), offset=0.007, y_max=hips[1] + 0.02, subdiv=1) 33: top = knee - 0.07 if boot_top is None else boot_top 34: b = O.wrap(fit, "boots", boots, ("LowerLeg", "Foot", "ToeBase") + (("UpperLeg",) if top > knee else ()), offset=0.015, 35: y_max=top, subdiv=1, hems=[((0, 1, 0), top - 0.005)]) 36: g = O.wrap(fit, "gloves", gloves, ("Hand", "Thumb", "Index", "Middle", "Ring", "Little"), offset=0.003, min_w=0.3, smooth=4) 37: out = [p for p in (s, t, b, g) if p is not None] 38: for p, grp in zip(out, ("doublet", "trousers", "boots", "gloves")): 39: p["group"] = grp 40: return out, s, t, b 41: 42: 43: def underwear(fit, band=False): 44: """Plain shorts (and a chest band with band=True) for inspecting bare bodies (the armory page, built 45: in the T-pose): hips to mid-thigh, close to the skin.""" 46: B = fit.bone 47: hips, knee = B["J_Bip_C_Hips"][1], B["J_Bip_L_LowerLeg"][1] 48: lo, hi = (hips + knee) / 2 + 0.03, hips + 0.07 49: # all the skin between the hems but the arms' (they hang there in the fitting pose): taking only skin 50: # weighted to the hips, legs and spine left holes where MakeHuman weights it to the chest or elsewhere 51: out = [O.wrap(fit, "shorts", "cloth", ("J_",), offset=0.004, y_min=lo - 0.04, y_max=hi + 0.04, subdiv=1, 52: exclude=("Shoulder", "Arm", "Hand", "Thumb", "Index", "Middle", "Ring", "Little"), 53: hems=[((0, 1, 0), hi - 0.005), ((0, -1, 0), -(lo + 0.005))], refloor=True, clamp=True)] 54: if band: 55: ch, uc = B["J_Bip_C_Chest"][1], B["J_Bip_C_UpperChest"][1] 56: blo, bhi = ch - 0.01, uc + 0.06 57: # like the shorts: triangles past the hems taken and clamped onto them (dropping them notched the 58: # top edge into steps on broad backs), every bone but the arms' 59: out.append(O.wrap(fit, "band", "cloth", ("J_",), offset=0.004, y_min=blo - 0.03, y_max=bhi + 0.03, subdiv=1, 60: exclude=("Shoulder", "Arm", "Hand", "Thumb", "Index", "Middle", "Ring", "Little"), 61: hems=[((0, 1, 0), bhi - 0.005), ((0, -1, 0), -(blo + 0.005))], refloor=True, clamp=True)) 62: out = [p for p in out if p is not None] 63: for p in out: 64: p["group"] = "underwear" 65: fit.keep_skin = ("J_",) # bare everywhere else: keep all the skin (VRoid 2 weights some to J_Sec_ bones) 66: # but not the skin well inside the shorts: on fitted bodies (tools/fit.py) it came through the waistband. 67: # Never the arms', which hang beside the hips in the fitting pose 68: arms = ("Shoulder", "Arm", "Hand", "Thumb", "Index", "Middle", "Ring", "Little") 69: fit.drop_skin = lambda p: ((p["pos"][:, 1] > lo + 0.015) & (p["pos"][:, 1] < hi - 0.015) 70: & (bone_weight(p, arms) < 0.05)) 71: return out 72: 73: 74: def underwear_f(fit): 75: return underwear(fit, band=True)read_file{"EndLine":310,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":210}
input
{ "EndLine": 310, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 210 }result, 102 lines
Showing lines 210-310 of 1407 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)):read_file{"EndLine":1310,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":1180}
input
{ "EndLine": 1310, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 1180 }result, 132 lines
Showing lines 1180-1310 of 1407 1180: u = uv[mk] 1181: ty = np.clip(((u[:, 1] % 1) * th).astype(int), 0, th - 1) 1182: tx = np.clip(((u[:, 0] % 1) * tw).astype(int), 0, tw - 1) 1183: cls = mark[ty, tx] 1184: on = cls == 2 1185: if self.mouth == "texture+lip": 1186: ty2 = np.clip((((u[:, 1] - LIP_DROP) % 1) * th).astype(int), 0, th - 1) 1187: under = (mark[ty2, tx] == 2) & ~on & (c == "5") 1188: c[under] = "4" 1189: if self.mouth in ("texture", "texture+lip"): 1190: c[on] = "1" 1191: c[cls == 1] = "2" 1192: char[mk] = c 1193: elif r == "shadow": # shadow cards: skin, always in shade 1194: char[mk] = "3" 1195: elif r == "hair": 1196: char[mk] = bands(lit[mk], HAIR_BANDS) 1197: elif r == "cloth": 1198: char[mk] = bands(lit[mk], CLOTH_BANDS) 1199: elif r in MATERIALS: 1200: nn = n[mk] 1201: back = nn[:, 2] < 0 # we see the inside of the piece 1202: lv = np.where(back, (-nn) @ LIGHT, lit[mk]) 1203: c = bands(lv, MATERIALS[r]) 1204: c[back] = [DARKER.get(x_, x_) for x_ in c[back]] 1205: char[mk] = c 1206: elif r in ("line",): 1207: char[mk] = "o" 1208: elif r == "brow": 1209: char[mk] = "A" 1210: elif r == "mouth": 1211: char[mk] = "1" 1212: elif r == "white": 1213: char[mk] = "w" 1214: elif r == "highlight": 1215: char[mk] = "w" 1216: elif r == "iris": 1217: t = self.tex.get(k) 1218: lum = np.full(mk.sum(), 0.5) 1219: if t is not None: 1220: th, tw = t.shape[:2] 1221: u = uv[mk] 1222: px = t[np.clip(((u[:, 1] % 1) * th).astype(int), 0, th - 1), 1223: np.clip(((u[:, 0] % 1) * tw).astype(int), 0, tw - 1), :3].astype(float) 1224: lum = px.mean(1) / 255 1225: char[mk] = np.select([lum < 0.25, lum < 0.5], ["r", "s"], "t") 1226: 1227: # hair depth layering: pixels sitting behind the outer hair surface nearby drop a tone 1228: hm = role == "hair" 1229: if hm.any(): 1230: from scipy.ndimage import maximum_filter 1231: hd = np.where(hm, buf["depth"], -np.inf) 1232: outer = maximum_filter(hd, size=9) 1233: deep = hm & (np.where(hm, outer - np.where(hm, hd, 0), 0) > 0.010) 1234: down = {"E": "D", "D": "C", "C": "B", "B": "A"} 1235: char[deep] = [down.get(c_, c_) for c_ in char[deep]] 1236: 1237: # reduce with feature priority (vectorised: counts per code per 4x4 block) 1238: out, dep, prim, rol = reduce_blocks(char, np.where(m, buf["depth"], -np.inf), np.where(m, pid, -1), role, h, w) 1239: self.scale = s / SS 1240: ink = self.ink(out, dep, prim, rol) 1241: if self.mouth == "drawn" and self.mouth_pts and all(q is not None for q in self.mouth_pts): 1242: pts = project(np.array(self.mouth_pts)) / [SS, SS, 1] 1243: draw_mouth(ink, rol, pts) 1244: if self.mouth in ("curve", "curve+lip") and self.mouth_curve is not None: 1245: draw_curve_mouth(ink, rol, dep, project(self.mouth_curve) / [SS, SS, 1], lip=self.mouth == "curve+lip") 1246: return ink 1247: 1248: def ink(self, c, dep, prim, rol): 1249: """Silhouette outline, separation lines between hair strands, jaw over neck, hair over skin. 1250: For each pixel the first neighbour (right, left, down, up) that triggers a rule decides.""" 1251: h, w = c.shape 1252: new = c.copy() 1253: done = (c == "") | np.isin(c, list("ortsw")) 1254: skin = np.isin(rol, ("face", "body")) 1255: mats = np.isin(rol, ("cloth",) + tuple(MATERIALS)) 1256: world = np.isin(rol, WORLD_ROLES) 1257: for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): 1258: def nb(a, fill): 1259: o = np.full_like(a, fill) 1260: ys, yd = (slice(max(dy, 0), h + min(dy, 0)), slice(max(-dy, 0), h + min(-dy, 0))) 1261: xs, xd = (slice(max(dx, 0), w + min(dx, 0)), slice(max(-dx, 0), w + min(-dx, 0))) 1262: o[yd, xd] = a[ys, xs] 1263: return o 1264: inside = nb(np.ones((h, w), bool), False) 1265: c1, d1, p1, r1 = nb(c, ""), nb(dep, np.inf), nb(prim, -1), nb(rol, "") 1266: res = np.full((h, w), "", "<U1") 1267: res[inside & (c1 == "")] = "o" 1268: far = inside & (c1 != "") & (d1 < dep - 0.012) 1269: hair0, hair1 = rol == "hair", r1 == "hair" 1270: skin1 = np.isin(r1, ("face", "body")) 1271: rule = np.full((h, w), "", "<U1") 1272: rule[mats & ((r1 != rol) | (p1 != prim))] = "o" 1273: rule[world] = "" 1274: rule[world & np.isin(r1, WORLD_ROLES) & (r1 != rol)] = "o" 1275: rule[skin & skin1] = "1" 1276: rule[hair0 & skin1] = "o" 1277: rule[hair0 & hair1 & (p1 != prim)] = "A" 1278: res[(res == "") & far] = rule[(res == "") & far] 1279: hit = ~done & (res != "") 1280: new[hit] = res[hit] 1281: done |= hit 1282: # hair casts a short shadow down onto skin 1283: hair = rol == "hair" 1284: above = np.zeros((h, w), bool) 1285: above[1:] |= hair[:-1] 1286: above[2:] |= hair[:-2] 1287: sh = (rol == "face") & np.isin(new, ("5", "4")) & above 1288: new[sh] = np.where(new[sh] == "5", "4", "3") 1289: return new 1290: 1291: 1292: def uv_to_3d(prims, uv): 1293: """3D point on a mesh at a texture coordinate (first triangle that contains it).""" 1294: uv = np.asarray(uv, float) 1295: for p in prims: 1296: T = p["uv"][p["tri"]] # (M,3,2) 1297: a, b, c = T[:, 0], T[:, 1], T[:, 2] 1298: v0, v1, v2 = b - a, c - a, uv - a 1299: d = v0[:, 0] * v1[:, 1] - v1[:, 0] * v0[:, 1] 1300: ok = np.abs(d) > 1e-12 1301: w1 = np.where(ok, (v2[:, 0] * v1[:, 1] - v1[:, 0] * v2[:, 1]) / np.where(ok, d, 1), -1) 1302: w2 = np.where(ok, (v0[:, 0] * v2[:, 1] - v2[:, 0] * v0[:, 1]) / np.where(ok, d, 1), -1) 1303: inside = ok & (w1 >= 0) & (w2 >= 0) & (w1 + w2 <= 1) 1304: if inside.any(): 1305: i = int(np.argmax(inside)) 1306: P = p["pos"][p["tri"][i]] 1307: return P[0] * (1 - w1[i] - w2[i]) + P[1] * w1[i] + P[2] * w2[i] 1308: # no triangle contains it (a gap between UV islands): take the nearest vertex in UV space 1309: best, bd = None, 1e9 1310: for p in prims:read_file{"EndLine":1180,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":1050}
input
{ "EndLine": 1180, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 1050 }result, 132 lines
Showing lines 1050-1180 of 1407 1050: from scipy.ndimage import binary_dilation, label 1051: self.marks = {} 1052: self.mouth_pts = None 1053: self.mouth_curve = None 1054: for k, p in enumerate(self.prims): 1055: if self.roles[k] != "face": 1056: continue 1057: t = self.v.material_texture(p["mi"]) 1058: if t is None: 1059: continue 1060: th, tw = t.shape[:2] 1061: vv, uu = np.mgrid[0:th, 0:tw] 1062: vv, uu = (vv + 0.5) / th, (uu + 0.5) / tw 1063: win = ((uu > MARK_WINDOW["u"][0]) & (uu < MARK_WINDOW["u"][1]) 1064: & (vv > MARK_WINDOW["v"][0]) & (vv < MARK_WINDOW["v"][1])) 1065: raw = (t[..., :3].mean(-1) / 255 < FACE_MARK) & (t[..., 3] > 0) & win 1066: grown = binary_dilation(raw, iterations=MARK_GROW) & win 1067: # 2 = mouth (dark line), 1 = nose mark (one tone softer) 1068: self.marks[k] = np.where(grown, np.where(vv >= MARK_WINDOW["nose_below"], 2, 1), 0).astype(np.uint8) 1069: if self.mouth_pts is None and raw.any(): 1070: lab, n = label(raw) 1071: if n: 1072: widths = [np.ptp(np.nonzero(lab == i)[1]) for i in range(1, n + 1)] 1073: ys, xs = np.nonzero(lab == 1 + int(np.argmax(widths))) 1074: th, tw = raw.shape 1075: ends = [(xs.min() + 0.5, ys[xs == xs.min()].mean()), (xs.max() + 0.5, ys[xs == xs.max()].mean()), 1076: (xs.mean(), ys.mean())] 1077: face_prims = [q for q, r in zip(self.prims, self.roles) if r == "face"] 1078: pts = [uv_to_3d(face_prims, (ex / tw, ey / th)) for ex, ey in ends] 1079: if pts[0] is not None and pts[1] is not None: 1080: if pts[2] is None: 1081: pts[2] = (pts[0] + pts[1]) / 2 1082: self.mouth_pts = pts 1083: # the painted line's centre, traced across: a 3D curve that hugs the lips 1084: lab_m = lab == 1 + int(np.argmax(widths)) 1085: curve = [] 1086: for x_ in np.linspace(xs.min(), xs.max(), 24): 1087: col = np.nonzero(lab_m[:, int(round(x_))])[0] 1088: if len(col): 1089: q = uv_to_3d(face_prims, ((x_ + 0.5) / tw, (col.mean() + 0.5) / th)) 1090: if q is not None: 1091: curve.append(q) 1092: self.mouth_curve = np.array(curve) if len(curve) >= 4 else None 1093: 1094: def frame(self, view="bust", pitch=None, w=None, h=None): 1095: """The view's camera (self.camera, which the live viewer uses) without rendering.""" 1096: return self._frame(view, pitch, w, h)[0] 1097: 1098: def _frame(self, view, pitch, w, h): 1099: if view == "bust": 1100: w, h = w or BUST_SIZE[0], h or BUST_SIZE[1] 1101: pitch = 4 if pitch is None else pitch 1102: pivot = np.array([0, self.lm["eye_y"] - 0.02, self.eye[2]]) 1103: s = BUST_EYE_CHIN * SS / self.span 1104: (cx, cy), ref_y = (BUST_EYE_AT[0] * SS, BUST_EYE_AT[1] * SS), self.lm["eye_y"] 1105: else: 1106: w, h = w or BATTLE_SIZE[0], h or BATTLE_SIZE[1] 1107: pitch = 10 if pitch is None else pitch 1108: pivot = np.zeros(3) 1109: s = BATTLE_PX_PER_M * SS 1110: (cx, cy), ref_y = (BATTLE_GROUND_AT[0] * SS, BATTLE_GROUND_AT[1] * SS), 0.0 1111: self.camera = dict(view=view, w=w, h=h, pitch=pitch, px_per_m=s / SS, pivot=(pivot * FLIP).tolist(), 1112: pivot_px=[cx / SS, (cy - (pivot[1] - ref_y) * s) / SS]) 1113: return self.camera, (w, h, pitch, pivot, s, cx, cy, ref_y) 1114: 1115: def render(self, yaw=35, pitch=None, view="bust", smooth=None, w=None, h=None): 1116: """view 'bust': the 96x80 portrait framed on the face. view 'battle': the 240x160 battle 1117: scene, whole figure on the ground; scenery stays put while the character turns.""" 1118: _, (w, h, pitch, pivot, s, cx, cy, ref_y) = self._frame(view, pitch, w, h) 1119: W, H = w * SS, h * SS 1120: R = camera_rotation(yaw, pitch) 1121: Rw = camera_rotation(0, pitch) 1122: 1123: def projector(Rm): 1124: def project(P): 1125: q = ((P - pivot) * FLIP) @ Rm.T 1126: return np.stack([cx + q[:, 0] * s, cy - (q[:, 1] + pivot[1] - ref_y) * s, q[:, 2]], -1) 1127: return project 1128: project = projector(R) 1129: project_w = projector(Rw) 1130: 1131: keep = [k for k, p in enumerate(self.prims) 1132: if self.roles[k] != "skip" and len(p["tri"]) 1133: and (view != "bust" or p.get("world") or p["pos"][np.unique(p["tri"])][:, 1].max() > self.chin_y - 0.45)] 1134: prims = [dict(self.prims[k], _screen=project_w(self.prims[k]["pos"])) if self.prims[k].get("world") 1135: else self.prims[k] for k in keep] 1136: cut = {j: self.tex[k] for j, k in enumerate(keep) 1137: if k in self.tex and self.roles[k] in ("line", "brow", "iris", "highlight", "white", "hair", "mouth")} 1138: buf = rasterize(prims, project, W, H, cut) 1139: pid = buf["prim"] 1140: m = pid >= 0 1141: n0 = interpolate(prims, buf, "nrm") 1142: n = (n0 * FLIP) @ R.T 1143: for j, k in enumerate(keep): 1144: if self.prims[k].get("world"): 1145: mk = pid == j 1146: n[mk] = (n0[mk] * FLIP) @ Rw.T 1147: n /= np.maximum(np.linalg.norm(n, axis=-1, keepdims=True), 1e-9) 1148: lit = n @ LIGHT 1149: for j, k in enumerate(keep): 1150: if self.prims[k].get("world"): 1151: mk = pid == j 1152: lit[mk] = n[mk] @ SUN 1153: uv = interpolate(prims, buf, "uv") 1154: 1155: if smooth is not None: 1156: key = np.array([-0.45, 0.55, 0.70]); key /= np.linalg.norm(key) 1157: sh_ = 0.30 + 0.62 * np.clip(n @ key, 0, 1) + 0.18 * np.clip(lit, 0, 1) 1158: img = np.zeros((H, W, 3)); img[:] = (16, 14, 24) 1159: for j, k in enumerate(keep): 1160: mk = pid == j 1161: if mk.any(): 1162: img[mk] = smooth.get(self.roles[k], np.array([200, 200, 200.])) * sh_[mk][:, None] 1163: return Image.fromarray(img.clip(0, 255).astype(np.uint8)) 1164: 1165: char = np.full((H, W), "", "<U1") 1166: role = np.full((H, W), "", "<U9") 1167: for j, k in enumerate(keep): 1168: mk = pid == j 1169: if not mk.any(): 1170: continue 1171: r = self.roles[k] 1172: role[mk] = r 1173: if r in ("face", "body"): 1174: c = bands(lit[mk], SKIN_BANDS) 1175: if r == "body": 1176: c[c == "5"] = "4" 1177: mark = self.marks.get(k) 1178: if mark is not None and self.mouth in ("texture", "texture+lip", "curve", "curve+lip"): 1179: th, tw = mark.shape 1180: u = uv[mk]read_file{"EndLine":1050,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":940}
input
{ "EndLine": 1050, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 940 }result, 112 lines
Showing lines 940-1050 of 1407 940: self.shape = sh 941: self.prims = self.v.primitives(mo, pose) 942: if lines: # a closing lid is the model's own open lash, moved 943: k_, P_open, eye, now = lines 944: p_ = self.prims[k_] 945: keep_lash_weight(p_, P_open, p_["uv"], eye, closure) 946: lash_size(p_["pos"], p_["uv"], {s_: v_ for s_, v_ in eye.items() if closure[s_] <= 0.05}, sh["lash"]) 947: keep_lash_fan(self.prims, now, k_, eye, closure) 948: for side, (up, others, edge, _) in eye.items(): # a shut eye is one closed lid: 949: if closure[side] < 0.9: 950: continue 951: if others: # its lower line goes, 952: p_["tri"] = p_["tri"][~np.isin(p_["tri"], np.concatenate(others)).all(1)] 953: m = np.isin(p_["tri"], up).all(1) # and its lash is drawn solid (export_glb.fill_lid): 954: self.prims.append(dict(p_, tri=p_["tri"][m], lid_fill=float(p_["uv"][edge, 1].max()))) 955: p_["tri"] = p_["tri"][~m] # the open lash's pale streaks and hollows read as 956: # shine beside an eye, but split a lid over skin 957: if hair_from: # swap in another model's hair 958: hv = VRM(hair_from) 959: hp = hv.primitives(None, ref_pose) 960: hroles = [role_of(p["material"]) for p in hp] 961: src = landmarks(hp, hroles) 962: dst = landmarks(self.prims, [role_of(p["material"]) for p in self.prims]) 963: k = (abs(dst["eyeR"][0] - dst["eyeL"][0]) / abs(src["eyeR"][0] - src["eyeL"][0]) 964: + (dst["eye_y"] - dst["chin"][1]) / (src["eye_y"] - src["chin"][1])) / 2 965: c_src = (src["eyeL"] + src["eyeR"]) / 2 966: c_dst = (dst["eyeL"] + dst["eyeR"]) / 2 967: hair = [p for p in hp if "_HAIR" in p["material"]] 968: for p in hair: 969: p["pos"] = c_dst + (p["pos"] - c_src) * k 970: n_ = len(p["pos"]) # rides our head bone from here on 971: p["J"], p["W"], p["jnames"] = np.zeros((n_, 4), np.int64), np.tile([1.0, 0, 0, 0], (n_, 1)), ["J_Bip_C_Head"] 972: self.prims = [p for p in self.prims if "_HAIR" not in p["material"]] + hair 973: self._hair_v = hv 974: if hair_trim: # drop hair pieces reaching past |x| = hair_trim (e.g. twin tails) 975: self.prims = [p for p in self.prims if "_HAIR" not in p["material"] 976: or np.abs(p["pos"][np.unique(p["tri"]), 0]).max() <= hair_trim] 977: self.roles = [p.get("role") or role_of(p["material"]) for p in self.prims] 978: for side, c in eye_closure(mo).items(): # a shut eye shows no white (it can poke between the lids) 979: if c < 0.9: 980: continue 981: for p, r in zip(self.prims, self.roles): 982: if r == "white" and len(p["tri"]): 983: cx = p["pos"][p["tri"]].mean(1)[:, 0] 984: p["tri"] = p["tri"][(cx < 0) != (side == "L")] 985: self.lm = landmarks(self.prims, self.roles) 986: if costume: # replace VRoid's clothes with ours 987: import outfit 988: fit = outfit.Fit(self.v, pose, self.prims, self.roles) 989: import rig 990: fit.grip = rig.grip_points(self.v, sh, ref_pose) # where each fist closes (weapons sit there) 991: keep_ = [k for k, r in enumerate(self.roles) if r != "cloth"] 992: self.prims = [self.prims[k] for k in keep_] 993: self.roles = [self.roles[k] for k in keep_] 994: costume = outfit.COSTUMES[costume] if isinstance(costume, str) else costume 995: fit.keep_skin = () # a costume may leave skin bare (fit.keep_skin = bones) 996: for pc in costume(fit): 997: self.prims.append(pc) 998: self.roles.append(pc["role"]) 999: # body skin below the neck is always under clothes in a bust; drop it so it can't poke through 1000: cy = self.lm["chin"][1] 1001: from vrm import bone_weight 1002: for p, r in zip(self.prims, self.roles): 1003: if r != "body": 1004: continue 1005: if costume: # keep head, neck and upper chest (the throat shows in the collar opening) 1006: wv = bone_weight(p, ("Head", "Neck", "UpperChest") + tuple(getattr(fit, "keep_skin", ()))) 1007: p["tri"] = p["tri"][(wv[p["tri"]] >= 0.5).all(1)] 1008: if getattr(fit, "drop_skin", None) is not None: # a costume may hide the skin it covers 1009: p["tri"] = p["tri"][~fit.drop_skin(p)[p["tri"]].all(1)] 1010: else: 1011: T = p["pos"][p["tri"]] 1012: head = T[:, :, 1].min(1) > cy - 0.02 1013: neck = (T[:, :, 1].min(1) > cy - 0.14) & (np.abs(T[:, :, 0]).max(1) < 0.055) 1014: p["tri"] = p["tri"][head | neck] 1015: for pc in world or []: # scenery: does not turn with the character 1016: self.prims.append(pc) 1017: self.roles.append(pc["role"]) 1018: # framing uses the unshaped face at the base head size, so head-size changes still show 1019: self.span = (self.lm["eye_y"] - self.lm["chin"][1]) / sh["head"] 1020: reshape(self.prims, self.roles, self.lm, sh) 1021: strip_blush(self.prims, self.roles, self.v, sh["blush"]) 1022: face_lines(self.prims, self.roles, self.v, sh["age_lines"]) 1023: lm_now = landmarks(self.prims, self.roles) # after the sculpt 1024: names, Wn = self.v.nodes_by_name(), self.v.node_world(pose) 1025: neck = (Wn[names["J_Bip_C_Neck"]][:3, 3], Wn[names["J_Bip_C_Head"]][:3, 3]) \ 1026: if "J_Bip_C_Neck" in names and "J_Bip_C_Head" in names else None 1027: face_normals(self.prims, self.roles, lm_now, sh["face_normals"], sh["hair_normals"], neck=neck) 1028: for pc, rl in shadow_cards(self.prims, self.roles, lm_now, sh["shadow_cards"], neck): 1029: self.prims.append(pc) 1030: self.roles.append(rl) 1031: if pose is not target_pose and any(not np.allclose(pose.get(k_, np.eye(4)), target_pose.get(k_, np.eye(4))) 1032: for k_ in set(pose) | set(target_pose)): 1033: repose(self.v, [p for p in self.prims if not p.get("world")], pose, target_pose) 1034: names = self.v.nodes_by_name() 1035: Wn = self.v.node_world() 1036: self.eye = (Wn[names["J_Adj_L_FaceEye"]][:3, 3] + Wn[names["J_Adj_R_FaceEye"]][:3, 3]) / 2 1037: self.chin_y = self.lm["chin"][1] 1038: self.tex = {} 1039: for k, p in enumerate(self.prims): 1040: if self.roles[k] in ("line", "brow", "iris", "highlight", "white", "hair", "mouth"): 1041: src = self._hair_v if (hair_from and "_HAIR" in p["material"]) else self.v 1042: t = src.material_texture(p["mi"]) 1043: if t is not None and self.roles[k] == "line": 1044: # VRoid's upper lid is a thick graded band over the iris; keep its dark core only 1045: t = t.copy() 1046: t[..., 3] = np.where(t[..., :3].mean(-1) < LID_CORE, t[..., 3], 0) 1047: if t is not None: 1048: self.tex[k] = t 1049: # painted face marks (lip line, nose) from the face texture, grown so they survive the vote 1050: from scipy.ndimage import binary_dilation, label
agent
+1:12:31reasoning
tools/vroid_portrait.py reveals critical details about how portraits are constructed. Specifically, lines 986-1033 show BUST_POSE is the default posture and is built-in unless overridden. The default pose is important in how the model is designed.
The portrait construction code reveals that the default BUST_POSE is fundamental, used to build the model skin, and it informs how body parts below the "UpperChest" are handled to prevent clothing or other objects from intersecting with the skin. Crucially, the code indicates a potential workaround to maintain custom face and hair normals by setting specific parameters when reshaping the portrait. This suggests the opportunity to avoid default overrides and preserve custom Blender-generated data during portrait generation.
edit_file{"Path":"~/work/lowmarch/tools/blender_sculpt_cast.py","Replacement":" # 8. \"Aurelia\" Swept-Wing Silverwing Longsword (built in right-hand grip frame in BUST_POSE)\n fist_vrm = np.asarray(guard_arm_L_vrm[\"fist_R\"], dtype=np.float64)\n fwd_vrm = np.asarray(guard_arm_L_vrm[\"fwd_R\"], dtype=np.float64)\n side_vrm = np.asarray(guard_arm_L_vrm[\"side_R\"], dtype=np.float64)\n flat_vrm = np.asarray(guard_arm_L_vrm[\"flat_R\"], dtype=np.float64)","Target":" # 8. \"Aurelia\" Swept-Wing Silverwing Longsword (built in right-hand grip frame)\n M_rh = guard_arm_L_vrm[\"M_R_Hand\"]\n R_rh = M_rh[:3, :3] / np.linalg.norm(M_rh[:3, :3], axis=0)\n fist_vrm = guard_arm_L_vrm[\"fist_R\"]\n fwd_vrm = R_rh @ np.array([0.0, 0.0, -1.0])\n side_vrm = R_rh @ np.array([1.0, 0.0, 0.0])\n flat_vrm = np.cross(fwd_vrm, side_vrm)"}
input
{ "Path": "~/work/lowmarch/tools/blender_sculpt_cast.py", "Replacement": " # 8. \"Aurelia\" Swept-Wing Silverwing Longsword (built in right-hand grip frame in BUST_POSE)\n fist_vrm = np.asarray(guard_arm_L_vrm[\"fist_R\"], dtype=np.float64)\n fwd_vrm = np.asarray(guard_arm_L_vrm[\"fwd_R\"], dtype=np.float64)\n side_vrm = np.asarray(guard_arm_L_vrm[\"side_R\"], dtype=np.float64)\n flat_vrm = np.asarray(guard_arm_L_vrm[\"flat_R\"], dtype=np.float64)", "Target": " # 8. \"Aurelia\" Swept-Wing Silverwing Longsword (built in right-hand grip frame)\n M_rh = guard_arm_L_vrm[\"M_R_Hand\"]\n R_rh = M_rh[:3, :3] / np.linalg.norm(M_rh[:3, :3], axis=0)\n fist_vrm = guard_arm_L_vrm[\"fist_R\"]\n fwd_vrm = R_rh @ np.array([0.0, 0.0, -1.0])\n side_vrm = R_rh @ np.array([1.0, 0.0, 0.0])\n flat_vrm = np.cross(fwd_vrm, side_vrm)" }result, 1 line
Made 1 replacement in ~/work/lowmarch/tools/blender_sculpt_cast.py starting at line 1093 (+5/-7 lines).
edit_file{"Path":"~/work/lowmarch/tools/blender_sculpt_cast.py","Replacement":" all_pcs = [(nm, rl, p, n, t, bt, \"hair\") for (nm, rl, p, n, t, bt) in hair_pcs] + armour_pcs\n pcs_arr = np.empty(len(all_pcs), dtype=object)\n for idx_p, item in enumerate(all_pcs):\n pcs_arr[idx_p] = item\n np.savez_compressed(\n out_npz,\n body_pos=body_pos,\n body_nrm=body_nrm,\n face_pos=face_pos,\n face_nrm=face_nrm,\n pieces=pcs_arr,\n )\n elif mode == \"korin\":\n pcs = build_korin_blender_upgrades(\n data[\"face_pos\"], data[\"face_tri\"], data[\"body_pos\"], data[\"body_tri\"], bones_vrm, lm_vrm\n )\n pcs_arr = np.empty(len(pcs), dtype=object)\n for idx_p, item in enumerate(pcs):\n pcs_arr[idx_p] = item\n np.savez_compressed(out_npz, pieces=pcs_arr)","Target":" all_pcs = [(nm, rl, p, n, t, bt, \"hair\") for (nm, rl, p, n, t, bt) in hair_pcs] + armour_pcs\n np.savez_compressed(\n out_npz,\n body_pos=body_pos,\n body_nrm=body_nrm,\n face_pos=face_pos,\n face_nrm=face_nrm,\n pieces=np.array(all_pcs, dtype=object),\n )\n elif mode == \"korin\":\n pcs = build_korin_blender_upgrades(\n data[\"face_pos\"], data[\"face_tri\"], data[\"body_pos\"], data[\"body_tri\"], bones_vrm, lm_vrm\n )\n np.savez_compressed(out_npz, pieces=np.array(pcs, dtype=object))"}
input
{ "Path": "~/work/lowmarch/tools/blender_sculpt_cast.py", "Replacement": " all_pcs = [(nm, rl, p, n, t, bt, \"hair\") for (nm, rl, p, n, t, bt) in hair_pcs] + armour_pcs\n pcs_arr = np.empty(len(all_pcs), dtype=object)\n for idx_p, item in enumerate(all_pcs):\n pcs_arr[idx_p] = item\n np.savez_compressed(\n out_npz,\n body_pos=body_pos,\n body_nrm=body_nrm,\n face_pos=face_pos,\n face_nrm=face_nrm,\n pieces=pcs_arr,\n )\n elif mode == \"korin\":\n pcs = build_korin_blender_upgrades(\n data[\"face_pos\"], data[\"face_tri\"], data[\"body_pos\"], data[\"body_tri\"], bones_vrm, lm_vrm\n )\n pcs_arr = np.empty(len(pcs), dtype=object)\n for idx_p, item in enumerate(pcs):\n pcs_arr[idx_p] = item\n np.savez_compressed(out_npz, pieces=pcs_arr)", "Target": " all_pcs = [(nm, rl, p, n, t, bt, \"hair\") for (nm, rl, p, n, t, bt) in hair_pcs] + armour_pcs\n np.savez_compressed(\n out_npz,\n body_pos=body_pos,\n body_nrm=body_nrm,\n face_pos=face_pos,\n face_nrm=face_nrm,\n pieces=np.array(all_pcs, dtype=object),\n )\n elif mode == \"korin\":\n pcs = build_korin_blender_upgrades(\n data[\"face_pos\"], data[\"face_tri\"], data[\"body_pos\"], data[\"body_tri\"], bones_vrm, lm_vrm\n )\n np.savez_compressed(out_npz, pieces=np.array(pcs, dtype=object))" }result, 1 line
Made 1 replacement in ~/work/lowmarch/tools/blender_sculpt_cast.py starting at line 1422 (+8/-2 lines).