|
@@ -1,5 +1,6 @@
|
|
|
# Blender addon to export an object as a lib.rs/chair mesh file
|
|
# Blender addon to export an object as a lib.rs/chair mesh file
|
|
|
# Based on the Raw mesh exporter distributed with blender
|
|
# 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 = {
|
|
bl_info = {
|
|
|
"name": "Chair Mesh Exporter",
|
|
"name": "Chair Mesh Exporter",
|
|
@@ -7,7 +8,7 @@ bl_info = {
|
|
|
"blender": (3, 0, 0),
|
|
"blender": (3, 0, 0),
|
|
|
"location": "File > Import-Export > Chair Mesh (.chr)",
|
|
"location": "File > Import-Export > Chair Mesh (.chr)",
|
|
|
# Same as the crate version
|
|
# Same as the crate version
|
|
|
- "version": (0, 2, 2),
|
|
|
|
|
|
|
+ "version": (0, 2, 3),
|
|
|
"category": "Import-Export"
|
|
"category": "Import-Export"
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -16,14 +17,27 @@ import math
|
|
|
# For writing floats
|
|
# For writing floats
|
|
|
import struct
|
|
import struct
|
|
|
|
|
|
|
|
-def face_to_tris(face, mesh, matrix, vertices, vertex_ids, indices):
|
|
|
|
|
|
|
+def face_to_tris(face, mesh, matrix, vertices, vertex_ids, colours, colour_ids, indices):
|
|
|
uvs = mesh.uv_layers.active.data
|
|
uvs = mesh.uv_layers.active.data
|
|
|
|
|
+ cols = mesh.vertex_colors.active.data
|
|
|
for n in range(3):
|
|
for n in range(3):
|
|
|
verti = face.vertices[n]
|
|
verti = face.vertices[n]
|
|
|
loopi = face.loops[n]
|
|
loopi = face.loops[n]
|
|
|
vert = mesh.vertices[verti]
|
|
vert = mesh.vertices[verti]
|
|
|
loop = mesh.loops[loopi]
|
|
loop = mesh.loops[loopi]
|
|
|
- v = [(matrix @ vert.co)[:], (loop.normal)[:], (uvs[loopi].uv)[:]]
|
|
|
|
|
|
|
+
|
|
|
|
|
+ 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
|
|
|
|
|
+
|
|
|
|
|
+ v = [(matrix @ vert.co)[:], coli, (loop.normal)[:], (uvs[loopi].uv)[:]]
|
|
|
# really inefficient but this is pyton so
|
|
# really inefficient but this is pyton so
|
|
|
key = str(v)
|
|
key = str(v)
|
|
|
if key in vertex_ids:
|
|
if key in vertex_ids:
|
|
@@ -37,6 +51,9 @@ def face_to_tris(face, mesh, matrix, vertices, vertex_ids, indices):
|
|
|
indices.append(i)
|
|
indices.append(i)
|
|
|
|
|
|
|
|
def process_scene(context, apply_modifiers):
|
|
def process_scene(context, apply_modifiers):
|
|
|
|
|
+ colours = []
|
|
|
|
|
+ colour_ids = {}
|
|
|
|
|
+ # TODO: texture names
|
|
|
objects = []
|
|
objects = []
|
|
|
indices = []
|
|
indices = []
|
|
|
|
|
|
|
@@ -59,12 +76,12 @@ def process_scene(context, apply_modifiers):
|
|
|
if not me.loop_triangles and me.polygons:
|
|
if not me.loop_triangles and me.polygons:
|
|
|
me.calc_loop_triangles()
|
|
me.calc_loop_triangles()
|
|
|
|
|
|
|
|
- # TODO: texture used by this object
|
|
|
|
|
|
|
+ # TODO: find texture used by this object
|
|
|
vertices = []
|
|
vertices = []
|
|
|
vertex_ids = {}
|
|
vertex_ids = {}
|
|
|
matrix = obj.matrix_world.copy()
|
|
matrix = obj.matrix_world.copy()
|
|
|
for face in me.loop_triangles:
|
|
for face in me.loop_triangles:
|
|
|
- face_to_tris(face, me, matrix, vertices, vertex_ids, indices)
|
|
|
|
|
|
|
+ face_to_tris(face, me, matrix, vertices, vertex_ids, colours, colour_ids, indices)
|
|
|
|
|
|
|
|
min_pos = [
|
|
min_pos = [
|
|
|
min(vertices, key=lambda v: v[0][0])[0][0],
|
|
min(vertices, key=lambda v: v[0][0])[0][0],
|
|
@@ -87,7 +104,7 @@ def process_scene(context, apply_modifiers):
|
|
|
if is_tmp_mesh:
|
|
if is_tmp_mesh:
|
|
|
obj.to_mesh_clear()
|
|
obj.to_mesh_clear()
|
|
|
|
|
|
|
|
- return objects, indices
|
|
|
|
|
|
|
+ return colours, objects, indices
|
|
|
|
|
|
|
|
def index_size(vertices):
|
|
def index_size(vertices):
|
|
|
if vertices < 256:
|
|
if vertices < 256:
|
|
@@ -142,7 +159,7 @@ def write_vec3_sin(f, v):
|
|
|
z = index_normal(v[1])
|
|
z = index_normal(v[1])
|
|
|
f.write(bytes([x, y, z]))
|
|
f.write(bytes([x, y, z]))
|
|
|
|
|
|
|
|
-def write_model(path, objects, indices, export_colours, export_normals, export_uvs, export_skeleton, short_pos, short_normals, short_uvs, short_weights):
|
|
|
|
|
|
|
+def write_model(path, colours, objects, indices, export_colours, export_normals, export_uvs, export_skeleton, short_pos, short_normals, short_uvs, short_weights):
|
|
|
f = open(path, "wb")
|
|
f = open(path, "wb")
|
|
|
# Chair file header
|
|
# Chair file header
|
|
|
f.write(bytes("CHAIR", "ascii"))
|
|
f.write(bytes("CHAIR", "ascii"))
|
|
@@ -169,11 +186,12 @@ def write_model(path, objects, indices, export_colours, export_normals, export_u
|
|
|
features |= 0b10000000
|
|
features |= 0b10000000
|
|
|
f.write(bytes([features]))
|
|
f.write(bytes([features]))
|
|
|
|
|
|
|
|
- # TODO: export unique vertex colours from objects
|
|
|
|
|
if export_colours:
|
|
if export_colours:
|
|
|
- f.write((1).to_bytes(2, "little"))
|
|
|
|
|
- f.write(bytes([255, 255, 255, 255]))
|
|
|
|
|
- if 1 > 255:
|
|
|
|
|
|
|
+ f.write((len(colours)).to_bytes(2, "little"))
|
|
|
|
|
+ for col in colours:
|
|
|
|
|
+ for chan in col:
|
|
|
|
|
+ f.write(bytes([int(chan * 255)]))
|
|
|
|
|
+ if len(colours) > 255:
|
|
|
colour_size = 2
|
|
colour_size = 2
|
|
|
else:
|
|
else:
|
|
|
colour_size = 1
|
|
colour_size = 1
|
|
@@ -205,8 +223,7 @@ def write_model(path, objects, indices, export_colours, export_normals, export_u
|
|
|
write_normals = write_vec3_float
|
|
write_normals = write_vec3_float
|
|
|
|
|
|
|
|
if export_uvs:
|
|
if export_uvs:
|
|
|
- # TODO: actual texture index
|
|
|
|
|
- f.write((0).to_bytes(colour_size, "little"))
|
|
|
|
|
|
|
+ f.write(bytes([0]))
|
|
|
if short_uvs:
|
|
if short_uvs:
|
|
|
write_uvs = write_vec2_byte
|
|
write_uvs = write_vec2_byte
|
|
|
else:
|
|
else:
|
|
@@ -216,12 +233,11 @@ def write_model(path, objects, indices, export_colours, export_normals, export_u
|
|
|
for v in obj[0]:
|
|
for v in obj[0]:
|
|
|
write_pos(f, v[0])
|
|
write_pos(f, v[0])
|
|
|
if export_colours:
|
|
if export_colours:
|
|
|
- # TODO: colour index
|
|
|
|
|
- f.write(bytes([0]))
|
|
|
|
|
|
|
+ f.write(v[1].to_bytes(colour_size, "little"))
|
|
|
if export_normals:
|
|
if export_normals:
|
|
|
- write_normals(f, v[1])
|
|
|
|
|
|
|
+ write_normals(f, v[2])
|
|
|
if export_uvs:
|
|
if export_uvs:
|
|
|
- write_uvs(f, v[2])
|
|
|
|
|
|
|
+ write_uvs(f, v[3])
|
|
|
# TODO: bone weights
|
|
# TODO: bone weights
|
|
|
|
|
|
|
|
# Indices
|
|
# Indices
|
|
@@ -304,8 +320,8 @@ class ModelExporter(Operator, ExportHelper):
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
def execute(self, context):
|
|
|
- objects, indices = process_scene(context, self.apply_modifiers)
|
|
|
|
|
- write_model(self.filepath, objects, indices, self.export_colours, self.export_normals, self.export_uvs, self.export_skeleton,
|
|
|
|
|
|
|
+ colours, objects, indices = process_scene(context, self.apply_modifiers)
|
|
|
|
|
+ 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.short_pos, self.short_normals, self.short_uvs, self.short_weights)
|
|
|
return {"FINISHED"}
|
|
return {"FINISHED"}
|
|
|
|
|
|