How to sample pose directly from an AnimationClip?

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?

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:

4819370–461882–AnimationMixing 2019_2.unitypackage (8.22 KB)

2 Likes

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)

@Grizmu , 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. :frowning: I need something that works at runtime, not just in the editor.

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

4823669–462653–AnimationMixing runtime 2019_2.unitypackage (8.79 KB)

Neat! In the end I’ve decided to do it with Animancer, but it’s nice to know this is possible without it.

1 Like

Nice plug for Animancer! Never heard of it until now. I may have to get it too.

2 Likes