File size: 2,547 Bytes
92f480e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | """Blender-only extraction; mappings are independently inferred by the verifier."""
import json
import sys
from pathlib import Path
import bpy
from mathutils import Vector
args = sys.argv[sys.argv.index("--")+1:]
mapping = json.loads(Path(args[0]).read_text())
scene = bpy.context.scene
arm = bpy.data.objects[mapping["armature"]]
body = bpy.data.objects[mapping["body"]]
root = mapping["root"]
feet = mapping["feet"]
if scene.frame_end - scene.frame_start > 2400:
raise ValueError("Animation exceeds supported 2401-frame inspection; evaluator review required")
height = max(v.co.z for v in body.data.vertices)-min(v.co.z for v in body.data.vertices)
height *= abs(body.matrix_world.to_scale().z)
rest_root = arm.matrix_world @ arm.data.bones[root].matrix_local
foot = arm.data.bones[feet[0]]
forward = arm.matrix_world.to_3x3() @ (foot.tail_local-foot.head_local)
forward.z = 0
if forward.length < .00001:
raise ValueError("Cannot infer character facing from mapped foot")
forward.normalize()
forward_in_root = rest_root.to_3x3().inverted() @ forward
rows = []
initial_vertices = None
for frame in range(scene.frame_start, scene.frame_end+1):
scene.frame_set(frame)
graph = bpy.context.evaluated_depsgraph_get()
ea = arm.evaluated_get(graph)
rm = ea.matrix_world @ ea.pose.bones[root].matrix
direction = rm.to_3x3() @ forward_in_root
direction.z = 0
direction.normalize()
fp = [ea.matrix_world @ ea.pose.bones[n].head for n in feet]
eb = body.evaluated_get(graph)
mesh = eb.to_mesh()
try:
verts = [rm.inverted() @ (eb.matrix_world @ v.co) for v in mesh.vertices]
if initial_vertices is None:
initial_vertices = [v.copy() for v in verts]
if len(verts) != len(initial_vertices):
raise ValueError("Changing topology needs evaluator review")
rms = (sum((a-b).length_squared for a,b in zip(verts,initial_vertices))/len(verts))**.5
finally:
eb.to_mesh_clear()
rows.append({"frame":frame,"position":list(rm.translation),"forward":list(direction),
"foot_lead":(fp[0]-fp[1]).dot(direction),
"foot_heights":[p.z-rm.translation.z for p in fp],"deformation_rms":rms})
report = {"blender_version":bpy.app.version_string,"mapping":mapping,"character_height":height,
"fps":scene.render.fps/scene.render.fps_base,"frames":rows,
"bound_character":any(m.type=="ARMATURE" and m.object==arm and m.show_viewport for m in body.modifiers)}
Path(args[1]).write_text(json.dumps(report)+"\n")
|