model/blend, containers/Blend: read .blend files (2.4x-5.x headers, SDNA structs by name, gzip/zstd), scenes with meshes of every storage generation, materials, images, cameras, lights, armatures, skins, shape keys, actions
This commit is contained in:
1 parent
01c044805f
commit
2e82b7d0af
15 files changed
+4855
-2
No files matched your search
@@ -0,0 +1,75 @@
|
||||
--!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 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/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
|
||||
function BlendFormat.sniff(r: Bytes.Reader): number
|
||||
if r:peek(7) == "BLENDER" then
|
||||
return 1
|
||||
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.evaluate = Anim.evaluate
|
||||
BlendFormat.curves = Anim.curves
|
||||
|
||||
return BlendFormat
|
||||
Reference in new issue
Block a user