I finally finished with my Python scripts that fix the rig.
In my other experiments, I tried out the “@” notation that Unity has, like it mentions in the manual:
https://docs.unity3d.com/Manual/Splittinganimations.htmlI ended up with lots more files. But it turns out, even though there’s the extra step of exporting them to FBX files before importing to Unity, the whole time it takes is quicker than when I was using .blend files in the project.
Note: Unity seems to dislike having “.” in animation names. Names like “Rapier.Move.Forward” get renamed into “Rapier_Move_Forward” when imported to Unity, for some reason. This was the same with my rig’s bone names. Names like “ORG-forearm.L” in Blender turns into “ORG-forearm_L” in Unity, so I decided to just stick with it.Originally, I was going with one .blend file per group of animations:
While it did feel convenient just having the .blend files themselves in the project, it had one big disadvantage: anytime I edit an animation, re-import times are extremely slow.
If I change even just 1 animation inside a .blend file, Unity has to completely re-import all animations inside it, because re-importing works on a per-file basis.
What’s worse is that each of them have their own copy of the character’s mesh (I need to see the character when animating). Which means even when I only edited 1 animation in it, Unity has to re-import not only the other animations, but also the mesh inside that .blend file.
That’s why re-importing (what I thought was only 1 animation) takes so long.
So while the idea that having the .blend file directly in the project might feel like you’re giving yourself quick iteration time, once you’re working with lots of animations, it really doesn’t.
_____________________
But the extra step of having to click on Export FBX is a hassle, right? This is why I made myself a Python script for quick exporting.
Initially, to convert all my existing animations from the .blend file into those FBX files, I made a script that calls the FBX exporter script to “batch” export all animations I want in one go, using the
“character_name@animation_name.fbx” notation that Unity uses. This not only makes it convenient for me, it also assured me that each animation was exported consistently, with the same settings.
I don’t have a GUI for this (learning the Blender Python API was already quite a lot, so I hadn’t bothered learning to make custom Blender GUI, although at initial glance, it reminds me of Unity’s IMGUI), so it has to be run from Blender’s Text Editor.
For new animations, I’ll be making another Python script to export only that animation. The idea with this script is that it should have minimal GUI, and fast. Same as the experience you get when you press
Ctrl + S to save.
So something like
Shift + Alt + S to export the currently viewed animation into an FBX file, again using the
“character_name@animation_name.fbx” notation. The script would be exporting it with the settings I know will work in Unity (FBX 6.1 ASCII version, with only the selected armature and action exported, and nothing else).
I’ll probably release these Python scripts as open-source, later on.
_____________________
On the Unity side of things, configuring the import settings for each of these animation FBX files is a hassle, because normally it would work like this:
1. copy the file into your project’s Assets folder
2. Alt+Tab into the Unity Editor
3. before you can react, Unity imports the file, since it detects the file as new (or changed)
4. you go to the file and configure its import settings
5. you click on Apply and Unity imports it again
This means for each file, Unity has to import it twice. It’s also a chore to go and click the proper checkboxes for the import settings you want for all of these FBX files, assigning the proper Avatar definition file, the Avatar Mask file, setting the animation’s loop to true if it needs to, etc.
So I made an
AssetPostprocessor script to do it for me. I also made a ScriptableObject to store my settings for these, that I named CharacterImportSettings. I could have several of these CharacterImportSettings, so I have one for the Desparo character, another for the Demento character, etc.
I know Unity 2018 has this new feature called
Presets which works the same (I only use Unity 2017.4 since that is the “long-term stable” release). Some reading into it shows that, using an AssetPostprocessor, you can effectively
apply a default import settings to all assets in a given folder. Very similar to the script that I ended up making.
The script I made works based on filename matching. I guess the only advantage with mine that I can think of is that it can automatically mark an animation to loop based on the animation name.
So an animation from the file “Desparo@Spear_Standing.fbx” will be automatically set to loop, while something like “Desparo@Spear_Attack_Thrust_1.fbx” won’t be.
_____________________
I’m also happy with this since now I don’t have to use the cumbersome controls for adding Extra Transforms to Expose (who in Unity thought this was good enough?!?):
Adding an Extra Transform to Expose meant having to click through menus on top of menus to get to the bone you want, each time starting from your rig’s root. And these menus can instantly vanish if you accidentally clicked outside, requiring you to do the whole thing all over again.
What I have doesn’t really fix this problem, it’s just a workaround so I don’t have to use those controls. I have a string array storing the values (which I had to type), and they’re applied using the AssetPostprocessor script I have.
I reuse the same type of rig for all the biped characters I make in Blender, so this is a huge time-saver.
The reason I have so many of these Extra Transforms is because I add colliders to the body parts. Those colliders are needed by the cloth simulation so that the cloth doesn’t pass through the character, and they’re also used by the combat system to detect hits. I also attach particle effects and lights on certain body parts.
I’ve also edited my Mecanim state machines to make use of the new animations, so everything’s now good to go.
The lesson here: just export to the goddamn FBX files, Unity does that anyway under the hood when you place .blend files into the project. You’ll see a “Unity-BlenderToFBX.py” inside your installation folder for Unity, normally under the “Editor/Data/Tools” subfolder. It’s a small script that finds and calls Blender’s FBX exporter script.
And make scripts to automate repetitive chores.