Преглед изворни кода

properly export texture names using material chair_texture prop or name

deltanedas пре 3 година
родитељ
комит
21329d158b
1 измењених фајлова са 71 додато и 44 уклоњено
  1. 71 44
      chair_exporter.py

+ 71 - 44
chair_exporter.py

@@ -1,13 +1,17 @@
 # Blender addon to export an object as a lib.rs/chair mesh file
+# To use, select object(s) and export with settings.
+# Texture name in the file will be either:
+# 1. custom "chair_texture" property on material
+# 2. the material's name
 # Based on the Raw mesh exporter distributed with blender
 
 bl_info = {
 	"name": "Chair Mesh Exporter",
 	"author": "deltanedas",
 	"blender": (3, 0, 0),
-	"location": "File > Import-Export > Chair Mesh (.chr)",
+	"location": "File > Export > Chair Mesh (.chr)",
 	# Crate verion 0.2.3
-	"version": (0, 2, 5),
+	"version": (0, 2, 6),
 	"category": "Import-Export"
 }
 
@@ -16,11 +20,11 @@ import math
 # For writing floats
 import struct
 
-def face_to_tris(store_colours, store_normals, store_uvs, face, mesh, matrix, vertices, vertex_ids, colours, colour_ids, indices):
-	if store_uvs:
+def face_to_tris(cfg, face, mesh, matrix, vertices, vertex_ids, colours, colour_ids, indices):
+	if cfg.export_uvs:
 		uvs = mesh.uv_layers.active.data
 
-	if store_colours:
+	if cfg.export_colours:
 		cols = mesh.vertex_colors.active.data
 
 	for n in range(3):
@@ -29,7 +33,7 @@ def face_to_tris(store_colours, store_normals, store_uvs, face, mesh, matrix, ve
 		vert = mesh.vertices[verti]
 		loop = mesh.loops[loopi]
 
-		if store_colours:
+		if cfg.export_colours:
 			col = (cols[loopi].color)[:]
 			# really inefficient but this is pyton so
 			colkey = str(col)
@@ -43,12 +47,12 @@ def face_to_tris(store_colours, store_normals, store_uvs, face, mesh, matrix, ve
 		else:
 			coli = None
 
-		if store_normals:
+		if cfg.export_normals:
 			normals = (loop.normal)[:]
 		else:
 			normals = None
 
-		if store_uvs:
+		if cfg.export_uvs:
 			uv = (uvs[loopi].uv)[:]
 		else:
 			uv = None
@@ -66,17 +70,18 @@ def face_to_tris(store_colours, store_normals, store_uvs, face, mesh, matrix, ve
 
 		indices.append(i)
 
-def process_scene(context, apply_modifiers, store_colours, store_normals, store_uvs):
+def process_scene(cfg, context):
 	colours = []
 	colour_ids = {}
-	# TODO: texture names
+	textures = []
+	texture_ids = {}
 	objects = []
 	indices = []
 
 	scene = context.scene
 	for obj in context.selected_objects:
 		me = None
-		if apply_modifiers or obj.type != "MESH":
+		if cfg.apply_modifiers or obj.type != "MESH":
 			try:
 				me = obj.to_mesh()
 			except:
@@ -92,15 +97,28 @@ def process_scene(context, apply_modifiers, store_colours, store_normals, store_
 		if not me.loop_triangles and me.polygons:
 			me.calc_loop_triangles()
 
-		if store_normals:
+		if cfg.export_normals:
 			me.calc_normals_split()
 
-		# TODO: find texture used by this object
+		# TODO: vertex groups split object into its own thing
+		if cfg.export_uvs:
+			# get texture name from custom property or use the material name
+			mat = obj.active_material
+			texture = mat["chair_texture"] or mat.name
+			if texture in texture_ids:
+				texturei = texture_ids[texture]
+			else:
+				texturei = len(textures)
+				textures.append(texture)
+				texture_ids[texture] = texturei
+		else:
+			texturei = None
+
 		vertices = []
 		vertex_ids = {}
 		matrix = obj.matrix_world.copy()
 		for face in me.loop_triangles:
-			face_to_tris(store_colours, store_normals, store_uvs, face, me, matrix, vertices, vertex_ids, colours, colour_ids, indices)
+			face_to_tris(cfg, face, me, matrix, vertices, vertex_ids, colours, colour_ids, indices)
 
 		min_pos = [
 			min(vertices, key=lambda v: v[0][0])[0][0],
@@ -117,13 +135,13 @@ def process_scene(context, apply_modifiers, store_colours, store_normals, store_
 			max_pos[1] - min_pos[1],
 			max_pos[2] - min_pos[2]
 		]
-		objects.append((vertices, scale, min_pos))
+		objects.append((vertices, scale, min_pos, texturei))
 
 		# Remove meshes that are created solely for exporting
 		if is_tmp_mesh:
 			obj.to_mesh_clear()
 
-	return colours, objects, indices
+	return colours, textures, objects, indices
 
 def index_size(vertices):
 	if vertices < 256:
@@ -183,34 +201,34 @@ def write_vec3_sin(f, v):
 	z = index_normal(v[1])
 	f.write(bytes([x, y, z]))
 
-def write_model(path, colours, objects, indices, export_colours, export_normals, export_uvs, export_skeleton, short_pos, short_normals, short_uvs, short_weights, flip_winding):
-	f = open(path, "wb")
+def write_model(cfg, colours, textures, objects, indices):
+	f = open(cfg.filepath, "wb")
 	# Chair file header
-	f.write(bytes("CHAIR", "ascii"))
+	f.write(b"CHAIR")
 	# Format version
 	f.write(bytes([0]))
 
 	# MeshFeatures
 	features = 0
-	if export_colours:
+	if cfg.export_colours:
 		features |= 0b0001
-	if export_normals:
+	if cfg.export_normals:
 		features |= 0b0010
-	if export_uvs:
+	if cfg.export_uvs:
 		features |= 0b0100
-	if export_skeleton:
+	if cfg.export_skeleton:
 		features |= 0b1000
-	if short_pos:
+	if cfg.short_pos:
 		features |= 0b00010000
-	if short_normals:
+	if cfg.short_normals:
 		features |= 0b00100000
-	if short_uvs:
+	if cfg.short_uvs:
 		features |= 0b01000000
-	if short_weights:
+	if cfg.short_weights:
 		features |= 0b10000000
 	f.write(bytes([features]))
 
-	if export_colours:
+	if cfg.export_colours:
 		f.write((len(colours)).to_bytes(2, "little"))
 		for col in colours:
 			for chan in col:
@@ -220,10 +238,19 @@ def write_model(path, colours, objects, indices, export_colours, export_normals,
 	else:
 		colour_size = 1
 
-	# TODO: export texture names
-	if export_uvs:
-		f.write(bytes([1, 5]))
-		f.write(bytes("chair", "ascii"))
+	if cfg.export_uvs:
+		if len(textures) > 255:
+			raise "Too many textures"
+
+		f.write(bytes([len(textures)]))
+		for texture in textures:
+			name = texture.encode("utf-8")
+			count = len(name)
+			if count > 255:
+				raise "Texture name " + texture + " is too long"
+
+			f.write(bytes([count]))
+			f.write(name)
 
 	# TODO: export skeleton
 
@@ -234,21 +261,22 @@ def write_model(path, colours, objects, indices, export_colours, export_normals,
 
 		scale = obj[1]
 		offset = obj[2]
-		if short_pos:
+		if cfg.short_pos:
 			write_vec3_float(f, scale)
 			write_vec3_float(f, offset)
 			write_pos = lambda f, v: write_vec3_byte(f, v, scale, offset)
 		else:
 			write_pos = write_vec3_float
 
-		if short_normals:
+		if cfg.short_normals:
 			write_normals = write_vec3_sin
 		else:
 			write_normals = write_vec3_float
 
-		if export_uvs:
-			f.write(bytes([0]))
-		if short_uvs:
+		if cfg.export_uvs:
+			texture = obj[3]
+			f.write(bytes([texture]))
+		if cfg.short_uvs:
 			write_uvs = write_vec2_byte
 		else:
 			write_uvs = write_vec2_float
@@ -256,18 +284,18 @@ def write_model(path, colours, objects, indices, export_colours, export_normals,
 		# Write data
 		for v in obj[0]:
 			write_pos(f, v[0])
-			if export_colours:
+			if cfg.export_colours:
 				f.write(v[1].to_bytes(colour_size, "little"))
-			if export_normals:
+			if cfg.export_normals:
 				write_normals(f, v[2])
-			if export_uvs:
+			if cfg.export_uvs:
 				write_uvs(f, v[3])
 			# TODO: bone weights
 
 	# Indices
 	f.write(len(indices).to_bytes(4, "little"))
 	indsize = index_size(sum([len(obj[0]) for obj in objects]))
-	if flip_winding:
+	if cfg.flip_winding:
 		for i in range(0, int(len(indices) / 3)):
 			n = i * 3
 			f.write(indices[n + 1].to_bytes(indsize, "little"))
@@ -357,9 +385,8 @@ class ModelExporter(Operator, ExportHelper):
 	)
 
 	def execute(self, context):
-		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, self.flip_winding)
+		colours, textures, objects, indices = process_scene(self, context)
+		write_model(self, colours, textures, objects, indices)
 		return {"FINISHED"}
 
 def menu_export(self, context):