#! Contains `document`, an FBX file as it's written: a tree of nodes, each a #! name and properties, numbers, text, bytes, or arrays of numbers; why one #! couldn't be read; and reading one, binary or ASCII, from a file or from #! bytes. #! #! FBX is Autodesk's, and undocumented: what's read here is what Blender's #! importer and others have found it to be. A binary file is a header, then #! nodes, each its end, its properties, and its children; arrays may be #! compressed by zlib. An ASCII file is the same tree written as text, as #! `Name: 1, "text" { ... }`. Either is read into the same `document`, whose #! nodes are kept in one list, by index, each with its parent and children. #! `scene::of` reads what a document's nodes mean: models, meshes, materials, #! skins, and animations. import std::io; module malunal::fbx; #! What kind of thing kept a file from being read or written. pub enum failure: u8 { unreadable, #! A file couldn't be read, or written. malformed, #! The data breaks FBX: a node past the file's end, a property of an unknown type, compressed data that won't inflate, text that isn't of nodes. unsupported, #! It's well formed, but of what isn't read: an FBX older than 7.0, whose objects are named rather than numbered. } #! Why a file couldn't be read, or written: the kind of thing, and what, as #! `Objects/Geometry: an array's data won't inflate`. It holds its text itself, #! its first 240 bytes, so it's plain data, as std's errors are. pub struct fbx_error { pub var kind: failure; var text: u8[240]; var length: u64; } #! What was wrong, and where. pub def fbx_error::describe(&this): string { return string::from(strview::over(&this.text[0] as *u8, this.length), nothing{}); } # An error of a kind, saying why. def failed(kind: failure, why: strview): fbx_error { var made := fbx_error{ kind = kind }; made.length = why.length < 240 ? why.length : 240; for i in 0..made.length { made.text[i] = why[i]; } return made; } #! What an index names where it names nothing: a node's parent at the root, #! or a model's mesh where it has none. pub const absent: u32 = 0xFFFFFFFF; #! The type of a property's value, by the letter binary FBX gives it. pub enum property_kind: u8 { boolean, #! `C`, a byte: 0 or 1, or a letter, as `T` or `Y`. int16, #! `Y` int32, #! `I` int64, #! `L` float32, #! `F` float64, #! `D` text, #! `S` raw, #! `R`, bytes, as an embedded file's. booleans, #! `b`, an array, as are those after it. int32s, #! `i` int64s, #! `l` float32s, #! `f` float64s, #! `d` } #! A property of a node: a number, text, bytes, or an array of numbers, each #! held as the widest of its kind. pub struct property { pub var kind: property_kind; #! A boolean's, or an integer's. pub var integer: i64; #! A float's. pub var real: f64; #! Text's, or raw bytes. pub var bytes: vector; #! An array of booleans' or integers'. pub var integers: vector; #! An array of floats'. pub var reals: vector; } #! Whether it's an array. pub def property::is_array(&this): bool { return this.kind as u8 >= property_kind::booleans as u8; } #! It as a number: a float's, or an integer's, or 0 of text, bytes, or an #! array. pub def property::number(&this): f64 { if this.kind == property_kind::float32 || this.kind == property_kind::float64 { return this.real; } return this.is_array() || this.kind == property_kind::text || this.kind == property_kind::raw ? 0 : this.integer as f64; } #! It as a whole number: an integer's, or a float's, cut. pub def property::whole(&this): i64 { if this.kind == property_kind::float32 || this.kind == property_kind::float64 { return this.real as i64; } return this.integer; } #! Its text, or its bytes as text; empty of a number. pub def property::text(&this): strview { return strview::over(this.bytes.data, this.bytes.length); } #! How many numbers its array holds. pub def property::count(&this): u64 { return this.kind == property_kind::float32s || this.kind == property_kind::float64s ? this.reals.length : this.integers.length; } #! A number of its array, as a float, whichever its kind. pub def property::real_at(&this, at: u64): f64 { if this.kind == property_kind::float32s || this.kind == property_kind::float64s { return this.reals[at]; } return this.integers[at] as f64; } #! A number of its array, as an integer, whichever its kind. pub def property::integer_at(&this, at: u64): i64 { if this.kind == property_kind::float32s || this.kind == property_kind::float64s { return this.reals[at] as i64; } return this.integers[at]; } #! A node of the tree: its name, its properties, and its children and parent, #! by their indices in the document's nodes. pub struct node { pub var name: string; pub var properties: vector; pub var children: vector; pub var parent: u32 = absent; } #! An FBX file as it's written: its version, as 7400 for 7.4, whether it was #! binary, and its tree of nodes, all of them in one list, those at the root #! named in `roots`. pub struct document { pub var version: u32 = 7400; pub var binary: bool = true; pub var nodes: vector; pub var roots: vector; } #! Adds a node, after its parent's other children. #! @param parent Its parent, or `absent` at the root. #! @param name Its name. #! @returns Its index. pub def document::add(&mut this, parent: u32, name: strview): u32 { val index := this.nodes.length as u32; this.nodes.append(node{ name = string::from(name, nothing{}), parent = parent }); if parent == absent { this.roots.append(index); } else { this.nodes[parent as u64].children.append(index); } return index; } #! The first child of a node of a name, or of the root where it's `absent`. pub def document::child(&this, of: u32, name: strview): maybe { val list := of == absent ? this.roots.span : this.nodes[of as u64].children.span; for i in 0..list.length { return list[i] if this.nodes[list[i] as u64].name.span == name; } return nothing{}; } #! The children of a node of a name, or of the root where it's `absent`. pub def document::children_named(&this, of: u32, name: strview): vector { var made := vector::::new(); val list := of == absent ? this.roots.span : this.nodes[of as u64].children.span; for i in 0..list.length { if this.nodes[list[i] as u64].name.span == name { made.append(list[i]); } } return made; } #! A property of a node, where it has that many. pub def document::property(&this, of: u32, which: u64): maybe<&property> { val held := &this.nodes[of as u64]; return nothing{} if which >= held.properties.length; return &held.properties[which]; } #! The numbers of a node: its first property's, where that's an array, or its #! properties' in turn, as an ASCII file before 7.0 writes them, as floats. pub def document::reals(&this, of: u32): vector { var made := vector::::new(); val held := &this.nodes[of as u64]; if held.properties.length != 0 && held.properties[0].is_array() { val first := &held.properties[0]; made.reserve(first.count()); for i in 0..first.count() { made.append(first.real_at(i)); } return made; } for i in 0..held.properties.length { made.append(held.properties[i].number()); } return made; } #! The numbers of a node, as `reals` gives them, as integers. pub def document::integers(&this, of: u32): vector { var made := vector::::new(); val held := &this.nodes[of as u64]; if held.properties.length != 0 && held.properties[0].is_array() { val first := &held.properties[0]; made.reserve(first.count()); for i in 0..first.count() { made.append(first.integer_at(i)); } return made; } for i in 0..held.properties.length { made.append(held.properties[i].whole()); } return made; } #! Reads a document from a file: binary or ASCII, whichever its first bytes #! say. #! @param at The file's path. #! @returns The document, or why it couldn't be read. pub def load(at: &path): result { val bytes := io::read_file(at) else (_) { return failed(failure::unreadable, $"{at.span} couldn't be read.".span); }; return decode(bytes.span); } #! Reads a document from a file named by text, as `load` does. pub def load(at: strview): result { val named := path::parse(at) else (_) { return failed(failure::unreadable, $"{at} isn't a path.".span); }; return load(&named); } #! Reads a document from bytes: a binary file's, which begin #! `Kaydara FBX Binary`, or else an ASCII file's text. pub def decode(bytes: span): result { return read_binary(bytes) if is_binary(bytes); return read_ascii(strview::over(bytes.data, bytes.length)); }