Why does bpy.ops.object.mode_set(mode='EDIT') switch to the Object Mode when executed?

I wrote a python script that modifies an existing blender armature that I have and some of these modifications include some commands that are only possible in Edit Mode. I know that depending on what is selected, some modes are not possible to switch to. In my case Pose, Edit and Object mode are the three available modes when I select my armature. In this instance, the following three commands could be used:

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

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

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


Of the above three only the second one switches to wrong mode i.e. Object Mode. I found no similar questions/forums on this kind of issue. When trying to change to Edit Mode, blender changes to Object mode and my commands that are intended for edit mode show context error messages (as expected if in Pose/Object mode..) making it not possible for my script to apply the correct changes.

Does anyone have any idea why this is happening and how I could stop this from happening?? Thanks

• Could we see your script, please? It might be faulty. – Lukasz-40sth Jan 22 '19 at 9:59
• right in the beginning, when you try to switch to edit mode it does not work. When you select the armature, the menu bar with the modes shows Pose, Edit and Object Mode and the corresponding commands work except for the Edit mode one.. its the first like of my script... – Yasmine Jan 23 '19 at 1:32
• I just experienced this today (in Blender 2.79). In my case, I set one armature to edit mode, deselected it, selected a different armature, then tried to set the different armature to edit mode -- this was where the mode_set("EDIT") silently failed. However, if I set the first armature back to object mode before selecting the other armature, then the mode_set("EDIT") on the other armature would succeed. I could be wrong, but Blender seems to only allow 1 armature to be in edit mode, and selecting a different armature (with bpy) doesn't automatically put the first back into object mode. – TiberiumFusion Feb 9 at 7:11