Search Unity

Adjusting start and end frames of animation clip with script

Discussion in 'Animation' started by themisharis, Aug 13, 2019.

  1. themisharis

    themisharis

    Joined:
    Aug 12, 2019
    Posts:
    1
    I have a .fbx model which is imported into Unity. (version 2019.3)
    There is one animation clip attached to the model.

    I want to cut the animation clip into multiple clips based on some separation data that I have (the start and end frames of each piece). I want to do this automatically and the process which I would follow manually goes like this:
    * Click on the model file. On the inspector, go under the Animation tab and modify the start and end frames of the animation.
    * Click on APPLY
    *
    Expand the fbx file on the project window. Then duplicate the animation clip file. That's one clip.

    I want to automate this procedure. I found that I don't have access to a clip's start and end frame. So I tried to use ModelImporter to access the import settings of the model and mimic the procedure above. Using that, I tried to also use ModelImporterClipAnimation and then change the fields firstFrame and lastFrame. Then, I use Instantiate() to copy the newly modified animation clip and save that copied clip somewhere else.

    My problem is that the clip doesn't seem to be modified. When I use Instantiate(), the whole clip is copied without any change to its start and end frames. I tried using SerializedObject as an attempt to mimic the pressing of the APPLY button in the model settings window, but I had no success.

    Any ideas about how to simplify the process or of what I am doing wrong?
     
  2. zhipingne

    zhipingne

    Joined:
    Aug 3, 2019
    Posts:
    1
    Code (CSharp):
    1. private void SplitAnimation(string file, int splitFramePoint)
    2. {
    3.     var fileName = Path.GetFileNameWithoutExtension(file);
    4.     var fileNameParts = fileName.Split('_');
    5.     fileNameParts[2] = (int.Parse(fileNameParts[2]) + 1).ToString("D2");
    6.     var newFile = file.Replace(fileName, string.Join("_", fileNameParts));
    7.     AssetDatabase.CopyAsset(file, newFile);
    8.                                                                                        
    9.     {
    10.         var animationImporter = AssetImporter.GetAtPath(file) as ModelImporter;
    11.         var defaultClipAnimations = animationImporter.defaultClipAnimations;
    12.         foreach (var animationImporterClipAnimation in defaultClipAnimations)
    13.         {
    14.             animationImporterClipAnimation.lastFrame = splitFramePoint;
    15.         }
    16.         animationImporter.clipAnimations = defaultClipAnimations;
    17.         animationImporter.SaveAndReimport();
    18.     }
    19.                                                                                        
    20.     {
    21.         var copiedAnimationImporter = AssetImporter.GetAtPath(newFile) as ModelImporter;
    22.         var defaultClipAnimations = copiedAnimationImporter.defaultClipAnimations;
    23.         foreach (var animationImporterClipAnimation in defaultClipAnimations)
    24.         {
    25.             animationImporterClipAnimation.firstFrame = splitFramePoint;
    26.             animationImporterClipAnimation.lastFrame -= splitFramePoint;
    27.         }
    28.         copiedAnimationImporter.clipAnimations = defaultClipAnimations;
    29.         copiedAnimationImporter.SaveAndReimport();
    30.     }
    31. }