Explorar el Código

fix possible deduplication issues

deltanedas hace 3 años
padre
commit
e3f7bc26f7
Se han modificado 1 ficheros con 35 adiciones y 19 borrados
  1. 35 19
      chair_exporter.py

+ 35 - 19
chair_exporter.py

@@ -1,14 +1,13 @@
 # Blender addon to export an object as a lib.rs/chair mesh file
 # Based on the Raw mesh exporter distributed with blender
-# FIXME: Currently might produce bloated mesh if not all features are used, the deduplication doesn't take what will actually be used into account
 
 bl_info = {
 	"name": "Chair Mesh Exporter",
 	"author": "deltanedas",
 	"blender": (3, 0, 0),
 	"location": "File > Import-Export > Chair Mesh (.chr)",
-	# Same as the crate version
-	"version": (0, 2, 3),
+	# Crate verion 0.2.3
+	"version": (0, 2, 4),
 	"category": "Import-Export"
 }
 
@@ -17,27 +16,44 @@ import math
 # For writing floats
 import struct
 
-def face_to_tris(face, mesh, matrix, vertices, vertex_ids, colours, colour_ids, indices):
-	uvs = mesh.uv_layers.active.data
-	cols = mesh.vertex_colors.active.data
+def face_to_tris(store_colours, store_normals, store_uvs, face, mesh, matrix, vertices, vertex_ids, colours, colour_ids, indices):
+	if store_uvs:
+		uvs = mesh.uv_layers.active.data
+
+	if store_colours:
+		cols = mesh.vertex_colors.active.data
+
 	for n in range(3):
 		verti = face.vertices[n]
 		loopi = face.loops[n]
 		vert = mesh.vertices[verti]
 		loop = mesh.loops[loopi]
 
-		col = (cols[loopi].color)[:]
-		# really inefficient but this is pyton so
-		colkey = str(col)
-		if colkey in colour_ids:
-			coli = colour_ids[colkey]
+		if store_colours:
+			col = (cols[loopi].color)[:]
+			# really inefficient but this is pyton so
+			colkey = str(col)
+			if colkey in colour_ids:
+				coli = colour_ids[colkey]
+			else:
+				# new colour, push to list
+				coli = len(colours)
+				colours.append(col)
+				colour_ids[colkey] = coli
+		else:
+			coli = None
+
+		if store_normals:
+			normals = (loop.normal)[:]
+		else:
+			normals = None
+
+		if store_uvs:
+			uv = (uvs[loopi].uv)[:]
 		else:
-			# new colour, push to list
-			coli = len(colours)
-			colours.append(col)
-			colour_ids[colkey] = coli
+			uv = None
 
-		v = [(matrix @ vert.co)[:], coli, (loop.normal)[:], (uvs[loopi].uv)[:]]
+		v = [(matrix @ vert.co)[:], coli, normals, uv]
 		# really inefficient but this is pyton so
 		key = str(v)
 		if key in vertex_ids:
@@ -50,7 +66,7 @@ def face_to_tris(face, mesh, matrix, vertices, vertex_ids, colours, colour_ids,
 
 		indices.append(i)
 
-def process_scene(context, apply_modifiers):
+def process_scene(context, apply_modifiers, store_colours, store_normals, store_uvs):
 	colours = []
 	colour_ids = {}
 	# TODO: texture names
@@ -81,7 +97,7 @@ def process_scene(context, apply_modifiers):
 		vertex_ids = {}
 		matrix = obj.matrix_world.copy()
 		for face in me.loop_triangles:
-			face_to_tris(face, me, matrix, vertices, vertex_ids, colours, colour_ids, indices)
+			face_to_tris(store_colours, store_normals, store_uvs, face, me, matrix, vertices, vertex_ids, colours, colour_ids, indices)
 
 		min_pos = [
 			min(vertices, key=lambda v: v[0][0])[0][0],
@@ -320,7 +336,7 @@ class ModelExporter(Operator, ExportHelper):
 	)
 
 	def execute(self, context):
-		colours, objects, indices = process_scene(context, self.apply_modifiers)
+		colours, objects, indices = process_scene(context, self.apply_modifiers, self.export_colours, self.export_normals, self.export_uvs)
 		write_model(self.filepath, colours, objects, indices, self.export_colours, self.export_normals, self.export_uvs, self.export_skeleton,
 			self.short_pos, self.short_normals, self.short_uvs, self.short_weights)
 		return {"FINISHED"}