A 3D model pipeline, pushed by Gemini 4 Argon
agent
+41:35reasoning
The script completed successfully, generating the expected VRM and NPZ assets for both characters. Further checking is needed to confirm the model's integrity and visual output within the target environment.
read_file{"EndLine":420,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":360}
input
{ "EndLine": 420, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 360 }result, 62 lines
Showing lines 360-420 of 1399 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 the 400: line sits, as a share of the head's depth in front of its middle. The rig's 0.13 shades too much of a VRoid face 401: (flatter than Yin's) under the armory's key; 0.0 keeps the front lit and shades the far jaw and the neck. 402: `pull`: how far toward the line's middle the normals point (0 = a capsule round the line, 1 = a sphere round its 403: middle). Hair is off by default: in the painted renderer it flattens the strands into one plastic sheen.""" 404: if amount <= 0 and hair <= 0: 405: return 406: chin_y = lm["chin"][1] 407: skin = np.concatenate([p["pos"][np.unique(p["tri"])] for p, r in zip(prims, roles) if r in ("face", "body")]) 408: mid = skin[(np.abs(skin[:, 0]) < 0.03) & (skin[:, 1] > lm["eye_y"])] 409: crown, front, back = mid[:, 1].max(), lm["nose"][2], mid[:, 2].max() # the face looks toward -z 410: tall = crown - chin_y 411: line_z = (front + back) / 2 - forward * (back - front) 412: y0, y1 = chin_y - 0.31 * tall, crown - 0.02 * tall # from under the jaw to near the crown 413: frame = neck_frame(prims, roles, neck) if neck is not None else None 414: for p, r in zip(prims, roles): 415: y = p["pos"][:, 1] 416: wn = None 417: if r in ("face", "brow", "line", "mouth"): 418: w = np.full(len(y), amount) 419: elif r == "body": 420: w = amount * np.clip((y - chin_y) / 0.02, 0, 1)read_file{"EndLine":350,"Path":"~/work/lowmarch/tools/export_glb.py","StartLine":250}
input
{ "EndLine": 350, "Path": "~/work/lowmarch/tools/export_glb.py", "StartLine": 250 }result, 102 lines
Showing lines 250-350 of 567 250: "coat": dict(kind="cloth", base=pc["signature"]), "cape": dict(kind="cloth", base=pc["signature"]), 251: "signature": dict(kind="cloth", base=pc["signature"]), "doublet": dict(kind="cloth", base=pc["doublet"]), 252: "cloth": dict(kind="cloth", base=pc["doublet"]), "heraldry": dict(kind="cloth", base=pc["heraldry"]), 253: "leather": dict(kind="leather", base=pc["leather"]), "boot": dict(kind="leather", base=pc["boot"]), 254: "glove": dict(kind="leather", base=pc["glove"]), "metal": dict(kind="steel", base=pc["steel"]), 255: "steel": dict(kind="steel", base=pc["steel"]), "gold": dict(kind="gold", base=pc["gold"]), 256: "grass": dict(kind="world", base="#5a7a3a"), "dirt": dict(kind="world", base="#6a4a32"), 257: "stone": dict(kind="world", base="#7c7a86"), 258: "motif": dict(kind="cloth", base=pc.get("motif", "#3a44a0"))} # decal paint on steel (tools/plates.py) 259: for role, val in pc.items(): # extra roles a cast entry names (cloth unless it says) 260: if role not in paint and isinstance(val, str) and val.startswith("#"): 261: paint[role] = dict(kind="cloth", base=val) 262: elif role not in paint and isinstance(val, dict) and "base" in val: 263: paint[role] = dict(kind=val.get("kind", "cloth"), base=val["base"]) 264: for role, g in (pc.get("grain") or {}).items(): # optional per-role brush grain (big flat cloth mottles) 265: if role in paint: 266: paint[role]["grain"] = g 267: return dict(roles=roles, light=vp.LIGHT.tolist(), sun=vp.SUN.tolist(), capsules=caps, cloth=cloth, face_marks=marks, 268: paint=paint, 269: mouth_curve=curve, mouth_bone="J_Bip_C_Head", ink=dict(o=hexp("o"), skin=hexp("1"), hair=hexp("A")), 270: skin=[hexp(c) for c in "12345"], depth_gap=0.012) 271: 272: 273: # ---- skinned export: one rigged character + animation clips -------------------------------------------------------- 274: F4 = np.diag([-1.0, 1.0, -1.0, 1.0]) 275: 276: 277: def _quat(R): 278: """Rotation matrix -> quaternion (x, y, z, w).""" 279: t = np.trace(R) 280: if t > 0: 281: s = math.sqrt(t + 1.0) * 2 282: w, x, y, z = 0.25 * s, (R[2, 1] - R[1, 2]) / s, (R[0, 2] - R[2, 0]) / s, (R[1, 0] - R[0, 1]) / s 283: elif R[0, 0] > R[1, 1] and R[0, 0] > R[2, 2]: 284: s = math.sqrt(1.0 + R[0, 0] - R[1, 1] - R[2, 2]) * 2 285: w, x, y, z = (R[2, 1] - R[1, 2]) / s, 0.25 * s, (R[0, 1] + R[1, 0]) / s, (R[0, 2] + R[2, 0]) / s 286: elif R[1, 1] > R[2, 2]: 287: s = math.sqrt(1.0 + R[1, 1] - R[0, 0] - R[2, 2]) * 2 288: w, x, y, z = (R[0, 2] - R[2, 0]) / s, (R[0, 1] + R[1, 0]) / s, 0.25 * s, (R[1, 2] + R[2, 1]) / s 289: else: 290: s = math.sqrt(1.0 + R[2, 2] - R[0, 0] - R[1, 1]) * 2 291: w, x, y, z = (R[1, 0] - R[0, 1]) / s, (R[0, 2] + R[2, 0]) / s, (R[1, 2] + R[2, 1]) / s, 0.25 * s 292: q = np.array([x, y, z, w]) 293: return q / np.linalg.norm(q) 294: 295: 296: def _trs(m): 297: t = m[:3, 3].copy() 298: sc = np.linalg.norm(m[:3, :3], axis=0) 299: return t, _quat(m[:3, :3] / sc), sc 300: 301: 302: def fill_line_specks(rgba, most=400): 303: """A line texture's specks filled: pale or clear texels enclosed by the painted (dark, opaque) line, up to `most` 304: texels a speck, take the line's colour. VRoid paints small highlights inside its lash lines; the painted 305: renderer drops pale texels, so they showed as skin-coloured specks, plainest in a closed lid.""" 306: from scipy import ndimage 307: dark = (rgba[..., 3] >= 128) & (rgba[..., :3].mean(-1) < 0.42 * 255) 308: holes = ndimage.binary_fill_holes(dark) & ~dark 309: lab, n = ndimage.label(holes) 310: if not n: 311: return rgba 312: size = ndimage.sum(holes, lab, np.arange(1, n + 1)) 313: fill = np.isin(lab, np.nonzero(size <= most)[0] + 1) 314: out = rgba.copy() 315: out[fill] = np.append(np.median(rgba[dark][:, :3], 0), 255).astype(rgba.dtype) 316: return out 317: 318: 319: def fill_lid(rgba, v_edge, close=3): 320: """A shut lid's lash texture: the lash as one solid stroke. Above its lid-edge row (v_edge), each eye's half of 321: the texture keeps its main stroke (the biggest piece of ink once gaps up to 2 * close texels are closed, so the 322: wisps touching it join it), solid down each texture column from its top ink to its bottom ink (the lash's 323: thickness runs down v), its pale streaks and hollows taking the line's colour; other ink there (a crease line or 324: a loose wisp above the lash) is cleared. An open eye reads those as shine and a crease; over a closed lid they 325: split it into separate lines. Below the edge row (the corner hook, the lower line's art) is left alone.""" 326: from scipy import ndimage 327: dark = (rgba[..., 3] >= 128) & (rgba[..., :3].mean(-1) < 0.42 * 255) 328: rows = int(np.ceil(v_edge * rgba.shape[0])) + 1 329: d = dark[:rows] 330: st = np.ones((2 * close + 1, 2 * close + 1), bool) 331: cl = ndimage.binary_closing(np.pad(d, close + 1), st)[close + 1:-close - 1, close + 1:-close - 1] 332: keep = np.zeros_like(d) 333: w = d.shape[1] 334: for x0, x1 in ((0, w // 2), (w // 2, w)): # VRoid's eyeline: one eye on each half 335: lab, n = ndimage.label(cl[:, x0:x1]) 336: if n: 337: big = lab == np.argmax(ndimage.sum(cl[:, x0:x1], lab, range(1, n + 1))) + 1 338: r = np.arange(big.shape[0])[:, None] # solid down each column, top ink to bottom ink 339: top, bot = np.argmax(big, 0), big.shape[0] - 1 - np.argmax(big[::-1], 0) 340: keep[:, x0:x1] = (r >= top[None]) & (r <= bot[None]) & big.any(0)[None] 341: out = rgba.copy() 342: top = out[:rows] 343: top[keep] = np.append(np.median(rgba[dark][:, :3], 0), 255).astype(rgba.dtype) 344: top[d & ~keep, 3] = 0 345: return out 346: 347: 348: # VRoid's mouth map has one layout on every face (its UVs): the teeth top left, the tongue top right, the mouth's 349: # inside below. Where to measure each part's own colour on it (u0, u1, v0, v1, image rows from the top). 350: MOUTH_PARTS = dict(teeth=(0.02, 0.33, 0.02, 0.48), tongue=(0.50, 0.99, 0.02, 0.48), inside=(0.02, 0.98, 0.55, 0.95))read_file{"EndLine":567,"Path":"~/work/lowmarch/tools/export_glb.py","StartLine":351}
input
{ "EndLine": 567, "Path": "~/work/lowmarch/tools/export_glb.py", "StartLine": 351 }result, 218 lines
Showing lines 351-567 of 567 351: # what the painted renderer's split (index.html, material kind 10) was tuned on: the inside, the tongue, the teeth 352: MOUTH_EVEN = np.array([[0.80, 0.46, 0.47], [0.93, 0.69, 0.66], [0.99, 0.98, 0.97]]) 353: 354: 355: def even_mouth(rgba): 356: """The mouth map recoloured so the renderer's fixed split into teeth, tongue and dark inside holds on every face. 357: Each map's own inside (median), tongue (its light pink: the 90th percentile of green over the tongue's part) and 358: teeth (90th percentile of their least channel: white, where the tongue's light is pink) are measured, every texel placed between them, and painted 359: in the colours the split was tuned on. A face whose whole mouth is darker (the sexy woman's) read solid maroon 360: under fixed thresholds, with no tongue.""" 361: rgb = rgba[..., :3].astype(np.float64) / 255 362: H, W = rgb.shape[:2] 363: part = {k: rgb[int(v0 * H):int(v1 * H), int(u0 * W):int(u1 * W)].reshape(-1, 3) 364: for k, (u0, u1, v0, v1) in MOUTH_PARTS.items()} 365: g_in, l_in = np.median(part["inside"][:, 1]), np.median(part["inside"].min(1)) 366: g_tg = np.percentile(part["tongue"][:, 1], 90) 367: l_th = np.percentile(part["teeth"].min(1), 90) 368: if g_tg - g_in < 0.03 or l_th - l_in < 0.05: # no tongue or teeth to tell apart: as painted 369: return rgba 370: ss = lambda x, a, b: np.clip((x - a) / (b - a), 0, 1) ** 2 * (3 - 2 * np.clip((x - a) / (b - a), 0, 1)) 371: tongue = ss((rgb[..., 1] - g_in) / (g_tg - g_in), 0.25, 0.45) 372: teeth = ss((rgb.min(-1) - l_in) / (l_th - l_in), 0.55, 0.70) 373: c = MOUTH_EVEN[0] + tongue[..., None] * (MOUTH_EVEN[1] - MOUTH_EVEN[0]) 374: c = c + teeth[..., None] * (MOUTH_EVEN[2] - c) 375: out = rgba.copy() 376: out[..., :3] = np.round(c * 255).astype(rgba.dtype) 377: return out 378: 379: 380: def export_skinned(portrait, palette, path, clips=None, max_tex=512): 381: """The character in its reference pose as a skinned glTF: the VRoid skeleton (with our bone 382: scales) drives the body, face, hair and every costume piece through the weights they carry. 383: clips: {name: [(time, pose dict), ...]} -> glTF animations (rotation, plus translation where a 384: pose moves a bone). Scenery is left out (export it with export()).""" 385: import vroid_portrait as vp 386: v = portrait.v 387: nodes_v = v.j["nodes"] 388: names = v.nodes_by_name() 389: parent = v.parents() 390: used = set() 391: for p in portrait.prims: 392: if "jnames" in p and not p.get("world"): 393: used.update(n for n in p["jnames"] if n in names) 394: joints = set() 395: for nm in used: # every used bone plus its ancestors 396: i = names[nm] 397: while i is not None and i not in joints: 398: joints.add(i) 399: i = parent.get(i) 400: order = sorted(joints) 401: jidx = {nodes_v[i].get("name"): k for k, i in enumerate(order)} 402: ref = portrait.pose_ref 403: local_ref = {i: F4 @ v.local_matrix(i, ref) @ F4 for i in order} 404: world_ref = {} 405: 406: def wref(i): 407: if i not in world_ref: 408: world_ref[i] = (wref(parent[i]) @ local_ref[i]) if parent.get(i) in joints else local_ref[i] 409: return world_ref[i] 410: 411: # glTF assembly (reuses the static exporter's buffer helpers by building a fresh document) 412: bin_ = bytearray() 413: views, accessors, meshes, gnodes, materials, textures, images, anims = [], [], [], [], [], [], [], [] 414: tex_index = {} 415: 416: def add_view(data, target=None): 417: while len(bin_) % 4: 418: bin_.append(0) 419: off = len(bin_) 420: bin_.extend(data) 421: vw = dict(buffer=0, byteOffset=off, byteLength=len(data)) 422: if target: 423: vw["target"] = target 424: views.append(vw) 425: return len(views) - 1 426: 427: def add_acc(arr, kind, comp, target=None, minmax=False): 428: arr = np.ascontiguousarray(arr) 429: a = dict(bufferView=add_view(arr.tobytes(), target), componentType=comp, count=int(arr.shape[0]), type=kind) 430: if minmax or (kind == "VEC3" and comp == 5126): 431: a["min"] = np.atleast_1d(arr.min(0)).tolist() 432: a["max"] = np.atleast_1d(arr.max(0)).tolist() 433: accessors.append(a) 434: return len(accessors) - 1 435: 436: def add_texture(key, rgba, limit=None): 437: if key in tex_index: 438: return tex_index[key] 439: im = Image.fromarray(rgba) 440: lim = limit or max_tex 441: if max(im.size) > lim: 442: k = lim / max(im.size) 443: im = im.resize((max(1, int(im.width * k)), max(1, int(im.height * k))), Image.LANCZOS) 444: b = io.BytesIO() 445: im.save(b, "PNG", optimize=True) 446: images.append(dict(bufferView=add_view(b.getvalue()), mimeType="image/png")) 447: textures.append(dict(source=len(images) - 1, sampler=0)) 448: tex_index[key] = len(textures) - 1 449: return tex_index[key] 450: 451: # joint nodes 452: node_of = {} 453: for i in order: 454: t, q, sc = _trs(local_ref[i]) 455: node_of[i] = len(gnodes) 456: gnodes.append(dict(name=nodes_v[i].get("name"), translation=t.tolist(), rotation=q.tolist(), scale=sc.tolist())) 457: for i in order: 458: kids = [node_of[c] for c in nodes_v[i].get("children", []) if c in joints] 459: if kids: 460: gnodes[node_of[i]]["children"] = kids 461: roots = [node_of[i] for i in order if parent.get(i) not in joints] 462: ibm = np.stack([np.linalg.inv(wref(i)).T for i in order]).astype(np.float32) # column-major 463: skin = dict(joints=[node_of[i] for i in order], inverseBindMatrices=add_acc(ibm.reshape(-1, 16), "MAT4", 5126), 464: skeleton=roots[0]) 465: mesh_nodes = [] 466: for k, (p, role) in enumerate(zip(portrait.prims, portrait.roles)): 467: if role == "skip" or not len(p["tri"]) or p.get("world") or "J" not in p: 468: continue 469: usedv, inv = np.unique(p["tri"], return_inverse=True) 470: pos = (p["pos"][usedv] * FLIP).astype(np.float32) 471: nrm = p["nrm"][usedv] * FLIP 472: nrm = (nrm / np.maximum(np.linalg.norm(nrm, axis=1, keepdims=True), 1e-9)).astype(np.float32) 473: remap = np.array([jidx.get(n, jidx.get("J_Bip_C_Head")) for n in p["jnames"]]) 474: J = remap[p["J"][usedv]].astype(np.uint16) 475: W = p["W"][usedv].astype(np.float32) 476: W /= np.maximum(W.sum(1, keepdims=True), 1e-9) 477: attrs = dict(POSITION=add_acc(pos, "VEC3", 5126, 34962), NORMAL=add_acc(nrm, "VEC3", 5126, 34962), 478: JOINTS_0=add_acc(J, "VEC4", 5123, 34962), WEIGHTS_0=add_acc(W, "VEC4", 5126, 34962)) 479: if "plate" in p: # outline plates (tools/plates.py): border, engraving, gradient 480: attrs["_PLATE"] = add_acc(np.asarray(p["plate"])[usedv].astype(np.float32), "VEC4", 5126, 34962) 481: mat = dict(name=f"{group_of(p, role)}:{role}", doubleSided=True, 482: pbrMetallicRoughness=dict(metallicFactor=0.0, roughnessFactor=0.9)) 483: tone = ROLE_TONE.get(role) 484: extra = getattr(portrait, "paint", None) or {} 485: color = hex_rgb(palette[tone]) if tone in palette else \ 486: hex_rgb(extra[role]) if isinstance(extra.get(role), str) else [0.8, 0.8, 0.8] 487: if role in TEXTURED: 488: src = portrait._hair_v if role == "hair" and getattr(portrait, "_hair_v", None) is not None \ 489: and "_HAIR" in p["material"] else portrait.v 490: raw = src.material_texture(p["mi"]) if p["mi"] >= 0 else None 491: if raw is not None and role == "line": 492: raw = fill_line_specks(raw) 493: if p.get("lid_fill") is not None: # a shut eye's lid (vroid_portrait): its lash solid 494: raw = fill_lid(raw, p["lid_fill"]) 495: if raw is not None and role == "mouth": 496: raw = even_mouth(raw) 497: if raw is not None: 498: attrs["TEXCOORD_0"] = add_acc(p["uv"][usedv].astype(np.float32), "VEC2", 5126, 34962) 499: mat["pbrMetallicRoughness"]["baseColorTexture"] = dict( 500: index=add_texture((id(src), p["mi"], p.get("lid_fill")), raw)) 501: if role == "hair": 502: mat["pbrMetallicRoughness"]["baseColorFactor"] = color + [1.0] 503: if role in ("hair", "line", "brow", "iris", "highlight"): 504: mat["alphaMode"], mat["alphaCutoff"] = "MASK", 0.5 505: else: 506: mat["pbrMetallicRoughness"]["baseColorFactor"] = color + [1.0] 507: else: 508: mat["pbrMetallicRoughness"]["baseColorFactor"] = color + [1.0] 509: if role in ("metal", "steel", "gold"): 510: mat["pbrMetallicRoughness"].update(metallicFactor=0.6, roughnessFactor=0.35) 511: if p.get("strands"): # kit hair (tools/hair.py): one shared strand texture 512: import hair as _hair 513: attrs["TEXCOORD_0"] = add_acc(np.asarray(p["uv"], np.float32)[usedv], "VEC2", 5126, 34962) 514: mat["pbrMetallicRoughness"]["baseColorTexture"] = dict(index=add_texture(("strands",), _hair.STRANDS)) 515: if "decal" in p: # a design in the piece's own coordinates (tools/plates.py Canvas) 516: uvd = np.asarray(p["uv"], np.float32)[usedv].copy() 517: uvd[:, 1] = 1 - uvd[:, 1] # the canvas draws t up; glTF's texture origin is the top left 518: attrs["TEXCOORD_0"] = add_acc(uvd, "VEC2", 5126, 34962) 519: mat["pbrMetallicRoughness"]["baseColorTexture"] = dict(index=add_texture(("decal", k), p["decal"], limit=1024)) 520: mat["extras"] = dict(decal=True) 521: materials.append(mat) 522: meshes.append(dict(name=group_of(p, role), primitives=[dict(attributes=attrs, 523: indices=add_acc(inv.reshape(-1).astype(np.uint32), "SCALAR", 5125, 34963), material=len(materials) - 1)])) 524: mesh_nodes.append(len(gnodes)) 525: gnodes.append(dict(name=f"{group_of(p, role)}/{p['material']}", mesh=len(meshes) - 1, skin=0, 526: extras=dict(group=group_of(p, role), role=role, material=p["material"]))) 527: # animations 528: for name, keys in (clips or {}).items(): 529: times = np.array([t for t, _ in keys], np.float32) 530: tacc = add_acc(times.reshape(-1, 1), "SCALAR", 5126, minmax=True) 531: samplers, channels = [], [] 532: for i in order: 533: mats = [F4 @ v.local_matrix(i, vp.bone_pose(v, portrait.shape, pose)) @ F4 for _, pose in keys] 534: trs = [_trs(m) for m in mats] 535: T = np.array([a for a, _, _ in trs], np.float32) 536: Q = np.array([b for _, b, _ in trs], np.float32) 537: for k in range(1, len(Q)): # keep quaternions on one hemisphere 538: if np.dot(Q[k], Q[k - 1]) < 0: 539: Q[k] = -Q[k] 540: t0, q0, _ = _trs(local_ref[i]) 541: if np.dot(Q[0], q0) < 0: 542: q0 = -q0 543: # a channel whenever the clip leaves the reference pose, even if it then holds still 544: if np.abs(Q - q0).max() > 1e-5: 545: samplers.append(dict(input=tacc, output=add_acc(Q, "VEC4", 5126), interpolation="LINEAR")) 546: channels.append(dict(sampler=len(samplers) - 1, target=dict(node=node_of[i], path="rotation"))) 547: if np.abs(T - t0).max() > 1e-5: 548: samplers.append(dict(input=tacc, output=add_acc(T, "VEC3", 5126), interpolation="LINEAR")) 549: channels.append(dict(sampler=len(samplers) - 1, target=dict(node=node_of[i], path="translation"))) 550: anims.append(dict(name=name, samplers=samplers, channels=channels)) 551: root = len(gnodes) 552: gnodes.append(dict(name="character", children=roots + mesh_nodes)) 553: gltf = dict(asset=dict(version="2.0", generator="lowmarch export_skinned"), scene=0, scenes=[dict(nodes=[root])], 554: nodes=gnodes, meshes=meshes, materials=materials, skins=[skin], accessors=accessors, bufferViews=views, 555: buffers=[dict(byteLength=len(bin_))], samplers=[dict(magFilter=9729, minFilter=9987)]) 556: if textures: 557: gltf.update(textures=textures, images=images) 558: if anims: 559: gltf["animations"] = anims 560: js = json.dumps(gltf, separators=(",", ":")).encode() 561: js += b" " * ((4 - len(js) % 4) % 4) 562: while len(bin_) % 4: 563: bin_.append(0) 564: out = struct.pack("<4sII", b"glTF", 2, 12 + 8 + len(js) + 8 + len(bin_)) 565: out += struct.pack("<I4s", len(js), b"JSON") + js + struct.pack("<I4s", len(bin_), b"BIN\x00") + bytes(bin_) 566: open(path, "wb").write(out) 567: return len(out)
agent
+41:56reasoning
The code checks for "strands" in the input parameters, suggesting a special handling for hair. The import of a "hair" module implies further processing related to hair geometry and textures. This section is adding texture coordinates if hair data is present.
read_file{"EndLine":1240,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":1100}
input
{ "EndLine": 1240, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 1100 }result, 142 lines
Showing lines 1100-1240 of 1399 1100: pivot = np.zeros(3) 1101: s = BATTLE_PX_PER_M * SS 1102: (cx, cy), ref_y = (BATTLE_GROUND_AT[0] * SS, BATTLE_GROUND_AT[1] * SS), 0.0 1103: self.camera = dict(view=view, w=w, h=h, pitch=pitch, px_per_m=s / SS, pivot=(pivot * FLIP).tolist(), 1104: pivot_px=[cx / SS, (cy - (pivot[1] - ref_y) * s) / SS]) 1105: return self.camera, (w, h, pitch, pivot, s, cx, cy, ref_y) 1106: 1107: def render(self, yaw=35, pitch=None, view="bust", smooth=None, w=None, h=None): 1108: """view 'bust': the 96x80 portrait framed on the face. view 'battle': the 240x160 battle 1109: scene, whole figure on the ground; scenery stays put while the character turns.""" 1110: _, (w, h, pitch, pivot, s, cx, cy, ref_y) = self._frame(view, pitch, w, h) 1111: W, H = w * SS, h * SS 1112: R = camera_rotation(yaw, pitch) 1113: Rw = camera_rotation(0, pitch) 1114: 1115: def projector(Rm): 1116: def project(P): 1117: q = ((P - pivot) * FLIP) @ Rm.T 1118: return np.stack([cx + q[:, 0] * s, cy - (q[:, 1] + pivot[1] - ref_y) * s, q[:, 2]], -1) 1119: return project 1120: project = projector(R) 1121: project_w = projector(Rw) 1122: 1123: keep = [k for k, p in enumerate(self.prims) 1124: if self.roles[k] != "skip" and len(p["tri"]) 1125: and (view != "bust" or p.get("world") or p["pos"][np.unique(p["tri"])][:, 1].max() > self.chin_y - 0.45)] 1126: prims = [dict(self.prims[k], _screen=project_w(self.prims[k]["pos"])) if self.prims[k].get("world") 1127: else self.prims[k] for k in keep] 1128: cut = {j: self.tex[k] for j, k in enumerate(keep) 1129: if k in self.tex and self.roles[k] in ("line", "brow", "iris", "highlight", "white", "hair", "mouth")} 1130: buf = rasterize(prims, project, W, H, cut) 1131: pid = buf["prim"] 1132: m = pid >= 0 1133: n0 = interpolate(prims, buf, "nrm") 1134: n = (n0 * FLIP) @ R.T 1135: for j, k in enumerate(keep): 1136: if self.prims[k].get("world"): 1137: mk = pid == j 1138: n[mk] = (n0[mk] * FLIP) @ Rw.T 1139: n /= np.maximum(np.linalg.norm(n, axis=-1, keepdims=True), 1e-9) 1140: lit = n @ LIGHT 1141: for j, k in enumerate(keep): 1142: if self.prims[k].get("world"): 1143: mk = pid == j 1144: lit[mk] = n[mk] @ SUN 1145: uv = interpolate(prims, buf, "uv") 1146: 1147: if smooth is not None: 1148: key = np.array([-0.45, 0.55, 0.70]); key /= np.linalg.norm(key) 1149: sh_ = 0.30 + 0.62 * np.clip(n @ key, 0, 1) + 0.18 * np.clip(lit, 0, 1) 1150: img = np.zeros((H, W, 3)); img[:] = (16, 14, 24) 1151: for j, k in enumerate(keep): 1152: mk = pid == j 1153: if mk.any(): 1154: img[mk] = smooth.get(self.roles[k], np.array([200, 200, 200.])) * sh_[mk][:, None] 1155: return Image.fromarray(img.clip(0, 255).astype(np.uint8)) 1156: 1157: char = np.full((H, W), "", "<U1") 1158: role = np.full((H, W), "", "<U9") 1159: for j, k in enumerate(keep): 1160: mk = pid == j 1161: if not mk.any(): 1162: continue 1163: r = self.roles[k] 1164: role[mk] = r 1165: if r in ("face", "body"): 1166: c = bands(lit[mk], SKIN_BANDS) 1167: if r == "body": 1168: c[c == "5"] = "4" 1169: mark = self.marks.get(k) 1170: if mark is not None and self.mouth in ("texture", "texture+lip", "curve", "curve+lip"): 1171: th, tw = mark.shape 1172: u = uv[mk] 1173: ty = np.clip(((u[:, 1] % 1) * th).astype(int), 0, th - 1) 1174: tx = np.clip(((u[:, 0] % 1) * tw).astype(int), 0, tw - 1) 1175: cls = mark[ty, tx] 1176: on = cls == 2 1177: if self.mouth == "texture+lip": 1178: ty2 = np.clip((((u[:, 1] - LIP_DROP) % 1) * th).astype(int), 0, th - 1) 1179: under = (mark[ty2, tx] == 2) & ~on & (c == "5") 1180: c[under] = "4" 1181: if self.mouth in ("texture", "texture+lip"): 1182: c[on] = "1" 1183: c[cls == 1] = "2" 1184: char[mk] = c 1185: elif r == "shadow": # shadow cards: skin, always in shade 1186: char[mk] = "3" 1187: elif r == "hair": 1188: char[mk] = bands(lit[mk], HAIR_BANDS) 1189: elif r == "cloth": 1190: char[mk] = bands(lit[mk], CLOTH_BANDS) 1191: elif r in MATERIALS: 1192: nn = n[mk] 1193: back = nn[:, 2] < 0 # we see the inside of the piece 1194: lv = np.where(back, (-nn) @ LIGHT, lit[mk]) 1195: c = bands(lv, MATERIALS[r]) 1196: c[back] = [DARKER.get(x_, x_) for x_ in c[back]] 1197: char[mk] = c 1198: elif r in ("line",): 1199: char[mk] = "o" 1200: elif r == "brow": 1201: char[mk] = "A" 1202: elif r == "mouth": 1203: char[mk] = "1" 1204: elif r == "white": 1205: char[mk] = "w" 1206: elif r == "highlight": 1207: char[mk] = "w" 1208: elif r == "iris": 1209: t = self.tex.get(k) 1210: lum = np.full(mk.sum(), 0.5) 1211: if t is not None: 1212: th, tw = t.shape[:2] 1213: u = uv[mk] 1214: px = t[np.clip(((u[:, 1] % 1) * th).astype(int), 0, th - 1), 1215: np.clip(((u[:, 0] % 1) * tw).astype(int), 0, tw - 1), :3].astype(float) 1216: lum = px.mean(1) / 255 1217: char[mk] = np.select([lum < 0.25, lum < 0.5], ["r", "s"], "t") 1218: 1219: # hair depth layering: pixels sitting behind the outer hair surface nearby drop a tone 1220: hm = role == "hair" 1221: if hm.any(): 1222: from scipy.ndimage import maximum_filter 1223: hd = np.where(hm, buf["depth"], -np.inf) 1224: outer = maximum_filter(hd, size=9) 1225: deep = hm & (np.where(hm, outer - np.where(hm, hd, 0), 0) > 0.010) 1226: down = {"E": "D", "D": "C", "C": "B", "B": "A"} 1227: char[deep] = [down.get(c_, c_) for c_ in char[deep]] 1228: 1229: # reduce with feature priority (vectorised: counts per code per 4x4 block) 1230: out, dep, prim, rol = reduce_blocks(char, np.where(m, buf["depth"], -np.inf), np.where(m, pid, -1), role, h, w) 1231: self.scale = s / SS 1232: ink = self.ink(out, dep, prim, rol) 1233: if self.mouth == "drawn" and self.mouth_pts and all(q is not None for q in self.mouth_pts): 1234: pts = project(np.array(self.mouth_pts)) / [SS, SS, 1] 1235: draw_mouth(ink, rol, pts) 1236: if self.mouth in ("curve", "curve+lip") and self.mouth_curve is not None: 1237: draw_curve_mouth(ink, rol, dep, project(self.mouth_curve) / [SS, SS, 1], lip=self.mouth == "curve+lip") 1238: return ink 1239: 1240: def ink(self, c, dep, prim, rol):read_file{"EndLine":1100,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":950}
input
{ "EndLine": 1100, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 950 }result, 152 lines
Showing lines 950-1100 of 1399 950: hv = VRM(hair_from) 951: hp = hv.primitives(None, ref_pose) 952: hroles = [role_of(p["material"]) for p in hp] 953: src = landmarks(hp, hroles) 954: dst = landmarks(self.prims, [role_of(p["material"]) for p in self.prims]) 955: k = (abs(dst["eyeR"][0] - dst["eyeL"][0]) / abs(src["eyeR"][0] - src["eyeL"][0]) 956: + (dst["eye_y"] - dst["chin"][1]) / (src["eye_y"] - src["chin"][1])) / 2 957: c_src = (src["eyeL"] + src["eyeR"]) / 2 958: c_dst = (dst["eyeL"] + dst["eyeR"]) / 2 959: hair = [p for p in hp if "_HAIR" in p["material"]] 960: for p in hair: 961: p["pos"] = c_dst + (p["pos"] - c_src) * k 962: n_ = len(p["pos"]) # rides our head bone from here on 963: 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"] 964: self.prims = [p for p in self.prims if "_HAIR" not in p["material"]] + hair 965: self._hair_v = hv 966: if hair_trim: # drop hair pieces reaching past |x| = hair_trim (e.g. twin tails) 967: self.prims = [p for p in self.prims if "_HAIR" not in p["material"] 968: or np.abs(p["pos"][np.unique(p["tri"]), 0]).max() <= hair_trim] 969: self.roles = [p.get("role") or role_of(p["material"]) for p in self.prims] 970: for side, c in eye_closure(mo).items(): # a shut eye shows no white (it can poke between the lids) 971: if c < 0.9: 972: continue 973: for p, r in zip(self.prims, self.roles): 974: if r == "white" and len(p["tri"]): 975: cx = p["pos"][p["tri"]].mean(1)[:, 0] 976: p["tri"] = p["tri"][(cx < 0) != (side == "L")] 977: self.lm = landmarks(self.prims, self.roles) 978: if costume: # replace VRoid's clothes with ours 979: import outfit 980: fit = outfit.Fit(self.v, pose, self.prims, self.roles) 981: import rig 982: fit.grip = rig.grip_points(self.v, sh, ref_pose) # where each fist closes (weapons sit there) 983: keep_ = [k for k, r in enumerate(self.roles) if r != "cloth"] 984: self.prims = [self.prims[k] for k in keep_] 985: self.roles = [self.roles[k] for k in keep_] 986: costume = outfit.COSTUMES[costume] if isinstance(costume, str) else costume 987: fit.keep_skin = () # a costume may leave skin bare (fit.keep_skin = bones) 988: for pc in costume(fit): 989: self.prims.append(pc) 990: self.roles.append(pc["role"]) 991: # body skin below the neck is always under clothes in a bust; drop it so it can't poke through 992: cy = self.lm["chin"][1] 993: from vrm import bone_weight 994: for p, r in zip(self.prims, self.roles): 995: if r != "body": 996: continue 997: if costume: # keep head, neck and upper chest (the throat shows in the collar opening) 998: wv = bone_weight(p, ("Head", "Neck", "UpperChest") + tuple(getattr(fit, "keep_skin", ()))) 999: p["tri"] = p["tri"][(wv[p["tri"]] >= 0.5).all(1)] 1000: if getattr(fit, "drop_skin", None) is not None: # a costume may hide the skin it covers 1001: p["tri"] = p["tri"][~fit.drop_skin(p)[p["tri"]].all(1)] 1002: else: 1003: T = p["pos"][p["tri"]] 1004: head = T[:, :, 1].min(1) > cy - 0.02 1005: neck = (T[:, :, 1].min(1) > cy - 0.14) & (np.abs(T[:, :, 0]).max(1) < 0.055) 1006: p["tri"] = p["tri"][head | neck] 1007: for pc in world or []: # scenery: does not turn with the character 1008: self.prims.append(pc) 1009: self.roles.append(pc["role"]) 1010: # framing uses the unshaped face at the base head size, so head-size changes still show 1011: self.span = (self.lm["eye_y"] - self.lm["chin"][1]) / sh["head"] 1012: reshape(self.prims, self.roles, self.lm, sh) 1013: strip_blush(self.prims, self.roles, self.v, sh["blush"]) 1014: face_lines(self.prims, self.roles, self.v, sh["age_lines"]) 1015: lm_now = landmarks(self.prims, self.roles) # after the sculpt 1016: names, Wn = self.v.nodes_by_name(), self.v.node_world(pose) 1017: neck = (Wn[names["J_Bip_C_Neck"]][:3, 3], Wn[names["J_Bip_C_Head"]][:3, 3]) \ 1018: if "J_Bip_C_Neck" in names and "J_Bip_C_Head" in names else None 1019: face_normals(self.prims, self.roles, lm_now, sh["face_normals"], sh["hair_normals"], neck=neck) 1020: for pc, rl in shadow_cards(self.prims, self.roles, lm_now, sh["shadow_cards"], neck): 1021: self.prims.append(pc) 1022: self.roles.append(rl) 1023: if pose is not target_pose and any(not np.allclose(pose.get(k_, np.eye(4)), target_pose.get(k_, np.eye(4))) 1024: for k_ in set(pose) | set(target_pose)): 1025: repose(self.v, [p for p in self.prims if not p.get("world")], pose, target_pose) 1026: names = self.v.nodes_by_name() 1027: Wn = self.v.node_world() 1028: self.eye = (Wn[names["J_Adj_L_FaceEye"]][:3, 3] + Wn[names["J_Adj_R_FaceEye"]][:3, 3]) / 2 1029: self.chin_y = self.lm["chin"][1] 1030: self.tex = {} 1031: for k, p in enumerate(self.prims): 1032: if self.roles[k] in ("line", "brow", "iris", "highlight", "white", "hair", "mouth"): 1033: src = self._hair_v if (hair_from and "_HAIR" in p["material"]) else self.v 1034: t = src.material_texture(p["mi"]) 1035: if t is not None and self.roles[k] == "line": 1036: # VRoid's upper lid is a thick graded band over the iris; keep its dark core only 1037: t = t.copy() 1038: t[..., 3] = np.where(t[..., :3].mean(-1) < LID_CORE, t[..., 3], 0) 1039: if t is not None: 1040: self.tex[k] = t 1041: # painted face marks (lip line, nose) from the face texture, grown so they survive the vote 1042: from scipy.ndimage import binary_dilation, label 1043: self.marks = {} 1044: self.mouth_pts = None 1045: self.mouth_curve = None 1046: for k, p in enumerate(self.prims): 1047: if self.roles[k] != "face": 1048: continue 1049: t = self.v.material_texture(p["mi"]) 1050: if t is None: 1051: continue 1052: th, tw = t.shape[:2] 1053: vv, uu = np.mgrid[0:th, 0:tw] 1054: vv, uu = (vv + 0.5) / th, (uu + 0.5) / tw 1055: win = ((uu > MARK_WINDOW["u"][0]) & (uu < MARK_WINDOW["u"][1]) 1056: & (vv > MARK_WINDOW["v"][0]) & (vv < MARK_WINDOW["v"][1])) 1057: raw = (t[..., :3].mean(-1) / 255 < FACE_MARK) & (t[..., 3] > 0) & win 1058: grown = binary_dilation(raw, iterations=MARK_GROW) & win 1059: # 2 = mouth (dark line), 1 = nose mark (one tone softer) 1060: self.marks[k] = np.where(grown, np.where(vv >= MARK_WINDOW["nose_below"], 2, 1), 0).astype(np.uint8) 1061: if self.mouth_pts is None and raw.any(): 1062: lab, n = label(raw) 1063: if n: 1064: widths = [np.ptp(np.nonzero(lab == i)[1]) for i in range(1, n + 1)] 1065: ys, xs = np.nonzero(lab == 1 + int(np.argmax(widths))) 1066: th, tw = raw.shape 1067: ends = [(xs.min() + 0.5, ys[xs == xs.min()].mean()), (xs.max() + 0.5, ys[xs == xs.max()].mean()), 1068: (xs.mean(), ys.mean())] 1069: face_prims = [q for q, r in zip(self.prims, self.roles) if r == "face"] 1070: pts = [uv_to_3d(face_prims, (ex / tw, ey / th)) for ex, ey in ends] 1071: if pts[0] is not None and pts[1] is not None: 1072: if pts[2] is None: 1073: pts[2] = (pts[0] + pts[1]) / 2 1074: self.mouth_pts = pts 1075: # the painted line's centre, traced across: a 3D curve that hugs the lips 1076: lab_m = lab == 1 + int(np.argmax(widths)) 1077: curve = [] 1078: for x_ in np.linspace(xs.min(), xs.max(), 24): 1079: col = np.nonzero(lab_m[:, int(round(x_))])[0] 1080: if len(col): 1081: q = uv_to_3d(face_prims, ((x_ + 0.5) / tw, (col.mean() + 0.5) / th)) 1082: if q is not None: 1083: curve.append(q) 1084: self.mouth_curve = np.array(curve) if len(curve) >= 4 else None 1085: 1086: def frame(self, view="bust", pitch=None, w=None, h=None): 1087: """The view's camera (self.camera, which the live viewer uses) without rendering.""" 1088: return self._frame(view, pitch, w, h)[0] 1089: 1090: def _frame(self, view, pitch, w, h): 1091: if view == "bust": 1092: w, h = w or BUST_SIZE[0], h or BUST_SIZE[1] 1093: pitch = 4 if pitch is None else pitch 1094: pivot = np.array([0, self.lm["eye_y"] - 0.02, self.eye[2]]) 1095: s = BUST_EYE_CHIN * SS / self.span 1096: (cx, cy), ref_y = (BUST_EYE_AT[0] * SS, BUST_EYE_AT[1] * SS), self.lm["eye_y"] 1097: else: 1098: w, h = w or BATTLE_SIZE[0], h or BATTLE_SIZE[1] 1099: pitch = 10 if pitch is None else pitch 1100: pivot = np.zeros(3)