HWI return what type of selection mode I'm in?

I want to create “if” conditional statements that ask if the user is in vertex, edge or face selection mode while in edit mode, such that I can create a hotkey to bypass the call menu that arises when you go to delete a selection of mesh. Can you do this?

Bonus question: Does anyone know if there is an operator or setting I can use to change the state of the “editing cage mode” of a subdivision surface modifier? Thanks.

vert_sel, edge_sel, face_sel = bpy.context.tool_settings.mesh_select_mode

if vert_sel:
    bpy.ops.mesh.delete(type='VERT')
elif edge_sel:
    bpy.ops.mesh.delete(type='EDGE')
elif face_sel:
    bpy.ops.mesh.delete(type='FACE')

Note that there is FACE_EDGE mode and there can be multiple select modes at the same time. You could do more checks and handle them in a meaningful way.

Overwrite the default X shortcut with an operator that does the above checks to replace the builtin delete menu.

Dude, perfect. Thankyou. I’m going to toy around with these a little bit before marking the thread as solved. Thanks again.

How would I check if I’m in edit mode?

for a global check:

bpy.context.mode.startswith(‘EDIT’) # there are multiple, e.g. EDIT_MESH, EDIT_ARMATURE

Objects also store mode, but you need to ensure there is a context object:

ob = bpy.context.object
if ob is not None:
    ob.mode == 'EDIT'
# else it should be object mode

Thanks. How would I implement that global check into a conditional? I’m still pretty new to Python. I’ve got one hotkey set up to delete objects in object mode, to bypass Blender’s call menu and get straight on to the deleting, and I’d like to use that same key to do the selection type sensitive deleting while in edit mode. I keep getting one working or the other. I could set up separate operators for them but that would not be ideal. Thanks.

For anyone looking for the second part of my original question I’ve discovered the answer to turning on editing cages:

bpy.context.object.modifiers[“Subsurf”].show_on_cage = True

(Switch out “Subsurf” for whichever modifier you wish to display the editing cage.)

it’s already a condition, just missing the “if”:

if bpy.context.mode.startswith('EDIT'):

but i would actually use the object-based test.

ob = bpy.context.object
if ob is None or ob.type != 'MESH':
    # do nothing
    pass
elif ob.mode == 'EDIT':
    bpy.ops.mesh.delete(...)
else:
    bpy.ops.object.delete(...)
    

Another way would be to poll-check:

if bpy.ops.mesh.delete.poll():
    # only if we have a mesh!
elif bpy.ops.object.delete.poll():
    # delete object if possible, otherwise do nothing

Thanks, I had tried using the global check as an if, but managed to forget I needed a “:” at the end of my if statement. Two many programing languages swirling around in my head.:spin: All works now. I have now successfully injected my “X” key with amphetamines lol. Once again, thanks so much.