I am using Blender to render some data visualization in a batch mode, i.e. by using:
blender.exe --background --python <myscript.py>
where my script is basically:
- completely removing all the default objects (Cube+its mesh, Camera, Light, Material)
- building everything from the scratch from the data supplied in the script
- rendering the scene to the file
Now, from time to time, Blender crashes with something like this:
Error : EXCEPTION_ACCESS_VIOLATION Address : 0x00007FFC0116D530 Module : C:\WINDOWS\SYSTEM32\ntdll.dll
I am rendering several different images (the complexity is basically the same, but the sizes and positions of the objects differ) and it just happens in one, two or three of them (out of 17) randomly i.e. not even in the same files.
Needless to say that when running the same script directly in Blender, it always works. So I am a bit lost how to debug this.
By blindly trying to pinpoint the troubled code I noticed following behavior:
When I do not remove the objects, cameras and lights (materials and meshes seems to be fine) at the beginning of the scene setup, it is likely not crashing anymore.
When I do remove everything at the beginning, but do not link the newly created objects by using
bpy.context.scene.collection.objects.link(obj), it is likely not crashing (but the objects are not in scene then).
When I do remove everything and then before adding any new objects call
bpy.context.view_layer.update(), it is likely not crashing.
The crash happens when adding objects based on meshes from Python data, using this function (but I cannot confirm it is actually in this function):
def add_mesh_obj(name, verts, edges, faces): mesh = bpy.data.meshes.new(name) mesh.from_pydata(verts, edges, faces) obj = bpy.data.objects.new(name, mesh) bpy.context.scene.collection.objects.link(obj) return obj
and it happens before I get to creating the cameras or the lights. So it seems to be related to mesh objects.
While it seems I have kind of fixed the problem (currently by doing
bpy.context.view_layer.update()), I am not sure I understand the cause and if it is a problem in my code, or if it is a bug in Blender, in which case I would report it.
I have been trying to reproduce it on simple testcase, but did not succeed so far, so if anyone has an idea, or an explanation, I will appreciate it.