Batch Conversion of BVH to FBX Motion Capture files

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[0] 
fbx_out = argv[0] + ".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.

image

 

All that remains is to import them to Unity and add a Humanoid rig and they’re ready to use in an Animator!

image

About these ads
This entry was posted in Game Dev and tagged , , , , , , . Bookmark the permalink.

2 Responses to Batch Conversion of BVH to FBX Motion Capture files

  1. Anil Punjabi says:

    Hi there.

    Many thanks for sharing your experience with migrating the animation files.

    Wondering you’ve had success with this approach and if you could kindly share your converted animation files that can be used with Mecanim.

    Would greatly appreciate it.

    Thanks,
    -Anil

    • alastaira says:

      Yes, it works great (depending on the quality of the BVH file with which you started, obviously). I can’t post the converted animation files because they’re not licensed for me to redistribute, but there’s plenty of free and commerical BVH files on the net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s