Files
animation/characters/work/lena/06g_pixel_melt.py
T

104 lines
3.5 KiB
Python
Raw Normal View History

# Stage 6g: locate the surviving inner-thigh flap by camera ray-cast, melt a sphere there.
# The flap dodged the roughness, boundary-rim, and normal-kink detectors — so aim through
# the diagnostic camera pixel where it is visibly rendered (dbg front_tight, ~px 565,630 of
# 1000^2) and heal whatever the ray hits. Prints the hit so the fix is auditable.
# blender --background --python 06g_pixel_melt.py -- <in.blend> <out.blend>
import bpy, sys, time, math
import numpy as np
from mathutils import Vector, Euler
argv = sys.argv[sys.argv.index("--") + 1:]
BLEND, OUT = argv[0], argv[1]
t0 = time.time()
# front_tight camera from dbg_gusset.py
CAM_LOC = Vector((0.0, -0.55, 0.41))
CAM_ROT = Euler((math.radians(90), 0, 0))
LENS, SENSOR = 85.0, 36.0
# pixels (x, y from top) in the 1000^2 render where the flap shows; a few samples across it
PIXELS = [(560, 615), (568, 628), (575, 640), (582, 652), (562, 640), (572, 618)]
R_MELT = 0.010
MELT_ITERS = 200
def log(m):
print(f"[pix {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)
deps = bpy.context.evaluated_depsgraph_get()
rot = CAM_ROT.to_matrix()
fwd = rot @ Vector((0, 0, -1))
right = rot @ Vector((1, 0, 0))
up = rot @ Vector((0, 1, 0))
half = SENSOR / (2 * LENS)
hits = []
for px, py in PIXELS:
ndc_x = (px / 1000.0 - 0.5) * 2
ndc_y = (0.5 - py / 1000.0) * 2
d = (fwd + right * (ndc_x * half) + up * (ndc_y * half)).normalized()
ok, loc, nrm_h, fi, obj, _ = bpy.context.scene.ray_cast(deps, CAM_LOC, d)
if ok:
hits.append(np.array(loc))
log(f"px({px},{py}) -> hit {np.round(np.array(loc), 4)}")
else:
log(f"px({px},{py}) -> MISS")
if not hits:
log("no hits; aborting without changes")
bpy.context.preferences.filepaths.save_version = 0 # no .blend1 autosave
bpy.ops.wm.save_as_mainfile(filepath=OUT)
sys.exit(0)
hits = np.array(hits)
ctr = hits.mean(axis=0)
log(f"flap centre {np.round(ctr,4)}, spread {np.round(hits.std(axis=0),4)}")
sel = np.linalg.norm(co - ctr, axis=1) < R_MELT
sidx = np.nonzero(sel)[0]
log(f"melt sphere r={R_MELT}: {len(sidx)} verts")
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)
# soft weight: full melt at centre, fades at rim so no new crease forms
w = np.clip(1.0 - np.linalg.norm(co[sidx] - ctr, axis=1) / R_MELT, 0.0, 1.0)
w = w * w * (3 - 2 * w)
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] = Q[sidx] + (mean[sidx] - Q[sidx]) * (0.6 * w[:, None])
d = np.linalg.norm(Q - co, axis=1)
log(f"melted, 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))
bpy.context.preferences.filepaths.save_version = 0 # no .blend1 autosave
bpy.ops.wm.save_as_mainfile(filepath=OUT)
log(f"WROTE {OUT}")
print("PIX_DONE")