Continuing my foray into the world of motion capture, I’ve been building up my own collection of mocap data files from various sources I find on the ‘net, with the hope of ultimately using them to animate a Mecanim rig in Unity. While I don’t think Mixamo will be worried quite yet(!), I have gathered enough BVH files such that loading them up individually into Blender and saving them to FBX necessary for import in Unity has become quite a chore.
So, I wrote a python script to automate the process, convert_fbx.py, as follows:
import bpy import sys #Get command line arguments argv = sys.argv argv = argv[argv.index("--") + 1:] # get all args after "—" bvh_in = argv fbx_out = argv + ".fbx" # Import the BVH file # See http://www.blender.org/documentation/blender_python_api_2_60_0/bpy.ops.import_anim.html bpy.ops.import_anim.bvh(filepath=bvh_in, filter_glob="*.bvh", global_scale=1, frame_start=1, use_fps_scale=False, use_cyclic=False, rotate_mode='NATIVE', axis_forward='-Z', axis_up='Y') # Export as FBX # See http://www.blender.org/documentation/blender_python_api_2_62_1/bpy.ops.export_scene.html bpy.ops.export_scene.fbx(filepath=fbx_out, axis_forward='-Z', axis_up='Y', use_anim=True, use_selection=True, use_default_take=False)
Then, create a batch file, bvh2fbx.bat, that loops through all BVH files in a directory and calls Blender from the command line with the script above, passing in the details of files to be converted:
FOR %%f IN (*.bvh) DO "c:\program files (x86)\blender foundation\blender\blender.exe" -b --python "c:\temp\convert_fbx.py" -- "%%f"
(Alter the path to the Blender executable and to where you saved the convert_fbx.py script as appropriate). Run the bvh2fbx.bat file from the commandline and you should get all BVH files in the directory automatically converted to FBX.
All that remains is to import them to Unity and add a Humanoid rig and they’re ready to use in an Animator!