Allow for importing multiple files simultaneously

This commit is contained in:
Pyogenics
2025-03-25 17:26:29 +00:00
parent 34d40f70a2
commit a4d62b33e7
2 changed files with 27 additions and 20 deletions

View File

@@ -53,14 +53,13 @@ def mirrorUVY(uv):
return (x, 1-y) return (x, 1-y)
class A3DBlenderImporter: 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.modelData = modelData
self.directory = directory self.directory = directory
self.materials = [] self.materials = []
self.meshes = [] self.meshes = []
# User settings # User settings
self.create_collection = create_collection
self.reset_empty_transform = reset_empty_transform self.reset_empty_transform = reset_empty_transform
self.try_import_textures = try_import_textures self.try_import_textures = try_import_textures
@@ -78,18 +77,12 @@ class A3DBlenderImporter:
self.meshes.append(me) self.meshes.append(me)
# Create objects # 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 = [] objects = []
for objectData in self.modelData.objects: for objectData in self.modelData.objects:
ob = self.buildBlenderObject(objectData) ob = self.buildBlenderObject(objectData)
objects.append(ob) objects.append(ob)
# Assign object parents and link to collection # Assign object parents and link to collection
for obI, ob in enumerate(objects): for obI, ob in enumerate(objects):
collection.objects.link(ob)
# Assign parents # Assign parents
parentID = self.modelData.transformParentIDs[obI] parentID = self.modelData.transformParentIDs[obI]
if parentID == 0 and self.modelData.version < 3: if parentID == 0 and self.modelData.version < 3:
@@ -100,6 +93,8 @@ class A3DBlenderImporter:
continue continue
parentOB = objects[parentID] parentOB = objects[parentID]
ob.parent = parentOB ob.parent = parentOB
return objects
''' '''
Blender data builders Blender data builders

View File

@@ -21,13 +21,15 @@ SOFTWARE.
''' '''
import bpy import bpy
from bpy.types import Operator from bpy.types import Operator, OperatorFileListElement
from bpy.props import StringProperty, BoolProperty from bpy.props import StringProperty, BoolProperty, CollectionProperty
from bpy_extras.io_utils import ImportHelper from bpy_extras.io_utils import ImportHelper
from .A3D import A3D from .A3D import A3D
from .A3DBlenderImporter import A3DBlenderImporter from .A3DBlenderImporter import A3DBlenderImporter
from glob import glob
''' '''
Operators Operators
''' '''
@@ -39,6 +41,7 @@ class ImportA3D(Operator, ImportHelper):
filter_glob: StringProperty(default="*.a3d", options={'HIDDEN'}) filter_glob: StringProperty(default="*.a3d", options={'HIDDEN'})
directory: StringProperty(subtype='DIR_PATH', options={'HIDDEN'}) directory: StringProperty(subtype='DIR_PATH', options={'HIDDEN'})
files: CollectionProperty(type=OperatorFileListElement, options={"HIDDEN", "SKIP_SAVE"})
# User options # User options
create_collection: BoolProperty(name="Create collection", description="Create a collection to hold all the model objects", default=False) 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) return ImportHelper.invoke(self, context, event)
def execute(self, context): 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 # Import data into blender
print(f"Reading A3D data from {filepath}") modelImporter = A3DBlenderImporter(modelData, self.directory, self.reset_empty_transform, self.try_import_textures)
modelData = A3D() objects += modelImporter.importData()
with open(filepath, "rb") as file:
modelData.read(file) # Link objects to collection
collection = bpy.context.collection
# Import data into blender if self.create_collection:
modelImporter = A3DBlenderImporter(modelData, self.directory, self.create_collection, self.reset_empty_transform, self.try_import_textures) collection = bpy.data.collections.new("Collection")
modelImporter.importData() bpy.context.collection.children.link(collection)
for obI, ob in enumerate(objects):
collection.objects.link(ob)
return {"FINISHED"} return {"FINISHED"}