Sunday, July 1, 2018

Unity Quick Tip: Editing .anim files



What's an .anim file?

An .anim file is created when we create a new animation clip in Unity. It contains all the data that the Animator needs to play a clip, be it object positions, names, rotations or anything else.

Actually these files are YAML, you can simply open them with any text editor and change them.


Why would I want to manually edit these files?

That's a great question! You normally wouldn't want to edit them as text, you would use the Unity Animator. But the Animator, as you probably know, has some flaws.
  • Changed an object name? The animation clip will lose the reference to it.
  • Want to change every position of every keyframe by a fixed amount? you'll need to edit them one by one.
  • You made some animations and then realized that the root bone had a different scale than 1 or you changed your sprites PPU? Changing those things would break all your animations.

Oh no! What can we do?

Do not worry! As we said we can open and edit these files manually, which means we can edit them with a script to change the values we need.

If you changed an object name, that's easy enough. Just search and replace as with any text.
Object names are stored like this: path: Bones/Hip, so you just need to change that with your new object path.

If you changed your root bone scale, position o sprites PPU, you'll need to get the positional values of every object and make some operation with them.
Positions are stored like this: value: {x: 0.43749998, y: -0.31250006, z: 0}.
We need to parse the file, get every string that matches that format, get every number inside it and replace it with our new values. As an example let's say we want to multiply them by 2.


Dirty work

Disclaimer: I know this code is awful and can be improved, but I know very little Python and just wanted to get the job done. Feel free to suggest a cleaner way to do it!

We will solve this using Python and Notepad++ (you could just as easily do it without Notepad++).
First we'll need to download Notepad++ (the 32-bit version) and this plugin that allows us to run Python scripts.
Once you have everything set up, open Notepad++ and go to Plugins/Python Script/New Script to create a new script and call it whatever you want and paste this code:


def multiply(match):
   return "value: {x: " + "{0}".format(str(float(match.group(1))*2)) + ", y: " + "{0}".format(str(float(match.group(3))*2))

editor.rereplace(r'value: {x: ([+-]?([0-9]*[.])?[0-9]+), y: ([+-]?([0-9]*[.])?[0-9]+)', multiply)


This code is defining a method that gets matches from a regular expresion and replaces them with what we tell it to. In this case, with the same string but with every number multiplied by 2.
Note that I only included X and Y coordinates, because I'm working in 2D, but you can easily extend this to cover the Z axis.

Now we just need to open the .anim file we want to edit and go to Plugins/Python Script/Scripts/yourScript, and it will change the file! Save it and go to Unity, once it's reimported you should see the changes in your animation right away.

Warning

While the code above will work and will change your animations, if you try to edit them in Unity the animator will go back to the previous state. That's because we didn't actually edited the local position of the objects. The problem is that a simple regular expression won't work for that and we'll need a YAML editor or something more powerful to do it but that escapes the scope of this post.

Any feedback or questions? Let me know in the comments!
If you enjoyed the post, remember to check the Patreon!

You can also follow Strangewire on FacebookTumblr

1 comment:

  1. I can't open the animation file in a text editor. I tried renaming it to a text file and then tried. It worked but renaming it to .anim won't change the text file into animation.

    ReplyDelete