1

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

alxbilger
  • 11
  • 2
  • 1
    Hello ! AFAIK obj.data.vertices[pos_id].co is 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 use from_pydata. Instead of trying to change the mesh directly and iteratively, make the change to the input data and run from_pydata once at the end – Gorgious Oct 18 '23 at 14:15
  • @Gorgious I must be missing something, I have no doubt you've set a coordinate of a vertex many times in the past, both in bmesh and normal mesh data, so how would the co be read-only? As in, you can't replace the vector with another? – Markus von Broady Oct 18 '23 at 19:40
  • 1
    @MarkusvonBroady you're right, I didn't remember it well. Sorry :) Found this while looking it up https://blender.stackexchange.com/questions/121292/how-can-i-reassign-or-move-vertexes-of-a-vertex-group My next best guess is that there is a discrepancy between the input list and the vertex size – Gorgious Oct 19 '23 at 12:44
  • Thanks for the suggestion. I tried with from_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
  • My script was also crashing after hundreds of frames, never investigated why… Maybe there's a bug in Blender, or it was some racing condition… – Markus von Broady Oct 20 '23 at 15:14

0 Answers0