I am writing my first Python script. For a selected armature, it copies the current action and creates a mirrored copy (flipping all poses Left to right and right to left.
Here is the code:
#****************************************
#MIRROR ACTION
#****************************************
import bpy
Last_Frame = -1
Current_Frame = bpy.context.scene.frame_current #get current frame number
Original_Action = bpy.context.object.animation_data.action #get name of active action in active object/armature
Mirror_Action_Name = Original_Action.name + '.mirror' #create a new name for action, which will be mirrored
Mirror_Action = Original_Action.copy() #create a copy action, which will be mirrored
Mirror_Action.name = Mirror_Action_Name #name the new action
bpy.context.object.animation_data.action = Mirror_Action #set the new action as the active action for the object/armature
while Last_Frame != Current_Frame: #iterate through all the frames in the current action
Last_Frame = Current_Frame; #update Last_Frame
bpy.ops.pose.copy() #copy pose
bpy.ops.pose.paste(flipped=True) #mirror pose
bpy.ops.anim.keyframe_insert(type='Available') #insert keyframes for mirrored pose
bpy.ops.screen.keyframe_jump(next=True) # jump to next keyframe
Current_Frame = bpy.context.scene.frame_current;print('current frame', Current_Frame) #update Current_Frame
#******************************************
#END FUNCTION
#******************************************
Without the while loop, if I type things in line by line in the console, repeating everything in the while loop manually, it works fine. But when I add the while loop, only the first two keyframes change. Anyone willing to give a hint what’s happening?
demo file attached