3DS Max Exporter

I created a 3DS Max exporter that exports mesh and animation data including skinned animations. In addition the exporter features the capability of exporting mesh only or animation only data which can be specified in an XML settings file when bulk exporting. Additionally, the exporter features transformation decomposition of animation data into scale, rotation and translation data. As a further optimization, the data is quantized reducing each frame’s transformation data to 20 bytes.

The exporter produces the required data format for my Character Animation System which leverages the aforementioned optimizations when blending transformations.

Source Code

An excerpt from the exporter: the function in which the animation data is extracted and quantized.

/*
file    War3Exporter.cpp
author  Warsam Osman
brief   ExportAnimations definition
*/
void War3Exporter::ExportAnimations( Object& theMesh, IGameNode* pNode  )
{
    int timeStep = 4800 / 30;
    
    theMesh.m_animation.reserve( ( m_SceneEndTime - m_SceneStartTime ) / timeStep );

    GMatrix toParent;
    toParent.SetIdentity();

    IGameNode* parent = pNode->GetNodeParent();

    TimeValue sceneEndTime = m_SceneEndTime;
    
    if( m_exportType == MESH_ONLY )
    {
        //Export only first frame
        sceneEndTime = m_SceneStartTime + timeStep;
    }

    //Quantization of transformations
    //First loop through the animation data and get the transformation bounds
    AABB3 translationBounds;
    AABB3 scaleBounds;
    Vec4f rotationBoundMin;
    Vec4f rotationBoundMax;
    for( int t = m_SceneStartTime; t < sceneEndTime; t += timeStep )
    {
        if (parent) 
            toParent = parent->GetWorldTM(t).Inverse();
        Matrix44 tm = pNode->GetWorldTM(t) * toParent;

        //Extract translation
        Vec3f positionv =  tm.GetTranslation();
        translationBounds.add( positionv );

        //Extract scale
        //Gramm Schmidt rotation to desired basis handedness
        Matrix44 gs = Orthonormalize( tm );
        Vec3f scalev = ExtractRotationScale( tm, gs );
        
        scaleBounds.add( scalev );

        //Extract rotation
        Quaternion rotv( gs );
        rotv.Normalize();

        if (rotv.w < rotationBoundMin[0]) rotationBoundMin[0] = rotv.w;
        if (rotv.w > rotationBoundMax[0]) rotationBoundMax[0] = rotv.w;

        if (rotv.x < rotationBoundMin[1]) rotationBoundMin[1] = rotv.x;
        if (rotv.x > rotationBoundMax[1]) rotationBoundMax[1] = rotv.x;

        if (rotv.y < rotationBoundMin[2]) rotationBoundMin[2] = rotv.y;
        if (rotv.y > rotationBoundMax[2]) rotationBoundMax[2] = rotv.y;

        if (rotv.z < rotationBoundMin[3]) rotationBoundMin[3] = rotv.z;
        if (rotv.z > rotationBoundMax[3]) rotationBoundMax[3] = rotv.z;
    }

    theMesh.scaleBounds = scaleBounds;
    theMesh.translationBounds = translationBounds;
    theMesh.rotationBoundMin = rotationBoundMin;
    theMesh.rotationBoundMax = rotationBoundMax;

    const Vec3f translationDimensions = translationBounds.size();
    const Vec3f scaleDimensions = scaleBounds.size();
    const Vec4f rotationDimension = rotationBoundMax - rotationBoundMin;
    
    //Loop through scene again and use the transformation bounds to quantize transformations
    for( int t = m_SceneStartTime; t < sceneEndTime; t += timeStep )
    {
        Transforms trans;
        if (parent)
            toParent = parent->GetWorldTM(t).Inverse();
        Matrix44 tm = pNode->GetWorldTM(t) * toParent;

        Vec3f transaltionVec3f, scaleVec3f;
        Vec4f rotationVec4f;
        
        //Extract translation
        transaltionVec3f = tm.GetTranslation();
        
        //Extract scale
        Matrix44 gs = Orthonormalize( tm );
        scaleVec3f = ExtractRotationScale( tm, gs );            
        
        //Extract rotation
        rotationVec4f = Quaternion( gs );

        //Quantize
        for( int i = 0; i < 4; ++i )
        {
            const float rotationInDimension = RangeMap(
                rotationVec4f[i],
                rotationBoundMin[i],
                rotationBoundMax[i],
                0.f,
                rotationDimension[i]);

            trans.rotation[i] = RangeMap(
                rotationInDimension,
                0.f, rotationDimension[i],
                0, MAXWORD);
        }

        for( int i = 0; i < 3; ++i )
        {
            const float translationInDimension = RangeMap( 
                transaltionVec3f[i], 
                theMesh.translationBounds.m_min[i], 
                theMesh.translationBounds.m_max[i],
                0.f, translationDimensions[i]);

            trans.translation[i] = RangeMap(
                translationInDimension,
                0.f, translationDimensions[i],
                0, MAXWORD);

            const float scaleInDimension = RangeMap(
                scaleVec3f[i],
                theMesh.scaleBounds.m_min[i], theMesh.scaleBounds.m_max[i],
                0.f, scaleDimensions[i]);
            
            trans.scale[i] = RangeMap( 
                scaleInDimension, 
                0.f, scaleDimensions[i],
                0, MAXWORD);
        }

        //Verify data by reverse quantization:
        Vec3f transTemp, scaleTemp;
        Vec4f rotTemp;
        const float invMaxWord = static_cast< float >( 1.0f / MAXWORD );
        for( int i = 0; i < 3; ++i )
        {   
            const float transDimOverMaxWord = translationDimensions[i]*invMaxWord;
            const float trans16TimesDimOverWord = trans.translation[i] * transDimOverMaxWord;
            transTemp[i] = trans16TimesDimOverWord + theMesh.translationBounds.m_min[i];

            const float scaleDimOverMaxWord = scaleDimensions[i]*invMaxWord;
            const float scale16TimesDimOverWord = trans.scale[i] * scaleDimOverMaxWord;
            scaleTemp[i] = scale16TimesDimOverWord + theMesh.scaleBounds.m_min[i];
        }

        for( int i = 0; i < 4; ++i )
        {   
            const float rotDimOverMaxWord = rotationDimension[i]*invMaxWord;
            const float rot16TimesDimOverWord = trans.rotation[i] * rotDimOverMaxWord;
            rotTemp[i] = rot16TimesDimOverWord + theMesh.rotationBoundMin[i];
        }

        //Ok if within desired percentage
        const float MINIMUM_ACCEPTABLE_ERROR = 0.01f; 

        if( fabsf((transTemp - transaltionVec3f).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR )
            throw("Quantized translation not within MINIMUM_ACCEPTABLE_ERROR");
        if( fabsf((scaleTemp - scaleVec3f ).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR )
            throw("Quantized scale not within MINIMUM_ACCEPTABLE_ERROR");
        if( fabsf((rotTemp - rotationVec4f).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR )
            throw("Quantized rotation not within MINIMUM_ACCEPTABLE_ERROR");

        theMesh.numFrames++;
        theMesh.m_animation.push_back( trans );
    }

    if( m_exportType == MESH_ONLY ) return;

    //Optimization - collapse to single if all frames the same
    if( theMesh.numFrames > 1 )
    {
        bool staticAnimation = true;
        
        for( size_t i = 0; i < theMesh.numFrames; ++i )
        {
            if( theMesh.m_animation[0] != theMesh.m_animation[i] )
            {
                staticAnimation = false;
                break;
            }
        }
        if( staticAnimation )
        {
            theMesh.m_animation.erase(theMesh.m_animation.begin()+1, theMesh.m_animation.end());
            theMesh.numFrames = 1;
        }
    }
}