Files
dusk/tools/asset/dmf/__main__.py
T

64 lines
1.5 KiB
Python

# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
"""
Writes DMF (Dusk Mesh Format) files.
DMF format:
Bytes 0-3: DMF\x00 (magic)
Bytes 4-7: uint32_t version = 1 (little-endian)
Bytes 8-11: uint32_t vertCount (little-endian)
Bytes 12+: meshvertex_t vertices[vertCount]
Each vertex is 20 bytes: uv[2] + pos[3] (5 floats, LE)
Usage:
python3 -m tools.asset.dmf <output.dmf> <vertices.bin>
Reads raw vertex bytes from vertices.bin and writes a DMF file.
"""
import argparse
import struct
import os
MAGIC = b'DMF\x00'
VERSION = 1
VERTEX_SIZE = 20
def write_dmf(path, vertex_bytes):
if len(vertex_bytes) % VERTEX_SIZE != 0:
raise ValueError(
f"Vertex data size {len(vertex_bytes)} is not a "
f"multiple of {VERTEX_SIZE}"
)
vert_count = len(vertex_bytes) // VERTEX_SIZE
buf = bytearray()
buf += MAGIC
buf += struct.pack('<I', VERSION)
buf += struct.pack('<I', vert_count)
buf += vertex_bytes
with open(path, 'wb') as f:
f.write(buf)
print(
f'Wrote {path}: {vert_count} vertices, {len(buf)} bytes'
)
return vert_count
def main():
parser = argparse.ArgumentParser(description="Write a DMF (Dusk Mesh Format) file")
parser.add_argument('output', help='Path to output .dmf file')
parser.add_argument('vertices', help='Path to raw vertex bytes file')
args = parser.parse_args()
with open(args.vertices, 'rb') as f:
vertex_bytes = f.read()
write_dmf(args.output, vertex_bytes)
if __name__ == '__main__':
main()