Creating a Blender addon to import a batch of object files from a selected folder requires some understanding of Blender's Python API. Below is a simple example of an addon that accomplishes this task.
Blender Addon Code
bl_info = {
"name": "Batch OBJ Importer",
"blender": (2, 82, 0), # Change according to your Blender version
"category": "Import-Export",
}
import bpy
import os
class OBJECT_OT_import_obj_batch(bpy.types.Operator):
"""Import a batch of OBJ files from a selected folder"""
bl_idname = "import_test.obj_batch"
bl_label = "Import OBJ Files Batch"
bl_options = {'REGISTER', 'UNDO'}
directory: str = bpy.props.StringProperty(subtype='DIR_PATH')
def execute(self, context):
if not self.directory:
self.report({'WARNING'}, "No directory selected")
return {'CANCELLED'}
# Get a list of all .obj files in the selected directory
obj_files = [f for f in os.listdir(self.directory) if f.lower().endswith('.obj')]
for obj_file in obj_files:
file_path = os.path.join(self.directory, obj_file)
bpy.ops.import_scene.obj(filepath=file_path)
self.report({'INFO'}, f"Imported {len(obj_files)} OBJ files.")
return {'FINISHED'}
class OBJECT_PT_import_obj_batch(bpy.types.Panel):
"""Panel to trigger OBJ Batch Import"""
bl_label = "Batch OBJ Import"
bl_idname = "OBJECT_PT_batch_obj_import"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
# Draw a button for batch import
layout.operator(OBJECT_OT_import_obj_batch.bl_idname)
class OBJECT_MT_import_obj_batch(bpy.types.Menu):
"""Add a menu item to the File -> Import menu"""
bl_idname = "OBJECT_MT_import_obj_batch"
bl_label = "Batch OBJ Import"
def draw(self, context):
layout = self.layout
layout.operator(OBJECT_OT_import_obj_batch.bl_idname)
def register():
bpy.utils.register_class(OBJECT_OT_import_obj_batch)
bpy.utils.register_class(OBJECT_PT_import_obj_batch)
bpy.utils.register_class(OBJECT_MT_import_obj_batch)
bpy.types.TOPBAR_MT_file_import.append(lambda self, context: self.layout.menu(OBJECT_MT_import_obj_batch.bl_idname))
def unregister():
bpy.utils.unregister_class(OBJECT_OT_import_obj_batch)
bpy.utils.unregister_class(OBJECT_PT_import_obj_batch)
bpy.utils.unregister_class(OBJECT_MT_import_obj_batch)
bpy.types.TOPBAR_MT_file_import.remove(lambda self, context: self.layout.menu(OBJECT_MT_import_obj_batch.bl_idname))
if __name__ == "__main__":
register()
Steps to Use the Addon
-
Save the Code: Copy the above code into a new Python file and save it with a .py extension (e.g., batch_obj_importer.py).
-
Install the Addon:
- Open Blender and go to
Edit > Preferences.
- In the Preferences window, go to the
Add-ons tab and click Install....
- Select your saved Python file and click
Install Add-on.
-
Enable the Addon: After installation, make sure to enable the addon by checking the checkbox next to its name.
-
Using the Addon:
- Navigate to the
File menu, then to Import, and select Batch OBJ Import.
- A dialogue will open allowing you to select the folder containing your OBJ files.
- After selecting the folder, the addon will import all the OBJ files found in that folder.
Important Notes
- Make sure to adjust the
blender version in the bl_info dictionary as per your version of Blender.
- The code here assumes you have a basic understanding of how to use Blender's interface.
- The addon imports OBJ files directly into the scene, so keep track of your object names to avoid duplicates or overwrites.