Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-fbx

Source of release 0.12.2

0.12.2 / tests / api / fbx_tests.luc

## Fbx.load, Fbx.save and warnings as luced-3d calls them: a Blender cube,
## the generated 7.4/7.5 files with raw and zlib arrays, instancing and Keep
## hierarchy, unit conversion, damaged files failing cleanly, and saves read
## back (a 1000-corner n-gon, instances, 7500, raw arrays).
from luce_fbx.fbx import Fbx
from luce_geocore.geocore import invalid, Mesh, GeometrySet, Vector3
from luce_std import math, paths

func near(a: float, b: float):
    if math.abs(a - b) >= 0.000001: print(f"near failed: actual={a}, expected={b}")
    assert(math.abs(a - b) < 0.000001)

pub func fbx_tests(scratch: str) -> unit!:
    let cube = Fbx.load("../fixtures/blender_279_default_7400_binary.fbx")
    let mesh = cube.mesh() else error(invalid, "no mesh")
    assert(mesh.point_count() == 8 and mesh.face_count() == 6)
    near(mesh.bounds().minimum.x, -1.0)
    near(mesh.bounds().maximum.x, 1.0)
    assert(cube.detail_value("fbx.metersPerUnit") == 1.0)
    for version in [7400, 7500]:
        for compressed in [0, 1]:
            let generated = Fbx.load(f"../fixtures/generated_{version}_{compressed}.fbx")
            assert((generated.mesh() else error(invalid, "no mesh")).face_count() == 1)
    let shared = Fbx.load("../fixtures/blender_293_instancing_7400_binary.fbx")
    assert(shared.instance_count() == 8)
    let merged = Fbx.load("../fixtures/blender_293_instancing_7400_binary.fbx", instancing = false)
    assert((merged.mesh() else error(invalid, "no mesh")).face_count() == 4000)
    let converted = Fbx.load("../fixtures/max_edge_visibility_7500_binary.fbx", convert_units = true)
    assert(converted.detail_value("fbx.metersPerUnit") == 1.0)
    for name in ["bad_offset", "truncated"]:
        var failed = false
        _ = Fbx.load(f"../fixtures/generated_{name}.fbx") catch failure:
            failed = true
        assert(failed)
    # Dual quaternion skinning animated: the mesh moves with the frame.
    let early = Fbx.load("../fixtures/maya_dq_weights_7500_binary.fbx", time = 0.5).mesh() else error(invalid, "no mesh")
    let late = Fbx.load("../fixtures/maya_dq_weights_7500_binary.fbx", frame = 1.2 * 24.0).mesh() else error(invalid, "no mesh")
    assert(early.point_count() == late.point_count() and early.point(0).x != late.point(0).x)
    let info = Fbx.info("../fixtures/maya_dq_weights_7500_binary.fbx")
    assert(info.contains("frameRate: 24") and info.contains("Deformer: ") and info.contains("animation: "))
    _ = Fbx.warnings()
    # Array attributes have no FBX layer: left out with a warning.
    let counts: list[int] = []
    for at in 0..<mesh.point_count():
        counts.append(1 if at == 0 else (2 if at == 1 else 0))
    let listed = mesh.with_array_attribute("nbrs", 0, true, 1, counts, [7.0, 8.0, 9.0])
    Fbx.save(GeometrySet.of_mesh(listed), paths.join(scratch, "arrays.fbx"))
    assert(Fbx.warnings().contains("FBX has no layer for array attributes; the mesh attribute nbrs was left out"))
    assert((Fbx.load(paths.join(scratch, "arrays.fbx")).mesh() else error(invalid, "no mesh")).face_count() == 6)
    save_tests(scratch)
    print("PASS Fbx.load, info and save: binary and ASCII scenes, instancing, units, animation and skins at a time, damaged files, saves read back")

## Saves read back.
func save_tests(scratch: str) -> unit!:
    # A 1000-corner concave star stays one face.
    let points: list[Vector3] = []
    let corners: list[int] = []
    for at in 0..<1000:
        let radius = 1.0 if at % 2 == 0 else 0.8
        let angle = 2.0 * math.pi * float(at) / 1000.0
        points.append(Vector3(radius * math.cos(angle), 0.0, radius * math.sin(angle)))
        corners.append(at)
    Fbx.save(GeometrySet.of_mesh(Mesh(points, [1000], corners)), paths.join(scratch, "star.fbx"))
    let star = Fbx.load(paths.join(scratch, "star.fbx")).mesh() else error(invalid, "no mesh")
    assert(star.face_count() == 1 and star.face_size(0) == 1000 and star.point_count() == 1000)
    near(star.point(500).x, -1.0)
    # The cube and the eight shared Suzannes, at 7400 and 7500, deflated
    # and raw.
    let cube = Fbx.load("../fixtures/blender_279_default_7400_binary.fbx")
    let shared = Fbx.load("../fixtures/blender_293_instancing_7400_binary.fbx")
    for version in [7400, 7500]:
        for deflate in [true, false]:
            Fbx.save(cube, paths.join(scratch, "cube.fbx"), version = version, deflate = deflate)
            let again = Fbx.load(paths.join(scratch, "cube.fbx"))
            let mesh = again.mesh() else error(invalid, "no mesh")
            assert(mesh.point_count() == 8 and mesh.face_count() == 6)
            assert(Fbx.info(paths.join(scratch, "cube.fbx")).contains(f"version: {version}"))
            Fbx.save(shared, paths.join(scratch, "shared.fbx"), version = version, deflate = deflate)
            assert(Fbx.load(paths.join(scratch, "shared.fbx")).instance_count() == 8)
    var refused = false
    Fbx.save(cube, paths.join(scratch, "never.fbx"), version = 6100) catch failure:
        refused = true
    assert(refused)