Similar to Import STL via python, you can use the ImportHelper
class if you'd like to import and modify objects. Have a look into the Operator File Import template that comes with Blender.
In order to join the objects, see @batFINGERs answer on: How to join objects with Python? How to import multiple objects using the file browser, you can find here: How to batch import Wavefront OBJ files?
# ##### 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": "Custom Batch Import Stl (.stl)",
"author": "brockmann",
"version": (0, 1, 0),
"blender": (2, 80, 0),
"location": "File > Import-Export",
"description": "Import Stl files and join them",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"}
import bpy
import os
from bpy_extras.io_utils import ImportHelper
from bpy.types import PropertyGroup
from bpy.props import StringProperty, CollectionProperty
class ImportSomeData(bpy.types.Operator, ImportHelper):
"""Batch Import Stl files and join them"""
bl_idname = "import_scene.custom_stls"
bl_label = "Import multiple OBJ's"
bl_options = {'PRESET', 'UNDO'}
# ImportHelper mixin class uses this
filename_ext = ".stl"
filter_glob: StringProperty(
default="*.stl",
options={'HIDDEN'},
)
# Selected files
files: CollectionProperty(type=PropertyGroup)
def execute(self, context):
# Get the folder
folder = os.path.dirname(self.filepath)
obs = []
# Iterate through the selected files
for i in self.files:
# Generate full path to file
path_to_file = (os.path.join(folder, i.name))
bpy.ops.import_mesh.stl(filepath=path_to_file)
# Append Object(s) to the list
obs.append(context.selected_objects[:])
# Print the imported object reference
print ("Imported object:", context.object)
# Join the objects based on:
# https://blender.stackexchange.com/a/133024/
obs = [x for sublist in obs for x in sublist]
c = {}
c["object"] = c["active_object"] = context.object
c["selected_objects"] = c["selected_editable_objects"] = obs
bpy.ops.object.join(c)
return {'FINISHED'}
# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ImportSomeData.bl_idname, text="Stl Batch & Join (.stl)")
def register():
bpy.utils.register_class(ImportSomeData)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(ImportSomeData)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
if __name__ == "__main__":
register()
# test call
bpy.ops.import_scene.custom_stls('INVOKE_DEFAULT')
Note: If you get paid for that and my code got your job done, please consider making a donation here: https://www.blender.org/foundation/donation-payment/ to make up for that.