A 3D model pipeline, pushed by Gemini 4 Argon
agent (cont.)
+12:04read_file{"EndLine":1060,"Path":"~/work/lowmarch/tools/outfit.py","StartLine":950}
input
{ "EndLine": 1060, "Path": "~/work/lowmarch/tools/outfit.py", "StartLine": 950 }result, 112 lines
Showing lines 950-1060 of 1272 950: 951: def grouped(pcs, g): 952: for pc in pcs: 953: if pc is not None: 954: pc["group"] = g 955: return [pc for pc in pcs if pc is not None] 956: 957: def rolled(name, pc, rims, r=0.0024, role="metal"): 958: return [inherit(pipe(f"{name}_roll{k}", role, rim, r=r), pc) for k, rim in enumerate(rims)] 959: 960: def cuff_plane(side): 961: a, hd = B[f"J_Bip_{side}_LowerArm"], B[f"J_Bip_{side}_Hand"] 962: n = (hd - a) / np.linalg.norm(hd - a) 963: return n, n @ (hd - n * 0.035) 964: 965: torso = ("Spine", "Chest", "UpperChest", "Bust", "Shoulder", "UpperArm", "LowerArm", "Neck", "Hips") 966: bodice = wrap(fit, "bodice", "signature", torso, offset=0.006, exclude=("UpperLeg",), 967: y_min=hips[1] + 0.02, y_max=nk[1] + 0.012, hems=[cuff_plane("L"), cuff_plane("R")]) 968: tights = wrap(fit, "tights", "doublet", ("Hips", "UpperLeg", "LowerLeg"), offset=0.004, y_max=hips[1] + 0.03, subdiv=1) 969: boots = wrap(fit, "boots", "metal", ("LowerLeg", "Foot"), offset=0.014, y_max=knee - 0.06, exclude=("ToeBase",), 970: subdiv=1, hems=[((0, 1, 0), knee - 0.065)]) 971: gloves = wrap(fit, "gloves", "metal", ("Hand", "Thumb", "Index", "Middle", "Ring", "Little"), 972: offset=0.0035, min_w=0.3, smooth=4) 973: out += grouped([bodice], "dress") + grouped([tights], "trousers") + grouped([boots], "boots") + grouped([gloves], "gloves") 974: out += grouped(sabaton(fit, "sabaton_L", "L") + sabaton(fit, "sabaton_R", "R"), "boots") 975: col, rim, _ = collar(fit, "collar", "signature", height_back=0.034, height_front=0.022) 976: out += grouped([col, inherit(pipe("collar_trim", "gold", rim, r=0.0022), col)], "dress") 977: 978: # steel breastplate, fitted, ending at the natural waist; gold line along the neckline 979: cuirass = wrap(fit, "breastplate", "steel", ("Spine", "Chest", "UpperChest", "Bust"), offset=0.010, # the female 980: # base weights the chest to its own Bust bones: leave them out and the plate has two holes 981: exclude=("Shoulder", "UpperArm", "Neck"), y_min=hips[1] + 0.085, y_max=nk[1] - 0.065, 982: base=Surface(bodice["pos"], bodice["tri"]), smooth=70, # a rigid shell, not a moulded bust 983: hems=[((0, 1, 0), hips[1] + 0.095), ((0, 1, 0), nk[1] - 0.075)]) 984: loops = sorted([cuirass["pos"][L] for L in cuirass["loops"] if len(L) > 12], key=lambda Lp: -Lp[:, 1].mean()) 985: out += grouped([cuirass] + [inherit(pipe(f"breastplate_roll{k}", "gold" if k == 0 else "metal", Lp, r=0.003, 986: closed=True), cuirass) for k, Lp in enumerate(loops)], "plate") 987: 988: # the skirt from the waist to the ankle, white petticoat edge (the viewer's border band) 989: legs = merge_surfaces((tights["pos"], tights["tri"]), (boots["pos"], boots["tri"])) 990: # the waist ring is cast onto the bodice without its sleeves: with them, the hanging arms beside 991: # the hips held the ring out at the sides, a shelf the user saw once the arms moved 992: waist = merge_surfaces(without(bodice, ("Shoulder", "UpperArm", "LowerArm", "Hand")), (tights["pos"], tights["tri"])) 993: ytop = hips[1] + 0.08 994: sk, grid = skirt(fit, "skirt", "signature", waist, legs, ytop, ytop - 0.24, flare=0.17) # hem above the ankle 995: sk["group"] = "dress" 996: out.append(sk) 997: out += grouped([inherit(ribbon("skirt_hem", "heraldry", np.concatenate([grid[:, -1], grid[:1, -1]]), width=0.03, 998: lift=0.0015, normals=near_normals(sk, np.concatenate([grid[:, -1], grid[:1, -1]]))), sk)], 999: "dress") 1000: out += grouped([inherit(pipe("waist_trim", "gold", grid[:, 0], r=0.0035, closed=True), sk)], "dress") 1001: 1002: plate_body = merge_surfaces((cuirass["pos"], cuirass["tri"]), (bodice["pos"], bodice["tri"]), (fit.pos, fit.tri)) 1003: for side in ("L", "R"): 1004: ll, ft = B[f"J_Bip_{side}_LowerLeg"], B[f"J_Bip_{side}_Foot"] 1005: la, hd = B[f"J_Bip_{side}_LowerArm"], B[f"J_Bip_{side}_Hand"] 1006: sp, rims = pauldron(fit, f"spaulder_{side}", "steel", plate_body, side=side, 1007: lames=((0, 54, 0.016, 0.022), (46, 76, 0.014, 0.020)), spread=105) 1008: out += grouped(sp, "plate") + grouped(sum((rolled(f"spaulder_{side}{k}", pc, [r_]) for k, (pc, r_) in 1009: enumerate(zip(sp, rims))), []), "plate") 1010: fore = limb_surface(fit, (f"{side}_LowerArm",), side) 1011: vb, rims = limb_lames(fit, f"vambrace_{side}", "steel", fore, la, hd, ((0.10, 0.97, 0.014, 0.017),), rmax=0.11) 1012: fit.bind(vb[0], rigid=f"J_Bip_{side}_LowerArm") 1013: out += grouped(vb + rolled(f"vambrace_{side}", vb[0], rims[0]), "plate") 1014: arm = limb_surface(fit, (f"{side}_LowerArm", f"{side}_UpperArm"), side) 1015: jg, jr = joint_guard(fit, f"couter_{side}", f"J_Bip_{side}_LowerArm", f"J_Bip_{side}_Hand", (0, 0, 1), side, 1016: arm, r=0.026, wing=0.042, depth=0.018) 1017: for pc in jg: 1018: fit.bind(pc, rigid=f"J_Bip_{side}_LowerArm") 1019: out += grouped(jg, "plate") 1020: shin = side_of(boots, side) 1021: # the greave starts below the boot's top: above it the casts find no boot and the knee bulges past it 1022: top = (knee - 0.075 - ll[1]) / (ft[1] - ll[1]) 1023: gv, rims = limb_lames(fit, f"greave_{side}", "steel", shin, ll, ft, ((top, 0.80, 0.006, 0.009),), rmax=0.11) 1024: fit.bind(gv[0], rigid=f"J_Bip_{side}_LowerLeg") 1025: out += grouped(gv + rolled(f"greave_{side}", gv[0], rims[0]), "plate") 1026: 1027: import hair 1028: out += [pc for pc in updo(fit) if pc["material"] != "OUTFIT_cowlick"] + hair.elaine(fit) 1029: out += sword(fit, "R", blade_len=0.84, blade_w=0.046, grip_len=0.21, hilt="gold", grip="signature") 1030: return [p for p in out if p is not None] 1031: 1032: 1033: # ---- costumes ------------------------------------------------------------------------- 1034: def lord_tunic(fit): 1035: """The first lord costume, head to toe: tunic, tails, belt, trousers, boots, gloves, a standing collar, 1036: shoulder plates on the near (left) shoulder over a cape clasped at the collarbones, gold trim, 1037: and a sword in the right hand.""" 1038: B = fit.bone 1039: nk, hips = B["J_Bip_C_Neck"], B["J_Bip_C_Hips"] 1040: knee = B["J_Bip_L_LowerLeg"][1] 1041: out = [] 1042: 1043: def cuff_plane(side): 1044: a, h = B[f"J_Bip_{side}_LowerArm"], B[f"J_Bip_{side}_Hand"] 1045: n = (h - a) / np.linalg.norm(h - a) 1046: return n, n @ (h - n * 0.025) 1047: 1048: torso = ("Spine", "Chest", "UpperChest", "Shoulder", "UpperArm", "LowerArm", "Neck", "Hips") 1049: tunic = wrap(fit, "tunic", "coat", torso, offset=0.009, exclude=("UpperLeg",), 1050: y_min=hips[1] - 0.03, y_max=nk[1] + 0.012, hems=[cuff_plane("L"), cuff_plane("R")]) 1051: trousers = wrap(fit, "trousers", "leather", ("Hips", "UpperLeg", "LowerLeg"), offset=0.008, 1052: y_max=hips[1] + 0.02, subdiv=1) 1053: boots = wrap(fit, "boots", "boot", ("LowerLeg", "Foot", "ToeBase"), offset=0.017, y_max=knee - 0.07, 1054: subdiv=1, hems=[((0, 1, 0), knee - 0.075)]) 1055: gloves = wrap(fit, "gloves", "glove", ("Hand", "Thumb", "Index", "Middle", "Ring", "Little"), 1056: offset=0.0028, min_w=0.6, smooth=4) 1057: out += [p for p in (tunic, trousers, boots, gloves) if p is not None] 1058: under = merge_surfaces((tunic["pos"], tunic["tri"]), (fit.pos, fit.tri)) 1059: 1060: for side in ("L", "R"):read_file{"EndLine":300,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":1}
input
{ "EndLine": 300, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 1 }result, 301 lines
Showing lines 1-300 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):read_file{"EndLine":1407,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":1060}
input
{ "EndLine": 1407, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 1060 }result, 349 lines
Showing lines 1060-1407 of 1407 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] 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: 1311: used = np.unique(p["tri"]) 1312: d = np.linalg.norm(p["uv"][used] - uv, axis=1) 1313: k = int(np.argmin(d)) 1314: if d[k] < bd: 1315: best, bd = p["pos"][used[k]], d[k] 1316: return best if bd < 0.02 else None 1317: 1318: 1319: def draw_mouth(c, rol, pts): 1320: """FE8-style closed mouth: a dark line between the corners with lighter ends, and a lower-lip 1321: shade under its middle. Only drawn on visible face pixels.""" 1322: (x0, y0, _), (x1, y1, _), _ = pts 1323: if x0 > x1: 1324: x0, y0, x1, y1 = x1, y1, x0, y0 1325: n = max(1, int(round(x1 - x0))) 1326: xs = [int(round(x0 + (x1 - x0) * i / n)) for i in range(n + 1)] 1327: ys = [int(round(y0 + (y1 - y0) * i / n)) for i in range(n + 1)] 1328: h, w = c.shape 1329: for i, (x, y) in enumerate(zip(xs, ys)): 1330: if not (0 <= x < w and 0 <= y < h) or rol[y, x] != "face": 1331: continue 1332: end = i in (0, len(xs) - 1) and len(xs) >= 4 1333: c[y, x] = "3" if end else "1" 1334: if 0 < i < len(xs) - 1 and len(xs) >= 3 and y + 1 < h and rol[y + 1, x] == "face" and c[y + 1, x] == "5": 1335: c[y + 1, x] = "4" 1336: 1337: 1338: def draw_curve_mouth(c, rol, dep, pts, lip=False, keep=(0.12, 0.88), dark=(0.28, 0.72)): 1339: """Closed mouth as a 1-px line along the projected lip curve: dark in the middle, one tone 1340: lighter toward the corners, stopping short of them. Only on visible face pixels, and only 1341: where the curve is not hidden behind something nearer.""" 1342: h, w = c.shape 1343: n = len(pts) 1344: seen = set() 1345: for k in range(n - 1): 1346: (x0, y0, z0), (x1, y1, z1) = pts[k], pts[k + 1] 1347: steps = max(1, int(max(abs(x1 - x0), abs(y1 - y0)) * 2)) 1348: for i in range(steps + 1): 1349: u = (k + i / steps) / (n - 1) 1350: if not keep[0] <= u <= keep[1]: 1351: continue 1352: x = int(round(x0 + (x1 - x0) * i / steps)) 1353: y = int(round(y0 + (y1 - y0) * i / steps)) 1354: z = z0 + (z1 - z0) * i / steps 1355: if (x, y) in seen or not (0 <= x < w and 0 <= y < h) or rol[y, x] != "face" or dep[y, x] > z + 0.01: 1356: continue 1357: seen.add((x, y)) 1358: c[y, x] = "1" if dark[0] <= u <= dark[1] else "2" 1359: if lip and dark[0] <= u <= dark[1] and y + 1 < h and rol[y + 1, x] == "face" and c[y + 1, x] == "5": 1360: c[y + 1, x] = "4" 1361: 1362: 1363: def smooth_view(portrait, yaw=35, pitch=None, palette=None, view="bust", w=None, h=None): 1364: """The 3D model itself, rendered smooth at 4x in the character's colours (same framing as 1365: render()), for reviewing shape. Uses the role colours, not VRoid's textures.""" 1366: pal = dict(PALETTE, **(palette or {})) 1367: hexrgb = lambda c: np.array([int(pal[c].lstrip("#")[i:i + 2], 16) for i in (0, 2, 4)], float) 1368: base = {"face": hexrgb("5"), "body": hexrgb("4"), "hair": hexrgb("D"), "cloth": hexrgb("M"), 1369: "line": hexrgb("o"), "brow": hexrgb("B"), "mouth": hexrgb("2"), "white": hexrgb("w"), 1370: "highlight": hexrgb("w"), "iris": hexrgb("s"), "coat": hexrgb("M"), "cape": hexrgb("W"), 1371: "metal": hexrgb("8"), "gold": hexrgb("H"), "leather": hexrgb("v"), "boot": hexrgb("v"), 1372: "glove": hexrgb("x"), "steel": hexrgb("8"), "grass": hexrgb("c"), "dirt": hexrgb("f"), 1373: "stone": hexrgb("l"), "signature": hexrgb("M"), "doublet": hexrgb("S"), 1374: "heraldry": hexrgb("N")} 1375: return portrait.render(yaw, pitch, view=view, smooth=base, w=w, h=h) 1376: 1377: 1378: def image(char, scale=1, bg=None, palette=None): 1379: pal = dict(PALETTE, **(palette or {})) 1380: h, w = char.shape 1381: im = Image.new("RGBA", (w, h), bg or (0, 0, 0, 0)) 1382: px = im.load() 1383: for y in range(h): 1384: for x in range(w): 1385: ch = char[y, x] 1386: if ch: 1387: hx = pal[ch].lstrip("#") 1388: px[x, y] = tuple(int(hx[i:i + 2], 16) for i in (0, 2, 4)) + (255,) 1389: return im.resize((w * scale, h * scale), Image.NEAREST) if scale > 1 else im 1390: 1391: 1392: def main(): 1393: ap = argparse.ArgumentParser() 1394: ap.add_argument("model") 1395: ap.add_argument("--yaw", type=float, default=35) 1396: ap.add_argument("--pitch", type=float, default=4) 1397: ap.add_argument("--png") 1398: ap.add_argument("--scale", type=int, default=1) 1399: a = ap.parse_args() 1400: p = Portrait(a.model) 1401: c = p.render(a.yaw, a.pitch) 1402: if a.png: 1403: image(c, a.scale).save(a.png) 1404: 1405: 1406: if __name__ == "__main__": 1407: main()read_file{"EndLine":1060,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":950}
input
{ "EndLine": 1060, "Path": "~/work/lowmarch/tools/vroid_portrait.py", "StartLine": 950 }result, 112 lines
Showing lines 950-1060 of 1407 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 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]