# Know when edit-mode is entered by script? (python)

How would I know through a script when the switch to edit mode happens? I need it for modal operator.

I was thinking on listening for 'TAB' event and checking obj.mode, but I hope there is a better way, for reasons like changed edit-mode shortcut or switching to edit-mode by script.

In your modal operator you can test object.mode for which mode the object is in - example:

import bpy

bpy.context.active_object.mode   # = 'OBJECT'

bpy.ops.object.mode_set(mode='EDIT')

bpy.context.active_object.mode   # = 'EDIT'


There is no callback, you will need to test this on 'tab' event or always to be sure..

There is no way to "trigger" your script when the user enters edit mode. The only way for you to run your own code on entering edit mode is to set the keyboard shortcuts to run your script instead of changing to edit mode.

As it sounds like you are referring to switching to edit mode while your modal operator is running, you will need to check the event and enter edit mode when the tab key is pressed, other operators will not be run while your modal operator is running.

def modal(self, context, event):
if event.type == 'TAB':
if context.active_object.mode == 'EDIT':
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
else:
bpy.ops.object.mode_set(mode='EDIT', toggle=False)

• other operators will be run if the modal returns {'PASS_THROUGH'} – Jaroslav Jerryno Novotny Dec 26 '14 at 20:14