design/figure-preview is a small native project that pins Kryon 199b51c9 and practices b38054fc. It plays Breeze's own Tai Chi, Sun Salutation, Workout and astral choreography on the practices figure, with practice and item pickers, play, pause, scrub, speed, orbit and zoom, the watercolor finish and its settings. It can also show the current flat figure for the same moment. Breeze's pose folders are module paths, so no pose code is copied. --capture writes the review frames, painted, bare and current. prepare-practice-human.py now applies MakeHuman's CC0 young-adult female macro targets. It smooths the hip and shoulder weights, keeps thigh weights below the hip crease, cuts a curved hairline, and adds the hair shell, bun, eyeballs, brows and lips.
467 lines
23 KiB
Python
467 lines
23 KiB
Python
#!/usr/bin/env python3
|
|
"""Bake the pinned CC0 MakeHuman surface for the native practice skin rig."""
|
|
import argparse
|
|
import gzip
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import numpy as np
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
source_file = ROOT / 'design/astral-projection/three/assets/makehuman-base.obj.gz'
|
|
vertices, groups, group = [], {}, ''
|
|
for line in gzip.open(source_file, 'rt'):
|
|
if line.startswith('v '):
|
|
vertices.append(list(map(float, line.split()[1:4])))
|
|
elif line.startswith('g '):
|
|
group = line.strip()[2:]
|
|
groups[group] = []
|
|
elif line.startswith('f '):
|
|
groups[group].append([int(item.split('/')[0]) - 1 for item in line.split()[1:]])
|
|
all_vertices = np.asarray(vertices, dtype=np.float64)
|
|
# MakeHuman's base mesh is its gender-neutral average. Her body and face take
|
|
# MakeHuman's own CC0 young-adult female macro shape: the three ethnic
|
|
# female-young targets in equal shares, as MakeHuman mixes them by default.
|
|
for race in ('african', 'asian', 'caucasian'):
|
|
target = ROOT / f'design/astral-projection/three/assets/makehuman-{race}-female-young.target.gz'
|
|
for line in gzip.open(target, 'rt'):
|
|
if line.strip() and not line.startswith('#'):
|
|
index, dx, dy, dz = line.split()
|
|
all_vertices[int(index)] += np.array([float(dx), float(dy), float(dz)])/3
|
|
ids = sorted({index for face in groups['body'] for index in face})
|
|
lookup = {old: index for index, old in enumerate(ids)}
|
|
body = all_vertices[ids]
|
|
scale = 1.7 / np.ptp(body[:, 1])
|
|
origin = np.array([0, body[:, 1].min(), 0])
|
|
positions = (body - origin) * scale
|
|
|
|
def joint(name):
|
|
indices = sorted({index for face in groups['joint-' + name] for index in face})
|
|
return (all_vertices[indices].mean(axis=0) - origin) * scale
|
|
|
|
bones = []
|
|
|
|
def bone(role, side, start, end, parent=-1, finger=0, segment=0):
|
|
index = len(bones)
|
|
bones.append(dict(role=role, side=side, origin=np.array(start), tip=np.array(end),
|
|
parent=parent, finger=finger, segment=segment))
|
|
return index
|
|
|
|
# The arm and finger chains match the preview's anatomical joints; native
|
|
# standing and floor practices also need the pelvis, spine, head and legs.
|
|
bone(0, 0, joint('pelvis'), joint('spine-3'))
|
|
bone(1, 0, joint('spine-3'), joint('spine-1'), 0)
|
|
bone(2, 0, joint('spine-1'), joint('neck'), 1)
|
|
bone(3, 0, joint('neck'), joint('head-2'), 2)
|
|
arm_sets, leg_sets = [], []
|
|
for side, prefix in enumerate(('l', 'r')):
|
|
arm = []
|
|
parent = 2
|
|
for role, start, end in [(4, 'shoulder', 'elbow'), (5, 'elbow', 'hand'),
|
|
(6, 'hand', 'finger-3-1')]:
|
|
parent = bone(role, side, joint(prefix+'-'+start), joint(prefix+'-'+end), parent)
|
|
arm.append(parent)
|
|
hand = parent
|
|
for finger in range(1, 6):
|
|
parent = hand
|
|
for segment in range(3):
|
|
parent = bone(7, side, joint(f'{prefix}-finger-{finger}-{segment+1}'),
|
|
joint(f'{prefix}-finger-{finger}-{segment+2}'), parent, finger, segment)
|
|
arm.append(parent)
|
|
arm_sets.append(arm)
|
|
leg = []
|
|
parent = 0
|
|
for role, start, end in [(8, 'upper-leg', 'knee'), (9, 'knee', 'ankle'),
|
|
(10, 'ankle', 'toe-3-3')]:
|
|
parent = bone(role, side, joint(prefix+'-'+start), joint(prefix+'-'+end), parent)
|
|
leg.append(parent)
|
|
leg_sets.append(leg)
|
|
|
|
weights = np.zeros((len(ids), 4), dtype=np.float64)
|
|
joints = np.zeros((len(ids), 4), dtype=np.uint8)
|
|
for index, point in enumerate(positions):
|
|
side = 0 if point[0] >= 0 else 1
|
|
shoulder = joint(('l' if side == 0 else 'r') + '-shoulder')
|
|
hip = joint(('l' if side == 0 else 'r') + '-upper-leg')
|
|
candidates = list(range(4)) + arm_sets[side]
|
|
# Blend branch eligibility across the torso instead of cutting the mesh
|
|
# at an X/Y line: adjacent elbow/finger vertices must move together.
|
|
arm_gain = max(np.clip((abs(point[0])-0.25)/0.08, 0, 1),
|
|
np.clip((point[1]-joint('spine-3')[1])/0.15, 0, 1))
|
|
if point[1] < hip[1] + 0.06:
|
|
candidates = [0, 1] + leg_sets[side]
|
|
arm_gain = 1.0
|
|
distances = []
|
|
for at in candidates:
|
|
start, end = bones[at]['origin'], bones[at]['tip']
|
|
delta = end-start
|
|
along = np.clip(np.dot(point-start, delta) / np.dot(delta, delta), 0, 1)
|
|
distances.append(np.linalg.norm(point-start-along*delta))
|
|
scores = 1 / np.maximum(np.asarray(distances), 0.008)**4
|
|
for candidate, at in enumerate(candidates):
|
|
if at >= 4 and bones[at]['role'] <= 7:
|
|
scores[candidate] *= arm_gain*arm_gain
|
|
order = np.argsort(-scores)[:4]
|
|
raw = scores[order]
|
|
raw /= raw.sum()
|
|
joints[index, :len(order)] = np.asarray(candidates)[order]
|
|
weights[index, :len(order)] = raw
|
|
|
|
# MakeHuman's own CC0 rig weights for this mesh, folded onto these 46 bones.
|
|
# The procedural weights above remain only for vertices the rig leaves out.
|
|
weights_file = ROOT / 'design/astral-projection/three/assets/makehuman-default-weights.mhw.gz'
|
|
rig = json.loads(gzip.open(weights_file, 'rt').read())
|
|
assert rig['license'] == 'CC0'
|
|
|
|
def rig_bones(name):
|
|
side = 0 if name.endswith('.L') else 1
|
|
base = 4 + side*21
|
|
stem = name.split('.')[0]
|
|
if stem in ('root', 'pelvis', 'spine05', 'spine04'):
|
|
return [(0, 1.0)]
|
|
if stem in ('spine03', 'spine02'):
|
|
return [(1, 1.0)]
|
|
if stem in ('spine01', 'breast', 'clavicle'):
|
|
return [(2, 1.0)]
|
|
if stem == 'shoulder01':
|
|
return [(2, 0.5), (base, 0.5)]
|
|
if stem.startswith('upperarm'):
|
|
return [(base, 1.0)]
|
|
if stem.startswith('lowerarm'):
|
|
return [(base+1, 1.0)]
|
|
if stem in ('wrist',) or stem.startswith('metacarpal'):
|
|
return [(base+2, 1.0)]
|
|
if stem.startswith('finger'):
|
|
finger, segment = map(int, stem[6:].split('-'))
|
|
return [(base+3+(finger-1)*3+segment-1, 1.0)]
|
|
if stem.startswith('upperleg'):
|
|
return [(base+18, 1.0)]
|
|
if stem.startswith('lowerleg'):
|
|
return [(base+19, 1.0)]
|
|
if stem == 'foot' or stem.startswith('toe'):
|
|
return [(base+20, 1.0)]
|
|
return [(3, 1.0)] # neck, head, jaw, eyes and face
|
|
|
|
for index, (at, side) in enumerate(((0, 0), (0, 1))):
|
|
assert bones[4+side*21]['role'] == 4 and bones[4+side*21+18]['role'] == 8
|
|
gathered = [dict() for _ in ids]
|
|
for name, entries in rig['weights'].items():
|
|
targets = rig_bones(name)
|
|
for vertex, weight in entries:
|
|
if vertex in lookup:
|
|
for target, share in targets:
|
|
gathered[lookup[vertex]][target] = gathered[lookup[vertex]].get(target, 0.0)+weight*share
|
|
for index, influence in enumerate(gathered):
|
|
strongest = sorted(((target, weight) for target, weight in influence.items() if weight > 0.004),
|
|
key=lambda item: -item[1])[:4]
|
|
total = sum(weight for _, weight in strongest)
|
|
if total < 0.5:
|
|
continue
|
|
joints[index] = 0
|
|
weights[index] = 0.0
|
|
for slot, (target, weight) in enumerate(strongest):
|
|
joints[index, slot] = target
|
|
weights[index, slot] = weight/total
|
|
|
|
# MakeHuman hands the groin and buttock from pelvis to thigh within a few
|
|
# millimetres, which tears the surface open in a forward fold or a deep
|
|
# stance. Diffuse that hand-over across the surface around the hips, and the
|
|
# shoulder's chest-to-arm hand-over the same way for arms raised overhead;
|
|
# other bones' shares, the hands and the fingers stay as rigged.
|
|
face_triangles = np.array([[lookup[face[0]], lookup[face[at]], lookup[face[at+1]]]
|
|
for face in groups['body'] for at in range(1, len(face)-1)], dtype=np.int64)
|
|
pairs = np.concatenate([face_triangles[:, [0, 1]], face_triangles[:, [1, 2]], face_triangles[:, [2, 0]]])
|
|
pairs = np.unique(np.concatenate([pairs, pairs[:, ::-1]]), axis=0)
|
|
degree = np.bincount(pairs[:, 0], minlength=len(ids)).astype(np.float64)
|
|
dense = np.zeros((len(ids), len(bones)))
|
|
for slot in range(4):
|
|
np.add.at(dense, (np.arange(len(ids)), joints[:, slot]), weights[:, slot])
|
|
arm_share = dense[:, [at for at, item in enumerate(bones) if 5 <= item['role'] <= 7]].sum(axis=1)
|
|
|
|
def diffuse(members, zone, steps):
|
|
shares = dense[:, members]
|
|
mass = shares.sum(axis=1)
|
|
active = zone & (mass > 0.02)
|
|
for _ in range(steps):
|
|
total = np.zeros_like(shares)
|
|
np.add.at(total, pairs[:, 0], shares[pairs[:, 1]])
|
|
mean = total/np.maximum(degree, 1)[:, None]
|
|
mixed = shares*0.5+mean*0.5
|
|
scale = mass/np.maximum(mixed.sum(axis=1), 1e-12)
|
|
shares[active] = mixed[active]*scale[active, None]
|
|
dense[:, members] = shares
|
|
|
|
hips_y = (joint('l-upper-leg')[1]+joint('r-upper-leg')[1])/2
|
|
hip_zone = (positions[:, 1] > hips_y-0.24) & (positions[:, 1] < hips_y+0.07) & (arm_share < 0.01)
|
|
thighs = [4+18, 4+21+18]
|
|
diffuse([0]+thighs, hip_zone, 120)
|
|
# The thighs never reach above the hip crease: a lifted or spread leg must
|
|
# not drag the waist out with it.
|
|
fade = np.clip((hips_y+0.07-positions[:, 1])/0.09, 0, 1)
|
|
for thigh in thighs:
|
|
removed = dense[:, thigh]*(1-fade)
|
|
dense[:, thigh] -= removed
|
|
dense[:, 0] += removed
|
|
for side in (0, 1):
|
|
shoulder = joint(('l' if side == 0 else 'r')+'-shoulder')
|
|
near = np.linalg.norm(positions-shoulder, axis=1) < 0.16
|
|
diffuse([1, 2, 4+side*21], near & (arm_share < 0.01), 40)
|
|
for index in range(len(ids)):
|
|
strongest = np.argsort(-dense[index])[:4]
|
|
shares = dense[index, strongest]
|
|
shares[shares < 0.004] = 0.0
|
|
joints[index] = strongest
|
|
weights[index] = shares/shares.sum()
|
|
|
|
# The middle waist belongs to the trunk even when a hand passes near it.
|
|
waist = (positions[:, 1] > joint('pelvis')[1]+0.06) & (positions[:, 1] < joint('spine-3')[1]) & (np.abs(positions[:, 0]) < 0.25)
|
|
arm_roles = np.array([4 <= item['role'] <= 7 for item in bones])
|
|
assert not np.any(arm_roles[joints[waist]] & (weights[waist] > 0)), 'Waist cannot follow an arm'
|
|
# Distal fingers must never cross back into the trunk-only region. The
|
|
# shoulder's outward slope is capped before the hand and thumb surface.
|
|
hands = (np.abs(positions[:, 0]) > 0.40) & (positions[:, 2] > 0.20)
|
|
assert np.all(np.sum(weights[hands] * (joints[hands] < 4), axis=1) < 0.1), 'Fingers cannot follow the trunk'
|
|
|
|
triangles = []
|
|
for face in groups['body']:
|
|
for index in range(1, len(face)-1):
|
|
triangles.append([lookup[face[0]], lookup[face[index]], lookup[face[index+1]]])
|
|
triangles = np.array(triangles, dtype=np.int64)
|
|
normals = np.zeros_like(positions)
|
|
face_normals = np.cross(positions[triangles[:, 1]]-positions[triangles[:, 0]],
|
|
positions[triangles[:, 2]]-positions[triangles[:, 0]])
|
|
for corner in range(3):
|
|
np.add.at(normals, triangles[:, corner], face_normals)
|
|
normals /= np.maximum(np.linalg.norm(normals, axis=1)[:, None], 1e-12)
|
|
uvs = np.column_stack((positions[:, 0]*3+0.5, positions[:, 1]*3))
|
|
# Regions keep the same surface and skin weights: skin, tunic, trousers, hair.
|
|
hip_height = joint('pelvis')[1]
|
|
neck_height = joint('neck')[1]
|
|
head = joint('head')
|
|
cuffs = [abs(joint(prefix+'-hand')[0])-0.025 for prefix in ('l', 'r')]
|
|
ankles = [joint(prefix+'-ankle')[1]+0.02 for prefix in ('l', 'r')]
|
|
|
|
eye_height = joint('l-eye')[1]
|
|
|
|
def hairline(points):
|
|
"""Positive on the hair: a hairline high on the forehead, lower over
|
|
the temples, behind the ears and down to the nape; the ears stay bare."""
|
|
points = np.atleast_2d(points)
|
|
x, y, z = points[:, 0], points[:, 1], points[:, 2]
|
|
turn = np.degrees(np.arctan2(np.abs(x), z-0.02))
|
|
height = eye_height+np.interp(turn, [0, 20, 45, 65, 85, 105, 125, 180],
|
|
[0.064, 0.062, 0.052, 0.034, 0.008, -0.014, -0.045, -0.080])
|
|
value = y-height
|
|
ears = (y < eye_height+0.002) & (np.abs(x) > 0.070)
|
|
value = np.where(ears, np.minimum(value, 0.070-np.abs(x)), value)
|
|
return np.where(y < 1.44, -1.0, value)
|
|
|
|
def region_of(points):
|
|
points = np.atleast_2d(points)
|
|
x, y, z = points[:, 0], points[:, 1], points[:, 2]
|
|
regions = np.zeros(len(points), dtype=np.uint8)
|
|
# Sleeves continue over the shoulder cap. A horizontal collar cutoff alone
|
|
# leaves isolated bare patches where the arm rises above the neck cutoff.
|
|
regions[(y > hip_height-0.09) & ((y < neck_height-0.04) |
|
|
((np.abs(x) > 0.115) & (y < neck_height+0.025)))] = 1
|
|
regions[y <= hip_height-0.04] = 2
|
|
for cuff, ankle in zip(cuffs, ankles):
|
|
# Hands hang below the hips in the bind pose; only arms reach this wide.
|
|
regions[(np.abs(x) > cuff) & (y > 0.5)] = 0
|
|
regions[y < ankle] = 0
|
|
regions[hairline(points) > 0] = 3
|
|
return regions
|
|
|
|
# Clean painted hems: split each triangle that crosses a region boundary
|
|
# exactly along the boundary's plane, then give every region its own copy
|
|
# of the shared hem vertices. Surface and skin stay continuous.
|
|
def plane(axis, value):
|
|
return lambda points: np.atleast_2d(points)[:, axis]-value
|
|
|
|
planes = [(1, hip_height-0.04), (1, neck_height-0.04), (1, neck_height+0.025), (0, 0.115), (0, -0.115)]
|
|
planes += [(0, cuff) for cuff in cuffs] + [(0, -cuff) for cuff in cuffs] + [(1, ankle) for ankle in ankles]
|
|
cuts = [(('plane',)+item, plane(*item)) for item in planes] + [(('hairline',), hairline)]
|
|
points, skin_joints, skin_weights = list(positions), list(joints), list(weights)
|
|
surface_normals = list(normals)
|
|
edges = {}
|
|
|
|
def hem_point(a, b, name, cut):
|
|
key = (min(a, b), max(a, b), name)
|
|
if key not in edges:
|
|
da, db = float(cut(points[a])[0]), float(cut(points[b])[0])
|
|
t = da/(da-db)
|
|
points.append(points[a]+(points[b]-points[a])*t)
|
|
normal = surface_normals[a]*(1-t)+surface_normals[b]*t
|
|
surface_normals.append(normal/max(np.linalg.norm(normal), 1e-12))
|
|
mixed = {}
|
|
for chain, share in ((a, 1-t), (b, t)):
|
|
for at, weight in zip(skin_joints[chain], skin_weights[chain]):
|
|
mixed[int(at)] = mixed.get(int(at), 0.0)+weight*share
|
|
strongest = sorted(mixed.items(), key=lambda item: -item[1])[:4]
|
|
total = sum(weight for _, weight in strongest)
|
|
skin_joints.append(np.array([at for at, _ in strongest]+[0]*(4-len(strongest)), dtype=np.uint8))
|
|
skin_weights.append(np.array([weight/total for _, weight in strongest]+[0.0]*(4-len(strongest))))
|
|
edges[key] = len(points)-1
|
|
return edges[key]
|
|
|
|
faces = [list(face) for face in triangles]
|
|
for name, cut in cuts:
|
|
split = []
|
|
for face in faces:
|
|
corners = np.array([points[at] for at in face])
|
|
distance = cut(corners)
|
|
if len(set(region_of(corners))) == 1 or not (np.any(distance > 1e-7) and np.any(distance < -1e-7)):
|
|
split.append(face)
|
|
continue
|
|
side = np.where(np.abs(distance) <= 1e-7, 0, np.sign(distance)).astype(int)
|
|
if 0 in side:
|
|
# One corner lies on the plane: cut the opposite edge once.
|
|
on = int(np.where(side == 0)[0][0])
|
|
a, b, c = face[on], face[(on+1) % 3], face[(on+2) % 3]
|
|
middle = hem_point(b, c, name, cut)
|
|
split += [[a, b, middle], [a, middle, c]]
|
|
continue
|
|
lone = int([at for at in range(3) if list(side).count(side[at]) == 1][0])
|
|
a, b, c = face[lone], face[(lone+1) % 3], face[(lone+2) % 3]
|
|
ab, ac = hem_point(a, b, name, cut), hem_point(a, c, name, cut)
|
|
split += [[a, ab, ac], [ab, b, c], [ab, c, ac]]
|
|
faces = split
|
|
points = np.array(points)
|
|
face_regions = region_of(np.array([points[face].mean(axis=0) for face in faces]))
|
|
# Thumbs sit inside the cuff plane in the bind pose: whatever the hand and
|
|
# fingers carry is skin.
|
|
hand_bones = np.array([item['role'] in (6, 7) for item in bones])
|
|
hand_share = [float(np.sum(skin_weights[at][hand_bones[skin_joints[at]]])) for at in range(len(points))]
|
|
for at, face in enumerate(faces):
|
|
if min(hand_share[corner] for corner in face) > 0.5:
|
|
face_regions[at] = 0
|
|
copies = {}
|
|
order = []
|
|
for face, region in zip(faces, face_regions):
|
|
for corner, at in enumerate(face):
|
|
key = (at, int(region))
|
|
if key not in copies:
|
|
copies[key] = len(order)
|
|
order.append(key)
|
|
face[corner] = copies[key]
|
|
source = np.array([at for at, _ in order])
|
|
regions = np.array([region for _, region in order], dtype=np.uint8)
|
|
positions = points[source]
|
|
normals = np.array(surface_normals)[source]
|
|
joints = np.array(skin_joints)[source]
|
|
weights = np.array(skin_weights)[source]
|
|
uvs = np.column_stack((positions[:, 0]*3+0.5, positions[:, 1]*3))
|
|
triangles = np.array(faces, dtype=np.int64)
|
|
# The head. MakeHuman's own eyeballs sit in the sockets; a hair shell rises
|
|
# from nothing at the hairline to a centimetre over the crown; a low bun sits
|
|
# at the back. All follow the head bone alone. Beyond the four washes the
|
|
# regions are 4 iris, 5 eye white, 6 lips and 7 brows.
|
|
def outward_faces(vertices, faces, center):
|
|
faces = np.array(faces, dtype=np.int64)
|
|
normal = np.cross(vertices[faces[:, 1]]-vertices[faces[:, 0]], vertices[faces[:, 2]]-vertices[faces[:, 0]])
|
|
flip = np.einsum('ij,ij->i', normal, vertices[faces].mean(axis=1)-center) < 0
|
|
faces[flip] = faces[flip][:, ::-1]
|
|
return faces
|
|
|
|
def vertex_normals(vertices, faces):
|
|
result = np.zeros_like(vertices)
|
|
face = np.cross(vertices[faces[:, 1]]-vertices[faces[:, 0]], vertices[faces[:, 2]]-vertices[faces[:, 0]])
|
|
for corner in range(3):
|
|
np.add.at(result, faces[:, corner], face)
|
|
return result/np.maximum(np.linalg.norm(result, axis=1)[:, None], 1e-12)
|
|
|
|
extras = []
|
|
hair_faces = triangles[np.all(regions[triangles] == 3, axis=1)]
|
|
used = np.unique(hair_faces)
|
|
depth = hairline(positions[used])
|
|
crown = np.clip((positions[used, 1]-eye_height-0.02)/0.08, 0, 1)
|
|
sides = np.clip(np.abs(positions[used, 0])/0.07, 0, 1)
|
|
thickness = (0.008+0.006*crown+0.005*sides)*np.clip(depth/0.025, 0, 1)**0.6
|
|
shell = positions[used]+normals[used]*thickness[:, None]
|
|
remap = np.full(len(positions), -1, dtype=np.int64)
|
|
remap[used] = np.arange(len(used))
|
|
shell_faces = remap[hair_faces]
|
|
extras.append((shell, vertex_normals(shell, shell_faces), np.full(len(shell), 3), shell_faces))
|
|
rings, segments = 10, 18
|
|
bun_center = np.array([0.0, eye_height+0.078, -0.056])
|
|
bun_radii = np.array([0.040, 0.036, 0.033])
|
|
units = np.array([[np.sin(np.pi*ring/rings)*np.cos(2*np.pi*at/segments), np.cos(np.pi*ring/rings),
|
|
np.sin(np.pi*ring/rings)*np.sin(2*np.pi*at/segments)]
|
|
for ring in range(rings+1) for at in range(segments)])
|
|
bun = bun_center+units*bun_radii
|
|
bun_faces = []
|
|
for ring in range(rings):
|
|
for at in range(segments):
|
|
a, b = ring*segments+at, ring*segments+(at+1) % segments
|
|
c, d = a+segments, b+segments
|
|
if ring > 0:
|
|
bun_faces.append([a, b, d])
|
|
if ring < rings-1:
|
|
bun_faces.append([a, d, c])
|
|
bun_faces = outward_faces(bun, bun_faces, bun_center)
|
|
extras.append((bun, vertex_normals(bun, bun_faces), np.full(len(bun), 3), bun_faces))
|
|
for prefix in ('l', 'r'):
|
|
indices = sorted({index for face in groups[f'helper-{prefix}-eye'] for index in face})
|
|
eye_lookup = {old: at for at, old in enumerate(indices)}
|
|
eye = (all_vertices[indices]-origin)*scale
|
|
center = eye.mean(axis=0)
|
|
eye_faces = outward_faces(eye, [[eye_lookup[face[0]], eye_lookup[face[at]], eye_lookup[face[at+1]]]
|
|
for face in groups[f'helper-{prefix}-eye'] for at in range(1, len(face)-1)], center)
|
|
gaze = joint(f'{prefix}-eye-target')-joint(f'{prefix}-eye')
|
|
gaze /= np.linalg.norm(gaze)
|
|
facing = (eye-center)/np.linalg.norm(eye-center, axis=1)[:, None] @ gaze
|
|
extras.append((eye, vertex_normals(eye, eye_faces), np.where(facing > np.cos(np.radians(32)), 4, 5), eye_faces))
|
|
# Brows arch over the eyes and the lips fill the mouth; both are painted on
|
|
# the skin's own vertices, so their edges stay soft.
|
|
x, y, z = positions[:, 0], positions[:, 1], positions[:, 2]
|
|
for prefix in ('l', 'r'):
|
|
eye = joint(prefix+'-eye')
|
|
across = (x-eye[0])/0.023
|
|
middle = eye[1]+0.017+0.004*(1-across**2)-0.002*np.sign(eye[0])*across
|
|
brow = (np.abs(across) < 1) & (np.abs(y-middle) < 0.0024*(1.25-0.6*np.abs(across))) & (z > eye[2]-0.005)
|
|
regions[brow & (regions == 0)] = 7
|
|
mouth = joint('mouth')
|
|
# MakeHuman's mouth joint is the jaw's pivot, level with the nose tip.
|
|
lip_height = mouth[1]-0.027
|
|
lips = ((x/0.021)**2+((y-lip_height)/0.0088)**2 < 1) & (z > 0.145)
|
|
regions[lips & (regions == 0)] = 6
|
|
head_index = [at for at, item in enumerate(bones) if item['role'] == 3][0]
|
|
for vertices, vertex_normal, vertex_region, faces_of in extras:
|
|
base = len(positions)
|
|
positions = np.vstack([positions, vertices])
|
|
normals = np.vstack([normals, vertex_normal])
|
|
joints = np.vstack([joints, np.tile(np.array([head_index, 0, 0, 0], dtype=np.uint8), (len(vertices), 1))])
|
|
weights = np.vstack([weights, np.tile(np.array([1.0, 0.0, 0.0, 0.0]), (len(vertices), 1))])
|
|
regions = np.concatenate([regions, np.asarray(vertex_region, dtype=np.uint8)])
|
|
triangles = np.vstack([triangles, np.asarray(faces_of, dtype=np.int64)+base])
|
|
uvs = np.column_stack((positions[:, 0]*3+0.5, positions[:, 1]*3))
|
|
quantized = np.rint(weights*65535).astype(np.int64)
|
|
quantized[:, 0] += 65535-quantized.sum(axis=1)
|
|
assert np.all(quantized >= 0) and np.all(quantized <= 65535)
|
|
assert np.isfinite(positions).all() and np.isfinite(normals).all()
|
|
assert np.max(joints) < len(bones) < 256
|
|
assert np.max(triangles) < len(positions) < 65536
|
|
assert np.all(quantized.sum(axis=1) == 65535)
|
|
assert len(bones) == 46
|
|
blob = bytearray(struct.pack('<8sIIIf', b'PFIG0001', len(positions), triangles.size, len(bones), 1.7))
|
|
for item in bones:
|
|
blob.extend(struct.pack('<4Bi6f', item['role'], item['side'], item['finger'], item['segment'],
|
|
item['parent'], *item['origin'], *item['tip']))
|
|
for point, normal, uv, influence, weight, region in zip(positions, normals, uvs, joints, quantized, regions):
|
|
blob.extend(struct.pack('<3f3h2f4B4HBB', *point, *np.rint(normal*32767).astype(int),
|
|
*uv, *influence, *weight, int(region), 0))
|
|
blob.extend(triangles.astype('<u2').tobytes())
|
|
output = args.output.resolve()
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
(output/'anatomy.bin').write_bytes(blob)
|
|
metadata = {'format':'PFIG0001', 'vertices':len(positions), 'triangles':len(triangles),
|
|
'bones':len(bones), 'bytes':len(blob), 'sha256':hashlib.sha256(blob).hexdigest(),
|
|
'source':'https://github.com/makehumancommunity/makehuman/blob/a8bc2d54ff0ac92e78ff71431b1023eda42bf482/makehuman/data/3dobjs/base.obj',
|
|
'license':'CC0-1.0', 'bone_names':[f"{b['role']}/{b['side']}/{b['finger']}/{b['segment']}" for b in bones]}
|
|
(output/'anatomy.json').write_text(json.dumps(metadata,indent=2)+'\n')
|
|
print(f"Prepared {len(positions)} vertices, {len(triangles)} triangles, {len(bones)} bones; {len(blob)} bytes")
|