model/blend: comments to specs only, a type fix; build: regenerate bundles
This commit is contained in:
1 parent
08f3b1c5bd
commit
be397639ea
9 files changed
+8471
-1102
No files matched your search
+3690
File diff suppressed because it is too large.
Load diff
+543
-535
File diff suppressed because it is too large.
Load diff
+3690
File diff suppressed because it is too large.
Load diff
+543
-535
File diff suppressed because it is too large.
Load diff
@@ -131,7 +131,6 @@ local function decompress(buf: buffer, o: number, n: number, opts: Options?): (b
|
||||
end
|
||||
|
||||
local function header(b: buffer, n: number): (number?, number, boolean, number, number, string?)
|
||||
-- -> header size, pointer size, little, version, format | nil, error
|
||||
if n < 12 or readstring(b, 0, 7) ~= "BLENDER" then
|
||||
return nil, 0, false, 0, 0, "not a .blend file (no BLENDER magic)"
|
||||
end
|
||||
@@ -425,7 +424,6 @@ function Blend.structOf(f: File, blk: Block): Struct?
|
||||
return f.dna.structs[blk.sdna + 1]
|
||||
end
|
||||
|
||||
-- element i (0-based) of a block, as its own SDNA struct or as `as` (a struct name)
|
||||
function Blend.struct(f: File, blk: Block, i: number?, as: string?): View?
|
||||
local s = if as then f.dna.byName[as] else f.dna.structs[blk.sdna + 1]
|
||||
if not s then
|
||||
@@ -535,8 +533,6 @@ local function scalar(f: File, t: string, size: number, o: number): number
|
||||
return readu8(b, o) -- char, uchar, uint8_t, bool
|
||||
end
|
||||
|
||||
-- a number (scalars), a string (char arrays), an array of numbers (other arrays), a View (embedded structs), the
|
||||
-- pointer key (pointers); nil when no field has any of the names
|
||||
function View.get(self: View, ...: string): any
|
||||
local fld = find(self, { ... })
|
||||
if not fld then
|
||||
@@ -619,7 +615,6 @@ function View.target(self: View, ...: string): Block?
|
||||
return Blend.resolve(self.file, self:ptr(...), self.block)
|
||||
end
|
||||
|
||||
-- the struct a pointer field points at (element i, 0-based), typed by the field's type or `as`
|
||||
function View.deref(self: View, name: string | { string }, i: number?, as: string?): View?
|
||||
local names: { any } = if type(name) == "table" then name else { name }
|
||||
local fld = find(self, names)
|
||||
@@ -639,7 +634,6 @@ function View.deref(self: View, name: string | { string }, i: number?, as: strin
|
||||
return Blend.struct(f, blk, i, sname)
|
||||
end
|
||||
|
||||
-- all elements of a pointer-to-array field: the block's count (or byte size / struct size when that is larger)
|
||||
function View.array(self: View, name: string | { string }, as: string?): { View }
|
||||
local first = self:deref(name, 0, as)
|
||||
if not first or not first.block then
|
||||
@@ -655,7 +649,6 @@ function View.array(self: View, name: string | { string }, as: string?): { View
|
||||
return out
|
||||
end
|
||||
|
||||
-- a pointer to an array of pointers (Material **mat, ActionLayer **layer_array, ...): n keys (default: the block size)
|
||||
function View.pointers(self: View, name: string | { string }, n: number?): { any }
|
||||
local names: { any } = if type(name) == "table" then name else { name }
|
||||
local fld = find(self, names)
|
||||
@@ -679,7 +672,6 @@ function View.pointers(self: View, name: string | { string }, n: number?): { any
|
||||
return out
|
||||
end
|
||||
|
||||
-- a ListBase field (first / last) walked through each element's `next`, typed as `as` or the first block's struct
|
||||
function View.list(self: View, name: string | { string }, as: string?, limit: number?): { View }
|
||||
local names: { any } = if type(name) == "table" then name else { name }
|
||||
local lb = self:sub(table.unpack(names))
|
||||
@@ -715,7 +707,6 @@ function View.list(self: View, name: string | { string }, as: string?, limit: nu
|
||||
return out
|
||||
end
|
||||
|
||||
-- the bytes a pointer field points at: (block offset, block size) in file.buf
|
||||
function View.data(self: View, ...: string): (number?, number)
|
||||
local blk = self:target(...)
|
||||
if not blk then
|
||||
@@ -724,7 +715,6 @@ function View.data(self: View, ...: string): (number?, number)
|
||||
return blk.offset, blk.size
|
||||
end
|
||||
|
||||
-- a NUL-terminated string behind a char pointer
|
||||
function View.cstring(self: View, ...: string): string?
|
||||
local blk = self:target(...)
|
||||
if not blk then
|
||||
@@ -735,7 +725,6 @@ function View.cstring(self: View, ...: string): string?
|
||||
return if z then string.sub(s, 1, z - 1) else s
|
||||
end
|
||||
|
||||
-- an ID's name without its two-letter code ("OBCube" -> "Cube")
|
||||
function View.idName(self: View): string?
|
||||
local id = self:sub("id")
|
||||
local n = if id then id:str("name") else self:str("name")
|
||||
|
||||
@@ -61,7 +61,6 @@ local function curve(f: Blend.File, fc: Blend.View): Curve?
|
||||
return { path = path, index = fc:num("array_index") or 0, keys = keys, extend = fc:num("extend") or 0, modifiers = #mods }
|
||||
end
|
||||
|
||||
-- the f-curves an AnimData plays from its action (nil when it has none)
|
||||
function Anim.curves(f: Blend.File, adt: Blend.View?): ({ Curve }?, string?)
|
||||
if not adt then
|
||||
return nil, nil
|
||||
@@ -207,7 +206,6 @@ local function bezier(a: Key, b: Key, x: number): number
|
||||
return bezierAt(y0, y1, y2, y3, t)
|
||||
end
|
||||
|
||||
-- Penner easing: t in 0..d, from b by c
|
||||
local function bounceOut(t: number, b: number, c: number, d: number): number
|
||||
t /= d
|
||||
if t < 1 / 2.75 then
|
||||
@@ -334,7 +332,6 @@ function Anim.range(curves: { Curve }): (number, number)
|
||||
return lo, hi
|
||||
end
|
||||
|
||||
-- "pose.bones[\"name\"].location" -> "name", "location"
|
||||
function Anim.split(path: string): (string?, string?, string)
|
||||
local kind, name, rest = string.match(path, '^(%w[%w_%.]-)%["(.-)"%]%.([%w_]+)$')
|
||||
if kind and name and rest then
|
||||
|
||||
@@ -129,7 +129,6 @@ local function find(layers: { Layer }, domain: string, kind: string?, name: stri
|
||||
return nil
|
||||
end
|
||||
|
||||
-- a raw pointer field (Mesh.mvert, ...) as a layer when no CustomData layer has it
|
||||
local function legacy(f: Blend.File, me: Blend.View, field: string, kind: string, domain: string): Layer?
|
||||
local blk = me:target(field)
|
||||
local st = f.dna.byName[kind]
|
||||
@@ -139,7 +138,6 @@ local function legacy(f: Blend.File, me: Blend.View, field: string, kind: string
|
||||
return { name = "", domain = domain, kind = kind, offset = blk.offset, bytes = blk.size, elem = st.size }
|
||||
end
|
||||
|
||||
-- reads element i (0-based) of a layer as number component c (0-based) of a generic kind
|
||||
local function reader(f: Blend.File, L: Layer): (number, number) -> number
|
||||
local b = f.buf
|
||||
local o, elem, k = L.offset, L.elem, L.kind
|
||||
@@ -166,7 +164,6 @@ local function reader(f: Blend.File, L: Layer): (number, number) -> number
|
||||
end
|
||||
Mesh.reader = reader
|
||||
|
||||
-- a field of an SDNA-struct layer: element i -> value (number) of component c
|
||||
local function fieldReader(f: Blend.File, L: Layer, field: string): ((number, number) -> number)?
|
||||
local st = f.dna.byName[L.kind]
|
||||
local fld = st and st.byName[field]
|
||||
@@ -249,7 +246,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
if nv > maxV or nl > maxV or nt > maxV or np > maxV then
|
||||
return nil, "limit: mesh over maxVertices"
|
||||
end
|
||||
-- positions
|
||||
local pos = buffer.create(nv * 12)
|
||||
local P = find(layers, "point", "float3", "position")
|
||||
local mv = find(layers, "point", "MVert") or legacy(f, me, "mvert", "MVert", "point")
|
||||
@@ -342,7 +338,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
end
|
||||
poly.cornerVert = corner.verts
|
||||
poly.nc = nl
|
||||
-- material index and smooth flags
|
||||
local mats = buffer.create(np * 4)
|
||||
local smooth = buffer.create(np)
|
||||
local mi = find(layers, "face", "int32", "material_index")
|
||||
@@ -396,12 +391,10 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
corner.tess = tessCorner
|
||||
end
|
||||
local nc = poly.nc
|
||||
-- corner index -> the element index in the source corner layers
|
||||
local remap: buffer? = corner.remap
|
||||
local function src(i: number): number
|
||||
return if remap then buffer.readu32(remap, i * 4) else i
|
||||
end
|
||||
-- UV maps
|
||||
for _, L in layers do
|
||||
if L.domain == "corner" and (L.kind == "float2" or L.kind == "MLoopUV") and string.sub(L.name, 1, 1) ~= "." then
|
||||
local r = if L.kind == "float2" then reader(f, L) else fieldReader(f, L, "uv")
|
||||
@@ -455,7 +448,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
end
|
||||
end
|
||||
end
|
||||
-- colour attributes: f32 x 4 linear per element
|
||||
for _, L in layers do
|
||||
if (L.domain == "corner" or L.domain == "point") and (L.kind == "color" or L.kind == "bytecolor") and string.sub(L.name, 1, 1) ~= "." then
|
||||
local n = if L.domain == "corner" then nc else nv
|
||||
@@ -489,8 +481,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
table.insert(poly.colors, { name = "Col", domain = "corner", data = tessColors(f, corner.tess :: buffer, nc, tf.offset, tf.bytes, tf.elem, col.offset, 4, not f.little, true) })
|
||||
end
|
||||
end
|
||||
-- MLoopCol layers carry the CD_PROP_BYTE_COLOR type (17) and are read above as bytecolor (r, g, b, a bytes)
|
||||
-- custom normals
|
||||
local cn = find(layers, "corner", "short2", "custom_normal")
|
||||
for _, L in layers do
|
||||
if not cn and L.domain == "corner" and L.cdtype == 41 then
|
||||
|
||||
@@ -37,7 +37,6 @@ function Normals.decode(poly: any, lnor: buffer, fanId: buffer, fans: Fans, _ang
|
||||
local id = readu32(fanId, c * 4)
|
||||
if not done[id] then
|
||||
done[id] = true
|
||||
-- walk back to the fan's first corner (crossing each corner's previous edge), then forward
|
||||
local start = c
|
||||
local cyclic = false
|
||||
local guard = 0
|
||||
@@ -71,7 +70,6 @@ function Normals.decode(poly: any, lnor: buffer, fanId: buffer, fans: Fans, _ang
|
||||
end
|
||||
local v = readu32(cv, start * 4)
|
||||
local nx, ny, nz = readf32(lnor, start * 12), readf32(lnor, start * 12 + 4), readf32(lnor, start * 12 + 8)
|
||||
-- the fan's edges in order: the first corner's previous edge, then every corner's next edge
|
||||
local corners = { start }
|
||||
local edges = { readu32(cv, readu32(prv, start * 4) * 4) }
|
||||
local q = start
|
||||
|
||||
@@ -84,7 +84,6 @@ end
|
||||
|
||||
export type Props = { [string]: { number } } -- location / rotation_euler / ... as Blender data paths, rotmode
|
||||
|
||||
-- an object's or pose bone's transform properties, keyed by their RNA names
|
||||
local function objectProps(ob: Blend.View): Props
|
||||
-- fields missing from old files take their neutral value
|
||||
local function v(fallback: { number }, ...: string): { number }
|
||||
@@ -611,7 +610,8 @@ local function animations(ctx: Ctx)
|
||||
end
|
||||
end
|
||||
for meshIdx, sk in ctx.meta.shapeKeys do
|
||||
local curves, name = Anim.curves(f, sk.key and sk.key:deref("adt", 0, "AnimData"))
|
||||
local found, name = Anim.curves(f, sk.key and sk.key:deref("adt", 0, "AnimData"))
|
||||
local curves: { Curve }? = found
|
||||
if not curves and sk.key and not sk.key:has("adt") then
|
||||
local names = {}
|
||||
for i, s in sk.shapes do
|
||||
@@ -624,9 +624,10 @@ local function animations(ctx: Ctx)
|
||||
for _, c in mc or {} do
|
||||
if string.sub(c.path, 1, 11) == "shape_keys." then
|
||||
c.path = string.sub(c.path, 12)
|
||||
curves = curves or {}
|
||||
local list: { Curve } = curves or {}
|
||||
curves = list
|
||||
name = name or mname
|
||||
table.insert(curves :: { Curve }, c)
|
||||
table.insert(list, c)
|
||||
end
|
||||
end
|
||||
if curves and #curves > 0 and name then
|
||||
@@ -857,7 +858,6 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- objects whose parent sits in a collection go under the parent; others under their (first) collection
|
||||
local firstUnder: { [any]: number? } = {}
|
||||
for _, e in order do
|
||||
local a = e.ob.block and e.ob.block.addr
|
||||
@@ -870,7 +870,6 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?)
|
||||
local u = a ~= nil and firstUnder[a] or nil
|
||||
object(ctx, e.ob, if u and u > 0 then u else nil, 0)
|
||||
end
|
||||
-- meshes after every object exists (armatures first, for skins)
|
||||
for node = 1, #scene.nodes do
|
||||
local ob = ctx.objView[node]
|
||||
if ob and (ob:num("type") or -1) == OB_MESH then
|
||||
|
||||
Reference in new issue
Block a user