50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
import json
|
|
import pathlib
|
|
import sys
|
|
|
|
import bpy
|
|
|
|
|
|
def main(blend_path: str, output_path: str) -> None:
|
|
bpy.ops.wm.open_mainfile(filepath=str(pathlib.Path(blend_path).resolve()), load_ui=False)
|
|
scene = bpy.context.scene
|
|
object_name = "AnimatedObject"
|
|
obj = bpy.data.objects.get(object_name)
|
|
if obj is None:
|
|
raise RuntimeError(f"{object_name} is missing")
|
|
|
|
frames = [1, 5, 10]
|
|
samples = []
|
|
for frame in frames:
|
|
scene.frame_set(frame)
|
|
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
evaluated = obj.evaluated_get(depsgraph)
|
|
mesh = evaluated.to_mesh(preserve_all_data_layers=True, depsgraph=depsgraph)
|
|
try:
|
|
samples.append({
|
|
"frame": frame,
|
|
"worldMatrix": [value for row in evaluated.matrix_world for value in row],
|
|
"positions": [coordinate for vertex in mesh.vertices for coordinate in vertex.co],
|
|
})
|
|
finally:
|
|
evaluated.to_mesh_clear()
|
|
|
|
result = {
|
|
"schemaVersion": 1,
|
|
"fixture": pathlib.Path(blend_path).name,
|
|
"evaluator": "Blender Depsgraph",
|
|
"blenderVersion": bpy.app.version_string,
|
|
"object": object_name,
|
|
"mesh": obj.data.name,
|
|
"frames": samples,
|
|
"tolerance": {"maxPositionError": 1e-5, "rmsPositionError": 1e-6, "maxMatrixError": 1e-5},
|
|
}
|
|
pathlib.Path(output_path).write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if "--" not in sys.argv or len(sys.argv) <= sys.argv.index("--") + 2:
|
|
raise SystemExit("usage: blender -b --python generate-frame-evaluation-golden.py -- input.blend output.json")
|
|
arguments = sys.argv[sys.argv.index("--") + 1:]
|
|
main(arguments[0], arguments[1])
|