Files

56 lines
2.0 KiB
Python
Raw Permalink Normal View History

# Local melt of the crotch underside strip (residue from the donor snap's cage edge).
# blender --background --python 06b_strip_melt.py -- <in.blend> <out.blend>
import bpy, sys, time
import numpy as np
argv = sys.argv[sys.argv.index("--") + 1:]
t0 = time.time()
bpy.ops.wm.open_mainfile(filepath=argv[0])
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)
mid = (np.abs(co[:, 0]) < 0.01) & (co[:, 2] > 0.38) & (co[:, 2] < 0.50)
sad = co[mid][np.argmin(co[mid, 2])]
# ABSOLUTE bounds: the donor snap moved the saddle landmark, so a saddle-relative window
# centred 3.5 cm below the visible residue
strip = (np.abs(co[:, 0]) < 0.048) & (co[:, 2] > 0.368) & (co[:, 2] < 0.434)
sidx = np.nonzero(strip)[0]
print(f"[strip] {len(sidx)} verts around saddle {sad}")
ev = np.empty(len(me.edges) * 2, dtype=np.int32)
me.edges.foreach_get("vertices", ev)
ev = ev.reshape(-1, 2)
o_ = np.concatenate([ev[:, 0], ev[:, 1]])
n_ = np.concatenate([ev[:, 1], ev[:, 0]])
s_ = np.argsort(o_, kind="stable")
o_s = o_[s_]
n_s = n_[s_]
ptr = np.searchsorted(o_s, np.arange(n_v + 1))
cnt = np.maximum(np.diff(ptr), 1)
Q = co.copy()
for _ in range(120):
su = np.add.reduceat(Q[n_s], ptr[:-1], axis=0)
emp = np.diff(ptr) == 0
su[emp] = Q[emp]
mean = su / cnt[:, None]
for f in (0.55, -0.58):
pass
Q[sidx] = 0.45 * Q[sidx] + 0.55 * mean[sidx]
print(f"[strip] melted, max move {np.linalg.norm(Q-co,axis=1).max():.4f} "
f"({time.time()-t0:.1f}s)")
me.vertices.foreach_set("co", Q.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=argv[1])
print("STRIP_DONE")