Animation/main.cxx

/****************************************************************************************

   Copyright (C) 2012 Autodesk, Inc.
   All rights reserved.

   Use of this software is subject to the terms of the Autodesk license agreement
   provided at the time of installation or download, or which otherwise accompanies
   this software in either electronic or hard copy form.

****************************************************************************************/

//
// Illustrates the use of animation stacks, layers, curvenodes and curves.
//

#include <fbxsdk.h>
#include "../Common/Common.h"

// Function prototypes.
bool CreateScene(FbxManager* pSdkManager, FbxScene* pScene);

int main(int /*argc*/, char** /*argv*/)
{
    FbxManager* lSdkManager = NULL;
    FbxScene* lScene = NULL;
    bool lResult;

    // Prepare the FBX SDK.
    InitializeSdkObjects(lSdkManager, lScene);

    // Create the scene.
    lResult = CreateScene(lSdkManager, lScene);
    if(lResult == false)
    {
        FBXSDK_printf("\n\nAn error occurred while creating the scene...\n");
        DestroySdkObjects(lSdkManager);
        return 0;
    }

    // Destroy all objects created by the FBX SDK.
    DestroySdkObjects(lSdkManager);

    return 0;
}

bool CreateScene(FbxManager* /*pSdkManager*/, FbxScene* pScene)
{
    int i;
    FbxTime lTime;
    FbxAnimCurveKey key;
    FbxAnimCurve* lCurve = NULL;

    // Create one animation stack
    FbxAnimStack* lAnimStack = FbxAnimStack::Create(pScene, "Stack001");

    // this stack animation range is limited from 0 to 1 second
    lAnimStack->LocalStop = FBXSDK_TIME_ONE_SECOND;
    lAnimStack->Description = "This is the animation stack description field.";

    // all animation stacks need, at least, one layer.
    FbxAnimLayer* lAnimLayer = FbxAnimLayer::Create(pScene, "Base Layer");  // the AnimLayer object name is "Base Layer"
    lAnimStack->AddMember(lAnimLayer);                                          // add the layer to the stack

    // Set and get the blend mode bypass of the layer
    bool val;
    lAnimLayer->SetBlendModeBypass(eFbxTypeCount, true);       // set the bypass to all the datatypes.
    val = lAnimLayer->GetBlendModeBypass(eFbxBool);           // val = true
    lAnimLayer->SetBlendModeBypass(eFbxBool, false);          // overwrite just for the bool datatype.
    val = lAnimLayer->GetBlendModeBypass(eFbxBool);           // val = false
    val = lAnimLayer->GetBlendModeBypass(eFbxChar);           // val = true
    val = lAnimLayer->GetBlendModeBypass(eFbxDateTime);        // val = true
    val = lAnimLayer->GetBlendModeBypass((EFbxType)-1);     // invalid type, val = false
    val = lAnimLayer->GetBlendModeBypass((EFbxType)120);    // invalid type (>MAX_TYPES), val = false


    // we want to animate the layer's weight property.
    FbxAnimCurveNode* wcn = lAnimLayer->CreateCurveNode(lAnimLayer->Weight);
    if (wcn)
    {
        // the curve node from the Weight property already contains 1 channel (Weight).
        i = wcn->GetChannelsCount();                            // i = 1

        // Now, let's add a second channel to the animation node. Note that this code
        // is useless and has only been provided to show the usage of the AddChannel and
        // ResetChannels
        bool ret;
        ret = wcn->AddChannel<int>("MyAddedIntChannel", 99);    // this call will succed
        i = wcn->GetChannelsCount();                            // i = 2
        ret = wcn->AddChannel<int>("MyAddedIntChannel", 10);    // this call will fail, since the channel already exists.
        i = wcn->GetChannelsCount();                            // i = 2
        wcn->ResetChannels();                                   // remove any added channels
        i = wcn->GetChannelsCount();                            // i = 1
    }

    // get the Weight curve (and create it if it does not exist, wich is the case!)
    lCurve = lAnimLayer->Weight.GetCurve<FbxAnimCurve>(lAnimLayer, true);
    if (lCurve)
    {
        // add two keys at time 0 sec and 1 sec with values 0 and 100 respectively.
        for (i = 0; i < 2; i++)
        {
            lTime.SetSecondDouble((float)i);
            key.Set(lTime, i*100.0f);
            lCurve->KeyAdd(lTime, key);
        }
    }

    //
    // now create a 3 components curvenode and animate two of the three channels.
    //
    // first, we need a "dummy" property so we can call the CreateTypedCurveNode
    FbxProperty p = FbxProperty::Create(pScene, FbxDouble3DT, "Vector3Property");
    p.Set(FbxDouble3(1.1, 2.2, 3.3));
    FbxAnimCurveNode* lCurveNode = FbxAnimCurveNode::CreateTypedCurveNode(p, pScene);

    // let's make sure the curveNode is added to the animation layer.
    lAnimLayer->AddMember(lCurveNode);

    // and to the "Vector3Property" since CreateTypedCurveNode does not make any connection
    p.ConnectSrcObject(lCurveNode);
    
    //Example of channel get value:
    //double v1 = lCurveNode->GetChannelValue<double>(0U, 0.0);   // v1 = 1.1
    //float  v2 = lCurveNode->GetChannelValue<float> (1U, 0.0f);  // v2 = 2.2
    //int    v3 = lCurveNode->GetChannelValue<int>   (2U, 0);     // v3 = 3

    //
    // create two free curves (not connected to anything)
    //

    // first curve
    lCurve = FbxAnimCurve::Create(pScene, "curve1");
    if (lCurve)
    {
        // add two keys at time 0 sec and 1 sec with values 0 and 10 respectively.
        for (i = 0; i < 2; i++)
        {
            lTime.SetSecondDouble((float)i);
            key.Set(lTime, i*10.0f);
            lCurve->KeyAdd(lTime, key);
        }
    }

    // connect it to the second channel
    lCurveNode->ConnectToChannel(lCurve, 1);

    // second curve
    lCurve = FbxAnimCurve::Create(pScene, "curve2");
    if (lCurve)
    {
        // add three keys 
        for (i = 1; i < 4; i++)
        {
            lTime.SetSecondDouble((float)i);
            key.Set(lTime, i*3.33f);
            lCurve->KeyAdd(lTime, key);
        }
    }
    // connect it to the third channel
    lCurveNode->ConnectToChannel(lCurve, "Z"); // for backward compatibility, string identifier are still 
    // allowed for the X,Y,Z and W components or "0", "1", ... "9", "A", "B", ... "F" for the Matrix44 datatype

    
    // ======================================================================
    //
    // Add a second animation layer and evaluate using the FbxAnimEvaluator
    //
    // ======================================================================
    lAnimLayer = FbxAnimLayer::Create(pScene, "Layer2"); 
    lAnimStack->AddMember(lAnimLayer);  

    // get the number of animation layers in the stack
    //int nbLayers = lAnimStack->GetMemberCount(FBX_TYPE(FbxAnimLayer));  // nblayers = 2
    lAnimLayer = lAnimStack->GetMember(FBX_TYPE(FbxAnimLayer), 1);      // get the second layer

    // set its blend mode to Additive
    lAnimLayer->BlendMode.Set(FbxAnimLayer::eBlendAdditive);

    // Now, let's animate the firrt channel of the "Vector3Property" (remember, we animated the second and
    // third on the base layer)
    // but firs, make sure the property is animatable otherwise the creation of the curveNode is prohibited.
    p.ModifyFlag(FbxPropertyAttr::eAnimatable, true);
    lCurveNode = p.GetCurveNode(lAnimLayer, true); // create it since it does not exist yet

    // use "curve2" to animate it
    lCurveNode->ConnectToChannel(lCurve, 0U);

    // and set the other two channels values
    lCurveNode->SetChannelValue<double>(1U, 5.0);
    lCurveNode->SetChannelValue<double>(2U, 0.0);

    // evaluate the "Vector3Property" value at three different times
    // with the use of the FbxAnimEvaluator so we take into account the two layers

    // make sure the evaluator is using the correct context (context == animstack)
    pScene->GetEvaluator()->SetContext(lAnimStack);
    for (i = 0; i < 3; i++)
    {
        lTime.SetSecondDouble((float)i*0.33f);
        FbxAnimCurveNode& val = pScene->GetEvaluator()->GetPropertyValue(p, lTime);

        double v[3];
        v[0] = val.GetChannelValue<double>(0U, 0.0);
        v[1] = val.GetChannelValue<double>(1U, 0.0);
        v[2] = val.GetChannelValue<double>(2U, 0.0);
    }

    /* the evaluated values for v at 0, .333 and .999 seconds are:
        
          time  |    0.0      |      0.33      |    0.99      |
       val      +-------------+----------------+--------------|
            0   |  3.33       |     3.69       |    4.05      |
            1   |  5.0        |     6.08       |    9.35      |
            2   |  0.0        |     1.098      |    2.19      |
                +-------------+----------------+--------------|
    */          
    return true;
}