Export animation (actions) with command line script

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

KTC
Posts: 11
Joined: Sun Jan 08, 2012 11:49 pm

Export animation (actions) with command line script

Postby KTC » Tue May 29, 2012 2:17 am

I'm trying to figure out a way to export animation keyframes for each individual action in a scene. My current method is to find each keyframe for each action, set the pose to that key, and export the current pose bone transforms. It doesn't work though. I can't get the pose bones to change when I change the frame.

I have searched the forums and found solutions, but they don't work for me. I think it's probably because I'm running the script from the command line rather than in an existing instance of Blender.

Here's my code so far:

Code: Select all

    for action in bpy.data.actions:
        ob.animation_data.action = action
        bpy.ops.object.mode_set(mode="POSE")
        bpy.ops.pose.select_all(action="SELECT")
        bpy.ops.pose.transforms_clear()
       
        # get all frame numbers in this action
        frames = dict()
        for fcurve in action.fcurves:
            for key in fcurve.keyframe_points:
                frame = key.co.x
                frames[frame] = 1
       
        for f in sorted(frames):
            bpy.context.scene.frame_set(f)
           
            # (read pose bone data here)

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue May 29, 2012 3:16 am

you should use print() for some debug output, to see what values your variables hold, maybe there's a problem

run Blender via cmd to see the print output
I'm sitting, waiting, wishing, building Blender in superstition...

KTC
Posts: 11
Joined: Sun Jan 08, 2012 11:49 pm

Postby KTC » Tue May 29, 2012 6:20 am

Mmhmm, I did that. It is able to get all the correct pose bones, actions, and frame numbers. It's just that setting the frame with bpy.context.scene.frame_set has no effect on the pose bones.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue May 29, 2012 7:51 pm

that's weird...

you may try

bpy.context.scene.update()

after changing frame
I'm sitting, waiting, wishing, building Blender in superstition...

KTC
Posts: 11
Joined: Sun Jan 08, 2012 11:49 pm

Postby KTC » Wed May 30, 2012 2:26 am

I just gave it a try. Didn't work. :(

KTC
Posts: 11
Joined: Sun Jan 08, 2012 11:49 pm

Postby KTC » Thu May 31, 2012 5:21 am

Solved. My previous code works. The problem was that matrix_basis, rotation_euler, rotation_quaternion, and rotation_axis_angle in PoseBone are always set to the identity transform. (Why would they do that?!) So, instead, I have to use this to get the transform for each frame:

Code: Select all

def getPoseBoneLocalMatrix(poseBone):
    if poseBone.parent is None:
        return poseBone.matrix
    else:
        return poseBone.parent.matrix.inverted() * poseBone.matrix


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests