diff --git a/build/Rosetta.luau b/build/Rosetta.luau index 703f7fe..4bae49f 100644 --- a/build/Rosetta.luau +++ b/build/Rosetta.luau @@ -162193,9 +162193,10 @@ __FILES["model/blend"] = function() -- file's own SDNA). Supports: the scene's objects and collections, transforms with parent inverse and bone parents, -- meshes of every storage generation (MFace, MPoly / MLoop, attribute layers, AttributeStorage) with UV maps, colour -- attributes, custom normals, material slots; Principled BSDF materials; packed and external images; cameras; lights; --- armatures, skins (vertex groups, top 4 weights), shape keys; actions sampled into channels. +-- armatures, skins (vertex groups, top 4 weights), shape keys (relative keys, vertex groups); actions sampled into +-- channels; rig data (bone collections / layers, colours, custom shapes, pose settings, constraint stacks) as extras. -- Not supported: modifiers (listed in meta.source.modifiers), curves / text / metaballs / grease pencil / volumes, --- NLA strips, drivers, constraints, f-curve modifiers, linked libraries, collection instances. +-- NLA strips, drivers, constraint evaluation, f-curve modifiers, linked libraries, collection instances. -- ------------------------------------------------------------------------------------------------ requires local Err = __include("core/Err") @@ -162221,6 +162222,7 @@ local BlendFormat = { type DecodeOptions = { triangulate: boolean?, -- default true (false: "polygons" primitives) flipV: boolean?, -- default true (glTF's top-left UV origin) + looseEdges: boolean?, -- edges outside faces as "lines" primitives (default false; always for custom shape widgets) collections: boolean?, -- collections as nodes (default true) scene: string?, -- a scene by name (default: the file's current scene) sampleStep: number?, -- frames between animation samples (default 1) @@ -162628,6 +162630,413 @@ end return Anim end +__FILES["model/blend/Armature"] = function() + +-- 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; the +-- common fields for the rest). Flags from DNA_armature_types.h, DNA_action_types.h, DNA_constraint_types.h. +local Blend = __include("containers/Blend") + +local Armature = {} + +type Target = number | string -- a scene node index, or the object's name when it is not in the scene +type Resolve = (ob: Blend.View?) -> Target? +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 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 +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"), + } + 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 + 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.power = d:num("power") or 1 + 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 + e.constraints = Armature.constraints(pc, resolve) +end + +return Armature +end __FILES["model/blend/Material"] = function() -- model/blend/Material: Blender materials to Scene PBR materials. With nodes: the Principled BSDF (the one feeding the @@ -163219,6 +163628,75 @@ end type Limits = { maxVertices: number? } +-- edge count and per-edge readers (vertex 1, vertex 2, sharp): ".edge_verts" / "sharp_edge" attributes or MEdge +local function edgeReaders(f: Blend.File, me: Blend.View, layers: { Layer }): (number, ((number) -> number)?, ((number) -> number)?, ((number) -> boolean)?) + local ne = count(me, "totedge", "edges_num") + local ev = find(layers, "edge", "int2", ".edge_verts") + local se = find(layers, "edge", "bool", "sharp_edge") + local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") + if ne == 0 then + return 0, nil, nil, nil + end + if ev then + local er = reader(f, ev) + local sr = if se then reader(f, se) else nil + return ne, function(i: number): number + return er(i, 0) + end, function(i: number): number + return er(i, 1) + end, function(i: number): boolean + return sr ~= nil and sr(i, 0) ~= 0 + end + elseif medge then + local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") + if a and b then + local ser = if se then reader(f, se) else nil + return ne, function(i: number): number + return a(i, 0) + end, function(i: number): number + return b(i, 0) + end, function(i: number): boolean + if ser then + return ser(i, 0) ~= 0 + end + return fl ~= nil and bit32.band(fl(i, 0), ME_SHARP) ~= 0 + end + end + end + return ne, nil, nil, nil +end + +-- edges no face uses, as u32 vertex pairs (rig widgets are usually edges only) +function Mesh.looseEdges(f: Blend.File, me: Blend.View, poly: Poly): buffer + local ne, v1, v2 = edgeReaders(f, me, poly.layers) + if not v1 or not v2 then + return buffer.create(0) + end + local nv = poly.nv + local used: { [number]: boolean } = {} + local cv, fs = poly.cornerVert, poly.faceStart + for fi = 0, poly.nf - 1 do + local s0, s1 = buffer.readu32(fs, fi * 4), buffer.readu32(fs, fi * 4 + 4) + for c = s0, s1 - 1 do + local a, b = buffer.readu32(cv, c * 4), buffer.readu32(cv, (if c + 1 < s1 then c + 1 else s0) * 4) + used[math.min(a, b) * nv + math.max(a, b)] = true + end + end + local out = {} + for i = 0, ne - 1 do + local a, b = v1(i), v2(i) + if a ~= b and a >= 0 and b >= 0 and a < nv and b < nv and not used[math.min(a, b) * nv + math.max(a, b)] then + table.insert(out, a) + table.insert(out, b) + end + end + local buf = buffer.create(#out * 4) + for k, v in out do + buffer.writeu32(buf, (k - 1) * 4, v) + end + return buf +end + function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, string?) local layers = Mesh.layers(f, me) local nv = count(me, "totvert", "verts_num") @@ -163498,10 +163976,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri poly.customNormals = d end -- sharp edges - local ne = count(me, "totedge", "edges_num") - local ev = find(layers, "edge", "int2", ".edge_verts") - local se = find(layers, "edge", "bool", "sharp_edge") - local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") local flags = me:num("flag") or 0 local smoothresh = me:num("smoothresh") local autoSmooth = f.version < 401 and bit32.band(flags, ME_AUTOSMOOTH) ~= 0 @@ -163509,27 +163983,8 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri poly.autoSmooth = smoothresh or math.rad(30) end local useSharp = f.version >= 401 or autoSmooth - if useSharp and ne > 0 then - local v1, v2, sharp - if ev and se then - local er, sr = reader(f, ev), reader(f, se) - v1 = function(i: number): number return er(i, 0) end - v2 = function(i: number): number return er(i, 1) end - sharp = function(i: number): boolean return sr(i, 0) ~= 0 end - elseif medge then - local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") - if a and b and fl then - v1 = function(i: number): number return a(i, 0) end - v2 = function(i: number): number return b(i, 0) end - local ser = if se then reader(f, se) else nil - sharp = function(i: number): boolean - if ser then - return ser(i, 0) ~= 0 - end - return bit32.band(fl(i, 0), ME_SHARP) ~= 0 - end - end - end + if useSharp then + local ne, v1, v2, sharp = edgeReaders(f, me, layers) if v1 and v2 and sharp then for i = 0, ne - 1 do if sharp(i) then @@ -163574,7 +164029,7 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri end -- shape keys: { name, value, relative (0-based), data: f32 x 3 per vertex } in file order, and the reference index -type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number } +type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number, vgroup: string? } function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, number?, Blend.View?) local key = me:deref("key", 0, "Key") if not key then @@ -163584,6 +164039,7 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num local refAddr = key:ptr("refkey") local out: { Shape } = {} local ref: number? = nil + local index = 0 for i, kb in blocks do local blk = kb:target("data") local d = buffer.create(nv * 12) @@ -163596,15 +164052,21 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num end end end - if kb.block and kb.block.addr == refAddr then + local isRef = kb.block ~= nil and kb.block.addr == refAddr + if isRef then ref = i + else + index += 1 end + local stored = kb:str("name") + -- unnamed in old files: named as Blender's 2.45 versioning does (Basis, Key 1, Key 2, ...) + local name: string = if stored and stored ~= "" then stored elseif isRef then "Basis" else "Key " .. index table.insert(out, { - -- unnamed in old files: Key 0 (the basis), Key 1, ... - name = kb:str("name") or ("Key " .. (i - 1)), value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, - mute = bit32.band(kb:num("flag") or 0, 2) ~= 0, + name = name, value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, + mute = bit32.band(kb:num("flag") or 0, 1) ~= 0, -- KEYBLOCK_MUTE min = kb:num("slidermin") or 0, max = kb:num("slidermax") or 1, + vgroup = if kb:str("vgroup") ~= "" then kb:str("vgroup") else nil, }) end if ref == nil and #out > 0 then @@ -164037,8 +164499,80 @@ end type Built = { mesh: Types.Mesh, slots: { number }, vertexSource: { { number } } } --- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints -function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: Skinning?, shapes: { MeshT.Shape }?, ref: number?): Built +-- morph targets of the output vertices (vsrc: their mesh vertices): each key minus its relative key, times the weight +-- of the key's vertex group, as Blender evaluates relative keys +local function targetDeltas(poly: MeshT.Poly, vsrc: { number }, shapes: { MeshT.Shape }, ref: number, groups: { string }?): { { [string]: Types.Accessor } } + local n = #vsrc + local targets: { { [string]: Types.Accessor } } = {} + for k, s in shapes do + if k ~= ref then + local rel = shapes[s.relative + 1] or shapes[ref] + local base = rel.data + local def: number? = nil + if s.vgroup and groups then + for gi, g in groups do + if g == s.vgroup then + def = gi - 1 + end + end + end + local t = table.create(n * 3) :: { number } + for i = 1, n do + local v = vsrc[i] + local w: number = 1 + if def then + w = 0 + local list = poly.weights and poly.weights[v + 1] or {} + for q = 1, #list - 1, 2 do + if list[q] == def then + w = list[q + 1] + end + end + end + for c = 0, 2 do + t[(i - 1) * 3 + c + 1] = (readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4)) * w + end + end + table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor }) + end + end + return targets +end + +-- a "lines" primitive of the given edges (u32 vertex pairs), with the morph targets the face primitives have +function Prim.lines(poly: MeshT.Poly, edges: buffer, shapes: { MeshT.Shape }?, ref: number?, groups: { string }?): Types.Primitive? + local ne = buffer.len(edges) // 8 + if ne == 0 then + return nil + end + local out: { [number]: number } = {} + local vsrc: { number } = {} + local I = buffer.create(ne * 8) + for k = 0, ne * 2 - 1 do + local v = readu32(edges, k * 4) + local o = out[v] + if not o then + o = #vsrc + out[v] = o + table.insert(vsrc, v) + end + writeu32(I, k * 4, o) + end + local n = #vsrc + local P = buffer.create(n * 12) + for i = 1, n do + buffer.copy(P, (i - 1) * 12, poly.pos, vsrc[i] * 12, 12) + end + local prim: Types.Primitive = { attributes = { POSITION = Scene.accessor(P, "f32", 3, n) }, indices = Scene.accessor(I, "u32", 1, ne * 2), mode = "lines" } + if shapes and ref and #shapes > 1 then + prim.targets = targetDeltas(poly, vsrc, shapes, ref, groups) + end + return prim +end + +-- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints; vgroups names the +-- vertex groups (def_nr + 1) for shape keys limited to one +function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: Skinning?, shapes: { MeshT.Shape }?, ref: number?, vgroups: { string }?): Built local triangulateOn = not (opts and opts.triangulate == false) local flipV = not (opts and opts.flipV == false) local withNormals = not (opts and opts.normals == false) @@ -164188,21 +164722,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S prim.material = mat end if shapes and ref and #shapes > 1 then - local base = shapes[ref].data - local targets: { { [string]: Types.Accessor } } = {} - for k, s in shapes do - if k ~= ref then - local t = table.create(n * 3) :: { number } - for i = 1, n do - local v = vsrc[i] - for c = 0, 2 do - t[(i - 1) * 3 + c + 1] = readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4) - end - end - table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor }) - end - end - prim.targets = targets + prim.targets = targetDeltas(poly, vsrc, shapes, ref, vgroups) end table.insert(mesh.primitives, prim) table.insert(sources, vsrc) @@ -164211,7 +164731,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S local w = {} for k, s in shapes do if k ~= ref then - table.insert(w, s.value) + table.insert(w, if s.mute then 0 else s.value) -- Blender skips muted keys end end mesh.weights = w @@ -164231,8 +164751,8 @@ __FILES["model/blend/Read"] = function() -- 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 --- does: 683 lm/W, / 4 pi for point and spot), armatures (bones to joint nodes with the saved pose), skins, shape keys --- and actions sampled into channels. +-- 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") local Types = __include("Types") local Mat4 = __include("codecs/model/Mat4") @@ -164241,6 +164761,7 @@ local Mesh = __include("model/blend/Mesh") local Prim = __include("model/blend/Prim") local Anim = __include("model/blend/Anim") local Material = __include("model/blend/Material") +local Armature = __include("model/blend/Armature") local Read = {} @@ -164250,6 +164771,7 @@ type M4 = { number } type Options = { triangulate: boolean?, flipV: boolean?, + looseEdges: boolean?, -- edges outside faces as "lines" primitives (default false; always for custom shape widgets) collections: boolean?, -- collections as nodes (default true) scene: string?, -- the scene to read (default: the file's current scene) sampleStep: number?, -- frames between animation samples (default 1) @@ -164400,6 +164922,18 @@ local function mat4(v: Blend.View?, ...: string): M4? end -- ------------------------------------------------------------------------------------------------ scene +type ArmInfo = { + joints: { number }, + names: { [string]: number }, + rest: { M4 }, + restLocal: { M4 }, + bones: { Blend.View }, + deform: { boolean }, + node: number, + poseNames: { [string]: Blend.View }, + arm: Blend.View, + pose: Blend.View?, +} type Ctx = { f: Blend.File, scene: Types.Scene, @@ -164411,7 +164945,9 @@ type Ctx = { meshes: { [string]: number }, materials: { [any]: number }, textures: { [any]: number }, - armatures: { [any]: { joints: { number }, names: { [string]: number }, rest: { M4 }, restLocal: { M4 }, bones: { Blend.View }, deform: { boolean }, node: number, poseNames: { [string]: Blend.View } } }, + armatures: { [any]: ArmInfo }, + widgets: { [any]: Blend.View }, -- custom shape objects by address + widgetOrder: { any }, prefix: { [number]: M4 }, -- node -> the matrix before its basis (parent inverse, bone tail offset) props: { [number]: Props }, meta: { [string]: any }, @@ -164458,7 +164994,7 @@ local function armature(ctx: Ctx, ob: Blend.View, node: number) end end end - local info = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames } + local info: ArmInfo = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames, arm = arm, pose = pose } local function walk(list: { Blend.View }, parentJoint: number?, parentRest: M4?, depth: number) if depth > 256 then return @@ -164628,20 +165164,37 @@ local function groupNames(ob: Blend.View, me: Blend.View): { string } return out end -local function mesh(ctx: Ctx, ob: Blend.View, node: number) +local function shapeExtras(shapes: { Mesh.Shape }, ref: number?): ({ string }, { { [string]: any } }) + local names: { string }, list: { { [string]: any } } = {}, {} + for k, sh in shapes do + if k ~= ref then + table.insert(names, sh.name) + end + local rel = shapes[sh.relative + 1] + local e: { [string]: any } = { name = sh.name, value = sh.value, slider_min = sh.min, slider_max = sh.max, mute = sh.mute } + e.relative_key = if rel then rel.name else nil + e.vertex_group = sh.vgroup + table.insert(list, e) + end + return names, list +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 local me = ob:deref("data", 0, "Mesh") if not me then - return + return nil, nil end local slots = slotMaterials(ctx, ob, me) local armOb = deformArmature(ctx, ob) local armInfo = armOb and armOb.block and ctx.armatures[armOb.block.addr] - local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) + local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) .. (if lines then ":lines" else "") local skin: Prim.Skinning? = nil + local groups = groupNames(ob, me) if armInfo then local joints = {} - for gi, name in groupNames(ob, me) do + for gi, name in groups do local j = armInfo.names[name] if j and armInfo.deform[j] then joints[gi - 1] = j - 1 @@ -164656,9 +165209,23 @@ local function mesh(ctx: Ctx, ob: Blend.View, node: number) error(err or "bad mesh", 0) end local shapes, ref = Mesh.shapes(f, me, poly.nv) - local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref) + local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref, groups) local m = built.mesh - m.extras = { blend = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } } + local extras: { [string]: any } = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } + m.extras = { blend = extras } + if lines then + local lp = Prim.lines(poly, Mesh.looseEdges(f, me, poly), shapes, ref, groups) + if lp then + table.insert(m.primitives, lp) + extras.looseEdges = (lp.indices :: Types.Accessor).count // 2 + end + end + if #shapes > 1 then + local names, list = shapeExtras(shapes, ref) + m.extras = { blend = extras, targetNames = names } + extras.shape_keys = list + extras.reference_key = if ref then shapes[ref].name else nil + end table.insert(ctx.scene.meshes, m) idx = #ctx.scene.meshes ctx.meshes[key] = idx @@ -164668,6 +165235,15 @@ local function mesh(ctx: Ctx, ob: Blend.View, node: number) ctx.meta.shapeKeys[idx] = { key = me:deref("key", 0, "Key"), mesh = me, shapes = shapes, ref = ref } end end + return idx, armInfo +end + +local function mesh(ctx: Ctx, ob: Blend.View, node: number) + local addr = ob.block and ob.block.addr + local idx, armInfo = meshOf(ctx, ob, ctx.opts.looseEdges == true or (addr ~= nil and ctx.widgets[addr] ~= nil)) + if not idx then + return + end local n = ctx.scene.nodes[node] n.mesh = idx if ctx.scene.meshes[idx].weights then @@ -164751,6 +165327,14 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number): if lay and ctx.f.version < 280 then extras.layers = lay end + -- Object.visibility_flag (2.8+): OB_HIDE_VIEWPORT 1, OB_HIDE_RENDER 4; restrictflag before: VIEW 1, RENDER 4 + local vis = ob:num("visibility_flag") or ob:num("restrictflag") or 0 + if bit32.band(vis, 1) ~= 0 then + extras.hide_viewport = true + end + if bit32.band(vis, 4) ~= 0 then + extras.hide_render = true + end local mods = {} for _, md in ob:list("modifiers") do table.insert(mods, { name = md:sub("modifier") and (md:sub("modifier") :: Blend.View):str("name") or md:str("name") or "", type = (string.gsub(md.struct.name, "ModifierData$", "")) }) @@ -164771,6 +165355,77 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number): return idx 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 + local ob = ctx.objView[node] + local extras = ob and (ctx.scene.nodes[node].extras :: any).blend + if not ob or not extras then + continue + end + local list = Armature.constraints(ob, target) + if #list > 0 then + extras.constraints = list + end + local info = ob.block and ctx.armatures[ob.block.addr] + if info then + local cols = Armature.collections(info.arm, info.bones, info.pose) + extras.bone_collections = cols.list + for j, bone in info.bones do + local name = bone:str("name") or "" + local pc = info.poseNames[name] + local e = (ctx.scene.nodes[info.joints[j]].extras :: any).blend + for k, v in Armature.bone(bone, pc, info.pose) do + e[k] = v + end + e.collections = cols.byBone[name] or {} + if pc then + Armature.pose(e, pc, target, shape) + end + end + end + end +end + +-- every custom shape object: its node (and mesh) when it is in the scene, else its mesh only +local function widgetList(ctx: Ctx): { { [string]: any } } + local list = {} + for _, addr in ctx.widgetOrder do + local ob = ctx.widgets[addr] + local t = ob:num("type") or -1 + local w: { [string]: any } = { name = ob:idName() or "", type = TYPE_NAME[t] or tostring(t) } + local node = ctx.objects[addr] + if node then + w.node = node + w.mesh = ctx.scene.nodes[node].mesh + elseif t == OB_MESH then + w.mesh = (meshOf(ctx, ob, true)) + end + table.insert(list, w) + end + return list +end + -- ------------------------------------------------------------------------------------------------ animation local function apply(props: Props, curves: { Curve }, frame: number, prefix: string?, name: string?): Props local p: Props = {} @@ -164962,7 +165617,7 @@ local function animations(ctx: Ctx) v = math.clamp(Anim.evaluate(c, fr), math.min(s.min, s.max), math.max(s.min, s.max)) -- the slider range end end - buffer.writef32(out, ((i - 1) * count + k) * 4, v) + buffer.writef32(out, ((i - 1) * count + k) * 4, if s.mute then 0 else v) k += 1 end end @@ -165019,7 +165674,7 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?) } local ctx: Ctx = { f = f, scene = scene, opts = o, objects = {}, objView = {}, parentOf = {}, world = {}, meshes = {}, materials = {}, - textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, + textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, widgets = {}, widgetOrder = {}, } local order: { { ob: Blend.View, under: number? } } = {} if sc then @@ -165098,6 +165753,7 @@ 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 + rigs(ctx) for node = 1, #scene.nodes do local ob = ctx.objView[node] if ob and (ob:num("type") or -1) == OB_MESH then @@ -165105,6 +165761,9 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?) end end skins(ctx) + if next(ctx.armatures) ~= nil then + scene.meta.blend = { widgets = widgetList(ctx), bone_color_sets = Armature.colorSets() } + end if o.animations ~= false then animations(ctx) end @@ -174028,6 +174687,16 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc return nil, Err.new(Err.CORRUPT, "bad mesh " .. (i - 1)) end local mesh: Types.Mesh = { name = str(m.name), primitives = {}, weights = if type(m.weights) == "table" then table.clone(m.weights) else nil, extras = keep(m, nil) } + -- morph target names: the extras.targetNames convention (Blender's exporter, three.js, Babylon) + local tn = type(m.extras) == "table" and m.extras.targetNames + if type(tn) == "table" and #tn > 0 then + local names = {} + for k, v in tn do + names[k] = if type(v) == "string" then v else tostring(v) + end + mesh.extras = mesh.extras or {} + ;(mesh.extras :: any).targetNames = names + end for k, p in arr(m.primitives) do if type(p) ~= "table" or type(p.attributes) ~= "table" then return nil, Err.new(Err.CORRUPT, string.format("mesh %d primitive %d: no attributes", i - 1, k - 1)) @@ -174744,6 +175413,12 @@ function Write.build(scene: Types.Scene, opts: Options?): (Out?, Err.Err?) end g.weights = mesh.weights restore(g, mesh.extras) + local tn = mesh.extras and (mesh.extras :: any).targetNames + if type(tn) == "table" and #tn > 0 and g.extras == nil then + local ex = object() + ex.targetNames = table.clone(tn) + g.extras = ex + end table.insert(meshes, g) end local cameras: { any } = {} @@ -210729,7 +211404,10 @@ local function defaultFor(name: string, k: number): number return 0 end -function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? }?): (any, Err.Err?) +-- opts.targets: morph targets concatenated by name (mesh.extras.targetNames, else prim.extras.targetNames; unnamed +-- ones by index), zero where a primitive lacks one; the result's extras.targetNames / extras.weights name them and +-- give their default weights (node weights, else mesh weights) +function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?, targets: boolean? }?): (any, Err.Err?) local placed: { Types.Placed } = {} if type(input) ~= "table" then return nil, Err.new(Err.ARG, "merge: expected a Scene or a list of Primitives") @@ -210743,6 +211421,9 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end local groups: { [number]: { Primitive } } = {} local order: { number } = {} + local withTargets = opts ~= nil and opts.targets == true + local keysOf: { [Primitive]: { any } } = {} -- per baked primitive: the key (name or index) of each target + local weightOf: { [any]: number } = {} local allPoints = #placed > 0 for _, pl in placed do if pl.primitive.mode ~= "points" then @@ -210762,6 +211443,27 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? for name, a in t.attributes do copy.attributes[name] = MeshOps.f32(a) end + if withTargets and t.targets and #t.targets > 0 then + local mesh = if input.kind == "scene" and pl.mesh > 0 then input.meshes[pl.mesh] else nil + local node = if input.kind == "scene" and pl.node > 0 then input.nodes[pl.node] else nil + local names = mesh and mesh.extras and mesh.extras.targetNames or (p.extras and (p.extras :: any).targetNames) + local weights = node and node.weights or (mesh and mesh.weights) + local keys, targets = {}, {} + for k, tg in t.targets do + local key = if type(names) == "table" and type(names[k]) == "string" then names[k] else k + keys[k] = key + if weightOf[key] == nil then + weightOf[key] = weights and weights[k] or 0 + end + local own = {} + for name, a in tg do + own[name] = MeshOps.f32(a) + end + targets[k] = own + end + copy.targets = targets + keysOf[copy] = keys + end if pl.matrix ~= Scene.IDENTITY then local ok, terr = MeshOps.transform(copy, pl.matrix) if not ok then @@ -210807,6 +211509,56 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end attrs[name] = Scene.accessor(out, "f32", comps, nv) end + local targets: { { [string]: Accessor } }? = nil + local targetKeys: { any } = {} + if withTargets then + local index: { [any]: number } = {} + for _, p in list do + for _, key in keysOf[p] or {} do + if not index[key] then + table.insert(targetKeys, key) + index[key] = #targetKeys + end + end + end + if #targetKeys > 0 then + local out: { { [string]: Accessor } } = {} + for ti, key in targetKeys do + local comps: { [string]: number } = {} + for _, p in list do + for k, pk in keysOf[p] or {} do + if pk == key then + for name, a in (p.targets :: any)[k] do + comps[name] = math.max(comps[name] or 0, a.components) + end + end + end + end + local tgt: { [string]: Accessor } = {} + for name, nc in comps do + local buf = buffer.create(nv * nc * 4) -- zero where a primitive lacks the target + local base = 0 + for _, p in list do + local cnt = vertexCount(p) + for k, pk in keysOf[p] or {} do + local a = if pk == key then (p.targets :: any)[k][name] else nil + if a then + for v = 0, math.min(cnt, a.count) - 1 do + for c = 0, math.min(nc, a.components) - 1 do + writef32(buf, ((base + v) * nc + c) * 4, readf32(a.data, (v * a.components + c) * 4)) + end + end + end + end + base += cnt + end + tgt[name] = Scene.accessor(buf, "f32", nc, nv) + end + out[ti] = tgt + end + targets = out + end + end local idx = buffer.create(ni * 4) local base, at = 0, 0 for _, p in list do @@ -210817,11 +211569,22 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? at += n base += vertexCount(p) end + local extras: { [string]: any }? = nil + if targets then + local names, weights = {}, {} + for k, key in targetKeys do + names[k] = if type(key) == "string" then key else tostring(key) + weights[k] = weightOf[key] or 0 + end + extras = { targetNames = names, weights = weights } + end return { attributes = attrs, indices = if points then nil else Scene.accessor(idx, "u32", 1, ni), mode = if points then "points" else "triangles", material = material, + targets = targets, + extras = extras, } end if opts and opts.byMaterial then diff --git a/build/Rosetta.luau.linemap.json b/build/Rosetta.luau.linemap.json index ff838e0..148176c 100644 --- a/build/Rosetta.luau.linemap.json +++ b/build/Rosetta.luau.linemap.json @@ -434,186 +434,187 @@ [161564, "src/Rosetta/model/b3d/init.luau"], [161619, "src/Rosetta/model/b3d/Read.luau"], [162190, "src/Rosetta/model/blend/init.luau"], -[162286, "src/Rosetta/model/blend/Anim.luau"], -[162632, "src/Rosetta/model/blend/Material.luau"], -[162984, "src/Rosetta/model/blend/Mesh.luau"], -[163619, "src/Rosetta/model/blend/Normals.luau"], -[163780, "src/Rosetta/model/blend/Prim.luau"], -[164229, "src/Rosetta/model/blend/Read.luau"], -[165129, "src/Rosetta/model/bsp/init.luau"], -[165320, "src/Rosetta/model/bsp/Lightmap.luau"], -[165386, "src/Rosetta/model/bsp/Quake.luau"], -[165775, "src/Rosetta/model/bsp/Quake3.luau"], -[166004, "src/Rosetta/model/bsp/Tree.luau"], -[166392, "src/Rosetta/model/buildmap/init.luau"], -[166664, "src/Rosetta/model/buildmap/Geometry.luau"], -[167067, "src/Rosetta/model/d3map/init.luau"], -[167349, "src/Rosetta/model/d3map/Parse.luau"], -[167547, "src/Rosetta/model/dae/init.luau"], -[167736, "src/Rosetta/model/dae/Math.luau"], -[167783, "src/Rosetta/model/dae/Read.luau"], -[169633, "src/Rosetta/model/dae/Write.luau"], -[170002, "src/Rosetta/model/fbx/init.luau"], -[170150, "src/Rosetta/model/fbx/Ascii.luau"], -[170420, "src/Rosetta/model/fbx/Binary.luau"], -[170875, "src/Rosetta/model/fbx/Read.luau"], -[172237, "src/Rosetta/model/fbx/Write.luau"], -[173222, "src/Rosetta/model/fbx/Xform.luau"], -[173263, "src/Rosetta/model/gltf/init.luau"], -[173500, "src/Rosetta/model/gltf/Pack.luau"], -[173569, "src/Rosetta/model/gltf/Read.luau"], -[174300, "src/Rosetta/model/gltf/Write.luau"], -[174980, "src/Rosetta/model/godot/init.luau"], -[175071, "src/Rosetta/model/godot/Build.luau"], -[175613, "src/Rosetta/model/godot/Primitives.luau"], -[175888, "src/Rosetta/model/godot/Surface.luau"], -[176373, "src/Rosetta/model/godot/Write.luau"], -[176702, "src/Rosetta/model/iqm/init.luau"], -[176778, "src/Rosetta/model/iqm/Read.luau"], -[177133, "src/Rosetta/model/iqm/Write.luau"], -[177820, "src/Rosetta/model/ldraw/init.luau"], -[177898, "src/Rosetta/model/ldraw/Colors.luau"], -[178216, "src/Rosetta/model/ldraw/Read.luau"], -[178793, "src/Rosetta/model/lwo/init.luau"], -[178869, "src/Rosetta/model/lwo/Build.luau"], -[179331, "src/Rosetta/model/lwo/Parse.luau"], -[179874, "src/Rosetta/model/map/init.luau"], -[180191, "src/Rosetta/model/map/Build.luau"], -[180435, "src/Rosetta/model/map/Parse.luau"], -[180820, "src/Rosetta/model/map/Write.luau"], -[180966, "src/Rosetta/model/mca/init.luau"], -[181308, "src/Rosetta/model/mca/Chunk.luau"], -[181441, "src/Rosetta/model/mca/Lz4Block.luau"], -[181511, "src/Rosetta/model/md2/init.luau"], -[181710, "src/Rosetta/model/md3/init.luau"], -[181938, "src/Rosetta/model/md5/init.luau"], -[182019, "src/Rosetta/model/md5/Read.luau"], -[182452, "src/Rosetta/model/mdl/init.luau"], -[182534, "src/Rosetta/model/mdl/HalfLife.luau"], -[182838, "src/Rosetta/model/mdl/Quake.luau"], -[183056, "src/Rosetta/model/ms3d/init.luau"], -[183128, "src/Rosetta/model/ms3d/Read.luau"], -[183574, "src/Rosetta/model/ms3d/Write.luau"], -[184043, "src/Rosetta/model/obj/init.luau"], -[184861, "src/Rosetta/model/obj/Mtl.luau"], -[185149, "src/Rosetta/model/off/init.luau"], -[185611, "src/Rosetta/model/ply/init.luau"], -[186716, "src/Rosetta/model/ply/Binary.luau"], -[186917, "src/Rosetta/model/pmx/init.luau"], -[186986, "src/Rosetta/model/pmx/Build.luau"], -[187282, "src/Rosetta/model/pmx/Pmd.luau"], -[187617, "src/Rosetta/model/pmx/Read.luau"], -[188021, "src/Rosetta/model/proc/init.luau"], -[188403, "src/Rosetta/model/qb/init.luau"], -[188796, "src/Rosetta/model/rbxm/init.luau"], -[189389, "src/Rosetta/model/rbxmx/init.luau"], -[190065, "src/Rosetta/model/robloxmesh/init.luau"], -[190724, "src/Rosetta/model/robloxmesh/Parse.luau"], -[191181, "src/Rosetta/model/robloxmesh/Write.luau"], -[191290, "src/Rosetta/model/schematic/init.luau"], -[191611, "src/Rosetta/model/schematic/Common.luau"], -[191731, "src/Rosetta/model/schematic/Litematic.luau"], -[191942, "src/Rosetta/model/schematic/Mcedit.luau"], -[192186, "src/Rosetta/model/schematic/Sponge.luau"], -[192315, "src/Rosetta/model/smd/init.luau"], -[192382, "src/Rosetta/model/smd/Read.luau"], -[192651, "src/Rosetta/model/smd/Write.luau"], -[192894, "src/Rosetta/model/sourcemdl/init.luau"], -[193065, "src/Rosetta/model/sourcemdl/Build.luau"], -[193495, "src/Rosetta/model/sourcemdl/Material.luau"], -[193656, "src/Rosetta/model/sourcemdl/Mdl.luau"], -[194183, "src/Rosetta/model/sourcemdl/Phy.luau"], -[194306, "src/Rosetta/model/sourcemdl/Vtx.luau"], -[194488, "src/Rosetta/model/sourcemdl/Vvd.luau"], -[194582, "src/Rosetta/model/stl/init.luau"], -[195032, "src/Rosetta/model/stl/Decode.luau"], -[195106, "src/Rosetta/model/t3d/init.luau"], -[195200, "src/Rosetta/model/t3d/Build.luau"], -[195605, "src/Rosetta/model/t3d/Parse.luau"], -[195773, "src/Rosetta/model/udmf/init.luau"], -[195856, "src/Rosetta/model/usda/init.luau"], -[196065, "src/Rosetta/model/usda/Build.luau"], -[197343, "src/Rosetta/model/usda/Parse.luau"], -[198141, "src/Rosetta/model/usda/Write.luau"], -[198958, "src/Rosetta/model/vbsp/init.luau"], -[199404, "src/Rosetta/model/vbsp/Faces.luau"], -[199628, "src/Rosetta/model/vbsp/Props.luau"], -[199720, "src/Rosetta/model/vmf/init.luau"], -[200045, "src/Rosetta/model/vmf/Build.luau"], -[200469, "src/Rosetta/model/vox/init.luau"], -[201355, "src/Rosetta/model/vox/Grid.luau"], -[201412, "src/Rosetta/model/vrml/init.luau"], -[201478, "src/Rosetta/model/x/init.luau"], -[201608, "src/Rosetta/model/x/Build.luau"], -[202171, "src/Rosetta/model/x/Parse.luau"], -[202468, "src/Rosetta/model/x3d/init.luau"], -[202546, "src/Rosetta/music/669/init.luau"], -[202753, "src/Rosetta/music/ahx/init.luau"], -[202805, "src/Rosetta/music/far/init.luau"], -[203035, "src/Rosetta/music/hmp/init.luau"], -[203300, "src/Rosetta/music/hvl/init.luau"], -[203352, "src/Rosetta/music/it/init.luau"], -[203771, "src/Rosetta/music/it/Itsex.luau"], -[203888, "src/Rosetta/music/med/init.luau"], -[204536, "src/Rosetta/music/midi/init.luau"], -[204680, "src/Rosetta/music/midi/Smf.luau"], -[205092, "src/Rosetta/music/mod/init.luau"], -[205446, "src/Rosetta/music/mtm/init.luau"], -[205669, "src/Rosetta/music/mus/init.luau"], -[205827, "src/Rosetta/music/okt/init.luau"], -[206109, "src/Rosetta/music/ptm/init.luau"], -[206332, "src/Rosetta/music/s3m/init.luau"], -[206569, "src/Rosetta/music/sf2/init.luau"], -[206613, "src/Rosetta/music/stm/init.luau"], -[206845, "src/Rosetta/music/ult/init.luau"], -[207150, "src/Rosetta/music/umx/init.luau"], -[207367, "src/Rosetta/music/xm/init.luau"], -[207929, "src/Rosetta/music/xmi/init.luau"], -[208138, "src/Rosetta/ops/AudioOps.luau"], -[208617, "src/Rosetta/ops/ImageOps/init.luau"], -[209265, "src/Rosetta/ops/ImageOps/Kernels.luau"], -[209905, "src/Rosetta/ops/LayerOps.luau"], -[209998, "src/Rosetta/ops/MeshOps/init.luau"], -[211050, "src/Rosetta/ops/MeshOps/Greedy.luau"], -[211217, "src/Rosetta/ops/MusicOps.luau"], -[211566, "src/Rosetta/playlist/m3u/init.luau"], -[211919, "src/Rosetta/playlist/pls/init.luau"], -[212097, "src/Rosetta/playlist/xspf/init.luau"], -[212300, "src/Rosetta/subtitle/ass/init.luau"], -[212744, "src/Rosetta/subtitle/srt/init.luau"], -[212917, "src/Rosetta/subtitle/vtt/init.luau"], -[213120, "src/Rosetta/vector/gamemaps/init.luau"], -[213307, "src/Rosetta/vector/gamemaps/Expand.luau"], -[213404, "src/Rosetta/vector/ldtk/init.luau"], -[213496, "src/Rosetta/vector/ldtk/Read.luau"], -[213832, "src/Rosetta/vector/ldtk/Write.luau"], -[214480, "src/Rosetta/vector/ogmo/init.luau"], -[214599, "src/Rosetta/vector/ogmo/Read.luau"], -[215004, "src/Rosetta/vector/ogmo/Write.luau"], -[215358, "src/Rosetta/vector/svg/init.luau"], -[215444, "src/Rosetta/vector/svg/Parse.luau"], -[216190, "src/Rosetta/vector/svg/PathData.luau"], -[216558, "src/Rosetta/vector/svg/Render.luau"], -[216975, "src/Rosetta/vector/svg/Style.luau"], -[217347, "src/Rosetta/vector/svg/Write.luau"], -[217547, "src/Rosetta/vector/tiled/init.luau"], -[217727, "src/Rosetta/vector/tiled/Data.luau"], -[217878, "src/Rosetta/vector/tiled/Tmj.luau"], -[218509, "src/Rosetta/vector/tiled/Tmx.luau"], -[219121, "src/Rosetta/video/avi/init.luau"], -[219415, "src/Rosetta/video/fli/init.luau"], -[219566, "src/Rosetta/video/flv/init.luau"], -[219950, "src/Rosetta/video/flv/Amf.luau"], -[220108, "src/Rosetta/video/flv/Mux.luau"], -[220389, "src/Rosetta/video/h264/init.luau"], -[220528, "src/Rosetta/video/mkv/init.luau"], -[220684, "src/Rosetta/video/mkv/Mux.luau"], -[220963, "src/Rosetta/video/mkv/Parse.luau"], -[221684, "src/Rosetta/video/mp4/init.luau"], -[222012, "src/Rosetta/video/mpegps/init.luau"], -[222199, "src/Rosetta/video/mpegts/init.luau"], -[222445, "src/Rosetta/video/ogv/init.luau"], -[222840, "src/Rosetta/video/smk/init.luau"], -[223149, "src/Rosetta/video/smk/Audio.luau"], -[223269, "src/Rosetta/video/y4m/init.luau"] +[162288, "src/Rosetta/model/blend/Anim.luau"], +[162634, "src/Rosetta/model/blend/Armature.luau"], +[163041, "src/Rosetta/model/blend/Material.luau"], +[163393, "src/Rosetta/model/blend/Mesh.luau"], +[164081, "src/Rosetta/model/blend/Normals.luau"], +[164242, "src/Rosetta/model/blend/Prim.luau"], +[164749, "src/Rosetta/model/blend/Read.luau"], +[165788, "src/Rosetta/model/bsp/init.luau"], +[165979, "src/Rosetta/model/bsp/Lightmap.luau"], +[166045, "src/Rosetta/model/bsp/Quake.luau"], +[166434, "src/Rosetta/model/bsp/Quake3.luau"], +[166663, "src/Rosetta/model/bsp/Tree.luau"], +[167051, "src/Rosetta/model/buildmap/init.luau"], +[167323, "src/Rosetta/model/buildmap/Geometry.luau"], +[167726, "src/Rosetta/model/d3map/init.luau"], +[168008, "src/Rosetta/model/d3map/Parse.luau"], +[168206, "src/Rosetta/model/dae/init.luau"], +[168395, "src/Rosetta/model/dae/Math.luau"], +[168442, "src/Rosetta/model/dae/Read.luau"], +[170292, "src/Rosetta/model/dae/Write.luau"], +[170661, "src/Rosetta/model/fbx/init.luau"], +[170809, "src/Rosetta/model/fbx/Ascii.luau"], +[171079, "src/Rosetta/model/fbx/Binary.luau"], +[171534, "src/Rosetta/model/fbx/Read.luau"], +[172896, "src/Rosetta/model/fbx/Write.luau"], +[173881, "src/Rosetta/model/fbx/Xform.luau"], +[173922, "src/Rosetta/model/gltf/init.luau"], +[174159, "src/Rosetta/model/gltf/Pack.luau"], +[174228, "src/Rosetta/model/gltf/Read.luau"], +[174969, "src/Rosetta/model/gltf/Write.luau"], +[175655, "src/Rosetta/model/godot/init.luau"], +[175746, "src/Rosetta/model/godot/Build.luau"], +[176288, "src/Rosetta/model/godot/Primitives.luau"], +[176563, "src/Rosetta/model/godot/Surface.luau"], +[177048, "src/Rosetta/model/godot/Write.luau"], +[177377, "src/Rosetta/model/iqm/init.luau"], +[177453, "src/Rosetta/model/iqm/Read.luau"], +[177808, "src/Rosetta/model/iqm/Write.luau"], +[178495, "src/Rosetta/model/ldraw/init.luau"], +[178573, "src/Rosetta/model/ldraw/Colors.luau"], +[178891, "src/Rosetta/model/ldraw/Read.luau"], +[179468, "src/Rosetta/model/lwo/init.luau"], +[179544, "src/Rosetta/model/lwo/Build.luau"], +[180006, "src/Rosetta/model/lwo/Parse.luau"], +[180549, "src/Rosetta/model/map/init.luau"], +[180866, "src/Rosetta/model/map/Build.luau"], +[181110, "src/Rosetta/model/map/Parse.luau"], +[181495, "src/Rosetta/model/map/Write.luau"], +[181641, "src/Rosetta/model/mca/init.luau"], +[181983, "src/Rosetta/model/mca/Chunk.luau"], +[182116, "src/Rosetta/model/mca/Lz4Block.luau"], +[182186, "src/Rosetta/model/md2/init.luau"], +[182385, "src/Rosetta/model/md3/init.luau"], +[182613, "src/Rosetta/model/md5/init.luau"], +[182694, "src/Rosetta/model/md5/Read.luau"], +[183127, "src/Rosetta/model/mdl/init.luau"], +[183209, "src/Rosetta/model/mdl/HalfLife.luau"], +[183513, "src/Rosetta/model/mdl/Quake.luau"], +[183731, "src/Rosetta/model/ms3d/init.luau"], +[183803, "src/Rosetta/model/ms3d/Read.luau"], +[184249, "src/Rosetta/model/ms3d/Write.luau"], +[184718, "src/Rosetta/model/obj/init.luau"], +[185536, "src/Rosetta/model/obj/Mtl.luau"], +[185824, "src/Rosetta/model/off/init.luau"], +[186286, "src/Rosetta/model/ply/init.luau"], +[187391, "src/Rosetta/model/ply/Binary.luau"], +[187592, "src/Rosetta/model/pmx/init.luau"], +[187661, "src/Rosetta/model/pmx/Build.luau"], +[187957, "src/Rosetta/model/pmx/Pmd.luau"], +[188292, "src/Rosetta/model/pmx/Read.luau"], +[188696, "src/Rosetta/model/proc/init.luau"], +[189078, "src/Rosetta/model/qb/init.luau"], +[189471, "src/Rosetta/model/rbxm/init.luau"], +[190064, "src/Rosetta/model/rbxmx/init.luau"], +[190740, "src/Rosetta/model/robloxmesh/init.luau"], +[191399, "src/Rosetta/model/robloxmesh/Parse.luau"], +[191856, "src/Rosetta/model/robloxmesh/Write.luau"], +[191965, "src/Rosetta/model/schematic/init.luau"], +[192286, "src/Rosetta/model/schematic/Common.luau"], +[192406, "src/Rosetta/model/schematic/Litematic.luau"], +[192617, "src/Rosetta/model/schematic/Mcedit.luau"], +[192861, "src/Rosetta/model/schematic/Sponge.luau"], +[192990, "src/Rosetta/model/smd/init.luau"], +[193057, "src/Rosetta/model/smd/Read.luau"], +[193326, "src/Rosetta/model/smd/Write.luau"], +[193569, "src/Rosetta/model/sourcemdl/init.luau"], +[193740, "src/Rosetta/model/sourcemdl/Build.luau"], +[194170, "src/Rosetta/model/sourcemdl/Material.luau"], +[194331, "src/Rosetta/model/sourcemdl/Mdl.luau"], +[194858, "src/Rosetta/model/sourcemdl/Phy.luau"], +[194981, "src/Rosetta/model/sourcemdl/Vtx.luau"], +[195163, "src/Rosetta/model/sourcemdl/Vvd.luau"], +[195257, "src/Rosetta/model/stl/init.luau"], +[195707, "src/Rosetta/model/stl/Decode.luau"], +[195781, "src/Rosetta/model/t3d/init.luau"], +[195875, "src/Rosetta/model/t3d/Build.luau"], +[196280, "src/Rosetta/model/t3d/Parse.luau"], +[196448, "src/Rosetta/model/udmf/init.luau"], +[196531, "src/Rosetta/model/usda/init.luau"], +[196740, "src/Rosetta/model/usda/Build.luau"], +[198018, "src/Rosetta/model/usda/Parse.luau"], +[198816, "src/Rosetta/model/usda/Write.luau"], +[199633, "src/Rosetta/model/vbsp/init.luau"], +[200079, "src/Rosetta/model/vbsp/Faces.luau"], +[200303, "src/Rosetta/model/vbsp/Props.luau"], +[200395, "src/Rosetta/model/vmf/init.luau"], +[200720, "src/Rosetta/model/vmf/Build.luau"], +[201144, "src/Rosetta/model/vox/init.luau"], +[202030, "src/Rosetta/model/vox/Grid.luau"], +[202087, "src/Rosetta/model/vrml/init.luau"], +[202153, "src/Rosetta/model/x/init.luau"], +[202283, "src/Rosetta/model/x/Build.luau"], +[202846, "src/Rosetta/model/x/Parse.luau"], +[203143, "src/Rosetta/model/x3d/init.luau"], +[203221, "src/Rosetta/music/669/init.luau"], +[203428, "src/Rosetta/music/ahx/init.luau"], +[203480, "src/Rosetta/music/far/init.luau"], +[203710, "src/Rosetta/music/hmp/init.luau"], +[203975, "src/Rosetta/music/hvl/init.luau"], +[204027, "src/Rosetta/music/it/init.luau"], +[204446, "src/Rosetta/music/it/Itsex.luau"], +[204563, "src/Rosetta/music/med/init.luau"], +[205211, "src/Rosetta/music/midi/init.luau"], +[205355, "src/Rosetta/music/midi/Smf.luau"], +[205767, "src/Rosetta/music/mod/init.luau"], +[206121, "src/Rosetta/music/mtm/init.luau"], +[206344, "src/Rosetta/music/mus/init.luau"], +[206502, "src/Rosetta/music/okt/init.luau"], +[206784, "src/Rosetta/music/ptm/init.luau"], +[207007, "src/Rosetta/music/s3m/init.luau"], +[207244, "src/Rosetta/music/sf2/init.luau"], +[207288, "src/Rosetta/music/stm/init.luau"], +[207520, "src/Rosetta/music/ult/init.luau"], +[207825, "src/Rosetta/music/umx/init.luau"], +[208042, "src/Rosetta/music/xm/init.luau"], +[208604, "src/Rosetta/music/xmi/init.luau"], +[208813, "src/Rosetta/ops/AudioOps.luau"], +[209292, "src/Rosetta/ops/ImageOps/init.luau"], +[209940, "src/Rosetta/ops/ImageOps/Kernels.luau"], +[210580, "src/Rosetta/ops/LayerOps.luau"], +[210673, "src/Rosetta/ops/MeshOps/init.luau"], +[211813, "src/Rosetta/ops/MeshOps/Greedy.luau"], +[211980, "src/Rosetta/ops/MusicOps.luau"], +[212329, "src/Rosetta/playlist/m3u/init.luau"], +[212682, "src/Rosetta/playlist/pls/init.luau"], +[212860, "src/Rosetta/playlist/xspf/init.luau"], +[213063, "src/Rosetta/subtitle/ass/init.luau"], +[213507, "src/Rosetta/subtitle/srt/init.luau"], +[213680, "src/Rosetta/subtitle/vtt/init.luau"], +[213883, "src/Rosetta/vector/gamemaps/init.luau"], +[214070, "src/Rosetta/vector/gamemaps/Expand.luau"], +[214167, "src/Rosetta/vector/ldtk/init.luau"], +[214259, "src/Rosetta/vector/ldtk/Read.luau"], +[214595, "src/Rosetta/vector/ldtk/Write.luau"], +[215243, "src/Rosetta/vector/ogmo/init.luau"], +[215362, "src/Rosetta/vector/ogmo/Read.luau"], +[215767, "src/Rosetta/vector/ogmo/Write.luau"], +[216121, "src/Rosetta/vector/svg/init.luau"], +[216207, "src/Rosetta/vector/svg/Parse.luau"], +[216953, "src/Rosetta/vector/svg/PathData.luau"], +[217321, "src/Rosetta/vector/svg/Render.luau"], +[217738, "src/Rosetta/vector/svg/Style.luau"], +[218110, "src/Rosetta/vector/svg/Write.luau"], +[218310, "src/Rosetta/vector/tiled/init.luau"], +[218490, "src/Rosetta/vector/tiled/Data.luau"], +[218641, "src/Rosetta/vector/tiled/Tmj.luau"], +[219272, "src/Rosetta/vector/tiled/Tmx.luau"], +[219884, "src/Rosetta/video/avi/init.luau"], +[220178, "src/Rosetta/video/fli/init.luau"], +[220329, "src/Rosetta/video/flv/init.luau"], +[220713, "src/Rosetta/video/flv/Amf.luau"], +[220871, "src/Rosetta/video/flv/Mux.luau"], +[221152, "src/Rosetta/video/h264/init.luau"], +[221291, "src/Rosetta/video/mkv/init.luau"], +[221447, "src/Rosetta/video/mkv/Mux.luau"], +[221726, "src/Rosetta/video/mkv/Parse.luau"], +[222447, "src/Rosetta/video/mp4/init.luau"], +[222775, "src/Rosetta/video/mpegps/init.luau"], +[222962, "src/Rosetta/video/mpegts/init.luau"], +[223208, "src/Rosetta/video/ogv/init.luau"], +[223603, "src/Rosetta/video/smk/init.luau"], +[223912, "src/Rosetta/video/smk/Audio.luau"], +[224032, "src/Rosetta/video/y4m/init.luau"] ] diff --git a/build/Rosetta.rbxm b/build/Rosetta.rbxm index 049235a..1e0b22c 100644 Binary files a/build/Rosetta.rbxm and b/build/Rosetta.rbxm differ diff --git a/build/Rosetta.rbxm.linemap.json b/build/Rosetta.rbxm.linemap.json index 6fa66e8..4ecb438 100644 --- a/build/Rosetta.rbxm.linemap.json +++ b/build/Rosetta.rbxm.linemap.json @@ -48,15 +48,15 @@ "image_1": [[8, "src/Rosetta/image/ani/init.luau"], [188, "src/Rosetta/image/art/init.luau"], [676, "src/Rosetta/image/blp/init.luau"], [934, "src/Rosetta/image/bmp/init.luau"], [1349, "src/Rosetta/image/dds/init.luau"], [1832, "src/Rosetta/image/exr/init.luau"], [2592, "src/Rosetta/image/exr/Codec.luau"], [2703, "src/Rosetta/image/exr/Piz.luau"], [3060, "src/Rosetta/image/gif/init.luau"], [3576, "src/Rosetta/image/gif/Decode.luau"], [3652, "src/Rosetta/image/hdr/init.luau"], [3881, "src/Rosetta/image/hdr/Codec.luau"], [4092, "src/Rosetta/image/icns/init.luau"], [4423, "src/Rosetta/image/ico/init.luau"], [4700, "src/Rosetta/image/ico/Dib.luau"], [4795, "src/Rosetta/image/ilbm/init.luau"], [5114, "src/Rosetta/image/jpeg/init.luau"], [5250, "src/Rosetta/image/ktx/init.luau"], [5910, "src/Rosetta/image/lmp/init.luau"], [6043, "src/Rosetta/image/pcx/init.luau"], [6307, "src/Rosetta/image/pnm/init.luau"], [6685, "src/Rosetta/image/pnm/Codec.luau"], [6962, "src/Rosetta/image/qoi/init.luau"], [7084, "src/Rosetta/image/qoi/Codec.luau"], [7241, "src/Rosetta/image/sgi/init.luau"], [7526, "src/Rosetta/image/spr/init.luau"]], "image_2": [[8, "src/Rosetta/image/sunras/init.luau"], [254, "src/Rosetta/image/tga/init.luau"], [524, "src/Rosetta/image/tga/Codec.luau"], [724, "src/Rosetta/image/tiff/init.luau"], [1551, "src/Rosetta/image/tiff/Pixels.luau"], [1941, "src/Rosetta/image/tim/init.luau"], [2183, "src/Rosetta/image/vtf/init.luau"], [2418, "src/Rosetta/image/webp/init.luau"], [3028, "src/Rosetta/image/webp/Alpha.luau"], [3104, "src/Rosetta/image/webp/Anim.luau"], [3259, "src/Rosetta/image/xbm/init.luau"], [3420, "src/Rosetta/image/xpm/init.luau"], [3688, "src/Rosetta/image/xpm/Names.luau"]], "layered": [[8, "src/Rosetta/layered/aseprite/init.luau"], [746, "src/Rosetta/layered/ora/init.luau"], [1033, "src/Rosetta/layered/psd/init.luau"], [1768, "src/Rosetta/layered/psd/Channels.luau"]], -"model_1": [[8, "src/Rosetta/model/3ds/init.luau"], [91, "src/Rosetta/model/3ds/Build.luau"], [738, "src/Rosetta/model/3ds/Read.luau"], [1234, "src/Rosetta/model/3ds/Write.luau"], [2039, "src/Rosetta/model/3mf/init.luau"], [2121, "src/Rosetta/model/3mf/Read.luau"], [2709, "src/Rosetta/model/3mf/Write.luau"], [3114, "src/Rosetta/model/ase/init.luau"], [3237, "src/Rosetta/model/ase/Build.luau"], [3811, "src/Rosetta/model/ase/Parse.luau"], [3880, "src/Rosetta/model/ase/Write.luau"], [4343, "src/Rosetta/model/b3d/init.luau"], [4398, "src/Rosetta/model/b3d/Read.luau"], [4969, "src/Rosetta/model/blend/init.luau"], [5065, "src/Rosetta/model/blend/Anim.luau"], [5411, "src/Rosetta/model/blend/Material.luau"], [5763, "src/Rosetta/model/blend/Mesh.luau"], [6398, "src/Rosetta/model/blend/Normals.luau"], [6559, "src/Rosetta/model/blend/Prim.luau"]], -"model_2": [[8, "src/Rosetta/model/blend/Read.luau"], [908, "src/Rosetta/model/bsp/init.luau"], [1099, "src/Rosetta/model/bsp/Lightmap.luau"], [1165, "src/Rosetta/model/bsp/Quake.luau"], [1554, "src/Rosetta/model/bsp/Quake3.luau"], [1783, "src/Rosetta/model/bsp/Tree.luau"], [2171, "src/Rosetta/model/buildmap/init.luau"], [2443, "src/Rosetta/model/buildmap/Geometry.luau"], [2846, "src/Rosetta/model/d3map/init.luau"], [3128, "src/Rosetta/model/d3map/Parse.luau"], [3326, "src/Rosetta/model/dae/init.luau"], [3515, "src/Rosetta/model/dae/Math.luau"], [3562, "src/Rosetta/model/dae/Read.luau"], [5412, "src/Rosetta/model/dae/Write.luau"], [5781, "src/Rosetta/model/fbx/init.luau"], [5929, "src/Rosetta/model/fbx/Ascii.luau"], [6199, "src/Rosetta/model/fbx/Binary.luau"]], -"model_3": [[8, "src/Rosetta/model/fbx/Read.luau"], [1370, "src/Rosetta/model/fbx/Write.luau"], [2355, "src/Rosetta/model/fbx/Xform.luau"], [2396, "src/Rosetta/model/gltf/init.luau"], [2633, "src/Rosetta/model/gltf/Pack.luau"], [2702, "src/Rosetta/model/gltf/Read.luau"], [3433, "src/Rosetta/model/gltf/Write.luau"], [4113, "src/Rosetta/model/godot/init.luau"], [4204, "src/Rosetta/model/godot/Build.luau"], [4746, "src/Rosetta/model/godot/Primitives.luau"], [5021, "src/Rosetta/model/godot/Surface.luau"], [5506, "src/Rosetta/model/godot/Write.luau"], [5835, "src/Rosetta/model/iqm/init.luau"], [5911, "src/Rosetta/model/iqm/Read.luau"], [6266, "src/Rosetta/model/iqm/Write.luau"], [6953, "src/Rosetta/model/ldraw/init.luau"], [7031, "src/Rosetta/model/ldraw/Colors.luau"]], +"model_1": [[8, "src/Rosetta/model/3ds/init.luau"], [91, "src/Rosetta/model/3ds/Build.luau"], [738, "src/Rosetta/model/3ds/Read.luau"], [1234, "src/Rosetta/model/3ds/Write.luau"], [2039, "src/Rosetta/model/3mf/init.luau"], [2121, "src/Rosetta/model/3mf/Read.luau"], [2709, "src/Rosetta/model/3mf/Write.luau"], [3114, "src/Rosetta/model/ase/init.luau"], [3237, "src/Rosetta/model/ase/Build.luau"], [3811, "src/Rosetta/model/ase/Parse.luau"], [3880, "src/Rosetta/model/ase/Write.luau"], [4343, "src/Rosetta/model/b3d/init.luau"], [4398, "src/Rosetta/model/b3d/Read.luau"], [4969, "src/Rosetta/model/blend/init.luau"], [5067, "src/Rosetta/model/blend/Anim.luau"], [5413, "src/Rosetta/model/blend/Armature.luau"], [5820, "src/Rosetta/model/blend/Material.luau"], [6172, "src/Rosetta/model/blend/Mesh.luau"], [6860, "src/Rosetta/model/blend/Normals.luau"], [7021, "src/Rosetta/model/blend/Prim.luau"]], +"model_2": [[8, "src/Rosetta/model/blend/Read.luau"], [1047, "src/Rosetta/model/bsp/init.luau"], [1238, "src/Rosetta/model/bsp/Lightmap.luau"], [1304, "src/Rosetta/model/bsp/Quake.luau"], [1693, "src/Rosetta/model/bsp/Quake3.luau"], [1922, "src/Rosetta/model/bsp/Tree.luau"], [2310, "src/Rosetta/model/buildmap/init.luau"], [2582, "src/Rosetta/model/buildmap/Geometry.luau"], [2985, "src/Rosetta/model/d3map/init.luau"], [3267, "src/Rosetta/model/d3map/Parse.luau"], [3465, "src/Rosetta/model/dae/init.luau"], [3654, "src/Rosetta/model/dae/Math.luau"], [3701, "src/Rosetta/model/dae/Read.luau"], [5551, "src/Rosetta/model/dae/Write.luau"], [5920, "src/Rosetta/model/fbx/init.luau"], [6068, "src/Rosetta/model/fbx/Ascii.luau"], [6338, "src/Rosetta/model/fbx/Binary.luau"]], +"model_3": [[8, "src/Rosetta/model/fbx/Read.luau"], [1370, "src/Rosetta/model/fbx/Write.luau"], [2355, "src/Rosetta/model/fbx/Xform.luau"], [2396, "src/Rosetta/model/gltf/init.luau"], [2633, "src/Rosetta/model/gltf/Pack.luau"], [2702, "src/Rosetta/model/gltf/Read.luau"], [3443, "src/Rosetta/model/gltf/Write.luau"], [4129, "src/Rosetta/model/godot/init.luau"], [4220, "src/Rosetta/model/godot/Build.luau"], [4762, "src/Rosetta/model/godot/Primitives.luau"], [5037, "src/Rosetta/model/godot/Surface.luau"], [5522, "src/Rosetta/model/godot/Write.luau"], [5851, "src/Rosetta/model/iqm/init.luau"], [5927, "src/Rosetta/model/iqm/Read.luau"], [6282, "src/Rosetta/model/iqm/Write.luau"], [6969, "src/Rosetta/model/ldraw/init.luau"], [7047, "src/Rosetta/model/ldraw/Colors.luau"]], "model_4": [[8, "src/Rosetta/model/ldraw/Read.luau"], [585, "src/Rosetta/model/lwo/init.luau"], [661, "src/Rosetta/model/lwo/Build.luau"], [1123, "src/Rosetta/model/lwo/Parse.luau"], [1666, "src/Rosetta/model/map/init.luau"], [1983, "src/Rosetta/model/map/Build.luau"], [2227, "src/Rosetta/model/map/Parse.luau"], [2612, "src/Rosetta/model/map/Write.luau"], [2758, "src/Rosetta/model/mca/init.luau"], [3100, "src/Rosetta/model/mca/Chunk.luau"], [3233, "src/Rosetta/model/mca/Lz4Block.luau"], [3303, "src/Rosetta/model/md2/init.luau"], [3502, "src/Rosetta/model/md3/init.luau"], [3730, "src/Rosetta/model/md5/init.luau"], [3811, "src/Rosetta/model/md5/Read.luau"], [4244, "src/Rosetta/model/mdl/init.luau"], [4326, "src/Rosetta/model/mdl/HalfLife.luau"], [4630, "src/Rosetta/model/mdl/Quake.luau"], [4848, "src/Rosetta/model/ms3d/init.luau"], [4920, "src/Rosetta/model/ms3d/Read.luau"], [5366, "src/Rosetta/model/ms3d/Write.luau"], [5835, "src/Rosetta/model/obj/init.luau"], [6653, "src/Rosetta/model/obj/Mtl.luau"], [6941, "src/Rosetta/model/off/init.luau"]], "model_5": [[8, "src/Rosetta/model/ply/init.luau"], [1113, "src/Rosetta/model/ply/Binary.luau"], [1314, "src/Rosetta/model/pmx/init.luau"], [1383, "src/Rosetta/model/pmx/Build.luau"], [1679, "src/Rosetta/model/pmx/Pmd.luau"], [2014, "src/Rosetta/model/pmx/Read.luau"], [2418, "src/Rosetta/model/proc/init.luau"], [2800, "src/Rosetta/model/qb/init.luau"], [3193, "src/Rosetta/model/rbxm/init.luau"], [3786, "src/Rosetta/model/rbxmx/init.luau"], [4462, "src/Rosetta/model/robloxmesh/init.luau"], [5121, "src/Rosetta/model/robloxmesh/Parse.luau"], [5578, "src/Rosetta/model/robloxmesh/Write.luau"], [5687, "src/Rosetta/model/schematic/init.luau"], [6008, "src/Rosetta/model/schematic/Common.luau"], [6128, "src/Rosetta/model/schematic/Litematic.luau"], [6339, "src/Rosetta/model/schematic/Mcedit.luau"], [6583, "src/Rosetta/model/schematic/Sponge.luau"], [6712, "src/Rosetta/model/smd/init.luau"], [6779, "src/Rosetta/model/smd/Read.luau"], [7048, "src/Rosetta/model/smd/Write.luau"], [7291, "src/Rosetta/model/sourcemdl/init.luau"], [7462, "src/Rosetta/model/sourcemdl/Build.luau"], [7892, "src/Rosetta/model/sourcemdl/Material.luau"]], "model_6": [[8, "src/Rosetta/model/sourcemdl/Mdl.luau"], [535, "src/Rosetta/model/sourcemdl/Phy.luau"], [658, "src/Rosetta/model/sourcemdl/Vtx.luau"], [840, "src/Rosetta/model/sourcemdl/Vvd.luau"], [934, "src/Rosetta/model/stl/init.luau"], [1384, "src/Rosetta/model/stl/Decode.luau"], [1458, "src/Rosetta/model/t3d/init.luau"], [1552, "src/Rosetta/model/t3d/Build.luau"], [1957, "src/Rosetta/model/t3d/Parse.luau"], [2125, "src/Rosetta/model/udmf/init.luau"], [2208, "src/Rosetta/model/usda/init.luau"], [2417, "src/Rosetta/model/usda/Build.luau"], [3695, "src/Rosetta/model/usda/Parse.luau"], [4493, "src/Rosetta/model/usda/Write.luau"], [5310, "src/Rosetta/model/vbsp/init.luau"], [5756, "src/Rosetta/model/vbsp/Faces.luau"], [5980, "src/Rosetta/model/vbsp/Props.luau"], [6072, "src/Rosetta/model/vmf/init.luau"], [6397, "src/Rosetta/model/vmf/Build.luau"], [6821, "src/Rosetta/model/vox/init.luau"], [7707, "src/Rosetta/model/vox/Grid.luau"], [7764, "src/Rosetta/model/vrml/init.luau"], [7830, "src/Rosetta/model/x/init.luau"]], "model_7": [[8, "src/Rosetta/model/x/Build.luau"], [571, "src/Rosetta/model/x/Parse.luau"], [868, "src/Rosetta/model/x3d/init.luau"]], "music": [[8, "src/Rosetta/music/669/init.luau"], [215, "src/Rosetta/music/ahx/init.luau"], [267, "src/Rosetta/music/far/init.luau"], [497, "src/Rosetta/music/hmp/init.luau"], [762, "src/Rosetta/music/hvl/init.luau"], [814, "src/Rosetta/music/it/init.luau"], [1233, "src/Rosetta/music/it/Itsex.luau"], [1350, "src/Rosetta/music/med/init.luau"], [1998, "src/Rosetta/music/midi/init.luau"], [2142, "src/Rosetta/music/midi/Smf.luau"], [2554, "src/Rosetta/music/mod/init.luau"], [2908, "src/Rosetta/music/mtm/init.luau"], [3131, "src/Rosetta/music/mus/init.luau"], [3289, "src/Rosetta/music/okt/init.luau"], [3571, "src/Rosetta/music/ptm/init.luau"], [3794, "src/Rosetta/music/s3m/init.luau"], [4031, "src/Rosetta/music/sf2/init.luau"], [4075, "src/Rosetta/music/stm/init.luau"], [4307, "src/Rosetta/music/ult/init.luau"], [4612, "src/Rosetta/music/umx/init.luau"], [4829, "src/Rosetta/music/xm/init.luau"], [5391, "src/Rosetta/music/xmi/init.luau"]], -"ops": [[8, "src/Rosetta/ops/AudioOps.luau"], [487, "src/Rosetta/ops/LayerOps.luau"], [580, "src/Rosetta/ops/MeshOps/init.luau"], [1632, "src/Rosetta/ops/MeshOps/Greedy.luau"], [1799, "src/Rosetta/ops/MusicOps.luau"]], +"ops": [[8, "src/Rosetta/ops/AudioOps.luau"], [487, "src/Rosetta/ops/LayerOps.luau"], [580, "src/Rosetta/ops/MeshOps/init.luau"], [1720, "src/Rosetta/ops/MeshOps/Greedy.luau"], [1887, "src/Rosetta/ops/MusicOps.luau"]], "playlist": [[8, "src/Rosetta/playlist/m3u/init.luau"], [361, "src/Rosetta/playlist/pls/init.luau"], [539, "src/Rosetta/playlist/xspf/init.luau"]], "subtitle": [[8, "src/Rosetta/subtitle/ass/init.luau"], [452, "src/Rosetta/subtitle/srt/init.luau"], [625, "src/Rosetta/subtitle/vtt/init.luau"]], "vector": [[8, "src/Rosetta/vector/gamemaps/init.luau"], [195, "src/Rosetta/vector/gamemaps/Expand.luau"], [292, "src/Rosetta/vector/ldtk/init.luau"], [384, "src/Rosetta/vector/ldtk/Read.luau"], [720, "src/Rosetta/vector/ldtk/Write.luau"], [1368, "src/Rosetta/vector/ogmo/init.luau"], [1487, "src/Rosetta/vector/ogmo/Read.luau"], [1892, "src/Rosetta/vector/ogmo/Write.luau"], [2246, "src/Rosetta/vector/svg/init.luau"], [2332, "src/Rosetta/vector/svg/Parse.luau"], [3078, "src/Rosetta/vector/svg/PathData.luau"], [3446, "src/Rosetta/vector/svg/Render.luau"], [3863, "src/Rosetta/vector/svg/Style.luau"], [4235, "src/Rosetta/vector/svg/Write.luau"], [4435, "src/Rosetta/vector/tiled/init.luau"], [4615, "src/Rosetta/vector/tiled/Data.luau"], [4766, "src/Rosetta/vector/tiled/Tmj.luau"], [5397, "src/Rosetta/vector/tiled/Tmx.luau"]], diff --git a/build/Rosetta.rbxmx b/build/Rosetta.rbxmx index 371eb4e..a484cf5 100644 --- a/build/Rosetta.rbxmx +++ b/build/Rosetta.rbxmx @@ -447,6 +447,7 @@ local __CHUNK = { ["model/b3d/Read"] = "model_1", ["model/blend"] = "model_1", ["model/blend/Anim"] = "model_1", + ["model/blend/Armature"] = "model_1", ["model/blend/Material"] = "model_1", ["model/blend/Mesh"] = "model_1", ["model/blend/Normals"] = "model_1", @@ -159806,7 +159807,7 @@ end --!optimize 2 --!nocheck -- rosetta chunk model_1 (build/Rosetta.rbxm): GENERATED by tools/bundle.py from src/Rosetta - do not edit. --- 19 modules (0 native: @native); the Rosetta root requires it when one of them is first included. +-- 20 modules (0 native: @native); the Rosetta root requires it when one of them is first included. -- Error lines map back to the sources through Rosetta.rbxm.linemap.json (tools/bundle.py --where). return function(__FILES, __include) __FILES["model/3ds"] = function() @@ -164777,9 +164778,10 @@ __FILES["model/blend"] = function() -- file's own SDNA). Supports: the scene's objects and collections, transforms with parent inverse and bone parents, -- meshes of every storage generation (MFace, MPoly / MLoop, attribute layers, AttributeStorage) with UV maps, colour -- attributes, custom normals, material slots; Principled BSDF materials; packed and external images; cameras; lights; --- armatures, skins (vertex groups, top 4 weights), shape keys; actions sampled into channels. +-- armatures, skins (vertex groups, top 4 weights), shape keys (relative keys, vertex groups); actions sampled into +-- channels; rig data (bone collections / layers, colours, custom shapes, pose settings, constraint stacks) as extras. -- Not supported: modifiers (listed in meta.source.modifiers), curves / text / metaballs / grease pencil / volumes, --- NLA strips, drivers, constraints, f-curve modifiers, linked libraries, collection instances. +-- NLA strips, drivers, constraint evaluation, f-curve modifiers, linked libraries, collection instances. -- ------------------------------------------------------------------------------------------------ requires local Err = __include("core/Err") @@ -164805,6 +164807,7 @@ local BlendFormat = { type DecodeOptions = { triangulate: boolean?, -- default true (false: "polygons" primitives) flipV: boolean?, -- default true (glTF's top-left UV origin) + looseEdges: boolean?, -- edges outside faces as "lines" primitives (default false; always for custom shape widgets) collections: boolean?, -- collections as nodes (default true) scene: string?, -- a scene by name (default: the file's current scene) sampleStep: number?, -- frames between animation samples (default 1) @@ -165212,6 +165215,413 @@ end return Anim end +__FILES["model/blend/Armature"] = function() + +-- 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; the +-- common fields for the rest). Flags from DNA_armature_types.h, DNA_action_types.h, DNA_constraint_types.h. +local Blend = __include("containers/Blend") + +local Armature = {} + +type Target = number | string -- a scene node index, or the object's name when it is not in the scene +type Resolve = (ob: Blend.View?) -> Target? +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 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 +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"), + } + 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 + 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.power = d:num("power") or 1 + 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 + e.constraints = Armature.constraints(pc, resolve) +end + +return Armature +end __FILES["model/blend/Material"] = function() -- model/blend/Material: Blender materials to Scene PBR materials. With nodes: the Principled BSDF (the one feeding the @@ -165803,6 +166213,75 @@ end type Limits = { maxVertices: number? } +-- edge count and per-edge readers (vertex 1, vertex 2, sharp): ".edge_verts" / "sharp_edge" attributes or MEdge +local function edgeReaders(f: Blend.File, me: Blend.View, layers: { Layer }): (number, ((number) -> number)?, ((number) -> number)?, ((number) -> boolean)?) + local ne = count(me, "totedge", "edges_num") + local ev = find(layers, "edge", "int2", ".edge_verts") + local se = find(layers, "edge", "bool", "sharp_edge") + local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") + if ne == 0 then + return 0, nil, nil, nil + end + if ev then + local er = reader(f, ev) + local sr = if se then reader(f, se) else nil + return ne, function(i: number): number + return er(i, 0) + end, function(i: number): number + return er(i, 1) + end, function(i: number): boolean + return sr ~= nil and sr(i, 0) ~= 0 + end + elseif medge then + local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") + if a and b then + local ser = if se then reader(f, se) else nil + return ne, function(i: number): number + return a(i, 0) + end, function(i: number): number + return b(i, 0) + end, function(i: number): boolean + if ser then + return ser(i, 0) ~= 0 + end + return fl ~= nil and bit32.band(fl(i, 0), ME_SHARP) ~= 0 + end + end + end + return ne, nil, nil, nil +end + +-- edges no face uses, as u32 vertex pairs (rig widgets are usually edges only) +function Mesh.looseEdges(f: Blend.File, me: Blend.View, poly: Poly): buffer + local ne, v1, v2 = edgeReaders(f, me, poly.layers) + if not v1 or not v2 then + return buffer.create(0) + end + local nv = poly.nv + local used: { [number]: boolean } = {} + local cv, fs = poly.cornerVert, poly.faceStart + for fi = 0, poly.nf - 1 do + local s0, s1 = buffer.readu32(fs, fi * 4), buffer.readu32(fs, fi * 4 + 4) + for c = s0, s1 - 1 do + local a, b = buffer.readu32(cv, c * 4), buffer.readu32(cv, (if c + 1 < s1 then c + 1 else s0) * 4) + used[math.min(a, b) * nv + math.max(a, b)] = true + end + end + local out = {} + for i = 0, ne - 1 do + local a, b = v1(i), v2(i) + if a ~= b and a >= 0 and b >= 0 and a < nv and b < nv and not used[math.min(a, b) * nv + math.max(a, b)] then + table.insert(out, a) + table.insert(out, b) + end + end + local buf = buffer.create(#out * 4) + for k, v in out do + buffer.writeu32(buf, (k - 1) * 4, v) + end + return buf +end + function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, string?) local layers = Mesh.layers(f, me) local nv = count(me, "totvert", "verts_num") @@ -166082,10 +166561,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri poly.customNormals = d end -- sharp edges - local ne = count(me, "totedge", "edges_num") - local ev = find(layers, "edge", "int2", ".edge_verts") - local se = find(layers, "edge", "bool", "sharp_edge") - local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") local flags = me:num("flag") or 0 local smoothresh = me:num("smoothresh") local autoSmooth = f.version < 401 and bit32.band(flags, ME_AUTOSMOOTH) ~= 0 @@ -166093,27 +166568,8 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri poly.autoSmooth = smoothresh or math.rad(30) end local useSharp = f.version >= 401 or autoSmooth - if useSharp and ne > 0 then - local v1, v2, sharp - if ev and se then - local er, sr = reader(f, ev), reader(f, se) - v1 = function(i: number): number return er(i, 0) end - v2 = function(i: number): number return er(i, 1) end - sharp = function(i: number): boolean return sr(i, 0) ~= 0 end - elseif medge then - local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") - if a and b and fl then - v1 = function(i: number): number return a(i, 0) end - v2 = function(i: number): number return b(i, 0) end - local ser = if se then reader(f, se) else nil - sharp = function(i: number): boolean - if ser then - return ser(i, 0) ~= 0 - end - return bit32.band(fl(i, 0), ME_SHARP) ~= 0 - end - end - end + if useSharp then + local ne, v1, v2, sharp = edgeReaders(f, me, layers) if v1 and v2 and sharp then for i = 0, ne - 1 do if sharp(i) then @@ -166158,7 +166614,7 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri end -- shape keys: { name, value, relative (0-based), data: f32 x 3 per vertex } in file order, and the reference index -type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number } +type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number, vgroup: string? } function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, number?, Blend.View?) local key = me:deref("key", 0, "Key") if not key then @@ -166168,6 +166624,7 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num local refAddr = key:ptr("refkey") local out: { Shape } = {} local ref: number? = nil + local index = 0 for i, kb in blocks do local blk = kb:target("data") local d = buffer.create(nv * 12) @@ -166180,15 +166637,21 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num end end end - if kb.block and kb.block.addr == refAddr then + local isRef = kb.block ~= nil and kb.block.addr == refAddr + if isRef then ref = i + else + index += 1 end + local stored = kb:str("name") + -- unnamed in old files: named as Blender's 2.45 versioning does (Basis, Key 1, Key 2, ...) + local name: string = if stored and stored ~= "" then stored elseif isRef then "Basis" else "Key " .. index table.insert(out, { - -- unnamed in old files: Key 0 (the basis), Key 1, ... - name = kb:str("name") or ("Key " .. (i - 1)), value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, - mute = bit32.band(kb:num("flag") or 0, 2) ~= 0, + name = name, value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, + mute = bit32.band(kb:num("flag") or 0, 1) ~= 0, -- KEYBLOCK_MUTE min = kb:num("slidermin") or 0, max = kb:num("slidermax") or 1, + vgroup = if kb:str("vgroup") ~= "" then kb:str("vgroup") else nil, }) end if ref == nil and #out > 0 then @@ -166621,8 +167084,80 @@ end type Built = { mesh: Types.Mesh, slots: { number }, vertexSource: { { number } } } --- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints -function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: Skinning?, shapes: { MeshT.Shape }?, ref: number?): Built +-- morph targets of the output vertices (vsrc: their mesh vertices): each key minus its relative key, times the weight +-- of the key's vertex group, as Blender evaluates relative keys +local function targetDeltas(poly: MeshT.Poly, vsrc: { number }, shapes: { MeshT.Shape }, ref: number, groups: { string }?): { { [string]: Types.Accessor } } + local n = #vsrc + local targets: { { [string]: Types.Accessor } } = {} + for k, s in shapes do + if k ~= ref then + local rel = shapes[s.relative + 1] or shapes[ref] + local base = rel.data + local def: number? = nil + if s.vgroup and groups then + for gi, g in groups do + if g == s.vgroup then + def = gi - 1 + end + end + end + local t = table.create(n * 3) :: { number } + for i = 1, n do + local v = vsrc[i] + local w: number = 1 + if def then + w = 0 + local list = poly.weights and poly.weights[v + 1] or {} + for q = 1, #list - 1, 2 do + if list[q] == def then + w = list[q + 1] + end + end + end + for c = 0, 2 do + t[(i - 1) * 3 + c + 1] = (readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4)) * w + end + end + table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor }) + end + end + return targets +end + +-- a "lines" primitive of the given edges (u32 vertex pairs), with the morph targets the face primitives have +function Prim.lines(poly: MeshT.Poly, edges: buffer, shapes: { MeshT.Shape }?, ref: number?, groups: { string }?): Types.Primitive? + local ne = buffer.len(edges) // 8 + if ne == 0 then + return nil + end + local out: { [number]: number } = {} + local vsrc: { number } = {} + local I = buffer.create(ne * 8) + for k = 0, ne * 2 - 1 do + local v = readu32(edges, k * 4) + local o = out[v] + if not o then + o = #vsrc + out[v] = o + table.insert(vsrc, v) + end + writeu32(I, k * 4, o) + end + local n = #vsrc + local P = buffer.create(n * 12) + for i = 1, n do + buffer.copy(P, (i - 1) * 12, poly.pos, vsrc[i] * 12, 12) + end + local prim: Types.Primitive = { attributes = { POSITION = Scene.accessor(P, "f32", 3, n) }, indices = Scene.accessor(I, "u32", 1, ne * 2), mode = "lines" } + if shapes and ref and #shapes > 1 then + prim.targets = targetDeltas(poly, vsrc, shapes, ref, groups) + end + return prim +end + +-- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints; vgroups names the +-- vertex groups (def_nr + 1) for shape keys limited to one +function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: Skinning?, shapes: { MeshT.Shape }?, ref: number?, vgroups: { string }?): Built local triangulateOn = not (opts and opts.triangulate == false) local flipV = not (opts and opts.flipV == false) local withNormals = not (opts and opts.normals == false) @@ -166772,21 +167307,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S prim.material = mat end if shapes and ref and #shapes > 1 then - local base = shapes[ref].data - local targets: { { [string]: Types.Accessor } } = {} - for k, s in shapes do - if k ~= ref then - local t = table.create(n * 3) :: { number } - for i = 1, n do - local v = vsrc[i] - for c = 0, 2 do - t[(i - 1) * 3 + c + 1] = readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4) - end - end - table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor }) - end - end - prim.targets = targets + prim.targets = targetDeltas(poly, vsrc, shapes, ref, vgroups) end table.insert(mesh.primitives, prim) table.insert(sources, vsrc) @@ -166795,7 +167316,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S local w = {} for k, s in shapes do if k ~= ref then - table.insert(w, s.value) + table.insert(w, if s.mute then 0 else s.value) -- Blender skips muted keys end end mesh.weights = w @@ -166828,8 +167349,8 @@ __FILES["model/blend/Read"] = function() -- 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 --- does: 683 lm/W, / 4 pi for point and spot), armatures (bones to joint nodes with the saved pose), skins, shape keys --- and actions sampled into channels. +-- 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") local Types = __include("Types") local Mat4 = __include("codecs/model/Mat4") @@ -166838,6 +167359,7 @@ local Mesh = __include("model/blend/Mesh") local Prim = __include("model/blend/Prim") local Anim = __include("model/blend/Anim") local Material = __include("model/blend/Material") +local Armature = __include("model/blend/Armature") local Read = {} @@ -166847,6 +167369,7 @@ type M4 = { number } type Options = { triangulate: boolean?, flipV: boolean?, + looseEdges: boolean?, -- edges outside faces as "lines" primitives (default false; always for custom shape widgets) collections: boolean?, -- collections as nodes (default true) scene: string?, -- the scene to read (default: the file's current scene) sampleStep: number?, -- frames between animation samples (default 1) @@ -166997,6 +167520,18 @@ local function mat4(v: Blend.View?, ...: string): M4? end -- ------------------------------------------------------------------------------------------------ scene +type ArmInfo = { + joints: { number }, + names: { [string]: number }, + rest: { M4 }, + restLocal: { M4 }, + bones: { Blend.View }, + deform: { boolean }, + node: number, + poseNames: { [string]: Blend.View }, + arm: Blend.View, + pose: Blend.View?, +} type Ctx = { f: Blend.File, scene: Types.Scene, @@ -167008,7 +167543,9 @@ type Ctx = { meshes: { [string]: number }, materials: { [any]: number }, textures: { [any]: number }, - armatures: { [any]: { joints: { number }, names: { [string]: number }, rest: { M4 }, restLocal: { M4 }, bones: { Blend.View }, deform: { boolean }, node: number, poseNames: { [string]: Blend.View } } }, + armatures: { [any]: ArmInfo }, + widgets: { [any]: Blend.View }, -- custom shape objects by address + widgetOrder: { any }, prefix: { [number]: M4 }, -- node -> the matrix before its basis (parent inverse, bone tail offset) props: { [number]: Props }, meta: { [string]: any }, @@ -167055,7 +167592,7 @@ local function armature(ctx: Ctx, ob: Blend.View, node: number) end end end - local info = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames } + local info: ArmInfo = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames, arm = arm, pose = pose } local function walk(list: { Blend.View }, parentJoint: number?, parentRest: M4?, depth: number) if depth > 256 then return @@ -167225,20 +167762,37 @@ local function groupNames(ob: Blend.View, me: Blend.View): { string } return out end -local function mesh(ctx: Ctx, ob: Blend.View, node: number) +local function shapeExtras(shapes: { Mesh.Shape }, ref: number?): ({ string }, { { [string]: any } }) + local names: { string }, list: { { [string]: any } } = {}, {} + for k, sh in shapes do + if k ~= ref then + table.insert(names, sh.name) + end + local rel = shapes[sh.relative + 1] + local e: { [string]: any } = { name = sh.name, value = sh.value, slider_min = sh.min, slider_max = sh.max, mute = sh.mute } + e.relative_key = if rel then rel.name else nil + e.vertex_group = sh.vgroup + table.insert(list, e) + end + return names, list +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 local me = ob:deref("data", 0, "Mesh") if not me then - return + return nil, nil end local slots = slotMaterials(ctx, ob, me) local armOb = deformArmature(ctx, ob) local armInfo = armOb and armOb.block and ctx.armatures[armOb.block.addr] - local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) + local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) .. (if lines then ":lines" else "") local skin: Prim.Skinning? = nil + local groups = groupNames(ob, me) if armInfo then local joints = {} - for gi, name in groupNames(ob, me) do + for gi, name in groups do local j = armInfo.names[name] if j and armInfo.deform[j] then joints[gi - 1] = j - 1 @@ -167253,9 +167807,23 @@ local function mesh(ctx: Ctx, ob: Blend.View, node: number) error(err or "bad mesh", 0) end local shapes, ref = Mesh.shapes(f, me, poly.nv) - local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref) + local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref, groups) local m = built.mesh - m.extras = { blend = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } } + local extras: { [string]: any } = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } + m.extras = { blend = extras } + if lines then + local lp = Prim.lines(poly, Mesh.looseEdges(f, me, poly), shapes, ref, groups) + if lp then + table.insert(m.primitives, lp) + extras.looseEdges = (lp.indices :: Types.Accessor).count // 2 + end + end + if #shapes > 1 then + local names, list = shapeExtras(shapes, ref) + m.extras = { blend = extras, targetNames = names } + extras.shape_keys = list + extras.reference_key = if ref then shapes[ref].name else nil + end table.insert(ctx.scene.meshes, m) idx = #ctx.scene.meshes ctx.meshes[key] = idx @@ -167265,6 +167833,15 @@ local function mesh(ctx: Ctx, ob: Blend.View, node: number) ctx.meta.shapeKeys[idx] = { key = me:deref("key", 0, "Key"), mesh = me, shapes = shapes, ref = ref } end end + return idx, armInfo +end + +local function mesh(ctx: Ctx, ob: Blend.View, node: number) + local addr = ob.block and ob.block.addr + local idx, armInfo = meshOf(ctx, ob, ctx.opts.looseEdges == true or (addr ~= nil and ctx.widgets[addr] ~= nil)) + if not idx then + return + end local n = ctx.scene.nodes[node] n.mesh = idx if ctx.scene.meshes[idx].weights then @@ -167348,6 +167925,14 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number): if lay and ctx.f.version < 280 then extras.layers = lay end + -- Object.visibility_flag (2.8+): OB_HIDE_VIEWPORT 1, OB_HIDE_RENDER 4; restrictflag before: VIEW 1, RENDER 4 + local vis = ob:num("visibility_flag") or ob:num("restrictflag") or 0 + if bit32.band(vis, 1) ~= 0 then + extras.hide_viewport = true + end + if bit32.band(vis, 4) ~= 0 then + extras.hide_render = true + end local mods = {} for _, md in ob:list("modifiers") do table.insert(mods, { name = md:sub("modifier") and (md:sub("modifier") :: Blend.View):str("name") or md:str("name") or "", type = (string.gsub(md.struct.name, "ModifierData$", "")) }) @@ -167368,6 +167953,77 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number): return idx 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 + local ob = ctx.objView[node] + local extras = ob and (ctx.scene.nodes[node].extras :: any).blend + if not ob or not extras then + continue + end + local list = Armature.constraints(ob, target) + if #list > 0 then + extras.constraints = list + end + local info = ob.block and ctx.armatures[ob.block.addr] + if info then + local cols = Armature.collections(info.arm, info.bones, info.pose) + extras.bone_collections = cols.list + for j, bone in info.bones do + local name = bone:str("name") or "" + local pc = info.poseNames[name] + local e = (ctx.scene.nodes[info.joints[j]].extras :: any).blend + for k, v in Armature.bone(bone, pc, info.pose) do + e[k] = v + end + e.collections = cols.byBone[name] or {} + if pc then + Armature.pose(e, pc, target, shape) + end + end + end + end +end + +-- every custom shape object: its node (and mesh) when it is in the scene, else its mesh only +local function widgetList(ctx: Ctx): { { [string]: any } } + local list = {} + for _, addr in ctx.widgetOrder do + local ob = ctx.widgets[addr] + local t = ob:num("type") or -1 + local w: { [string]: any } = { name = ob:idName() or "", type = TYPE_NAME[t] or tostring(t) } + local node = ctx.objects[addr] + if node then + w.node = node + w.mesh = ctx.scene.nodes[node].mesh + elseif t == OB_MESH then + w.mesh = (meshOf(ctx, ob, true)) + end + table.insert(list, w) + end + return list +end + -- ------------------------------------------------------------------------------------------------ animation local function apply(props: Props, curves: { Curve }, frame: number, prefix: string?, name: string?): Props local p: Props = {} @@ -167559,7 +168215,7 @@ local function animations(ctx: Ctx) v = math.clamp(Anim.evaluate(c, fr), math.min(s.min, s.max), math.max(s.min, s.max)) -- the slider range end end - buffer.writef32(out, ((i - 1) * count + k) * 4, v) + buffer.writef32(out, ((i - 1) * count + k) * 4, if s.mute then 0 else v) k += 1 end end @@ -167616,7 +168272,7 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?) } local ctx: Ctx = { f = f, scene = scene, opts = o, objects = {}, objView = {}, parentOf = {}, world = {}, meshes = {}, materials = {}, - textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, + textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, widgets = {}, widgetOrder = {}, } local order: { { ob: Blend.View, under: number? } } = {} if sc then @@ -167695,6 +168351,7 @@ 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 + rigs(ctx) for node = 1, #scene.nodes do local ob = ctx.objView[node] if ob and (ob:num("type") or -1) == OB_MESH then @@ -167702,6 +168359,9 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?) end end skins(ctx) + if next(ctx.armatures) ~= nil then + scene.meta.blend = { widgets = widgetList(ctx), bone_color_sets = Armature.colorSets() } + end if o.animations ~= false then animations(ctx) end @@ -176638,6 +177298,16 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc return nil, Err.new(Err.CORRUPT, "bad mesh " .. (i - 1)) end local mesh: Types.Mesh = { name = str(m.name), primitives = {}, weights = if type(m.weights) == "table" then table.clone(m.weights) else nil, extras = keep(m, nil) } + -- morph target names: the extras.targetNames convention (Blender's exporter, three.js, Babylon) + local tn = type(m.extras) == "table" and m.extras.targetNames + if type(tn) == "table" and #tn > 0 then + local names = {} + for k, v in tn do + names[k] = if type(v) == "string" then v else tostring(v) + end + mesh.extras = mesh.extras or {} + ;(mesh.extras :: any).targetNames = names + end for k, p in arr(m.primitives) do if type(p) ~= "table" or type(p.attributes) ~= "table" then return nil, Err.new(Err.CORRUPT, string.format("mesh %d primitive %d: no attributes", i - 1, k - 1)) @@ -177354,6 +178024,12 @@ function Write.build(scene: Types.Scene, opts: Options?): (Out?, Err.Err?) end g.weights = mesh.weights restore(g, mesh.extras) + local tn = mesh.extras and (mesh.extras :: any).targetNames + if type(tn) == "table" and #tn > 0 and g.extras == nil then + local ex = object() + ex.targetNames = table.clone(tn) + g.extras = ex + end table.insert(meshes, g) end local cameras: { any } = {} @@ -212129,7 +212805,10 @@ local function defaultFor(name: string, k: number): number return 0 end -function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? }?): (any, Err.Err?) +-- opts.targets: morph targets concatenated by name (mesh.extras.targetNames, else prim.extras.targetNames; unnamed +-- ones by index), zero where a primitive lacks one; the result's extras.targetNames / extras.weights name them and +-- give their default weights (node weights, else mesh weights) +function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?, targets: boolean? }?): (any, Err.Err?) local placed: { Types.Placed } = {} if type(input) ~= "table" then return nil, Err.new(Err.ARG, "merge: expected a Scene or a list of Primitives") @@ -212143,6 +212822,9 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end local groups: { [number]: { Primitive } } = {} local order: { number } = {} + local withTargets = opts ~= nil and opts.targets == true + local keysOf: { [Primitive]: { any } } = {} -- per baked primitive: the key (name or index) of each target + local weightOf: { [any]: number } = {} local allPoints = #placed > 0 for _, pl in placed do if pl.primitive.mode ~= "points" then @@ -212162,6 +212844,27 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? for name, a in t.attributes do copy.attributes[name] = MeshOps.f32(a) end + if withTargets and t.targets and #t.targets > 0 then + local mesh = if input.kind == "scene" and pl.mesh > 0 then input.meshes[pl.mesh] else nil + local node = if input.kind == "scene" and pl.node > 0 then input.nodes[pl.node] else nil + local names = mesh and mesh.extras and mesh.extras.targetNames or (p.extras and (p.extras :: any).targetNames) + local weights = node and node.weights or (mesh and mesh.weights) + local keys, targets = {}, {} + for k, tg in t.targets do + local key = if type(names) == "table" and type(names[k]) == "string" then names[k] else k + keys[k] = key + if weightOf[key] == nil then + weightOf[key] = weights and weights[k] or 0 + end + local own = {} + for name, a in tg do + own[name] = MeshOps.f32(a) + end + targets[k] = own + end + copy.targets = targets + keysOf[copy] = keys + end if pl.matrix ~= Scene.IDENTITY then local ok, terr = MeshOps.transform(copy, pl.matrix) if not ok then @@ -212207,6 +212910,56 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end attrs[name] = Scene.accessor(out, "f32", comps, nv) end + local targets: { { [string]: Accessor } }? = nil + local targetKeys: { any } = {} + if withTargets then + local index: { [any]: number } = {} + for _, p in list do + for _, key in keysOf[p] or {} do + if not index[key] then + table.insert(targetKeys, key) + index[key] = #targetKeys + end + end + end + if #targetKeys > 0 then + local out: { { [string]: Accessor } } = {} + for ti, key in targetKeys do + local comps: { [string]: number } = {} + for _, p in list do + for k, pk in keysOf[p] or {} do + if pk == key then + for name, a in (p.targets :: any)[k] do + comps[name] = math.max(comps[name] or 0, a.components) + end + end + end + end + local tgt: { [string]: Accessor } = {} + for name, nc in comps do + local buf = buffer.create(nv * nc * 4) -- zero where a primitive lacks the target + local base = 0 + for _, p in list do + local cnt = vertexCount(p) + for k, pk in keysOf[p] or {} do + local a = if pk == key then (p.targets :: any)[k][name] else nil + if a then + for v = 0, math.min(cnt, a.count) - 1 do + for c = 0, math.min(nc, a.components) - 1 do + writef32(buf, ((base + v) * nc + c) * 4, readf32(a.data, (v * a.components + c) * 4)) + end + end + end + end + base += cnt + end + tgt[name] = Scene.accessor(buf, "f32", nc, nv) + end + out[ti] = tgt + end + targets = out + end + end local idx = buffer.create(ni * 4) local base, at = 0, 0 for _, p in list do @@ -212217,11 +212970,22 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? at += n base += vertexCount(p) end + local extras: { [string]: any }? = nil + if targets then + local names, weights = {}, {} + for k, key in targetKeys do + names[k] = if type(key) == "string" then key else tostring(key) + weights[k] = weightOf[key] or 0 + end + extras = { targetNames = names, weights = weights } + end return { attributes = attrs, indices = if points then nil else Scene.accessor(idx, "u32", 1, ni), mode = if points then "points" else "triangles", material = material, + targets = targets, + extras = extras, } end if opts and opts.byMaterial then diff --git a/build/rosetta.lua b/build/rosetta.lua index 06435c4..84cb697 100644 --- a/build/rosetta.lua +++ b/build/rosetta.lua @@ -162835,9 +162835,10 @@ local __compat = __compat local buffer = __compat.buffer --!strict -- file's own SDNA). Supports: the scene's objects and collections, transforms with parent inverse and bone parents, -- meshes of every storage generation (MFace, MPoly / MLoop, attribute layers, AttributeStorage) with UV maps, colour -- attributes, custom normals, material slots; Principled BSDF materials; packed and external images; cameras; lights; --- armatures, skins (vertex groups, top 4 weights), shape keys; actions sampled into channels. +-- armatures, skins (vertex groups, top 4 weights), shape keys (relative keys, vertex groups); actions sampled into +-- channels; rig data (bone collections / layers, colours, custom shapes, pose settings, constraint stacks) as extras. -- Not supported: modifiers (listed in meta.source.modifiers), curves / text / metaballs / grease pencil / volumes, --- NLA strips, drivers, constraints, f-curve modifiers, linked libraries, collection instances. +-- NLA strips, drivers, constraint evaluation, f-curve modifiers, linked libraries, collection instances. -- ------------------------------------------------------------------------------------------------ requires local Err = __include("core/Err") @@ -162876,6 +162877,7 @@ local BlendFormat = { + function BlendFormat.sniff(r) local head = r:peek(7) if head == "BLENDER" then @@ -163270,6 +163272,413 @@ end return Anim end +__FILES["model/blend/Armature"] = function() +local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, __compat.bit32, __compat.type, __compat.__iter --!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; the +-- common fields for the rest). Flags from DNA_armature_types.h, DNA_action_types.h, DNA_constraint_types.h. +local Blend = __include("containers/Blend") + +local Armature = {} + +-- 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) +return { bit32.rshift(hex, 16) / 255, bit32.band(bit32.rshift(hex, 8), 255) / 255, bit32.band(hex, 255) / 255 } +end + +function Armature.colorSets() +local out = {} + for i, s in __iter(THEME) do + out[i] = { normal = rgb(s[1]), select = rgb(s[2]), active = rgb(s[3]) } + end + return out +end + +local ROTMODE= { [0] = "QUATERNION", [1] = "XYZ", [2] = "XZY", [3] = "YXZ", [4] = "YZX", [5] = "ZXY", [6] = "ZYX", [-1] = "AXIS_ANGLE" } +local SPACE= { [0] = "WORLD", [1] = "LOCAL", [2] = "POSE", [3] = "LOCAL_WITH_PARENT", [5] = "CUSTOM", [6] = "LOCAL_OWNER_ORIENT" } +local EULER_ORDER= { [0] = "AUTO", [1] = "XYZ", [2] = "XZY", [3] = "YXZ", [4] = "YZX", [5] = "ZXY", [6] = "ZYX" } +local TRACK= { [0] = "TRACK_X", [1] = "TRACK_Y", [2] = "TRACK_Z", [3] = "TRACK_NEGATIVE_X", [4] = "TRACK_NEGATIVE_Y", [5] = "TRACK_NEGATIVE_Z" } +local ROT_MIX= { [0] = "REPLACE", [1] = "OFFSET", [2] = "ADD", [3] = "BEFORE", [4] = "AFTER" } +local TRANS_MIX= { [0] = "REPLACE", [1] = "BEFORE", [2] = "AFTER", [3] = "BEFORE_FULL", [4] = "BEFORE_SPLIT", [5] = "AFTER_FULL", [6] = "AFTER_SPLIT" } +local TYPE= { + [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= { + 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 function bit(v, mask) +return bit32.band(v or 0, mask) ~= 0 +end + +local function bits(v, masks) +return { bit(v, masks[1]), bit(v, masks[2]), bit(v, masks[3]) } +end + +local function vec3(v, name, fallback) +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, name) +local s = v:str(name) + return (s and s ~= "" and{s }or{nil +})[1]end + +-- ThemeWireColor: char solid[4], select[4], active[4] (sRGB bytes) +local function wire(v) +if not v then + return nil + end + local out= {} + for _, pair in __iter({ { "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, custom) +if not index or index == 0 then + return nil + end + local c= { palette = index < 0 and"CUSTOM" or string.format("THEME%02d", index) } + local w = (index < 0 and{(wire(custom) )}or{nil +})[1] 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) +local bc = v:sub("color") + if not bc then + return nil + end + return color(bc:num("palette_index"), bc:sub("custom")) +end +-- ------------------------------------------------------------------------------------------------ bones +-- 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, bones, pose) +local out= { list = {}, byBone = {} } + local byAddr= {} + for _, b in __iter(bones) do + if b.block then + byAddr[b.block.addr] = b:str("name") or "" + end + end + local function add(name, members) + for _, m in __iter(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= {} + if #array > 0 then + for _, addr in __iter(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= {} + for i, c in __iter(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 __iter(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 __iter(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 __iter(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= {} + for _, pc in __iter(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 __iter(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, pc, pose) +local flag = bone:num("flag") or 0 + local e= { + 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"), + } + if bone:has("color") then + e.color = boneColor(bone) + elseif pc and pose then + local g = pc:num("agrp_index") or 0 + local grp = (g > 0 and{pose:list("agroups", "bActionGroup")[g] }or{nil +})[1] if grp then + e.color = color(grp:num("customCol"), grp:sub("cs")) + end + end + return e +end + +-- ------------------------------------------------------------------------------------------------ constraints +local function constraintData(e, kind, d, 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 = (d:num("type") or 0) == 1 and"DISTANCE" or"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 = (mix and{ROT_MIX[mix] }or{(bit(flag, 128) and"OFFSET" or"REPLACE") +})[1] 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 + 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.power = d:num("power") or 1 + 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 __iter(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 __iter(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 __iter(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 = (m and #m == 16 and{m }or{nil -- column-major +})[1] end +end + +-- a constraint stack (ListBase of bConstraint) in evaluation order +function Armature.constraints(owner, resolve) +local out = {} + for _, c in __iter(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= { + 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 __iter(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, pc, resolve, shape) + 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 = (tx and{(text(tx, "name") )}or{nil +})[1] 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 __iter(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 = (pc:has("color") and{(boneColor(pc) )}or{nil +})[1] e.constraints = Armature.constraints(pc, resolve) +end + +return Armature +end __FILES["model/blend/Material"] = function() local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, __compat.bit32, __compat.type, __compat.__iter --!strict -- model/blend/Material: Blender materials to Scene PBR materials. With nodes: the Principled BSDF (the one feeding the @@ -163859,8 +164268,77 @@ local v = me:num(...) return (v and v > 0 and{v }or{0 })[1]end +-- edge count and per-edge readers (vertex 1, vertex 2, sharp): ".edge_verts" / "sharp_edge" attributes or MEdge +local function edgeReaders(f, me, layers) +local ne = count(me, "totedge", "edges_num") + local ev = find(layers, "edge", "int2", ".edge_verts") + local se = find(layers, "edge", "bool", "sharp_edge") + local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") + if ne == 0 then + return 0, nil, nil, nil + end + if ev then + local er = reader(f, ev) + local sr = (se and{(reader(f, se) )}or{nil +})[1] return ne, function(i) +return er(i, 0) + end, function(i) +return er(i, 1) + end, function(i) +return sr ~= nil and sr(i, 0) ~= 0 + end + elseif medge then + local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") + if a and b then + local ser = (se and{(reader(f, se) )}or{nil +})[1] return ne, function(i) +return a(i, 0) + end, function(i) +return b(i, 0) + end, function(i) +if ser then + return ser(i, 0) ~= 0 + end + return fl ~= nil and bit32.band(fl(i, 0), ME_SHARP) ~= 0 + end + end + end + return ne, nil, nil, nil +end + +-- edges no face uses, as u32 vertex pairs (rig widgets are usually edges only) +function Mesh.looseEdges(f, me, poly) +local ne, v1, v2 = edgeReaders(f, me, poly.layers) + if not v1 or not v2 then + return buffer.create(0) + end + local nv = poly.nv + local used= {} + local cv, fs = poly.cornerVert, poly.faceStart + for fi = 0, poly.nf - 1 do + local s0, s1 = buffer.readu32(fs, fi * 4), buffer.readu32(fs, fi * 4 + 4) + for c = s0, s1 - 1 do + local a, b = buffer.readu32(cv, c * 4), buffer.readu32(cv, ((c + 1 < s1 and((c + 1 ))or(s0))) * 4) + used[math.min(a, b) * nv + math.max(a, b)] = true + end + end + local out = {} + for i = 0, ne - 1 do + local a, b = v1(i), v2(i) + if a ~= b and a >= 0 and b >= 0 and a < nv and b < nv and not used[math.min(a, b) * nv + math.max(a, b)] then + table.insert(out, a) + table.insert(out, b) + end + end + local buf = buffer.create(#out * 4) + for k, v in __iter(out) do + buffer.writeu32(buf, (k - 1) * 4, v) + end + return buf +end + function Mesh.read(f, me, limits) local layers = Mesh.layers(f, me) local nv = count(me, "totvert", "verts_num") @@ -164140,10 +164618,6 @@ local d = buffer.create(nc * 8) poly.customNormals = d end -- sharp edges - local ne = count(me, "totedge", "edges_num") - local ev = find(layers, "edge", "int2", ".edge_verts") - local se = find(layers, "edge", "bool", "sharp_edge") - local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") local flags = me:num("flag") or 0 local smoothresh = me:num("smoothresh") local autoSmooth = f.version < 401 and bit32.band(flags, ME_AUTOSMOOTH) ~= 0 @@ -164151,27 +164625,8 @@ local d = buffer.create(nc * 8) poly.autoSmooth = smoothresh or math.rad(30) end local useSharp = f.version >= 401 or autoSmooth - if useSharp and ne > 0 then - local v1, v2, sharp - if ev and se then - local er, sr = reader(f, ev), reader(f, se) - v1 = function(i)return er(i, 0) end - v2 = function(i)return er(i, 1) end - sharp = function(i)return sr(i, 0) ~= 0 end - elseif medge then - local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") - if a and b and fl then - v1 = function(i)return a(i, 0) end - v2 = function(i)return b(i, 0) end - local ser = (se and{(reader(f, se) )}or{nil -})[1] sharp = function(i) -if ser then - return ser(i, 0) ~= 0 - end - return bit32.band(fl(i, 0), ME_SHARP) ~= 0 - end - end - end + if useSharp then + local ne, v1, v2, sharp = edgeReaders(f, me, layers) if v1 and v2 and sharp then for i = 0, ne - 1 do if sharp(i) then @@ -164226,6 +164681,7 @@ local key = me:deref("key", 0, "Key") local refAddr = key:ptr("refkey") local out= {} local ref= nil + local index = 0 for i, kb in __iter(blocks) do local blk = kb:target("data") local d = buffer.create(nv * 12) @@ -164238,15 +164694,21 @@ local key = me:deref("key", 0, "Key") end end end - if kb.block and kb.block.addr == refAddr then + local isRef = kb.block ~= nil and kb.block.addr == refAddr + if isRef then ref = i + else + index =index+ 1 end - table.insert(out, { - -- unnamed in old files: Key 0 (the basis), Key 1, ... - name = kb:str("name") or ("Key " .. (i - 1)), value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, - mute = bit32.band(kb:num("flag") or 0, 2) ~= 0, + local stored = kb:str("name") + -- unnamed in old files: named as Blender's 2.45 versioning does (Basis, Key 1, Key 2, ...) + local name= (stored and stored ~= "" and{stored }or{isRef and"Basis" or"Key " .. index +})[1] table.insert(out, { + name = name, value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, + mute = bit32.band(kb:num("flag") or 0, 1) ~= 0, -- KEYBLOCK_MUTE min = kb:num("slidermin") or 0, max = kb:num("slidermax") or 1, + vgroup = (kb:str("vgroup") ~= "" and{(kb:str("vgroup") )}or{nil})[1], }) end if ref == nil and #out > 0 then @@ -164677,10 +165139,82 @@ local n = math.floor(#t / comps return Scene.accessor(b, "f32", comps, n) end --- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints +-- morph targets of the output vertices (vsrc: their mesh vertices): each key minus its relative key, times the weight +-- of the key's vertex group, as Blender evaluates relative keys -function Prim.build(poly, slots, opts, skin, shapes, ref) +local function targetDeltas(poly, vsrc, shapes, ref, groups) +local n = #vsrc + local targets= {} + for k, s in __iter(shapes) do + if k ~= ref then + local rel = shapes[s.relative + 1] or shapes[ref] + local base = rel.data + local def= nil + if s.vgroup and groups then + for gi, g in __iter(groups) do + if g == s.vgroup then + def = gi - 1 + end + end + end + local t = (table.create(n * 3) ) +for i = 1, n do + local v = vsrc[i] + local w= 1 + if def then + w = 0 + local list = poly.weights and poly.weights[v + 1] or {} + for q = 1, #list - 1, 2 do + if list[q] == def then + w = list[q + 1] + end + end + end + for c = 0, 2 do + t[(i - 1) * 3 + c + 1] = (readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4)) * w + end + end + table.insert(targets, { POSITION = accessor(t, 3) } ) + end + end + return targets +end + +-- a "lines" primitive of the given edges (u32 vertex pairs), with the morph targets the face primitives have +function Prim.lines(poly, edges, shapes, ref, groups) +local ne = math.floor(buffer.len(edges) / 8 +) if ne == 0 then + return nil + end + local out= {} + local vsrc= {} + local I = buffer.create(ne * 8) + for k = 0, ne * 2 - 1 do + local v = readu32(edges, k * 4) + local o = out[v] + if not o then + o = #vsrc + out[v] = o + table.insert(vsrc, v) + end + writeu32(I, k * 4, o) + end + local n = #vsrc + local P = buffer.create(n * 12) + for i = 1, n do + buffer.copy(P, (i - 1) * 12, poly.pos, vsrc[i] * 12, 12) + end + local prim= { attributes = { POSITION = Scene.accessor(P, "f32", 3, n) }, indices = Scene.accessor(I, "u32", 1, ne * 2), mode = "lines" } + if shapes and ref and #shapes > 1 then + prim.targets = targetDeltas(poly, vsrc, shapes, ref, groups) + end + return prim +end + +-- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints; vgroups names the +-- vertex groups (def_nr + 1) for shape keys limited to one +function Prim.build(poly, slots, opts, skin, shapes, ref, vgroups) local triangulateOn = not (opts and opts.triangulate == false) local flipV = not (opts and opts.flipV == false) local withNormals = not (opts and opts.normals == false) @@ -164830,21 +165364,7 @@ for i = 1, n do prim.material = mat end if shapes and ref and #shapes > 1 then - local base = shapes[ref].data - local targets= {} - for k, s in __iter(shapes) do - if k ~= ref then - local t = (table.create(n * 3) ) -for i = 1, n do - local v = vsrc[i] - for c = 0, 2 do - t[(i - 1) * 3 + c + 1] = readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4) - end - end - table.insert(targets, { POSITION = accessor(t, 3) } ) - end - end - prim.targets = targets + prim.targets = targetDeltas(poly, vsrc, shapes, ref, vgroups) end table.insert(mesh.primitives, prim) table.insert(sources, vsrc) @@ -164853,7 +165373,7 @@ for i = 1, n do local w = {} for k, s in __iter(shapes) do if k ~= ref then - table.insert(w, s.value) + table.insert(w, s.mute and 0 or s.value) -- Blender skips muted keys end end mesh.weights = w @@ -164873,8 +165393,8 @@ local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, _ -- 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 --- does: 683 lm/W, / 4 pi for point and spot), armatures (bones to joint nodes with the saved pose), skins, shape keys --- and actions sampled into channels. +-- 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") local Types = __include("Types") local Mat4 = __include("codecs/model/Mat4") @@ -164883,6 +165403,7 @@ local Mesh = __include("model/blend/Mesh") local Prim = __include("model/blend/Prim") local Anim = __include("model/blend/Anim") local Material = __include("model/blend/Material") +local Armature = __include("model/blend/Armature") local Read = {} @@ -164901,6 +165422,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 LA_LOCAL, LA_SUN, LA_SPOT, LA_HEMI, LA_AREA = 0, 1, 2, 3, 4 @@ -165053,6 +165575,20 @@ end + + + + + + + + + + + + + + @@ -165100,7 +165636,7 @@ local function armature(ctx, ob, node) end end end - local info = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames } + local info= { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames, arm = arm, pose = pose } local function walk(list, parentJoint, parentRest, depth) if depth > 256 then return @@ -165270,20 +165806,37 @@ local list = me:list("vertex_group_names", "bDeformGroup") return out end -local function mesh(ctx, ob, node) - local f = ctx.f +local function shapeExtras(shapes, ref) +local names, list= {}, {} + for k, sh in __iter(shapes) do + if k ~= ref then + table.insert(names, sh.name) + end + local rel = shapes[sh.relative + 1] + local e= { name = sh.name, value = sh.value, slider_min = sh.min, slider_max = sh.max, mute = sh.mute } + e.relative_key = (rel and{rel.name }or{nil +})[1] e.vertex_group = sh.vgroup + table.insert(list, e) + end + return names, list +end + +-- the scene mesh of an object's mesh data (shared between objects with the same data, slots and armature) +local function meshOf(ctx, ob, lines) +local f = ctx.f local me = ob:deref("data", 0, "Mesh") if not me then - return + return nil, nil end local slots = slotMaterials(ctx, ob, me) local armOb = deformArmature(ctx, ob) local armInfo = armOb and armOb.block and ctx.armatures[armOb.block.addr] - local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) + local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) .. (lines and":lines" or"") local skin= nil + local groups = groupNames(ob, me) if armInfo then local joints = {} - for gi, name in __iter(groupNames(ob, me)) do + for gi, name in __iter(groups) do local j = armInfo.names[name] if j and armInfo.deform[j] then joints[gi - 1] = j - 1 @@ -165298,9 +165851,23 @@ local function mesh(ctx, ob, node) error(err or "bad mesh", 0) end local shapes, ref = Mesh.shapes(f, me, poly.nv) - local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref) + local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref, groups) local m = built.mesh - m.extras = { blend = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } } + local extras= { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } + m.extras = { blend = extras } + if lines then + local lp = Prim.lines(poly, Mesh.looseEdges(f, me, poly), shapes, ref, groups) + if lp then + table.insert(m.primitives, lp) + extras.looseEdges = math.floor((lp.indices ).count / 2 +) end + end + if #shapes > 1 then + local names, list = shapeExtras(shapes, ref) + m.extras = { blend = extras, targetNames = names } + extras.shape_keys = list + extras.reference_key = (ref and{shapes[ref].name }or{nil +})[1] end table.insert(ctx.scene.meshes, m) idx = #ctx.scene.meshes ctx.meshes[key] = idx @@ -165310,6 +165877,15 @@ local function mesh(ctx, ob, node) ctx.meta.shapeKeys[idx] = { key = me:deref("key", 0, "Key"), mesh = me, shapes = shapes, ref = ref } end end + return idx, armInfo +end + +local function mesh(ctx, ob, node) + local addr = ob.block and ob.block.addr + local idx, armInfo = meshOf(ctx, ob, ctx.opts.looseEdges == true or (addr ~= nil and ctx.widgets[addr] ~= nil)) + if not idx then + return + end local n = ctx.scene.nodes[node] n.mesh = idx if ctx.scene.meshes[idx].weights then @@ -165393,6 +165969,14 @@ local addr = ob.block and ob.block.addr if lay and ctx.f.version < 280 then extras.layers = lay end + -- Object.visibility_flag (2.8+): OB_HIDE_VIEWPORT 1, OB_HIDE_RENDER 4; restrictflag before: VIEW 1, RENDER 4 + local vis = ob:num("visibility_flag") or ob:num("restrictflag") or 0 + if bit32.band(vis, 1) ~= 0 then + extras.hide_viewport = true + end + 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$", "")) }) @@ -165413,6 +165997,77 @@ local addr = ob.block and ob.block.addr return idx end +-- ------------------------------------------------------------------------------------------------ rigs +-- an object as a constraint target or custom shape: its node, else its name; widgets records custom shapes +local function resolver(ctx, widgets) +return function(ob) +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 = (addr ~= nil and{ctx.objects[addr] }or{nil +})[1] if node then + return node + end + return ob:idName() + end +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 extras = ob and (ctx.scene.nodes[node].extras ).blend + if not ob or not extras then +__DARKLUA_CONTINUE_16=true break + end + local list = Armature.constraints(ob, target) + if #list > 0 then + extras.constraints = list + end + local info = ob.block and ctx.armatures[ob.block.addr] + if info then + local cols = Armature.collections(info.arm, info.bones, info.pose) + extras.bone_collections = cols.list + for j, bone in __iter(info.bones) do + local name = bone:str("name") or "" + local pc = info.poseNames[name] + local e = (ctx.scene.nodes[info.joints[j] ].extras ).blend + for k, v in __iter(Armature.bone(bone, pc, info.pose)) do + e[k] = v + end + e.collections = cols.byBone[name] or {} + if pc then + Armature.pose(e, pc, target, shape) + end + end + end +__DARKLUA_CONTINUE_16=true until true if not __DARKLUA_CONTINUE_16 then break end end +end + +-- every custom shape object: its node (and mesh) when it is in the scene, else its mesh only +local function widgetList(ctx) +local list = {} + for _, addr in __iter(ctx.widgetOrder) do + local ob = ctx.widgets[addr] + local t = ob:num("type") or -1 + local w= { name = ob:idName() or "", type = TYPE_NAME[t] or tostring(t) } + local node = ctx.objects[addr] + if node then + w.node = node + w.mesh = ctx.scene.nodes[node].mesh + elseif t == OB_MESH then + w.mesh = (meshOf(ctx, ob, true)) + end + table.insert(list, w) + end + return list +end + -- ------------------------------------------------------------------------------------------------ animation local function apply(props, curves, frame, prefix, name) local p= {} @@ -165604,7 +166259,7 @@ local track = { translation = {}, rotation = {}, scale = {} } v = math.clamp(Anim.evaluate(c, fr), math.min(s.min, s.max), math.max(s.min, s.max)) -- the slider range end end - buffer.writef32(out, ((i - 1) * count + k) * 4, v) + buffer.writef32(out, ((i - 1) * count + k) * 4, s.mute and 0 or v) k =k+ 1 end end @@ -165661,7 +166316,7 @@ local o= opts or {} } local ctx= { f = f, scene = scene, opts = o, objects = {}, objView = {}, parentOf = {}, world = {}, meshes = {}, materials = {}, - textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, + textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, widgets = {}, widgetOrder = {}, } local order= {} if sc then @@ -165740,6 +166395,7 @@ local o= opts or {} local u = a ~= nil and firstUnder[a] or nil object(ctx, e.ob, (u and u > 0 and{u }or{nil})[1], 0) end + rigs(ctx) for node = 1, #scene.nodes do local ob = ctx.objView[node] if ob and (ob:num("type") or -1) == OB_MESH then @@ -165747,6 +166403,9 @@ local o= opts or {} end end skins(ctx) + if next(ctx.armatures) ~= nil then + scene.meta.blend = { widgets = widgetList(ctx), bone_color_sets = Armature.colorSets() } + end if o.animations ~= false then animations(ctx) end @@ -174670,6 +175329,16 @@ if type(v) ~= "table" then return nil, Err.new(Err.CORRUPT, "bad mesh " .. (i - 1)) end local mesh= { name = str(m.name), primitives = {}, weights = (type(m.weights) == "table" and{(table.clone(m.weights) )}or{nil})[1], extras = keep(m, nil) } + -- morph target names: the extras.targetNames convention (Blender's exporter, three.js, Babylon) + local tn = type(m.extras) == "table" and m.extras.targetNames + if type(tn) == "table" and #tn > 0 then + local names = {} + for k, v in __iter(tn) do + names[k] = (type(v) == "string" and{v }or{(tostring(v) +)})[1] end + mesh.extras = mesh.extras or {} + ;(mesh.extras ).targetNames = names + end for k, p in __iter(arr(m.primitives)) do if type(p) ~= "table" or type(p.attributes) ~= "table" then return nil, Err.new(Err.CORRUPT, string.format("mesh %d primitive %d: no attributes", i - 1, k - 1)) @@ -175386,6 +176055,12 @@ if type(v) ~= "table" then end g.weights = mesh.weights restore(g, mesh.extras) + local tn = mesh.extras and (mesh.extras ).targetNames + if type(tn) == "table" and #tn > 0 and g.extras == nil then + local ex = object() + ex.targetNames = table.clone(tn) + g.extras = ex + end table.insert(meshes, g) end local cameras= {} @@ -211371,6 +212046,9 @@ if string.sub(name, 1, 6) == "COLOR_" then return 0 end +-- opts.targets: morph targets concatenated by name (mesh.extras.targetNames, else prim.extras.targetNames; unnamed +-- ones by index), zero where a primitive lacks one; the result's extras.targetNames / extras.weights name them and +-- give their default weights (node weights, else mesh weights) function MeshOps.merge(input, opts) local placed= {} if type(input) ~= "table" then @@ -211385,6 +212063,9 @@ local placed= {} end local groups= {} local order= {} + local withTargets = opts ~= nil and opts.targets == true + local keysOf= {} -- per baked primitive: the key (name or index) of each target + local weightOf= {} local allPoints = #placed > 0 for _, pl in __iter(placed) do if pl.primitive.mode ~= "points" then @@ -211404,6 +212085,27 @@ local placed= {} for name, a in __iter(t.attributes) do copy.attributes[name] = MeshOps.f32(a) end + if withTargets and t.targets and #t.targets > 0 then + local mesh = (input.kind == "scene" and pl.mesh > 0 and{input.meshes[pl.mesh] }or{nil +})[1] local node = (input.kind == "scene" and pl.node > 0 and{input.nodes[pl.node] }or{nil +})[1] local names = mesh and mesh.extras and mesh.extras.targetNames or (p.extras and (p.extras ).targetNames) + local weights = node and node.weights or (mesh and mesh.weights) + local keys, targets = {}, {} + for k, tg in __iter(t.targets) do + local key = (type(names) == "table" and type(names[k]) == "string" and{names[k] }or{k +})[1] keys[k] = key + if weightOf[key] == nil then + weightOf[key] = weights and weights[k] or 0 + end + local own = {} + for name, a in __iter(tg) do + own[name] = MeshOps.f32(a) + end + targets[k] = own + end + copy.targets = targets + keysOf[copy] = keys + end if pl.matrix ~= Scene.IDENTITY then local ok, terr = MeshOps.transform(copy, pl.matrix) if not ok then @@ -211449,6 +212151,56 @@ local names= {} end attrs[name] = Scene.accessor(out, "f32", comps, nv) end + local targets= nil + local targetKeys= {} + if withTargets then + local index= {} + for _, p in __iter(list) do + for _, key in __iter(keysOf[p] or {}) do + if not index[key] then + table.insert(targetKeys, key) + index[key] = #targetKeys + end + end + end + if #targetKeys > 0 then + local out= {} + for ti, key in __iter(targetKeys) do + local comps= {} + for _, p in __iter(list) do + for k, pk in __iter(keysOf[p] or {}) do + if pk == key then + for name, a in __iter((p.targets )[k]) do + comps[name] = math.max(comps[name] or 0, a.components) + end + end + end + end + local tgt= {} + for name, nc in __iter(comps) do + local buf = buffer.create(nv * nc * 4) -- zero where a primitive lacks the target + local base = 0 + for _, p in __iter(list) do + local cnt = vertexCount(p) + for k, pk in __iter(keysOf[p] or {}) do + local a = (pk == key and{(p.targets )[k][name] }or{nil +})[1] if a then + for v = 0, math.min(cnt, a.count) - 1 do + for c = 0, math.min(nc, a.components) - 1 do + writef32(buf, ((base + v) * nc + c) * 4, readf32(a.data, (v * a.components + c) * 4)) + end + end + end + end + base =base+ cnt + end + tgt[name] = Scene.accessor(buf, "f32", nc, nv) + end + out[ti] = tgt + end + targets = out + end + end local idx = buffer.create(ni * 4) local base, at = 0, 0 for _, p in __iter(list) do @@ -211459,11 +212211,22 @@ local names= {} at =at+ n base =base+ vertexCount(p) end + local extras= nil + if targets then + local names, weights = {}, {} + for k, key in __iter(targetKeys) do + names[k] = (type(key) == "string" and{key }or{(tostring(key) +)})[1] weights[k] = weightOf[key] or 0 + end + extras = { targetNames = names, weights = weights } + end return { attributes = attrs, indices = (points and{nil }or{(Scene.accessor(idx, "u32", 1, ni))})[1], mode = points and"points" or"triangles", material = material, + targets = targets, + extras = extras, } end if opts and opts.byMaterial then diff --git a/build/rosetta.lua.linemap.json b/build/rosetta.lua.linemap.json index 439e733..ec62632 100644 --- a/build/rosetta.lua.linemap.json +++ b/build/rosetta.lua.linemap.json @@ -434,186 +434,187 @@ [162206, "src/Rosetta/model/b3d/init.luau"], [162261, "src/Rosetta/model/b3d/Read.luau"], [162832, "src/Rosetta/model/blend/init.luau"], -[162928, "src/Rosetta/model/blend/Anim.luau"], -[163274, "src/Rosetta/model/blend/Material.luau"], -[163626, "src/Rosetta/model/blend/Mesh.luau"], -[164261, "src/Rosetta/model/blend/Normals.luau"], -[164422, "src/Rosetta/model/blend/Prim.luau"], -[164871, "src/Rosetta/model/blend/Read.luau"], -[165771, "src/Rosetta/model/bsp/init.luau"], -[165962, "src/Rosetta/model/bsp/Lightmap.luau"], -[166028, "src/Rosetta/model/bsp/Quake.luau"], -[166417, "src/Rosetta/model/bsp/Quake3.luau"], -[166646, "src/Rosetta/model/bsp/Tree.luau"], -[167034, "src/Rosetta/model/buildmap/init.luau"], -[167306, "src/Rosetta/model/buildmap/Geometry.luau"], -[167709, "src/Rosetta/model/d3map/init.luau"], -[167991, "src/Rosetta/model/d3map/Parse.luau"], -[168189, "src/Rosetta/model/dae/init.luau"], -[168378, "src/Rosetta/model/dae/Math.luau"], -[168425, "src/Rosetta/model/dae/Read.luau"], -[170275, "src/Rosetta/model/dae/Write.luau"], -[170644, "src/Rosetta/model/fbx/init.luau"], -[170792, "src/Rosetta/model/fbx/Ascii.luau"], -[171062, "src/Rosetta/model/fbx/Binary.luau"], -[171517, "src/Rosetta/model/fbx/Read.luau"], -[172879, "src/Rosetta/model/fbx/Write.luau"], -[173864, "src/Rosetta/model/fbx/Xform.luau"], -[173905, "src/Rosetta/model/gltf/init.luau"], -[174142, "src/Rosetta/model/gltf/Pack.luau"], -[174211, "src/Rosetta/model/gltf/Read.luau"], -[174942, "src/Rosetta/model/gltf/Write.luau"], -[175622, "src/Rosetta/model/godot/init.luau"], -[175713, "src/Rosetta/model/godot/Build.luau"], -[176255, "src/Rosetta/model/godot/Primitives.luau"], -[176530, "src/Rosetta/model/godot/Surface.luau"], -[177015, "src/Rosetta/model/godot/Write.luau"], -[177344, "src/Rosetta/model/iqm/init.luau"], -[177420, "src/Rosetta/model/iqm/Read.luau"], -[177775, "src/Rosetta/model/iqm/Write.luau"], -[178462, "src/Rosetta/model/ldraw/init.luau"], -[178540, "src/Rosetta/model/ldraw/Colors.luau"], -[178858, "src/Rosetta/model/ldraw/Read.luau"], -[179435, "src/Rosetta/model/lwo/init.luau"], -[179511, "src/Rosetta/model/lwo/Build.luau"], -[179973, "src/Rosetta/model/lwo/Parse.luau"], -[180516, "src/Rosetta/model/map/init.luau"], -[180833, "src/Rosetta/model/map/Build.luau"], -[181077, "src/Rosetta/model/map/Parse.luau"], -[181462, "src/Rosetta/model/map/Write.luau"], -[181608, "src/Rosetta/model/mca/init.luau"], -[181950, "src/Rosetta/model/mca/Chunk.luau"], -[182083, "src/Rosetta/model/mca/Lz4Block.luau"], -[182153, "src/Rosetta/model/md2/init.luau"], -[182352, "src/Rosetta/model/md3/init.luau"], -[182580, "src/Rosetta/model/md5/init.luau"], -[182661, "src/Rosetta/model/md5/Read.luau"], -[183094, "src/Rosetta/model/mdl/init.luau"], -[183176, "src/Rosetta/model/mdl/HalfLife.luau"], -[183480, "src/Rosetta/model/mdl/Quake.luau"], -[183698, "src/Rosetta/model/ms3d/init.luau"], -[183770, "src/Rosetta/model/ms3d/Read.luau"], -[184216, "src/Rosetta/model/ms3d/Write.luau"], -[184685, "src/Rosetta/model/obj/init.luau"], -[185503, "src/Rosetta/model/obj/Mtl.luau"], -[185791, "src/Rosetta/model/off/init.luau"], -[186253, "src/Rosetta/model/ply/init.luau"], -[187358, "src/Rosetta/model/ply/Binary.luau"], -[187559, "src/Rosetta/model/pmx/init.luau"], -[187628, "src/Rosetta/model/pmx/Build.luau"], -[187924, "src/Rosetta/model/pmx/Pmd.luau"], -[188259, "src/Rosetta/model/pmx/Read.luau"], -[188663, "src/Rosetta/model/proc/init.luau"], -[189045, "src/Rosetta/model/qb/init.luau"], -[189438, "src/Rosetta/model/rbxm/init.luau"], -[190031, "src/Rosetta/model/rbxmx/init.luau"], -[190707, "src/Rosetta/model/robloxmesh/init.luau"], -[191366, "src/Rosetta/model/robloxmesh/Parse.luau"], -[191823, "src/Rosetta/model/robloxmesh/Write.luau"], -[191932, "src/Rosetta/model/schematic/init.luau"], -[192253, "src/Rosetta/model/schematic/Common.luau"], -[192373, "src/Rosetta/model/schematic/Litematic.luau"], -[192584, "src/Rosetta/model/schematic/Mcedit.luau"], -[192828, "src/Rosetta/model/schematic/Sponge.luau"], -[192957, "src/Rosetta/model/smd/init.luau"], -[193024, "src/Rosetta/model/smd/Read.luau"], -[193293, "src/Rosetta/model/smd/Write.luau"], -[193536, "src/Rosetta/model/sourcemdl/init.luau"], -[193707, "src/Rosetta/model/sourcemdl/Build.luau"], -[194137, "src/Rosetta/model/sourcemdl/Material.luau"], -[194298, "src/Rosetta/model/sourcemdl/Mdl.luau"], -[194825, "src/Rosetta/model/sourcemdl/Phy.luau"], -[194948, "src/Rosetta/model/sourcemdl/Vtx.luau"], -[195130, "src/Rosetta/model/sourcemdl/Vvd.luau"], -[195224, "src/Rosetta/model/stl/init.luau"], -[195674, "src/Rosetta/model/stl/Decode.luau"], -[195748, "src/Rosetta/model/t3d/init.luau"], -[195842, "src/Rosetta/model/t3d/Build.luau"], -[196247, "src/Rosetta/model/t3d/Parse.luau"], -[196415, "src/Rosetta/model/udmf/init.luau"], -[196498, "src/Rosetta/model/usda/init.luau"], -[196707, "src/Rosetta/model/usda/Build.luau"], -[197985, "src/Rosetta/model/usda/Parse.luau"], -[198783, "src/Rosetta/model/usda/Write.luau"], -[199600, "src/Rosetta/model/vbsp/init.luau"], -[200046, "src/Rosetta/model/vbsp/Faces.luau"], -[200270, "src/Rosetta/model/vbsp/Props.luau"], -[200362, "src/Rosetta/model/vmf/init.luau"], -[200687, "src/Rosetta/model/vmf/Build.luau"], -[201111, "src/Rosetta/model/vox/init.luau"], -[201997, "src/Rosetta/model/vox/Grid.luau"], -[202054, "src/Rosetta/model/vrml/init.luau"], -[202120, "src/Rosetta/model/x/init.luau"], -[202250, "src/Rosetta/model/x/Build.luau"], -[202813, "src/Rosetta/model/x/Parse.luau"], -[203110, "src/Rosetta/model/x3d/init.luau"], -[203188, "src/Rosetta/music/669/init.luau"], -[203395, "src/Rosetta/music/ahx/init.luau"], -[203447, "src/Rosetta/music/far/init.luau"], -[203677, "src/Rosetta/music/hmp/init.luau"], -[203942, "src/Rosetta/music/hvl/init.luau"], -[203994, "src/Rosetta/music/it/init.luau"], -[204413, "src/Rosetta/music/it/Itsex.luau"], -[204530, "src/Rosetta/music/med/init.luau"], -[205178, "src/Rosetta/music/midi/init.luau"], -[205322, "src/Rosetta/music/midi/Smf.luau"], -[205734, "src/Rosetta/music/mod/init.luau"], -[206088, "src/Rosetta/music/mtm/init.luau"], -[206311, "src/Rosetta/music/mus/init.luau"], -[206469, "src/Rosetta/music/okt/init.luau"], -[206751, "src/Rosetta/music/ptm/init.luau"], -[206974, "src/Rosetta/music/s3m/init.luau"], -[207211, "src/Rosetta/music/sf2/init.luau"], -[207255, "src/Rosetta/music/stm/init.luau"], -[207487, "src/Rosetta/music/ult/init.luau"], -[207792, "src/Rosetta/music/umx/init.luau"], -[208009, "src/Rosetta/music/xm/init.luau"], -[208571, "src/Rosetta/music/xmi/init.luau"], -[208780, "src/Rosetta/ops/AudioOps.luau"], -[209259, "src/Rosetta/ops/ImageOps/init.luau"], -[209907, "src/Rosetta/ops/ImageOps/Kernels.luau"], -[210547, "src/Rosetta/ops/LayerOps.luau"], -[210640, "src/Rosetta/ops/MeshOps/init.luau"], -[211692, "src/Rosetta/ops/MeshOps/Greedy.luau"], -[211859, "src/Rosetta/ops/MusicOps.luau"], -[212208, "src/Rosetta/playlist/m3u/init.luau"], -[212561, "src/Rosetta/playlist/pls/init.luau"], -[212739, "src/Rosetta/playlist/xspf/init.luau"], -[212942, "src/Rosetta/subtitle/ass/init.luau"], -[213386, "src/Rosetta/subtitle/srt/init.luau"], -[213559, "src/Rosetta/subtitle/vtt/init.luau"], -[213762, "src/Rosetta/vector/gamemaps/init.luau"], -[213949, "src/Rosetta/vector/gamemaps/Expand.luau"], -[214046, "src/Rosetta/vector/ldtk/init.luau"], -[214138, "src/Rosetta/vector/ldtk/Read.luau"], -[214474, "src/Rosetta/vector/ldtk/Write.luau"], -[215122, "src/Rosetta/vector/ogmo/init.luau"], -[215241, "src/Rosetta/vector/ogmo/Read.luau"], -[215646, "src/Rosetta/vector/ogmo/Write.luau"], -[216000, "src/Rosetta/vector/svg/init.luau"], -[216086, "src/Rosetta/vector/svg/Parse.luau"], -[216832, "src/Rosetta/vector/svg/PathData.luau"], -[217200, "src/Rosetta/vector/svg/Render.luau"], -[217617, "src/Rosetta/vector/svg/Style.luau"], -[217989, "src/Rosetta/vector/svg/Write.luau"], -[218189, "src/Rosetta/vector/tiled/init.luau"], -[218369, "src/Rosetta/vector/tiled/Data.luau"], -[218520, "src/Rosetta/vector/tiled/Tmj.luau"], -[219151, "src/Rosetta/vector/tiled/Tmx.luau"], -[219763, "src/Rosetta/video/avi/init.luau"], -[220057, "src/Rosetta/video/fli/init.luau"], -[220208, "src/Rosetta/video/flv/init.luau"], -[220592, "src/Rosetta/video/flv/Amf.luau"], -[220750, "src/Rosetta/video/flv/Mux.luau"], -[221031, "src/Rosetta/video/h264/init.luau"], -[221170, "src/Rosetta/video/mkv/init.luau"], -[221326, "src/Rosetta/video/mkv/Mux.luau"], -[221605, "src/Rosetta/video/mkv/Parse.luau"], -[222326, "src/Rosetta/video/mp4/init.luau"], -[222654, "src/Rosetta/video/mpegps/init.luau"], -[222841, "src/Rosetta/video/mpegts/init.luau"], -[223087, "src/Rosetta/video/ogv/init.luau"], -[223482, "src/Rosetta/video/smk/init.luau"], -[223791, "src/Rosetta/video/smk/Audio.luau"], -[223911, "src/Rosetta/video/y4m/init.luau"] +[162930, "src/Rosetta/model/blend/Anim.luau"], +[163276, "src/Rosetta/model/blend/Armature.luau"], +[163683, "src/Rosetta/model/blend/Material.luau"], +[164035, "src/Rosetta/model/blend/Mesh.luau"], +[164723, "src/Rosetta/model/blend/Normals.luau"], +[164884, "src/Rosetta/model/blend/Prim.luau"], +[165391, "src/Rosetta/model/blend/Read.luau"], +[166430, "src/Rosetta/model/bsp/init.luau"], +[166621, "src/Rosetta/model/bsp/Lightmap.luau"], +[166687, "src/Rosetta/model/bsp/Quake.luau"], +[167076, "src/Rosetta/model/bsp/Quake3.luau"], +[167305, "src/Rosetta/model/bsp/Tree.luau"], +[167693, "src/Rosetta/model/buildmap/init.luau"], +[167965, "src/Rosetta/model/buildmap/Geometry.luau"], +[168368, "src/Rosetta/model/d3map/init.luau"], +[168650, "src/Rosetta/model/d3map/Parse.luau"], +[168848, "src/Rosetta/model/dae/init.luau"], +[169037, "src/Rosetta/model/dae/Math.luau"], +[169084, "src/Rosetta/model/dae/Read.luau"], +[170934, "src/Rosetta/model/dae/Write.luau"], +[171303, "src/Rosetta/model/fbx/init.luau"], +[171451, "src/Rosetta/model/fbx/Ascii.luau"], +[171721, "src/Rosetta/model/fbx/Binary.luau"], +[172176, "src/Rosetta/model/fbx/Read.luau"], +[173538, "src/Rosetta/model/fbx/Write.luau"], +[174523, "src/Rosetta/model/fbx/Xform.luau"], +[174564, "src/Rosetta/model/gltf/init.luau"], +[174801, "src/Rosetta/model/gltf/Pack.luau"], +[174870, "src/Rosetta/model/gltf/Read.luau"], +[175611, "src/Rosetta/model/gltf/Write.luau"], +[176297, "src/Rosetta/model/godot/init.luau"], +[176388, "src/Rosetta/model/godot/Build.luau"], +[176930, "src/Rosetta/model/godot/Primitives.luau"], +[177205, "src/Rosetta/model/godot/Surface.luau"], +[177690, "src/Rosetta/model/godot/Write.luau"], +[178019, "src/Rosetta/model/iqm/init.luau"], +[178095, "src/Rosetta/model/iqm/Read.luau"], +[178450, "src/Rosetta/model/iqm/Write.luau"], +[179137, "src/Rosetta/model/ldraw/init.luau"], +[179215, "src/Rosetta/model/ldraw/Colors.luau"], +[179533, "src/Rosetta/model/ldraw/Read.luau"], +[180110, "src/Rosetta/model/lwo/init.luau"], +[180186, "src/Rosetta/model/lwo/Build.luau"], +[180648, "src/Rosetta/model/lwo/Parse.luau"], +[181191, "src/Rosetta/model/map/init.luau"], +[181508, "src/Rosetta/model/map/Build.luau"], +[181752, "src/Rosetta/model/map/Parse.luau"], +[182137, "src/Rosetta/model/map/Write.luau"], +[182283, "src/Rosetta/model/mca/init.luau"], +[182625, "src/Rosetta/model/mca/Chunk.luau"], +[182758, "src/Rosetta/model/mca/Lz4Block.luau"], +[182828, "src/Rosetta/model/md2/init.luau"], +[183027, "src/Rosetta/model/md3/init.luau"], +[183255, "src/Rosetta/model/md5/init.luau"], +[183336, "src/Rosetta/model/md5/Read.luau"], +[183769, "src/Rosetta/model/mdl/init.luau"], +[183851, "src/Rosetta/model/mdl/HalfLife.luau"], +[184155, "src/Rosetta/model/mdl/Quake.luau"], +[184373, "src/Rosetta/model/ms3d/init.luau"], +[184445, "src/Rosetta/model/ms3d/Read.luau"], +[184891, "src/Rosetta/model/ms3d/Write.luau"], +[185360, "src/Rosetta/model/obj/init.luau"], +[186178, "src/Rosetta/model/obj/Mtl.luau"], +[186466, "src/Rosetta/model/off/init.luau"], +[186928, "src/Rosetta/model/ply/init.luau"], +[188033, "src/Rosetta/model/ply/Binary.luau"], +[188234, "src/Rosetta/model/pmx/init.luau"], +[188303, "src/Rosetta/model/pmx/Build.luau"], +[188599, "src/Rosetta/model/pmx/Pmd.luau"], +[188934, "src/Rosetta/model/pmx/Read.luau"], +[189338, "src/Rosetta/model/proc/init.luau"], +[189720, "src/Rosetta/model/qb/init.luau"], +[190113, "src/Rosetta/model/rbxm/init.luau"], +[190706, "src/Rosetta/model/rbxmx/init.luau"], +[191382, "src/Rosetta/model/robloxmesh/init.luau"], +[192041, "src/Rosetta/model/robloxmesh/Parse.luau"], +[192498, "src/Rosetta/model/robloxmesh/Write.luau"], +[192607, "src/Rosetta/model/schematic/init.luau"], +[192928, "src/Rosetta/model/schematic/Common.luau"], +[193048, "src/Rosetta/model/schematic/Litematic.luau"], +[193259, "src/Rosetta/model/schematic/Mcedit.luau"], +[193503, "src/Rosetta/model/schematic/Sponge.luau"], +[193632, "src/Rosetta/model/smd/init.luau"], +[193699, "src/Rosetta/model/smd/Read.luau"], +[193968, "src/Rosetta/model/smd/Write.luau"], +[194211, "src/Rosetta/model/sourcemdl/init.luau"], +[194382, "src/Rosetta/model/sourcemdl/Build.luau"], +[194812, "src/Rosetta/model/sourcemdl/Material.luau"], +[194973, "src/Rosetta/model/sourcemdl/Mdl.luau"], +[195500, "src/Rosetta/model/sourcemdl/Phy.luau"], +[195623, "src/Rosetta/model/sourcemdl/Vtx.luau"], +[195805, "src/Rosetta/model/sourcemdl/Vvd.luau"], +[195899, "src/Rosetta/model/stl/init.luau"], +[196349, "src/Rosetta/model/stl/Decode.luau"], +[196423, "src/Rosetta/model/t3d/init.luau"], +[196517, "src/Rosetta/model/t3d/Build.luau"], +[196922, "src/Rosetta/model/t3d/Parse.luau"], +[197090, "src/Rosetta/model/udmf/init.luau"], +[197173, "src/Rosetta/model/usda/init.luau"], +[197382, "src/Rosetta/model/usda/Build.luau"], +[198660, "src/Rosetta/model/usda/Parse.luau"], +[199458, "src/Rosetta/model/usda/Write.luau"], +[200275, "src/Rosetta/model/vbsp/init.luau"], +[200721, "src/Rosetta/model/vbsp/Faces.luau"], +[200945, "src/Rosetta/model/vbsp/Props.luau"], +[201037, "src/Rosetta/model/vmf/init.luau"], +[201362, "src/Rosetta/model/vmf/Build.luau"], +[201786, "src/Rosetta/model/vox/init.luau"], +[202672, "src/Rosetta/model/vox/Grid.luau"], +[202729, "src/Rosetta/model/vrml/init.luau"], +[202795, "src/Rosetta/model/x/init.luau"], +[202925, "src/Rosetta/model/x/Build.luau"], +[203488, "src/Rosetta/model/x/Parse.luau"], +[203785, "src/Rosetta/model/x3d/init.luau"], +[203863, "src/Rosetta/music/669/init.luau"], +[204070, "src/Rosetta/music/ahx/init.luau"], +[204122, "src/Rosetta/music/far/init.luau"], +[204352, "src/Rosetta/music/hmp/init.luau"], +[204617, "src/Rosetta/music/hvl/init.luau"], +[204669, "src/Rosetta/music/it/init.luau"], +[205088, "src/Rosetta/music/it/Itsex.luau"], +[205205, "src/Rosetta/music/med/init.luau"], +[205853, "src/Rosetta/music/midi/init.luau"], +[205997, "src/Rosetta/music/midi/Smf.luau"], +[206409, "src/Rosetta/music/mod/init.luau"], +[206763, "src/Rosetta/music/mtm/init.luau"], +[206986, "src/Rosetta/music/mus/init.luau"], +[207144, "src/Rosetta/music/okt/init.luau"], +[207426, "src/Rosetta/music/ptm/init.luau"], +[207649, "src/Rosetta/music/s3m/init.luau"], +[207886, "src/Rosetta/music/sf2/init.luau"], +[207930, "src/Rosetta/music/stm/init.luau"], +[208162, "src/Rosetta/music/ult/init.luau"], +[208467, "src/Rosetta/music/umx/init.luau"], +[208684, "src/Rosetta/music/xm/init.luau"], +[209246, "src/Rosetta/music/xmi/init.luau"], +[209455, "src/Rosetta/ops/AudioOps.luau"], +[209934, "src/Rosetta/ops/ImageOps/init.luau"], +[210582, "src/Rosetta/ops/ImageOps/Kernels.luau"], +[211222, "src/Rosetta/ops/LayerOps.luau"], +[211315, "src/Rosetta/ops/MeshOps/init.luau"], +[212455, "src/Rosetta/ops/MeshOps/Greedy.luau"], +[212622, "src/Rosetta/ops/MusicOps.luau"], +[212971, "src/Rosetta/playlist/m3u/init.luau"], +[213324, "src/Rosetta/playlist/pls/init.luau"], +[213502, "src/Rosetta/playlist/xspf/init.luau"], +[213705, "src/Rosetta/subtitle/ass/init.luau"], +[214149, "src/Rosetta/subtitle/srt/init.luau"], +[214322, "src/Rosetta/subtitle/vtt/init.luau"], +[214525, "src/Rosetta/vector/gamemaps/init.luau"], +[214712, "src/Rosetta/vector/gamemaps/Expand.luau"], +[214809, "src/Rosetta/vector/ldtk/init.luau"], +[214901, "src/Rosetta/vector/ldtk/Read.luau"], +[215237, "src/Rosetta/vector/ldtk/Write.luau"], +[215885, "src/Rosetta/vector/ogmo/init.luau"], +[216004, "src/Rosetta/vector/ogmo/Read.luau"], +[216409, "src/Rosetta/vector/ogmo/Write.luau"], +[216763, "src/Rosetta/vector/svg/init.luau"], +[216849, "src/Rosetta/vector/svg/Parse.luau"], +[217595, "src/Rosetta/vector/svg/PathData.luau"], +[217963, "src/Rosetta/vector/svg/Render.luau"], +[218380, "src/Rosetta/vector/svg/Style.luau"], +[218752, "src/Rosetta/vector/svg/Write.luau"], +[218952, "src/Rosetta/vector/tiled/init.luau"], +[219132, "src/Rosetta/vector/tiled/Data.luau"], +[219283, "src/Rosetta/vector/tiled/Tmj.luau"], +[219914, "src/Rosetta/vector/tiled/Tmx.luau"], +[220526, "src/Rosetta/video/avi/init.luau"], +[220820, "src/Rosetta/video/fli/init.luau"], +[220971, "src/Rosetta/video/flv/init.luau"], +[221355, "src/Rosetta/video/flv/Amf.luau"], +[221513, "src/Rosetta/video/flv/Mux.luau"], +[221794, "src/Rosetta/video/h264/init.luau"], +[221933, "src/Rosetta/video/mkv/init.luau"], +[222089, "src/Rosetta/video/mkv/Mux.luau"], +[222368, "src/Rosetta/video/mkv/Parse.luau"], +[223089, "src/Rosetta/video/mp4/init.luau"], +[223417, "src/Rosetta/video/mpegps/init.luau"], +[223604, "src/Rosetta/video/mpegts/init.luau"], +[223850, "src/Rosetta/video/ogv/init.luau"], +[224245, "src/Rosetta/video/smk/init.luau"], +[224554, "src/Rosetta/video/smk/Audio.luau"], +[224674, "src/Rosetta/video/y4m/init.luau"] ] diff --git a/docs/guide/scene.md b/docs/guide/scene.md index 57fab06..53c1857 100644 --- a/docs/guide/scene.md +++ b/docs/guide/scene.md @@ -246,7 +246,7 @@ Every function accepts any accessor type and writes `f32` attributes and `u32` i | Function | Does | |---|---| -| `merge(scene, opts?)` | bakes every primitive (node transforms applied) into one triangle primitive; `byMaterial = true` returns one per material. Lines and points are skipped; skins / morphs stay in rest pose | +| `merge(scene, opts?)` | bakes every primitive (node transforms applied) into one triangle primitive; `byMaterial = true` returns one per material. Lines and points are skipped; skins / morphs stay in rest pose; `targets = true` keeps the morph targets, merged by name (`mesh.extras.targetNames`, else by index) and zero where a primitive lacks one, named in the result's `extras.targetNames` with default weights in `extras.weights` | | `bounds(primitive \| scene)` | `min { x, y, z }, max { x, y, z }` (a scene: world space) | | `transform(prim, matrix)` | in place: positions, normals, tangents (and morph deltas); a mirroring matrix flips the winding | | `triangulate(prim, opts?)` | a new triangle primitive from strips, fans and polygons (`method = "earclip"` default, or `"fan"`) | @@ -959,9 +959,10 @@ cameras, lights, armatures, skins, shape keys and actions. Blender is z-up (`met | Materials | the Principled BSDF (base colour, metallic, roughness, alpha, emission and its strength, image textures on Base Color / Emission, Normal through a Normal Map node, Roughness / Metallic through Separate RGB), else the legacy `r, g, b` colour | | Images | packed images as encoded bytes (`texture.data`), external ones as `uri` (relative to the `.blend`, bytes through `files` / `resolve`) | | Cameras, lights | perspective (`yfov` from the lens, sensor fit and render aspect) / orthographic; point, sun, spot (watts to candela / lux as Blender's glTF exporter does); area lights are skipped (`meta.source.skippedLights`) | -| Armatures | a joint node per bone (rest pose times the saved pose), one skin per deformed mesh | +| Armatures | a joint node per bone (rest pose times the saved pose), one skin per deformed mesh; the rig data (bones, collections, colours, custom shapes, constraints) in `extras.blend`, see [Rig data](#rig-data) | +| Shape keys | morph targets (each key minus its relative key, times its vertex group's weights, as Blender evaluates them), named in `mesh.extras.targetNames`; muted keys have weight 0; every key's fields in `mesh.extras.blend.shape_keys` | | Animation | the active action of each object, armature (pose bones) and shape key set, f-curves evaluated (constant, linear, bezier, easing; extrapolation) and sampled once per frame (`sampleStep`) into channels; time 0 is `meta.source.frameZero` at `meta.source.fps` | -| Not loaded | modifiers (listed per object in `meta.source.modifiers`, never applied), curves, text, metaballs, grease pencil, volumes, NLA strips, drivers, constraints, linked libraries | +| Not loaded | modifiers (listed per object in `meta.source.modifiers`, never applied), curves, text, metaballs, grease pencil, volumes, NLA strips, drivers, linked libraries; constraints are data only (never evaluated) | ```luau local scene = assert(F.model.load(riggedBlendBytes)) @@ -974,6 +975,70 @@ assert(scene.materials[1].name == "Skin" and scene.materials[1].roughness > 0.69 print(src.modifiers.Tube[1].type) -- Armature (not applied) ``` +### Rig data + +Field names are Blender's Python API names. Spaces: the armature node carries the armature object's transform; each +joint node's rest matrix is relative to its parent bone (root bones: to the armature object), so a joint's world rest +matrix is armature world x `Bone.matrix_local`, and the skins' inverse bind matrices map mesh space to that joint space +(glTF's convention). Roll is already part of the rest matrices. Constraints are read, never evaluated: joint nodes +hold the saved pose. + +| Where | Fields | +|---|---| +| joint node `extras.blend` (Bone) | `bone = true`, `length`, `deform`, `use_connect`, `show_wire`, `hide`, `head_local` / `tail_local` (armature space), `roll` (radians, armature space), `collections` (names, in collection order), `color` | +| joint node `extras.blend` (pose bone) | `rotation_mode` (`"QUATERNION"`, `"XYZ"` .. `"ZYX"`, `"AXIS_ANGLE"`), `lock_location` / `lock_rotation` / `lock_scale` (3 booleans), `lock_rotation_w`, `lock_rotations_4d`, `custom_shape` (node index, or the object's name when it is not in the scene), `custom_shape_transform` (bone name), `custom_shape_translation` / `custom_shape_rotation_euler` / `custom_shape_scale_xyz`, `use_custom_shape_bone_size`, `custom_shape_wire_width` (4.2+), `ik_stretch`, `lock_ik_x/y/z`, `use_ik_limit_x/y/z`, `ik_min_x/y/z`, `ik_max_x/y/z`, `ik_stiffness_x/y/z`, `pose_color`, `constraints` | +| armature node `extras.blend` | `bone_collections`: `{ name, visible, parent? }` (parent by name; files before 4.0 get Blender 4.0's conversion: a `Layer N` collection per used layer, then one hidden collection per bone group) | +| any object node `extras.blend` | `constraints` (object constraints), `hide_viewport`, `hide_render` (when set) | +| `scene.meta.blend` | `widgets`: every custom shape object `{ name, type, node?, mesh? }` (objects outside the scene's collections get a mesh without a node; edge-only meshes as `"lines"` primitives); `bone_color_sets`: the default theme's `THEME01` .. `THEME20` | + +A colour is `{ palette, normal, select, active }`: `palette` is `"THEME01"` .. `"THEME20"` or `"CUSTOM"` (nil: the +default colour), the three colours are 0..1 sRGB (the custom colours, or the theme set's). Before 4.0 the bone group +colour is the bone's `color`, as Blender's conversion does. + +A constraint (in stack order) is `{ type, name, influence, mute, target, subtarget, owner_space, target_space }` with +`target` a node index or an object name; `space_object` / `space_subtarget` for `"CUSTOM"` spaces, `head_tail` / +`use_bbone_shape` where Blender has them, `targets` (`{ target, subtarget, weight }`) for multi-target constraints. +Type fields: + +| Type | Fields | +|---|---| +| `IK` | `pole_target`, `pole_subtarget`, `pole_angle`, `chain_count`, `iterations`, `use_tail`, `use_stretch`, `use_location`, `use_rotation`, `weight`, `orient_weight`, `lock_location_x/y/z`, `lock_rotation_x/y/z`, `ik_type`, `distance` | +| `COPY_ROTATION` | `use_x/y/z`, `invert_x/y/z`, `euler_order`, `mix_mode` | +| `COPY_LOCATION` | `use_x/y/z`, `invert_x/y/z`, `use_offset` | +| `COPY_SCALE` | `use_x/y/z`, `use_offset`, `power` | +| `COPY_TRANSFORMS` | `mix_mode`, `remove_target_shear` | +| `DAMPED_TRACK` | `track_axis` | +| `LIMIT_ROTATION` | `use_limit_x/y/z`, `min_x/y/z`, `max_x/y/z`, `euler_order`, `use_transform_limit` | +| `LIMIT_LOCATION`, `LIMIT_SCALE` | `use_min_x/y/z`, `use_max_x/y/z`, `min_x/y/z`, `max_x/y/z`, `use_transform_limit` | +| `CHILD_OF` | `use_location_x/y/z`, `use_rotation_x/y/z`, `use_scale_x/y/z`, `set_inverse_pending`, `inverse_matrix` (column-major) | + +Shape keys: `mesh.extras.blend.shape_keys` lists every key in file order (the basis too) as `{ name, value, slider_min, +slider_max, mute, relative_key, vertex_group? }`, and `reference_key` names the basis. Actions on shape key values +become `weights` channels (clamped to the slider range). + +```luau +local rig = assert(F.model.load(riggedBlendBytes)) +local joint = {} +for _, node in rig.nodes do + local e = node.extras and node.extras.blend + if e and e.bone then + joint[node.name] = e + end +end +local upper, ctrl = joint.upper, joint.ctrl +assert(upper.deform and not ctrl.deform) +assert(upper.head_local[3] == 1 and upper.tail_local[3] == 2 and upper.roll == 0) +assert(ctrl.collections[1] == "Controls" and ctrl.custom_shape == "WGT_ring") -- a widget outside the scene +local track = upper.constraints[1] +assert(track.type == "DAMPED_TRACK" and rig.nodes[track.target].name == "Rig" and track.subtarget == "ctrl") +print(track.track_axis, track.influence) -- TRACK_Y 1 +local widget = rig.meta.blend.widgets[1] +assert(widget.name == "WGT_ring" and widget.node == nil) +assert(rig.meshes[widget.mesh].primitives[1].mode == "lines") +local tube = rig.meshes[1] +assert(tube.extras.targetNames[1] == "Bulge" and tube.extras.blend.shape_keys[2].slider_max == 2) +``` + Anything else in the file is reachable through the container: `F.format("blend").open` parses the blocks and the SDNA, and every struct reads by field name (`get`, `num`, `str`, `vec`, `sub`, `deref`, `list` for ListBase, `array` and `pointers` behind pointers). diff --git a/src/Rosetta/model/blend/Armature.luau b/src/Rosetta/model/blend/Armature.luau new file mode 100644 index 0000000..bc4b32e --- /dev/null +++ b/src/Rosetta/model/blend/Armature.luau @@ -0,0 +1,405 @@ +--!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; the +-- common fields for the rest). 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 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"), + } + 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 + 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.power = d:num("power") or 1 + 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 + e.constraints = Armature.constraints(pc, resolve) +end + +return Armature diff --git a/src/Rosetta/model/blend/Mesh.luau b/src/Rosetta/model/blend/Mesh.luau index 25155b5..a972b34 100644 --- a/src/Rosetta/model/blend/Mesh.luau +++ b/src/Rosetta/model/blend/Mesh.luau @@ -236,6 +236,75 @@ end export type Limits = { maxVertices: number? } +-- edge count and per-edge readers (vertex 1, vertex 2, sharp): ".edge_verts" / "sharp_edge" attributes or MEdge +local function edgeReaders(f: Blend.File, me: Blend.View, layers: { Layer }): (number, ((number) -> number)?, ((number) -> number)?, ((number) -> boolean)?) + local ne = count(me, "totedge", "edges_num") + local ev = find(layers, "edge", "int2", ".edge_verts") + local se = find(layers, "edge", "bool", "sharp_edge") + local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") + if ne == 0 then + return 0, nil, nil, nil + end + if ev then + local er = reader(f, ev) + local sr = if se then reader(f, se) else nil + return ne, function(i: number): number + return er(i, 0) + end, function(i: number): number + return er(i, 1) + end, function(i: number): boolean + return sr ~= nil and sr(i, 0) ~= 0 + end + elseif medge then + local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") + if a and b then + local ser = if se then reader(f, se) else nil + return ne, function(i: number): number + return a(i, 0) + end, function(i: number): number + return b(i, 0) + end, function(i: number): boolean + if ser then + return ser(i, 0) ~= 0 + end + return fl ~= nil and bit32.band(fl(i, 0), ME_SHARP) ~= 0 + end + end + end + return ne, nil, nil, nil +end + +-- edges no face uses, as u32 vertex pairs (rig widgets are usually edges only) +function Mesh.looseEdges(f: Blend.File, me: Blend.View, poly: Poly): buffer + local ne, v1, v2 = edgeReaders(f, me, poly.layers) + if not v1 or not v2 then + return buffer.create(0) + end + local nv = poly.nv + local used: { [number]: boolean } = {} + local cv, fs = poly.cornerVert, poly.faceStart + for fi = 0, poly.nf - 1 do + local s0, s1 = buffer.readu32(fs, fi * 4), buffer.readu32(fs, fi * 4 + 4) + for c = s0, s1 - 1 do + local a, b = buffer.readu32(cv, c * 4), buffer.readu32(cv, (if c + 1 < s1 then c + 1 else s0) * 4) + used[math.min(a, b) * nv + math.max(a, b)] = true + end + end + local out = {} + for i = 0, ne - 1 do + local a, b = v1(i), v2(i) + if a ~= b and a >= 0 and b >= 0 and a < nv and b < nv and not used[math.min(a, b) * nv + math.max(a, b)] then + table.insert(out, a) + table.insert(out, b) + end + end + local buf = buffer.create(#out * 4) + for k, v in out do + buffer.writeu32(buf, (k - 1) * 4, v) + end + return buf +end + function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, string?) local layers = Mesh.layers(f, me) local nv = count(me, "totvert", "verts_num") @@ -515,10 +584,6 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri poly.customNormals = d end -- sharp edges - local ne = count(me, "totedge", "edges_num") - local ev = find(layers, "edge", "int2", ".edge_verts") - local se = find(layers, "edge", "bool", "sharp_edge") - local medge = find(layers, "edge", "MEdge") or legacy(f, me, "medge", "MEdge", "edge") local flags = me:num("flag") or 0 local smoothresh = me:num("smoothresh") local autoSmooth = f.version < 401 and bit32.band(flags, ME_AUTOSMOOTH) ~= 0 @@ -526,27 +591,8 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri poly.autoSmooth = smoothresh or math.rad(30) end local useSharp = f.version >= 401 or autoSmooth - if useSharp and ne > 0 then - local v1, v2, sharp - if ev and se then - local er, sr = reader(f, ev), reader(f, se) - v1 = function(i: number): number return er(i, 0) end - v2 = function(i: number): number return er(i, 1) end - sharp = function(i: number): boolean return sr(i, 0) ~= 0 end - elseif medge then - local a, b, fl = fieldReader(f, medge, "v1"), fieldReader(f, medge, "v2"), fieldReader(f, medge, "flag") - if a and b and fl then - v1 = function(i: number): number return a(i, 0) end - v2 = function(i: number): number return b(i, 0) end - local ser = if se then reader(f, se) else nil - sharp = function(i: number): boolean - if ser then - return ser(i, 0) ~= 0 - end - return bit32.band(fl(i, 0), ME_SHARP) ~= 0 - end - end - end + if useSharp then + local ne, v1, v2, sharp = edgeReaders(f, me, layers) if v1 and v2 and sharp then for i = 0, ne - 1 do if sharp(i) then @@ -591,7 +637,7 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri end -- shape keys: { name, value, relative (0-based), data: f32 x 3 per vertex } in file order, and the reference index -export type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number } +export type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number, vgroup: string? } function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, number?, Blend.View?) local key = me:deref("key", 0, "Key") if not key then @@ -601,6 +647,7 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num local refAddr = key:ptr("refkey") local out: { Shape } = {} local ref: number? = nil + local index = 0 for i, kb in blocks do local blk = kb:target("data") local d = buffer.create(nv * 12) @@ -613,15 +660,21 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num end end end - if kb.block and kb.block.addr == refAddr then + local isRef = kb.block ~= nil and kb.block.addr == refAddr + if isRef then ref = i + else + index += 1 end + local stored = kb:str("name") + -- unnamed in old files: named as Blender's 2.45 versioning does (Basis, Key 1, Key 2, ...) + local name: string = if stored and stored ~= "" then stored elseif isRef then "Basis" else "Key " .. index table.insert(out, { - -- unnamed in old files: Key 0 (the basis), Key 1, ... - name = kb:str("name") or ("Key " .. (i - 1)), value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, - mute = bit32.band(kb:num("flag") or 0, 2) ~= 0, + name = name, value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d, + mute = bit32.band(kb:num("flag") or 0, 1) ~= 0, -- KEYBLOCK_MUTE min = kb:num("slidermin") or 0, max = kb:num("slidermax") or 1, + vgroup = if kb:str("vgroup") ~= "" then kb:str("vgroup") else nil, }) end if ref == nil and #out > 0 then diff --git a/src/Rosetta/model/blend/Prim.luau b/src/Rosetta/model/blend/Prim.luau index 010b907..570a290 100644 --- a/src/Rosetta/model/blend/Prim.luau +++ b/src/Rosetta/model/blend/Prim.luau @@ -258,8 +258,80 @@ end export type Built = { mesh: Types.Mesh, slots: { number }, vertexSource: { { number } } } --- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints -function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: Skinning?, shapes: { MeshT.Shape }?, ref: number?): Built +-- morph targets of the output vertices (vsrc: their mesh vertices): each key minus its relative key, times the weight +-- of the key's vertex group, as Blender evaluates relative keys +local function targetDeltas(poly: MeshT.Poly, vsrc: { number }, shapes: { MeshT.Shape }, ref: number, groups: { string }?): { { [string]: Types.Accessor } } + local n = #vsrc + local targets: { { [string]: Types.Accessor } } = {} + for k, s in shapes do + if k ~= ref then + local rel = shapes[s.relative + 1] or shapes[ref] + local base = rel.data + local def: number? = nil + if s.vgroup and groups then + for gi, g in groups do + if g == s.vgroup then + def = gi - 1 + end + end + end + local t = table.create(n * 3) :: { number } + for i = 1, n do + local v = vsrc[i] + local w: number = 1 + if def then + w = 0 + local list = poly.weights and poly.weights[v + 1] or {} + for q = 1, #list - 1, 2 do + if list[q] == def then + w = list[q + 1] + end + end + end + for c = 0, 2 do + t[(i - 1) * 3 + c + 1] = (readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4)) * w + end + end + table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor }) + end + end + return targets +end + +-- a "lines" primitive of the given edges (u32 vertex pairs), with the morph targets the face primitives have +function Prim.lines(poly: MeshT.Poly, edges: buffer, shapes: { MeshT.Shape }?, ref: number?, groups: { string }?): Types.Primitive? + local ne = buffer.len(edges) // 8 + if ne == 0 then + return nil + end + local out: { [number]: number } = {} + local vsrc: { number } = {} + local I = buffer.create(ne * 8) + for k = 0, ne * 2 - 1 do + local v = readu32(edges, k * 4) + local o = out[v] + if not o then + o = #vsrc + out[v] = o + table.insert(vsrc, v) + end + writeu32(I, k * 4, o) + end + local n = #vsrc + local P = buffer.create(n * 12) + for i = 1, n do + buffer.copy(P, (i - 1) * 12, poly.pos, vsrc[i] * 12, 12) + end + local prim: Types.Primitive = { attributes = { POSITION = Scene.accessor(P, "f32", 3, n) }, indices = Scene.accessor(I, "u32", 1, ne * 2), mode = "lines" } + if shapes and ref and #shapes > 1 then + prim.targets = targetDeltas(poly, vsrc, shapes, ref, groups) + end + return prim +end + +-- slots[k] = the scene material of slot k - 1 (or 0 for none); skin maps deform groups to joints; vgroups names the +-- vertex groups (def_nr + 1) for shape keys limited to one +function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: Skinning?, shapes: { MeshT.Shape }?, ref: number?, vgroups: { string }?): Built local triangulateOn = not (opts and opts.triangulate == false) local flipV = not (opts and opts.flipV == false) local withNormals = not (opts and opts.normals == false) @@ -409,21 +481,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S prim.material = mat end if shapes and ref and #shapes > 1 then - local base = shapes[ref].data - local targets: { { [string]: Types.Accessor } } = {} - for k, s in shapes do - if k ~= ref then - local t = table.create(n * 3) :: { number } - for i = 1, n do - local v = vsrc[i] - for c = 0, 2 do - t[(i - 1) * 3 + c + 1] = readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4) - end - end - table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor }) - end - end - prim.targets = targets + prim.targets = targetDeltas(poly, vsrc, shapes, ref, vgroups) end table.insert(mesh.primitives, prim) table.insert(sources, vsrc) @@ -432,7 +490,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S local w = {} for k, s in shapes do if k ~= ref then - table.insert(w, s.value) + table.insert(w, if s.mute then 0 else s.value) -- Blender skips muted keys end end mesh.weights = w diff --git a/src/Rosetta/model/blend/Read.luau b/src/Rosetta/model/blend/Read.luau index 494c5e0..e6fb1e0 100644 --- a/src/Rosetta/model/blend/Read.luau +++ b/src/Rosetta/model/blend/Read.luau @@ -3,8 +3,8 @@ -- 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 --- does: 683 lm/W, / 4 pi for point and spot), armatures (bones to joint nodes with the saved pose), skins, shape keys --- and actions sampled into channels. +-- 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 = require("@rosetta/containers/Blend") local Types = require("@rosetta/Types") local Mat4 = require("@rosetta/codecs/model/Mat4") @@ -13,6 +13,7 @@ local Mesh = require("@rosetta/model/blend/Mesh") 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 Read = {} @@ -22,6 +23,7 @@ type M4 = { number } export type Options = { triangulate: boolean?, flipV: boolean?, + looseEdges: boolean?, -- edges outside faces as "lines" primitives (default false; always for custom shape widgets) collections: boolean?, -- collections as nodes (default true) scene: string?, -- the scene to read (default: the file's current scene) sampleStep: number?, -- frames between animation samples (default 1) @@ -172,6 +174,18 @@ local function mat4(v: Blend.View?, ...: string): M4? end -- ------------------------------------------------------------------------------------------------ scene +type ArmInfo = { + joints: { number }, + names: { [string]: number }, + rest: { M4 }, + restLocal: { M4 }, + bones: { Blend.View }, + deform: { boolean }, + node: number, + poseNames: { [string]: Blend.View }, + arm: Blend.View, + pose: Blend.View?, +} type Ctx = { f: Blend.File, scene: Types.Scene, @@ -183,7 +197,9 @@ type Ctx = { meshes: { [string]: number }, materials: { [any]: number }, textures: { [any]: number }, - armatures: { [any]: { joints: { number }, names: { [string]: number }, rest: { M4 }, restLocal: { M4 }, bones: { Blend.View }, deform: { boolean }, node: number, poseNames: { [string]: Blend.View } } }, + armatures: { [any]: ArmInfo }, + widgets: { [any]: Blend.View }, -- custom shape objects by address + widgetOrder: { any }, prefix: { [number]: M4 }, -- node -> the matrix before its basis (parent inverse, bone tail offset) props: { [number]: Props }, meta: { [string]: any }, @@ -230,7 +246,7 @@ local function armature(ctx: Ctx, ob: Blend.View, node: number) end end end - local info = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames } + local info: ArmInfo = { joints = {}, names = {}, rest = {}, restLocal = {}, bones = {}, deform = {}, node = node, poseNames = poseNames, arm = arm, pose = pose } local function walk(list: { Blend.View }, parentJoint: number?, parentRest: M4?, depth: number) if depth > 256 then return @@ -400,20 +416,37 @@ local function groupNames(ob: Blend.View, me: Blend.View): { string } return out end -local function mesh(ctx: Ctx, ob: Blend.View, node: number) +local function shapeExtras(shapes: { Mesh.Shape }, ref: number?): ({ string }, { { [string]: any } }) + local names: { string }, list: { { [string]: any } } = {}, {} + for k, sh in shapes do + if k ~= ref then + table.insert(names, sh.name) + end + local rel = shapes[sh.relative + 1] + local e: { [string]: any } = { name = sh.name, value = sh.value, slider_min = sh.min, slider_max = sh.max, mute = sh.mute } + e.relative_key = if rel then rel.name else nil + e.vertex_group = sh.vgroup + table.insert(list, e) + end + return names, list +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 local me = ob:deref("data", 0, "Mesh") if not me then - return + return nil, nil end local slots = slotMaterials(ctx, ob, me) local armOb = deformArmature(ctx, ob) local armInfo = armOb and armOb.block and ctx.armatures[armOb.block.addr] - local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) + local key = tostring(me.block and me.block.index) .. ":" .. table.concat(slots, ",") .. ":" .. tostring(armOb and armOb.block and armOb.block.index) .. (if lines then ":lines" else "") local skin: Prim.Skinning? = nil + local groups = groupNames(ob, me) if armInfo then local joints = {} - for gi, name in groupNames(ob, me) do + for gi, name in groups do local j = armInfo.names[name] if j and armInfo.deform[j] then joints[gi - 1] = j - 1 @@ -428,9 +461,23 @@ local function mesh(ctx: Ctx, ob: Blend.View, node: number) error(err or "bad mesh", 0) end local shapes, ref = Mesh.shapes(f, me, poly.nv) - local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref) + local built = Prim.build(poly, slots, { triangulate = ctx.opts.triangulate, flipV = ctx.opts.flipV }, skin, shapes, ref, groups) local m = built.mesh - m.extras = { blend = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } } + local extras: { [string]: any } = { vertices = poly.nv, faces = poly.nf, corners = poly.nc, generation = poly.generation, uvs = #poly.uvs, colors = #poly.colors, shapes = #shapes } + m.extras = { blend = extras } + if lines then + local lp = Prim.lines(poly, Mesh.looseEdges(f, me, poly), shapes, ref, groups) + if lp then + table.insert(m.primitives, lp) + extras.looseEdges = (lp.indices :: Types.Accessor).count // 2 + end + end + if #shapes > 1 then + local names, list = shapeExtras(shapes, ref) + m.extras = { blend = extras, targetNames = names } + extras.shape_keys = list + extras.reference_key = if ref then shapes[ref].name else nil + end table.insert(ctx.scene.meshes, m) idx = #ctx.scene.meshes ctx.meshes[key] = idx @@ -440,6 +487,15 @@ local function mesh(ctx: Ctx, ob: Blend.View, node: number) ctx.meta.shapeKeys[idx] = { key = me:deref("key", 0, "Key"), mesh = me, shapes = shapes, ref = ref } end end + return idx, armInfo +end + +local function mesh(ctx: Ctx, ob: Blend.View, node: number) + local addr = ob.block and ob.block.addr + local idx, armInfo = meshOf(ctx, ob, ctx.opts.looseEdges == true or (addr ~= nil and ctx.widgets[addr] ~= nil)) + if not idx then + return + end local n = ctx.scene.nodes[node] n.mesh = idx if ctx.scene.meshes[idx].weights then @@ -523,6 +579,14 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number): if lay and ctx.f.version < 280 then extras.layers = lay end + -- Object.visibility_flag (2.8+): OB_HIDE_VIEWPORT 1, OB_HIDE_RENDER 4; restrictflag before: VIEW 1, RENDER 4 + local vis = ob:num("visibility_flag") or ob:num("restrictflag") or 0 + if bit32.band(vis, 1) ~= 0 then + extras.hide_viewport = true + end + if bit32.band(vis, 4) ~= 0 then + extras.hide_render = true + end local mods = {} for _, md in ob:list("modifiers") do table.insert(mods, { name = md:sub("modifier") and (md:sub("modifier") :: Blend.View):str("name") or md:str("name") or "", type = (string.gsub(md.struct.name, "ModifierData$", "")) }) @@ -543,6 +607,77 @@ local function object(ctx: Ctx, ob: Blend.View, under: number?, depth: number): return idx 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 + local ob = ctx.objView[node] + local extras = ob and (ctx.scene.nodes[node].extras :: any).blend + if not ob or not extras then + continue + end + local list = Armature.constraints(ob, target) + if #list > 0 then + extras.constraints = list + end + local info = ob.block and ctx.armatures[ob.block.addr] + if info then + local cols = Armature.collections(info.arm, info.bones, info.pose) + extras.bone_collections = cols.list + for j, bone in info.bones do + local name = bone:str("name") or "" + local pc = info.poseNames[name] + local e = (ctx.scene.nodes[info.joints[j]].extras :: any).blend + for k, v in Armature.bone(bone, pc, info.pose) do + e[k] = v + end + e.collections = cols.byBone[name] or {} + if pc then + Armature.pose(e, pc, target, shape) + end + end + end + end +end + +-- every custom shape object: its node (and mesh) when it is in the scene, else its mesh only +local function widgetList(ctx: Ctx): { { [string]: any } } + local list = {} + for _, addr in ctx.widgetOrder do + local ob = ctx.widgets[addr] + local t = ob:num("type") or -1 + local w: { [string]: any } = { name = ob:idName() or "", type = TYPE_NAME[t] or tostring(t) } + local node = ctx.objects[addr] + if node then + w.node = node + w.mesh = ctx.scene.nodes[node].mesh + elseif t == OB_MESH then + w.mesh = (meshOf(ctx, ob, true)) + end + table.insert(list, w) + end + return list +end + -- ------------------------------------------------------------------------------------------------ animation local function apply(props: Props, curves: { Curve }, frame: number, prefix: string?, name: string?): Props local p: Props = {} @@ -734,7 +869,7 @@ local function animations(ctx: Ctx) v = math.clamp(Anim.evaluate(c, fr), math.min(s.min, s.max), math.max(s.min, s.max)) -- the slider range end end - buffer.writef32(out, ((i - 1) * count + k) * 4, v) + buffer.writef32(out, ((i - 1) * count + k) * 4, if s.mute then 0 else v) k += 1 end end @@ -791,7 +926,7 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?) } local ctx: Ctx = { f = f, scene = scene, opts = o, objects = {}, objView = {}, parentOf = {}, world = {}, meshes = {}, materials = {}, - textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, + textures = {}, armatures = {}, prefix = {}, props = {}, meta = meta, pending = {}, widgets = {}, widgetOrder = {}, } local order: { { ob: Blend.View, under: number? } } = {} if sc then @@ -870,6 +1005,7 @@ 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 + rigs(ctx) for node = 1, #scene.nodes do local ob = ctx.objView[node] if ob and (ob:num("type") or -1) == OB_MESH then @@ -877,6 +1013,9 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?) end end skins(ctx) + if next(ctx.armatures) ~= nil then + scene.meta.blend = { widgets = widgetList(ctx), bone_color_sets = Armature.colorSets() } + end if o.animations ~= false then animations(ctx) end diff --git a/src/Rosetta/model/blend/init.luau b/src/Rosetta/model/blend/init.luau index 0f14405..e62f158 100644 --- a/src/Rosetta/model/blend/init.luau +++ b/src/Rosetta/model/blend/init.luau @@ -4,9 +4,10 @@ -- file's own SDNA). Supports: the scene's objects and collections, transforms with parent inverse and bone parents, -- meshes of every storage generation (MFace, MPoly / MLoop, attribute layers, AttributeStorage) with UV maps, colour -- attributes, custom normals, material slots; Principled BSDF materials; packed and external images; cameras; lights; --- armatures, skins (vertex groups, top 4 weights), shape keys; actions sampled into channels. +-- armatures, skins (vertex groups, top 4 weights), shape keys (relative keys, vertex groups); actions sampled into +-- channels; rig data (bone collections / layers, colours, custom shapes, pose settings, constraint stacks) as extras. -- Not supported: modifiers (listed in meta.source.modifiers), curves / text / metaballs / grease pencil / volumes, --- NLA strips, drivers, constraints, f-curve modifiers, linked libraries, collection instances. +-- NLA strips, drivers, constraint evaluation, f-curve modifiers, linked libraries, collection instances. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -32,6 +33,7 @@ local BlendFormat = { export type DecodeOptions = { triangulate: boolean?, -- default true (false: "polygons" primitives) flipV: boolean?, -- default true (glTF's top-left UV origin) + looseEdges: boolean?, -- edges outside faces as "lines" primitives (default false; always for custom shape widgets) collections: boolean?, -- collections as nodes (default true) scene: string?, -- a scene by name (default: the file's current scene) sampleStep: number?, -- frames between animation samples (default 1) diff --git a/src/Rosetta/model/gltf/Read.luau b/src/Rosetta/model/gltf/Read.luau index f3d1176..90222d6 100644 --- a/src/Rosetta/model/gltf/Read.luau +++ b/src/Rosetta/model/gltf/Read.luau @@ -460,6 +460,16 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc return nil, Err.new(Err.CORRUPT, "bad mesh " .. (i - 1)) end local mesh: Types.Mesh = { name = str(m.name), primitives = {}, weights = if type(m.weights) == "table" then table.clone(m.weights) else nil, extras = keep(m, nil) } + -- morph target names: the extras.targetNames convention (Blender's exporter, three.js, Babylon) + local tn = type(m.extras) == "table" and m.extras.targetNames + if type(tn) == "table" and #tn > 0 then + local names = {} + for k, v in tn do + names[k] = if type(v) == "string" then v else tostring(v) + end + mesh.extras = mesh.extras or {} + ;(mesh.extras :: any).targetNames = names + end for k, p in arr(m.primitives) do if type(p) ~= "table" or type(p.attributes) ~= "table" then return nil, Err.new(Err.CORRUPT, string.format("mesh %d primitive %d: no attributes", i - 1, k - 1)) diff --git a/src/Rosetta/model/gltf/Write.luau b/src/Rosetta/model/gltf/Write.luau index 9ff38ff..a94ecd0 100644 --- a/src/Rosetta/model/gltf/Write.luau +++ b/src/Rosetta/model/gltf/Write.luau @@ -445,6 +445,12 @@ function Write.build(scene: Types.Scene, opts: Options?): (Out?, Err.Err?) end g.weights = mesh.weights restore(g, mesh.extras) + local tn = mesh.extras and (mesh.extras :: any).targetNames + if type(tn) == "table" and #tn > 0 and g.extras == nil then + local ex = object() + ex.targetNames = table.clone(tn) + g.extras = ex + end table.insert(meshes, g) end local cameras: { any } = {} diff --git a/src/Rosetta/ops/MeshOps/init.luau b/src/Rosetta/ops/MeshOps/init.luau index 2d03efa..0491e26 100644 --- a/src/Rosetta/ops/MeshOps/init.luau +++ b/src/Rosetta/ops/MeshOps/init.luau @@ -732,7 +732,10 @@ local function defaultFor(name: string, k: number): number return 0 end -function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? }?): (any, Err.Err?) +-- opts.targets: morph targets concatenated by name (mesh.extras.targetNames, else prim.extras.targetNames; unnamed +-- ones by index), zero where a primitive lacks one; the result's extras.targetNames / extras.weights name them and +-- give their default weights (node weights, else mesh weights) +function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?, targets: boolean? }?): (any, Err.Err?) local placed: { Types.Placed } = {} if type(input) ~= "table" then return nil, Err.new(Err.ARG, "merge: expected a Scene or a list of Primitives") @@ -746,6 +749,9 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end local groups: { [number]: { Primitive } } = {} local order: { number } = {} + local withTargets = opts ~= nil and opts.targets == true + local keysOf: { [Primitive]: { any } } = {} -- per baked primitive: the key (name or index) of each target + local weightOf: { [any]: number } = {} local allPoints = #placed > 0 for _, pl in placed do if pl.primitive.mode ~= "points" then @@ -765,6 +771,27 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? for name, a in t.attributes do copy.attributes[name] = MeshOps.f32(a) end + if withTargets and t.targets and #t.targets > 0 then + local mesh = if input.kind == "scene" and pl.mesh > 0 then input.meshes[pl.mesh] else nil + local node = if input.kind == "scene" and pl.node > 0 then input.nodes[pl.node] else nil + local names = mesh and mesh.extras and mesh.extras.targetNames or (p.extras and (p.extras :: any).targetNames) + local weights = node and node.weights or (mesh and mesh.weights) + local keys, targets = {}, {} + for k, tg in t.targets do + local key = if type(names) == "table" and type(names[k]) == "string" then names[k] else k + keys[k] = key + if weightOf[key] == nil then + weightOf[key] = weights and weights[k] or 0 + end + local own = {} + for name, a in tg do + own[name] = MeshOps.f32(a) + end + targets[k] = own + end + copy.targets = targets + keysOf[copy] = keys + end if pl.matrix ~= Scene.IDENTITY then local ok, terr = MeshOps.transform(copy, pl.matrix) if not ok then @@ -810,6 +837,56 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end attrs[name] = Scene.accessor(out, "f32", comps, nv) end + local targets: { { [string]: Accessor } }? = nil + local targetKeys: { any } = {} + if withTargets then + local index: { [any]: number } = {} + for _, p in list do + for _, key in keysOf[p] or {} do + if not index[key] then + table.insert(targetKeys, key) + index[key] = #targetKeys + end + end + end + if #targetKeys > 0 then + local out: { { [string]: Accessor } } = {} + for ti, key in targetKeys do + local comps: { [string]: number } = {} + for _, p in list do + for k, pk in keysOf[p] or {} do + if pk == key then + for name, a in (p.targets :: any)[k] do + comps[name] = math.max(comps[name] or 0, a.components) + end + end + end + end + local tgt: { [string]: Accessor } = {} + for name, nc in comps do + local buf = buffer.create(nv * nc * 4) -- zero where a primitive lacks the target + local base = 0 + for _, p in list do + local cnt = vertexCount(p) + for k, pk in keysOf[p] or {} do + local a = if pk == key then (p.targets :: any)[k][name] else nil + if a then + for v = 0, math.min(cnt, a.count) - 1 do + for c = 0, math.min(nc, a.components) - 1 do + writef32(buf, ((base + v) * nc + c) * 4, readf32(a.data, (v * a.components + c) * 4)) + end + end + end + end + base += cnt + end + tgt[name] = Scene.accessor(buf, "f32", nc, nv) + end + out[ti] = tgt + end + targets = out + end + end local idx = buffer.create(ni * 4) local base, at = 0, 0 for _, p in list do @@ -820,11 +897,22 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? at += n base += vertexCount(p) end + local extras: { [string]: any }? = nil + if targets then + local names, weights = {}, {} + for k, key in targetKeys do + names[k] = if type(key) == "string" then key else tostring(key) + weights[k] = weightOf[key] or 0 + end + extras = { targetNames = names, weights = weights } + end return { attributes = attrs, indices = if points then nil else Scene.accessor(idx, "u32", 1, ni), mode = if points then "points" else "triangles", material = material, + targets = targets, + extras = extras, } end if opts and opts.byMaterial then diff --git a/tests/model/blend.luau b/tests/model/blend.luau index 31d4fd9..05b73d4 100644 --- a/tests/model/blend.luau +++ b/tests/model/blend.luau @@ -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) diff --git a/tests/model/gltf.luau b/tests/model/gltf.luau index 26a1383..77e35d0 100644 --- a/tests/model/gltf.luau +++ b/tests/model/gltf.luau @@ -229,6 +229,11 @@ do t:check(#skin.animations == 1 and skin.animations[1].channels[1].path == "rotation", "skin animation") local morph = load("SimpleMorph/glTF") t:check(#morph.meshes[1].primitives[1].targets == 2 and morph.meshes[1].weights[1] == 0.5, "morph targets, weights") + -- mesh.extras.targetNames: written as glTF mesh extras, read back from them + morph.meshes[1].extras = { targetNames = { "first", "second" } } + local named = decode(Gltf.encode(morph, { binary = true }) :: buffer) :: any + local tn = named and named.meshes[1].extras and named.meshes[1].extras.targetNames + t:check(tn and tn[1] == "first" and tn[2] == "second", "targetNames round trip") local q = load("AnimatedMorphCube/glTF-Quantized") local qp = q.meshes[1].primitives[1] t:check(qp.attributes.NORMAL.type == "snorm8" and qp.targets and qp.targets[1].POSITION.type == "i16", "quantized normals / target deltas") diff --git a/tests/ops/meshops.luau b/tests/ops/meshops.luau index 128e679..ec4f00a 100644 --- a/tests/ops/meshops.luau +++ b/tests/ops/meshops.luau @@ -290,6 +290,44 @@ do t:check(e and e.code == Err.ARG, "merge junk") end +-- merge with morph targets: by name across meshes, zero where missing, deltas through the node's linear part only +do + local s = S.new() + local tri = { 0, 0, 0, 1, 0, 0, 0, 1, 0 } + local a = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles", targets = { + { POSITION = f32({ 0, 0, 1, 0, 0, 1, 0, 0, 1 }, 3) }, { POSITION = f32({ 1, 0, 0, 1, 0, 0, 1, 0, 0 }, 3) }, + } } + local b = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles", targets = { + { POSITION = f32({ 0, 2, 0, 0, 2, 0, 0, 2, 0 }, 3) }, { POSITION = f32({ 0, 0, 3, 0, 0, 3, 0, 0, 3 }, 3) }, + } } + local c = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles" } + s.meshes = { + { primitives = { a }, weights = { 0.5, 0.25 }, extras = { targetNames = { "smile", "blink" } } }, + { primitives = { b }, weights = { 0.75, 1 }, extras = { targetNames = { "blink", "puff" } } }, + { primitives = { c } }, + } + s.nodes = { + { children = {}, mesh = 1, scale = { 2, 2, 2 } }, + { children = {}, mesh = 2, translation = { 5, 0, 0 } }, + { children = {}, mesh = 3 }, + } + s.roots = { 1, 2, 3 } + t:check(MeshOps.merge(s).targets == nil, "merge drops targets by default") + local m = MeshOps.merge(s, { targets = true }) :: any + local names = m.extras and m.extras.targetNames or {} + t:check(#m.targets == 3 and names[1] == "smile" and names[2] == "blink" and names[3] == "puff", "targets merged by name") + t:check(m.extras.weights[1] == 0.5 and m.extras.weights[2] == 0.25 and m.extras.weights[3] == 1, "default weights (first mesh wins)") + local function d(ti: number, v: number, k: number): number + return get(m.targets[ti].POSITION, v, k) + end + t:check(near(d(1, 0, 2), 2) and near(d(1, 3, 2), 0) and near(d(1, 6, 2), 0), "smile: scaled by the node, zero elsewhere") + t:check(near(d(2, 0, 0), 2) and near(d(2, 3, 1), 2) and near(d(2, 6, 0), 0), "blink from both meshes, translation ignored") + t:check(near(d(3, 3, 2), 3) and near(d(3, 0, 2), 0), "puff from the second mesh only") + t:check(get(a.targets[1].POSITION, 0, 2) == 1, "the source targets are untouched") + local list = MeshOps.merge({ a, c }, { targets = true }) :: any + t:check(#list.targets == 2 and list.extras.targetNames[1] == "1", "a list of primitives: targets by index") +end + do local s = S.fromPrimitive(MeshOps.triangulate(cube()) :: any) s.nodes[1].translation = { 1, 2, 3 } diff --git a/tools/fixtures/_blend_make.py b/tools/fixtures/_blend_make.py index 0d91b24..0de943d 100644 --- a/tools/fixtures/_blend_make.py +++ b/tools/fixtures/_blend_make.py @@ -7,6 +7,11 @@ # keys, actions on bones / objects / shape keys (bezier, linear, constant), a bone parent # scene__z.blend scene_ saved compressed (gzip before 3.0, zstd after) # compat_.blend 2.7x only: the "legacy mesh format" save (MFace tessellated faces) +# ctl_.blend a control rig: deform / control bones, layers + bone groups (before 4.0) or bone +# collections and colours (4.x), custom shapes (a hidden widget in the scene, one outside it), +# pose-bone constraints (IK, copy rotation / location / transforms, damped track, limit +# rotation, child of, stretch to), lock and IK flags; shape keys with slider ranges, mute, +# relative keys, a vertex group, and a shape-key action import bpy, sys, os, math out = sys.argv[sys.argv.index("--") + 1] @@ -394,8 +399,234 @@ def rig_file(): save(os.path.join(out, "rig_%s.blend" % TAG)) +def setp(obj, attr, value): + if hasattr(obj, attr): + try: + setattr(obj, attr, value) + except (TypeError, ValueError, AttributeError): + pass + + +def ctl_file(): + reset() + sc = bpy.context.scene + tgt = link(bpy.data.objects.new("Tgt", None)) + tgt.location = (1, 2, 3) + tgt.rotation_euler = (0.2, 0.4, 0.1) + cv = [(math.cos(a * math.pi / 4), math.sin(a * math.pi / 4), 0) for a in range(8)] + circle = bpy.data.meshes.new("WGT_circle") + circle.from_pydata(cv, [(a, (a + 1) % 8) for a in range(8)], []) + circle.update() + wcircle = bpy.data.objects.new("WGT_circle", circle) + if OLD: + wcircle.use_fake_user = True + else: + wgts = bpy.data.collections.new("Widgets") # not linked to the scene + wgts.objects.link(wcircle) + box = bpy.data.meshes.new("WGT_box") + box.from_pydata([(x, y, z) for z in (0, 1) for y in (-0.5, 0.5) for x in (-0.5, 0.5)], + [(0, 1), (1, 3), (3, 2), (2, 0), (4, 5), (5, 7), (7, 6), (6, 4), (0, 4), (1, 5), (2, 6), (3, 7)], []) + box.update() + wbox = link(bpy.data.objects.new("WGT_box", box)) + wbox.location = (0, 0, -3) + if OLD: + wbox.hide = True + else: + wbox.hide_viewport = True + wbox.hide_render = True + arm_data = bpy.data.armatures.new("CtlData") + rig = link(bpy.data.objects.new("Ctl", arm_data)) + rig.location = (0.5, -1, 0.25) + rig.rotation_euler = (0.1, 0, 0.3) + rig.scale = (1, 1, 1.5) + if OLD: + sc.objects.active = rig + else: + bpy.context.view_layer.objects.active = rig + bpy.ops.object.mode_set(mode="EDIT") + eb = arm_data.edit_bones + spec = ( # name, head, tail, roll, parent, connect, deform + ("root", (0, 0, 0), (0, 1, 0), 0.0, None, False, False), + ("spine", (0, 0, 0.2), (0, 0.1, 1.2), 0.4, "root", False, True), + ("upper", (0, 0.1, 1.2), (0.8, 0.1, 1.3), -0.7, "spine", True, True), + ("lower", (0.8, 0.1, 1.3), (1.6, 0.3, 1.1), 1.1, "upper", True, True), + ("hand_ik", (1.6, 0.3, 1.1), (1.6, 0.8, 1.1), 0.0, "root", False, False), + ("pole", (0.8, -1, 1.3), (0.8, -1.3, 1.3), 0.2, "root", False, False), + ("limit", (-0.5, 0, 0.5), (-0.5, 0, 1.1), 2.5, "root", False, True), + ) + for name, head, tail, roll, parent, connect, deform in spec: + b = eb.new(name) + b.head, b.tail, b.roll = head, tail, roll + if parent: + b.parent = eb[parent] + b.use_connect = connect + b.use_deform = deform + bpy.ops.object.mode_set(mode="OBJECT") + bones = arm_data.bones + bones["root"].show_wire = True + pose = rig.pose.bones + if V >= (4, 0): + ctrl = arm_data.collections.new("Ctrl") + try: + ik = arm_data.collections.new("IK", parent=ctrl) + except TypeError: + ik = arm_data.collections.new("IK") + deform = arm_data.collections.new("Deform") + mch = arm_data.collections.new("MCH") + for c in list(arm_data.collections): + for b in list(c.bones): + c.unassign(b) + for n in ("root", "hand_ik", "pole"): + ctrl.assign(bones[n]) + for n in ("hand_ik", "pole"): + ik.assign(bones[n]) + for n in ("spine", "upper", "lower", "limit"): + deform.assign(bones[n]) + mch.assign(bones["limit"]) + ik.is_visible = False + mch.is_visible = False + bones["root"].color.palette = "THEME04" + bones["pole"].color.palette = "CUSTOM" + bones["pole"].color.custom.normal = (1.0, 0.0, 0.0) + bones["pole"].color.custom.select = (0.0, 1.0, 0.0) + bones["pole"].color.custom.active = (0.0, 0.0, 1.0) + pose["spine"].color.palette = "THEME09" + else: + layers = {"root": (0,), "hand_ik": (0, 1), "pole": (0, 1), "spine": (16,), "upper": (16,), "lower": (16,), "limit": (16, 31)} + for n, ls in layers.items(): + bones[n].layers = [i in ls for i in range(32)] + arm_data.layers = [i in (0, 16) for i in range(32)] + g1 = rig.pose.bone_groups.new(name="Ctrls") + g1.color_set = "THEME04" + g2 = rig.pose.bone_groups.new(name="Custom") + g2.color_set = "CUSTOM" + g2.colors.normal = (1.0, 0.0, 0.0) + g2.colors.select = (0.0, 1.0, 0.0) + g2.colors.active = (0.0, 0.0, 1.0) + pose["root"].bone_group = g1 + pose["hand_ik"].bone_group = g1 + pose["pole"].bone_group = g2 + # custom shapes + pose["root"].custom_shape = wcircle + setp(pose["root"], "custom_shape_scale", 1.5) + setp(pose["root"], "custom_shape_scale_xyz", (1.5, 0.5, 2.0)) + pose["spine"].custom_shape = wbox + pose["spine"].custom_shape_transform = pose["root"] + setp(pose["spine"], "custom_shape_translation", (0, 0.1, 0)) + setp(pose["spine"], "custom_shape_rotation_euler", (0.1, 0.2, 0.3)) + setp(pose["spine"], "use_custom_shape_bone_size", False) + pose["pole"].custom_shape = wcircle + # locks, rotation modes, IK settings + pose["root"].lock_location = (True, True, True) + pose["spine"].rotation_mode = "QUATERNION" + pose["spine"].lock_rotation = (True, False, True) + pose["spine"].lock_rotation_w = True + pose["spine"].lock_rotations_4d = True + pose["limit"].lock_scale = (False, True, False) + pose["upper"].ik_stretch = 0.1 + pose["upper"].lock_ik_y = True + pose["upper"].use_ik_limit_x = True + pose["upper"].ik_min_x = -0.5 + pose["upper"].ik_max_x = 0.7 + pose["upper"].ik_stiffness_z = 0.25 + pose["lower"].rotation_mode = "YZX" + pose["hand_ik"].rotation_mode = "AXIS_ANGLE" + # constraints + c = pose["spine"].constraints.new("COPY_ROTATION") + c.name = "CopyRot" + c.target = tgt + c.use_y = False + c.invert_x = True + setp(c, "mix_mode", "ADD") + c.owner_space, c.target_space = "LOCAL_WITH_PARENT", "LOCAL" + c.influence = 0.75 + c = pose["upper"].constraints.new("COPY_LOCATION") + c.target, c.subtarget = rig, "root" + c.use_offset = True + c.use_z = False + c.invert_z = True + c.head_tail = 0.5 + c.target_space = "POSE" + c = pose["upper"].constraints.new("LIMIT_LOCATION") + c.use_min_x, c.min_x = True, -0.25 + c.use_max_z, c.max_z = True, 0.75 + c.use_transform_limit = True + c = pose["upper"].constraints.new("COPY_SCALE") + c.target = tgt + c.use_y = False + c.use_offset = True + c = pose["lower"].constraints.new("IK") + c.target, c.subtarget = rig, "hand_ik" + c.pole_target, c.pole_subtarget = rig, "pole" + c.pole_angle = -1.2 + c.chain_count = 2 + c.use_tail = True + c.use_stretch = False + c.iterations = 300 + c.weight = 0.6 + c = pose["hand_ik"].constraints.new("COPY_TRANSFORMS") + c.target = tgt + setp(c, "mix_mode", "BEFORE") + c.influence = 0.3 + c.mute = True + c = pose["pole"].constraints.new("DAMPED_TRACK") + c.target, c.subtarget = rig, "lower" + c.track_axis = "TRACK_NEGATIVE_Z" + c.head_tail = 0.25 + c = pose["limit"].constraints.new("LIMIT_ROTATION") + c.use_limit_x, c.min_x, c.max_x = True, -0.3, 0.4 + c.use_limit_z, c.min_z, c.max_z = True, -1.0, 1.0 + c.owner_space = "LOCAL" + c = pose["limit"].constraints.new("CHILD_OF") + c.name = "Parent" + c.target = tgt + c.use_rotation_y = False + c.use_scale_x = False + from mathutils import Matrix + c.inverse_matrix = Matrix(((1, 0, 0, 0.5), (0, 0, -1, 0.25), (0, 1, 0, -1), (0, 0, 0, 1))) + c = pose["limit"].constraints.new("STRETCH_TO") + c.target = tgt + c.influence = 0.5 + # shape keys on a mesh parented to the rig + fv = [(x * 0.5, 0, z * 0.5) for z in range(3) for x in range(3)] + ff = [(0, 1, 4, 3), (1, 2, 5, 4), (3, 4, 7, 6), (4, 5, 8, 7)] + face = link(bpy.data.objects.new("Face", mesh("FaceMesh", fv, ff))) + face.parent = rig + face.location = (0, -0.5, 0) + face.modifiers.new("Armature", "ARMATURE").object = rig + vg = face.vertex_groups.new(name="upper") + vg.add([6, 7, 8], 1.0, "REPLACE") + vg.add([3, 4, 5], 0.4, "REPLACE") + face.vertex_groups.new(name="spine").add(list(range(6)), 1.0, "REPLACE") + face.shape_key_add(name="Basis") + smile = face.shape_key_add(name="Smile") + for i, p in enumerate(smile.data): + p.co = (p.co[0], p.co[1] + 0.1 * i, p.co[2]) + smile.slider_min, smile.slider_max = -0.5, 2.0 + smile.value = 1.5 + blink = face.shape_key_add(name="Blink") + for i, p in enumerate(blink.data): + p.co = (p.co[0] + 0.05 * i, p.co[1] + 0.1 * i, p.co[2] - 0.02 * i) + blink.relative_key = smile + blink.mute = True + blink.vertex_group = "upper" + blink.value = 0.5 + puff = face.shape_key_add(name="Puff") + for i, p in enumerate(puff.data): + p.co = (p.co[0] * 1.2, p.co[1] - 0.05, p.co[2]) + puff.value = 0.3 + for f, v in ((1, 0.0), (10, 2.0), (20, -0.5)): + smile.value = v + smile.keyframe_insert("value", frame=f) + smile.value = 1.5 + sc.frame_set(1) + update() + save(os.path.join(out, "ctl_%s.blend" % TAG)) + + scene_file() rig_file() +ctl_file() if OLD: scene_file() save(os.path.join(out, "compat_%s.blend" % TAG), compat=True) diff --git a/tools/fixtures/_blend_ref.py b/tools/fixtures/_blend_ref.py index fb37763..f8b50be 100644 --- a/tools/fixtures/_blend_ref.py +++ b/tools/fixtures/_blend_ref.py @@ -2,7 +2,9 @@ # -- ): opens each file (old ones through Blender's versioning) and writes # /.json with the reference values (object world matrices, mesh counts / bounds / attribute sums / corner normals, # vertex groups, shape keys, materials, images, cameras, lights, bones, f-curve values, posed world matrices per frame), -# and exports a .glb of each file (Z up, no modifiers) whose JSON part is returned as "gltf". +# the rig data of each armature (bone head / tail / roll, collections, colours, custom shapes, locks, IK settings, +# constraints with every RNA property of their type) and the default theme's bone colour sets; posed matrices are +# taken with every constraint muted (the reader does not evaluate constraints). It exports a .glb of each file (Z up, no modifiers) whose JSON part is returned as "gltf". import bpy, sys, os, json, struct, math args = sys.argv[sys.argv.index("--") + 1:] @@ -62,6 +64,8 @@ def mesh_info(ob): if me.shape_keys: k = me.shape_keys info["shapes"] = [{"name": b.name, "value": r6(b.value), "relative": b.relative_key.name, + "slider_min": r6(b.slider_min), "slider_max": r6(b.slider_max), "mute": bool(b.mute), + "vertex_group": b.vertex_group, "co": [r6(p.co[c]) for p in b.data for c in range(3)]} for b in k.key_blocks] info["reference"] = k.reference_key.name info["modifiers"] = [m.type for m in ob.modifiers] @@ -122,6 +126,87 @@ def fcurves(act): return out +def rna_value(v): + if v is None: + return "" + if isinstance(v, bpy.types.ID): + return v.name + if isinstance(v, bpy.types.PoseBone): + return v.name + if isinstance(v, bool): + return v + if isinstance(v, (int, float)): + return r6(v) + if isinstance(v, str): + return v + if hasattr(v, "col"): # a Matrix + return [r6(x) for x in mat(v)] + try: + return [rna_value(x) for x in v] + except TypeError: + return None + + +SKIP = {"rna_type", "show_expanded", "active", "is_valid", "is_override_data_editable", "error_location", + "error_rotation", "targets", "space_object", "space_subtarget"} + + +def constraint_info(c): + out = {} + for prop in c.bl_rna.properties: + k = prop.identifier + if k in SKIP or prop.type == "COLLECTION": + continue + try: + v = rna_value(getattr(c, k)) + except Exception: # noqa: BLE001 + continue + if v is not None: + out[k] = v + return out + + +def color_info(c): + return {"palette": c.palette, "normal": [r6(x) for x in c.custom.normal], "select": [r6(x) for x in c.custom.select], + "active": [r6(x) for x in c.custom.active]} + + +def rig_info(ob): + arm = ob.data + bones = [] + for b in arm.bones: + _, roll = bpy.types.Bone.AxisRollFromMatrix(b.matrix_local.to_3x3()) + pb = ob.pose.bones[b.name] + pose = { + "custom_shape": pb.custom_shape.name if pb.custom_shape else "", + "custom_shape_transform": pb.custom_shape_transform.name if pb.custom_shape_transform else "", + "custom_shape_translation": [r6(x) for x in pb.custom_shape_translation], + "custom_shape_rotation_euler": [r6(x) for x in pb.custom_shape_rotation_euler], + "custom_shape_scale_xyz": [r6(x) for x in pb.custom_shape_scale_xyz], + "use_custom_shape_bone_size": pb.use_custom_shape_bone_size, + "lock_location": list(pb.lock_location), "lock_rotation": list(pb.lock_rotation), + "lock_rotation_w": pb.lock_rotation_w, "lock_rotations_4d": pb.lock_rotations_4d, + "lock_scale": list(pb.lock_scale), "rotation_mode": pb.rotation_mode, "ik_stretch": r6(pb.ik_stretch), + "color": color_info(pb.color), + "constraints": [constraint_info(c) for c in pb.constraints], + } + for a in "xyz": + for k in ("lock_ik_", "use_ik_limit_", "ik_min_", "ik_max_", "ik_stiffness_"): + pose[k + a] = rna_value(getattr(pb, k + a)) + bones.append({"name": b.name, "head_local": [r6(x) for x in b.head_local], "tail_local": [r6(x) for x in b.tail_local], + "roll": r6(roll), "deform": b.use_deform, "use_connect": b.use_connect, "show_wire": b.show_wire, + "collections": [c.name for c in b.collections], "color": color_info(b.color), "pose": pose}) + cols = [{"name": c.name, "visible": c.is_visible, "parent": c.parent.name if c.parent else ""} for c in arm.collections_all] + return {"bones": bones, "collections": cols} + + +def theme_colors(): + out = [] + for cs in bpy.context.preferences.themes[0].bone_color_sets: + out.append({"normal": [r6(x) for x in cs.normal], "select": [r6(x) for x in cs.select], "active": [r6(x) for x in cs.active]}) + return out + + FRAMES = [1 + 0.5 * i for i in range(43)] # 1 .. 22 (past the last key: extrapolation) @@ -140,11 +225,13 @@ def dump(path): obs = list(sc.objects) ref = {"version": list(bpy.data.version), "fps": sc.render.fps / sc.render.fps_base, "frameStart": sc.frame_start, "frameCurrent": sc.frame_current, "resolution": [sc.render.resolution_x, sc.render.resolution_y], - "objects": [], "materials": [], "images": [], "cameras": {}, "lights": {}, "armatures": {}, "actions": {}} + "objects": [], "materials": [], "images": [], "cameras": {}, "lights": {}, "armatures": {}, "actions": {}, + "rigs": {}, "themeBoneColors": theme_colors()} for ob in obs: o = {"name": ob.name, "type": ob.type, "parent": ob.parent.name if ob.parent else "", "parent_type": ob.parent_type, "parent_bone": ob.parent_bone, "world": [r6(x) for x in mat(ob.matrix_world)], - "collections": [c.name for c in ob.users_collection]} + "collections": [c.name for c in ob.users_collection], "hide_viewport": ob.hide_viewport, + "hide_render": ob.hide_render} if ob.type == "MESH": o["mesh"] = mesh_info(ob) if ob.animation_data and ob.animation_data.action: @@ -168,6 +255,9 @@ def dump(path): if ob.type == "ARMATURE": ref["armatures"][ob.name] = [{"name": b.name, "parent": b.parent.name if b.parent else "", "deform": b.use_deform, "local": [r6(x) for x in mat(b.matrix_local)], "length": r6(b.length)} for b in ob.data.bones] + ref["rigs"][ob.name] = rig_info(ob) + if ob.constraints: + o["constraints"] = [constraint_info(c) for c in ob.constraints] for m in bpy.data.materials: ref["materials"].append(material_info(m)) for im in bpy.data.images: @@ -175,6 +265,13 @@ def dump(path): ref["images"].append({"name": im.name, "filepath": im.filepath, "packed": im.packed_file.size if im.packed_file else 0, "source": im.source}) # posed world matrices: objects, pose bones (world), shape key values + for ob in obs: + for c in ob.constraints: + c.mute = True + if ob.type == "ARMATURE": + for pb in ob.pose.bones: + for c in pb.constraints: + c.mute = True poses = [] if ref["actions"]: for f in FRAMES: diff --git a/tools/fixtures/docs_blend.py b/tools/fixtures/docs_blend.py index e8f59f1..dc843a7 100644 --- a/tools/fixtures/docs_blend.py +++ b/tools/fixtures/docs_blend.py @@ -1,5 +1,6 @@ # Docs inputs for the .blend section of docs/guide/scene.md (tools/fixtures.py; tools/docs_check.py explains the -# inputs): a small rig saved zstd-compressed by the nixpkgs Blender (a two-bone armature, a skinned cylinder with a +# inputs): a small rig saved zstd-compressed by the nixpkgs Blender (a two-bone armature with a control bone in a bone +# collection, a damped track constraint and an edge-only custom shape outside the scene, a skinned cylinder with a # shape key, a bone action and a Principled material). # tests/gen/docs_blend.luau { files = { ["rig.blend"] = bytes }, vars = { riggedBlendBytes = "rig.blend" } } import importlib.util, os, shutil, subprocess, tempfile @@ -19,7 +20,16 @@ a = arm.edit_bones.new("lower") a.head, a.tail = (0, 0, 0), (0, 0, 1) b = arm.edit_bones.new("upper") b.head, b.tail, b.parent, b.use_connect = (0, 0, 1), (0, 0, 2), a, True +c = arm.edit_bones.new("ctrl") +c.head, c.tail, c.use_deform = (0.5, 0, 2), (0.5, 0, 2.5), False bpy.ops.object.mode_set(mode="OBJECT") +arm.collections.new("Controls").assign(arm.bones["ctrl"]) +ring = bpy.data.meshes.new("WGT_ring") +ring.from_pydata([(math.cos(k * math.pi / 4), math.sin(k * math.pi / 4), 0) for k in range(8)], [(k, (k + 1) % 8) for k in range(8)], []) +bpy.data.collections.new("Widgets").objects.link(bpy.data.objects.new("WGT_ring", ring)) +rig.pose.bones["ctrl"].custom_shape = bpy.data.objects["WGT_ring"] +track = rig.pose.bones["upper"].constraints.new("DAMPED_TRACK") +track.target, track.subtarget = rig, "ctrl" ring = [(math.cos(k * math.pi / 3) * 0.3, math.sin(k * math.pi / 3) * 0.3) for k in range(6)] verts = [(x, y, z) for z in (0, 1, 2) for x, y in ring] faces = [(r * 6 + k, r * 6 + (k + 1) % 6, (r + 1) * 6 + (k + 1) % 6, (r + 1) * 6 + k) for r in range(2) for k in range(6)] @@ -41,6 +51,7 @@ for i in range(18): up.add([i], (i // 6) * 0.5, "REPLACE") tube.shape_key_add(name="Basis") bulge = tube.shape_key_add(name="Bulge") +bulge.slider_max = 2.0 for p in bulge.data: p.co = (p.co[0] * 1.4, p.co[1] * 1.4, p.co[2]) pb = rig.pose.bones["upper"]