Search Unity

Assign AnimationClips to AnimController at time of FBX Import?

Discussion in 'Animation' started by aer0ace, Jul 7, 2020.

  1. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Is it possible to load AnimationClips to an existing AnimationController in an AssetPostprocessor when an FBX is imported?

    I have an existing AnimationController, and what I'd like to do is when I import an FBX file with AnimationClips, I'd like the AssetPostprocessor to load the AnimationClips by name, find the same state in the AnimationController by name, and force-assign it to that state. The reason I want to do this is that when the source FBX is wiped clean and re-imported from scratch, the references to the AnimationClips are destroyed, and I'd like the AssetPostprocessor to re-establish those links. Someone may say that this works already, but I've run into the case far too many times that the reference is broken and I have to reassign them manually.

    I see that in the ModelImporter, there is the list of clipAnimations, but these are ModelImporterClipAnimation, not the actual AnimationClip.

    I understand that I can use Resources.Load<AnimationClip>(), but this is at game runtime, not during edit time, which is what I like about the AssetPostprocessor.

    This solution has eluded me for several years now, and all the searches online have yielded only partial solutions, or solutions that aren't applicable to my use case.

    Any ideas? Thanks in advance!
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,565
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I've tried LoadAllAssetsAtPath, but I think the problem is you can't use this while in the AssetPostprocess call when loading the same FBX asset. The clips return 0 count. The assetImporter does have ModelImportClipAnimation, but that's not an instantiated clip.

    I suppose Resources.Load would have the same problem in the AssetPostprocessor for the same reason.

    Hmm, I haven't tried OnPostprocessAnimation() yet. This occurs after an AnimationClip is imported.
     
    Last edited: Jul 7, 2020
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    So, I tried OnPostprocessAnimation(GameObject root, AnimationClip clip), and it seems that this is what I want, but it doesn't fully work. I can assign the clips to each states' motion property, but when I go to save it, I check it, and the motion is null. I was sure to call SetDirty(animatorController), and SaveAssets(), but this did not work.

    Is what I'm doing even possible? I've tried maybe 3 times to do this in the last 6 years, and it seems as if Unity is purposefully preventing me from changing this, but it doesn't make sense that this would be the case.
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,565
    It's definitely possible. There's an asset on the store for generating Animator Controllers with code. What does your code look like? Most likely there is something you need to re-assign back to the Animator Controller after making your changes. Any time you get something (like a list of layers or states) and make changes to it, make sure you assign it back to the property you got it from.
     
  6. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I think I may have actually just got this.
    Well, I was definitely re-assigning the edited layers back to the controller, but my last problem was that I was assigning the AnimationClip that was passed from OnPreprocessAnimation() when I should have been copying that clip into a new clip, and assigning that into the motion for the state of the controller. That seemed to get the controller to save the asset.

    What asset on the store is used for generating AnimatorControllers? Is it your Animancer? I could have sworn I came across this before. I may end up using that instead, if this solution doesn't pan out!

    Thanks for the help!
     
  7. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Not sure why I haven't purchased Animancer yet. I think I'm going to be using that instead. I've run into so many timing problems with AnimatorControllers on previous projects. So, take my money!
     
  8. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,565
    No, Animancer lets you avoid Animator Controllers entirely (which I obviously recommend).

    C# Animator was the asset I was thinking of.
     
  9. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Ah, I see. Either way, I am going to evaluate Animancer, as I don't need complex state machine logic. I just need to play clips, sometimes just to load a pose on initialization, and some of your points that you bring up against Mecanim are drawbacks that I've personally run into, so I'm hoping it's the way to go.