Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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

2 Replies

· 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.

avatar image
0

Answer by wdr434-domowe · May 19, 2021 at 06:10 PM

I had issue because path to model was wrong. Try this solution:

 string path = Application.dataPath + "/Resources/<model_name>.fbx";
 var assetPath = path.Replace(Application.dataPath, "Assets");
 
 ModelImporter MI = (ModelImporter)AssetImporter.GetAtPath(assetPath);
 ModelImporterClipAnimation[] clips = MI.defaultClipAnimations;
 
 foreach(var clip in clips) { Debug.Log(clip.name); }
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FBX Importing Problem 1 Answer

Animation: Are multiple bindposes in a rig supported? 1 Answer

Problem in Game mode with animation 0 Answers

What is the best way to modify the Car Tutorial for more cars? 2 Answers

Trying to use character animation from poser, without any success? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges