Tuesday, 30 November 2010

Quick Walk Cycle and Python to Mirror a Cycle



I started out aiming for John Travolta's Saturday Night Fever walk to staying alive but I quickly ended up with far too many keys and too snappy and exaggerated an action for a cycle.  I realised I needed to change the rig a bit to get the movements I needed so I stripped it right down and worked up this basic walk cycle just to test the changes work.

I also wanted to work with only half a cycle and be able to have a script mirror it for me.  I coded this quick python timesaver.  Basically just select all the bones you have animation on and enter the start and end frames into the script.  Eg. You have a stride on frames 1 to 21 and you want the mirrored stride to be placed on frames 21-41 so you enter start_frame=1 end_frame=21.  Be warned the script will purge any modifiers you have on fcurves (its lazy at the moment) and replace them with a simple cycle modifier so your pair of strides (on 1-41 in the example) continue looping forever.

import bpy

start_frame=1
end_frame=21


def set_frame(sf):
    bpy.context.scene.frame_set(frame=sf)
    bpy.context.active_object.update(scene=bpy.context.scene)
    bpy.context.scene.update()


for each_fcurve in bpy.context.active_object.animation_data.action.fcurves:
    for each_modifier in each_fcurve.modifiers:
        each_fcurve.modifiers.remove(each_modifier)

each_frame=start_frame
while each_frame<=end_frame:
    set_frame(sf=each_frame)
    bpy.ops.pose.copy()
    set_frame(sf=each_frame+(end_frame-start_frame))
    bpy.ops.pose.paste(flipped=True)
    print('copying '+str(each_frame)+' to '+str(bpy.context.scene.frame_current))
    bpy.ops.anim.keyframe_insert_menu(type=-4, confirm_success=False, always_prompt=False)
    if each_frame==end_frame:
        break
    set_frame(sf=each_frame)
    bpy.ops.screen.keyframe_jump(next=True)
    each_frame=bpy.context.scene.frame_current

#kill fcurve mods and add a cycle mod
for each_fcurve in bpy.context.active_object.animation_data.action.fcurves:
    each_fcurve.modifiers.new(type='CYCLES')       

No comments:

Post a Comment