Files
rosetta/src/Rosetta/model/blend/Armature.luau
T

533 lines
25 KiB
Luau

--!strict
-- model/blend/Armature: the rig data of an armature as Blender's Python API names it: bones (head / tail / roll in
-- armature space, deform, connect, wire), bone collections (4.x BoneCollection arrays and lists; before 4.0 the layers
-- and bone groups converted as Blender 4.0's versioning does), bone colours (BoneColor; bone group colours before 4.0),
-- pose bones (custom shapes, locks, rotation mode, IK settings) and constraint stacks (bConstraint and the data structs
-- of IK, copy rotation / location / scale / transforms, damped track, limit rotation / location / scale, child of,
-- action, transform, stretch to, armature, floor, limit distance, track to, locked track; the common fields for the
-- rest), bone inheritance and b-bone settings, the stored pose basis. Flags from DNA_armature_types.h,
-- DNA_action_types.h, DNA_constraint_types.h.
local Blend = require("@rosetta/containers/Blend")
local Armature = {}
export type Target = number | string -- a scene node index, or the object's name when it is not in the scene
export type Resolve = (ob: Blend.View?) -> Target?
export type Color = { palette: string, normal: { number }?, select: { number }?, active: { number }? }
-- Blender's default theme (userdef_default_theme.c): bone colour sets THEME01..THEME20 as normal, select, active
local THEME = {
{ 0x9a0000, 0xbd1111, 0xf70a0a }, { 0xf74018, 0xf66913, 0xfa9900 }, { 0x1e9109, 0x59b70b, 0x83ef1d },
{ 0x0a3694, 0x3667df, 0x5ec1ef }, { 0xa9294e, 0xc1416a, 0xf05d91 }, { 0x430c78, 0x543aa3, 0x8764d5 },
{ 0x24785a, 0x3c9579, 0x6fb6ab }, { 0x4b707c, 0x6a8691, 0x9bc2cd }, { 0xf4c90c, 0xeec236, 0xf3ff00 },
{ 0x1e2024, 0x484c56, 0xffffff }, { 0x6f2f6a, 0x9845be, 0xd330d6 }, { 0x6c8e22, 0x7fb022, 0xbbef5b },
{ 0x8d8d8d, 0xb0b0b0, 0xdedede }, { 0x834326, 0x8b5811, 0xbd6a11 }, { 0x08310e, 0x1c430b, 0x34622b },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
}
local function rgb(hex: number): { number }
return { bit32.rshift(hex, 16) / 255, bit32.band(bit32.rshift(hex, 8), 255) / 255, bit32.band(hex, 255) / 255 }
end
function Armature.colorSets(): { { normal: { number }, select: { number }, active: { number } } }
local out = {}
for i, s in THEME do
out[i] = { normal = rgb(s[1]), select = rgb(s[2]), active = rgb(s[3]) }
end
return out
end
local ROTMODE: { [number]: string } = { [0] = "QUATERNION", [1] = "XYZ", [2] = "XZY", [3] = "YXZ", [4] = "YZX", [5] = "ZXY", [6] = "ZYX", [-1] = "AXIS_ANGLE" }
local SPACE: { [number]: string } = { [0] = "WORLD", [1] = "LOCAL", [2] = "POSE", [3] = "LOCAL_WITH_PARENT", [5] = "CUSTOM", [6] = "LOCAL_OWNER_ORIENT" }
local EULER_ORDER: { [number]: string } = { [0] = "AUTO", [1] = "XYZ", [2] = "XZY", [3] = "YXZ", [4] = "YZX", [5] = "ZXY", [6] = "ZYX" }
local TRACK: { [number]: string } = { [0] = "TRACK_X", [1] = "TRACK_Y", [2] = "TRACK_Z", [3] = "TRACK_NEGATIVE_X", [4] = "TRACK_NEGATIVE_Y", [5] = "TRACK_NEGATIVE_Z" }
local ROT_MIX: { [number]: string } = { [0] = "REPLACE", [1] = "OFFSET", [2] = "ADD", [3] = "BEFORE", [4] = "AFTER" }
local TRANS_MIX: { [number]: string } = { [0] = "REPLACE", [1] = "BEFORE", [2] = "AFTER", [3] = "BEFORE_FULL", [4] = "BEFORE_SPLIT", [5] = "AFTER_FULL", [6] = "AFTER_SPLIT" }
local TYPE: { [number]: string } = {
[1] = "CHILD_OF", [2] = "TRACK_TO", [3] = "IK", [4] = "FOLLOW_PATH", [5] = "LIMIT_ROTATION", [6] = "LIMIT_LOCATION",
[7] = "LIMIT_SCALE", [8] = "COPY_ROTATION", [9] = "COPY_LOCATION", [10] = "COPY_SCALE", [11] = "PYTHON", [12] = "ACTION",
[13] = "LOCKED_TRACK", [14] = "LIMIT_DISTANCE", [15] = "STRETCH_TO", [16] = "FLOOR", [17] = "RIGID_BODY_JOINT",
[18] = "CLAMP_TO", [19] = "TRANSFORM", [20] = "SHRINKWRAP", [21] = "DAMPED_TRACK", [22] = "SPLINE_IK",
[23] = "COPY_TRANSFORMS", [24] = "MAINTAIN_VOLUME", [25] = "PIVOT", [26] = "FOLLOW_TRACK", [27] = "CAMERA_SOLVER",
[28] = "OBJECT_SOLVER", [29] = "TRANSFORM_CACHE", [30] = "ARMATURE", [31] = "GEOMETRY_ATTRIBUTE",
}
-- constraint types with a head_tail (bConstraint.headtail) in Blender's API
local HEAD_TAIL: { [string]: boolean } = {
COPY_LOCATION = true, COPY_TRANSFORMS = true, DAMPED_TRACK = true, TRACK_TO = true, LOCKED_TRACK = true,
STRETCH_TO = true, LIMIT_DISTANCE = true,
}
local CONSTRAINT_OFF, CONSTRAINT_BBONE_SHAPE = 0x200, 0x400
local BONE_CONNECTED, BONE_HIDDEN_P, BONE_NO_DEFORM, BONE_DRAWWIRE = 0x10, 0x40, 0x1000, 0x20000
local PCHAN_DRAW_NO_CUSTOM_BONE_SIZE = 1
-- bPoseChannel.protectflag (OB_LOCK_*) and ikflag (BONE_IK_*)
local LOCK_LOC, LOCK_ROT, LOCK_SCALE, LOCK_ROTW, LOCK_ROT4D = { 1, 2, 4 }, { 8, 16, 32 }, { 64, 128, 256 }, 512, 1024
local IK_NO_DOF, IK_LIMIT = { 1, 2, 4 }, { 8, 16, 32 }
local AXES = { "x", "y", "z" }
local XYZ: { [number]: string } = { [0] = "X", [1] = "Y", [2] = "Z" }
-- bActionConstraint.type, .mix_mode
local ACTION_CHANNEL: { [number]: string } = {
[0] = "ROTATION_X", [1] = "ROTATION_Y", [2] = "ROTATION_Z", [10] = "SCALE_X", [11] = "SCALE_Y", [12] = "SCALE_Z",
[20] = "LOCATION_X", [21] = "LOCATION_Y", [22] = "LOCATION_Z",
}
local ACTION_MIX: { [number]: string } = { [0] = "AFTER_FULL", [1] = "AFTER", [2] = "BEFORE", [3] = "BEFORE_FULL", [4] = "BEFORE_SPLIT", [5] = "AFTER_SPLIT" }
local MAP: { [number]: string } = { [0] = "LOCATION", [1] = "ROTATION", [2] = "SCALE" }
-- the driver target rotation modes (DTAR_ROTMODE_*)
local FROM_ROT: { [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 MIX_LOC: { [number]: string } = { [0] = "ADD", [1] = "REPLACE" }
local MIX_ROT: { [number]: string } = { [0] = "ADD", [1] = "REPLACE", [2] = "BEFORE", [3] = "AFTER" }
local MIX_SCALE: { [number]: string } = { [0] = "REPLACE", [1] = "MULTIPLY" }
local VOLUME: { [number]: string } = { [0] = "VOLUME_XZX", [1] = "VOLUME_X", [2] = "VOLUME_Z", [3] = "NO_VOLUME" }
local KEEP_AXIS: { [number]: string } = { [0] = "PLANE_X", [1] = "SWING_Y", [2] = "PLANE_Z" }
local FLOOR: { [number]: string } = { [0] = "FLOOR_X", [1] = "FLOOR_Y", [2] = "FLOOR_Z", [3] = "FLOOR_NEGATIVE_X", [4] = "FLOOR_NEGATIVE_Y", [5] = "FLOOR_NEGATIVE_Z" }
local DIST_MODE: { [number]: string } = { [0] = "LIMITDIST_INSIDE", [1] = "LIMITDIST_OUTSIDE", [2] = "LIMITDIST_ONSURFACE" }
local UP: { [number]: string } = { [0] = "UP_X", [1] = "UP_Y", [2] = "UP_Z" }
local LOCK: { [number]: string } = { [0] = "LOCK_X", [1] = "LOCK_Y", [2] = "LOCK_Z" }
-- Bone.inherit_scale_mode, bbone_prev_type / bbone_next_type, bbone_mapping_mode
local INHERIT_SCALE: { [number]: string } = { [0] = "FULL", [1] = "FIX_SHEAR", [2] = "AVERAGE", [3] = "NONE", [4] = "NONE_LEGACY", [5] = "ALIGNED" }
local HANDLE_TYPE: { [number]: string } = { [0] = "AUTO", [1] = "ABSOLUTE", [2] = "RELATIVE", [3] = "TANGENT" }
local MAPPING: { [number]: string } = { [0] = "STRAIGHT", [1] = "CURVED" }
local BONE_HINGE, BONE_NO_SCALE, BONE_NO_LOCAL_LOCATION, BONE_RELATIVE_PARENTING, BONE_ADD_PARENT_END_ROLL = 0x200, 0x8000, 0x400000, 0x800000, 0x1000000
local function bit(v: number?, mask: number): boolean
return bit32.band(v or 0, mask) ~= 0
end
local function bits(v: number?, masks: { number }): { boolean }
return { bit(v, masks[1]), bit(v, masks[2]), bit(v, masks[3]) }
end
local function vec3(v: Blend.View, name: string, fallback: { number }?): { number }?
local x = v:vec(name)
if x and #x >= 3 then
return { x[1], x[2], x[3] }
end
return fallback and table.clone(fallback)
end
local function text(v: Blend.View, name: string): string?
local s = v:str(name)
return if s and s ~= "" then s else nil
end
-- ThemeWireColor: char solid[4], select[4], active[4] (sRGB bytes)
local function wire(v: Blend.View?): { [string]: { number } }?
if not v then
return nil
end
local out: { [string]: { number } } = {}
for _, pair in { { "normal", "solid" }, { "select", "select" }, { "active", "active" } } do
local key, field = pair[1], pair[2]
local fld = v.struct.byName[field]
local o = fld and v.offset + fld.offset
if not o or o + 3 > buffer.len(v.file.buf) then
return nil
end
local b = v.file.buf
out[key] = { buffer.readu8(b, o) / 255, buffer.readu8(b, o + 1) / 255, buffer.readu8(b, o + 2) / 255 }
end
return out
end
-- palette: 0 default, 1..20 the theme sets, -1 custom (BoneColor.palette_index, bActionGroup.customCol); the colours
-- are the custom ones, or the default theme's set
local function color(index: number?, custom: Blend.View?): Color?
if not index or index == 0 then
return nil
end
local c: Color = { palette = if index < 0 then "CUSTOM" else string.format("THEME%02d", index) }
local w = if index < 0 then wire(custom) else nil
if w then
c.normal, c.select, c.active = w.normal, w.select, w.active
elseif index > 0 and THEME[index] then
local s = THEME[index]
c.normal, c.select, c.active = rgb(s[1]), rgb(s[2]), rgb(s[3])
end
return c
end
local function boneColor(v: Blend.View): Color?
local bc = v:sub("color")
if not bc then
return nil
end
return color(bc:num("palette_index"), bc:sub("custom"))
end
-- ------------------------------------------------------------------------------------------------ bones
export type Collections = {
list: { { name: string, visible: boolean, parent: string? } },
byBone: { [string]: { string } }, -- bone name -> its collections, in collection order
}
-- 4.1+: collection_array with child_index / child_count; 4.0: the collections list; before 4.0 the used layers
-- ("Layer N", hidden when the layer is off) and then the bone groups (hidden), as Blender's 4.0 versioning converts them
function Armature.collections(arm: Blend.View, bones: { Blend.View }, pose: Blend.View?): Collections
local out: Collections = { list = {}, byBone = {} }
local byAddr: { [any]: string } = {}
for _, b in bones do
if b.block then
byAddr[b.block.addr] = b:str("name") or ""
end
end
local function add(name: string, members: { string })
for _, m in members do
local l = out.byBone[m] or {}
out.byBone[m] = l
table.insert(l, name)
end
end
local VISIBLE = 1
local array = arm:has("collection_array") and arm:pointers("collection_array", arm:num("collection_array_num") or 0) or {}
local cols: { Blend.View } = {}
if #array > 0 then
for _, addr in array do
local blk = addr and Blend.resolve(arm.file, addr, arm.block)
local v = blk and Blend.struct(arm.file, blk, 0, "BoneCollection")
if v then
table.insert(cols, v)
end
end
elseif arm:has("collections") then
cols = arm:list("collections", "BoneCollection")
end
if #cols > 0 then
local parent: { [number]: string } = {}
for i, c in cols do
local first, n = c:num("child_index") or 0, c:num("child_count") or 0
for k = first + 1, math.min(first + n, #cols) do
parent[k] = c:str("name")
end
local _ = i
end
for i, c in cols do
local name = c:str("name") or ""
table.insert(out.list, { name = name, visible = bit(c:num("flags"), VISIBLE), parent = parent[i] })
local members = {}
for _, m in c:list("bones", "BoneCollectionMember") do
local n = byAddr[m:ptr("bone")]
if n then
table.insert(members, n)
end
end
add(name, members)
end
return out
end
local used, shown = arm:num("layer_used"), arm:num("layer")
if used then
for layer = 0, 31 do
local mask = bit32.lshift(1, layer)
if bit(used, mask) then
local name = "Layer " .. (layer + 1)
table.insert(out.list, { name = name, visible = bit(shown, mask) })
local members = {}
for _, b in bones do
if bit(b:num("layer"), mask) then
table.insert(members, b:str("name") or "")
end
end
add(name, members)
end
end
end
if pose then
local groups = pose:list("agroups", "bActionGroup")
local members: { [number]: { string } } = {}
for _, pc in pose:list("chanbase", "bPoseChannel") do
local g = pc:num("agrp_index") or 0
if g > 0 and groups[g] then
members[g] = members[g] or {}
table.insert(members[g], pc:str("name") or "")
end
end
for g, grp in groups do
local name = grp:str("name") or ""
table.insert(out.list, { name = name, visible = false })
add(name, members[g] or {})
end
end
return out
end
-- a Bone's own fields: head_local / tail_local / roll in armature space (the roll is already in arm_mat, the rest
-- matrix); color from the Bone (4.x) or from the bone group of its pose channel (before 4.0)
function Armature.bone(bone: Blend.View, pc: Blend.View?, pose: Blend.View?): { [string]: any }
local flag = bone:num("flag") or 0
local e: { [string]: any } = {
deform = not bit(flag, BONE_NO_DEFORM),
use_connect = bit(flag, BONE_CONNECTED),
show_wire = bit(flag, BONE_DRAWWIRE),
hide = bit(flag, BONE_HIDDEN_P),
head_local = vec3(bone, "arm_head"),
tail_local = vec3(bone, "arm_tail"),
roll = bone:num("arm_roll"),
use_inherit_rotation = not bit(flag, BONE_HINGE),
use_local_location = not bit(flag, BONE_NO_LOCAL_LOCATION),
use_relative_parent = bit(flag, BONE_RELATIVE_PARENTING),
}
local ism = bone:num("inherit_scale_mode")
e.inherit_scale = if ism then INHERIT_SCALE[ism] else (if bit(flag, BONE_NO_SCALE) then "NONE_LEGACY" else "FULL") -- BONE_NO_SCALE before 2.81
e.bbone_segments = bone:num("segments") or 1
e.bbone_x, e.bbone_z = bone:num("xwidth"), bone:num("zwidth")
e.bbone_easein, e.bbone_easeout = bone:num("ease1") or 1, bone:num("ease2") or 1
e.bbone_rollin, e.bbone_rollout = bone:num("roll1") or 0, bone:num("roll2") or 0
e.bbone_curveinx, e.bbone_curveinz = bone:num("curveInX") or 0, bone:num("curveInY") or 0 -- curveInY is the API's z
e.bbone_curveoutx, e.bbone_curveoutz = bone:num("curveOutX") or 0, bone:num("curveOutY") or 0
for _, side in { "in", "out" } do
local cap = if side == "in" then "In" else "Out"
local v = vec3(bone, "scale_" .. side)
if not v then
-- before 3.0: scaleIn (x) and scale_in_y (z); before 2.8 one value for both
local x = bone:num("scale" .. cap) or 1
v = { x, 1, bone:num("scale_" .. side .. "_y") or x }
end
e["bbone_scale" .. side] = v
end
e.bbone_handle_type_start = HANDLE_TYPE[bone:num("bbone_prev_type") or 0]
e.bbone_handle_type_end = HANDLE_TYPE[bone:num("bbone_next_type") or 0]
local prev, nxt = bone:deref("bbone_prev", 0, "Bone"), bone:deref("bbone_next", 0, "Bone")
e.bbone_custom_handle_start = if prev then prev:str("name") else nil
e.bbone_custom_handle_end = if nxt then nxt:str("name") else nil
e.use_endroll_as_inroll = if bone:has("bbone_flag") then bit(bone:num("bbone_flag"), 1) else bit(flag, BONE_ADD_PARENT_END_ROLL)
e.bbone_mapping_mode = MAPPING[bone:num("bbone_mapping_mode") or 0]
if bone:has("color") then
e.color = boneColor(bone)
elseif pc and pose then
local g = pc:num("agrp_index") or 0
local grp = if g > 0 then pose:list("agroups", "bActionGroup")[g] else nil
if grp then
e.color = color(grp:num("customCol"), grp:sub("cs"))
end
end
return e
end
-- ------------------------------------------------------------------------------------------------ constraints
local function constraintData(e: { [string]: any }, kind: string, d: Blend.View, resolve: Resolve)
local flag = d:num("flag") or 0
if kind == "IK" then
-- CONSTRAINT_IK_TIP / ROT / STRETCH / POS, NO_POS_X.. / NO_ROT_X..; type 0 copy pose, 1 distance
e.pole_target = resolve(d:deref("poletar", 0, "Object"))
e.pole_subtarget = text(d, "polesubtarget")
e.pole_angle = d:num("poleangle")
e.chain_count = d:num("rootbone")
e.iterations = d:num("iterations")
e.weight = d:num("weight")
e.orient_weight = d:num("orientweight")
e.use_tail = bit(flag, 1)
e.use_rotation = bit(flag, 2)
e.use_stretch = bit(flag, 16)
e.use_location = bit(flag, 32)
-- lock_* are the inverse of the NO_POS / NO_ROT bits, as Blender's API defines them
e.lock_location_x, e.lock_location_y, e.lock_location_z = not bit(flag, 256), not bit(flag, 512), not bit(flag, 1024)
e.lock_rotation_x, e.lock_rotation_y, e.lock_rotation_z = not bit(flag, 2048), not bit(flag, 4096), not bit(flag, 8192)
e.ik_type = if (d:num("type") or 0) == 1 then "DISTANCE" else "COPY_POSE"
e.distance = d:num("dist")
elseif kind == "COPY_ROTATION" then
-- ROTLIKE_X 1, Y 2, Z 4, X_INVERT 16, Y_INVERT 32, Z_INVERT 64, OFFSET 128 (mix_mode OFFSET before 2.82)
e.use_x, e.use_y, e.use_z = bit(flag, 1), bit(flag, 2), bit(flag, 4)
e.invert_x, e.invert_y, e.invert_z = bit(flag, 16), bit(flag, 32), bit(flag, 64)
e.euler_order = EULER_ORDER[d:num("euler_order") or 0]
local mix = d:num("mix_mode")
e.mix_mode = if mix then ROT_MIX[mix] else (if bit(flag, 128) then "OFFSET" else "REPLACE")
elseif kind == "COPY_LOCATION" then
-- LOCLIKE_X 1, Y 2, Z 4, X_INVERT 16, Y_INVERT 32, Z_INVERT 64, OFFSET 128
e.use_x, e.use_y, e.use_z = bit(flag, 1), bit(flag, 2), bit(flag, 4)
e.invert_x, e.invert_y, e.invert_z = bit(flag, 16), bit(flag, 32), bit(flag, 64)
e.use_offset = bit(flag, 128)
elseif kind == "COPY_SCALE" then
-- SIZELIKE_X 1, Y 2, Z 4, OFFSET 8, MULTIPLY 16 (use_add is its inverse), UNIFORM 32
e.use_x, e.use_y, e.use_z = bit(flag, 1), bit(flag, 2), bit(flag, 4)
e.use_offset = bit(flag, 8)
e.use_add = not bit(flag, 16)
e.use_make_uniform = bit(flag, 32)
e.power = d:num("power") or 1
elseif kind == "ACTION" then
-- flag: ACTCON_BONE_USE_OBJECT_ACTION 1, ACTCON_USE_EVAL_TIME 2
local act = d:deref("act", 0, "bAction")
e.action = if act then act:idName() else nil
e.transform_channel = ACTION_CHANNEL[d:num("type") or 20]
e.frame_start, e.frame_end = d:num("start") or 1, d:num("end") or 1
e.min, e.max = d:num("min") or 0, d:num("max") or 0
e.mix_mode = ACTION_MIX[d:num("mix_mode") or 0]
e.use_bone_object_action = bit(flag, 1)
e.use_eval_time = bit(flag, 2)
e.eval_time = d:num("eval_time") or 0
elseif kind == "TRANSFORM" then
e.map_from, e.map_to = MAP[d:num("from") or 0], MAP[d:num("to") or 0]
local map = d.struct.byName.map
for i, a in AXES do
local o = map and d.offset + map.offset + i - 1
e["map_to_" .. a .. "_from"] = XYZ[if o and o < buffer.len(d.file.buf) then buffer.readu8(d.file.buf, o) else i - 1]
end
e.use_motion_extrapolate = (d:num("expo") or 0) ~= 0
e.from_rotation_mode = FROM_ROT[d:num("from_rotation_mode") or 0]
e.to_euler_order = EULER_ORDER[d:num("to_euler_order") or 0]
e.mix_mode = MIX_LOC[d:num("mix_mode_loc") or 0]
e.mix_mode_rot = MIX_ROT[d:num("mix_mode_rot") or 0]
e.mix_mode_scale = MIX_SCALE[d:num("mix_mode_scale") or 0]
for _, side in { "from", "to" } do
for _, suffix in { "", "_rot", "_scale" } do
local lo, hi = d:vec(side .. "_min" .. suffix), d:vec(side .. "_max" .. suffix)
for i, a in AXES do
e[side .. "_min_" .. a .. suffix] = lo and lo[i] or 0
e[side .. "_max_" .. a .. suffix] = hi and hi[i] or 0
end
end
end
elseif kind == "STRETCH_TO" then
-- STRETCHTOCON_USE_BULGE_MIN 1, MAX 2
e.rest_length = d:num("orglength")
e.bulge = d:num("bulge")
e.volume = VOLUME[d:num("volmode") or 0]
e.keep_axis = KEEP_AXIS[d:num("plane") or 0]
e.use_bulge_min, e.use_bulge_max = bit(flag, 1), bit(flag, 2)
e.bulge_min, e.bulge_max, e.bulge_smooth = d:num("bulge_min"), d:num("bulge_max"), d:num("bulge_smooth")
elseif kind == "ARMATURE" then
-- CONSTRAINT_ARMATURE_QUATERNION 1, ENVELOPE 2, CUR_LOCATION 4
e.use_deform_preserve_volume, e.use_bone_envelopes, e.use_current_location = bit(flag, 1), bit(flag, 2), bit(flag, 4)
elseif kind == "FLOOR" then
-- flag: MINMAX_USEROT 4
e.floor_location = FLOOR[d:num("minmaxflag") or 2]
e.offset = d:num("offset") or 0
e.use_rotation = bit(flag, 4)
elseif kind == "LIMIT_DISTANCE" then
e.distance = d:num("dist")
e.limit_mode = DIST_MODE[d:num("mode") or 0]
elseif kind == "TRACK_TO" then
-- reserved1 the track axis, reserved2 the up axis; flags TARGET_Z_UP 1
e.track_axis = TRACK[d:num("reserved1") or 1]
e.up_axis = UP[d:num("reserved2") or 2]
e.use_target_z = bit(d:num("flags"), 1)
elseif kind == "LOCKED_TRACK" then
e.track_axis = TRACK[d:num("trackflag") or 1]
e.lock_axis = LOCK[d:num("lockflag") or 2]
elseif kind == "COPY_TRANSFORMS" then
e.mix_mode = TRANS_MIX[d:num("mix_mode") or 0]
e.remove_target_shear = bit(flag, 1)
elseif kind == "DAMPED_TRACK" then
e.track_axis = TRACK[d:num("trackflag") or 1]
elseif kind == "LIMIT_ROTATION" then
-- LIMIT_XROT 1, YROT 2, ZROT 4; flag2 LIMIT_TRANSFORM 2
for i, a in AXES do
e["use_limit_" .. a] = bit(flag, bit32.lshift(1, i - 1))
e["min_" .. a] = d:num(a .. "min")
e["max_" .. a] = d:num(a .. "max")
end
e.use_transform_limit = bit(d:num("flag2"), 2)
e.euler_order = EULER_ORDER[d:num("euler_order") or 0]
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
e["use_min_" .. a] = bit(flag, bit32.lshift(1, (i - 1) * 2))
e["use_max_" .. a] = bit(flag, bit32.lshift(2, (i - 1) * 2))
e["min_" .. a] = d:num(a .. "min")
e["max_" .. a] = d:num(a .. "max")
end
e.use_transform_limit = bit(d:num("flag2"), 2)
elseif kind == "CHILD_OF" then
-- CHILDOF_LOCX 1 .. SIZEZ 256, SET_INVERSE 512
for i, a in AXES do
e["use_location_" .. a] = bit(flag, bit32.lshift(1, i - 1))
e["use_rotation_" .. a] = bit(flag, bit32.lshift(8, i - 1))
e["use_scale_" .. a] = bit(flag, bit32.lshift(64, i - 1))
end
e.set_inverse_pending = bit(flag, 512)
local m = d:vec("invmat")
e.inverse_matrix = if m and #m == 16 then m else nil -- column-major
end
end
-- a constraint stack (ListBase of bConstraint) in evaluation order
function Armature.constraints(owner: Blend.View, resolve: Resolve): { { [string]: any } }
local out = {}
for _, c in owner:list("constraints", "bConstraint") do
local t = c:num("type") or 0
local kind = TYPE[t] or tostring(t)
local flag = c:num("flag") or 0
local e: { [string]: any } = {
type = kind,
name = c:str("name") or "",
influence = c:num("enforce") or 1,
mute = bit(flag, CONSTRAINT_OFF),
owner_space = SPACE[c:num("ownspace") or 0],
target_space = SPACE[c:num("tarspace") or 0],
}
if HEAD_TAIL[kind] then
e.head_tail = c:num("headtail") or 0
e.use_bbone_shape = bit(flag, CONSTRAINT_BBONE_SHAPE)
end
if e.owner_space == "CUSTOM" or e.target_space == "CUSTOM" then
e.space_object = resolve(c:deref("space_object", 0, "Object"))
e.space_subtarget = text(c, "space_subtarget")
end
local d = c:deref("data", 0)
if d then
if d:has("tar") then
e.target = resolve(d:deref("tar", 0, "Object"))
end
e.subtarget = text(d, "subtarget")
if d:has("targets") then
local targets = {}
for _, ct in d:list("targets", "bConstraintTarget") do
table.insert(targets, { target = resolve(ct:deref("tar", 0, "Object")), subtarget = text(ct, "subtarget"), weight = ct:num("weight") })
end
e.targets = targets
end
constraintData(e, kind, d, resolve)
end
table.insert(out, e)
end
return out
end
-- ------------------------------------------------------------------------------------------------ pose bones
-- a bPoseChannel's settings into e: constraint targets through resolve, the custom shape object through shape
function Armature.pose(e: { [string]: any }, pc: Blend.View, resolve: Resolve, shape: Resolve)
local protect = pc:num("protectflag") or 0
local ik = pc:num("ikflag") or 0
e.rotation_mode = ROTMODE[pc:num("rotmode") or 0] or "QUATERNION"
e.lock_location = bits(protect, LOCK_LOC)
e.lock_rotation = bits(protect, LOCK_ROT)
e.lock_rotation_w = bit(protect, LOCK_ROTW)
e.lock_rotations_4d = bit(protect, LOCK_ROT4D)
e.lock_scale = bits(protect, LOCK_SCALE)
local custom = pc:deref("custom", 0, "Object")
if custom then
e.custom_shape = shape(custom)
end
local tx = pc:deref("custom_tx", 0, "bPoseChannel")
e.custom_shape_transform = if tx then text(tx, "name") else nil
e.custom_shape_translation = vec3(pc, "custom_translation", { 0, 0, 0 })
e.custom_shape_rotation_euler = vec3(pc, "custom_rotation_euler", { 0, 0, 0 })
local s = pc:num("custom_scale") or 1 -- before 3.0: one uniform scale
e.custom_shape_scale_xyz = vec3(pc, "custom_scale_xyz", { s, s, s })
e.use_custom_shape_bone_size = not bit(pc:num("drawflag"), PCHAN_DRAW_NO_CUSTOM_BONE_SIZE)
e.custom_shape_wire_width = pc:num("custom_shape_wire_width")
e.ik_stretch = pc:num("ikstretch") or 0
local lo, hi, stiff = pc:vec("limitmin"), pc:vec("limitmax"), pc:vec("stiffness")
for i, a in AXES do
e["lock_ik_" .. a] = bit(ik, IK_NO_DOF[i])
e["use_ik_limit_" .. a] = bit(ik, IK_LIMIT[i])
e["ik_min_" .. a] = lo and lo[i] or -math.pi
e["ik_max_" .. a] = hi and hi[i] or math.pi
e["ik_stiffness_" .. a] = stiff and stiff[i] or 0
end
e.pose_color = if pc:has("color") then boneColor(pc) else nil
-- the stored basis (rotation_quaternion w, x, y, z; rotation_axis_angle angle, x, y, z)
e.location = vec3(pc, "loc", { 0, 0, 0 })
local q = pc:vec("quat")
e.rotation_quaternion = if q and #q >= 4 then { q[1], q[2], q[3], q[4] } else { 1, 0, 0, 0 }
e.rotation_euler = vec3(pc, "eul", { 0, 0, 0 })
local axis = vec3(pc, "rotAxis", { 0, 1, 0 }) :: { number }
e.rotation_axis_angle = { pc:num("rotAngle") or 0, axis[1], axis[2], axis[3] }
e.scale = vec3(pc, "size", nil) or vec3(pc, "scale", { 1, 1, 1 })
e.constraints = Armature.constraints(pc, resolve)
end
return Armature