model/blend: drivers (object, object data, shape keys: variables, targets, keys, f-curve modifiers) and custom properties (objects, pose bones, bones) as data; f-curve modifiers on raw action curves; limit rotation use_legacy_behavior; 5.0 bone hide driver move for older files

This commit is contained in:
4DBug committed 2026-11-03 00:16:12 -06:00
1 parent b4e1ffebd4
commit a1a3971207
16 files changed
+2056 -605

No files matched your search

+121 -32
View File
@@ -136,44 +136,133 @@ local EASING: { [number]: string } = { [0] = "AUTO", [1] = "EASE_IN", [2] = "EAS
local HANDLE: { [number]: string } = { [0] = "FREE", [1] = "AUTO", [2] = "VECTOR", [3] = "ALIGNED", [4] = "AUTO_CLAMPED" }
local ACT_FRAME_RANGE = 0x1000
-- FModifier.type, flag (FMODIFIER_FLAG_MUTED 8, RANGERESTRICT 16, USEINFLUENCE 32)
local MOD_TYPE: { [number]: string } = {
[1] = "GENERATOR", [2] = "FNGENERATOR", [3] = "ENVELOPE", [4] = "CYCLES", [5] = "NOISE", [6] = "FILTER", [7] = "PYTHON",
[8] = "LIMITS", [9] = "STEPPED",
}
local CYCLE_MODE: { [number]: string } = { [0] = "NONE", [1] = "REPEAT", [2] = "REPEAT_OFFSET", [3] = "MIRROR" }
local FN_TYPE: { [number]: string } = { [0] = "SIN", [1] = "COS", [2] = "TAN", [3] = "SQRT", [4] = "LN", [5] = "SINC" }
local NOISE_BLEND: { [number]: string } = { [0] = "REPLACE", [1] = "ADD", [2] = "SUBTRACT", [3] = "MULTIPLY" }
local function bit(v: number?, mask: number): boolean
return bit32.band(v or 0, mask) ~= 0
end
local function modifier(f: Blend.File, m: Blend.View): { [string]: any }
local t = m:num("type") or 0
local flag = m:num("flag") or 0
local e: { [string]: any } = { type = MOD_TYPE[t] or tostring(t), name = m:str("name") }
e.mute = bit(flag, 8)
e.use_restricted_range = bit(flag, 16)
e.use_influence = bit(flag, 32)
e.influence = m:num("influence") or 1
e.frame_start, e.frame_end = m:num("sfra") or 0, m:num("efra") or 0
e.blend_in, e.blend_out = m:num("blendin") or 0, m:num("blendout") or 0
local d = m:deref("data", 0)
if not d then
return e
end
local kind = e.type
if kind == "GENERATOR" then
-- mode 0 expanded polynomial, 1 factorised; flag FCM_GENERATOR_ADDITIVE 1
e.mode = if (d:num("mode") or 0) == 1 then "POLYNOMIAL_FACTORISED" else "POLYNOMIAL"
e.use_additive = bit(d:num("flag"), 1)
e.poly_order = d:num("poly_order") or 1
local at, size = d:data("coefficients")
local n = math.min(d:num("arraysize") or 0, size // 4)
local c = {}
for k = 0, n - 1 do
c[k + 1] = f.f32(f.buf, (at :: number) + k * 4)
end
e.coefficients = c
elseif kind == "FNGENERATOR" then
e.function_type = FN_TYPE[d:num("type") or 0]
e.amplitude, e.phase_multiplier = d:num("amplitude"), d:num("phase_multiplier")
e.phase_offset, e.value_offset = d:num("phase_offset"), d:num("value_offset")
e.use_additive = bit(d:num("flag"), 1)
elseif kind == "ENVELOPE" then
e.reference_value, e.default_min, e.default_max = d:num("midval"), d:num("min"), d:num("max")
local points = {}
for _, p in d:array("data", "FCM_EnvelopeData") do
table.insert(points, { frame = p:num("time"), min = p:num("min"), max = p:num("max") })
end
e.control_points = points
elseif kind == "CYCLES" then
e.mode_before, e.mode_after = CYCLE_MODE[d:num("before_mode") or 1], CYCLE_MODE[d:num("after_mode") or 1]
e.cycles_before, e.cycles_after = d:num("before_cycles") or 0, d:num("after_cycles") or 0
elseif kind == "NOISE" then
e.blend_type = NOISE_BLEND[d:num("modification") or 0]
e.scale, e.strength, e.phase, e.offset, e.depth = d:num("size"), d:num("strength"), d:num("phase"), d:num("offset"), d:num("depth")
elseif kind == "LIMITS" then
-- flag FCM_LIMIT_XMIN 1, XMAX 2, YMIN 4, YMAX 8
local r = d:sub("rect")
local fl = d:num("flag")
e.use_min_x, e.use_max_x, e.use_min_y, e.use_max_y = bit(fl, 1), bit(fl, 2), bit(fl, 4), bit(fl, 8)
if r then
e.min_x, e.max_x, e.min_y, e.max_y = r:num("xmin"), r:num("xmax"), r:num("ymin"), r:num("ymax")
end
elseif kind == "STEPPED" then
-- the API's frame_start / frame_end here are the stepping limits (flag FCM_STEPPED_NO_BEFORE 1, NO_AFTER 2)
e.frame_step, e.frame_offset = d:num("step_size"), d:num("offset")
e.use_frame_start, e.use_frame_end = bit(d:num("flag"), 1), bit(d:num("flag"), 2)
e.frame_start, e.frame_end = d:num("start_frame"), d:num("end_frame")
end
return e
end
-- an f-curve as Blender's API shows it: { data_path, bone?, path, array_index, extrapolation, keys, modifiers }, and
-- its keys' frame span
function Anim.fcurve(f: Blend.File, fc: Blend.View): ({ [string]: any }, number, number)
local lo: number, hi: number = math.huge, -math.huge
local path = fc:cstring("rna_path") or ""
local kind, who, rest = Anim.split(path)
local keys: { { [string]: any } } = {}
for _, b in fc:array("bezt", "BezTriple") do
local v = b:vec("vec")
if v and #v >= 9 then
local x: number = v[4]
local k: { [string]: any } = { co = { x, v[5] }, handle_left = { v[1], v[2] }, handle_right = { v[7], v[8] } }
k.interpolation = INTERPOLATION[b:num("ipo") or 2] or "BEZIER"
k.easing = EASING[b:num("easing") or 0] or "AUTO"
k.handle_left_type, k.handle_right_type = HANDLE[b:num("h1") or 0], HANDLE[b:num("h2") or 0]
k.back, k.amplitude, k.period = b:num("back") or 1.70158, b:num("amplitude") or 0.8, b:num("period") or 4.1
table.insert(keys, k)
lo, hi = math.min(lo, x), math.max(hi, x)
end
end
if #keys == 0 then
for _, p in fc:array("fpt", "FPoint") do
local v = p:vec("vec")
if v and #v >= 2 then
local x: number = v[1]
local k: { [string]: any } = { co = { x, v[2] } }
k.interpolation = "LINEAR"
table.insert(keys, k)
lo, hi = math.min(lo, x), math.max(hi, x)
end
end
end
local entry: { [string]: any } = { data_path = path, path = if kind == "pose.bones" then rest else path, keys = keys }
entry.bone = if kind == "pose.bones" then who else nil
entry.array_index = fc:num("array_index") or 0
entry.extrapolation = if (fc:num("extend") or 0) == 1 then "LINEAR" else "CONSTANT"
local mods = {}
for _, m in fc:list("modifiers", "FModifier") do
table.insert(mods, modifier(f, m))
end
entry.modifiers = mods
return entry, lo, hi
end
-- an action as Blender's API shows it: frame_range (the manual range, else the keys' span, at least one frame) and
-- every f-curve with its keyframe points
function Anim.raw(f: Blend.File, act: Blend.View): { [string]: any }
local curves: { { [string]: any } } = {}
local lo: number, hi: number = math.huge, -math.huge
for _, fc in fcurves(f, act, nil) do
local path = fc:cstring("rna_path") or ""
local kind, who, rest = Anim.split(path)
local keys: { { [string]: any } } = {}
for _, b in fc:array("bezt", "BezTriple") do
local v = b:vec("vec")
if v and #v >= 9 then
local x: number = v[4]
local k: { [string]: any } = { co = { x, v[5] }, handle_left = { v[1], v[2] }, handle_right = { v[7], v[8] } }
k.interpolation = INTERPOLATION[b:num("ipo") or 2] or "BEZIER"
k.easing = EASING[b:num("easing") or 0] or "AUTO"
k.handle_left_type, k.handle_right_type = HANDLE[b:num("h1") or 0], HANDLE[b:num("h2") or 0]
k.back, k.amplitude, k.period = b:num("back") or 1.70158, b:num("amplitude") or 0.8, b:num("period") or 4.1
table.insert(keys, k)
lo, hi = math.min(lo, x), math.max(hi, x)
end
end
if #keys == 0 then
for _, p in fc:array("fpt", "FPoint") do
local v = p:vec("vec")
if v and #v >= 2 then
local x: number = v[1]
local k: { [string]: any } = { co = { x, v[2] } }
k.interpolation = "LINEAR"
table.insert(keys, k)
lo, hi = math.min(lo, x), math.max(hi, x)
end
end
end
local entry: { [string]: any } = { data_path = path, path = if kind == "pose.bones" then rest else path, keys = keys }
entry.bone = if kind == "pose.bones" then who else nil
entry.array_index = fc:num("array_index") or 0
entry.extrapolation = if (fc:num("extend") or 0) == 1 then "LINEAR" else "CONSTANT"
local entry, a, b = Anim.fcurve(f, fc)
lo, hi = math.min(lo, a), math.max(hi, b)
table.insert(curves, entry)
end
if bit32.band(act:num("flag") or 0, ACT_FRAME_RANGE) ~= 0 and act:has("frame_start") then
+2
View File
@@ -420,6 +420,8 @@ local function constraintData(e: { [string]: any }, kind: string, d: Blend.View,
end
e.use_transform_limit = bit(d:num("flag2"), 2)
e.euler_order = EULER_ORDER[d:num("euler_order") or 0]
-- LIMIT_ROT_LEGACY 8; files before 4.2 keep the old behaviour (Blender's versioning sets it)
e.use_legacy_behavior = bit(flag, 8) or d.file.version < 402
elseif kind == "LIMIT_LOCATION" or kind == "LIMIT_SCALE" then
-- LIMIT_XMIN 1, XMAX 2, YMIN 4, YMAX 8, ZMIN 16, ZMAX 32; flag2 LIMIT_TRANSFORM 2
for i, a in AXES do
+173
View File
@@ -0,0 +1,173 @@
--!strict
-- model/blend/Driver: drivers (AnimData.drivers: FCurves with a ChannelDriver, its DriverVars and DriverTargets) and
-- ID properties (IDProperty groups: custom properties) as data, in Blender's Python API names. Never evaluated.
-- Enums from DNA_anim_types.h and DNA_ID.h.
local Blend = require("@rosetta/containers/Blend")
local Anim = require("@rosetta/model/blend/Anim")
local Driver = {}
export type Resolve = (id: Blend.View?) -> (number | string)?
-- ChannelDriver.type; flag DRIVER_FLAG_USE_SELF 64
local TYPE: { [number]: string } = { [0] = "AVERAGE", [1] = "SCRIPTED", [2] = "SUM", [3] = "MIN", [4] = "MAX" }
local VAR_TYPE: { [number]: string } = { [0] = "SINGLE_PROP", [1] = "ROTATION_DIFF", [2] = "LOC_DIFF", [3] = "TRANSFORMS", [4] = "CONTEXT_PROP" }
-- DriverTarget.transChan, rotation_mode (DTAR_ROTMODE_*), context_property
local TRANSFORM: { [number]: string } = {
[0] = "LOC_X", [1] = "LOC_Y", [2] = "LOC_Z", [3] = "ROT_X", [4] = "ROT_Y", [5] = "ROT_Z", [6] = "SCALE_X",
[7] = "SCALE_Y", [8] = "SCALE_Z", [9] = "SCALE_AVG", [10] = "ROT_W",
}
local ROTATION_MODE: { [number]: string } = {
[0] = "AUTO", [1] = "XYZ", [2] = "XZY", [3] = "YXZ", [4] = "YZX", [5] = "ZXY", [6] = "ZYX", [7] = "QUATERNION",
[8] = "SWING_TWIST_X", [9] = "SWING_TWIST_Y", [10] = "SWING_TWIST_Z",
}
local CONTEXT: { [number]: string } = { [0] = "ACTIVE_SCENE", [1] = "ACTIVE_VIEW_LAYER" }
-- ID codes (two characters) to the API's id_type
local ID_TYPE: { [string]: string } = {
OB = "OBJECT", ME = "MESH", AR = "ARMATURE", KE = "KEY", MA = "MATERIAL", SC = "SCENE", WO = "WORLD", CA = "CAMERA",
LA = "LIGHT", TE = "TEXTURE", AC = "ACTION", GR = "COLLECTION", NT = "NODETREE", IM = "IMAGE", TX = "TEXT",
CU = "CURVE", LT = "LATTICE", MB = "META", SK = "SPEAKER", PA = "PARTICLE", LS = "LINESTYLE", GD = "GREASEPENCIL",
MC = "MOVIECLIP", MK = "MASK", PL = "PALETTE", CF = "CACHEFILE", WS = "WORKSPACE", LP = "LIGHT_PROBE", VO = "VOLUME",
PT = "POINTCLOUD", CV = "CURVES", GP = "GREASEPENCIL_V3", SO = "SOUND", VF = "FONT", BR = "BRUSH", LI = "LIBRARY",
WM = "WINDOWMANAGER", SN = "SCREEN",
}
local function bit(v: number?, mask: number): boolean
return bit32.band(v or 0, mask) ~= 0
end
local function idType(f: Blend.File, code: number?): string
if not code or code == 0 then
return "OBJECT"
end
local lo, hi = bit32.band(code, 255), bit32.band(bit32.rshift(code, 8), 255)
local a, b = if f.little then lo else hi, if f.little then hi else lo
local s = string.char(a, b)
return ID_TYPE[s] or s
end
-- a DriverTarget: id resolved to a node (objects in the scene) or the ID's name; flag DTAR_FLAG_LOCALSPACE 4,
-- DTAR_FLAG_LOCAL_CONSTS 8 (with LOCALSPACE: the API's LOCAL_SPACE, alone TRANSFORM_SPACE); options
-- DTAR_OPTION_USE_FALLBACK 1
local function target(f: Blend.File, t: Blend.View, resolve: Resolve): { [string]: any }
local flag = t:num("flag") or 0
local e: { [string]: any } = { id = resolve(t:deref("id", 0)), id_type = idType(f, t:num("idtype")) }
e.data_path = t:cstring("rna_path") or ""
e.bone_target = t:str("pchan_name") or ""
e.transform_type = TRANSFORM[t:num("transChan") or 0]
e.transform_space = if not bit(flag, 4) then "WORLD_SPACE" elseif bit(flag, 8) then "LOCAL_SPACE" else "TRANSFORM_SPACE"
e.rotation_mode = ROTATION_MODE[t:num("rotation_mode") or 0]
e.context_property = CONTEXT[t:num("context_property") or 0]
if t:has("fallback_value") then
e.use_fallback_value = bit(t:num("options"), 1)
e.fallback_value = t:num("fallback_value")
end
return e
end
-- the driver f-curves of an AnimData: Anim.fcurve's fields plus driver = { type, expression, use_self, variables }
function Driver.list(f: Blend.File, adt: Blend.View?, resolve: Resolve): { { [string]: any } }
local out = {}
if not adt then
return out
end
for _, fc in adt:list("drivers", "FCurve") do
local entry = Anim.fcurve(f, fc)
local d = fc:deref("driver", 0, "ChannelDriver")
if d then
local variables = {}
for _, v in d:list("variables", "DriverVar") do
local targets = {}
local list = v:get("targets")
local n = math.min(v:num("num_targets") or 1, if type(list) == "table" then #list else 0)
for k = 1, n do
table.insert(targets, target(f, list[k], resolve))
end
table.insert(variables, { name = v:str("name") or "", type = VAR_TYPE[v:num("type") or 0] or "SINGLE_PROP", targets = targets })
end
entry.driver = {
type = TYPE[d:num("type") or 0] or "AVERAGE",
expression = d:str("expression") or "",
use_self = bit(d:num("flag"), 64),
variables = variables,
}
end
table.insert(out, entry)
end
return out
end
-- ------------------------------------------------------------------------------------------------ ID properties
-- IDProperty.type: STRING 0, INT 1, FLOAT 2, ARRAY 5, GROUP 6, ID 7, DOUBLE 8, IDPARRAY 9, BOOLEAN 10
local function value(p: Blend.View, depth: number): any
local t = p:num("type") or -1
local f = p.file
local d = p:sub("data")
if not d or depth > 32 then
return nil
end
local valField = d.struct.byName.val
local valAt = valField and d.offset + valField.offset
if t == 1 then
return d:num("val")
elseif t == 10 then
return (d:num("val") or 0) ~= 0
elseif t == 2 then
return if valAt and valAt + 4 <= buffer.len(f.buf) then f.f32(f.buf, valAt) else nil
elseif t == 8 then
return if valAt and valAt + 8 <= buffer.len(f.buf) then f.f64(f.buf, valAt) else nil
elseif t == 0 then
local at, size = d:data("pointer")
if not at then
return ""
end
local s = buffer.readstring(f.buf, at, math.min(size, math.max(p:num("len") or size, 0)))
local z = string.find(s, "\0", 1, true)
return if z then string.sub(s, 1, z - 1) else s
elseif t == 5 then
local at, size = d:data("pointer")
local sub = p:num("subtype") or 1
local width = if sub == 8 then 8 elseif sub == 10 then 1 else 4
local n = math.min(p:num("len") or 0, size // width)
local out = table.create(n)
for k = 0, n - 1 do
local o = (at :: number) + k * width
out[k + 1] = if sub == 2 then f.f32(f.buf, o) elseif sub == 8 then f.f64(f.buf, o) elseif sub == 10 then buffer.readu8(f.buf, o) ~= 0 else f.i32(f.buf, o)
end
return out
elseif t == 6 then
local out = {}
for _, c in d:list("group", "IDProperty") do
local name = c:str("name")
if name then
out[name] = value(c, depth + 1)
end
end
return out
elseif t == 7 then
local id = d:deref("pointer", 0)
return if id then id:idName() else nil
elseif t == 9 then
local out = {}
for k, c in d:array("pointer", "IDProperty") do
out[k] = value(c, depth + 1)
end
return out
end
return nil
end
-- a properties group as { [name] = number | boolean | string | array | table } (the pre-3.0 "_RNA_UI" metadata left out)
function Driver.props(group: Blend.View?): { [string]: any }?
if not group or (group:num("type") or -1) ~= 6 then
return nil
end
local out = value(group, 0)
if type(out) ~= "table" then
return nil
end
out._RNA_UI = nil
return if next(out) ~= nil then out else nil
end
return Driver
+48 -19
View File
@@ -14,6 +14,7 @@ local Prim = require("@rosetta/model/blend/Prim")
local Anim = require("@rosetta/model/blend/Anim")
local Material = require("@rosetta/model/blend/Material")
local Armature = require("@rosetta/model/blend/Armature")
local Driver = require("@rosetta/model/blend/Driver")
local Read = {}
@@ -432,6 +433,25 @@ local function shapeExtras(shapes: { Mesh.Shape }, ref: number?): ({ string }, {
return names, list
end
-- an object as a constraint target or custom shape: its node, else its name; widgets records custom shapes
local function resolver(ctx: Ctx, widgets: boolean): Armature.Resolve
return function(ob: Blend.View?): Armature.Target?
if not ob then
return nil
end
local addr = ob.block and ob.block.addr
if widgets and addr ~= nil and not ctx.widgets[addr] then
ctx.widgets[addr] = ob
table.insert(ctx.widgetOrder, addr)
end
local node = if addr ~= nil then ctx.objects[addr] else nil
if node then
return node
end
return ob:idName()
end
end
-- the scene mesh of an object's mesh data (shared between objects with the same data, slots and armature)
local function meshOf(ctx: Ctx, ob: Blend.View, lines: boolean): (number?, ArmInfo?)
local f = ctx.f
@@ -474,6 +494,9 @@ local function meshOf(ctx: Ctx, ob: Blend.View, lines: boolean): (number?, ArmIn
end
end
if #shapes > 1 then
local key = me:deref("key", 0, "Key")
local kd = Driver.list(f, key and key:deref("adt", 0, "AnimData"), resolver(ctx, false))
extras.shape_key_drivers = if #kd > 0 then kd else nil
local names, list = shapeExtras(shapes, ref)
m.extras = { blend = extras, targetNames = names }
extras.shape_keys = list
@@ -609,25 +632,6 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number):
end
-- ------------------------------------------------------------------------------------------------ rigs
-- an object as a constraint target or custom shape: its node, else its name; widgets records custom shapes
local function resolver(ctx: Ctx, widgets: boolean): Armature.Resolve
return function(ob: Blend.View?): Armature.Target?
if not ob then
return nil
end
local addr = ob.block and ob.block.addr
if widgets and addr ~= nil and not ctx.widgets[addr] then
ctx.widgets[addr] = ob
table.insert(ctx.widgetOrder, addr)
end
local node = if addr ~= nil then ctx.objects[addr] else nil
if node then
return node
end
return ob:idName()
end
end
local function rigs(ctx: Ctx)
local target, shape = resolver(ctx, false), resolver(ctx, true)
for node = 1, #ctx.scene.nodes do
@@ -640,6 +644,29 @@ local function rigs(ctx: Ctx)
if #list > 0 then
extras.constraints = list
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)
extras.drivers = if #drivers > 0 then drivers else nil
local dataBlock = ob:target("data")
local data = dataBlock and Blend.struct(ctx.f, dataBlock)
local dataDrivers = if data and data:has("adt") then Driver.list(ctx.f, data:deref("adt", 0, "AnimData"), target) else {}
if ctx.f.version < 500 and dataBlock and dataBlock.code == "AR" then
-- Blender 5.0 moved bone visibility to pose bones: its versioning moves bones["x"].hide drivers to the object
local kept = {}
for _, d in dataDrivers do
local bone = string.match(d.data_path, '^bones%["(.-)"%]%.hide$')
if bone then
d.data_path, d.bone, d.path = "pose." .. d.data_path, bone, "hide"
table.insert(drivers, d)
else
table.insert(kept, d)
end
end
dataDrivers = kept
extras.drivers = if #drivers > 0 then drivers else nil
end
extras.data_drivers = if #dataDrivers > 0 then dataDrivers else nil
local info = ob.block and ctx.armatures[ob.block.addr]
if info then
local cols = Armature.collections(info.arm, info.bones, info.pose)
@@ -652,6 +679,8 @@ local function rigs(ctx: Ctx)
e[k] = v
end
e.collections = cols.byBone[name] or {}
e.bone_props = Driver.props(bone:deref("prop", 0, "IDProperty"))
e.props = if pc then Driver.props(pc:deref("prop", 0, "IDProperty")) else nil
if pc then
Armature.pose(e, pc, target, shape)
end
+2 -2
View File
@@ -6,9 +6,9 @@
-- attributes, custom normals, material slots; Principled BSDF materials; packed and external images; cameras; lights;
-- armatures, skins (vertex groups, top 4 weights), shape keys (relative keys, vertex groups); actions sampled into
-- channels, and every action's raw f-curves in meta.blend.actions; rig data (bone collections / layers, colours,
-- custom shapes, pose settings, constraint stacks) as extras.
-- custom shapes, pose settings, constraint stacks), drivers and custom properties as extras (data, never evaluated).
-- Not supported: modifiers (listed in meta.source.modifiers), curves / text / metaballs / grease pencil / volumes,
-- NLA strips, drivers, constraint evaluation, f-curve modifiers, linked libraries, collection instances.
-- NLA strips, driver and constraint evaluation, f-curve modifiers, linked libraries, collection instances.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")