Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-fbx

Source of release 0.12.1

0.12.1 / src / writer / tree.lucb

## The FBX node tree a save builds before it encodes it (encode.lucb).
## Nodes are kept depth first, each with its properties; a node's subtree
## runs to its `end`. Top-level nodes are written in `roots` order, which
## the builder may rearrange (Definitions, counted last, goes before
## Objects). Arrays are blobs of raw little-endian values the encoder
## deflates in parallel. Every other fragment of the writer builds on this.
from luce_std import math
import memory
from luce_geocore.core import parallel as pool
from luce_compress import compress
import errors
import source
from convert import Warnings, placement_of
from luce_geocore.geocore import Vector3, Matrix4, Mesh
from luce_geocore.kernel import Float3, Attribute, AttributeStore, Domain, DataType, GeometrySet, Curves, CurveSettings, KnotMode, CurveType, Instances, Placement, set_detail, mesh_component, curves_type, instances_component, curves_origin
from luce_geocore.core.growing import Growing

## One node: its name (in the tree's text), properties and subtree.
struct WNode:
    var name_start: u32
    var name_length: u32
    var first_prop: u32
    var prop_count: u32
    ## One past the last node of its subtree.
    var end: u32

## One property. Scalars keep their bits; strings and raw bytes a span of
## the tree's text; arrays a blob.
struct WProp:
    ## The FBX type code: C, Y, I, L, F, D, S, R, or i, l, f, d, b for arrays.
    var kind: u8
    var bits: u64
    var start: usize
    var length: usize

## An array property's values: `count` elements of `raw` (owned), and after
## packing the zlib stream in `packed[..<packed_length]` (owned; empty when
## stored raw).
struct Blob:
    var kind: u8
    var count: usize
    var raw: u8[]
    var packed: u8[]
    var packed_length: usize
    var failed: bool

    func release():
        if self.raw.length > 0: free(self.raw) in memory.heap
        if self.packed.length > 0: free(self.packed) in memory.heap

## Bytes of one element of array type `kind`.
func element_bytes(kind: u8) -> usize:
    match kind:
        'd', 'l': return 8
        'f', 'i': return 4
        _: return 1

pub struct Tree:
    var nodes: Growing[WNode]
    var props: Growing[WProp]
    var text: Growing[u8]
    var blobs: Growing[Blob]
    var roots: Growing[u32]
    var open: Growing[u32]

    pub func init():
        self.nodes = Growing[WNode]()
        self.props = Growing[WProp]()
        self.text = Growing[u8]()
        self.blobs = Growing[Blob]()
        self.roots = Growing[u32]()
        self.open = Growing[u32]()

    pub func close():
        for at in 0..<self.blobs.count: self.blobs.items[at].release()
        self.nodes.close()
        self.props.close()
        self.text.close()
        self.blobs.close()
        self.roots.close()
        self.open.close()

    ## Open a child of the open node (or a top-level node).
    pub func begin(name: str) -> !:
        let index = (u32)self.nodes.count
        let start = (u32)self.text.count
        for byte in name.bytes: try self.text.push(byte)
        try self.nodes.push(WNode(name_start = start, name_length = (u32)name.length, first_prop = (u32)self.props.count, prop_count = 0, end = index + 1))
        if self.open.count == 0: try self.roots.push(index)
        try self.open.push(index)

    ## Close the open node.
    pub func end():
        self.open.count -= 1
        let index = (usize)self.open.items[self.open.count]
        self.nodes.items[index].end = (u32)self.nodes.count

    ## The node properties are being added to (the last one opened).
    func prop(value: WProp) -> !:
        let index = (usize)self.open.items[self.open.count - 1]
        try self.props.push(value)
        self.nodes.items[index].prop_count += 1

    pub func int32(value: i32) -> !:
        try self.prop(WProp(kind = 'I', bits = (u64)(u32)value, start = 0, length = 0))

    pub func int64(value: i64) -> !:
        try self.prop(WProp(kind = 'L', bits = (u64)value, start = 0, length = 0))

    pub func real64(value: f64) -> !:
        try self.prop(WProp(kind = 'D', bits = value.bits(), start = 0, length = 0))

    ## A one-byte value ('C'): 'T' or 'F' for Shading, 1 or 0 for flags.
    pub func flag(value: u8) -> !:
        try self.prop(WProp(kind = 'C', bits = (u64)value, start = 0, length = 0))

    pub func string(value: str) -> !:
        let start = self.text.count
        for byte in value.bytes: try self.text.push(byte)
        try self.prop(WProp(kind = 'S', bits = 0, start = start, length = value.length))

    ## An object's name as FBX 7 stores it: "Name\x00\x01Class".
    pub func object_name(name: str, class: str) -> !:
        let start = self.text.count
        for byte in name.bytes: try self.text.push(byte)
        try self.text.push(0)
        try self.text.push(1)
        for byte in class.bytes: try self.text.push(byte)
        try self.prop(WProp(kind = 'S', bits = 0, start = start, length = name.length + 2 + class.length))

    pub func raw(value: const u8[]) -> !:
        let start = self.text.count
        for byte in value: try self.text.push(byte)
        try self.prop(WProp(kind = 'R', bits = 0, start = start, length = value.length))

    ## An array property of `count` elements in `raw`, which the tree takes
    ## (and frees, even when this fails).
    pub func array(kind: u8, count: usize, raw: u8[]) -> !:
        var blob = Blob(kind = kind, count = count, raw = raw, packed = empty_bytes(), packed_length = 0, failed = false)
        self.blobs.push(blob) catch failure:
            blob.release()
            error(failure.code, failure.message)
        try self.prop(WProp(kind = kind, bits = 0, start = self.blobs.count - 1, length = 0))

    ## Arrays from values the caller keeps.
    pub func ints(values: const i32[]) -> !:
        let raw = try new u8[values.length * 4] --- in memory.heap
        if values.length > 0: memory.copy(raw, u8[]((u8*)values.data, values.length * 4), values.length * 4)
        try self.array('i', values.length, raw)

    pub func reals(values: const f64[]) -> !:
        let raw = try new u8[values.length * 8] --- in memory.heap
        if values.length > 0: memory.copy(raw, u8[]((u8*)values.data, values.length * 8), values.length * 8)
        try self.array('d', values.length, raw)

    # -- Leaves: a node with one value.

    pub func leaf_int32(name: str, value: i32) -> !:
        try self.begin(name)
        try self.int32(value)
        self.end()

    pub func leaf_int64(name: str, value: i64) -> !:
        try self.begin(name)
        try self.int64(value)
        self.end()

    pub func leaf_real(name: str, value: f64) -> !:
        try self.begin(name)
        try self.real64(value)
        self.end()

    pub func leaf_string(name: str, value: str) -> !:
        try self.begin(name)
        try self.string(value)
        self.end()

    ## An empty node (no properties, no children).
    pub func leaf(name: str) -> !:
        try self.begin(name)
        self.end()

    # -- Properties70 entries: P: name, type, label, flags, values.

    func p(name: str, kind: str, label: str, flags: str) -> !:
        try self.begin("P")
        try self.string(name)
        try self.string(kind)
        try self.string(label)
        try self.string(flags)

    pub func p_int(name: str, value: i32) -> !:
        try self.p(name, "int", "Integer", "")
        try self.int32(value)
        self.end()

    pub func p_bool(name: str, value: bool) -> !:
        try self.p(name, "bool", "", "")
        try self.int32(1 if value else 0)
        self.end()

    pub func p_enum(name: str, value: i32) -> !:
        try self.p(name, "enum", "", "")
        try self.int32(value)
        self.end()

    pub func p_double(name: str, value: f64) -> !:
        try self.p(name, "double", "Number", "")
        try self.real64(value)
        self.end()

    pub func p_time(name: str, value: i64) -> !:
        try self.p(name, "KTime", "Time", "")
        try self.int64(value)
        self.end()

    pub func p_string(name: str, value: str) -> !:
        try self.p(name, "KString", "", "")
        try self.string(value)
        self.end()

    pub func p_color(name: str, value: Vector3) -> !:
        try self.p(name, "ColorRGB", "Color", "")
        try self.real64(value.x)
        try self.real64(value.y)
        try self.real64(value.z)
        self.end()

    ## A three-number property of type `kind` (Lcl Translation, Vector3D...).
    pub func p_vector(name: str, kind: str, label: str, flags: str, value: Vector3) -> !:
        try self.p(name, kind, label, flags)
        try self.real64(value.x)
        try self.real64(value.y)
        try self.real64(value.z)
        self.end()

    ## Move top-level node `moved` (a root index) to just before `before`.
    pub func root_before(moved: u32, before: u32):
        var from_at: usize = 0
        var to_at: usize = 0
        for at in 0..<self.roots.count:
            if self.roots.items[at] == moved: from_at = at
            if self.roots.items[at] == before: to_at = at
        if from_at <= to_at: return
        let value = self.roots.items[from_at]
        var at = from_at
        while at > to_at:
            self.roots.items[at] = self.roots.items[at - 1]
            at -= 1
        self.roots.items[to_at] = value

func empty_bytes() -> u8[]:
    var none_yet: u8[]
    return none_yet