Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-fbx

Source of release 0.12.2

0.12.2 / src / fbx.lucb

## The luce-fbx export: FBX for Luce. Loading FBX files (binary and ASCII)
## as luce-geocore GeometrySets, and saving GeometrySets as binary FBX.
## Everything here is a thin entry point; the work is in document/, scene/,
## convert/ and writer/.
import interop
from luce_std import math
import memory
import source
import convert
import writer
import errors
from document import Document
from scene import Scene, Animation, absent
from luce_geocore.geocore import GeometrySet
from luce_geocore.kernel import adopt_set
from luce_geocore.core.growing import Growing
import strings

## The last load's warnings on this thread, up to 4 KiB of whole lines.
local var last_warnings: u8[4096]
local var last_length: usize

pub struct Fbx:
    let reserved: u8 = 0

    ## The scene of the FBX file at `path` as a GeometrySet (docs/MAPPING.md):
    ## every mesh merged into one mesh in world space, each face's model path
    ## in `path` and material in `material`. `keep_hierarchy` places each
    ## model's mesh as an instance in its own space instead; `instancing`
    ## loads a mesh several models share once, as instances;
    ## `convert_units` turns the file Y-up and scales it to meters;
    ## `normals` imports the file's normals as `N`. `time` (seconds) or
    ## `frame` (at the file's frame rate) evaluates the first animation stack
    ## there; without either the file's values are read as they are.
    ## `deform` applies blend shapes and skins. What was left out is in
    ## `warnings()`.
    pub static func load(path: str, keep_hierarchy: bool = false, instancing: bool = true, convert_units: bool = false, normals: bool = true, time: f64 = math.nan, frame: f64 = math.nan, deform: bool = true) -> interop.Reference[GeometrySet]!:
        let data = try source.read(path)
        defer free(data) in memory.heap
        var document = try Document.parse(data)
        defer document.close()
        var warnings = convert.Warnings()
        defer warnings.close()
        let options = convert.ImportOptions(keep_hierarchy = keep_hierarchy, instancing = instancing, convert_units = convert_units, normals = normals, time = time, frame = frame, deform = deform)
        let set = try convert.import_document(&document, &options, &warnings)
        remember(&warnings)
        return try adopt_set(set)

    ## Write `set` to `path` as binary FBX (docs/MAPPING.md, "Writing"):
    ## version 7400, or 7500 when asked (0 picks 7500 only when the file
    ## needs 64-bit offsets); `deflate` compresses arrays over 128 bytes.
    ## What was left out is in `warnings()`.
    pub static func save(set: interop.Reference[GeometrySet], path: str, version: i64 = 0, deflate: bool = true) -> !:
        if version != 0 and version != 7400 and version != 7500: error(errors.unsupported, "FBX writing supports versions 7400 and 7500")
        var warnings = convert.Warnings()
        defer warnings.close()
        let options = writer.ExportOptions(version = (u32)version, deflate = deflate)
        let data = try writer.encode_set(try set.get(), &options, &warnings)
        defer free(data) in memory.heap
        try source.write(path, data)
        remember_text(warnings.text.view())

    ## A summary of the FBX file at `path`, one "key: value" per line:
    ## version, encoding, creator, up axis, meters per unit, frame rate, the
    ## first animation stack's time span (seconds, and frames), and object
    ## counts by class.
    pub static func info(path: str) -> interop.Owned[str]!:
        let data = try source.read(path)
        defer free(data) in memory.heap
        var document = try Document.parse(data)
        defer document.close()
        var scene = try Scene.of(&document)
        defer scene.close()
        var animation = try scene.animation()
        defer animation.close()
        var out = Growing[u8]()
        defer out.close()
        try describe(&document, &scene, &animation, &out)
        return try interop.copy_text((str)out.view())

    ## The warnings of this thread's last load, one per line.
    pub static func warnings() -> interop.Owned[str]!:
        return try interop.copy_text((str)last_warnings[..<last_length])

## `Fbx.info`'s lines.
func describe(document: const Document*, scene: const Scene*, animation: const Animation*, out: Growing[u8]*) -> !:
    let s = scene.settings
    var line: u8[512] = ---
    try append(out, try strings.format(line, f"version: {document.version}\n"))
    try append(out, "encoding: binary\n" if document.binary else "encoding: ascii\n")
    try append(out, try strings.format(line, f"upAxis: {s.up.text()}\nfrontAxis: {s.front.text()}\nmetersPerUnit: {s.meters}\nframeRate: {s.frame_rate}\n"))
    if animation.stack != absent:
        try append(out, try strings.format(line, f"animation: {scene.name(animation.stack)}\nstart: {animation.start}\nstop: {animation.stop}\nstartFrame: {animation.start * s.frame_rate}\nstopFrame: {animation.stop * s.frame_rate}\n"))
    # Objects by class, in first-seen order.
    var classes = Growing[u32]()
    defer classes.close()
    for object in 1..<(u32)scene.objects.count:
        var seen = false
        for earlier in classes.view(): seen = seen or scene.class(earlier) == scene.class(object)
        if not seen: try classes.push(object)
    for first in classes.view():
        var count: usize = 0
        for object in 1..<(u32)scene.objects.count:
            if scene.class(object) == scene.class(first): count += 1
        try append(out, try strings.format(line, f"{scene.class(first)}: {count}\n"))

func append(out: Growing[u8]*, text: str) -> !:
    for byte in text.bytes: try out.push(byte)

func remember(warnings: const convert.Warnings*):
    remember_text(warnings.text.view())

func remember_text(text: const u8[]):
    var length = text.length
    if length > last_warnings.length:
        length = last_warnings.length
        while length > 0 and text[length - 1] != 10: length -= 1
    memory.copy(last_warnings[..<length], text[..<length], length)
    last_length = length