Search Unity

Converting legacy models/animations to Mecanim?

Discussion in 'Animation' started by JoeStrout, Dec 2, 2013.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm sure this must be a FAQ by now, but my google-fu is weak tonight.

    I've been using Unity for quite a while, but never messed around much with character animations before. So I'm not heavily invested in any particular approach, but the handwriting on the wall clearly states that Mecanim is the way to go. I've gone through Will's tutorial, and done some experimentation, and I'm sold: I'd like to settle on Mecanim animation for future projects.

    The trouble is, I'm still finding a tone of great models and animations in the legacy format. For example, the Astro Dude tech demo (from Unity, in the Asset Store). This contains an adorable little character, and ten nice little animations.

    1. How would I convert this model for use with Mecanim animations? I believe this is possible, by changing the Animation Type (under Rig) from Legacy to Humanoid, and then configuring. But it'd be comforting to have a definitive how-to on this, if there is one.

    2. Can I also convert these animations for use with other Mecanim humanoid models? I suspect the sad answer is "no," but I'd be happy to be wrong.

    Basically, what do all you experienced folks do with these older art assets? Use 'em as-is, convert 'em, or just not use them at all?

    Many thanks,
    - Joe
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    A model's a model. Legacy vs. Humanoid vs. Generic just tells Unity how to handle it. You should have no problem setting Astro Dude's rig to Humanoid. Unity's automapper might have difficulty figuring out some models' skeleton structures, but you can click Configure... and fix them up manually if that's the case.

    The same holds true for animations. The only major difference is that Mecanim supports root motion out of the box, and most legacy animation is done in-place. In this case, you could use only the animations that don't move the character, such as waving and bowing, and grab root motion animations from Unity's Raw Mocap Pack or Locomotion Starter Kit. I mix and match like this all the time with "legacy" models/animation and those built with root motion.

    Or you could script root motion, but that's more complicated.

    One nice feature of Mecanim is that you can visually build and preview your animation state machines, which your sons might enjoy doing.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Thanks Tony, this is helpful info. The model conversion sounds like what I was expected, but that bit about mixing the animations is news to me. Somehow I had the impression that legacy animations were based on morph maps rather than bones, but either that's not true, or the Mecanim system works with both.

    I think I'll spend a couple of days trying to do just what you described: switch Astro Dude to a Humanoid rig, and mix his stock animations with some Mecanim animations from elsewhere (such as the Locomotion Starter Kit). I'll write this up as a tutorial and post it somewhere, so others can both learn from it and point out anything dumb I might be doing!
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, I've run into a bit of a snag having to do with the vertical position of the model. (This is using Astro Dude.)

    When I convert the model, map out all the bones, and then drag it to my scene, a Y position of 0 embeds him in the ground. Just the top half of his helmet is peeking out of my Y=0 ground plane. No big deal so far.

    Now when I go to convert each animation, I see the purple preview also embedded in the ground in the Preview window. But here, I can correct it in the Animations tab: under Root Transform Position (Y), I change "Based Upon" to Feet. This makes the animation look fine in the preview, and when I apply the converted animation to my model, then at runtime, my model looks great there too — he idles or dies or whatever on the ground, instead of four feet under.

    But when I'm arranging my scene, I still have a model that's about 1.3 meters lower than it should be. This is a real pain, since I can't really see what the scene's going to look like. His position while animating is quite different from his static position (and I suspect this will cause other headaches, too).

    Where have I gone wrong here? Isn't there some way to apply an offset to the model itself, so his root position is between his feet even when not animating?

    Thanks,
    - Joe
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    And, nuts, more trouble.

    I next tried to retarget another Mecanim animation to my converted Astro Dude model. I used IdlesWave.fbx from the MecanimTute project. Dragged it in, opened it up, found the animation clip (named "idle"), and dragged it into my Animator, where I renamed the state to "wave" so I don't get confused.

    Then I added a transition from my real idle state (a converted Astro Dude animation) to my new wave state. But already, in the inspector for this transition, I can see something is wrong. When the playhead gets to the second part (the wave state), instead of waving, Astro Dude curls into a fetal position and freezes.

    If I optimistically run the game anyway, it's pretty much the same result: when I trigger the wave, Astro Dude assumes the fetal position, sinks into the ground, stays there for the duration of the wave, and then comes back up to idle.

    In the project hierarchy, when I inspect that animation clip, the preview looks fine — bald skinny guys does a nice wave. But applied to Astro Dude, no joy.

    This is very disappointing, to say the least. Being able to use Mecanim animations with converted characters was the whole point. I'm sure it must be possible... any idea what I've done wrong?

    Thanks,
    - Joe
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    I don't know of any way to apply the offset to the model itself except by editing it in, say, Cheetah 3D.

    Did you import IdlesWave.fbx as Humanoid? I just loaded Astro Dude and the Mecanim Locomotion Starter in a test project, and it works fine -- apart from the model starting halfway in the ground as you mentioned. (Sorry, I don't have the example scenes project downloaded, and it's taking a really long time to download....)

    [Edit:] Finally downloaded. I imported IdlesWave and it works, too. For Avatar Definition, I selected Create From This Model. I don't know if that makes a difference or not for you. A minor issue is that Astro Dude's head is so big that the hand clips into his helmet.
     
    Last edited: Dec 3, 2013
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Drat. I don't know what I'm doing differently, but it's still not working for me.

    I checked the Rig import settings on IdlesWave.fbx, and it was set to Generic. So I changed it to Humanoid, applied, and for good measure deleted and recreated the corresponding Animator state. Now he doesn't curl into a fetal position, but he doesn't wave either — just sort of stiffens and waves about (a subtle root transformation, I would guess) a little.

    For Avatar Definition, I'm also using Create From This Model.

    I'm not concerned about the hand clipping into the helmet — that's to be expected. In fact I'd be delighted if the little guy would raise his hand at all.

    ...Ugh, and now my idle and die animations are no longer working properly, either! I'm starting to feel like this Mecanim animation stuff is still a little green. :( But it's just as likely that I've shot myself in the foot somehow. I'll report back if I figure it out.

    EDIT: a minute later, the idle and die animations are working again. It's the darndest thing — somehow I was in a state where the currently active state in the Animator view was not drawing its usual blue progress bar, and in the scene, the animations were not being applied to the model, though the root transformations still were (so I could tell that the state machine was actually doing something). I tried turning off "Apply Root Motion" on the player model, and on that test, the progress bars came back and the idle/die animations worked again. Turned "Apply Root Motion" back on, and they still worked.

    I'm unable to reproduce exactly that state, but I can reproduce part of it: if I toggle Apply Root Motion (in either direction) while the script is running, then my animations stop working (the model still changes position, but doesn't change shape). Once in this state it stays that way, even if I toggle it back, until I stop the run. So, I guess tweaking that property during a run breaks something within Mecanim.

    Sigh... I guess I'll start over and try to reproduce your success with the wave. There must be something I'm doing differently...
     
    Last edited: Dec 3, 2013
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    I forgot to mention that I tested this in Unity 4.3.1. Maybe there's a difference. Also, try importing Astro Dude and IdlesWave with the same scales (on the Model tab). Since he only moves a little bit, maybe IdlesWave is getting imported at a smaller scale.

    I can see that toggling Apply Root Motion at runtime might cause problems, since Mecanim attempts to modify the animation by extracting the curves related to root motion.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Thanks for the tips! I didn't expect the Model scale to matter, since animations are just a collection of rotations for the bones (aren't they?). But I'll give it a try.

    I've also learned that it very much matters what else is in the project. I tried it all again, this time importing the complete Astro Dude and Locomotion Starter projects, and it worked. Previously I had imported just the parts I thought I needed (mainly the models and animations), and that didn't work out well.

    Also, I've found that it matters what you set the avatar to in the anmiation import settings. "Create From This Model" seems to usually work; "Copy From" some other avatar usually doesn't — but sometimes it does. Sometimes the very same copy-from doesn't work, and other times it does; there's certainly some voodoo there I don't yet fully grok.

    But I've also discovered that when it's not going to work, you can tell this right away in the animation preview (on the clip inside the FBX). The preview model will curl right into the fetal position instead of doing the action. So you know it's no good, and you can go try other import settings until the preview looks good. At that point, it's probably going to work when applied to another model.

    Finally, I've learned that once you have an animation previewing correctly, you can select the clip, Duplicate it, and get a simple .anim file that appears as its own project item. I noticed that the "Warrior Anim" sets in the Asset Store are shipped this way; just a collection of animation clips (.anim files), which seem easier and more reliable to use. So, my new plan is to build up a library of these, converting any FBX-based animation into one or more .anim's as soon as I get it. Hopefully that will spare me the voodoo once it's done.

    So I'm definitely making progress... and once I feel a little more confident in the whole process, I really will write up a tutorial to share any wisdom I've gained!
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Glad it's working! I forgot to mention duplicating clips. People also do this to make an editable copy of the clip in order to add animation events (new for Mecanim in 4.3). For example, you could add an event at the moment Astro Dude's feet leave the ground in a jump animation, and spawn a dust cloud particle effect.
     
  11. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
    Is this possible to retarget an animation with the humanoid rig to run properly for mechanim
    I know the Generic and Humanoid both includes in new animator (Mechanim) but the problem is that when I change rig of my character from generic to humanoid. The animations got spoiled. The Character has been configured properly for humanoid rig and the animation get played in the animation tab but the don't work from inside the script? What I am doing wrong?
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Try this:

    1. Select the character in the Scene view.
    2. Open the Animation view.
    3. Try to play the animation in the Animation view.

    Does the character in the Scene view animate properly? If so, it's probably a script issue. Look at your script. If it doesn't work, it might be an import issue.