I was going through my answers to questions over on the Blender StackExchange when I noticed at some point I had written working solutions for both batch import and export of wavefront obj files so I thought I would share both here.
Note: These scripts should work for any file that holds mesh data such as .ply, .stl etc if you change the scene operator.
The regular workflow for importing files into Blender is to do this one at a time as it will only import the first selected if you use the import menu. This will obviously become tedious for several dozen files but is easily solved using a bit of Python. Back in Blender 2.49 IIRC you could batch import by holding Shift and then selecting a directory but this behaviour was removed for some reason. These scripts were tested on a Windows machine so naturally you will probably have to tweak the path if you are using another os.
# http://blender.stackexchange.com/questions/5064/batch-import-wavefront-obj/5065#5065 import os import bpy # location to the directory where the objs are located # if you are using windows style backslashes, you need to cancel one with another path_to_obj_dir = os.path.join('C:\\', 'folder_name') # get list of all files in directory file_list = sorted(os.listdir(path_to_obj_dir)) # get a list of files ending in 'obj' obj_list = [item for item in file_list if item[-3:] == 'obj'] # loop through the strings in obj_list and add the files to the scene for item in obj_list: path_to_file = os.path.join(path_to_obj_dir, item) bpy.ops.import_scene.obj(filepath = path_to_file)
# http://blender.stackexchange.com/questions/5382/export-multiple-objects-to-obj/5383#5383 import bpy # get the path where the blend file is located path = bpy.path.abspath('//') # deselect all objects bpy.ops.object.select_all(action='DESELECT') # loop through all the objects in the scene scene = bpy.context.scene for ob in scene.objects: # make the current object active and select it scene.objects.active = ob ob.select = True # make sure that we only export meshes if ob.type == 'MESH': # export the currently selected object to its own file based on its name bpy.ops.export_scene.obj(filepath=str(path + ob.name + '.obj'), use_selection=True) # deselect the object and move on to another if any more are left ob.select = False
To have this only export selected objects you could comment line 7 and change line 11.
# remove bpy.ops.object.select_all(action='DESELECT') # change to this where instead of the entire scene, we only look through selected objects for ob in bpy.context.selected_objects: