Correct transform parent reading and implement parent assignment

Thanks to tubixpvp for pointing out the error with transform parent reading
This commit is contained in:
Pyogenics
2025-03-19 14:28:04 +00:00
parent ac96886e46
commit f9de035859
3 changed files with 40 additions and 18 deletions

7
.gitignore vendored
View File

@@ -1,2 +1,7 @@
# Build and cache
__pycache__/
*.zip
*.zip
# Editor files
.venv/
.vscode/

View File

@@ -38,9 +38,12 @@ A3D model object
'''
class A3D:
def __init__(self):
self.version = 0
self.materials = []
self.meshes = []
self.transforms = {}
self.transforms = []
self.transformParentIDs = []
self.objects = []
'''
@@ -53,14 +56,14 @@ class A3D:
raise RuntimeError(f"Invalid A3D signature: {signature}")
# Read file version and read version specific data
version, _ = unpackStream("<2H", stream) # Likely major.minor version code
print(f"Reading A3D version {version}")
self.version, _ = unpackStream("<2H", stream) # Likely major.minor version code
print(f"Reading A3D version {self.version}")
if version == 1:
if self.version == 1:
self.readRootBlock1(stream)
elif version == 2:
elif self.version == 2:
self.readRootBlock2(stream)
elif version == 3:
elif self.version == 3:
self.readRootBlock3(stream)
'''
@@ -175,15 +178,14 @@ class A3D:
# Read data
print(f"Reading transform block with {transformCount} transforms")
transforms = []
for _ in range(transformCount):
transform = A3DObjects.A3DTransform()
transform.read2(stream)
transforms.append(transform)
# Read and assign transform ids
for transformI in range(transformCount):
transformID, = unpackStream("<I", stream)
self.transforms[transformID] = transforms[transformI]
self.transforms.append(transform)
# Read parent ids
for _ in range(transformCount):
parentID, = unpackStream("<i", stream)
self.transformParentIDs.append(parentID)
def readTransformBlock3(self, stream):
# Verify signature
@@ -197,11 +199,11 @@ class A3D:
for _ in range(transformCount):
transform = A3DObjects.A3DTransform()
transform.read3(stream)
transforms.append(transform)
# Read and assign transform ids
for transformI in range(transformCount):
transformID, = unpackStream("<I", stream)
self.transforms[transformI] = transforms[transformI] #XXX: The IDs seem to be incorrect and instead map to index?
self.transforms.append(transform)
# Read parent ids
for _ in range(transformCount):
parentID, = unpackStream("<i", stream)
self.transformParentIDs.append(parentID)
# Padding
padding = calculatePadding(length)

View File

@@ -78,9 +78,24 @@ class A3DBlenderImporter:
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:
# version 2 models use 0 to signify empty parent
continue
elif parentID == -1:
# version 3 models use -1 to signify empty parent
continue
parentOB = objects[parentID]
ob.parent = parentOB
'''
Blender data builders