Compress blender file, save Exporter script from blender

main
parent 560e9d8db7
commit 6531db13d6

@ -1,6 +1,7 @@
import bpy import bpy
import bmesh import bmesh
import os import os
import shutil
import struct import struct
from mathutils import *; from math import * from mathutils import *; from math import *
from bpy_extras.io_utils import (axis_conversion) from bpy_extras.io_utils import (axis_conversion)
@ -8,18 +9,33 @@ from bpy_extras.io_utils import (axis_conversion)
C = bpy.context C = bpy.context
D = bpy.data D = bpy.data
LEVEL_EXPORT_NAME = "level"
EXPORT_DIRECTORY = "exported" EXPORT_DIRECTORY = "exported"
if not os.path.exists(bpy.path.abspath(f"//{EXPORT_DIRECTORY}")): print("\n\nLet's get it started")
os.makedirs(bpy.path.abspath(f"//{EXPORT_DIRECTORY}"))
if os.path.exists(bpy.path.abspath(f"//{EXPORT_DIRECTORY}")):
shutil.rmtree(bpy.path.abspath(f"//{EXPORT_DIRECTORY}"))
os.makedirs(bpy.path.abspath(f"//{EXPORT_DIRECTORY}"))
def write_f32(f, number: float): def write_f32(f, number: float):
f.write(bytes(struct.pack("f", number))) f.write(bytes(struct.pack("f", number)))
def write_u64(f, number: int): def write_u64(f, number: int):
f.write(bytes(struct.pack("Q", number))) f.write(bytes(struct.pack("Q", number)))
def write_string(f, s: str):
encoded = s.encode("utf8")
write_u64(f, len(encoded))
f.write(encoded)
# for the level.bin
level_object_data = []
collision_cubes = []
saved_meshes = set()
for o in D.objects: for o in D.objects:
mapping = axis_conversion( mapping = axis_conversion(
from_forward = "Y", from_forward = "Y",
@ -27,34 +43,80 @@ for o in D.objects:
to_forward = "-Z", to_forward = "-Z",
to_up = "Y", to_up = "Y",
) )
mapping.resize_4x4() mesh_name = o.to_mesh().name # use this over o.name so instanced objects which refer to the same mesh, both use the same serialized mesh.
print(mapping)
output_filepath = bpy.path.abspath(f"//{EXPORT_DIRECTORY}/{o.name}.bin") if o.users_collection[0].name == 'Level' and mesh_name == "CollisionCube":
print(f"\n\nExporting {output_filepath}") collision_cubes.append((o.location, o.dimensions))
with open(output_filepath, "wb") as f: else:
bm = bmesh.new() if o.users_collection[0].name == 'Level':
mesh = o.to_mesh() print(f"Object {o.name} has mesh name {o.to_mesh().name}")
bm.from_mesh(mesh) assert(o.rotation_euler.order == 'XYZ')
bmesh.ops.triangulate(bm, faces=bm.faces) level_object_data.append((mesh_name, mapping @ o.location, o.rotation_euler, o.scale))
bm.transform(mapping) if mesh_name in saved_meshes:
bm.to_mesh(mesh) continue
saved_meshes.add(mesh_name)
vertices = []
mapping.resize_4x4()
for polygon in mesh.polygons: assert(mesh_name != LEVEL_EXPORT_NAME)
if len(polygon.loop_indices) == 3: output_filepath = bpy.path.abspath(f"//{EXPORT_DIRECTORY}/{mesh_name}.bin")
for loopIndex in polygon.loop_indices: print(f"Exporting {output_filepath}")
loop = mesh.loops[loopIndex] with open(output_filepath, "wb") as f:
position = mesh.vertices[loop.vertex_index].undeformed_co bm = bmesh.new()
uv = mesh.uv_layers.active.data[loop.index].uv mesh = o.to_mesh()
normal = loop.normal bm.from_mesh(mesh)
bmesh.ops.triangulate(bm, faces=bm.faces)
vertices.append(position) bm.transform(mapping)
bm.to_mesh(mesh)
write_u64(f, len(vertices))
for v in vertices:
write_f32(f, v.x) vertices = []
write_f32(f, v.y)
write_f32(f, v.z) for polygon in mesh.polygons:
print(f"Wrote {len(vertices)} vertices") if len(polygon.loop_indices) == 3:
for loopIndex in polygon.loop_indices:
loop = mesh.loops[loopIndex]
position = mesh.vertices[loop.vertex_index].undeformed_co
uv = mesh.uv_layers.active.data[loop.index].uv
normal = loop.normal
vertices.append((position, uv))
write_u64(f, len(vertices))
for v_and_uv in vertices:
v, uv = v_and_uv
write_f32(f, v.x)
write_f32(f, v.y)
write_f32(f, v.z)
write_f32(f, uv.x)
write_f32(f, uv.y)
print(f"Wrote {len(vertices)} vertices")
with open(bpy.path.abspath(f"//{EXPORT_DIRECTORY}/{LEVEL_EXPORT_NAME}.bin"), "wb") as f:
write_u64(f, len(level_object_data))
for o in level_object_data:
name, v, rotation, scale = o
print(f"Writing instanced object {name}")
write_string(f, name)
write_f32(f, v.x)
write_f32(f, v.y)
write_f32(f, v.z)
# rotation fields different because y+ is up now
write_f32(f, rotation.x)
write_f32(f, rotation.y)
write_f32(f, rotation.z)
write_f32(f, scale.x)
write_f32(f, scale.y)
write_f32(f, scale.z)
write_u64(f, len(collision_cubes))
for c in collision_cubes:
blender_pos, blender_dims = c
write_f32(f, blender_pos.x)
write_f32(f, -blender_pos.y)
write_f32(f, blender_dims.x)
write_f32(f, blender_dims.y)
assert(blender_dims.x > 0.0)
assert(blender_dims.y > 0.0)

BIN
art/art.blend (Stored with Git LFS)

Binary file not shown.
Loading…
Cancel
Save