Accessing Animation Curve Data [MEL]


 

 

Animation curves in maya can be one of a number of types, dependent on what data type is to be animated. The 8 animation curve types can be summed up in the following table :

 

Type Input Type Output Type
MFn::kAnimCurveTL time distance
MFn::kAnimCurveTA time angle
MFn::kAnimCurveTT time time
MFn::kAnimCurveTU time double
MFn::kAnimCurveUL double distance
MFn::kAnimCurveUA double angle
MFn::kAnimCurveUT double time
MFn::kAnimCurveUU double double

 

As far as i am aware, maya only makes these distinctions so that it knows when to perform unit conversions (ie, frames to seconds, meters to inches etc). All of these curve types can be accessed using exactly the same code, so i will leave it up to the reader to decide if that information is useful.

 

All animation data within maya is stored as animation curves. Now, I have a distinct belief, that when writing exporters, it is better to sample the data per-frame, rather than export actual animation curves. The main reason is that most rigs will be driven by IK chains, set driven keys and expressions that the TD has lovingly set up. If you use the animation curves directly, then expect to be implementing the rest of those features in your game engine as well!

 

I would therefore advise that animation curves should be used sparingly, only in places where you are sure that no external connections will be driving the values. One example may be blend shape weights, or per vertex tweaks on lattices. Generally then it is better to check an attribute to see if an animation curve is attached, and then export the curve. That way you will not accumulate curves that have no meaning to you in your data files.

 

 

 

 

 

Finding all Animation Curves
in a Scene

This is not a recommended way of getting access to the animation curves. Although it will access all of the anim curves within the scene, there may be a number of curves connected to nodes, that you are not exporting.

Exporting animation data for those nodes is *not* going to be a good idea.

 

 


#include<maya/MFnMesh.h>
#include<maya/MItDependencyNodes.h>

// create an iterator to go through all meshes
MItDependencyNodes it(MFn::kAnimCurve);

while(!it.isDone())
{
 

// output each curve we find
OutputAnimCurve(it.item());

// get next mesh
it.next();

}



 

Accessing an animation curve via an attribute.

A better method of accessing the curve data, is to therefore query curves attached to nodes that we are exporting. Often it is sensible to only export curves, which are attached to attributes that we know we are exporting.

 

The function on the right takes an MObject for a node and the name of the attribute you want to check.

 

It then checks to see if there are any connections on the attribute that are of type MFn::kAnimCurve. If it finds an animations curve then it will export it.

 

 

 

 


#include<maya/MFnMesh.h>

bool AnimCurveFromAttr(MObject& node,const char* attrname)
{
 

// attach the function set to the object
MFnDependencyNode
fn(node);

// get the plug to the requested attribute
MPlug plug = fn.findPlug(attrname,&stat);

if(stat != MS::kSuccess) return false;

// use the function set to get the points

MPlugArray Connections;

// get an array of connections to this attribute
plug.connectedTo( Connections, true, false, 0 );

// loop through all connections. We are looking for
// any attached animation curves

for(int i=0;i!=Connections.length();++i) {

 

// get a reference to the node
MObject connected = Connections[i].node();

// if it's an animation curve output it
if
(connected.hasFn(MFn::kAnimCurve)) {

  OutputAnimCurve(connected);

}

}

}


 

 

Outputting the animation curve

 

The function set, MFnAnimCurve, allows you to access animation curve data. We simply bind the function set to the MObject for the animation curve, and can then loop through each of the key frames.

 

Maya uses hermite curves to interpolate the animation curves, so you will recieve back tangents as well as time and the key value.

 

 

 

 

 

 

 

 

 

 

 


#include <maya/MFnAnimCurve.h>

void OutputAnimCurve (MObject &obj)
{

 

MFnAnimCurve fn(obj);

unsigned int iKeyCount = fn.numKeys();

// dont bother if its a pointless curve....
if(iKeyCount == 0) return;

cout << "AnimCurve " << fn.name().asChar() << endl;
cout << "NumKeys " << iKeyCount << endl;

// get all keyframe times & values
for(unsigned int i=0;i<iKeyCount;i++)
{

 

MTime Time = fnAnimCurve.time(i).value();
float Value = fnAnimCurve.value(i);

// now i'm not too sure about this, so I will
// state something that may be untrue. When
// getting the tangents to the animation curves
// you can pretty much ignore the x tangent
// values because time is generally a linear
// value. (am i right here?) If I am wrong then
// write the x values as well.

float
ix,iy,ox,oy;
fnAnimCurve.getTangent(i,ix,iy,true);
fnAnimCurve.getTangent(i,ox,oy,false);

// write keyframe info
cout
<< " time " << Time.as(MTime::kSeconds);
cout << " value " << Value;
cout << " InTangent " << iy;
cout << " OutTangent " << oy << endl;

}

}




 

 

What Next?

Material Data

Transformation Data

Blend Shape Deformers

Soft Skinned Surfaces

Rigid Skinned Surfaces

Lattice Deformers

index

Rob Bateman [2004]


[HOME] [MEL] [API]