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 AlkisFortuneFish · Jan 16, 2014 at 07:58 PM · editormecanimevents.fbxmodelimporter

Setting ModelImporter's clipAnimations clears events for all referenced clips, is this intentional?

I have written a wizard to set the root transform settings for muscle clips on multiple model assets, since the AnimationClipEditor embedded in the ModelImporter does not support multiselection. It works, but as soon as the clipAnimations array is set, all clips lose their events. I presume that is caused by the clips being passed by copy but I have not found any event setters at that ModelImport level being exposed in the API. It seems to be handled by AnimationClipInfoProperties, internally.

I have worked around the issue with a nasty hack involving reading the event properties from the meta file, making the changes, reinserting the lost events to the clips straight on the meta file and re-importing.

I have been wondering, have I missed some function somewhere, have I just hit the need for functionality that has not been made public yet or is the lost event association a bug I should report?

Interestingly, events are also lost after using the Copy From Other Mask option in the importer, which I also solved the same way as above.

Thanks!

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

Answer by AlkisFortuneFish · Feb 05, 2014 at 12:33 PM

So, I've found a less hacky but still rather undocumented way of doing this, using serialized properties instead of messing with textual meta files. It is basically lifted out of Unity's private AnimationClipInfoProperties class and I've successfully used it both to copy events from one animation to another and to make changes to clipAnimations via script without losing all the events.

 ModelImporter modelImporter = (ModelImporter) setImporter.GetAtPath(AssetDatabase.GetAssetPath(TargetObject));
 
 SerializedObject so = new SerializedObject(modelImporter);
 
 SerializedProperty clips = so.FindProperty("m_ClipAnimations");
 List<AnimationEvent[]> animationEvents = new List<AnimationEvent[]>(modelImporter.clipAnimations.Length);
 
 for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
 {
     animationEvents.Add(GetEvents(clips.GetArrayElementAtIndex (i)));
 }
 
 // Make your changes and write them, destroying the events.
 
 for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
 {
     SetEvents(clips.GetArrayElementAtIndex(i), animationEvents[i]);
 }
 
 so.ApplyModifiedProperties();

Extra needed methods:

 public AnimationEvent[] GetEvents (SerializedProperty sp)
 {
     SerializedProperty serializedProperty = sp.FindPropertyRelative("events");
     AnimationEvent[] array = null;
     
     if (serializedProperty != null && serializedProperty.isArray)
     {
         int count = serializedProperty.arraySize;
         array = new AnimationEvent[count];
         
         for (int i = 0; i < count; i++)
         {
             AnimationEvent animationEvent = new AnimationEvent();
             
             SerializedProperty eventProperty = serializedProperty.GetArrayElementAtIndex (i);
             animationEvent.floatParameter = eventProperty.FindPropertyRelative ("floatParameter").floatValue;
             animationEvent.functionName = eventProperty.FindPropertyRelative ("functionName").stringValue;
             animationEvent.intParameter = eventProperty.FindPropertyRelative ("intParameter").intValue;
             animationEvent.objectReferenceParameter = eventProperty.FindPropertyRelative ("objectReferenceParameter").objectReferenceValue;
             animationEvent.stringParameter = eventProperty.FindPropertyRelative ("data").stringValue;
             animationEvent.time     = eventProperty.FindPropertyRelative ("time").floatValue;
             array [i] = animationEvent;
         }
     }
     return array;
 }
 
 public void SetEvents (SerializedProperty sp, AnimationEvent[] newEvents)
 {
     SerializedProperty serializedProperty = sp.FindPropertyRelative("events");
     if (serializedProperty != null && serializedProperty.isArray && newEvents != null && newEvents.Length > 0)
     {
         serializedProperty.ClearArray ();
         for (int i = 0; i < newEvents.Length; i++)
         {
             AnimationEvent animationEvent = newEvents [i];
             serializedProperty.InsertArrayElementAtIndex (serializedProperty.arraySize);
             
             SerializedProperty eventProperty = serializedProperty.GetArrayElementAtIndex (i);
             eventProperty.FindPropertyRelative ("floatParameter").floatValue = animationEvent.floatParameter;
             eventProperty.FindPropertyRelative ("functionName").stringValue = animationEvent.functionName;
             eventProperty.FindPropertyRelative ("intParameter").intValue = animationEvent.intParameter;
             eventProperty.FindPropertyRelative ("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;
             eventProperty.FindPropertyRelative ("data").stringValue = animationEvent.stringParameter;
             eventProperty.FindPropertyRelative ("time").floatValue = animationEvent.time;
         }
     }
 }
Comment
Add comment · Show 3 · 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 Immanuel-Scholz · Jun 26, 2014 at 07:34 AM 0
Share

That is very interesting. Can you fix your code posting, please? Especially how did you get the SerializedProperty "clips" out of the $$anonymous$$odelImporter?

avatar image Immanuel-Scholz · Jun 26, 2014 at 08:01 AM 0
Share

It seems to be var clips = new SerializedObject(modelImporter).FindProperty("m_clipAnimations");

avatar image AlkisFortuneFish · Jun 26, 2014 at 12:42 PM 0
Share

Ugh, that was odd, I swear that was fine when I posted it, the parser was somehow eating everything between the start of the pre tag and the AnimationEvents line! Fixed.

avatar image
1

Answer by Immanuel-Scholz · Jun 26, 2014 at 08:33 AM

For completeness, here is a reformatted but complete piece of code to illustrate how to fix Unity's "throw away all Animation Events when assigning clips/masks" - bug. All credit goes to AlkisFortuneFish, I just completed the missing pieces and applied my personal coding stlye :P

 using System;
 using System.Linq;
 using System.Collections;
 using System.Collections.Generic;
 using Object = UnityEngine.Object;
 using UnityEngine;
 using UnityEditor;
 using UnityEditorInternal;
 
 public class MyAssetPostProcessor : AssetPostprocessor
 {
     public void OnPreprocessModel()
     {
         ModelImporter modelImporter = (ModelImporter)this.assetImporter;
     
         // either assigning clipAnimations or bodyMasks can kill all animation events, so save/restore them
         var animationEvents = new List<AnimationEvent[]>(modelImporter.clipAnimations.Length);
         var clips = new SerializedObject(modelImporter).FindProperty("m_ClipAnimations");
         for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
         {
             var ev = clips.GetArrayElementAtIndex(i).FindPropertyRelative("events");
             AnimationEvent[] array = new AnimationEvent[ev.arraySize];
             for (int j = 0; j < array.Length; j++)
             {
                 var p = ev.GetArrayElementAtIndex(j);
                 array[j] = new AnimationEvent
                 {
                     functionName = p.FindPropertyRelative("functionName").stringValue,
                     intParameter = p.FindPropertyRelative("intParameter").intValue,
                     floatParameter = p.FindPropertyRelative("floatParameter").floatValue,
                     objectReferenceParameter = p.FindPropertyRelative("objectReferenceParameter").objectReferenceValue,
                     stringParameter = p.FindPropertyRelative("data").stringValue,
                     time = p.FindPropertyRelative("time").floatValue,
                     messageOptions = (SendMessageOptions)Enum.GetValues(typeof(SendMessageOptions)).GetValue(p.FindPropertyRelative("messageOptions").intValue)
                 };
             }
             animationEvents.Add(array);
         }
 
         // here comes all the harmfull stuff
         DoSomethingThatAssignsClipAnimations(modelImporter);
 
         // restore events
         clips.serializedObject.Update();
         for (int i = 0; i < Math.Min(modelImporter.clipAnimations.Length, animationEvents.Count); i++)
         {
             var ev = clips.GetArrayElementAtIndex(i).FindPropertyRelative("events");
             ev.arraySize = animationEvents[i].Length;
             for (int j = 0; j < animationEvents[i].Length; j++)
             {
                 var p = ev.GetArrayElementAtIndex(j);
                 var e = animationEvents[i][j];
                 p.FindPropertyRelative("functionName").stringValue = e.functionName;
                 p.FindPropertyRelative("intParameter").intValue = e.intParameter;
                 p.FindPropertyRelative("floatParameter").floatValue = e.floatParameter;
                 p.FindPropertyRelative("objectReferenceParameter").objectReferenceValue = e.objectReferenceParameter;
                 p.FindPropertyRelative("data").stringValue = e.stringParameter;
                 p.FindPropertyRelative("time").floatValue = e.time;
                 p.FindPropertyRelative("messageOptions").intValue = Array.IndexOf(Enum.GetValues(typeof(SendMessageOptions)), e.messageOptions);
             }
         }
         clips.serializedObject.ApplyModifiedProperties();
     }
 }
 
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 AlkisFortuneFish · Jun 26, 2014 at 12:44 PM 0
Share

Thanks for that, cool to see my code being useful to someone else as well!

avatar image AlkisFortuneFish · Jun 26, 2014 at 12:51 PM 0
Share

Also, you may find some of the extra stuff I posted in this thread useful, namely how to generate animation clips from an imported FBX in code without having to first manually invoke the importer to populate clipAnimations and how to make mask changes actually stick.

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

20 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

Related Questions

Is there an event triggered before editor starts rebuilding scripts? 1 Answer

Capture input events before scene view does? 0 Answers

UnityEvent Serialization Problem 1 Answer

is it possible to extend Mecanim? 1 Answer

Hooks for updating keys in Animation Curve editor window 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