- Home /
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!
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;
}
}
}
That is very interesting. Can you fix your code posting, please? Especially how did you get the SerializedProperty "clips" out of the $$anonymous$$odelImporter?
It seems to be var clips = new SerializedObject(modelImporter).FindProperty("m_clipAnimations");
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.
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();
}
}
Thanks for that, cool to see my code being useful to someone else as well!
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.