model/blend: rig data (bone head / tail / roll, deform, collections and layers, colours, custom shapes and widgets, pose locks and IK settings, constraint stacks), shape key fields and targetNames, relative keys and vertex groups in morph targets, muted keys, loose edges as lines

This commit is contained in:
4DBug committed 2026-11-02 21:49:19 -06:00
1 parent 03e7b21eca
commit 835ddde65b
7 files changed
+1241 -68

No files matched your search

+196 -7
View File
@@ -144,7 +144,50 @@ local function corners(mesh: any, facemats: { number }, faceSizes: { number }?):
return out
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 }
-- rig values against bpy: numbers within eps, lists element by element, "" in the reference for nil
local function same(got: any, want: any, eps: number): boolean
if want == "" and got == nil then
return true
end
if type(want) == "number" then
return type(got) == "number" and near(got, want, eps)
end
if type(want) == "table" then
if type(got) ~= "table" or #got ~= #want then
return false
end
for i = 1, #want do
if not same(got[i], want[i], eps) then
return false
end
end
return true
end
return got == want
end
local function angleNear(a: number?, b: number, eps: number): boolean
if not a then
return false
end
local d = (a - b) % (2 * math.pi)
return d < eps or 2 * math.pi - d < eps
end
local REQUIRED: { [string]: { string } } = {
IK = { "chain_count", "pole_target", "pole_subtarget", "pole_angle", "use_tail", "use_stretch", "iterations", "weight" },
COPY_ROTATION = { "use_x", "use_y", "use_z", "invert_x", "invert_y", "invert_z", "mix_mode", "euler_order" },
COPY_LOCATION = { "use_x", "use_y", "use_z", "invert_x", "invert_y", "invert_z", "use_offset", "head_tail" },
COPY_SCALE = { "use_x", "use_y", "use_z", "use_offset", "power" },
COPY_TRANSFORMS = { "mix_mode", "head_tail" },
DAMPED_TRACK = { "track_axis", "head_tail" },
LIMIT_ROTATION = { "use_limit_x", "use_limit_y", "use_limit_z", "min_x", "max_x", "min_z", "max_z", "use_transform_limit" },
LIMIT_LOCATION = { "use_min_x", "use_max_x", "use_min_z", "use_max_z", "min_x", "max_x", "min_z", "max_z", "use_transform_limit" },
CHILD_OF = { "inverse_matrix", "use_location_x", "use_rotation_y", "use_scale_x", "use_scale_z", "set_inverse_pending" },
}
local COMMON = { "type", "name", "influence", "mute", "target", "subtarget", "owner_space", "target_space" }
local checked = { rigs = 0, constraints = 0, worlds = 0, meshes = 0, normals = 0, weights = 0, shapes = 0, frames = 0, skinned = 0, curves = 0, materials = 0, cameras = 0, lights = 0 }
local samples = {}
t:check(#Sets.old >= 4, "the old demo files were fetched (" .. #Sets.old .. ")")
@@ -174,6 +217,8 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
t:check(i, label .. ": object " .. o.name)
if i then
t:check(nearList(W[i], o.world, 1e-4), label .. ": " .. o.name .. " world\n got " .. fmt(W[i]) .. "\n want " .. fmt(o.world))
local e = scene.nodes[i].extras.blend
t:check((e.hide_viewport == true) == o.hide_viewport and (e.hide_render == true) == o.hide_render, label .. ": " .. o.name .. " hide flags")
checked.worlds += 1
end
end
@@ -196,8 +241,12 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
t:eq(e.vertices, mi.vertices, label .. ": " .. o.name .. " vertices")
local tris = 0
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
local facePrims = 0
for _, p in mesh.primitives do
tris += p.indices.count // 3
if p.mode ~= "lines" then
tris += p.indices.count // 3
facePrims += 1
end
local P = read(p.attributes.POSITION)
for v = 0, p.attributes.POSITION.count - 1 do
for k = 1, 3 do
@@ -219,7 +268,7 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
end
t:eq(e.faces, mi.faces, label .. ": " .. o.name .. " faces")
t:eq(e.corners, mi.corners, label .. ": " .. o.name .. " corners")
t:eq(#mesh.primitives, (function()
t:eq(facePrims, (function()
local u, n = {}, 0
for _, m in mi.facemats do
if not u[m] then
@@ -331,20 +380,47 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
end
end
t:eq(#(mesh.weights or {}), #mi.shapes - 1, label .. ": " .. o.name .. " morph weights")
local names = mesh.extras.targetNames or {}
local keys = mesh.extras.blend.shape_keys or {}
t:eq(#keys, #mi.shapes, label .. ": " .. o.name .. " extras.blend.shape_keys")
t:eq(mesh.extras.blend.reference_key, mi.reference, label .. ": " .. o.name .. " reference key")
local groupWeight = {}
for v, list in mi.weights or {} do
for _, g in list do
groupWeight[g[1] .. "/" .. (v - 1)] = g[2]
end
end
for k, s in mi.shapes do
local e = keys[k] or {}
t:check(e.name == s.name and e.relative_key == s.relative and e.mute == s.mute and (e.vertex_group or "") == s.vertex_group
and near(e.slider_min, s.slider_min, 1e-6) and near(e.slider_max, s.slider_max, 1e-6)
and (o.shapeAction or near(e.value, s.value, 1e-6)), label .. ": " .. o.name .. " shape key " .. s.name .. " fields")
end
local ti = 0
local worst = 0
for k, s in mi.shapes do
if k ~= refIndex then
ti += 1
t:eq(names[ti], s.name, label .. ": " .. o.name .. " targetNames[" .. ti .. "]")
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")
t:check(near(mesh.weights[ti], if s.mute then 0 else s.value, 1e-6), label .. ": " .. o.name .. " " .. s.name .. " weight")
end
-- the delta from the relative key, times the key's vertex group weight
local rel = refIndex
for q, r in mi.shapes do
if r.name == s.relative then
rel = q
end
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]
for q = 1, 3 do
local want = s.co[v * 3 + q] - mi.shapes[refIndex].co[v * 3 + q]
local want = s.co[v * 3 + q] - mi.shapes[rel].co[v * 3 + q]
if s.vertex_group ~= "" then
want *= groupWeight[s.vertex_group .. "/" .. v] or 0
end
worst = math.max(worst, if get then math.abs(get(cv[2], q - 1) - want) else math.huge)
end
end
@@ -456,7 +532,9 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
for _, n in scene.nodes do
if n.mesh then
for _, p in scene.meshes[n.mesh].primitives do
got += p.indices.count // 3
if p.mode ~= "lines" then
got += p.indices.count // 3
end
end
end
end
@@ -474,6 +552,107 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
t:check(j and nearList(W[j], Scene.mul(aw, b["local"]), 1e-4), label .. ": bone " .. b.name)
end
end
-- rig data against bpy (Blender 5.x reads the old files through its versioning: layers and bone groups as
-- collections, bone group colours as bone colours)
local function nameOf(v: any): any
return if type(v) == "number" then scene.nodes[v].name else v
end
local function compareTable(got: { [string]: any }, want: { [string]: any }, keys: { string }, what: string)
for _, k in keys do
if want[k] == nil and (k == "target" or k == "subtarget") then
t:check(got[k] == nil, what .. ": no " .. k)
continue
end
t:check(want[k] ~= nil, what .. ": the reference has " .. k)
local g = got[k]
if k == "target" or k == "pole_target" or k == "custom_shape" then
g = nameOf(g)
end
t:check(same(g, want[k], 1e-5), what .. " " .. k .. ": got " .. tostring(g) .. ", want " .. tostring(want[k]))
end
end
local function compareColor(got: any, want: any, what: string)
if want.palette == "DEFAULT" then
t:check(got == nil, what .. ": default colour")
else
-- bpy's custom colours are the stored ones (zero for a theme set in 4.x); the reader gives the theme's
local custom = want.palette == "CUSTOM"
t:check(got and got.palette == want.palette and got.normal and (not custom or nearList(got.normal, want.normal, 1e-5) and nearList(got.select, want.select, 1e-5) and nearList(got.active, want.active, 1e-5)), what .. ": colour " .. want.palette)
end
end
local POSE_KEYS = { "custom_shape", "custom_shape_transform", "custom_shape_translation", "custom_shape_rotation_euler",
"custom_shape_scale_xyz", "use_custom_shape_bone_size", "lock_location", "lock_rotation", "lock_rotation_w",
"lock_rotations_4d", "lock_scale", "rotation_mode", "ik_stretch" }
for _, a in { "x", "y", "z" } do
for _, k in { "lock_ik_", "use_ik_limit_", "ik_min_", "ik_max_", "ik_stiffness_" } do
table.insert(POSE_KEYS, k .. a)
end
end
for armName, rig in ref.rigs or {} do
local ae = scene.nodes[objects[armName]].extras.blend
t:eq(#ae.bone_collections, #rig.collections, label .. ": " .. armName .. " bone collections")
for k, c in rig.collections do
local g = ae.bone_collections[k] or {}
t:check(g.name == c.name and g.visible == c.visible and (g.parent or "") == c.parent, label .. ": collection " .. c.name)
end
for _, b in rig.bones do
local e = scene.nodes[bones[b.name]].extras.blend
local what = label .. ": bone " .. b.name
t:check(e.deform == b.deform and e.use_connect == b.use_connect and e.show_wire == b.show_wire, what .. " flags")
t:check(nearList(e.head_local, b.head_local, 1e-5) and nearList(e.tail_local, b.tail_local, 1e-5), what .. " head / tail")
t:check(angleNear(e.roll, b.roll, 1e-4), what .. " roll " .. tostring(e.roll) .. " / " .. b.roll)
t:check(same(e.collections, b.collections, 0), what .. " collections " .. table.concat(e.collections, ",") .. " / " .. table.concat(b.collections, ","))
compareColor(e.color, b.color, what)
compareColor(e.pose_color, b.pose.color, what .. " pose")
compareTable(e, b.pose, POSE_KEYS, what)
t:eq(#e.constraints, #b.pose.constraints, what .. " constraints")
for k, rc in b.pose.constraints do
local c = e.constraints[k] or {}
local keys = table.clone(COMMON)
for _, x in REQUIRED[rc.type] or {} do
table.insert(keys, x)
end
compareTable(c, rc, keys, what .. " constraint " .. rc.name)
for key, v in c do -- everything else the reader gives, where bpy has it
if rc[key] ~= nil and type(v) ~= "table" and key ~= "target" and key ~= "pole_target" then
t:check(same(v, rc[key], 1e-5), what .. " constraint " .. rc.name .. " " .. key .. ": got " .. tostring(v) .. ", want " .. tostring(rc[key]))
end
end
checked.constraints += 1
end
end
checked.rigs += 1
end
if next(ref.rigs or {}) then
local mb = scene.meta.blend
for k, cs in ref.themeBoneColors do
local g = mb.bone_color_sets[k]
t:check(g and nearList(g.normal, cs.normal, 1e-5) and nearList(g.select, cs.select, 1e-5) and nearList(g.active, cs.active, 1e-5), label .. ": theme bone colours " .. k)
end
-- every custom shape is a widget: a scene node, or a mesh outside the scene; edge-only widgets as lines
local shapes = {}
for _, rig in ref.rigs do
for _, b in rig.bones do
if b.pose.custom_shape ~= "" then
shapes[b.pose.custom_shape] = true
end
end
end
for _, w in mb.widgets do
t:check(shapes[w.name], label .. ": widget " .. w.name .. " is a custom shape")
shapes[w.name] = nil
t:check(w.node == nil or scene.nodes[w.node].name == w.name, label .. ": widget node")
local m = w.mesh and scene.meshes[w.mesh]
local lines = 0
for _, p in m and m.primitives or {} do
if p.mode == "lines" then
lines += p.indices.count // 2
end
end
t:check(lines > 0, label .. ": widget " .. w.name .. " edges (" .. lines .. ")")
end
t:check(next(shapes) == nil, label .. ": every custom shape is listed")
end
-- f-curves through the evaluator, at every half frame (bezier, linear, constant, extrapolation)
local file = assert(Blend.open(buffer.fromstring(c.data)))
for _, o in ref.objects do
@@ -551,7 +730,16 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
for name, values in p.shapes 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, {})
local want = {}
for _, o in ref.objects do
if o.name == name then
for k, s in o.mesh.shapes do
if s.name ~= o.mesh.reference then
table.insert(want, if s.mute then 0 else values[k])
end
end
end
end
-- 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))
@@ -609,6 +797,7 @@ for _, tag in { "old", "279", "293", "35", "41", "cur" } do
end
end
end
t:note("checked: %d rigs, %d constraints", checked.rigs, checked.constraints)
t:note("checked: %d world matrices, %d meshes, %d corner normal sets, %d weight sets, %d shape key sets, %d f-curves, %d posed frames, %d skinned frames, %d materials, %d cameras, %d lights",
checked.worlds, checked.meshes, checked.normals, checked.weights, checked.shapes, checked.curves, checked.frames, checked.skinned, checked.materials, checked.cameras, checked.lights)