tailorrig-explorer / rigconv.py
suvadityamuk's picture
suvadityamuk HF Staff
TailorRig explorer with full 39,073-sample index
124dbdb verified
Raw History Blame Contribute Delete
10.5 kB
"""Shared TailorRig helpers: read one sample out of a subset zip, pack it for the browser, describe it, draw it.
Used by the Space server (on-demand sample loading) and by tools/build_index.py (the one-off index + thumbnail job).
Every subset zip stores each `<uuid>.npz` as its own DEFLATE entry, so one sample is one HTTP range read.
"""
import io, json, re, struct, zlib
import numpy as np
REPO = "tencent/TailorRig"
REVISION = "96ed3da4f5b98ef886c886467a25e2652b69b6e4"
SUBSETS = ["rignet", "humanrig", "articulation_xlv2", "vroid"]
LOCAL_HEADER_SLACK = 1024 # local header = 30 bytes + name + extra; names are < 80 bytes, extras small
# ---------------------------------------------------------------- reading
def entry_range(offset, csize):
"""Byte range [start, end) that covers one zip entry's local header + compressed payload."""
return offset, offset + 30 + LOCAL_HEADER_SLACK + csize
def inflate_entry(blob, csize):
"""`blob` starts at the entry's local header. Returns the decompressed .npz bytes."""
if blob[:4] != b"PK\x03\x04":
raise ValueError("not a zip local header")
method, = struct.unpack("<H", blob[8:10])
nl, el = struct.unpack("<HH", blob[26:30])
data = blob[30 + nl + el: 30 + nl + el + csize]
return zlib.decompress(data, -15) if method == 8 else bytes(data)
def load_npz(raw):
d = np.load(io.BytesIO(raw), allow_pickle=False)
return {k: d[k] for k in d.files}
# ---------------------------------------------------------------- derived data
def dense_top4(d):
"""Top-4 skinning influences per vertex as (idx uint16 [V,4], weight float32 [V,4]), rows sum to 1."""
V = len(d["vertices"])
J = len(d["joints"])
row = d["skinning_weights_row"].astype(np.int64)
col = d["skinning_weights_col"].astype(np.int64)
val = d["skinning_weights_value"].astype(np.float32)
keep = (val > 0) & (row < V) & (col < J)
row, col, val = row[keep], col[keep], val[keep]
# sort by vertex, then weight descending, then rank within vertex
order = np.lexsort((-val, row))
row, col, val = row[order], col[order], val[order]
first = np.searchsorted(row, row, side="left")
rank = np.arange(len(row)) - first
m = rank < 4
idx = np.zeros((V, 4), np.uint16)
w = np.zeros((V, 4), np.float32)
idx[row[m], rank[m]] = col[m]
w[row[m], rank[m]] = val[m]
s = w.sum(1)
orphan = s <= 0
if orphan.any(): # unweighted vertices follow their nearest joint
P = d["vertices"][orphan]
dist = ((P[:, None, :] - d["joints"][None, :, :]) ** 2).sum(-1)
idx[orphan, 0] = dist.argmin(1)
w[orphan, 0] = 1
s = w.sum(1)
w /= s[:, None]
return idx, w
def vertex_normals(d):
n = np.asarray(d.get("vertex_normals", np.zeros_like(d["vertices"])), np.float32)
ln = np.linalg.norm(n, axis=1)
bad = ~np.isfinite(ln) | (ln < 1e-6)
if bad.any(): # rebuild missing normals from faces
V, F = d["vertices"], d["faces"].astype(np.int64)
fn = np.cross(V[F[:, 1]] - V[F[:, 0]], V[F[:, 2]] - V[F[:, 0]])
acc = np.zeros_like(V)
for k in range(3):
np.add.at(acc, F[:, k], fn)
n = np.where(bad[:, None], acc, n)
ln = np.linalg.norm(n, axis=1)
return n / np.maximum(ln, 1e-12)[:, None]
def forward_sign(d, names):
"""+1 if the character faces +Y, -1 if it faces -Y (Blender default). Judged from foot -> toe direction."""
ix = {n: i for i, n in enumerate(names)}
J = d["joints"]
votes = []
for s in ("l", "r"):
a, b = ix.get(f"foot_{s}"), ix.get(f"ball_{s}")
if a is not None and b is not None:
votes.append(J[b, 1] - J[a, 1])
if votes and abs(sum(votes)) > 1e-6:
return 1 if sum(votes) > 0 else -1
return -1
_NUM = re.compile(r"(_\d+)+$")
def bone_group(name):
"""`head_hair_17_02` -> `head_hair`, `pelvis_skirt_01_01_l` -> `pelvis_skirt`, `finger_03_01_r` -> `finger`."""
n = re.sub(r"_[lr]$", "", name)
n = _NUM.sub("", n)
n = re.sub(r"_[lr]$", "", n)
return n
def describe(d, subset):
names = [str(x) for x in d["joint_names"]]
body = np.asarray(d["body_joint_mask"], bool)
V, F = d["vertices"], d["faces"]
lo, hi = V.min(0), V.max(0)
extra = sorted({bone_group(n) for n, b in zip(names, body) if not b})
groups = {bone_group(n) for n in names}
fingers = sum(1 for n in names if n.startswith("finger_"))
return {
"s": subset,
"u": str(d["uuid"]),
"v": int(len(V)),
"f": int(len(F)),
"j": int(len(names)),
"b": int(body.sum()),
"nnz": int(len(d["skinning_weights_value"])),
"h": round(float(hi[2] - lo[2]), 4),
"fing": fingers,
"toes": int("ball" in groups),
"x": extra, # extra (non-body) bone groups
"fw": forward_sign(d, names),
}
# ---------------------------------------------------------------- browser payload
MAGIC = b"TRIG"
def pack(d, subset):
"""Binary payload for the viewer: 4-byte magic, u32 json length, json header, then 4-aligned buffers."""
names = [str(x) for x in d["joint_names"]]
pos = np.ascontiguousarray(d["vertices"], np.float32)
nrm = np.round(vertex_normals(d) * 127).astype(np.int8)
idx = np.ascontiguousarray(d["faces"], np.uint32)
sidx, sw = dense_top4(d)
sw8 = np.round(sw * 255).astype(np.int32)
sw8[:, 0] += 255 - sw8.sum(1) # exact 255 total so weights stay normalized after dequantization
sw8 = np.clip(sw8, 0, 255).astype(np.uint8)
bufs = [("position", pos), ("normal", nrm), ("index", idx), ("skinIndex", sidx), ("skinWeight", sw8)]
head = {
"subset": subset,
"uuid": str(d["uuid"]),
"names": names,
"parents": [int(p) for p in d["parents"]],
"body": [bool(b) for b in d["body_joint_mask"]],
"joints": np.round(d["joints"].astype(np.float64), 6).tolist(),
"forward": forward_sign(d, names),
"counts": {"v": int(len(pos)), "f": int(len(idx)), "j": len(names), "nnz": int(len(d["skinning_weights_value"]))},
"buffers": {},
}
# offsets are relative to the start of the binary section
off = 0
for k, a in bufs:
head["buffers"][k] = [off, a.nbytes, str(a.dtype)]
off += a.nbytes + (-a.nbytes % 4)
hj = json.dumps(head, separators=(",", ":")).encode()
hj += b" " * (-(len(hj) + 8) % 4)
out = io.BytesIO()
out.write(MAGIC)
out.write(struct.pack("<I", len(hj)))
out.write(hj)
for _, a in bufs:
b = a.tobytes()
out.write(b)
out.write(b"\0" * (-len(b) % 4))
return out.getvalue()
# ---------------------------------------------------------------- thumbnails
def render_thumb(d, size=128, ss=2, seed=0):
"""Clay render of the mesh seen from the front with the skeleton drawn over it. Returns RGBA uint8 [size,size,4].
Pure numpy: triangles are area-sampled into points and z-buffered at `ss`x resolution, then box-downsampled.
"""
names = [str(x) for x in d["joint_names"]]
fw = forward_sign(d, names)
V = d["vertices"].astype(np.float32)
F = d["faces"].astype(np.int64)
N = vertex_normals(d)
J = d["joints"].astype(np.float32)
# camera looks at the character's front; screen x = character's left->right as seen, y = up (Z)
sx = -fw # facing -Y: viewer at -Y looking +Y sees +X on their right
X, Y, Z = V[:, 0] * sx, V[:, 1] * fw, V[:, 2] # Y now grows towards the viewer
R = size * ss
lo = np.array([X.min(), Z.min()])
hi = np.array([X.max(), Z.max()])
ext = max(hi[0] - lo[0], hi[1] - lo[1], 1e-9)
scale = R * 0.9 / ext
c = (lo + hi) / 2
px = (X - c[0]) * scale + R / 2
py = R / 2 - (Z - c[1]) * scale
# area-weighted point sampling of triangles
a, b, cc = F[:, 0], F[:, 1], F[:, 2]
area = 0.5 * np.abs((px[b] - px[a]) * (py[cc] - py[a]) - (px[cc] - px[a]) * (py[b] - py[a]))
n = np.minimum(np.ceil(area * 6).astype(np.int64) + 1, 4000)
budget = 4_000_000
if n.sum() > budget:
n = np.maximum((n * (budget / n.sum())).astype(np.int64), 1)
tri = np.repeat(np.arange(len(F)), n)
rng = np.random.default_rng(seed)
r1 = np.sqrt(rng.random(len(tri), np.float32))
r2 = rng.random(len(tri), np.float32)
wa, wb, wc = 1 - r1, r1 * (1 - r2), r1 * r2
ia, ib, ic = a[tri], b[tri], cc[tri]
spx = wa * px[ia] + wb * px[ib] + wc * px[ic]
spy = wa * py[ia] + wb * py[ib] + wc * py[ic]
dep = wa * Y[ia] + wb * Y[ib] + wc * Y[ic]
nn = wa[:, None] * N[ia] + wb[:, None] * N[ib] + wc[:, None] * N[ic]
ix = np.clip(spx.astype(np.int64), 0, R - 1)
iy = np.clip(spy.astype(np.int64), 0, R - 1)
pix = iy * R + ix
order = np.lexsort((-dep, pix)) # per pixel, nearest (largest Y) first
pix_s = pix[order]
first = np.ones(len(pix_s), bool)
first[1:] = pix_s[1:] != pix_s[:-1]
sel = order[first]
# shading in view space: normal x mirrored like the screen, y towards the viewer
nv = np.stack([nn[sel, 0] * sx, nn[sel, 1] * fw, nn[sel, 2]], 1)
nv /= np.maximum(np.linalg.norm(nv, axis=1, keepdims=True), 1e-9)
light = np.array([-0.35, 0.75, 0.55], np.float32)
light /= np.linalg.norm(light)
lam = np.clip(nv @ light, 0, 1)
rim = np.clip(1 - np.abs(nv[:, 1]), 0, 1) ** 3
shade = 0.30 + 0.62 * lam + 0.18 * rim
base = np.array([226, 214, 198], np.float32)
img = np.zeros((R * R, 4), np.float32)
img[pix[sel], :3] = np.clip(base[None] * shade[:, None], 0, 255)
img[pix[sel], 3] = 255
img = img.reshape(R, R, 4)
# skeleton overlay
jx = (J[:, 0] * sx - c[0]) * scale + R / 2
jy = R / 2 - (J[:, 2] - c[1]) * scale
body = np.asarray(d["body_joint_mask"], bool)
from PIL import Image, ImageDraw
im = Image.fromarray(img.astype(np.uint8), "RGBA")
ov = Image.new("RGBA", im.size, (0, 0, 0, 0))
dr = ImageDraw.Draw(ov)
for j, p in enumerate(d["parents"]):
if p >= 0:
col = (255, 122, 26, 235) if body[j] else (64, 160, 255, 220)
dr.line([(jx[p], jy[p]), (jx[j], jy[j])], fill=col, width=max(2, ss + 1))
rr = ss * 1.4
for j in range(len(J)):
col = (255, 244, 230, 255) if body[j] else (200, 230, 255, 255)
dr.ellipse([jx[j] - rr, jy[j] - rr, jx[j] + rr, jy[j] + rr], fill=col)
im = Image.alpha_composite(im, ov)
return np.asarray(im.resize((size, size), Image.LANCZOS))