Detect Enter / Leave Edit Mode

Im writing a script for Blender v2.57b, and I need to be able to detect when the user enter or leave edit mode.

I search the API but couldn’t find anything else except:

bpy.ops.object.editmode_toggle()

or bpy.ops.object.mode_set

but there’s no bpy.ops.object.mode_get…

There’s always bpy.context.edit_object that return None or the Object that is currently edited, but is there a more “elegant” way to do this?

All I need to know is 1 or 0 if Im in edit mode or not, anybody?

TIA

look at the mode property of the context object


    obj = bpy.ocntext.object
    if obj:
        print(obj.mode)

i think you mean ‘context’ not ‘ocntext’


obj = bpy.context.object
if obj:
print(obj.mode)

Your question in my (humble) opinion is wrong.

You want to know how to change edit and object mode for an object?

Suppose you have an ‘active’ object then you have (at least) three possibilities:
the toggle you found yourself

and an example (for my scripted tutorial)


def makeFace(v1,v2):
    obj = bpy.context.active_object
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.object.mode_set(mode='OBJECT')
    obj.data.vertices[v1].select = True
    obj.data.vertices[v2].select = True
    bpy.ops.object.editmode_toggle() #edit mode!
    bpy.ops.mesh.edge_face_add()

where you can see other possibilities