3ba86b2ea8
Bulk import of the working lanes that were living untracked on the PC. Content: - characters/ Lena/male body lanes, bakes, texture work, run logs - clothing/ garment pipeline, configs, gates, contract docs - garments/ MD-authored garment sources (.zprj/.zpac) - UAL-Lib/ Universal Animation Library 2 source (.blend/.fbx/.glb) - tools/ blender_bridge, iclone_bridge, md_bridge, tailor, glm_agent - docs/, plans/, dev/, .agents/plans/ Repo hygiene: - .gitattributes: LFS now covers .blend, .zprj, .zpac, .obj, .npy and the Reallusion .iAvatar/.ccAvatar/.ccRestore containers. Without this the ~3.8 GB in this commit would land as raw blobs. .png/.jpg are left out on purpose — ~250 are already tracked raw and converting them would rewrite every one without shrinking history. - .gitignore: exclude /accurig/ (~1 GB AccuRig program files, redistributable from Reallusion, nothing authored here) and /dev/null/ (git-lfs hook copies dropped by a `>/dev/null` redirect on Windows). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
169 lines
5.0 KiB
Python
169 lines
5.0 KiB
Python
# Stage 13: heal the garment-BOUNDARY dig-ins. Every stomach/waist line sits where an old
|
|
# garment mask edge was: the sculpt replaced the region INSIDE the mask, but anchored its
|
|
# membrane on the rim — exactly where the waistband/leg-hems/bra-band dug into the body, so
|
|
# the dig-in ring survived as the boundary condition. Free a band STRADDLING every garment
|
|
# boundary (grow-out XOR shrink-in), plus slit seams on unmoved skin (0.5 mm mapping), and
|
|
# solve the rim-anchored membrane across it.
|
|
# blender --background --python 13_rim_membrane.py -- <in.blend> <masks.npz> <raw.glb> <out.blend>
|
|
import bpy, sys, time
|
|
import numpy as np
|
|
from mathutils import Vector
|
|
from mathutils.kdtree import KDTree
|
|
|
|
argv = sys.argv[sys.argv.index("--") + 1:]
|
|
BLEND, MASKS, RAW, OUT = argv[0], argv[1], argv[2], argv[3]
|
|
t0 = time.time()
|
|
|
|
RIM = 10 # rings each side of the garment boundary (~1 cm)
|
|
Z0, Z1 = 0.40, 0.86
|
|
SEAM_TOL = 0.0005 # only trust seam mapping on UNMOVED skin
|
|
|
|
|
|
def log(m):
|
|
print(f"[rim {time.time()-t0:6.1f}s] {m}", flush=True)
|
|
|
|
|
|
bpy.ops.wm.open_mainfile(filepath=BLEND)
|
|
ob = max([o for o in bpy.data.objects if o.type == 'MESH'],
|
|
key=lambda o: len(o.data.vertices))
|
|
me = ob.data
|
|
n_v = len(me.vertices)
|
|
co = np.empty(n_v * 3)
|
|
me.vertices.foreach_get("co", co)
|
|
co = co.reshape(-1, 3)
|
|
M = np.load(MASKS)
|
|
garment = M["garment"]
|
|
|
|
n_e = len(me.edges)
|
|
ev0 = np.empty(n_e * 2, dtype=np.int32)
|
|
me.edges.foreach_get("vertices", ev0)
|
|
ev0 = ev0.reshape(-1, 2)
|
|
|
|
|
|
def grow_edges(mask, rings, ev):
|
|
m = mask.copy()
|
|
for _ in range(rings):
|
|
hit = m[ev[:, 0]] | m[ev[:, 1]]
|
|
m2 = m.copy()
|
|
m2[ev[:, 0]] |= hit
|
|
m2[ev[:, 1]] |= hit
|
|
m = m2
|
|
return m
|
|
|
|
|
|
def shrink_edges(mask, rings, ev):
|
|
return ~grow_edges(~mask, rings, ev)
|
|
|
|
|
|
rim_band = grow_edges(garment, RIM, ev0) & ~shrink_edges(garment, RIM, ev0)
|
|
log(f"garment rim band: {rim_band.sum()}")
|
|
|
|
# slit seams on unmoved skin
|
|
before = set(bpy.data.objects)
|
|
bpy.ops.import_scene.gltf(filepath=RAW)
|
|
new = [o for o in bpy.data.objects if o not in before]
|
|
raw = max([o for o in new if o.type == 'MESH'], key=lambda o: len(o.data.vertices))
|
|
rme = raw.data
|
|
rn = len(rme.vertices)
|
|
rco = np.empty(rn * 3)
|
|
rme.vertices.foreach_get("co", rco)
|
|
rco = rco.reshape(-1, 3)
|
|
l_tot = np.empty(len(rme.polygons), dtype=np.int32)
|
|
rme.polygons.foreach_get("loop_total", l_tot)
|
|
l_start = np.empty(len(rme.polygons), dtype=np.int32)
|
|
rme.polygons.foreach_get("loop_start", l_start)
|
|
l_v = np.empty(len(rme.loops), dtype=np.int32)
|
|
rme.loops.foreach_get("vertex_index", l_v)
|
|
ecount = {}
|
|
for fs, ft in zip(l_start, l_tot):
|
|
idxs = l_v[fs:fs + ft]
|
|
for k in range(ft):
|
|
a, b = idxs[k], idxs[(k + 1) % ft]
|
|
kk = (a, b) if a < b else (b, a)
|
|
ecount[kk] = ecount.get(kk, 0) + 1
|
|
rbnd = np.zeros(rn, dtype=bool)
|
|
for (a, b), c in ecount.items():
|
|
if c == 1:
|
|
rbnd[a] = rbnd[b] = True
|
|
for o in new:
|
|
bpy.data.objects.remove(o, do_unlink=True)
|
|
|
|
kd = KDTree(n_v)
|
|
for i in range(n_v):
|
|
kd.insert(Vector(co[i]), i)
|
|
kd.balance()
|
|
seam = np.zeros(n_v, dtype=bool)
|
|
for p in rco[rbnd]:
|
|
if not (Z0 < p[2] < Z1):
|
|
continue
|
|
hit = kd.find(Vector(p))
|
|
if hit[0] is not None and hit[2] < SEAM_TOL:
|
|
seam[hit[1]] = True
|
|
log(f"seam verts (tight map): {seam.sum()}")
|
|
|
|
navel = (np.abs(co[:, 0]) < 0.022) & (co[:, 2] > 0.495) & (co[:, 2] < 0.555) & (co[:, 1] < 0)
|
|
free_m = ((rim_band | grow_edges(seam, 3, ev0)) &
|
|
(co[:, 2] > Z0) & (co[:, 2] < Z1) & ~navel)
|
|
collar = grow_edges(free_m, 2, ev0) & ~free_m
|
|
S = np.nonzero(free_m | collar)[0]
|
|
in_S = np.zeros(n_v, dtype=bool)
|
|
in_S[S] = True
|
|
glb = np.full(n_v, -1, dtype=np.int64)
|
|
glb[S] = np.arange(len(S))
|
|
se = ev0[in_S[ev0].all(axis=1)]
|
|
a_ = glb[se[:, 0]]
|
|
b_ = glb[se[:, 1]]
|
|
deg = np.zeros(len(S))
|
|
np.add.at(deg, a_, 1.0)
|
|
np.add.at(deg, b_, 1.0)
|
|
free = free_m[S]
|
|
log(f"free {free.sum()}, collar {(~free).sum()}")
|
|
|
|
|
|
def Ls(X):
|
|
out = deg[:, None] * X
|
|
np.add.at(out, a_, -X[b_])
|
|
np.add.at(out, b_, -X[a_])
|
|
return out
|
|
|
|
|
|
def A_op(U):
|
|
X = np.zeros((len(S), 3))
|
|
X[free] = U
|
|
return Ls(Ls(X))[free]
|
|
|
|
|
|
Xc = np.zeros((len(S), 3))
|
|
Xc[~free] = co[S[~free]]
|
|
rhs = -Ls(Ls(Xc))[free]
|
|
U = co[S[free]].copy()
|
|
r = rhs - A_op(U)
|
|
p = r.copy()
|
|
rs = (r * r).sum()
|
|
rs0 = rs
|
|
for it in range(120000):
|
|
Ap = A_op(p)
|
|
al = rs / max((p * Ap).sum(), 1e-30)
|
|
U += al * p
|
|
r -= al * Ap
|
|
rs2 = (r * r).sum()
|
|
if rs2 < 1e-18 or rs2 < rs0 * 1e-14:
|
|
break
|
|
p = r + (rs2 / rs) * p
|
|
rs = rs2
|
|
co_new = co.copy()
|
|
co_new[S[free]] = U
|
|
d = np.linalg.norm(co_new - co, axis=1)
|
|
log(f"membrane: {free.sum()} verts (CG {it}, rel {rs2/max(rs0,1e-30):.2e}), max move {d.max():.4f}")
|
|
|
|
me.vertices.foreach_set("co", co_new.reshape(-1))
|
|
me.update()
|
|
if me.has_custom_normals:
|
|
vn = np.empty(n_v * 3, dtype=np.float32)
|
|
me.vertices.foreach_get("normal", vn)
|
|
me.normals_split_custom_set_from_vertices(vn.reshape(-1, 3))
|
|
bpy.context.preferences.filepaths.save_version = 0 # no .blend1 autosave
|
|
bpy.ops.wm.save_as_mainfile(filepath=OUT)
|
|
log(f"WROTE {OUT}")
|
|
print("RIM_DONE")
|