How to export FBX at runtime with animation(s), rig and the mesh to Maya, blender etc.. ?

Hello everyone !

What I want to do is basically to export FBX at runtime with animation, rig and mesh to maya (or similar tools) and view the export in 3D app with all the features exported…

Looking forward for your answers and suggestions !

Here’s a working code snippet that should at least be a good starting point:

private static void ExportFbxScene(string filename) {
    var manager = FbxManager.Create();
    manager.SetIOSettings(FbxIOSettings.Create(manager, Globals.IOSROOT));
    var exporter = FbxExporter.Create(manager, "exporter");
    exporter.Initialize(filename, /*FileFormat=*/-1, manager.GetIOSettings());
    var scene = FbxScene.Create(manager, "scene");
    AddSkeletonTo(ref scene);
    exporter.Export(scene);
}

private static void AddSkeletonTo(ref FbxScene scene) {
    var rootObj = FbxNode.Create(scene, "root-obj");
    var bones = new List<FbxNode>();
    var boneNames = new[]  { "root-bone", "child-bone" };
    scene.GetRootNode().AddChild(rootObj);

    for (var i = 0; i < boneNames.Length; i++) {
        // create the bone object
        var skeletonAttribute = FbxSkeleton.Create(scene, boneNames*);*

skeletonAttribute.SetSkeletonType(i == 0 ?
FbxSkeleton.EType.eRoot : FbxSkeleton.EType.eLimbNode);
var bone = FbxNode.Create(scene, boneNames*);*
bone.SetNodeAttribute(skeletonAttribute);
// set the bone’s transform
bone.LclTranslation.Set(new FbxDouble3(0, -5, 0)); // x, -y, z
bone.LclRotation.Set(new FbxDouble3(0, -0, -0)); // x, -y, -z
bone.LclScaling.Set(new FbxDouble3(1, 1, 1)); // x, y, z
// add to parent bone
if (i > 0) bones[i - 1].AddChild(bone); // link: A → B → C → …
// add to local bone list for next iteration
bones.Add(bone);
}

var pose = FbxPose.Create(scene, boneNames[0]);
pose.SetIsBindPose(true); // default is rest pose
foreach (var bone in boneNodes) {
var bindMatrix = bone.EvaluateGlobalTransform();
pose.Add(bone, new FbxMatrix(bindMatrix));
}
scene.AddPose(pose);
}
Sources I used:
- [Unity FBX Developer’s Guide][1]
- [Import FBX Animations Using C][2]
- [Autodesk FBX SDK API Reference][3]
- [Snippets from various online repos][4]
_[1]: https://docs.unity3d.com/Packages/com.unity.formats.fbx@2.0/manual/devguide.html*_
_
[2]: https://yejunny.wordpress.com/2020/02/24/import-fbx-animation-using-c/*_
[3]: Help
_*[4]: https://cpp.hotexamples.com/examples/-/FbxSkeleton/-/cpp-fbxskeleton-class-examples.html*_

Instead of writing your own exporter, you should use Unity’s “built-in” FBX exporter (which is easily installed through a package using package manager. There’s also free code from Unity for the old FBX Exporter (but the new package-version is better (more updated).

*The FBX exporter also exports animation, but unfortunately, no one knows how to get the exporter to properly export (and apply) the animation to the model, even Unity staff don’t know since people have been asking them how for years now.