model/blend: vertex and triangle parents, parent_type / parent_bone / parent_vertices / matrix_parent_inverse, widget flags, modifier stacks with Blender's type names and the armature modifier's settings

This commit is contained in:
4DBug committed 2026-11-03 04:57:53 -06:00
1 parent a9874e9f54
commit 2c2a52e4b3
11 files changed
+1003 -405

No files matched your search

+139 -14
View File
@@ -167925,8 +167925,8 @@ __FILES["model/blend/Read"] = function()
local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, __compat.bit32, __compat.type, __compat.__iter --!strict
-- model/blend/Read: a parsed .blend (containers/Blend) to a Scene: the current scene's objects (from its collections,
-- or Base list before 2.8) with their transforms (euler orders, quaternion, axis angle, delta transforms, parent
-- inverse; bone parents), meshes (model/blend/Mesh, model/blend/Prim), materials (Principled BSDF node or the legacy
-- colour), images (packed bytes or external uris), cameras, lights (watts to candela / lux as Blender's glTF exporter
-- inverse; bone, vertex and triangle parents), meshes (model/blend/Mesh, model/blend/Prim), materials (Principled
-- BSDF node or the legacy colour), images (packed bytes or external uris), cameras, lights (watts to candela / lux as Blender's glTF exporter
-- does: 683 lm/W, / 4 pi for point and spot), armatures (bones to joint nodes with the saved pose, rig data through
-- model/blend/Armature), skins, shape keys and actions sampled into channels.
local Blend = __include("containers/Blend")
@@ -167960,7 +167960,7 @@ local Scene = Types.Scene
local OB_EMPTY, OB_MESH, OB_LAMP, OB_CAMERA, OB_ARMATURE = 0, 1, 10, 11, 25
local PARSKEL, PARBONE = 4, 7
local PARSKEL, PARVERT1, PARVERT3, PARBONE = 4, 5, 6, 7
local LA_LOCAL, LA_SUN, LA_SPOT, LA_HEMI, LA_AREA = 0, 1, 2, 3, 4
local EULER = { [1] = "xyz", [2] = "xzy", [3] = "yxz", [4] = "yzx", [5] = "zxy", [6] = "zyx" }
local TYPE_NAME= {
@@ -168478,6 +168478,57 @@ local function skins(ctx)
end
end
-- Object.partype as the API names it (PARSKEL is "ARMATURE" under an armature, "LATTICE" under a lattice)
local PARENT_TYPE= { [0] = "OBJECT", [4] = "ARMATURE", [5] = "VERTEX", [6] = "VERTEX_3", [7] = "BONE" }
-- vertex positions of a mesh object (its own data, no modifiers or shape keys), for vertex parents; nil if unavailable
local function parentVertices(ctx, par, indices)
if (par:num("type") or -1) ~= OB_MESH then
return nil
end
local me = par:deref("data", 0, "Mesh")
local ok, poly = pcall(Mesh.read, ctx.f, me , { maxVertices = ctx.opts.maxVertices })
if not ok or not poly or not me then
return nil
end
local out = {}
for k, i in __iter(indices) do
if i < 0 or i >= poly.nv then
return nil
end
out[k] = { buffer.readf32(poly.pos, i * 12), buffer.readf32(poly.pos, i * 12 + 4), buffer.readf32(poly.pos, i * 12 + 8) }
end
return out
end
-- Blender's tri_to_quat (math_geom.cc) as a rotation matrix: z along the triangle normal, x along v1 -> v2
local function triFrame(v1, v2, v3)
local function sub(a, b)
return { a[1] - b[1], a[2] - b[2], a[3] - b[3] }
end
local function norm(a)
local l = math.sqrt(a[1] * a[1] + a[2] * a[2] + a[3] * a[3])
return l > 0 and{ a[1] / l, a[2] / l, a[3] / l } or a
end
local e1, e2 = sub(v2, v1), sub(v3, v2) -- normal_tri_v3: (v1 - v2) x (v2 - v3)
local a, b = sub(v1, v2), sub(v2, v3)
local no = norm({ a[2] * b[3] - a[3] * b[2], a[3] * b[1] - a[1] * b[3], a[1] * b[2] - a[2] * b[1] })
local n = norm({ no[2], -no[1], 0 })
if n[1] == 0 and n[2] == 0 then
n = { 1, 0, 0 }
end
local angle = -0.5 * math.acos(math.clamp(no[3], -1, 1))
local q1 = { n[1] * math.sin(angle), n[2] * math.sin(angle), 0, math.cos(angle) } -- x, y, z, w
local m1 = Mat4.fromQuat(q1)
local inv = Mat4.inverseFull(m1) or Mat4.identity()
local vx, vy = Mat4.point(inv, e1[1], e1[2], e1[3])
local l = math.sqrt(vx * vx + vy * vy)
local angle2 = (l > 0 and{(0.5 * math.atan2(vy / l, vx / l) )}or{0
})[1] local q2 = { 0, 0, math.sin(angle2), math.cos(angle2) }
local _ = e2
return Mat4.mul(m1, Mat4.fromQuat(q2))
end
local function object(ctx, ob, under, depth)
local addr = ob.block and ob.block.addr
if addr ~= nil and ctx.objects[addr] then
@@ -168488,6 +168539,7 @@ local addr = ob.block and ob.block.addr
end
local parentOb = ob:deref("parent", 0, "Object")
local parentNode= under
local vertexFallback = false
local prefix = Mat4.identity()
local partype = ob:num("partype") or 0
if parentOb then
@@ -168496,6 +168548,7 @@ local addr = ob.block and ob.block.addr
parentNode = pn
prefix = mat4(ob, "parentinv") or Mat4.identity()
if partype == PARBONE then
-- the joint node is the bone's head frame (rest x pose); Blender parents to the tail
local info = parentOb.block and ctx.armatures[parentOb.block.addr]
local bone = ob:str("parsubstr") or ""
local j = info and info.names[bone]
@@ -168504,6 +168557,26 @@ local addr = ob.block and ob.block.addr
local len = info.bones[j]:num("length") or 0
prefix = Mat4.mul(Mat4.translation(0, len, 0), prefix)
end
elseif partype == PARVERT1 or partype == PARVERT3 then
local idx = { ob:num("par1") or 0 }
if partype == PARVERT3 then
idx = { ob:num("par1") or 0, ob:num("par2") or 0, ob:num("par3") or 0 }
end
local vs = parentVertices(ctx, parentOb, idx)
if vs and partype == PARVERT3 then
local m = triFrame(vs[1], vs[2], vs[3])
m[13], m[14], m[15] = (vs[1][1] + vs[2][1] + vs[3][1]) / 3, (vs[1][2] + vs[2][2] + vs[3][2]) / 3, (vs[1][3] + vs[2][3] + vs[3][3]) / 3
prefix = Mat4.mul(m, prefix)
elseif vs then
-- the parent's rotation and scale do not apply: world = T(parent world x vertex) x parentinv x basis,
-- expressed under the parent node for its saved world matrix
local pw = worldOf(ctx, pn)
local wx, wy, wz = Mat4.point(pw, vs[1][1], vs[1][2], vs[1][3])
local inv = Mat4.inverseFull(pw) or Mat4.identity()
prefix = Mat4.mul(Mat4.mul(inv, Mat4.translation(wx, wy, wz)), prefix)
else
vertexFallback = true
end
end
end
end
@@ -168523,6 +168596,20 @@ local addr = ob.block and ob.block.addr
local t = ob:num("type") or -1
local extras= { type = TYPE_NAME[t] or tostring(t) }
node.extras = { blend = extras }
if parentOb then
local pt = PARENT_TYPE[partype] or tostring(partype)
if partype == PARSKEL and (parentOb:num("type") or -1) == 22 then
pt = "LATTICE"
end
extras.parent_type = pt
extras.matrix_parent_inverse = mat4(ob, "parentinv")
if partype == PARBONE then
extras.parent_bone = ob:str("parsubstr")
elseif partype == PARVERT1 or partype == PARVERT3 then
extras.parent_vertices = partype == PARVERT1 and{ ob:num("par1") or 0 } or{ ob:num("par1") or 0, ob:num("par2") or 0, ob:num("par3") or 0 }
extras.parent_vertices_unresolved = vertexFallback and true or nil -- parented to the object instead
end
end
local lay = ob:num("lay")
if lay and ctx.f.version < 280 then
extras.layers = lay
@@ -168535,14 +168622,6 @@ local addr = ob.block and ob.block.addr
if bit32.band(vis, 4) ~= 0 then
extras.hide_render = true
end
local mods = {}
for _, md in __iter(ob:list("modifiers")) do
table.insert(mods, { name = md:sub("modifier") and ((md:sub("modifier") )):str("name") or md:str("name") or "", type = (string.gsub(md.struct.name, "ModifierData$", "")) })
end
if #mods > 0 then
extras.modifiers = mods
ctx.meta.modifiers[node.name or tostring(idx)] = mods
end
if t == OB_ARMATURE then
armature(ctx, ob, idx)
elseif t == OB_CAMERA then
@@ -168556,18 +168635,60 @@ local addr = ob.block and ob.block.addr
end
-- ------------------------------------------------------------------------------------------------ rigs
-- ModifierData.type names as the API gives them, from the struct name ("SimpleDeformModifierData" -> SIMPLE_DEFORM)
local MOD_NAME= {
LaplacianSmooth = "LAPLACIANSMOOTH", LaplacianDeform = "LAPLACIANDEFORM", WeightVGEdit = "VERTEX_WEIGHT_EDIT",
WeightVGMix = "VERTEX_WEIGHT_MIX", WeightVGProximity = "VERTEX_WEIGHT_PROXIMITY", UVProject = "UV_PROJECT",
UVWarp = "UV_WARP", MeshSeqCache = "MESH_SEQUENCE_CACHE", Softbody = "SOFT_BODY", ParticleSystem = "PARTICLE_SYSTEM",
ParticleInstance = "PARTICLE_INSTANCE", Fluidsim = "FLUID_SIMULATION", Smoke = "SMOKE", Surface = "SURFACE",
MeshToVolume = "MESH_TO_VOLUME", VolumeToMesh = "VOLUME_TO_MESH",
}
-- the modifier stack (ModifierData.mode: eModifierMode_Realtime 1, Render 2); ARMATURE with its settings
-- (ArmatureModifierData.deformflag: ARM_DEF_VGROUP 1, ENVELOPE 2, QUATERNION 4, INVERT_VGROUP 16)
local function modifiers(ob, resolve)
local out = {}
for _, md in __iter(ob:list("modifiers")) do
local head = md:sub("modifier") or md
local raw = string.gsub(md.struct.name, "ModifierData$", "")
local kind = MOD_NAME[raw] or string.upper((string.gsub(raw, "(%l)(%u)", "%1_%2")))
local mode = head:num("mode") or 3
local e= { type = kind, name = head:str("name") or "" }
e.show_viewport = bit32.band(mode, 1) ~= 0
e.show_render = bit32.band(mode, 2) ~= 0
if kind == "ARMATURE" then
local flag = md:num("deformflag") or 1
e.object = resolve(md:deref("object", 0, "Object"))
e.use_vertex_groups = bit32.band(flag, 1) ~= 0
e.use_bone_envelopes = bit32.band(flag, 2) ~= 0
e.use_deform_preserve_volume = bit32.band(flag, 4) ~= 0
e.invert_vertex_group = bit32.band(flag, 16) ~= 0
e.use_multi_modifier = (md:num("multi") or 0) ~= 0
local vg = md:str("defgrp_name")
e.vertex_group = (vg and vg ~= "" and{vg }or{nil
})[1] end
table.insert(out, e)
end
return out
end
local function rigs(ctx)
local target, shape = resolver(ctx, false), resolver(ctx, true)
for node = 1, #ctx.scene.nodes do
local __DARKLUA_CONTINUE_16=false repeat local ob = ctx.objView[node]
local __DARKLUA_CONTINUE_17=false repeat local ob = ctx.objView[node]
local extras = ob and (ctx.scene.nodes[node].extras ).blend
if not ob or not extras then
__DARKLUA_CONTINUE_16=true break
__DARKLUA_CONTINUE_17=true break
end
local list = Armature.constraints(ob, target)
if #list > 0 then
extras.constraints = list
end
local mods = modifiers(ob, target)
if #mods > 0 then
extras.modifiers = mods
ctx.meta.modifiers[ctx.scene.nodes[node].name or tostring(node)] = mods
end
local id = ob:sub("id")
extras.props = Driver.props(id and id:deref("properties", 0, "IDProperty"))
local drivers = Driver.list(ctx.f, ob:deref("adt", 0, "AnimData"), target)
@@ -168610,7 +168731,7 @@ __DARKLUA_CONTINUE_16=true break
end
end
end
__DARKLUA_CONTINUE_16=true until true if not __DARKLUA_CONTINUE_16 then break end end
__DARKLUA_CONTINUE_17=true until true if not __DARKLUA_CONTINUE_17 then break end end
end
-- every custom shape object: its node (and mesh) when it is in the scene, else its mesh only
@@ -168624,6 +168745,10 @@ local list = {}
if node then
w.node = node
w.mesh = ctx.scene.nodes[node].mesh
local e = ctx.scene.nodes[node].extras
if e and e.blend then
e.blend.widget = true
end
elseif t == OB_MESH then
w.mesh = (meshOf(ctx, ob, true))
end