Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to sample pose directly from an AnimationClip?

Discussion in 'Animation' started by JoeStrout, Aug 2, 2019.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've got a project with a very unusual need: I need to sample and mix AnimationClip frames, in essentially random order.

    For example: I want to set my model to 10% of ClipA at frame 0 plus 90% of ClipB at frame 17. Or — also an acceptable alternative — 10% of ClipA at frame 0, plus 90% of whatever pose the model was previously in.

    I know about AnimationClip.SampleAnimation, but it doesn't support this sort of mixing. I'd be happy to apply all the bone rotations (in our animations there are only rotations, no scaling or translations) myself. But how do I get that data?

    The only method I can think of, based on the docs, is to have an invisible avatar in the scene, SampleAnimation into that, and then read the rotations out of its transform hierarchy and apply them to my real model. That would work but seems a bit cumbersome. Is there any better method?
     
  2. Grizmu

    Grizmu

    Joined:
    Aug 27, 2013
    Posts:
    131
    One hacky way that I know for getting this result is using the animator with two additive layers, where each has states with speed set to 0. You can then play those states at a given frame and sample/mix them by changing the weight of the layers. Example added as the attachement.

    With this approach you can use as many layers as you like, at a cost of having to buffer some editor-only animator stuff before deploying. Other problem is that it hurts to not be able to create animator states from script at runtime.

    Playables are also available, and they have blend trees which could be probably used in this case:
    https://docs.unity3d.com/Manual/Playables-Examples.html
     

    Attached Files:

    ProfShuai and JoeStrout like this.
  3. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    bam, that's an idea!

    have to try out if it works with mecanim.

    (that might even work with changing anim states's clips, hm)
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @Griz, this is a very cool trick. It took me a little while to fully grok it, but I believe I understand now.

    But unless I'm mistaken, this relies on UnityEditor.Animations, and so can't work in a build. :( I need something that works at runtime, not just in the editor.
     
  5. Grizmu

    Grizmu

    Joined:
    Aug 27, 2013
    Posts:
    131
    @JoeStrout You can avoid that import by baking some stuff, and it will work just fine in the build :).
     

    Attached Files:

  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Neat! In the end I've decided to do it with Animancer, but it's nice to know this is possible without it.
     
    Kybernetik likes this.
  7. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Nice plug for Animancer! Never heard of it until now. I may have to get it too.
     
    Kybernetik and JoeStrout like this.