Files
models/fbx/writing.sakura
T

240 lines
8.7 KiB
Plaintext
Raw Normal View History

2026-10-30 12:04:54 -05:00
#! Contains writing a document as binary FBX, `encode` and `save`, and making
#! properties to build one by hand. Arrays are written as they are, not
#! compressed, which FBX allows; nodes end with a record of zeros where they
#! have children, and where Blender's writer finds Autodesk's reader wants one,
#! and the file with the footer Autodesk's writer gives.
import std::io;
import std::math;
module malunal::fbx;
#! A property of a boolean.
pub def property::of_bool(value: bool): property {
return property{ kind = property_kind::boolean, integer = value ? 1 : 0 };
}
#! A property of a 32-bit integer.
pub def property::of_int32(value: i32): property {
return property{ kind = property_kind::int32, integer = value as i64 };
}
#! A property of a 64-bit integer, as an object's id.
pub def property::of_int64(value: i64): property {
return property{ kind = property_kind::int64, integer = value };
}
#! A property of a 32-bit float.
pub def property::of_float32(value: f32): property {
return property{ kind = property_kind::float32, real = value as f64 };
}
#! A property of a 64-bit float.
pub def property::of_float64(value: f64): property {
return property{ kind = property_kind::float64, real = value };
}
#! A property of text.
pub def property::of_text(value: strview): property {
var made := property{ kind = property_kind::text };
made.bytes.extend(value);
return made;
}
#! A property of bytes, as an embedded file's.
pub def property::of_bytes(value: span<u8>): property {
var made := property{ kind = property_kind::raw };
made.bytes.extend(value);
return made;
}
#! A property of an array of integers, of a kind: `booleans`, `int32s`, or
#! `int64s`.
pub def property::of_integers(kind: property_kind, values: span<i64>): property {
var made := property{ kind = kind };
made.integers.extend(values);
return made;
}
#! A property of an array of floats, of a kind: `float32s` or `float64s`.
pub def property::of_reals(kind: property_kind, values: span<f64>): property {
var made := property{ kind = kind };
made.reals.extend(values);
return made;
}
# The footer's first 16 bytes, and its last, as Autodesk's writer gives them.
const footer_id: u8[16] = [0xFA, 0xBC, 0xAB, 0x09, 0xD0, 0xC8, 0xD4, 0x66, 0xB1, 0x76, 0xFB, 0x83, 0x1C, 0xF7, 0x26, 0x7E];
const footer_magic: u8[16] = [0xF8, 0x5A, 0x8C, 0x6A, 0xDE, 0xF5, 0xD9, 0x7E, 0xEC, 0xE9, 0x0C, 0xE3, 0x75, 0x8F, 0x29, 0x0B];
#! Writes a document as binary FBX, of its version, or 7.4 where it's of
#! before 7.0.
#! @param made The document.
#! @returns The file's bytes, or why it couldn't be written: `malformed` where a
#! node's name is longer than 255 bytes, or a file of 7.4 or before
#! would pass 4 GB.
pub def encode(made: &document): result<vector<u8>, fbx_error> {
val version: u32 = made.version >= 7000 ? made.version : 7400;
val wide := version >= 7500;
var out := vector::<u8>::new();
out.extend(binary_magic);
out.append(0);
out.append(0x1A);
out.append(0);
put_u32(&mut out, version);
# An ASCII document's objects are named as `Class::name`, which binary FBX
# writes as `name\0\1Class`.
var renamed: u32 = absent;
if !made.binary {
if made.child(absent, "Objects") is some(objects) {
renamed = objects;
}
}
for k in 0..made.roots.length {
write_node(&mut out, made, made.roots[k], k + 1 == made.roots.length, wide, renamed) else (e) { return e; };
}
put_zeros(&mut out, wide ? 25 : 13);
out.extend(footer_id[:]);
put_zeros(&mut out, 4);
val past := out.length % 16;
put_zeros(&mut out, past == 0 ? 16 : 16 - past);
put_u32(&mut out, version);
put_zeros(&mut out, 120);
out.extend(footer_magic[:]);
return out;
}
#! Writes a document to a file, as `encode` writes it, its folder made where it
#! isn't.
#! @returns Nothing, or why it couldn't be written: `unreadable` where the file
#! couldn't be.
pub def save(made: &document, at: &path): result<void, fbx_error> {
val bytes := encode(made) else (e) { return e; };
if at.parent() is some(&above) {
_ = io::create_directories(above);
}
io::write_file(at, bytes.span) else (_) { return failed(failure::unreadable, $"{at.span} couldn't be written.".span); };
return;
}
#! Writes a document to a file named by text, as `save` does.
pub def save(made: &document, at: strview): result<void, fbx_error> {
val named := path::parse(at) else (_) { return failed(failure::unreadable, $"{at} isn't a path.".span); };
return save(made, &named);
}
def put_u32(out: &mut vector<u8>, value: u32): void {
for i in 0..4u {
out.append(((value >> (8 * i)) & 0xFF) as u8);
}
}
def put_u64(out: &mut vector<u8>, value: u64): void {
for i in 0..8u {
out.append(((value >> (8 * i)) & 0xFF) as u8);
}
}
def put_zeros(out: &mut vector<u8>, count: u64): void {
for _ in 0..count {
out.append(0);
}
}
# Writes a number at a place written before, 32 or 64 bits wide.
def patch(out: &mut vector<u8>, at: u64, value: u64, wide: bool): void {
val bytes: u64 = wide ? 8 : 4;
for i in 0..bytes {
out[at + i] = ((value >> (8 * i)) & 0xFF) as u8;
}
}
# Writes a node: its header, its properties, its children, and the record of
# zeros ending them, where it needs one.
def write_node(out: &mut vector<u8>, made: &document, index: u32, last: bool, wide: bool, renamed: u32): result<void, fbx_error> {
val held := &made.nodes[index as u64];
return failed(failure::malformed, $"A node's name is {held.name.length} bytes, past the 255 there may be.".span) if held.name.length > 255;
val begun := out.length;
put_zeros(out, wide ? 24 : 12);
out.append(held.name.length as u8);
out.extend(held.name.span);
val properties_begun := out.length;
for i in 0..held.properties.length {
val name := held.properties[i].text();
if i == 1 && renamed != absent && held.parent == renamed && held.properties[i].kind == property_kind::text && name.find("::") is some(at) {
var turned := string::from(name.substr(at + 2, name.length - at - 2), nothing{});
turned.push(0);
turned.push(1);
turned.append(name.substr(0, at));
write_property(out, &property::of_text(turned.span));
continue;
}
write_property(out, &held.properties[i]);
}
val properties_length := out.length - properties_begun;
for k in 0..held.children.length {
write_node(out, made, held.children[k], k + 1 == held.children.length, wide, renamed) else (e) { return e; };
}
val always := held.name.span == "AnimationStack" || held.name.span == "AnimationLayer";
if held.children.length != 0 || (held.properties.length == 0 && !last) || always {
put_zeros(out, wide ? 25 : 13);
}
val end := out.length as u64;
return failed(failure::malformed, "It would pass 4 GB, which FBX of 7.4 and before can't.") if !wide && end > 0xFFFFFFFF;
val step: u64 = wide ? 8 : 4;
patch(out, begun, end, wide);
patch(out, begun + step, held.properties.length as u64, wide);
patch(out, begun + 2 * step, properties_length as u64, wide);
return;
}
# Writes a property: its type's letter, then its value.
def write_property(out: &mut vector<u8>, held: &property): void {
val kind := held.kind;
if kind == property_kind::boolean {
out.append('C');
out.append(held.integer as u8);
} else if kind == property_kind::int16 {
out.append('Y');
val v := held.integer as u64;
out.append((v & 0xFF) as u8);
out.append(((v >> 8) & 0xFF) as u8);
} else if kind == property_kind::int32 {
out.append('I');
put_u32(out, held.integer as u64 as u32);
} else if kind == property_kind::int64 {
out.append('L');
put_u64(out, held.integer as u64);
} else if kind == property_kind::float32 {
out.append('F');
put_u32(out, math::bits_of(held.real as f32));
} else if kind == property_kind::float64 {
out.append('D');
put_u64(out, math::bits_of(held.real));
} else if kind == property_kind::text || kind == property_kind::raw {
out.append(kind == property_kind::text ? 'S' : 'R');
put_u32(out, held.bytes.length as u32);
out.extend(held.bytes.span);
} else {
val codes: u8[5] = ['b', 'i', 'l', 'f', 'd'];
val sizes: u64[5] = [1, 4, 8, 4, 8];
val which := (kind as u8 - property_kind::booleans as u8) as u64;
val count := held.count();
out.append(codes[which]);
put_u32(out, count as u32);
put_u32(out, 0);
put_u32(out, (count * sizes[which]) as u32);
for i in 0..count {
if which == 0 {
out.append(held.integers[i] as u8);
} else if which == 1 {
put_u32(out, held.integers[i] as u64 as u32);
} else if which == 2 {
put_u64(out, held.integers[i] as u64);
} else if which == 3 {
put_u32(out, math::bits_of(held.reals[i] as f32));
} else {
put_u64(out, math::bits_of(held.reals[i]));
}
}
}
}