I am a developer of a physics simulation engine, and I want to connect it to Blender, so I can render simulations in Blender. The physics engine simulates soft bodies. Deformation of meshes occurs, usually at each time step.
I use Blender 3.6.4 on Windows.
The approach I took is the following:
- Just after the first time step of the simulation, I create a new collection, with a new object, with a new mesh using the data I just computed from the simulation:
new_mesh = bpy.data.meshes.new(object_name)
new_mesh.clear_geometry()
new_mesh.from_pydata(object_data["position"], {}, object_data["faces"])
new_mesh.update()
new_object = bpy.data.objects.new(object_name, new_mesh)
collection.objects.link(new_object)
- After the following time steps, I update the vertices and insert a keyframe on them:
obj = collection.objects[object_name]
pos_id = 0
for v in object_data["position"]:
if len(obj.data.vertices) > pos_id:
obj.data.vertices[pos_id].co = v
obj.data.vertices[pos_id].keyframe_insert("co", frame=iteration)
pos_id = pos_id + 1
What do you think about the approach? Can you suggest a more appropriate strategy?
In the meantime, the current approach makes Blender crash randomly: not always the same time step, not always the same stack trace. However, I often have the crash happening on obj.data.vertices[pos_id].co = v, leading (sometimes) to the following stack trace:
blender.exe :0x00007FF648F1C600 blender::deg::`anonymous namespace'::deg_graph_node_tag_zero
blender.exe :0x00007FF648F1CC20 blender::deg::graph_id_tag_update
blender.exe :0x00007FF648F1D700 DEG_id_tag_update
blender.exe :0x00007FF6491C1280 rna_Mesh_update_positions_tag
blender.exe :0x00007FF6490FBC40 rna_property_update
blender.exe :0x00007FF649107340 RNA_property_update
Any idea how to solve this crash?
Thanks
obj.data.vertices[pos_id].cois readonly so I'm not surprised you have a crash when you try to assign it. The most straightforward and fast way to set a mesh is to usefrom_pydata. Instead of trying to change the mesh directly and iteratively, make the change to the input data and runfrom_pydataonce at the end – Gorgious Oct 18 '23 at 14:15cobe read-only? As in, you can't replace the vector with another? – Markus von Broady Oct 18 '23 at 19:40from_pydata, but I still have the crash. I tried to reduce the problem by removing the keyframe management, by removing the faces, by removing the collection hierarchy. Still the random crash. I also verified the validity of the data – alxbilger Oct 20 '23 at 14:04