Files
animation/characters/work/lena/15_true_weld.py
T

95 lines
3.3 KiB
Python
Raw Normal View History

# Stage 15: TRUE topological weld of the panel seams. Diagnosis: membranes level both sides
# of every seam yet the lines persist -> the two sides are disconnected vertex runs (the
# original "weld" unified positions only). Each panel smooths and shades independently, so a
# crack survives any vertex MOVEMENT. Fix: bmesh remove_doubles restricted to the seam verts,
# which merges the runs into shared vertices -> shared normals -> no shading discontinuity.
# Loops keep their UVs, so the baked texture is unaffected. Vertex count changes; this is
# only legal at the END of the pipeline (masks.npz indices die here).
# blender --background --python 15_true_weld.py -- <in.blend> <raw.glb> <out.blend>
import bpy, bmesh, sys, time
import numpy as np
from mathutils import Vector
from mathutils.kdtree import KDTree
argv = sys.argv[sys.argv.index("--") + 1:]
BLEND, RAW, OUT = argv[0], argv[1], argv[2]
t0 = time.time()
SEAM_TOL = 0.0025
MERGE_DIST = 0.002
def log(m):
print(f"[weld {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)
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)
log(f"working mesh {n_v}v | raw mesh {rn}v | equal: {rn == n_v}")
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)
log(f"raw boundary verts: {rbnd.sum()}")
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]:
for (_, idx, dist) in kd.find_range(Vector(p), SEAM_TOL):
seam[idx] = True
log(f"seam verts (range map): {seam.sum()}")
bm = bmesh.new()
bm.from_mesh(me)
bm.verts.ensure_lookup_table()
sel = [bm.verts[i] for i in np.nonzero(seam)[0]]
res = bmesh.ops.remove_doubles(bm, verts=sel, dist=MERGE_DIST)
bm.to_mesh(me)
bm.free()
me.update()
n_after = len(me.vertices)
log(f"merged: {n_v} -> {n_after} verts (-{n_v - n_after})")
# smooth vertex normals across the now-shared seams
vn = np.empty(n_after * 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("WELD_DONE")