FBX SDK Programmer's Guide > Animation > Example >

# Animating a Node

Setting up the data structures for animation is simple if you are not using blended animation. You need at least one animation stack, one animation layer, and one animation curve node for every FBX property.

This example shows how 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 shows how to animate the translation property of a camera, you can animate any FBX property of any FBX object, if the FBX property is animatable. See eFbxPropertyFlags::eANIMATABLE for more information.

Follow the instructions in these sections to animate the local translation property of the camera.

## Start by creating the Animation stack and Animation Layer

Perform the following steps:

1. Create one animation stack.

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

2. Create one animation layer.

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

3. Add the base layer to the animation stack.

       myAnimStack->AddMember(myAnimBaseLayer);

After adding the base layer to the animation stack, you can create the animation curve nodes to connect the animation data to the FBX property.

## Create the Animation Curve Nodes

The CubeCreator tutorial program creates one animation curve node for each axis along which the camera moves.

Create 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:

- myAnimCurveNode is automatically created with three animation channels of type double because the type of lclTranslation is fbxDouble3. See FbxNode::LclTranslation.
- myAnimCurveNode is automatically connected to myCameraNode.LclTranslation, which is the local translation property of the camera.
- myAnimCurveNode is automatically added as a member curve node to myAnimBaseLayer. Animation layers are containers for animation curve nodes.

For another approach to create animation curve nodes and animation curves, see the animation sample program in <yourFBXSDKpath>\samples\Animation\.

After the animation curve node (myAnimCurveNode) is set up to connect the animation curves to the local translation property of the camera (myCameraNode), you can create the animation curves.

## Create the Animation curves

Perform the following steps:

1. Create the animation curve for translation of the camera 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(myAnimLayer, KFCURVENODE_T_X, true);

2. Define the starting and ending keyframes of the animation curve.

       // 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();

If you run this animation, the camera takes 20 seconds to move 500 units along the X-axis.

You can refer to these sample programs that demonstrate animation:

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

#### See Also

- Animation Data Structures
- Cameras