agent (cont.)

+5:32
  • read_file{"EndLine":1050,"Path":"~/work/lowmarch/tools/vroid_portrait.py","StartLine":650}

    input

    {
      "EndLine": 1050,
      "Path": "~/work/lowmarch/tools/vroid_portrait.py",
      "StartLine": 650
    }

    result, 402 lines

    Showing lines 650-1050 of 1407
    650:                 col.append(o + d * (t + 0.0008 + 0.0025 * (1 - k / 7)))      # further off at the top: the jaw stays behind
    651:             if len(col) == 8:
    652:                 cols.append(col)
    653:         if len(cols) >= 3:
    654:             out.append((_shadow_card("chin", body, np.array(cols)), "shadow"))
    655:     if face is not None:                                    # under the nose tip: flat on top, rounded below
    656:         fs = Surface(face["pos"], face["tri"])
    657:         nose = lm["nose"]
    658:         w, h = 0.055 * span, 0.05 * span
    659:         y_t = nose[1] - 0.015 * span
    660:         zf = face["pos"][:, 2].min() - 0.05
    661:         cols = []
    662:         for x in np.linspace(-w, w, 15):
    663:             col = []
    664:             for t in np.linspace(0, 1, 4):
    665:                 o = np.array([nose[0] + x, y_t - t * h * np.sqrt(max(0.0, 1 - (x / w) ** 2)), zf])
    666:                 tt = fs.cast(o, np.array([0.0, 0.0, 1.0]), rmax=0.3, far=False)
    667:                 if np.isnan(tt):
    668:                     break
    669:                 col.append(o + np.array([0.0, 0.0, tt - 0.0005]))
    670:             if len(col) == 4:
    671:                 cols.append(col)
    672:         if len(cols) >= 3:
    673:             out.append((_shadow_card("nose", face, np.array(cols)), "shadow"))
    674:     return out
    675: 
    676: 
    677: def face_lines(prims, roles, v, amount):
    678:     """Age, drawn the way the study draws it on otherwise young anime faces: a few thin lines
    679:     (from the nose's wing round the mouth, under the eyes, crow's feet, a frown between the brows,
    680:     down from the mouth's corners), darker and more of them as `amount` (0..1) rises. Each stroke is
    681:     laid out in 3D from the face's landmarks, dropped onto the face from the front, and painted into a
    682:     copy of the face texture (which the renderers multiply into the skin)."""
    683:     if amount <= 0:
    684:         return
    685:     from PIL import Image, ImageDraw
    686:     from fitting import Surface
    687:     lm = landmarks(prims, roles)
    688:     face = [p for p, r in zip(prims, roles) if r == "face" and len(p["tri"])]
    689:     if not face:
    690:         return
    691:     off = np.cumsum([0] + [len(p["pos"]) for p in face])
    692:     pos = np.concatenate([p["pos"] for p in face])
    693:     uv = np.concatenate([p["uv"] for p in face])
    694:     tri = np.concatenate([p["tri"] + off[i] for i, p in enumerate(face)])
    695:     surf = Surface(pos, tri)
    696: 
    697:     def verts(*rs):
    698:         return np.concatenate([p["pos"][np.unique(p["tri"])] for p, r in zip(prims, roles) if r in rs])
    699:     iris = verts("iris")
    700:     mouth = verts("mouth")
    701:     eL, eR = lm["eyeL"], lm["eyeR"]
    702:     ex = abs(eR[0] - eL[0]) / 2                                  # half the distance between the eyes
    703:     span = lm["span"]
    704:     zf = pos[:, 2].min() - 0.05                                  # in front of the face (it faces -z)
    705:     mo = lm["mouth"]
    706:     mhw = 0.55 * np.ptp(mouth[:, 0])                             # half the mouth's width (the inner mouth spans it)
    707: 
    708:     def eye_box(c):
    709:         """Half the visible eye's width and height: VRoid's iris card spans the opening's height, and
    710:         the opening is about 1.6 irises wide (the white card is far bigger than what shows)."""
    711:         w = iris[np.sign(iris[:, 0]) == np.sign(c[0])]
    712:         return 0.8 * np.ptp(w[:, 0]), np.ptp(w[:, 1]) / 2
    713: 
    714:     def drop(x, y):
    715:         """A point on the face seen straight on: the nearest surface hit from the front."""
    716:         t = surf.cast(np.array([x, y, zf]), np.array([0.0, 0.0, 1.0]), rmax=0.3, far=False)
    717:         return None if np.isnan(t) else np.array([x, y, zf + t])
    718: 
    719:     def to_uv(P):
    720:         _, cp, ti = surf.query(P)
    721:         T = pos[tri[ti]]
    722:         v0, v1, v2 = T[:, 1] - T[:, 0], T[:, 2] - T[:, 0], cp - T[:, 0]
    723:         d00, d01, d11 = (v0 * v0).sum(1), (v0 * v1).sum(1), (v1 * v1).sum(1)
    724:         d20, d21 = (v2 * v0).sum(1), (v2 * v1).sum(1)
    725:         den = np.where(np.abs(d00 * d11 - d01 * d01) < 1e-20, 1e-20, d00 * d11 - d01 * d01)
    726:         b1 = (d11 * d20 - d01 * d21) / den
    727:         b2 = (d00 * d21 - d01 * d20) / den
    728:         U = uv[tri[ti]]
    729:         return U[:, 0] * (1 - b1 - b2)[:, None] + U[:, 1] * b1[:, None] + U[:, 2] * b2[:, None]
    730: 
    731:     def curve(ctrl, n=24):
    732:         """A smooth stroke through control points (x, y), quadratic B-spline-ish via Chaikin."""
    733:         Q = np.asarray(ctrl, float)
    734:         for _ in range(3):
    735:             if len(Q) < 3:
    736:                 break
    737:             Q = np.vstack([Q[:1]] + [np.vstack([0.75 * Q[i] + 0.25 * Q[i + 1], 0.25 * Q[i] + 0.75 * Q[i + 1]])
    738:                                      for i in range(len(Q) - 1)] + [Q[-1:]])
    739:         t = np.linspace(0, 1, n)
    740:         L = np.r_[0, np.cumsum(np.linalg.norm(np.diff(Q, axis=0), axis=1))]
    741:         return np.stack([np.interp(t * L[-1], L, Q[:, 0]), np.interp(t * L[-1], L, Q[:, 1])], 1)
    742: 
    743:     a = float(np.clip(amount, 0, 1))
    744:     strokes = []                                                 # (2D curve, width scale, darkness scale)
    745:     for c, sd in ((eL, -1), (eR, 1)):
    746:         ew, eh = eye_box(c)
    747:         out = sd                                                 # +x is the character's right
    748:         # under the eye: one arc below the lower lid; a second, fainter, when old
    749:         strokes.append((curve([(c[0] - out * 0.45 * ew, c[1] - 0.80 * eh), (c[0], c[1] - 1.05 * eh),
    750:                                (c[0] + out * 0.55 * ew, c[1] - 0.85 * eh)]), 1.0, 1.0))
    751:         if a > 0.55:
    752:             strokes.append((curve([(c[0] - out * 0.15 * ew, c[1] - 1.35 * eh), (c[0] + out * 0.35 * ew, c[1] - 1.40 * eh),
    753:                                    (c[0] + out * 0.75 * ew, c[1] - 1.20 * eh)]), 0.8, 0.7))
    754:         # from the nose's wing round the mouth
    755:         nose_y = lm["nose"][1]
    756:         strokes.append((curve([(out * 0.32 * ex, nose_y + 0.03 * span), (out * 0.56 * ex, nose_y - 0.10 * span),
    757:                                (out * (mhw + 0.18 * ex), mo[1] + 0.02 * span)]), 1.2, 1.0))
    758:         # crow's feet at the outer corner
    759:         if a > 0.4:
    760:             oc = np.array([c[0] + out * 1.05 * ew, c[1]])
    761:             for ang in ((-18, 1.0), (8, 0.8)) if a < 0.75 else ((-25, 1.0), (0, 0.9), (22, 0.8)):
    762:                 d = np.radians(ang[0])
    763:                 tip = oc + np.array([out * np.cos(d), np.sin(d)]) * 0.55 * ew
    764:                 strokes.append((curve([oc, (oc + tip) / 2, tip], 10), 0.7 * ang[1], 0.8))
    765:         # down from the mouth's corner
    766:         if a > 0.85:
    767:             strokes.append((curve([(out * (mhw + 0.06 * ex), mo[1] - 0.06 * span),
    768:                                    (out * (mhw + 0.10 * ex), mo[1] - 0.30 * span)], 14), 0.9, 0.85))
    769:     # a frown between the brows
    770:     if a > 0.3:
    771:         y0 = (eL[1] + eR[1]) / 2
    772:         _, eh = eye_box(eL)
    773:         for sd in (-1, 1):
    774:             strokes.append((curve([(sd * 0.10 * ex, y0 + 0.9 * eh), (sd * 0.13 * ex, y0 + 1.9 * eh)], 10), 0.85, 0.8))
    775:     # paint: each stroke dropped onto the face, into texture space, tapered at both ends
    776:     src = None
    777:     for p in face:
    778:         m = v.j["materials"][p["mi"]]
    779:         t = m.get("pbrMetallicRoughness", {}).get("baseColorTexture")
    780:         if t is not None:
    781:             src = v.j["textures"][t["index"]]["source"]
    782:             break
    783:     if src is None:
    784:         return
    785:     tex = v.image(src).copy()
    786:     H, W = tex.shape[:2]
    787:     S = 2                                                        # supersampled mask
    788:     mask = Image.new("L", (W * S, H * S), 0)
    789:     dr = ImageDraw.Draw(mask)
    790:     base_w = 4.2 * W / 1024 * (0.7 + 0.4 * a)
    791:     for C, ws, ds in strokes:
    792:         P = [drop(x, y) for x, y in C]
    793:         P = np.array([q for q in P if q is not None])
    794:         if len(P) < 3:
    795:             continue
    796:         U = to_uv(P) * [W * S, H * S]
    797:         n = len(U)
    798:         for i in range(n - 1):
    799:             t = (i + 0.5) / (n - 1)
    800:             w = base_w * S * ws * np.sin(np.pi * np.clip(t, 0.05, 0.95)) ** 0.7
    801:             dr.line([tuple(U[i]), tuple(U[i + 1])], fill=int(255 * min(1.0, ds)), width=max(1, int(round(w))))
    802:     mk = np.asarray(mask.resize((W, H), Image.LANCZOS), float) / 255
    803:     dark = (0.30 + 0.22 * a) * mk
    804:     tex = tex.astype(float)
    805:     tex[..., :3] *= (1 - dark)[..., None]
    806:     v._img[src] = np.clip(tex, 0, 255).astype(np.uint8)
    807: 
    808: 
    809: MOUTH_ROOM = 0.002      # m: an open mouth's inside stays this far within the face's lower outline
    810: 
    811: 
    812: def mouth_room(v, mo, pose, yaws=(0.0, 30.0, -30.0)):
    813:     """How far (m) the mouth's inside (teeth, tongue, its dark) stays above the face's lower outline, seen from the
    814:     front and turned: the least, over columns 2 mm wide, of its lowest point less the face skin's. Negative: it
    815:     shows below the chin."""
    816:     prs = v.primitives(mo, pose)
    817:     # the face skin as points along its triangles' edges, so every column holds a piece of its outline
    818:     F = [p["pos"][p["tri"]] for p in prs if role_of(p["material"]) == "face" and len(p["tri"])]
    819:     M = [p["pos"][np.unique(p["tri"])] for p in prs if role_of(p["material"]) == "mouth" and len(p["tri"])]
    820:     if not F or not M:
    821:         return np.inf
    822:     T, M = np.concatenate(F), np.concatenate(M)
    823:     F = np.concatenate([T[:, i] * (1 - t) + T[:, (i + 1) % 3] * t for i in range(3) for t in np.linspace(0, 1, 6)[:-1]])
    824:     least = np.inf
    825:     for yaw in yaws:
    826:         a = np.radians(yaw)
    827:         fx = F[:, 0] * np.cos(a) + F[:, 2] * np.sin(a)
    828:         mx = M[:, 0] * np.cos(a) + M[:, 2] * np.sin(a)
    829:         fb, mb = np.floor(fx / 0.002).astype(int), np.floor(mx / 0.002).astype(int)
    830:         for b in np.unique(mb):
    831:             f = F[fb == b, 1]
    832:             if len(f):
    833:                 least = min(least, M[mb == b, 1].min() - f.min())
    834:     return least
    835: 
    836: 
    837: def hold_mouth(v, mo, pose):
    838:     """A recipe's mouth shapes (Fcl_MTH_*, together) scaled down until the mouth's inside stays MOUTH_ROOM within the
    839:     face, so weights past VRoid's 1 (a shout) open each face only as far as it holds: on a long jaw the lower teeth
    840:     and the mouth's floor came out under the chin. Returns the shapes and the scale (1: unchanged)."""
    841:     keys = [k for k in mo if "MTH_" in k and mo[k] > 0]
    842:     if not keys or mouth_room(v, mo, pose) >= MOUTH_ROOM:
    843:         return mo, 1.0
    844:     scaled = lambda s: dict(mo, **{k: mo[k] * s for k in keys})
    845:     lo, hi = 0.0, 1.0
    846:     for _ in range(7):
    847:         mid = (lo + hi) / 2
    848:         lo, hi = (mid, hi) if mouth_room(v, scaled(mid), pose) >= MOUTH_ROOM else (lo, mid)
    849:     return scaled(lo), lo
    850: 
    851: 
    852: def reshape(prims, roles, lm, sh):
    853:     """Face length, sculpting, eye size/tilt and mouth width (skeleton and expression are applied
    854:     earlier, when the primitives are built)."""
    855:     ey, cy = lm["eye_y"], lm["chin"][1]
    856:     span = ey - cy
    857:     if sh["face_len"]:
    858:         from vrm import bone_weight
    859:         for p, r in zip(prims, roles):
    860:             if r in SCULPT_ROLES:
    861:                 t = np.clip((ey - p["pos"][:, 1]) / span, 0, None)
    862:                 d = sh["face_len"] * span * np.minimum(t, 1.0)
    863:                 if r == "body" and "J" in p:            # the neck below the head stays where the clothes were fitted
    864:                     d = d * np.clip(bone_weight(p, ("Head",)), 0, 1)
    865:                 p["pos"][:, 1] -= d
    866:         lm = dict(lm, chin=lm["chin"] - [0, sh["face_len"] * span, 0])
    867:     sculpt(prims, roles, lm, sh)
    868:     for p, r in zip(prims, roles):
    869:         P = p["pos"]
    870:         if sh["mouth"] != 1.0 and r == "mouth":
    871:             P[:, 0] = lm["mouth"][0] + (P[:, 0] - lm["mouth"][0]) * sh["mouth"]
    872:         if r in ("iris", "highlight") and sh.get("iris", 1.0) != 1.0:      # smaller irises read older
    873:             for c in (lm["eyeL"], lm["eyeR"]):
    874:                 side = (P[:, 0] < 0) if c[0] < 0 else (P[:, 0] >= 0)
    875:                 q = P[side] - c
    876:                 q[:, :2] *= sh["iris"]
    877:                 P[side] = c + q
    878:         if r in ("iris", "white", "highlight", "line") and (sh["eye"] != 1 or sh["eye_tilt"]):
    879:             for c in (lm["eyeL"], lm["eyeR"]):
    880:                 side = (P[:, 0] < 0) if c[0] < 0 else (P[:, 0] >= 0)
    881:                 q = P[side] - c
    882:                 q[:, :2] *= sh["eye"]
    883:                 q[:, 1] += sh["eye_tilt"] * np.sign(c[0]) * q[:, 0] * 0.4
    884:                 P[side] = c + q
    885: 
    886: 
    887: def keep_lash_fan(prims, now, k, lines, closure):
    888:     """A shut eye closed further (`lid_gap`) moves its lash fan (VRoid's FaceEyelash) with the lid edge instead of
    889:     folding it past shut: the fan stays as the expression's own close leaves it, shifted as the edge shifts at each x.
    890:     now: the primitives before the extra close; k: the eyeline's index."""
    891:     E = prims[k]["pos"]
    892:     for side, (_, _, edge, _) in lines.items():
    893:         if closure.get(side, 0.0) < 0.9:
    894:             continue
    895:         o = np.argsort(now[k]["pos"][edge, 0])
    896:         d = E[edge][o] - now[k]["pos"][edge][o]
    897:         for p, q in zip(prims, now):
    898:             if "FaceEyelash" not in p["material"]:
    899:                 continue
    900:             m = (q["pos"][:, 0] < 0) == (side == "L")
    901:             for ax in range(3):
    902:                 p["pos"][m, ax] = q["pos"][m, ax] + np.interp(q["pos"][m, 0], now[k]["pos"][edge][o, 0], d[:, ax])
    903: 
    904: 
    905: class Portrait:
    906:     def __init__(self, path, morphs=None, pose=BUST_POSE, shape=None, hair_from=None, costume=None,
    907:                  mouth="curve", world=None, ref_pose=BUST_POSE, hair_trim=None):
    908:         """mouth: how the closed mouth is drawn. 'texture' = VRoid's painted lip line, thickened;
    909:         'texture+lip' = that plus a lower-lip shade; 'drawn' = an FE8-style mouth between the
    910:         projected mouth corners; 'none' = geometry only (the line disappears when closed)."""
    911:         self.mouth = mouth
    912:         self.v = VRM(path)
    913:         sh = dict(NEUTRAL, **(shape or {}))
    914:         mo = dict(sh["rest"])
    915:         for k_, w_ in (morphs or {}).items():
    916:             mo[k_] = min(1.0, mo.get(k_, 0.0) + w_)
    917:         if sh["fangs"] < 1.0:                            # some faces' teeth have fangs (sample V's); 0 files them away
    918:             mo["Fcl_HA_Short"] = max(mo.get("Fcl_HA_Short", 0.0), 1.0 - sh["fangs"])
    919:         # everything is built in the reference pose (arms down), then posed through the skeleton
    920:         target_pose = bone_pose(self.v, sh, pose)
    921:         pose = bone_pose(self.v, sh, ref_pose)
    922:         mo, self.mouth_hold = hold_mouth(self.v, mo, pose)
    923:         closure = eye_closure(mo)
    924:         lines = None
    925:         if max(closure.values()) > 0.05 or sh["lash"] != 1.0:   # the eyeline open, and how far a shut eye must close
    926:             eyes = {k_: w_ for k_, w_ in mo.items() if "EYE_" not in k_ and "ALL_" not in k_}
    927:             now = self.v.primitives(mo, pose)
    928:             ln = [i_ for i_, p_ in enumerate(now) if "FaceEyeline" in p_["material"]]
    929:             if ln:
    930:                 prs = [self.v.primitives(eyes, pose)[ln[0]], now[ln[0]],
    931:                        self.v.primitives(dict(eyes, Fcl_EYE_Close=1.0), pose)[ln[0]]]
    932:                 eye = eye_lines(prs[0])
    933:                 # the character's one lash size: set on the open lash, which every closed lid is moved from
    934:                 lines = (ln[0], lash_size(prs[0]["pos"].copy(), prs[0]["uv"], eye, sh["lash"]), eye, now)
    935:                 further = lid_gap(prs[1]["pos"], prs[2]["pos"], prs[0]["pos"], lines[2], prs[0]["uv"], prs[0]["tri"],
    936:                                   self.v.material_texture(prs[0]["mi"]))
    937:         mo = hide_closed_irises(mo, further if lines else None)
    938:         self.pose_now = target_pose
    939:         self.pose_ref = pose
    940:         self.shape = sh
    941:         self.prims = self.v.primitives(mo, pose)
    942:         if lines:                                        # a closing lid is the model's own open lash, moved
    943:             k_, P_open, eye, now = lines
    944:             p_ = self.prims[k_]
    945:             keep_lash_weight(p_, P_open, p_["uv"], eye, closure)
    946:             lash_size(p_["pos"], p_["uv"], {s_: v_ for s_, v_ in eye.items() if closure[s_] <= 0.05}, sh["lash"])
    947:             keep_lash_fan(self.prims, now, k_, eye, closure)
    948:             for side, (up, others, edge, _) in eye.items():   # a shut eye is one closed lid:
    949:                 if closure[side] < 0.9:
    950:                     continue
    951:                 if others:                                  # its lower line goes,
    952:                     p_["tri"] = p_["tri"][~np.isin(p_["tri"], np.concatenate(others)).all(1)]
    953:                 m = np.isin(p_["tri"], up).all(1)           # and its lash is drawn solid (export_glb.fill_lid):
    954:                 self.prims.append(dict(p_, tri=p_["tri"][m], lid_fill=float(p_["uv"][edge, 1].max())))
    955:                 p_["tri"] = p_["tri"][~m]                   # the open lash's pale streaks and hollows read as
    956:                                                             # shine beside an eye, but split a lid over skin
    957:         if hair_from:                                    # swap in another model's hair
    958:             hv = VRM(hair_from)
    959:             hp = hv.primitives(None, ref_pose)
    960:             hroles = [role_of(p["material"]) for p in hp]
    961:             src = landmarks(hp, hroles)
    962:             dst = landmarks(self.prims, [role_of(p["material"]) for p in self.prims])
    963:             k = (abs(dst["eyeR"][0] - dst["eyeL"][0]) / abs(src["eyeR"][0] - src["eyeL"][0])
    964:                  + (dst["eye_y"] - dst["chin"][1]) / (src["eye_y"] - src["chin"][1])) / 2
    965:             c_src = (src["eyeL"] + src["eyeR"]) / 2
    966:             c_dst = (dst["eyeL"] + dst["eyeR"]) / 2
    967:             hair = [p for p in hp if "_HAIR" in p["material"]]
    968:             for p in hair:
    969:                 p["pos"] = c_dst + (p["pos"] - c_src) * k
    970:                 n_ = len(p["pos"])                       # rides our head bone from here on
    971:                 p["J"], p["W"], p["jnames"] = np.zeros((n_, 4), np.int64), np.tile([1.0, 0, 0, 0], (n_, 1)), ["J_Bip_C_Head"]
    972:             self.prims = [p for p in self.prims if "_HAIR" not in p["material"]] + hair
    973:             self._hair_v = hv
    974:         if hair_trim:                                    # drop hair pieces reaching past |x| = hair_trim (e.g. twin tails)
    975:             self.prims = [p for p in self.prims if "_HAIR" not in p["material"]
    976:                           or np.abs(p["pos"][np.unique(p["tri"]), 0]).max() <= hair_trim]
    977:         self.roles = [p.get("role") or role_of(p["material"]) for p in self.prims]
    978:         for side, c in eye_closure(mo).items():          # a shut eye shows no white (it can poke between the lids)
    979:             if c < 0.9:
    980:                 continue
    981:             for p, r in zip(self.prims, self.roles):
    982:                 if r == "white" and len(p["tri"]):
    983:                     cx = p["pos"][p["tri"]].mean(1)[:, 0]
    984:                     p["tri"] = p["tri"][(cx < 0) != (side == "L")]
    985:         self.lm = landmarks(self.prims, self.roles)
    986:         if costume:                                      # replace VRoid's clothes with ours
    987:             import outfit
    988:             fit = outfit.Fit(self.v, pose, self.prims, self.roles)
    989:             import rig
    990:             fit.grip = rig.grip_points(self.v, sh, ref_pose)  # where each fist closes (weapons sit there)
    991:             keep_ = [k for k, r in enumerate(self.roles) if r != "cloth"]
    992:             self.prims = [self.prims[k] for k in keep_]
    993:             self.roles = [self.roles[k] for k in keep_]
    994:             costume = outfit.COSTUMES[costume] if isinstance(costume, str) else costume
    995:             fit.keep_skin = ()                       # a costume may leave skin bare (fit.keep_skin = bones)
    996:             for pc in costume(fit):
    997:                 self.prims.append(pc)
    998:                 self.roles.append(pc["role"])
    999:         # body skin below the neck is always under clothes in a bust; drop it so it can't poke through
    1000:         cy = self.lm["chin"][1]
    1001:         from vrm import bone_weight
    1002:         for p, r in zip(self.prims, self.roles):
    1003:             if r != "body":
    1004:                 continue
    1005:             if costume:                     # keep head, neck and upper chest (the throat shows in the collar opening)
    1006:                 wv = bone_weight(p, ("Head", "Neck", "UpperChest") + tuple(getattr(fit, "keep_skin", ())))
    1007:                 p["tri"] = p["tri"][(wv[p["tri"]] >= 0.5).all(1)]
    1008:                 if getattr(fit, "drop_skin", None) is not None:      # a costume may hide the skin it covers
    1009:                     p["tri"] = p["tri"][~fit.drop_skin(p)[p["tri"]].all(1)]
    1010:             else:
    1011:                 T = p["pos"][p["tri"]]
    1012:                 head = T[:, :, 1].min(1) > cy - 0.02
    1013:                 neck = (T[:, :, 1].min(1) > cy - 0.14) & (np.abs(T[:, :, 0]).max(1) < 0.055)
    1014:                 p["tri"] = p["tri"][head | neck]
    1015:         for pc in world or []:              # scenery: does not turn with the character
    1016:             self.prims.append(pc)
    1017:             self.roles.append(pc["role"])
    1018:         # framing uses the unshaped face at the base head size, so head-size changes still show
    1019:         self.span = (self.lm["eye_y"] - self.lm["chin"][1]) / sh["head"]
    1020:         reshape(self.prims, self.roles, self.lm, sh)
    1021:         strip_blush(self.prims, self.roles, self.v, sh["blush"])
    1022:         face_lines(self.prims, self.roles, self.v, sh["age_lines"])
    1023:         lm_now = landmarks(self.prims, self.roles)          # after the sculpt
    1024:         names, Wn = self.v.nodes_by_name(), self.v.node_world(pose)
    1025:         neck = (Wn[names["J_Bip_C_Neck"]][:3, 3], Wn[names["J_Bip_C_Head"]][:3, 3]) \
    1026:             if "J_Bip_C_Neck" in names and "J_Bip_C_Head" in names else None
    1027:         face_normals(self.prims, self.roles, lm_now, sh["face_normals"], sh["hair_normals"], neck=neck)
    1028:         for pc, rl in shadow_cards(self.prims, self.roles, lm_now, sh["shadow_cards"], neck):
    1029:             self.prims.append(pc)
    1030:             self.roles.append(rl)
    1031:         if pose is not target_pose and any(not np.allclose(pose.get(k_, np.eye(4)), target_pose.get(k_, np.eye(4)))
    1032:                                            for k_ in set(pose) | set(target_pose)):
    1033:             repose(self.v, [p for p in self.prims if not p.get("world")], pose, target_pose)
    1034:         names = self.v.nodes_by_name()
    1035:         Wn = self.v.node_world()
    1036:         self.eye = (Wn[names["J_Adj_L_FaceEye"]][:3, 3] + Wn[names["J_Adj_R_FaceEye"]][:3, 3]) / 2
    1037:         self.chin_y = self.lm["chin"][1]
    1038:         self.tex = {}
    1039:         for k, p in enumerate(self.prims):
    1040:             if self.roles[k] in ("line", "brow", "iris", "highlight", "white", "hair", "mouth"):
    1041:                 src = self._hair_v if (hair_from and "_HAIR" in p["material"]) else self.v
    1042:                 t = src.material_texture(p["mi"])
    1043:                 if t is not None and self.roles[k] == "line":
    1044:                     # VRoid's upper lid is a thick graded band over the iris; keep its dark core only
    1045:                     t = t.copy()
    1046:                     t[..., 3] = np.where(t[..., :3].mean(-1) < LID_CORE, t[..., 3], 0)
    1047:                 if t is not None:
    1048:                     self.tex[k] = t

    2 more lines not shown.

  • read_file{"EndLine":888,"Path":"~/work/lowmarch/tools/cage.py","StartLine":740}

    input

    {
      "EndLine": 888,
      "Path": "~/work/lowmarch/tools/cage.py",
      "StartLine": 740
    }

    result, 150 lines

    Showing lines 740-888 of 888
    740: 
    741: 
    742: def _put(j, binb, acc, arr):
    743:     a = j["accessors"][acc]
    744:     bv = j["bufferViews"][a["bufferView"]]
    745:     off = bv.get("byteOffset", 0) + a.get("byteOffset", 0)
    746:     nc = arr.shape[1]
    747:     assert a["componentType"] == 5126 and bv.get("byteStride", 4 * nc) == 4 * nc, "tightly packed floats only"
    748:     binb[off:off + 4 * arr.size] = arr.astype(np.float32).tobytes()
    749:     if "min" in a:
    750:         a["min"], a["max"] = arr.min(0).tolist(), arr.max(0).tolist()
    751: 
    752: 
    753: def write(path, v, meshes, Jn):
    754:     """The base again with every mesh's positions, normals and morph deltas, the joints and the skins'
    755:     inverse bind matrices replaced in place (VRoid's rest pose has no rotations: joints are translations)."""
    756:     j = json.loads(json.dumps(v.j))
    757:     binb = bytearray(v.bin)
    758:     sa = j.get("extensions", {}).get("VRM", {}).get("secondaryAnimation", {})
    759:     for node, s in getattr(v, "head_scale", {}).items():
    760:         for cg in sa.get("colliderGroups", []):
    761:             if cg["node"] == node:
    762:                 for c in cg["colliders"]:
    763:                     c["offset"] = {a: x * s for a, x in c["offset"].items()}
    764:                     c["radius"] *= s
    765:         for g in sa.get("boneGroups", []):
    766:             if g.get("comment") == "Hair":
    767:                 g["hitRadius"] = g.get("hitRadius", 0.02) * s
    768:     if getattr(v, "hair_colliders", None):            # the hair's body spheres: ours, fitted (collide.hair), for the
    769:         head = [cg for cg in sa["colliderGroups"] if j["nodes"][cg["node"]]["name"] == "J_Bip_C_Head"]   # sample's
    770:         sa["colliderGroups"] = head + [dict(node=int(b), colliders=[dict(offset=dict(zip("xyz", map(float, o))), radius=float(r))
    771:                                                                     for o, r in cs]) for b, cs in v.hair_colliders]
    772:         for g in sa["boneGroups"]:
    773:             if g.get("comment") == "Hair":
    774:                 g["colliderGroups"] = list(range(len(sa["colliderGroups"])))
    775:     for mi, md in meshes.items():
    776:         _put(j, binb, md["at"]["POSITION"], md["Pn"])
    777:         _put(j, binb, md["at"]["NORMAL"], md["Nn"])
    778:         for t, d in zip(md["targets"], md["deltas"]):
    779:             _put(j, binb, t["POSITION"], d)
    780:     par = v.parents()
    781:     for i, n in enumerate(j["nodes"]):
    782:         assert n.get("rotation", [0, 0, 0, 1]) == [0, 0, 0, 1] and "matrix" not in n and "scale" not in n
    783:         n["translation"] = (Jn[i] - (Jn[par[i]] if i in par else 0)).tolist()
    784:     for sk in j["skins"]:
    785:         ibm = np.zeros((len(sk["joints"]), 16))
    786:         for k, ni in enumerate(sk["joints"]):
    787:             m = np.eye(4)
    788:             m[:3, 3] = -Jn[ni]
    789:             ibm[k] = m.T.ravel()                     # glTF matrices are column-major
    790:         _put(j, binb, sk["inverseBindMatrices"], ibm)
    791:     j.setdefault("asset", {})["generator"] = "lowmarch cage.py on " + j.get("asset", {}).get("generator", "VRoid")
    792:     js = json.dumps(j, separators=(",", ":")).encode()
    793:     js += b" " * ((4 - len(js) % 4) % 4)
    794:     while len(binb) % 4:
    795:         binb.append(0)
    796:     data = struct.pack("<4sII", b"glTF", 2, 12 + 8 + len(js) + 8 + len(binb))
    797:     data += struct.pack("<I4s", len(js), b"JSON") + js + struct.pack("<I4s", len(binb), b"BIN\x00") + bytes(binb)
    798:     os.makedirs(os.path.dirname(path), exist_ok=True)
    799:     tmp = f"{path}.{os.getpid()}"
    800:     open(tmp, "wb").write(data)
    801:     os.replace(tmp, path)
    802:     return len(data)
    803: 
    804: 
    805: # ---- fitting (tools/fit.py's loop, with this builder's controls)
    806: def _c(*paths):
    807:     return tuple(tuple(p.split(".")) for p in paths)
    808: 
    809: 
    810: CONTROLS = {    # ratio: [(spec paths moved together, bounds, first step)]
    811:     "crotch_H": [(_c("len.UpperLeg", "len.LowerLeg"), (-0.45, 0.4), 0.1)],
    812:     "heads": [(_c("head"), (-0.3, 0.6), 0.1)],
    813:     "neck_hd": [(_c("len.Neck"), (-0.6, 0.6), 0.15)],
    814:     "shoulder_hd": [(_c("len.Shoulder"), (-0.5, 3.0), 0.4)],
    815:     "hip_hd": [(_c("wide.Hips"), (-0.3, 1.2), 0.15)],
    816:     "waist_hd": [(_c("rings.torso.waist.w"), (-0.3, 1.5), 0.15)],
    817:     "arm_hd": [(_c("len.UpperArm", "len.LowerArm"), (-0.4, 0.5), 0.1)],
    818:     "hand_CE": [(_c("hand"), (-0.3, 0.6), 0.1)],
    819:     "foot_CE": [(_c("foot"), (-0.3, 0.6), 0.1)],
    820:     "cup_p": [(_c("bust_shape.inflate"), (0.0, 0.04), 0.008)],  # VRoid's breasts inflated (tools/anthro.py's cup)
    821:     "thigh_p": [(_c("swell.UpperLeg"), (-0.01, 0.03), 0.004)],  # mid-thigh (tools/anthro.py), metres along normals
    822: }
    823: 
    824: 
    825: def probe(spec, anthro=None):
    826:     import fit
    827:     os.makedirs(OUT, exist_ok=True)
    828:     p = os.path.join(OUT, f".probe_{os.getpid()}.vrm")
    829:     write(p, *build(spec))
    830:     try:
    831:         return fit.measure(p, anthro)
    832:     finally:
    833:         os.remove(p)
    834: 
    835: 
    836: def _key(spec, hair=True):
    837:     src = "".join(open(os.path.join(HERE, f)).read() for f in ("cage.py", "fit.py", "proportions.py", "makehuman.py",
    838:                                                                  "anthro.py", "heads.py", "vroid_portrait.py")
    839:                   + (("collide.py",) if hair else ()))
    840:     ref = spec.get("ref")
    841:     if isinstance(ref, str):        # a cast entry's reference: its spec too (a refitted reference left the body stale)
    842:         sys.path[:0] = [os.path.join(os.path.dirname(HERE), "art", "cast")]
    843:         from cast import CAST
    844:         src += json.dumps(CAST[ref]["mh"], sort_keys=True)
    845:     return hashlib.sha1((src + json.dumps(spec, sort_keys=True)).encode()).hexdigest()[:16]
    846: 
    847: 
    848: def fitted(spec, log=None):
    849:     """spec with its proportion controls fitted to spec["fit"]'s archetype (cached)."""
    850:     if "fit" not in spec:
    851:         return spec
    852:     import fit
    853:     k = _key(spec, hair=False)          # the hair's collision (collide.py) never moves the body
    854:     try:
    855:         cache = json.load(open(CACHE))
    856:     except (OSError, ValueError):
    857:         cache = {}
    858:     if k not in cache:
    859:         s, _ = fit.fit(spec, log, probe=probe, controls=CONTROLS, head_step=False, rescale=False)
    860:         try:
    861:             cache = json.load(open(CACHE))
    862:         except (OSError, ValueError):
    863:             cache = {}
    864:         cache[k] = s
    865:         os.makedirs(OUT, exist_ok=True)
    866:         tmp = CACHE + f".{os.getpid()}"
    867:         json.dump(cache, open(tmp, "w"), indent=1, sort_keys=True)
    868:         os.replace(tmp, CACHE)
    869:     return cache[k]
    870: 
    871: 
    872: def path_for(name, spec):
    873:     return os.path.join(OUT, f"{name}_{_key(spec)}.vrm")
    874: 
    875: 
    876: def ensure(name, spec, log=None):
    877:     p = path_for(name, spec)
    878:     if not os.path.exists(p):
    879:         write(p, *build(fitted(copy.deepcopy(spec), log)))
    880:     return p
    881: 
    882: 
    883: if __name__ == "__main__":
    884:     sys.path[:0] = [os.path.join(os.path.dirname(HERE), "art", "cast")]
    885:     from cast import CAST
    886:     todo = sys.argv[1:] or [n for n, c in CAST.items() if c.get("cage")]
    887:     for name in todo:
    888:         print(name, ensure(name, CAST[name]["cage"], print))
  • read_file{"EndLine":567,"Path":"~/work/lowmarch/tools/export_glb.py","StartLine":251}

    input

    {
      "EndLine": 567,
      "Path": "~/work/lowmarch/tools/export_glb.py",
      "StartLine": 251
    }

    result, 318 lines

    Showing lines 251-567 of 567
    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))
    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)