model/blend: rig data (bone head / tail / roll, deform, collections and layers, colours, custom shapes and widgets, pose locks and IK settings, constraint stacks), shape key fields and targetNames, relative keys and vertex groups in morph targets, muted keys, loose edges as lines
This commit is contained in:
1 parent
03e7b21eca
commit
835ddde65b
7 files changed
+1241
-68
No files matched your search
@@ -7,6 +7,11 @@
|
||||
# keys, actions on bones / objects / shape keys (bezier, linear, constant), a bone parent
|
||||
# scene_<ver>_z.blend scene_<ver> saved compressed (gzip before 3.0, zstd after)
|
||||
# compat_<ver>.blend 2.7x only: the "legacy mesh format" save (MFace tessellated faces)
|
||||
# ctl_<ver>.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)
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
# <this> -- <json list of .blend paths> <out dir>): opens each file (old ones through Blender's versioning) and writes
|
||||
# <out dir>/<file>.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:
|
||||
|
||||
Reference in new issue
Block a user