diff --git a/io_scene_a3d/A3DBlenderImporter.py b/io_scene_a3d/A3DBlenderImporter.py index f1da48d..36a1962 100644 --- a/io_scene_a3d/A3DBlenderImporter.py +++ b/io_scene_a3d/A3DBlenderImporter.py @@ -53,14 +53,13 @@ def mirrorUVY(uv): return (x, 1-y) class A3DBlenderImporter: - def __init__(self, modelData, directory, create_collection=True, reset_empty_transform=True, try_import_textures=True): + def __init__(self, modelData, directory, reset_empty_transform=True, try_import_textures=True): self.modelData = modelData self.directory = directory self.materials = [] self.meshes = [] # User settings - self.create_collection = create_collection self.reset_empty_transform = reset_empty_transform self.try_import_textures = try_import_textures @@ -78,18 +77,12 @@ class A3DBlenderImporter: self.meshes.append(me) # Create objects - collection = bpy.context.collection # By default use the current active collection - if self.create_collection: - collection = bpy.data.collections.new("Object") - bpy.context.collection.children.link(collection) objects = [] for objectData in self.modelData.objects: ob = self.buildBlenderObject(objectData) objects.append(ob) # Assign object parents and link to collection for obI, ob in enumerate(objects): - collection.objects.link(ob) - # Assign parents parentID = self.modelData.transformParentIDs[obI] if parentID == 0 and self.modelData.version < 3: @@ -100,6 +93,8 @@ class A3DBlenderImporter: continue parentOB = objects[parentID] ob.parent = parentOB + + return objects ''' Blender data builders diff --git a/io_scene_a3d/__init__.py b/io_scene_a3d/__init__.py index 1b72f19..98356fc 100644 --- a/io_scene_a3d/__init__.py +++ b/io_scene_a3d/__init__.py @@ -21,13 +21,15 @@ SOFTWARE. ''' import bpy -from bpy.types import Operator -from bpy.props import StringProperty, BoolProperty +from bpy.types import Operator, OperatorFileListElement +from bpy.props import StringProperty, BoolProperty, CollectionProperty from bpy_extras.io_utils import ImportHelper from .A3D import A3D from .A3DBlenderImporter import A3DBlenderImporter +from glob import glob + ''' Operators ''' @@ -39,6 +41,7 @@ class ImportA3D(Operator, ImportHelper): filter_glob: StringProperty(default="*.a3d", options={'HIDDEN'}) directory: StringProperty(subtype='DIR_PATH', options={'HIDDEN'}) + files: CollectionProperty(type=OperatorFileListElement, options={"HIDDEN", "SKIP_SAVE"}) # User options create_collection: BoolProperty(name="Create collection", description="Create a collection to hold all the model objects", default=False) @@ -52,17 +55,26 @@ class ImportA3D(Operator, ImportHelper): return ImportHelper.invoke(self, context, event) def execute(self, context): - filepath = self.filepath + objects = [] + for file in self.files: + filepath = self.directory + file.name + # Read the file + print(f"Reading A3D data from {filepath}") + modelData = A3D() + with open(filepath, "rb") as file: + modelData.read(file) - # Read the file - print(f"Reading A3D data from {filepath}") - modelData = A3D() - with open(filepath, "rb") as file: - modelData.read(file) - - # Import data into blender - modelImporter = A3DBlenderImporter(modelData, self.directory, self.create_collection, self.reset_empty_transform, self.try_import_textures) - modelImporter.importData() + # Import data into blender + modelImporter = A3DBlenderImporter(modelData, self.directory, self.reset_empty_transform, self.try_import_textures) + objects += modelImporter.importData() + + # Link objects to collection + collection = bpy.context.collection + if self.create_collection: + collection = bpy.data.collections.new("Collection") + bpy.context.collection.children.link(collection) + for obI, ob in enumerate(objects): + collection.objects.link(ob) return {"FINISHED"}