-1

As we know, fbx file can conatin animation. How to get animation of fbx file in unity during runtime?

I use this script for printing position, rotation and scale of every node of fbx. How can I get position, rotation and scale of the fbx at any point of time in its animation?

using Autodesk.Fbx;
using UnityEngine;
 
public class DisplayingAnimation : MonoBehaviour
{
    public string fbxPath;
 
    private FbxScene scene;
 
    private void Awake()
    {
        ImportFBX();
    }
 
    private void ImportFBX()
    {
        FbxManager manager = FbxManager.Create();
        FbxIOSettings ioSettings = FbxIOSettings.Create(manager, Globals.IOSROOT);
        manager.SetIOSettings(ioSettings);
 
        FbxImporter importer = FbxImporter.Create(manager, "Importer");
 
        if (importer.Initialize(fbxPath, -1, manager.GetIOSettings()))
        {
            scene = FbxScene.Create(manager, "Scene");
            importer.Import(scene);
 
            FbxNode rootNode = scene.GetRootNode();
            if (rootNode != null)
            {
                ProcessNode(rootNode);
            }
        }
 
        importer.Destroy();
        ioSettings.Destroy();
        manager.Destroy();
    }
 
    private void ProcessNode(FbxNode node)
    {
        int childCount = node.GetChildCount();
        for (int i = 0; i < childCount; i++)
        {
            FbxNode childNode = node.GetChild(i);
            PrintCoordinates(childNode);
            ProcessNode(childNode);
        }
    }
 
    private void PrintCoordinates(FbxNode node)
    {
        Vector3 translation = new Vector3((float)globalTransform.GetT().X, (float)globalTransform.GetT().Y, (float)globalTransform.GetT().Z);
        Quaternion rotation = new Quaternion((float)globalTransform.GetQ().X, (float)globalTransform.GetQ().Y, (float)globalTransform.GetQ().Z, (float)globalTransform.GetQ().W);
        Vector3 scale = new Vector3((float)globalTransform.GetS().X, (float)globalTransform.GetS().Y, (float)globalTransform.GetS().Z);
 
        Debug.Log($"Coordinates of {node.GetName()}: Translation = {translation}, Rotation = {rotation}, Scale = {scale}: parent {node.GetParent().GetName()}");
    }
}
Anonymous
  • 13
  • 4
  • depends .. how do you import the FBX file? – derHugo Jul 04 '23 at 15:34
  • I have edited the message – Anonymous Jul 04 '23 at 15:40
  • Don't know exactly how this one works but can you check in the Editor if there is any `Animator` or `Animation` component attached to your imported model? – derHugo Jul 04 '23 at 15:45
  • My advice: Don't. FBX is an interchange format, and is not suited to real time usage. You'd be better off writing an offline application that extracts the data into your own format, and then use that in the game instead. – robthebloke Jul 05 '23 at 02:53

1 Answers1

0

To access the animation data, you'll need to use the Animation component in Unity. Here's how you can modify your script to get the animation data:

using Autodesk.Fbx;
    using UnityEngine;
    
    public class DisplayingAnimation : MonoBehaviour
    {
        public string fbxPath;
    
        private FbxScene scene;
    
        private void Awake()
        {
            ImportFBX();
        }
    
        private void ImportFBX()
        {
            FbxManager manager = FbxManager.Create();
            FbxIOSettings ioSettings = FbxIOSettings.Create(manager, Globals.IOSROOT);
            manager.SetIOSettings(ioSettings);
    
            FbxImporter importer = FbxImporter.Create(manager, "Importer");
    
            if (importer.Initialize(fbxPath, -1, manager.GetIOSettings()))
            {
                scene = FbxScene.Create(manager, "Scene");
                importer.Import(scene);
    
                FbxNode rootNode = scene.GetRootNode();
                if (rootNode != null)
                {
                    // Assume you have an animation stack at index 0 (change this if you have multiple animations)
                    FbxAnimStack animStack = scene.GetSrcObject<FbxAnimStack>(0);
                    if (animStack != null)
                    {
                        // Get the start and end time of the animation
                        FbxTimeSpan animTimeSpan = animStack.GetLocalTimeSpan();
                        FbxTime startTime = animTimeSpan.GetStart();
                        FbxTime endTime = animTimeSpan.GetStop();
    
                        // Set the time step for sampling the animation
                        FbxTime timeStep = FbxTime.FromSecondDouble(1.0 / 30.0); // 30 FPS, adjust as needed
    
                        // Loop through the animation time range and sample the transformations
                        for (FbxTime currentTime = startTime; currentTime <= endTime; currentTime += timeStep)
                        {
                            scene.GetEvaluator().SetContext(scene);
    
                            // Sample the transformations at the current time
                            ProcessNode(rootNode, currentTime);
                        }
                    }
                }
            }
    
            importer.Destroy();
            ioSettings.Destroy();
            manager.Destroy();
        }
    
        private void ProcessNode(FbxNode node, FbxTime time)
        {
            FbxAMatrix globalTransform = node.EvaluateGlobalTransform(time);
            // ... Continue with the rest of your code
            // You can access the globalTransform to get the position, rotation, and scale at the current time.
            // Remember to convert the FbxAMatrix to Unity's Vector3 and Quaternion for position and rotation.
        }
    
        // Rest of your code remains the same
    }
haris.doku
  • 36
  • 4