29
$\begingroup$

Is it possible to batch import Wavefront .obj to blender? I'm working on 2.69. If I select multiple .obj files, only one gets imported. Also if I hit A on file selection window to select all files (.obj and .mtl) I get an error, nothing gets imported. I have to import several hundreds of .obj into the blender file, is this possible?

$\endgroup$
0

2 Answers 2

28
$\begingroup$

Unfortunately, this is not possible by default with Blender 2.5x+ it seems. In 2.49, you could hold Shift then press the Import Obj button and it would prompt you to select a directory with the sequence.

In 2.69 - and at least until 2.82 - you can do the same with a script — it could be converted into a more usable utility but it works.

import os
import bpy

# put the location to the folder where the objs are located here in this fashion
# this line will only work on windows ie C:\objects
path_to_obj_dir = os.path.join('C:\\', 'objects')
 
# 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.endswith('.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, use_split_objects=False, use_split_groups=False)

For unix machines the direct path should look like:

# location of obj folder
path_to_obj_dir = '/home/user/Desktop/objects/'
$\endgroup$
5
  • 2
    $\begingroup$ To use the script, simply paste it into Blender's text editor, edit the path on the 5th line and press Alt + P or Run. $\endgroup$
    – iKlsR
    Nov 23, 2013 at 16:09
  • $\begingroup$ Thank you so much, works like a charm, but I had to do some research and trial error with my very poor knowledge of python. I had to change the extension to OBJ because by using print(obj_list) I noticed that it wasn't seeing the objs that had an uppercase extension, checked it with print(file_list) and then changed to OBJ and it worked! $\endgroup$ Nov 24, 2013 at 19:22
  • $\begingroup$ @akzash rejected your suggested edit re removing objects from windwos path. Your error would be in that you don't have an objects folder in c drive. $\endgroup$
    – batFINGER
    Dec 14, 2017 at 12:22
  • $\begingroup$ @iKlsR I am importing many .obj files sequentially. Very quickly, import time increases significantly to the point that it takes minutes for each object to get imported. Could you please take a look at my question here and see if you can offer a solution? Appreciate it. $\endgroup$
    – Amir
    Mar 4, 2018 at 20:38
  • 1
    $\begingroup$ FWIW the same bpy.ops.import_scene.obj() method will also work in Blender 2.82. $\endgroup$ Jun 3, 2020 at 15:08
36
$\begingroup$

The following Add-on allows to import multiple Wavefront OBJ files at once. By using the file browser the import process is platform independent. Also all the usual options like Forward Axis, Up Axis or Clamp Size are available.

Repository: github.com/p2or/blender-batch-import-wavefront-obj

enter image description here

io_batch_import_objs.py (updated for 3.5+)

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>


bl_info = {
    "name": "Batch Import Wavefront (.obj)",
    "author": "p2or",
    "version": (0, 7, 0),
    "blender": (3, 5, 0),
    "location": "File > Import-Export",
    "description": "Import multiple OBJ files, UV's and their materials",
    "doc_url": "https://github.com/p2or/blender-batch-import-wavefront-obj",
    "tracker_url": "https://github.com/p2or/blender-batch-import-wavefront-obj",
    "category": "Import-Export"}


import bpy
from pathlib import Path

from bpy_extras.io_utils import ImportHelper

from bpy.props import (BoolProperty,
                       FloatProperty,
                       StringProperty,
                       EnumProperty,
                       CollectionProperty)


class WM_OT_batchWavefront(bpy.types.Operator, ImportHelper):
    """Batch Import Wavefront"""
    bl_idname = "wm.obj_import_batch"
    bl_label = "Import multiple OBJ's"
    bl_options = {'PRESET', 'UNDO'}

    filename_ext = ".obj"

    filter_glob = StringProperty(
            default="*.obj",
            options={'HIDDEN'})

    files: CollectionProperty(type=bpy.types.PropertyGroup)
    
    global_scale_setting: FloatProperty(
            name="Scale",
            description="Value by which to enlarge or shrink" \
                    "the objects with respect to the world origin",
            min=0.0001, max=10000.0,
            soft_min=0.01, soft_max=1000.0,
            default=1.0)

    clamp_size_setting: FloatProperty(
            name="Clamp Bounding Box",
            description="Resize the objects to keep bounding box" \
                    "under this value. Value 0 diables clamping",
            min=0.0, max=1000.0,
            soft_min=0.0, soft_max=1000.0,
            default=0.0)
    
    axis_forward_setting: EnumProperty(
            name="Forward Axis",
            items=(('X', "X", ""),
                   ('Y', "Y", ""),
                   ('Z', "Z", ""),
                   ('NEGATIVE_X', "-X", ""),
                   ('NEGATIVE_Y', "-Y", ""),
                   ('NEGATIVE_Z', "-Z", ""),
                   ),
            default='NEGATIVE_Z')
            
    axis_up_setting: EnumProperty(
            name="Up Axis",
            items=(('X', "X", ""),
                   ('Y', "Y", ""),
                   ('Z', "Z", ""),
                   ('NEGATIVE_X', "-X", ""),
                   ('NEGATIVE_Y', "-Y", ""),
                   ('NEGATIVE_Z', "-Z", ""),
                   ),
            default='Y')
            
    validate_setting: BoolProperty(
            name="Validate Meshes",
            description="Check imported mesh objects for invalid data")
    
    vgroup_setting: BoolProperty(
            name="Vertex Groups",
            description="Import OBJ groups as vertex groups")

    use_split_objects_setting: BoolProperty(
            name="Split By Object",
            default=True,
            description="Import each OBJ 'o' as a separate object")

    use_split_groups_setting: BoolProperty(
            name="Split By Group",
            description="Import each OBJ 'o' as a separate object")
            
    
    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False  # No animation.
        
        box = layout.box()
        box.label(text="Transform", icon='OBJECT_DATA')
        col = box.column()
        col.prop(self, "global_scale_setting")
        col.prop(self, "clamp_size_setting")
        col.separator()
        col.row().prop(self, "axis_forward_setting", expand=True)
        col.separator(factor=0.5)
        col.row().prop(self, "axis_up_setting", expand=True)
        col.separator()
        
        box = layout.box()
        box.label(text="Options", icon='EXPORT')
        col = box.column()
        col.prop(self, "use_split_objects_setting")
        col.prop(self, "use_split_groups_setting")
        col.prop(self, "vgroup_setting")
        col.prop(self, "validate_setting")

    def execute(self, context):
        folder = Path(self.filepath)
        for selection in self.files:
            fp = Path(folder.parent, selection.name)
            if fp.suffix == '.obj':
                bpy.ops.wm.obj_import(
                                filepath = str(fp),
                                global_scale = self.global_scale_setting,
                                clamp_size = self.clamp_size_setting,
                                forward_axis = self.axis_forward_setting,
                                up_axis = self.axis_up_setting,
                                use_split_objects = self.use_split_objects_setting,
                                use_split_groups = self.use_split_groups_setting,
                                import_vertex_groups = self.vgroup_setting,
                                validate_meshes = self.validate_setting
                                )
        return {'FINISHED'}


def menu_func_import(self, context):
    self.layout.operator(
                WM_OT_batchWavefront.bl_idname, 
                text="Wavefront Batch (.obj)")

def register():
    bpy.utils.register_class(WM_OT_batchWavefront)
    bpy.types.TOPBAR_MT_file_import.append(menu_func_import)

def unregister():
    bpy.utils.unregister_class(WM_OT_batchWavefront)
    bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)


if __name__ == "__main__":
    register()

    # test call
    #bpy.ops.wm.obj_import_batch('INVOKE_DEFAULT')

Note: It is basically just a quick and dirty wrapper of wm.obj_import or import_scene.obj operator in versions prior to Blender 3.2.0 until batch import is supported. To batch export scene objects, see: Export multiple objects to .obj?

Versions

To keep track of all the changes over the years I decided to create a dedicated repository instead of posting each version here, which makes it easier to download as well:

Blender Addon Version Release
Blender 2.65+ 0.1.0 Download
Blender 2.80+ 0.2.0 Download
Blender 2.92+ 0.3.0 Download
Blender 3.2+ 0.4.0 Download
Blender 3.3+ 0.5.0 Download
Blender 3.4+ 0.6.1 Download
Blender 3.5+ 0.7.0 Download

@JuhaW added a Scale setting to 0.2.0, you can download the latest version from here.


$\endgroup$
11
  • 2
    $\begingroup$ Just wanted to add, THANKS. This helped my workflow immeasurably. I'm consolidating torus knots (KnotPlot) of MULTIPLE obj components and was looking for a way to streamline this import process. Your process (addon) reduced it to seconds. THANKS. $\endgroup$
    – user16237
    Aug 8, 2015 at 7:27
  • $\begingroup$ Many thanks poor, works with Blender 2.76.11. $\endgroup$
    – JuhaW
    Mar 2, 2016 at 10:13
  • $\begingroup$ @JuhaW For collaborative work I thought it's a good idea to create a gist, instead of updating the answer continuously. I hope you like it. Thanks again. $\endgroup$
    – p2or
    Mar 15, 2016 at 11:05
  • $\begingroup$ How did you manage to offset the loaded OBJs? I can't see it in the example code. I could need it for importing an OBJ to a custom position and parent it. $\endgroup$
    – Ray
    Jul 14, 2016 at 18:09
  • 1
    $\begingroup$ sweet, 2.92+ works on 3.0.1, which is awesome $\endgroup$ Jan 29, 2022 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .