# Delete components script crashes

I wrote an addon that removes components (using BMesh) depending on the context of the selection. The problem is that he crashes a Blender. I cannot establish a pattern, but most often this happens after several operations in the Edit mode and an attempt to delete all the polygons or n-gon. If I try to repeat the same operations, everything can go well.

since the polygon delete causes the problem, I think in this part of the code:

import bpy
import bmesh

def delete_components():  # for components selection mode

obj = bpy.context.edit_object       # prepearing BMesh variables
me = obj.data
bm = bmesh.from_edit_mesh(me)

sel_faces = [s for s in bm.faces if s.select]   # get the list of selected polygons

if bpy.context.edit_object.type == 'MESH':      # if mesh is selected

sel_mode = bpy.context.tool_settings.mesh_select_mode[:] # define active selection modes

if sel_mode[2] == True:                     # if polygon selection mode is on
for f in sel_faces:                     # unselect all edges belonging to these polygons
for e in f.edges:
e.select = False
bmesh.ops.delete(bm, geom=sel_faces, context = "FACES")    # delete polygons

bmesh.update_edit_mesh(me, True)            #update BMesh mesh

delete_components()


link to the full code: https://github.com/Borschberry/smart_delete

p.s. This is my first addon and I just started working in Blender. Correct me if I incorrectly described the problem.

• Hi. Please reduce your code to a small example that still shows the problem and include the code directly in your question. Jan 27 '19 at 4:24
• Recommend passing arguments to methods rather than using globals. Add some debug print statements to narrow down where it crashes. Not sure whether it's the case now but the delete bmesh operator was notoriously dodgy. Could remove faces from collection instead. Jan 27 '19 at 11:55

Based on your script you appear to be using Blender 2.80. The following may help to address some issues. Blender 2.80 allows for multiple objects to be in edit mode at the same time which is not strictly addressed in the below but the script only acts on the active object in the event multiple objects are selected in edit mode. previously your script could break if run in object mode causing obj = bpy.context.edit_object to be None type and me = obj.data to fail check was added prior to function call.

import bpy
import bmesh

def delete_faces():
obj = bpy.context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
sel_faces = [s for s in bm.faces if s.select]

sel_mode = bpy.context.tool_settings.mesh_select_mode[:] # define active selection modes

if sel_mode[2] == True:  # if polygon selection mode is on
#        bmesh.ops.delete(bm, geom=sel_faces, context=5)  # bl 2.79
bmesh.ops.delete(bm, geom=sel_faces, context='FACES')  # bl 2.80
# deselecting edges is not needed use FACES_ONLY if that is the desired effect
# ('VERTS', 'EDGES', 'FACES_ONLY', 'EDGES_FACES', 'FACES', 'FACES_KEEP_BOUNDARY', 'TAGGED_ONLY')
bmesh.update_edit_mesh(me, True, True)

if bpy.context.mode == 'EDIT_MESH':  # ensures both edit mode and mesh type before call
delete_faces()