• 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
0
Question by neoRiley · Jun 12, 2014 at 06:24 PM · animationeditoranimationssplit

How to split animations via script in editor

I have a pack of models I purchased from a vendor that have the animation list provided. The list and frame #'s are identical, but the rigging is different apparently, so I have to re-type every single one on every model.

There has to be a way to write an editor script that can "add new clip", designate "start frame" and "end frame" to automate the process, yes? (essentially doing what is spelled out here)

Has anyone dealt with this or have any ideas on how to accomplish it?

thanks very much

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

Answer by neoRiley · Jun 12, 2014 at 07:12 PM

I both love and hate asking and THEN finding. So, I ask to save time ;)

I found this script that works perfectly - you provide a text file named identically to the FBX, then reimport and bingo - all clips are created via the txt file. I'm pasting a modified version as there is one line that threw an obsolete error.

 // FbxAnimListPostprocessor.cs : Use an external text file to import a list of
 
 // splitted animations for FBX 3D models.
 //
 // Put this script in your "Assets/Editor" directory. When Importing or
 // Reimporting a FBX file, the script will search a text file with the
 // same name and the ".txt" extension.
 // File format: one line per animation clip "firstFrame-lastFrame loopFlag animationName"
 // The keyworks "loop" or "noloop" are optional.
 // Example:
 // 0-50 loop Move forward
 // 100-190 die
 
 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.IO;
 using System.Text.RegularExpressions;
 using System;
 
 public class FbxAnimListPostprocessor : AssetPostprocessor
 {
     public void  OnPreprocessModel()
     {
         if (Path.GetExtension(assetPath).ToLower() == ".fbx"
             && !assetPath.Contains("@"))
         {
             try
             {
                 // Remove 6 chars because dataPath and assetPath both contain "assets" directory
                 string fileAnim = Application.dataPath + Path.ChangeExtension(assetPath, ".txt").Substring(6);
                 StreamReader file = new StreamReader(fileAnim);
                 
                 string sAnimList = file.ReadToEnd();
                 file.Close();
                 
                 if (EditorUtility.DisplayDialog("FBX Animation Import from file",
                                                 fileAnim, "Import", "Cancel"))
                 {
                     System.Collections.ArrayList List = new ArrayList();
                     ParseAnimFile(sAnimList, ref List);
                     
                     ModelImporter modelImporter = assetImporter as ModelImporter;
                     modelImporter.clipAnimations = (ModelImporterClipAnimation[])
                         List.ToArray(typeof(ModelImporterClipAnimation));
                     
                     EditorUtility.DisplayDialog("Imported animations",
                                                 "Number of imported clips: "
                                                 + modelImporter.clipAnimations.GetLength(0).ToString(), "OK");
                 }
             }
             catch {}
             // (Exception e) { EditorUtility.DisplayDialog("Imported animations", e.Message, "OK"); }
         }
     }
     
     void ParseAnimFile(string sAnimList, ref System.Collections.ArrayList List)
     {
         Regex regexString = new Regex(" *(?<firstFrame>[0-9]+) *- *(?<lastFrame>[0-9]+) *(?<loop>(loop|noloop| )) *(?<name>[^\r^\n]*[^\r^\n^ ])",
                                       RegexOptions.Compiled | RegexOptions.ExplicitCapture);
         
         Match match = regexString.Match(sAnimList, 0);
         while (match.Success)
         {
             ModelImporterClipAnimation clip = new ModelImporterClipAnimation();
             
             if (match.Groups["firstFrame"].Success)
             {
                 clip.firstFrame = System.Convert.ToInt32(match.Groups["firstFrame"].Value, 10);
             }
             if (match.Groups["lastFrame"].Success)
             {
                 clip.lastFrame = System.Convert.ToInt32(match.Groups["lastFrame"].Value, 10);
             }
             if (match.Groups["loop"].Success)
             {
                 clip.loop = match.Groups["loop"].Value == "loop";
             }
             if (match.Groups["name"].Success)
             {
                 clip.name = match.Groups["name"].Value;
             }
             
             List.Add(clip);
             
             match = regexString.Match(sAnimList, match.Index + match.Length);
         }
     }
 }


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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

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

Related Questions

Declaring animations on a script in the editor 1 Answer

Can't script multiple animations on the player? 1 Answer

Simple Character Animations 2 Answers

save scene to separate multiple scene files 1 Answer

Alternative To "SetEditorCurve"? 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