# How to batch convert between file formats?

I have about thousands of files I need to convert into from one format to another. Both formats are handled by existing add-ons..

I assume the script process would go something like this:

• Select every source file in a directory and it's sub-directories
• Store the path and filename of the source format.
• Delete everything in scene and import the file
• Export the file to the destination format with the same path and the same filename as the original file, with a different extension.
• move on to the next item

The problem is that I have very little experience with Blender, and Python is not my area. The manual solution of importing and exporting works perfectly fine, as there is no need for anything to be modified.

• what are using to import .meshdata ? Jul 25, 2015 at 21:34
• Was wondering if you could point me in the direction regarding batch exporting since you found good success in the endeavor. I am looking to importing a whole bunch of FBX file then save them individually into blender format. I have looked over the stuff posted here but did not really understand how it all fit together. Is there a add-on plugin that you wrote yourself or packages you could share so that I can edit them to the fix the files I needed to convert Thanks for your time. Dec 26, 2015 at 6:08

Set the path variable and change the import line to whatever importer you are using :

import bpy
import os

path = 'C:/path/to/files/'  # set this path

for root, dirs, files in os.walk(path):
for f in files:
if f.endswith('.meshdata') :
mesh_file = os.path.join(path, f)
obj_file = os.path.splitext(mesh_file)[0] + ".obj"

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

bpy.ops.import_scene.obj(filepath=mesh_file) # change this line

bpy.ops.object.select_all(action='SELECT')

bpy.ops.export_scene.obj(filepath=obj_file)

• Although I have one issue. I can't seem to find the correct importer function. Here's the init.py: pastebin.com/G980EA2h and here's the meshdata_import.py: pastebin.com/bKkWiVMD Here's a meshdata file: dump.no/files/78e43d0cb895/… the error occurs on this line: bpy.ops.object.mode_set(mode='OBJECT') With the error: RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect. Disabling everything bellow the import line causes no errors, but also no imported files. Could you have a look? Jul 26, 2015 at 14:31
• Okay, it now successfully imports all the objects, but it's still getting the "RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect" error. caused by "bpy.ops.object.mode_set(mode='OBJECT')" Jul 26, 2015 at 15:08
• @MatsBakken ( sorry I was away ) can you just comment this line and try again (it's not that crucial ) Jul 26, 2015 at 15:41
• Wow, I didn't actually think of that. It works now! Thank you! Although it only does the specified directory and not it's sub directories. I guess I should read up on python. Jul 26, 2015 at 15:57
• @Mats see if this helps Jul 26, 2015 at 16:22

This script is an example of how you can loop over all files and convert from one format to another.

This example converts all .obj files to .x3d, but changing the formats is trivial.

CONVERT_DIR = "/my/test/directory"

import os

def file_iter(path, ext):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
ext = os.path.splitext(filename)[1]
if ext.lower().endswith(ext):
yield os.path.join(dirpath, filename)

import bpy

def reset_blend():

def convert_recursive(base_path):
for filepath_src in file_iter(base_path, ".obj"):
filepath_dst = os.path.splitext(filepath_src)[0] + ".x3d"

print("Converting %r -> %r" % (filepath_src, filepath_dst))

reset_blend()

bpy.ops.import_scene.obj(filepath=filepath_src)
bpy.ops.export_scene.x3d(filepath=filepath_dst)

if __name__ == "__main__":
convert_recursive(CONVERT_DIR)

• I'm getting this error when I use this script: TypeError: Converting py args to operator properties: : keyword "use_empty" unrecognized Jul 30, 2017 at 5:07