Fix incorrect geometry importing, now triangles should be joined together

This commit is contained in:
Pyogenics
2025-03-22 21:49:49 +00:00
parent f9de035859
commit 34d40f70a2
2 changed files with 25 additions and 37 deletions

View File

@@ -48,6 +48,10 @@ def addImageTextureToMaterial(image, node_tree):
# Apply image # Apply image
if image != None: textureNode.image = image if image != None: textureNode.image = image
def mirrorUVY(uv):
x, y = uv
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, create_collection=True, reset_empty_transform=True, try_import_textures=True):
self.modelData = modelData self.modelData = modelData
@@ -133,49 +137,33 @@ class A3DBlenderImporter:
normal2 += vertexBuffer.data normal2 += vertexBuffer.data
# Add blender vertices # Add blender vertices
blenderVertexIndices = [] blenderCoordinates = []
blenderVertices = [] for coordinate in coordinates:
blenderUV1s = [] blenderCoordinates += coordinate # Blender doesn't like tuples
blenderUV2s = [] me.vertices.add(len(blenderCoordinates)//3)
me.vertices.foreach_set("co", blenderCoordinates)
# Aggregate submesh data and import
indices = []
for submesh in meshData.submeshes: for submesh in meshData.submeshes:
polygonCount = len(submesh.indices) // 3 indices += submesh.indices
me.vertices.add(polygonCount*3) me.loops.add(len(indices))
me.loops.add(polygonCount*3) me.loops.foreach_set("vertex_index", indices)
me.polygons.add(polygonCount) me.polygons.add(len(indices)//3)
me.polygons.foreach_set("loop_start", range(0, len(indices), 3))
for indexI in range(submesh.indexCount):
index = submesh.indices[indexI]
blenderVertexIndices.append(indexI)
blenderVertices += list(coordinates[index])
if len(uv1) != 0:
for indexI in range(submesh.indexCount):
index = submesh.indices[indexI]
x, y = uv1[index]
blenderUV1s.append((x, 1-y))
if len(uv2) != 0:
for indexI in range(submesh.indexCount):
index = submesh.indices[indexI]
x, y = uv2[index]
blenderUV2s.append((x, 1-y))
me.vertices.foreach_set("co", blenderVertices)
me.polygons.foreach_set("loop_start", range(0, len(blenderVertices)//3, 3))
me.loops.foreach_set("vertex_index", blenderVertexIndices)
# UVs # UVs
if len(uv1) != 0: if len(uv1) != 0:
uvData = me.uv_layers.new(name="UV1").data uvData = me.uv_layers.new(name="UV1").data
for polygonI, po in enumerate(me.polygons): for po in me.polygons:
indexI = polygonI * 3 uvData[po.loop_start].uv = mirrorUVY(uv1[indices[po.loop_start]])
uvData[po.loop_start].uv = blenderUV1s[blenderVertexIndices[indexI]] uvData[po.loop_start+1].uv = mirrorUVY(uv1[indices[po.loop_start+1]])
uvData[po.loop_start+1].uv = blenderUV1s[blenderVertexIndices[indexI+1]] uvData[po.loop_start+2].uv = mirrorUVY(uv1[indices[po.loop_start+2]])
uvData[po.loop_start+2].uv = blenderUV1s[blenderVertexIndices[indexI+2]]
if len(uv2) != 0: if len(uv2) != 0:
uvData = me.uv_layers.new(name="UV2").data uvData = me.uv_layers.new(name="UV2").data
for polygonI, po in enumerate(me.polygons): for po in me.polygons:
indexI = polygonI * 3 uvData[po.loop_start].uv = mirrorUVY(uv2[indices[po.loop_start]])
uvData[po.loop_start].uv = blenderUV2s[blenderVertexIndices[indexI]] uvData[po.loop_start+1].uv = mirrorUVY(uv2[indices[po.loop_start+1]])
uvData[po.loop_start+1].uv = blenderUV2s[blenderVertexIndices[indexI+1]] uvData[po.loop_start+2].uv = mirrorUVY(uv2[indices[po.loop_start+2]])
uvData[po.loop_start+2].uv = blenderUV2s[blenderVertexIndices[indexI+2]]
# Apply materials (version 2) # Apply materials (version 2)
faceIndexBase = 0 faceIndexBase = 0

View File

@@ -41,7 +41,7 @@ class ImportA3D(Operator, ImportHelper):
directory: StringProperty(subtype='DIR_PATH', options={'HIDDEN'}) directory: StringProperty(subtype='DIR_PATH', options={'HIDDEN'})
# User options # User options
create_collection: BoolProperty(name="Create collection", description="Create a collection to hold all the model objects", default=True) create_collection: BoolProperty(name="Create collection", description="Create a collection to hold all the model objects", default=False)
try_import_textures: BoolProperty(name="Search for textures", description="Automatically search for lightmap, track and wheel textures and attempt to apply them", default=True) try_import_textures: BoolProperty(name="Search for textures", description="Automatically search for lightmap, track and wheel textures and attempt to apply them", default=True)
reset_empty_transform: BoolProperty(name="Reset empty transforms", description="Reset rotation and scale if it is set to 0, more useful for version 2 models like props", default=True) reset_empty_transform: BoolProperty(name="Reset empty transforms", description="Reset rotation and scale if it is set to 0, more useful for version 2 models like props", default=True)