I have a script that needs to run some special update code every time I enter or leave edit mode. To this end I created a custom editmode toggle button, which I tied to the spacebar. Works as expected: every time I hit spacebar, Blender toggles edit mode and writes a message in the terminal.
However, I would really like to overwrite the behavior of the tab button. I would expect that all that is needed is to replace ‘SPACE’ by ‘TAB’ in the script below, but it does not work. When I hit tab, the built-in editmode toggle is run, and not my custom script. Is this a but, or the intended behaviour?
import bpy
class MH_OT_editmode_toggle(bpy.types.Operator):
bl_idname = "mh.editmode_toggle"
bl_label = "Editmode toggle"
def execute(self, context):
ob = context.object
if not (ob and ob.type == 'MESH'):
print("No mesh")
elif ob.mode == 'EDIT':
bpy.ops.object.mode_set(mode='OBJECT')
print("Object mode set")
else:
bpy.ops.object.mode_set(mode='EDIT')
print("Edit mode set")
return{'FINISHED'}
def register():
bpy.utils.register_module(__name__)
km = bpy.context.window_manager.keyconfigs.active.keymaps['3D View']
km.keymap_items.new("mh.editmode_toggle", 'SPACE', 'PRESS')
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()