Running custom function with hotkey?

Well now, this seems a whole lot more active than the official Blender forums, so I think I’ll post questions here from now on.

Okay, based on the post here, along with some digging, I have come up with the following simple function:

def intuitive_delete():
	mode = bpy.context.tool_settings.mesh_select_mode
	if mode[0] == True:
		#Vertex select mode, delete selected vertices:
		bpy.ops.mesh.delete(type='VERT')
	elif mode[1] == True:
		#Edge select mode, delete selected edges:
		bpy.ops.mesh.delete(type='EGDE')
	elif mode[2] == True:
		#Face select mode, delete selected faces:
		#TODO: Customize face delete type!
		#delType = 'ONLY_FACE'
		delType = 'FACE'
		bpy.ops.mesh.delete(type=delType)
		#Blender leaves the 'nothing' selected, so fix that:
		bpy.ops.mesh.select_all(action='DESELECT')

Now, I want to make it so the ‘delete’ key runs that. Just… runs it. I don’t need it to have a UI button. I don’t need it to be an official add-on yet. I don’t need it to be a command that the hotkey interface will automagically know how to work with. I just need it to unquestioningly call that function.

I know it’s feasible, because if I paste that into the Python console, it works; the function is registered, and running intuitive_delete() will do what I want. But I need some way to, A) Auto-register that function at runtime, and B) Link the ‘delete’ key to intuitive_delete(). It seems very simple, but… apparently not. Which, naturally, is why I’m here asking for help.

Er… Bump? Anyone? Pretty please?


# GPL LICENSE

bl_info = {
    'name': 'make that delete',
    'author': 'yourname',
    'version': (0, 0, 1),
    'blender': (2, 5, 8),
    'description': 'read intuitive delete comments',
    'wiki_url': '',
    'tracker_url': '',
    'category': 'Mesh'}

import bpy

def intuitive_delete():
    print("ran intuitive delete")
    mode = bpy.context.tool_settings.mesh_select_mode
    if mode[0] == True:
        #Vertex select mode, delete selected vertices:
        bpy.ops.mesh.delete(type='VERT')
    elif mode[1] == True:
        #Edge select mode, delete selected edges:
        bpy.ops.mesh.delete(type='EGDE')
    elif mode[2] == True:
        #Face select mode, delete selected faces:
        #TODO: Customize face delete type!
        #delType = 'ONLY_FACE'
        delType = 'FACE'
        bpy.ops.mesh.delete(type=delType)
        #Blender leaves the 'nothing' selected, so fix that:
        bpy.ops.mesh.select_all(action='DESELECT')


class OBJECT_OT_INTUITIVEDELETE(bpy.types.Operator):
    bl_idname = "object.intdelete"
    bl_label = "int delete"
    bl_description = "intuitively delete bla"

    def execute(self, context):
        intuitive_delete()
        return {'FINISHED'}


def register():
    bpy.utils.register_class(OBJECT_OT_INTUITIVEDELETE)

def unregister():
    bpy.utils.unregister_class(OBJECT_OT_INTUITIVEDELETE)


if __name__ == "__main__":
    register()
    

  1. save that as a .py
  2. stick it in Scripts/addons/
  3. in user preferences, go to input->3dview->Mesh-> (click edit to modify)
  4. Scroll down, add a new one, use object.intdelete as the operator name
  5. pick your kb shortcut.

then save a default blendfile as your template that you start with each time blender starts (ctrl+u)

Hmm, I was hoping to avoid using it as a full add-on, but I guess I may as well just use it as a base for what I do intend to make as an add-on… Which that gives me quite a bit of help with right there, so thank you.