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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by netoelgrande · Oct 21, 2014 at 08:15 PM · animatorunityeditorfbxclip

Extract animations from FBX Model per script

Hi. I´ve been looking around in the answers and forums for a way to automate the animation process in my Unity projects and this is where I have found no more answers. So far I can get the animation from the fbx model using AssetDatabase and then I assign it using an OverrideAnimationController. The problem is that this will only work in the Editor and I would like to save the clip from the fbx in the resources folder so I can access it per script in any build. Apparently the way to do this is with this code:

 [ExecuteInEditMode]
 /// <summary>
 /// This class assigns all the scripts, colliders, meshes, etc, neccessary for the subsegment of a level.
 /// It works with the editor so the folder hierarchy of the level models, the animator controller and the prefabs has to be conserved
 /// </summary>
 public class Assigner : MonoBehaviour 
 {
     void Update () 
     {
         //Extract the animations from the fbxs and place the in resources to be
         //used for overrideAnimation        
         AnimationClip orgClip = (AnimationClip)AssetDatabase.LoadAssetAtPath(
             "Assets/Resources/AssigningKit/L_Design/"+transform.name+".fbx",typeof(AnimationClip) );
         SerializedObject serializedClip = new SerializedObject(orgClip);
         AnimationClipSettings2 clipSettings = new AnimationClipSettings2(
             serializedClip.FindProperty("m_AnimationClipSettings"));
         clipSettings.loopTime = true;
         serializedClip.ApplyModifiedProperties();
         AnimationClip placeClip = new AnimationClip();    
         placeClip.name=transform.name;
         //Save the animation 
         if( !Resources.Load("AssigningKit/Animation/"+transform.name+"Anim") ){
             AssetDatabase.CreateAsset(placeClip,"Assets/Resources/AssigningKit" +
                 "/Animation/"+transform.name+".anim");
             AssetDatabase.Refresh();
             //Copy Curves
             AnimationClip copyAnim = (AnimationClip)AssetDatabase.LoadAssetAtPath(
                 "Assets/Resources/AssigningKit/Animation/"
                 +transform.name+".anim",typeof(AnimationClip) );
             AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(
                 orgClip, true);
             for (int i = 0; i < curveDatas.Length; i++)
             {
                 AnimationUtility.SetEditorCurve(
                     copyAnim,
                     EditorCurveBinding.FloatCurve(curveDatas[i].path
                                               , curveDatas[i].type
                                               , curveDatas[i].propertyName),
                     curveDatas[i].curve
                     );
             }
             AssetDatabase.Refresh();
             
             SerializedObject serializedClip2 = new SerializedObject(copyAnim);
             AnimationClipSettings2 clipSettings2 = new AnimationClipSettings2(
                 serializedClip.FindProperty("m_AnimationClipSettings"));
             clipSettings.loopTime = true;
             serializedClip.ApplyModifiedProperties();
         }
 }
 }

The problem with this code is that, while it creates the clip it isn't assignable to an Animator Controller. Nevertheless, if I duplicate the clip in the editor it works like it should. It also looks way different in the Inspector. The duplicated version lets me bake rotation, position and such, while the script created version only lets me choose the wrap mode. Much like an animation created with the Animation Window, the only difference being that the animation created with the Animation Window can be assigned to an Animation Controller. So any help would be appreciated in showing how to create a clip per script that can be assigned to the Controller.

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
Best Answer

Answer by netoelgrande · Oct 24, 2014 at 01:12 AM

Ok I figured it out... There exists in the EditorUltility namespace that copies any unity object via serialization. So this copies everything no only the curves. The modified code is now.

 //Extract the animations from the fbxs and place the in resources to be
         //used for overrideAnimation        
         AnimationClip orgClip = (AnimationClip)AssetDatabase.LoadAssetAtPath(
             "Assets/Resources/AssigningKit/L_Design/"+transform.name+".fbx",typeof(AnimationClip) );
         SerializedObject serializedClip = new SerializedObject(orgClip);
         AnimationClipSettings2 clipSettings = new AnimationClipSettings2(
             serializedClip.FindProperty("m_AnimationClipSettings"));
         clipSettings.loopTime = true;
         serializedClip.ApplyModifiedProperties();
         //Save the clip
         AnimationClip placeClip = new AnimationClip();
         if( !Resources.Load("AssigningKit/Animation/"+transform.name+".anim") ){
             EditorUtility.CopySerialized(orgClip,placeClip);
             AssetDatabase.CreateAsset(placeClip,"Assets/Resources/AssigningKit" +
                 "/Animation/"+transform.name+".anim");
             AssetDatabase.Refresh();

Comment
Add comment · Show 1 · 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 jctz · May 14, 2015 at 06:38 PM 0
Share

I need to do something like this in order to build AnimOverridecontrollers in the editor at build time. Unfotunately our anim FBXs consist of multiple takes/clips in one FBX file. How would I load these individual clips from within a single FBX file?

For example our

ANI$$anonymous$$_FIR$$anonymous$$fbx file has the following takes/clips: ANI$$anonymous$$_FIRE_BEGIN, ANI$$anonymous$$_FIRE_LOOP, ANI$$anonymous$$_FIRE_END

avatar image
1

Answer by iperov · Jul 01, 2018 at 12:57 PM

just instantiate clip and then you can use CreateAsset

 AnimationClip new_clip = UnityEngine.Object.Instantiate(clip);
 new_clip.name = "asd";
 new_clip.frameRate = 14;
                                         
 AssetDatabase.CreateAsset(new_clip, "..." );
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

play different clips via GetKeyDown in unity 0 Answers

Mecanim x and z plane root motion not working when importing .fbx, but works with .blend 2 Answers

import mocap animations ? 0 Answers

Load Animator From XML without using UnityEditor 0 Answers

How to import animations from one model to others 0 Answers


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