0
$\begingroup$

I want to toggle Object Mode to Pose Mode by script code, because I usually run blender in Object Mode but sometimes I need to add some keyframes while I run blender. that is why I want to toggle Object Mode to Pose Mode by script code. plz help T_T

I Used and combined this codes.. let me know how to solve it sir T.T

bpy.ops.object.select_all(action = 'DESELECT')
bpy.ops.object.select_by_type(extend = False, type = 'ARMATURE')
bpy.ops.outliner.item_activate(extend=False, deselect_all=True)
bpy.ops.object.posemode_toggle()
bpy.ops.pose.group_select()
first_Bone = bpy.data.objects['Armature'].pose.bones["first_bone"]
first_Bone.select_set(True)
$\endgroup$
0
$\begingroup$

Probably the easiest way for you to toggle between Object Mode and Pose Mode is to simply call bpy.ops.object.posemode_toggle() again. If you're in Object Mode, that will switch you to Pose Mode. If you're in Pose Mode, then you'll go to Object Mode. Here is a link to the Blender 2.83 API, though unfortunately the description is sparse.

Here's a quick script you can run to test, where here I've also created an armature and showed one way to move it via keyframing (since it seems like you were interested in doing that).

import bpy
from mathutils import Vector

# Make armature
bpy.ops.object.armature_add(enter_editmode=False, location=(0, 0, 0))
arm = bpy.context.object

# Enter pose mode.
bpy.ops.object.posemode_toggle()

# Assign keyframes to the armature.
arm.pose.bones['Bone'].keyframe_insert('location', frame=0)
arm.pose.bones["Bone"].location += Vector((0,2,0))
arm.pose.bones['Bone'].keyframe_insert('location', frame=20)
arm.pose.bones["Bone"].location += Vector((0,0,2))
arm.pose.bones['Bone'].keyframe_insert('location', frame=40)

# Return to object mode.
bpy.ops.object.posemode_toggle()

If the code above is in a file test.py, then run blender -P test.py.

If the last line is commented out, you will see this when Blender opens in Pose Mode:

enter image description here

If you leave it un-commented in the code, then you will see this when Blender opens in Object Mode:

enter image description here

$\endgroup$
2
  • $\begingroup$ Oh thank you very very much!! I understand what you mean. Thanks for your Answer!! 8-) $\endgroup$
    – DeerWorld
    Jun 10 '20 at 8:06
  • $\begingroup$ Sounds good. If this answer worked for you, please consider accepting it. @DeerWorld $\endgroup$ Jun 10 '20 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.