4
$\begingroup$

I have an animation consisting of translation in X and Y and rotation along the Z axis.

Animation curves

Each of these Bézier curves consist of points, such as the black points in the picture above. Each of these curves also have two control points that define the behavior of the curve, as seen in the orange circles around the selected points. There is one to the left and one to the right. If you select a point in the Graph Editor, the Properties panel displays the coordinates of the keyframe point and its left and right handles.

Point properties

I am building an exteral game that has its own Bézier animation interpolation. I need to export an array of the values defining each keyframe for each axis of transformation including the Key, Left Handle, and Right Handle X and Y values. For example, the curves shown above look like this when exported by hand:

x: [
    [{x: -3.904, y: -65.564003}, {x: 0.0, y: -65.564003}, {x: 3.904, y: -65.564003}],
    [{x: 6.0959997, y: -65.5639954}, {x: 10.0, y: -52.0340004}, {x: 13.9040012, y: -38.5040054}],
    [{x: 16.0958843, y: -18.3652306}, {x: 20.0, y: 9.9868374}, {x: 23.9041157, y: 38.3389053}],
    [{x: 26.0958843, y: 68.4369736}, {x: 30.0, y: 93.2079773}, {x: 31.9520569, y: 105.5934753}],
    [{x: 33.0479431, y: 115.0458527}, {x: 35.0, y: 115.0458527}, {x: 36.9520569, y: 115.0458527}],
    [{x: 38.0479431, y: 76.4329605}, {x: 40.0, y: 62.4304733}, {x: 43.9041138, y: 34.4255066}],
    [{x: 46.0958862, y: 37.9866829}, {x: 50.0, y: 24.1973839}, {x: 53.9041138, y: 10.4080839}],
    [{x: 56.0958862, y: 5.8076649}, {x: 60.0, y: -8.2093506}, {x: 63.9041138, y: -22.226366}],
    [{x: 66.0958862, y: -36.7316742}, {x: 70.0, y: -47.6089821}, {x: 74.2945251, y: -59.5740204}],
    [{x: 76.7054749, y: -65.5638962}, {x: 81.0, y: -65.5638962}, {x: 85.2945251, y: -65.5638962}]
],
y: [
    [{x: -3.904115, y: 523.7230835}, {x: 0.0, y: 523.7230835}, {x: 3.904115, y: 523.7230835}],
    [{x: 6.0958853, y: 568.0751343}, {x: 10.0, y: 595.0602417}, {x: 13.9041147, y: 622.0453491}],
    [{x: 16.0958843, y: 661.9623413}, {x: 20.0, y: 661.9623413}, {x: 23.9041157, y: 661.9623413}],
    [{x: 26.0958843, y: 628.7947998}, {x: 30.0, y: 597.6083374}, {x: 31.9520569, y: 582.0151367}],
    [{x: 33.0479431, y: 549.9043579}, {x: 35.0, y: 549.9043579}, {x: 36.9520569, y: 549.9043579}],
    [{x: 38.0479431, y: 559.7282715}, {x: 40.0, y: 562.875}, {x: 43.9041138, y: 569.168457}],
    [{x: 46.0958862, y: 569.1739502}, {x: 50.0, y: 569.1739502}, {x: 53.9041138, y: 569.1739502}],
    [{x: 56.0958862, y: 559.8475952}, {x: 60.0, y: 557.9870605}, {x: 63.9041138, y: 556.1265259}],
    [{x: 66.0958862, y: 557.9870605}, {x: 70.0, y: 556.1265259}, {x: 74.2945251, y: 554.0799561}],
    [{x: 76.7054749, y: 523.7230835}, {x: 81.0, y: 523.7230835}, {x: 85.2945251, y: 523.7230835}]
],
r: [
    [{x: -3.904115, y: -69.0623513}, {x: 0.0, y: -69.0623513}, {x: 3.904115, y: -69.0623513}],
    [{x: 6.0958853, y: -53.1340769}, {x: 10.0, y: -40.8093182}, {x: 13.9041147, y: -28.4845594}],
    [{x: 16.0958843, y: -16.8711888}, {x: 20.0, y: -5.9250832}, {x: 23.9041157, y: 5.0210223}],
    [{x: 26.0958843, y: 15.2653908}, {x: 30.0, y: 15.2653908}, {x: 33.9041138, y: 15.2653908}],
    [{x: 36.0958862, y: 6.9039369}, {x: 40.0, y: 1.5133458}, {x: 43.9041138, y: -3.8772452}],
    [{x: 46.0958862, y: -6.205909}, {x: 50.0, y: -12.3495301}, {x: 53.9041138, y: -18.4931504}],
    [{x: 56.0958862, y: -22.0891887}, {x: 60.0, y: -29.9591973}, {x: 63.9041138, y: -37.8292076}],
    [{x: 66.0958862, y: -45.3238171}, {x: 70.0, y: -52.6660109}, {x: 74.2945251, y: -60.7424227}],
    [{x: 76.7054749, y: -69.0623513}, {x: 81.0, y: -69.0623513}, {x: 85.2945251, y: -69.0623513}]
]

I need a script that can export these values from each keyframe of each axis of translation/rotation in the selected object into a textual format, preferably in the same format as shown above.

$\endgroup$

1 Answer 1

4
$\begingroup$

The coordinate and handles (co, handle_left, handle_right) can be read / set from the KeyFrame keyframe point.

Test code, runs on action on active object. Prints the fcurve name and each co and handle.

import bpy

context = bpy.context
obj = context.object
action =  context.object.animation_data.action
#print("fcurve_dic = {")
for fcurve in action.fcurves:

    print("'%s[%d]' : [" % (fcurve.data_path, fcurve.array_index))
    for kfp in fcurve.keyframe_points:
        #print("\t{", end="")
        vals = []
        for prop in ["co", "handle_left", "handle_right"]:
            co = getattr(kfp, prop)
            vals.append("{'x':%.4f, 'y':%.4f}" %  co[:])
        print("\t[%s]" % ", ".join(vals))
    print("\t]")

#print("}")
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .