Scripting: How to change objects mode and then revert back to previous mode In my script I'm trying to set mode to a certain type, but when I'm done with whatever I'm doing I would like to set back the previous mode it was in.

Example: Scene is in EDIT mode, but I need OBJECT mode, but then when I'm done I want to go in to EDIT mode back since it was initial mode.

My code goes:

originalMode = bpy.ops.object.mode
bpy.ops.object.mode_set(mode='OBJECT')

...

bpy.ops.object.mode_set(mode=originalMode)


And I get this error:

  ... File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\modules\bpy\ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
TypeError: Converting py args to operator properties:  expected a string enum, not BPyOpsSubModOp


I don't know how to convert this back to 'EDIT' string to set the mode...
Hlp please :)
I don't know that is this: bpy.ops.object.mode current mode is here: bpy.context.mode

Save current mode:

returnto=False
if(context.mode!='OBJECT'):
    returnto=context.mode


Return to edit mode:

if(returnto):
    bpy.ops.object.mode_set(mode=returnto)


bpy.context.mode