how to switch context to Pose Mode and then back to original mode

The details of problem in the code

    # "Protected" Interface
    def _ClearAllBoneTransforms(self):
        armatureBones = None
        armatureBones = self.ExportObject.BlenderObject.data.bones
        # select all bones and clear transforms
        for armatureBone in armatureBones:
            armatureBone.select = True
            armatureBone.select_tail = True
            armatureBone.select_head = True
            armatureBones.active = armatureBone;
        //remember mode, switch somehow to pose mode
        bpy.ops.pose.transforms_clear()  //it fails, I get error failed, context is incorrect - I suppose because I am in Object Mode and need dynamically change context to pose
        //back to remembered mode

In order to switch context you will have to run the operator manually.


import bpy


class SwitchContextsPanel(bpy.types.Panel):
	"""Creates a Panel in the scene context of the properties editor"""
	bl_label = "Switch Contexts Panel Demo"
	bl_idname = "SCENE_PT_switch_context"
	bl_space_type = "PROPERTIES"
	bl_region_type = "WINDOW"
	bl_context = "scene"


	def draw(self, context):
		layout = self.layout
		scene = context.scene
		obj = context.active_object


		if obj.type == "ARMATURE":


			# Method 1
			opmode = "OBJECT" if obj.mode == "POSE" else "POSE"
			optext = ("switch to " + opmode + " mode").title()
			op = layout.operator("object.mode_set", text=optext)
			op.mode = opmode


			# Method 2
			layout.operator("object.posemode_toggle")


bpy.utils.register_class(SwitchContextsPanel)




I don’t know though if there is an automatic way to invoke operators and change contexts, perhaps this functionality is out of Blender’s code design.