Files
rosetta/src/Rosetta/model/blend/init.luau
T

95 lines
4.5 KiB
Luau

--!strict
-- model/blend: Blender .blend files (read only), every version from 2.4x to 5.x: plain, gzip or zstd compressed;
-- 32 / 64-bit pointers, either byte order; the 5.0+ header. New in rosetta (from the file layout, read through each
-- file's own SDNA). Supports: the scene's objects and collections, transforms with parent inverse and bone parents,
-- meshes of every storage generation (MFace, MPoly / MLoop, attribute layers, AttributeStorage) with UV maps, colour
-- attributes, custom normals, material slots; Principled BSDF materials; packed and external images; cameras; lights;
-- armatures, skins (vertex groups, top 4 weights), shape keys; actions sampled into channels.
-- Not supported: modifiers (listed in meta.source.modifiers), curves / text / metaballs / grease pencil / volumes,
-- NLA strips, drivers, constraints, f-curve modifiers, linked libraries, collection instances.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local Blend = require("@rosetta/containers/Blend")
local Deflate = require("@rosetta/codecs/compress/Deflate")
local Zstd = require("@rosetta/codecs/compress/Zstd")
local Read = require("@rosetta/model/blend/Read")
local Anim = require("@rosetta/model/blend/Anim")
-- ------------------------------------------------------------------------------------------------ contract
local BlendFormat = {
name = "blend",
category = "model",
extensions = { "blend" },
mime = { "application/x-blender" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Blend", "codecs/compress/Deflate", "codecs/compress/Zstd", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/Polygon" },
caps = { decode = true, encode = false, materials = true, textures = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true, polygons = true, compressed = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
triangulate: boolean?, -- default true (false: "polygons" primitives)
flipV: boolean?, -- default true (glTF's top-left UV origin)
collections: boolean?, -- collections as nodes (default true)
scene: string?, -- a scene by name (default: the file's current scene)
sampleStep: number?, -- frames between animation samples (default 1)
animations: boolean?, -- default true
resolve: ((uri: string) -> any)?, -- external images
files: { [string]: any }?,
maxBytes: number?, -- decompressed size limit (default 2^31)
maxVertices: number?, -- per mesh (default 2^25)
}
-- ------------------------------------------------------------------------------------------------ sniff
-- compressed files: gzip by peeking the first bytes; zstd by decompressing files up to 4 MB (larger zstd streams
-- score 0.5: picked for model loads, the archive otherwise). Ties with archive/gzip and archive/zst go to the extension.
function BlendFormat.sniff(r: Bytes.Reader): number
local head = r:peek(7)
if head == "BLENDER" then
return 1
end
local n = r.limit - r.base
if string.sub(head, 1, 3) == "\31\139\8" then
local inner = Deflate.peek(r.buf, r.base, n, 7)
return if inner and buffer.tostring(inner) == "BLENDER" then 1 else 0
end
if string.sub(head, 1, 4) == Zstd.MAGIC then
if n > 4 * 2 ^ 20 then
return 0.5
end
local out = Zstd.decompress(r.buf, r.base, n, { verify = false })
return if out and buffer.len(out) >= 7 and buffer.readstring(out, 0, 7) == "BLENDER" then 1 else 0
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
function BlendFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Blend.File?, Err.Err?)
return Blend.open(r.buf, r.base, r.limit - r.base, { maxBytes = opts and opts.maxBytes })
end
function BlendFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
local f, err = BlendFormat.open(r, opts)
if not f then
return nil, err
end
local ok, scene, msg = pcall(Read.scene, f, opts)
if not ok then
return nil, Err.from(tostring(scene))
end
if not scene then
return nil, Err.from(msg or "bad file")
end
return scene, nil
end
-- ------------------------------------------------------------------------------------------------ extras
BlendFormat.container = Blend
BlendFormat.evaluate = Anim.evaluate
BlendFormat.curves = Anim.curves
BlendFormat.ipo = Anim.ipo
return BlendFormat