From 1bc1cf8cd2c66bbae615a5ddba883b7cd55bd67f Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 8 Feb 2026 07:00:28 +0100 Subject: feat(3d): Implement Blender export and binary scene loading pipeline --- tools/asset_packer.cc | 9 ++++ tools/blender_export.py | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tools/blender_export.py (limited to 'tools') diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc index 32742bd..42dfa7a 100644 --- a/tools/asset_packer.cc +++ b/tools/asset_packer.cc @@ -127,6 +127,7 @@ int main(int argc, char* argv[]) { fprintf( assets_data_cc_file, "// This file is auto-generated by asset_packer.cc. Do not edit.\n\n"); + fprintf(assets_data_cc_file, "#include \n"); fprintf(assets_data_cc_file, "#include \"util/asset_manager.h\"\n"); fprintf(assets_data_cc_file, "#include \"%s\"\n", generated_header_name.c_str()); @@ -499,6 +500,14 @@ int main(int argc, char* argv[]) { fprintf(assets_data_cc_file, " return %zu;\n", asset_build_infos.size()); fprintf(assets_data_cc_file, "}\n\n"); + fprintf(assets_data_cc_file, "AssetId GetAssetIdByName(const char* name) {\n"); + for (const auto& info : asset_build_infos) { + fprintf(assets_data_cc_file, " if (std::strcmp(name, \"%s\") == 0) return AssetId::ASSET_%s;\n", + info.name.c_str(), info.name.c_str()); + } + fprintf(assets_data_cc_file, " return AssetId::ASSET_LAST_ID;\n"); + fprintf(assets_data_cc_file, "}\n\n"); + std::fclose(assets_data_cc_file); printf("Asset packer successfully generated records for %zu assets.\n", diff --git a/tools/blender_export.py b/tools/blender_export.py new file mode 100644 index 0000000..da7b986 --- /dev/null +++ b/tools/blender_export.py @@ -0,0 +1,117 @@ +import bpy +import struct +import os + +# Output format: +# Header: +# char[4] magic = "SCN1" +# uint32_t num_objects +# uint32_t num_cameras (reserved) +# uint32_t num_lights (reserved) +# +# Object Block: +# char[64] name +# uint32_t type (0=CUBE, 1=SPHERE, 2=PLANE, 3=TORUS, 4=BOX, 5=SKYBOX, 6=MESH) +# vec3 position +# quat rotation (x, y, z, w) +# vec3 scale +# vec4 color +# uint32_t mesh_name_len +# char[] mesh_name (if type == MESH) +# float mass +# float restitution +# uint32_t is_static (bool) + +def export_scene(filepath): + print(f"Exporting scene to {filepath}...") + + objects = [obj for obj in bpy.context.scene.objects if obj.visible_get() and obj.type == 'MESH'] + + with open(filepath, 'wb') as f: + # Header + f.write(b'SCN1') + f.write(struct.pack(' + mesh_asset_name = "MESH_" + obj.name.upper().replace('.', '_') + + name_len = len(mesh_asset_name) + f.write(struct.pack(' 0: + f.write(mesh_asset_name.encode('utf-8')) + + # Physics properties (from custom properties or defaults) + mass = obj.get('mass', 1.0) + restitution = obj.get('restitution', 0.5) + is_static = obj.get('is_static', 0) + + f.write(struct.pack('