model/blend: Blender 1.6x-2.0x files (big-endian, 32-bit, TFace / MCol, IPO curves, additive dsize), easing curves, owner-scoped address lookup; containers/Blend tests; docs section; bench line
This commit is contained in:
1 parent
2e82b7d0af
commit
3c8d5e99d7
14 files changed
+766
-110
No files matched your search
@@ -0,0 +1,294 @@
|
||||
-- containers/Blend tests: synthetic files in every header / pointer size / byte order (12-byte header with 4 and 8
|
||||
-- byte pointers, little and big endian; the 5.0+ 17-byte header with large block heads), structs by name, arrays,
|
||||
-- pointers, ListBase walks, addresses reused between IDs, gzip; the real files of tools/fixtures/blend.py (headers,
|
||||
-- versions, zstd); errors; the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local Blend = require("@rosetta/containers/Blend")
|
||||
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Cur = require("@tests/gen/blend_cur")
|
||||
local Old = require("@tests/gen/blend_293")
|
||||
|
||||
local t = T.suite("containers/Blend")
|
||||
|
||||
-- a writer of either byte order
|
||||
local function writer(little: boolean)
|
||||
local parts: { string } = {}
|
||||
local w = {}
|
||||
local function bytes(v: number, n: number): string
|
||||
local s = {}
|
||||
for k = 0, n - 1 do
|
||||
s[k + 1] = string.char(math.floor(v / 256 ^ k) % 256)
|
||||
end
|
||||
local str = table.concat(s)
|
||||
return if little then str else string.reverse(str)
|
||||
end
|
||||
function w.u8(v: number)
|
||||
table.insert(parts, string.char(v))
|
||||
end
|
||||
function w.i16(v: number)
|
||||
table.insert(parts, bytes(v % 65536, 2))
|
||||
end
|
||||
function w.i32(v: number)
|
||||
table.insert(parts, bytes(v % 4294967296, 4))
|
||||
end
|
||||
function w.u64(v: number)
|
||||
table.insert(parts, bytes(v, 8))
|
||||
end
|
||||
function w.f32(v: number)
|
||||
local b = buffer.create(4)
|
||||
buffer.writef32(b, 0, v)
|
||||
local s = buffer.tostring(b)
|
||||
table.insert(parts, if little then s else string.reverse(s))
|
||||
end
|
||||
function w.f64(v: number)
|
||||
local b = buffer.create(8)
|
||||
buffer.writef64(b, 0, v)
|
||||
local s = buffer.tostring(b)
|
||||
table.insert(parts, if little then s else string.reverse(s))
|
||||
end
|
||||
function w.str(s: string)
|
||||
table.insert(parts, s)
|
||||
end
|
||||
function w.ptr(v: number, ps: number)
|
||||
if ps == 8 then
|
||||
w.u64(v)
|
||||
else
|
||||
w.i32(v)
|
||||
end
|
||||
end
|
||||
function w.done(): string
|
||||
return table.concat(parts)
|
||||
end
|
||||
return w
|
||||
end
|
||||
|
||||
-- SDNA: ListBase { void *first, *last }; Thing { Thing *next, *prev; int a; float b[2]; char name[8]; short s;
|
||||
-- char pad[2]; void *data }; Holder { ListBase things; Thing *one; double d; ID-less }
|
||||
local NAMES = { "*first", "*last", "*next", "*prev", "a", "b[2]", "name[8]", "s", "pad[2]", "*data", "things", "*one", "d" }
|
||||
local TYPES = { "char", "short", "int", "float", "double", "void", "ListBase", "Thing", "Holder" }
|
||||
|
||||
local function sdna(little: boolean, ps: number): string
|
||||
local w = writer(little)
|
||||
local function pad()
|
||||
local s = w.done()
|
||||
return (4 - #s % 4) % 4
|
||||
end
|
||||
w.str("SDNA")
|
||||
w.str("NAME")
|
||||
w.i32(#NAMES)
|
||||
for _, n in NAMES do
|
||||
w.str(n .. "\0")
|
||||
end
|
||||
for _ = 1, pad() do
|
||||
w.u8(0)
|
||||
end
|
||||
w.str("TYPE")
|
||||
w.i32(#TYPES)
|
||||
for _, n in TYPES do
|
||||
w.str(n .. "\0")
|
||||
end
|
||||
for _ = 1, pad() do
|
||||
w.u8(0)
|
||||
end
|
||||
w.str("TLEN")
|
||||
local thing = ps * 2 + 4 + 8 + 8 + 2 + 2 + ps
|
||||
for _, l in { 1, 2, 4, 4, 8, 0, ps * 2, thing, ps * 2 + ps + 8 } do
|
||||
w.i16(l)
|
||||
end
|
||||
for _ = 1, pad() do
|
||||
w.u8(0)
|
||||
end
|
||||
w.str("STRC")
|
||||
w.i32(3)
|
||||
local function struct(ty: number, fields: { { number } })
|
||||
w.i16(ty)
|
||||
w.i16(#fields)
|
||||
for _, f in fields do
|
||||
w.i16(f[1])
|
||||
w.i16(f[2])
|
||||
end
|
||||
end
|
||||
-- type / name indices are 0-based
|
||||
struct(6, { { 5, 0 }, { 5, 1 } })
|
||||
struct(7, { { 7, 2 }, { 7, 3 }, { 2, 4 }, { 3, 5 }, { 0, 6 }, { 1, 7 }, { 0, 8 }, { 5, 9 } })
|
||||
struct(8, { { 6, 10 }, { 7, 11 }, { 4, 12 } })
|
||||
return w.done()
|
||||
end
|
||||
|
||||
local function file(little: boolean, ps: number, large: boolean, dupe: boolean?): string
|
||||
local w = writer(little)
|
||||
if large then
|
||||
w.str("BLENDER17-01v0502")
|
||||
else
|
||||
w.str("BLENDER" .. (if ps == 8 then "-" else "_") .. (if little then "v" else "V") .. "279")
|
||||
end
|
||||
local function head(code: string, size: number, addr: number, sdnaIndex: number, count: number)
|
||||
w.str(code .. string.rep("\0", 4 - #code))
|
||||
if large then
|
||||
w.i32(sdnaIndex)
|
||||
w.u64(addr)
|
||||
w.u64(size)
|
||||
w.u64(count)
|
||||
else
|
||||
w.i32(size)
|
||||
w.ptr(addr, ps)
|
||||
w.i32(sdnaIndex)
|
||||
w.i32(count)
|
||||
end
|
||||
end
|
||||
local thingSize = ps * 2 + 24 + ps
|
||||
head("HO", ps * 3 + 8, 0x1000, 2, 1)
|
||||
w.ptr(0x2000, ps) -- things.first
|
||||
w.ptr(0x3000, ps) -- things.last
|
||||
w.ptr(0x3000, ps) -- one
|
||||
w.f64(2.5)
|
||||
local function thing(next: number, prev: number, a: number, b1: number, b2: number, name: string, s: number, data: number)
|
||||
w.ptr(next, ps)
|
||||
w.ptr(prev, ps)
|
||||
w.i32(a)
|
||||
w.f32(b1)
|
||||
w.f32(b2)
|
||||
w.str(name .. string.rep("\0", 8 - #name))
|
||||
w.i16(s)
|
||||
w.str("\0\0")
|
||||
w.ptr(data, ps)
|
||||
end
|
||||
head("DATA", thingSize, 0x2000, 1, 1)
|
||||
thing(0x3000, 0, -7, 1.5, -2.25, "first", -300, 0x4000)
|
||||
head("DATA", thingSize, 0x3000, 1, 1)
|
||||
thing(0, 0x2000, 123456, 0.5, 4, "second!", 1234, 0)
|
||||
head("DATA", 6, 0x4000, 0, 1)
|
||||
w.str("hello\0")
|
||||
if dupe then
|
||||
-- a second ID whose own data reuses the address 0x4000
|
||||
head("H2", ps * 3 + 8, 0x5000, 2, 1)
|
||||
w.ptr(0, ps)
|
||||
w.ptr(0, ps)
|
||||
w.ptr(0x4000, ps)
|
||||
w.f64(-1)
|
||||
head("DATA", thingSize, 0x4000, 1, 1)
|
||||
thing(0, 0, 99, 0, 0, "dup", 0, 0)
|
||||
end
|
||||
local dna = sdna(little, ps)
|
||||
head("DNA1", #dna, 0x9000, 0, 1)
|
||||
w.str(dna)
|
||||
head("ENDB", 0, 0, 0, 0)
|
||||
return w.done()
|
||||
end
|
||||
|
||||
for _, v in { { true, 8, false }, { true, 4, false }, { false, 8, false }, { false, 4, false }, { true, 8, true } } do
|
||||
local little, ps, large = v[1], v[2], v[3]
|
||||
local label = string.format("%s-endian, %d-byte pointers%s", if little then "little" else "big", ps, if large then ", 5.0 header" else "")
|
||||
local f, err = Blend.open(buffer.fromstring(file(little, ps, large)))
|
||||
t:check(f, label .. ": open: " .. tostring(err))
|
||||
if not f then
|
||||
continue
|
||||
end
|
||||
t:eq(f.pointerSize, ps, label .. ": pointer size")
|
||||
t:eq(f.little, little, label .. ": byte order")
|
||||
t:eq(f.version, if large then 502 else 279, label .. ": version")
|
||||
t:eq(f.format, if large then 1 else 0, label .. ": format")
|
||||
t:eq(#f.blocks, 5, label .. ": blocks (ENDB not listed)")
|
||||
t:eq(f.dna.byName.Thing.size, ps * 3 + 24, label .. ": struct size from TLEN")
|
||||
t:eq(f.dna.byName.Thing.byName.data.offset, ps * 2 + 24, label .. ": field offsets")
|
||||
local ho = assert(Blend.struct(f, Blend.blocks(f, "HO")[1]))
|
||||
t:eq(ho:num("d"), 2.5, label .. ": double")
|
||||
local list = ho:list("things")
|
||||
t:eq(#list, 2, label .. ": ListBase walk")
|
||||
t:eq(list[1]:num("a"), -7, label .. ": int")
|
||||
t:eq(list[2]:num("a"), 123456, label .. ": int 2")
|
||||
t:eq(list[1]:num("s"), -300, label .. ": short")
|
||||
t:eq(list[1]:str("name"), "first", label .. ": char array")
|
||||
t:eq(list[2]:str("name"), "second!", label .. ": char array 2")
|
||||
local b = list[1]:vec("b")
|
||||
t:check(b and b[1] == 1.5 and b[2] == -2.25, label .. ": float array")
|
||||
t:eq(list[1]:cstring("data"), "hello", label .. ": pointer to raw data")
|
||||
local one = ho:deref("one")
|
||||
t:check(one and one:num("a") == 123456, label .. ": deref by field type")
|
||||
t:check(ho:ptr("one") ~= nil and list[2]:ptr("next") == nil, label .. ": null pointers are nil")
|
||||
t:check(ho:get("missing") == nil and ho:has("d") and not ho:has("missing"), label .. ": unknown fields")
|
||||
t:eq(ho:num("missing", "d"), 2.5, label .. ": the first existing of several names")
|
||||
local fld = Blend.field(f, "Thing", "nope", "b")
|
||||
t:check(fld and fld.count == 2 and fld.type == "float" and #fld.dims == 1, label .. ": Blend.field")
|
||||
end
|
||||
|
||||
-- an address reused by another ID's data resolves inside the ID that points at it
|
||||
do
|
||||
local f = assert(Blend.open(buffer.fromstring(file(true, 8, false, true))))
|
||||
local h1 = assert(Blend.struct(f, Blend.blocks(f, "HO")[1]))
|
||||
local h2 = assert(Blend.struct(f, Blend.blocks(f, "H2")[1]))
|
||||
t:eq(h1:list("things")[1]:cstring("data"), "hello", "reused address: the first ID's raw data")
|
||||
local d = h2:deref("one")
|
||||
t:check(d and d:num("a") == 99 and d:str("name") == "dup", "reused address: the second ID's own block")
|
||||
end
|
||||
|
||||
-- gzip (Blender before 3.0)
|
||||
do
|
||||
local raw = file(true, 8, false)
|
||||
local gz = assert(Deflate.gzip(raw))
|
||||
local f, err = Blend.open(gz)
|
||||
t:check(f and f.compression == "gzip" and #f.blocks == 5, "gzip: " .. tostring(err))
|
||||
t:eq(Blend.sniff(gz), 0.05, "gzip sniffs weakly")
|
||||
t:eq(Blend.sniff(buffer.fromstring(raw)), 1, "magic")
|
||||
end
|
||||
|
||||
-- the real files: headers and compression
|
||||
for _, set in { Cur, Old } do
|
||||
for _, c in set do
|
||||
local f, err = Blend.open(buffer.fromstring(c.data))
|
||||
t:check(f, c.name .. ": " .. tostring(err))
|
||||
if f then
|
||||
local want = c.ref.version[1] * 100 + c.ref.version[2]
|
||||
t:eq(f.version, want, c.name .. ": version")
|
||||
t:eq(f.format, if want >= 500 then 1 else 0, c.name .. ": header format")
|
||||
if string.find(c.name, "_z%.blend$") then
|
||||
t:eq(f.compression, if want >= 300 then "zstd" else "gzip", c.name .. ": compression")
|
||||
end
|
||||
t:check(f.dna.byName.Object and f.dna.byName.Mesh and #Blend.blocks(f, "OB") > 0, c.name .. ": SDNA and objects")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local f, e = Blend.open(buffer.fromstring(s))
|
||||
return if f then nil else e.code
|
||||
end
|
||||
local good = file(true, 8, false)
|
||||
t:eq(code("BLEND"), Err.TRUNCATED, "short file")
|
||||
t:eq(code("NOTBLEND-v279" .. string.rep("\0", 40)), Err.CORRUPT, "bad magic")
|
||||
t:eq(code(string.sub(good, 1, #good - 30)), Err.TRUNCATED, "no ENDB")
|
||||
t:eq(code("BLENDER17-02v0502" .. string.rep("\0", 40)), Err.UNSUPPORTED, "unknown header format version")
|
||||
local noDna = string.gsub(good, "DNA1", "DNA2")
|
||||
t:eq(code(noDna), Err.CORRUPT, "no DNA1")
|
||||
local badDna = string.gsub(good, "STRC", "STRX")
|
||||
t:eq(code(badDna), Err.CORRUPT, "bad SDNA")
|
||||
local _, e = Blend.open(buffer.fromstring(good), 0, 999999)
|
||||
t:eq(e and e.code, Err.ARG, "bad range")
|
||||
|
||||
local samples = { good, file(false, 4, false), file(true, 8, true, true) }
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local f = Blend.open(b)
|
||||
if f then
|
||||
for _, blk in f.blocks do
|
||||
local v = Blend.struct(f, blk)
|
||||
if v then
|
||||
for _, fld in v.struct.fields do
|
||||
local x = v:get(fld.name)
|
||||
if fld.pointer then
|
||||
v:deref(fld.name)
|
||||
v:array(fld.name)
|
||||
v:pointers(fld.name)
|
||||
v:cstring(fld.name)
|
||||
elseif fld.type == "ListBase" then
|
||||
v:list(fld.name)
|
||||
end
|
||||
local _ = x
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return f
|
||||
end, samples, { mutations = 64 })
|
||||
t:done()
|
||||
+49
-18
@@ -19,6 +19,7 @@ local Sets = {
|
||||
["35"] = require("@tests/gen/blend_35"),
|
||||
["41"] = require("@tests/gen/blend_41"),
|
||||
cur = require("@tests/gen/blend_cur"),
|
||||
old = require("@tests/gen/blend_old"), -- Blender 1.66 - 2.04 demo files (big-endian among them)
|
||||
}
|
||||
|
||||
local t = T.suite("blend")
|
||||
@@ -146,7 +147,8 @@ end
|
||||
local checked = { worlds = 0, meshes = 0, normals = 0, weights = 0, shapes = 0, frames = 0, skinned = 0, curves = 0, materials = 0, cameras = 0, lights = 0 }
|
||||
local samples = {}
|
||||
|
||||
for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
t:check(#Sets.old >= 4, "the old demo files were fetched (" .. #Sets.old .. ")")
|
||||
for _, tag in { "old", "279", "293", "35", "41", "cur" } do
|
||||
for _, c in Sets[tag] do
|
||||
local ref = c.ref
|
||||
local label = c.name
|
||||
@@ -334,8 +336,10 @@ for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
for k, s in mi.shapes do
|
||||
if k ~= refIndex then
|
||||
ti += 1
|
||||
t:check(near(mesh.weights[ti], s.value, 1e-6), label .. ": " .. o.name .. " " .. s.name .. " weight")
|
||||
for ci, cv in cs do
|
||||
if not o.shapeAction then -- animated values are checked per frame below
|
||||
t:check(near(mesh.weights[ti], s.value, 1e-6), label .. ": " .. o.name .. " " .. s.name .. " weight")
|
||||
end
|
||||
for ci, cv in if mi.cornerVerts then cs else {} do
|
||||
local tgt = cv[1].targets and cv[1].targets[ti]
|
||||
local get = tgt and read(tgt.POSITION)
|
||||
local v = mi.cornerVerts[ci]
|
||||
@@ -391,9 +395,11 @@ for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
tex = x
|
||||
end
|
||||
end
|
||||
if im.packed > 0 then
|
||||
t:check(tex and tex.data and buffer.len(tex.data) == im.packed and tex.mime == "image/png", label .. ": packed image " .. im.name)
|
||||
elseif tex then
|
||||
if not tex then
|
||||
continue -- not used by a node material (legacy texture slots, face images)
|
||||
elseif im.packed > 0 then
|
||||
t:check(tex.data and buffer.len(tex.data) == im.packed and (tag == "old" or tex.mime == "image/png"), label .. ": packed image " .. im.name)
|
||||
else
|
||||
t:eq(tex.uri, "outside.png", label .. ": external image uri")
|
||||
t:check(tex.data and buffer.len(tex.data) == #Files["outside.png"], label .. ": external image bytes through opts.files")
|
||||
end
|
||||
@@ -432,17 +438,26 @@ for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
checked.lights += 1
|
||||
end
|
||||
-- triangles: the glTF exporter's meshes
|
||||
if gl.meshes then
|
||||
local curves = false -- the exporter turns curves / text / surfaces / metaballs into meshes; we do not
|
||||
for _, o in ref.objects do
|
||||
curves = curves or o.type == "CURVE" or o.type == "FONT" or o.type == "SURFACE" or o.type == "META"
|
||||
end
|
||||
if gl.meshes and not curves then
|
||||
-- per instance (both sides share meshes differently)
|
||||
local want = 0
|
||||
for _, m in gl.meshes do
|
||||
for _, p in m.primitives do
|
||||
want += gl.accessors[p.indices + 1].count // 3
|
||||
for _, n in gl.nodes or {} do
|
||||
if n.mesh then
|
||||
for _, p in gl.meshes[n.mesh + 1].primitives do
|
||||
want += gl.accessors[p.indices + 1].count // 3
|
||||
end
|
||||
end
|
||||
end
|
||||
local got = 0
|
||||
for _, m in scene.meshes do
|
||||
for _, p in m.primitives do
|
||||
got += p.indices.count // 3
|
||||
for _, n in scene.nodes do
|
||||
if n.mesh then
|
||||
for _, p in scene.meshes[n.mesh].primitives do
|
||||
got += p.indices.count // 3
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(got, want, label .. ": triangles (glTF exporter)")
|
||||
@@ -471,9 +486,16 @@ for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
ob = v
|
||||
end
|
||||
end
|
||||
local curves = B.curves(file, ob and ob:deref("adt", 0, "AnimData")) or {}
|
||||
t:eq(#curves, #refCurves, label .. ": " .. o.name .. " f-curves")
|
||||
local curves = B.curves(file, ob and ob:deref("adt", 0, "AnimData"))
|
||||
if not curves and ob and not ob:has("adt") then
|
||||
curves = B.ipo(file, ob:deref("ipo", 0, "Ipo")) -- before 2.5
|
||||
end
|
||||
curves = curves or {}
|
||||
for _, rc in refCurves do
|
||||
local p = rc.path
|
||||
if not (p == "location" or p == "scale" or string.find(p, "rotation") or string.find(p, "^delta_")) then
|
||||
continue -- only the transform curves are read
|
||||
end
|
||||
local cur = nil
|
||||
for _, x in curves do
|
||||
if x.path == rc.path and x.index == rc.index then
|
||||
@@ -492,14 +514,21 @@ for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
end
|
||||
-- the animations sampled at whole frames: posed world matrices, bones, shape key values, skinned positions
|
||||
if #ref.poses > 0 then
|
||||
t:check(#scene.animations > 0, label .. ": animations")
|
||||
local animated = false -- transform or shape key curves (others, such as an object colour, are not read)
|
||||
for _, list in ref.actions do
|
||||
for _, rc in list do
|
||||
local p = rc.path
|
||||
animated = animated or p == "location" or p == "scale" or string.find(p, "rotation") ~= nil or string.find(p, "key_blocks") ~= nil
|
||||
end
|
||||
end
|
||||
t:check(#scene.animations > 0 or not animated, label .. ": animations")
|
||||
local caches = {}
|
||||
for _, p in ref.poses do
|
||||
local frame = p.frame
|
||||
if frame ~= math.floor(frame) or frame > 20 then
|
||||
continue
|
||||
end
|
||||
local time = (frame - src.frameZero) / src.fps
|
||||
local time = (frame - (src.frameZero or src.frameStart)) / src.fps
|
||||
local pose = {}
|
||||
for ai, a in scene.animations do
|
||||
caches[ai] = caches[ai] or {}
|
||||
@@ -523,7 +552,9 @@ for _, tag in { "279", "293", "35", "41", "cur" } do
|
||||
local i = objects[name]
|
||||
local w = pose[i] and pose[i].weights or scene.nodes[i].weights
|
||||
local want = table.move(values, 2, #values, 1, {})
|
||||
t:check(nearList(w, want, 1e-3), label .. ": frame " .. frame .. " " .. name .. " shape values " .. fmt(w) .. " / " .. fmt(want))
|
||||
-- pre-2.5 IPO bezier keys: Blender recomputes auto handles when converting them; we keep the stored ones
|
||||
local eps = if tag == "old" then 1e-2 else 1e-3
|
||||
t:check(nearList(w, want, eps), label .. ": frame " .. frame .. " " .. name .. " shape values " .. fmt(w) .. " / " .. fmt(want))
|
||||
end
|
||||
for name, want in p.skinned or {} do
|
||||
-- morph, then skin: sum of w * jointWorld * inverseBind * position
|
||||
|
||||
Reference in new issue
Block a user