FBX SDK Programmer's Guide > Animation >

# Animating a Node

Note

This section is loosely based on the CubeCreator tutorial program, located in <yourFBXSDKpath>\examples\UI Examples\CubeCreator. For more information on creating a camera, see Cameras.

Unless you are using blended animation, setting up the data structures required for animation is very straightforward. You’ll need one animation stack; one animation layer; and one animation curve node for every animated property.

Let’s say that you need to animate a camera. More specifically, you want to animate the local translation property of a camera’s FbxNode object:

    FbxScene* myScene;        // An initialized and valid scene object
    FbxNode* myCameraNode;    // An initialized and valid node object

    // Set the default local translation values (X, Y, Z) for the camera
    myCameraNode.LclTranslation.Set(fbxDouble3(1.1, 2.2, 3.3))

Although this example deals with the translation of a camera, you can animate any FBX property of any FBX object, providing that the property is animatable (see eFbxPropertyFlags::eANIMATABLE).

You always need at least one stack and one layer

Begin by creating one animation stack:

    // Define an animation stack
    FbxAnimStack* myAnimStack = FbxAnimStack::Create(pScene, "My stack");

You need to define only one animation layer:

    // Create the base layer (this is mandatory)
    FbxAnimLayer* myAnimBaseLayer = FbxAnimLayer::Create(pScene, "Layer0");

Add this layer to the animation stack:

    myAnimStack->AddMember(myAnimBaseLayer);

Curve nodes connect animation data to the animated property

CubeCreator creates one animation curve node for each axis along which the camera moves. You need one animation curve node to connect the translation data to the camera node’s LclTranslation property:

    // Get the camera’s curve node for local translation.
    // The second parameter to GetCurveNode() is "true" to ensure
    // that if the curve node does not exist, it will automatically be created.
    FbxAnimCurveNode* myAnimCurveNode = pCamera->LclTranslation.GetCurveNode(pAnimLayer, true);

Notes on animation curve nodes:

- myAnimCurveNode was automatically created with three animation channels of type double, because the type of lclTranslation is fbxDouble3. See FbxNode::LclTranslation.
- myAnimCurveNode was automatically connected to myCameraNode.LclTranslation, the local translation property of the camera that we are animating.
- myAnimCurveNode was automatically added as a member curve node to myAnimBaseLayer. Animation layers are containers for animation curve nodes.
- For another approach to creating animation curve nodes and animation curves, see the Animation sample program in <yourFBXSDKpath>\examples\Animation\

myAnimCurveNode is all set up to connect animation curves to the local translation property of myCameraNode. All that is missing are the animation curves.

Curve nodes are containers for animation curves

Here, we create the animation curve for translation on the X-axis:

    FbxAnimCurve* myTranXCurve = NULL;   // Curve for local translation on X-axis
    FbxTime myTime;                         // For the start and stop keys.
    int myKeyIndex = 0;                // Index for the keys that define the curve

    // Get the animation curve for local translation of the camera.
    // true: If the curve does not exist yet, create it.
    myTranXCurve = myCameraNode->LclTranslation.GetCurve<FbxAnimCurve>
                                      (myAnimLayer, KFCURVENODE_T_X, true);

Here, we define the curve’s starting and ending keyframes:

    // First the start keyframe
    myAnimCurve->KeyModifyBegin();
    myTime.SetSecondDouble(0.0);             // Starting time
    myKeyIndex = myAnimCurve->KeyAdd(lTime); // Add the start key; returns 0

    myAnimCurve->KeySet(lKeyIndex,           // Set the zero’th key
                        lTime,               // Starting time
                        0.0,                 // Starting X value
    FbxAnimCurveDef::eINTERPOLATION_LINEAR);// Straight line between 2 points

    // Then the stop keyframe
    lTime.SetSecondDouble(20.0);
    lKeyIndex = lCurve->KeyAdd(lTime);
    lCurve->KeySet(lKeyIndex, lTime, 500.0, FbxAnimCurveDef::eINTERPOLATION_LINEAR);
    lCurve->KeyModifyEnd();

So if you were to run this animation, the camera would take 20 seconds to move 500 units along the X-axis.

Sample programs that demonstrate animation:

- The Animation sample program in <yourFBXSDKpath>\examples\Animation\.
- The CubeCreator tutorial program in <yourFBXSDKpath>\examples\UI Examples\CubeCreator\.

#### See Also

- Animation data structures