{"id":354,"date":"2013-09-10T00:15:18","date_gmt":"2013-09-10T05:15:18","guid":{"rendered":"http:\/\/www.warsam.com\/?page_id=354"},"modified":"2023-02-18T15:59:56","modified_gmt":"2023-02-18T23:59:56","slug":"3ds-max-exporter","status":"publish","type":"page","link":"https:\/\/www.warsam.com\/?page_id=354","title":{"rendered":"3DS Max Exporter"},"content":{"rendered":"<p><!-- <iframe loading=\"lazy\" src=\"\/\/player.vimeo.com\/video\/74546075\" width=\"500\" height=\"281\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen><\/iframe> --><\/p>\n<p>I created a <span class=\"bh\">3DS Max exporter<\/span> that exports mesh and animation data including <span class=\"bh\">skinned animations<\/span>. In addition the exporter features the capability of exporting <span class=\"bh\">mesh only or animation only<\/span> data which can be specified in an XML settings file when bulk exporting. Additionally, the exporter features <span class=\"bh\">transformation decomposition<\/span> of animation data into scale, rotation and translation data. As a further optimization, the <span class=\"bh\">data is quantized<\/span> reducing each frame&#8217;s transformation data to 20 bytes.<\/p>\n<p>The exporter produces the required data format for my <a title=\"Character Animation System\" href=\"https:\/\/www.warsam.com\/?page_id=348\">Character Animation System<\/a> which leverages the aforementioned optimizations when blending transformations.<\/p>\n<h2>Source Code <\/h2>\n<p>An excerpt from the exporter: the function in which the animation data is extracted and quantized.<br \/>\n<div id=\"squelch-taas-tab-group-0\" class=\"squelch-taas-tab-group squelch-taas-override\" data-title=\"\" data-disabled=\"false\" data-collapsible=\"true\" data-active=\"0\" data-event=\"click\"><ul><\/ul><\/div>\n <br \/>\n[crayon lang=&#8221;cpp&#8221;]<br \/>\n\/*<br \/>\nfile    War3Exporter.cpp<br \/>\nauthor  Warsam Osman<br \/>\nbrief   ExportAnimations definition<br \/>\n*\/<br \/>\nvoid War3Exporter::ExportAnimations( Object&#038; theMesh, IGameNode* pNode  )<br \/>\n{<br \/>\n    int timeStep = 4800 \/ 30;<\/p>\n<p>    theMesh.m_animation.reserve( ( m_SceneEndTime &#8211; m_SceneStartTime ) \/ timeStep );<\/p>\n<p>    GMatrix toParent;<br \/>\n    toParent.SetIdentity();<\/p>\n<p>    IGameNode* parent = pNode->GetNodeParent();<\/p>\n<p>    TimeValue sceneEndTime = m_SceneEndTime;<\/p>\n<p>    if( m_exportType == MESH_ONLY )<br \/>\n    {<br \/>\n        \/\/Export only first frame<br \/>\n        sceneEndTime = m_SceneStartTime + timeStep;<br \/>\n    }<\/p>\n<p>    \/\/Quantization of transformations<br \/>\n    \/\/First loop through the animation data and get the transformation bounds<br \/>\n    AABB3 translationBounds;<br \/>\n    AABB3 scaleBounds;<br \/>\n    Vec4f rotationBoundMin;<br \/>\n    Vec4f rotationBoundMax;<br \/>\n    for( int t = m_SceneStartTime; t < sceneEndTime; t += timeStep )\n    {\n        if (parent) \n            toParent = parent->GetWorldTM(t).Inverse();<br \/>\n        Matrix44 tm = pNode->GetWorldTM(t) * toParent;<\/p>\n<p>        \/\/Extract translation<br \/>\n        Vec3f positionv =  tm.GetTranslation();<br \/>\n        translationBounds.add( positionv );<\/p>\n<p>        \/\/Extract scale<br \/>\n        \/\/Gramm Schmidt rotation to desired basis handedness<br \/>\n        Matrix44 gs = Orthonormalize( tm );<br \/>\n        Vec3f scalev = ExtractRotationScale( tm, gs );<\/p>\n<p>        scaleBounds.add( scalev );<\/p>\n<p>        \/\/Extract rotation<br \/>\n        Quaternion rotv( gs );<br \/>\n        rotv.Normalize();<\/p>\n<p>        if (rotv.w < rotationBoundMin[0]) rotationBoundMin[0] = rotv.w;\n        if (rotv.w > rotationBoundMax[0]) rotationBoundMax[0] = rotv.w;<\/p>\n<p>        if (rotv.x < rotationBoundMin[1]) rotationBoundMin[1] = rotv.x;\n        if (rotv.x > rotationBoundMax[1]) rotationBoundMax[1] = rotv.x;<\/p>\n<p>        if (rotv.y < rotationBoundMin[2]) rotationBoundMin[2] = rotv.y;\n        if (rotv.y > rotationBoundMax[2]) rotationBoundMax[2] = rotv.y;<\/p>\n<p>        if (rotv.z < rotationBoundMin[3]) rotationBoundMin[3] = rotv.z;\n        if (rotv.z > rotationBoundMax[3]) rotationBoundMax[3] = rotv.z;<br \/>\n    }<\/p>\n<p>    theMesh.scaleBounds = scaleBounds;<br \/>\n    theMesh.translationBounds = translationBounds;<br \/>\n    theMesh.rotationBoundMin = rotationBoundMin;<br \/>\n    theMesh.rotationBoundMax = rotationBoundMax;<\/p>\n<p>    const Vec3f translationDimensions = translationBounds.size();<br \/>\n    const Vec3f scaleDimensions = scaleBounds.size();<br \/>\n    const Vec4f rotationDimension = rotationBoundMax &#8211; rotationBoundMin;<\/p>\n<p>    \/\/Loop through scene again and use the transformation bounds to quantize transformations<br \/>\n    for( int t = m_SceneStartTime; t < sceneEndTime; t += timeStep )\n    {\n        Transforms trans;\n        if (parent)\n            toParent = parent->GetWorldTM(t).Inverse();<br \/>\n        Matrix44 tm = pNode->GetWorldTM(t) * toParent;<\/p>\n<p>        Vec3f transaltionVec3f, scaleVec3f;<br \/>\n        Vec4f rotationVec4f;<\/p>\n<p>        \/\/Extract translation<br \/>\n        transaltionVec3f = tm.GetTranslation();<\/p>\n<p>        \/\/Extract scale<br \/>\n        Matrix44 gs = Orthonormalize( tm );<br \/>\n        scaleVec3f = ExtractRotationScale( tm, gs );            <\/p>\n<p>        \/\/Extract rotation<br \/>\n        rotationVec4f = Quaternion( gs );<\/p>\n<p>        \/\/Quantize<br \/>\n        for( int i = 0; i < 4; ++i )\n        {\n            const float rotationInDimension = RangeMap<float,float>(<br \/>\n                rotationVec4f[i],<br \/>\n                rotationBoundMin[i],<br \/>\n                rotationBoundMax[i],<br \/>\n                0.f,<br \/>\n                rotationDimension[i]);<\/p>\n<p>            trans.rotation[i] = RangeMap<float,unsigned short>(<br \/>\n                rotationInDimension,<br \/>\n                0.f, rotationDimension[i],<br \/>\n                0, MAXWORD);<br \/>\n        }<\/p>\n<p>        for( int i = 0; i < 3; ++i )\n        {\n            const float translationInDimension = RangeMap<float,float>(<br \/>\n                transaltionVec3f[i],<br \/>\n                theMesh.translationBounds.m_min[i],<br \/>\n                theMesh.translationBounds.m_max[i],<br \/>\n                0.f, translationDimensions[i]);<\/p>\n<p>            trans.translation[i] = RangeMap<float,unsigned short>(<br \/>\n                translationInDimension,<br \/>\n                0.f, translationDimensions[i],<br \/>\n                0, MAXWORD);<\/p>\n<p>            const float scaleInDimension = RangeMap<float,float>(<br \/>\n                scaleVec3f[i],<br \/>\n                theMesh.scaleBounds.m_min[i], theMesh.scaleBounds.m_max[i],<br \/>\n                0.f, scaleDimensions[i]);<\/p>\n<p>            trans.scale[i] = RangeMap<float,unsigned short>(<br \/>\n                scaleInDimension,<br \/>\n                0.f, scaleDimensions[i],<br \/>\n                0, MAXWORD);<br \/>\n        }<\/p>\n<p>        \/\/Verify data by reverse quantization:<br \/>\n        Vec3f transTemp, scaleTemp;<br \/>\n        Vec4f rotTemp;<br \/>\n        const float invMaxWord = static_cast< float >( 1.0f \/ MAXWORD );<br \/>\n        for( int i = 0; i < 3; ++i )\n        {   \n            const float transDimOverMaxWord = translationDimensions[i]*invMaxWord;\n            const float trans16TimesDimOverWord = trans.translation[i] * transDimOverMaxWord;\n            transTemp[i] = trans16TimesDimOverWord + theMesh.translationBounds.m_min[i];\n\n            const float scaleDimOverMaxWord = scaleDimensions[i]*invMaxWord;\n            const float scale16TimesDimOverWord = trans.scale[i] * scaleDimOverMaxWord;\n            scaleTemp[i] = scale16TimesDimOverWord + theMesh.scaleBounds.m_min[i];\n        }\n\n        for( int i = 0; i < 4; ++i )\n        {   \n            const float rotDimOverMaxWord = rotationDimension[i]*invMaxWord;\n            const float rot16TimesDimOverWord = trans.rotation[i] * rotDimOverMaxWord;\n            rotTemp[i] = rot16TimesDimOverWord + theMesh.rotationBoundMin[i];\n        }\n\n        \/\/Ok if within desired percentage\n        const float MINIMUM_ACCEPTABLE_ERROR = 0.01f; \n\n        if( fabsf((transTemp - transaltionVec3f).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR )<br \/>\n            throw(&#8220;Quantized translation not within MINIMUM_ACCEPTABLE_ERROR&#8221;);<br \/>\n        if( fabsf((scaleTemp &#8211; scaleVec3f ).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR )<br \/>\n            throw(&#8220;Quantized scale not within MINIMUM_ACCEPTABLE_ERROR&#8221;);<br \/>\n        if( fabsf((rotTemp &#8211; rotationVec4f).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR )<br \/>\n            throw(&#8220;Quantized rotation not within MINIMUM_ACCEPTABLE_ERROR&#8221;);<\/p>\n<p>        theMesh.numFrames++;<br \/>\n        theMesh.m_animation.push_back( trans );<br \/>\n    }<\/p>\n<p>    if( m_exportType == MESH_ONLY ) return;<\/p>\n<p>    \/\/Optimization &#8211; collapse to single if all frames the same<br \/>\n    if( theMesh.numFrames > 1 )<br \/>\n    {<br \/>\n        bool staticAnimation = true;<\/p>\n<p>        for( size_t i = 0; i < theMesh.numFrames; ++i )\n        {\n            if( theMesh.m_animation[0] != theMesh.m_animation[i] )\n            {\n                staticAnimation = false;\n                break;\n            }\n        }\n        if( staticAnimation )\n        {\n            theMesh.m_animation.erase(theMesh.m_animation.begin()+1, theMesh.m_animation.end());\n            theMesh.numFrames = 1;\n        }\n    }\n}\n[\/crayon]\n[\/tab]\n[\/tabs]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.warsam.com\/?page_id=354\">Read more &#8250;<\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"full-width-page.php","meta":[],"_links":{"self":[{"href":"https:\/\/www.warsam.com\/index.php?rest_route=\/wp\/v2\/pages\/354"}],"collection":[{"href":"https:\/\/www.warsam.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.warsam.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.warsam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.warsam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354"}],"version-history":[{"count":17,"href":"https:\/\/www.warsam.com\/index.php?rest_route=\/wp\/v2\/pages\/354\/revisions"}],"predecessor-version":[{"id":728,"href":"https:\/\/www.warsam.com\/index.php?rest_route=\/wp\/v2\/pages\/354\/revisions\/728"}],"wp:attachment":[{"href":"https:\/\/www.warsam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}