A 3D model pipeline, pushed by Gemini 4 Carbon
agent (cont.)
+5:23read_file{"EndLine":250,"Path":"~/work/lowmarch/tools/export_glb.py","StartLine":1}
input
{ "EndLine": 250, "Path": "~/work/lowmarch/tools/export_glb.py", "StartLine": 1 }result, 251 lines
Showing lines 1-250 of 567 1: """Export a composed character (VRoid base + our shapes, costume, props, scenery) to GLB for the 2: browser 3D viewer. 3: 4: Each primitive becomes a mesh named by its group (body, hair, costume pieces, sword, terrain) so 5: the viewer can toggle and x-ray them. Face, eye, line, brow, mouth and hair keep VRoid's textures 6: (downscaled) for alpha and detail; everything else gets the character's palette colour. The model 7: is turned to face +z, the direction a default three.js camera looks from. 8: """ 9: import io 10: import math 11: import json 12: import struct 13: 14: import numpy as np 15: from PIL import Image 16: 17: FLIP = np.array([-1.0, 1.0, -1.0]) 18: TEXTURED = ("face", "iris", "white", "highlight", "line", "brow", "mouth", "hair") 19: ROLE_TONE = {"face": "5", "body": "4", "shadow": "3", "hair": "D", "brow": "B", "coat": "M", "cape": "W", "metal": "8", 20: "signature": "M", "doublet": "S", "heraldry": "N", 21: "steel": "8", "gold": "H", "leather": "v", "boot": "v", "glove": "x", "cloth": "M", 22: "grass": "c", "dirt": "f", "stone": "l", "mouth": "2", "line": "o", "iris": "s", "white": "w", 23: "highlight": "w"} 24: 25: 26: def group_of(p, role): 27: if p.get("world"): 28: return "terrain" 29: if p.get("group"): 30: return p["group"] 31: if role in ("face", "body", "shadow", "iris", "white", "highlight", "line", "brow", "mouth"): 32: return "body" 33: if role == "hair": 34: return "hair" 35: name = p["material"].replace("OUTFIT_", "") 36: for key in ("trim", "edge", "hem", "placket", "buckle", "clasp"): 37: if key in name: 38: return "trim" 39: for key in ("pauldron", "cape", "collar", "tunic", "tails", "belt", "trousers", "boots", "gloves"): 40: if name.startswith(key): 41: return key 42: if name in ("blade", "guard", "grip", "pommel", "fuller"): 43: return "sword" 44: return "costume" 45: 46: 47: def skin_ramp(h): 48: """A skin tone's lit, shade and deep-shade colours from its lit colour. The shade is darker and a little more 49: saturated, its hue nudged toward red on human tones (so deep skin stays rich and warm, not ashy) and toward 50: violet on the fantasy blues, greens and greys. Hex strings, light to dark.""" 51: import colorsys 52: r, g, b = (int(h[i:i + 2], 16) / 255 for i in (1, 3, 5)) 53: hue, sat, val = colorsys.rgb_to_hsv(r, g, b) 54: warm = hue < 0.17 or hue > 0.9 55: dh = -0.015 if warm else (0.025 if hue < 0.75 else -0.025) 56: 57: def tone(k, dv, ds): 58: rr, gg, bb = colorsys.hsv_to_rgb((hue + k * dh) % 1.0, min(1.0, sat + (1 - sat) * ds), val * dv) 59: return "#%02x%02x%02x" % tuple(int(round(c * 255)) for c in (rr, gg, bb)) 60: return [h, tone(1, 0.80, 0.12), tone(2, 0.50, 0.22)] 61: 62: 63: def hex_rgb(h): 64: h = h.lstrip("#") 65: return [int(h[i:i + 2], 16) / 255 for i in (0, 2, 4)] 66: 67: 68: def export(portrait, palette, path, max_tex=512, only_world=False): 69: """portrait: vroid_portrait.Portrait. palette: full char -> hex map. only_world: export just 70: the scenery (the character goes out skinned through export_skinned).""" 71: bin_ = bytearray() 72: views, accessors, meshes, nodes, materials, textures, images = [], [], [], [], [], [], [] 73: tex_index = {} 74: 75: def add_view(data, target=None): 76: while len(bin_) % 4: 77: bin_.append(0) 78: off = len(bin_) 79: bin_.extend(data) 80: v = dict(buffer=0, byteOffset=off, byteLength=len(data)) 81: if target: 82: v["target"] = target 83: views.append(v) 84: return len(views) - 1 85: 86: def add_acc(arr, kind, comp, target): 87: arr = np.ascontiguousarray(arr) 88: vi = add_view(arr.tobytes(), target) 89: a = dict(bufferView=vi, componentType=comp, count=int(arr.shape[0]), type=kind) 90: if kind == "VEC3" and comp == 5126: 91: a["min"] = arr.min(0).tolist() 92: a["max"] = arr.max(0).tolist() 93: accessors.append(a) 94: return len(accessors) - 1 95: 96: def add_texture(key, rgba): 97: if key in tex_index: 98: return tex_index[key] 99: im = Image.fromarray(rgba) 100: if max(im.size) > max_tex: 101: k = max_tex / max(im.size) 102: im = im.resize((max(1, int(im.width * k)), max(1, int(im.height * k))), Image.LANCZOS) 103: b = io.BytesIO() 104: im.save(b, "PNG", optimize=True) 105: vi = add_view(b.getvalue()) 106: images.append(dict(bufferView=vi, mimeType="image/png")) 107: textures.append(dict(source=len(images) - 1, sampler=0)) 108: tex_index[key] = len(textures) - 1 109: return tex_index[key] 110: 111: for k, (p, role) in enumerate(zip(portrait.prims, portrait.roles)): 112: if role == "skip" or not len(p["tri"]) or (only_world and not p.get("world")): 113: continue 114: used, inv = np.unique(p["tri"], return_inverse=True) 115: pos = (p["pos"][used] * FLIP).astype(np.float32) 116: nrm = p["nrm"][used] * FLIP 117: nrm = (nrm / np.maximum(np.linalg.norm(nrm, axis=1, keepdims=True), 1e-9)).astype(np.float32) 118: tri = inv.reshape(-1, 3).astype(np.uint32) # FLIP is a 180-degree turn: winding holds 119: attrs = dict(POSITION=add_acc(pos, "VEC3", 5126, 34962), NORMAL=add_acc(nrm, "VEC3", 5126, 34962)) 120: mat = dict(name=f"{group_of(p, role)}:{role}", doubleSided=True, 121: pbrMetallicRoughness=dict(metallicFactor=0.0, roughnessFactor=0.9)) 122: tone = ROLE_TONE.get(role) 123: color = hex_rgb(palette[tone]) if tone in palette else [0.8, 0.8, 0.8] 124: tex = portrait.tex.get(k) 125: if role == "face": 126: tex = portrait.v.material_texture(p["mi"]) 127: if role in TEXTURED and tex is not None: 128: uv = p["uv"][used].astype(np.float32) 129: attrs["TEXCOORD_0"] = add_acc(uv, "VEC2", 5126, 34962) 130: src = portrait._hair_v if role == "hair" and getattr(portrait, "_hair_v", None) is not None \ 131: and "_HAIR" in p["material"] else portrait.v 132: raw = src.material_texture(p["mi"]) 133: mat["pbrMetallicRoughness"]["baseColorTexture"] = dict(index=add_texture((id(src), p["mi"], role), raw)) 134: if role == "hair": # strands are grey in the texture: tint them 135: mat["pbrMetallicRoughness"]["baseColorFactor"] = color + [1.0] 136: if role in ("hair", "line", "brow", "iris", "highlight"): 137: mat["alphaMode"] = "MASK" 138: mat["alphaCutoff"] = 0.5 139: else: 140: mat["pbrMetallicRoughness"]["baseColorFactor"] = color + [1.0] 141: if role in ("metal", "steel", "gold"): 142: mat["pbrMetallicRoughness"].update(metallicFactor=0.6, roughnessFactor=0.35) 143: materials.append(mat) 144: meshes.append(dict(name=group_of(p, role), primitives=[dict( 145: attributes=attrs, indices=add_acc(tri.reshape(-1), "SCALAR", 5125, 34963), material=len(materials) - 1)])) 146: nodes.append(dict(name=f"{group_of(p, role)}/{p['material']}", mesh=len(meshes) - 1, 147: extras=dict(group=group_of(p, role), role=role, world=bool(p.get("world")), material=p["material"]))) 148: gltf = dict(asset=dict(version="2.0", generator="lowmarch export_glb"), scene=0, 149: scenes=[dict(nodes=list(range(len(nodes))))], nodes=nodes, meshes=meshes, materials=materials, 150: accessors=accessors, bufferViews=views, buffers=[dict(byteLength=len(bin_))], 151: samplers=[dict(magFilter=9729, minFilter=9987)]) 152: if textures: 153: gltf.update(textures=textures, images=images) 154: js = json.dumps(gltf, separators=(",", ":")).encode() 155: js += b" " * ((4 - len(js) % 4) % 4) 156: while len(bin_) % 4: 157: bin_.append(0) 158: out = struct.pack("<4sII", b"glTF", 2, 12 + 8 + len(js) + 8 + len(bin_)) 159: out += struct.pack("<I4s", len(js), b"JSON") + js + struct.pack("<I4s", len(bin_), b"BIN\x00") + bytes(bin_) 160: open(path, "wb").write(out) 161: return len(out) 162: 163: 164: # ---- data for the real-time viewer ---------------------------------------------------------- 165: CAPSULES = [("J_Bip_C_Hips", "J_Bip_C_Spine"), ("J_Bip_C_Spine", "J_Bip_C_Chest"), 166: ("J_Bip_C_Chest", "J_Bip_C_UpperChest"), ("J_Bip_C_UpperChest", "J_Bip_C_Neck"), 167: ("J_Bip_C_Neck", "J_Bip_C_Head")] + \ 168: [(f"J_Bip_{s}_{a}", f"J_Bip_{s}_{b}") for s in "LR" for a, b in 169: (("UpperArm", "LowerArm"), ("LowerArm", "Hand"), ("UpperLeg", "LowerLeg"), ("LowerLeg", "Foot"))] 170: # simulated cloth: piece -> (its static trims, hidden while the cloth simulates; the role whose colour 171: # the simulated border band takes; the bones whose colliders it meets, None for all). Panels hanging 172: # from the belt meet the legs only: the hip collider's round end pushes them out behind. 173: LEGS = ("UpperLeg", "LowerLeg") 174: CLOTH = {"OUTFIT_cape": (("OUTFIT_cape_trim", "OUTFIT_cape_hem", "OUTFIT_cape_edge0", "OUTFIT_cape_edge1"), "gold", None), 175: "OUTFIT_tails": (("OUTFIT_tails_hem",), "gold", LEGS), 176: "OUTFIT_tabard": (("OUTFIT_tabard_hem", "OUTFIT_tabard_edge0", "OUTFIT_tabard_edge1"), "heraldry", LEGS), 177: "OUTFIT_tabard_back": (("OUTFIT_tabard_back_hem", "OUTFIT_tabard_back_edge0", "OUTFIT_tabard_back_edge1"), 178: "heraldry", LEGS), 179: "OUTFIT_skirt": (("OUTFIT_skirt_hem",), "heraldry", LEGS)} 180: CLOTH_HEM_ONLY = {"OUTFIT_skirt"} # border band on the hem row only (its waist has a gold trim) 181: 182: 183: def scene_info(portrait, palette): 184: """Everything the browser's pixel renderer and cloth solver need besides the meshes: palette 185: bands per role, the lights, body colliders, cloth grids and the mouth curve (model space, 186: turned to face +z like the GLB).""" 187: import vroid_portrait as vp 188: from vrm import bone_weight 189: hexp = lambda c: palette[c] 190: bands = lambda tbl: [[thr if thr > -5 else -9, hexp(c)] for thr, c in tbl] 191: roles = {"face": dict(mode="bands", bands=bands(vp.SKIN_BANDS)), 192: "body": dict(mode="bands", bands=bands(vp.SKIN_BANDS), cap=hexp("4")), 193: "shadow": dict(mode="flat", color=hexp("3")), 194: "hair": dict(mode="bands", bands=bands(vp.HAIR_BANDS)), 195: "cloth": dict(mode="bands", bands=bands(vp.CLOTH_BANDS)), 196: "line": dict(mode="flat", color=hexp("o")), "brow": dict(mode="flat", color=hexp("A")), 197: "mouth": dict(mode="flat", color=hexp("1")), "white": dict(mode="flat", color=hexp("w")), 198: "highlight": dict(mode="flat", color=hexp("w")), 199: "iris": dict(mode="iris", colors=[hexp("r"), hexp("s"), hexp("t")], cuts=[0.25, 0.5])} 200: for r, tbl in vp.MATERIALS.items(): 201: roles[r] = dict(mode="bands", bands=bands(tbl), world=r in vp.WORLD_ROLES) 202: names = portrait.v.nodes_by_name() 203: W = portrait.v.node_world(portrait.pose_now) 204: body = [p for p, r in zip(portrait.prims, portrait.roles) if r == "body"][0] 205: caps = [] 206: for a, b in CAPSULES: 207: if a not in names or b not in names: 208: continue 209: A, B = W[names[a]][:3, 3], W[names[b]][:3, 3] 210: wv = bone_weight(body, (a,)) 211: pts = body["pos"][wv > 0.5] 212: if len(pts) < 8: 213: continue 214: ab = B - A 215: t = np.clip(((pts - A) @ ab) / (ab @ ab), 0, 1) 216: d = np.linalg.norm(pts - (A + t[:, None] * ab), axis=1) 217: caps.append(dict(a=(A * FLIP).tolist(), b=(B * FLIP).tolist(), r=float(np.percentile(d, 70) + 0.012), 218: bones=[a, b])) 219: cloth = {} 220: for p in portrait.prims: 221: if p["material"] in CLOTH and "grid" in p: 222: trims, band, collide = CLOTH[p["material"]] 223: cloth[p["material"]] = dict(u=int(p["grid"][0]), v=int(p["grid"][1]), trims=list(trims), band=band, 224: collide=list(collide) if collide else None, closed=bool(p.get("closed")), 225: edges="hem" if p["material"] in CLOTH_HEM_ONLY else "all") 226: curve = None 227: if getattr(portrait, "mouth_curve", None) is not None: 228: face = [p for p, r in zip(portrait.prims, portrait.roles) if r == "face"] 229: allp = np.concatenate([p["pos"] for p in face]) 230: alln = np.concatenate([p["nrm"] for p in face]) 231: from scipy.spatial import cKDTree 232: _, i = cKDTree(allp).query(portrait.mouth_curve) 233: curve = ((portrait.mouth_curve + alln[i] * 0.0015) * FLIP).tolist() 234: marks = None 235: if getattr(portrait, "marks", None): 236: m = next(iter(portrait.marks.values())) 237: img = Image.fromarray(np.where(m == 1, 128, 0).astype(np.uint8), "L") # nose mark only; the mouth is a line 238: b = io.BytesIO() 239: img.save(b, "PNG", optimize=True) 240: import base64 241: marks = base64.b64encode(b.getvalue()).decode() 242: # painted renderer: a material kind and one base colour per role; the shader builds the ramp 243: pc = dict(signature="#7d1e2c", doublet="#2a2d3d", heraldry="#dfe2e8", leather="#5f3e2b", boot="#4a3326", glove="#5f3e2b", 244: steel="#a6adbb", gold="#b08b3f", hair=hexp("C"), trim="#b08b3f") 245: pc.update(getattr(portrait, "paint", None) or {}) 246: skin = dict(kind="skin", base=pc.get("skin") or "#f8e1d5") # light skin unless a tone is set 247: skin["ramp"] = skin_ramp(skin["base"]) # its lit, shade and deep shade 248: paint = {"face": dict(skin), "body": dict(skin), "shadow": dict(skin, shade=True), # shadow cards: skin in shade 249: "hair": dict(kind="hair", base=pc["hair"]), 250: "coat": dict(kind="cloth", base=pc["signature"]), "cape": dict(kind="cloth", base=pc["signature"]),