- Home /
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.
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();
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
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, "..." );