• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by PaLa15 · Apr 22, 2015 at 01:01 PM · animationfbxloading file

Loading Animations via C# Script directly from .fbx?

Hey Guys, as the title of this question says, i want to load the animations from my .fbx model via c# (without the UnityEditor class!) and add them to the animation component (also added per script).

 public class AnimatorController : MonoBehaviour {
     
     public string modelName;
         public string aniName;
     public AnimationClip clip;
     void Start ()
     {
         gameObject.AddComponent<Animation> ();
         anim = gameObject.GetComponent <Animation> ();
         clip = Resources.Load (modelName + ".fbx/idle", typeof(AnimationClip)) as AnimationClip;
         clip.legacy = true;
         anim.AddClip (clip, "idle");
         anim.wrapMode = WrapMode.Loop;
         anim.playAutomatically = true;
         anim.Play ("idle");
     }
 }

After that clip still is null.

This is what the .fbx looks like in the project window:

alt text

I need that script to load and animate models without creating a prefab or using the inspector.

Thanks in advance.

screenshot-1.png (8.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by soltex1 · May 01, 2016 at 07:21 AM

What I have tried:

I was trying to solve the problem but with a .blend file instead.

First of all, this:

 Object animation_clips = Resources.LoadAssetAtPath("PATH-TO-YOUR-MODEL", typeof(AnimationClip));

returned only one animation clip, so, I thought that:

 Object[] animation_clips = Resources.LoadAssetAtPath("PATH-TO-YOUR-MODEL", typeof(AnimationClip[]));

would return all, however, I was wrong, It returned an empty array.

Solution that I found out [using ModelImporter format]:

After some research, I found out this solution:

 ModelImporter model_imp = AssetImporter.GetAtPath("PATH-TO-YOUR-MODEL") as ModelImporter;

For instance, to print all the animations, you can simple do:

 foreach (var anim_clip model_imp.clipAnimations) { Debug.Log(anim.clip); }

Source: http://answers.unity3d.com/questions/674318/accessing-clipanimations-from-fbx-files.html

Afterwards problem and solution [using AnimationClip format]:

In my case, the animation clips in ModelImpoterAnimationClip format wasn't enough, because I couldn't use them as AnimationClip. So, I found out another solution to make the exact same thing, but loading the animation clips as AnimationClip format, using:

 Resources.LoadAssetAtPath("PATH-TO-YOUR-MODEL", typeof(Motion[]))
 var clips = Resources.FindObjectsOfTypeAll<AnimationClip>();

Now, you can do whatever you want, for instance print:

 foreach (var c in clips) { Debug.Log(c.name); }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Giuse86 · Apr 25, 2019 at 10:42 AM 0
Share

Hi, i'm having the same problem, but this script doesn't work... could you help me please?

avatar image Kekaku Giuse86 · Nov 09, 2020 at 06:19 AM 0
Share

Say your problem, please.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add Multiple Animations 1 Answer

Preserving FBX animations in maya? 1 Answer

unity3d - large file with animations 0 Answers

Unity FBX Legacy Animation Offset Issue 0 Answers

Playing single object animation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges