# Stage 6f: fold melt in the crotch box, normal-deviation detector. # The mesh has only 3 boundary verts (00_welded closed the slits) — the remaining flap and # dotted seam lines are welded CREASES: positions locally smooth, normals kinked. Detect # verts whose normal deviates > ANG_THR from the 5-iter smoothed normal field, grow, melt. # blender --background --python 06f_fold_melt.py -- import bpy, sys, time, math import numpy as np argv = sys.argv[sys.argv.index("--") + 1:] BLEND, OUT = argv[0], argv[1] t0 = time.time() # optional box override: [ang_deg] if len(argv) >= 5: BX, BZ0, BZ1 = float(argv[2]), float(argv[3]), float(argv[4]) ANG_THR = math.radians(float(argv[5])) if len(argv) > 5 else math.radians(15.0) else: BX, BZ0, BZ1 = 0.075, 0.340, 0.480 ANG_THR = math.radians(15.0) # navel stays: it is a real feature made of exactly the kind of kink this melts NAVEL = lambda co: (np.abs(co[:, 0]) < 0.022) & (co[:, 2] > 0.495) & (co[:, 2] < 0.555) & (co[:, 1] < 0) BOX = lambda co: (np.abs(co[:, 0]) < BX) & (co[:, 2] > BZ0) & (co[:, 2] < BZ1) & ~NAVEL(co) MELT_ITERS = 150 def log(m): print(f"[fold {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) nrm = np.empty(n_v * 3) me.vertices.foreach_get("normal", nrm) nrm = nrm.reshape(-1, 3) n_e = len(me.edges) ev = np.empty(n_e * 2, dtype=np.int32) me.edges.foreach_get("vertices", ev) ev = ev.reshape(-1, 2) order = np.concatenate([ev[:, 0], ev[:, 1]]) nbr = np.concatenate([ev[:, 1], ev[:, 0]]) srt = np.argsort(order, kind="stable") o_s = order[srt] n_s = nbr[srt] ptr = np.searchsorted(o_s, np.arange(n_v + 1)) cnt = np.maximum(ptr[1:] - ptr[:-1], 1) N = nrm.copy() for _ in range(5): acc = np.zeros_like(N) np.add.at(acc, o_s, N[n_s]) N = acc / cnt[:, None] N /= np.maximum(np.linalg.norm(N, axis=1, keepdims=True), 1e-12) dot = np.clip((nrm * N).sum(axis=1), -1.0, 1.0) ang = np.arccos(dot) box = BOX(co) hot = box & (ang > ANG_THR) log(f"box {box.sum()}, folds {hot.sum()} (>{math.degrees(ANG_THR):.0f} deg)") if hot.sum(): hc = co[hot] log(f"fold cluster: x [{hc[:,0].min():+.4f}..{hc[:,0].max():+.4f}] " f"y [{hc[:,1].min():+.4f}..{hc[:,1].max():+.4f}] " f"z [{hc[:,2].min():+.4f}..{hc[:,2].max():+.4f}]") m = hot.copy() for _ in range(3): h = m[ev[:, 0]] | m[ev[:, 1]] m2 = m.copy() m2[ev[:, 0]] |= h m2[ev[:, 1]] |= h m = m2 sidx = np.nonzero(m)[0] Q = co.copy() for _ in range(MELT_ITERS): acc = np.zeros_like(Q) np.add.at(acc, o_s, Q[n_s]) mean = acc / cnt[:, None] Q[sidx] = 0.5 * Q[sidx] + 0.5 * mean[sidx] d = np.linalg.norm(Q - co, axis=1) log(f"melted {len(sidx)} fold verts, max move {d.max():.4f}") 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)) else: log("no folds found") bpy.context.preferences.filepaths.save_version = 0 # no .blend1 autosave bpy.ops.wm.save_as_mainfile(filepath=OUT) log(f"WROTE {OUT}") print("FOLD_DONE")