1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
# SPDX-FileCopyrightText: 2022-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
"""
blender -b --factory-startup --python tests/python/bl_rigging_symmetrize.py -- --testdir /path/to/tests/data/animation
"""
import pathlib
import sys
import unittest
import bpy
def check_loc_rot_scale(self, bone, exp_bone):
# Check if posistions are the same
self.assertEqualVector(
bone.head, exp_bone.head, "Head position", bone.name)
self.assertEqualVector(
bone.tail, exp_bone.tail, "Tail position", bone.name)
# Scale
self.assertEqualVector(
bone.scale, exp_bone.scale, "Scale", bone.name)
# Rotation
rot_mode = exp_bone.rotation_mode
self.assertEqual(bone.rotation_mode, rot_mode, "Rotations mode does not match on bone %s" % (bone.name))
if rot_mode == 'QUATERNION':
self.assertEqualVector(
bone.rotation_quaternion, exp_bone.rotation_quaternion, "Quaternion rotation", bone.name)
elif rot_mode == 'AXIS_ANGLE':
self.assertEqualVector(
bone.axis_angle, exp_bone.axis_angle, "Axis Angle rotation", bone.name)
else:
# Euler rotation
self.assertEqualVector(
bone.rotation_euler, exp_bone.rotation_euler, "Euler rotation", bone.name)
def check_parent(self, bone, exp_bone):
self.assertEqual(type(bone.parent), type(exp_bone.parent),
"Mismatching types in pose.bones[%s].parent" % (bone.name))
self.assertTrue(bone.parent is None or bone.parent.name == exp_bone.parent.name,
"Bone parent does not match on bone %s" % (bone.name))
def check_bendy_bones(self, bone, exp_bone):
bone_variables = bone.bl_rna.properties.keys()
bendy_bone_variables = [
var for var in bone_variables if var.startswith("bbone_")]
for var in bendy_bone_variables:
value = getattr(bone, var)
exp_value = getattr(exp_bone, var)
self.assertEqual(type(value), type(exp_value),
"Mismatching types in pose.bones[%s].%s" % (bone.name, var))
if isinstance(value, str):
self.assertEqual(value, exp_value,
"Mismatching value in pose.bones[%s].%s" % (bone.name, var))
elif hasattr(value, "name"):
self.assertEqual(value.name, exp_value.name,
"Mismatching value in pose.bones[%s].%s" % (bone.name, var))
else:
self.assertAlmostEqual(value, exp_value,
"Mismatching value in pose.bones[%s].%s" % (bone.name, var))
def check_ik(self, bone, exp_bone):
bone_variables = bone.bl_rna.properties.keys()
prefixes = ("ik_", "lock_ik", "use_ik")
ik_bone_variables = (
var for var in bone_variables
if var.startswith(prefixes)
)
for var in ik_bone_variables:
value = getattr(bone, var)
exp_value = getattr(exp_bone, var)
self.assertAlmostEqual(value, exp_value,
"Mismatching value in pose.bones[%s].%s" % (bone.name, var))
def check_constraints(self, input_arm, expected_arm, bone, exp_bone):
const_len = len(bone.constraints)
expo_const_len = len(exp_bone.constraints)
self.assertEqual(const_len, expo_const_len,
"Constraints mismatch on bone %s" % (bone.name))
for exp_constraint in exp_bone.constraints:
const_name = exp_constraint.name
# Make sure that the constraint exists
self.assertTrue(const_name in bone.constraints,
"Bone %s is expected to contain constraint %s, but it does not." % (
bone.name, const_name))
constraint = bone.constraints[const_name]
const_variables = constraint.bl_rna.properties.keys()
for var in const_variables:
if var == "is_override_data":
# This variable is not used for local (non linked) data.
# For local object it is not initialized, so don't check this value.
continue
value = getattr(constraint, var)
exp_value = getattr(exp_constraint, var)
self.assertEqual(type(value), type(exp_value),
"Mismatching constraint value types in pose.bones[%s].constraints[%s].%s" % (
bone.name, const_name, var))
if isinstance(value, bpy.types.bpy_prop_collection):
# Don't compare collection properties.
continue
if isinstance(value, str):
self.assertEqual(value, exp_value,
"Mismatching constraint value in pose.bones[%s].constraints[%s].%s" % (
bone.name, const_name, var))
elif hasattr(value, "name"):
# Some constraints targets the armature itself, so the armature name should mismatch.
if value.name == input_arm.name and exp_value.name == expected_arm.name:
continue
self.assertEqual(value.name, exp_value.name,
"Mismatching constraint value in pose.bones[%s].constraints[%s].%s" % (
bone.name, const_name, var))
elif isinstance(value, bool):
self.assertEqual(value, exp_value,
"Mismatching constraint boolean in pose.bones[%s].constraints[%s].%s" % (
bone.name, const_name, var))
elif isinstance(value, float):
msg = "Mismatching constraint value in pose.bones[%s].constraints[%s].%s" % (
bone.name, const_name, var)
self.assertAlmostEqual(value, exp_value, places=6, msg=msg)
elif isinstance(value, int):
msg = "Mismatching constraint value in pose.bones[%s].constraints[%s].%s" % (
bone.name, const_name, var)
self.assertEqual(value, exp_value, msg=msg)
elif value is None:
# Since above the types were compared already, if value is none, so is exp_value.
pass
else:
self.fail(f"unexpected value type: {value!r} is of type {type(value)}")
class AbstractAnimationTest:
@classmethod
def setUpClass(cls):
cls.testdir = args.testdir
def setUp(self):
self.assertTrue(self.testdir.exists(),
'Test dir %s should exist' % self.testdir)
class ArmatureSymmetrizeTest(AbstractAnimationTest, unittest.TestCase):
def test_symmetrize_operator(self):
"""Test that the symmetrize operator is working correctly."""
bpy.ops.wm.open_mainfile(filepath=str(
self.testdir / "symm_test.blend"))
# #81541 (D9214)
arm = bpy.data.objects['transform_const_rig']
expected_arm = bpy.data.objects['expected_transform_const_rig']
self.assertEqualSymmetrize(arm, expected_arm)
# #66751 (D6009)
arm = bpy.data.objects['dragon_rig']
expected_arm = bpy.data.objects['expected_dragon_rig']
self.assertEqualSymmetrize(arm, expected_arm)
def assertEqualSymmetrize(self, input_arm, expected_arm):
# Symmetrize our input armature
bpy.context.view_layer.objects.active = input_arm
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.armature.select_all(action='SELECT')
bpy.ops.armature.symmetrize()
bpy.ops.object.mode_set(mode='OBJECT')
# Make sure that the bone count is the same
bone_len = len(input_arm.pose.bones)
expected_bone_len = len(expected_arm.pose.bones)
self.assertEqual(bone_len, expected_bone_len,
"Expected bone count to match")
for exp_bone in expected_arm.pose.bones:
bone_name = exp_bone.name
# Make sure that the bone exists
self.assertTrue(bone_name in input_arm.pose.bones,
"Armature is expected to contain bone %s, but it does not." % (bone_name))
bone = input_arm.pose.bones[bone_name]
# Loc Rot Scale
check_loc_rot_scale(self, bone, exp_bone)
# Parent settings
check_parent(self, bone, exp_bone)
# Bendy Bones
check_bendy_bones(self, bone, exp_bone)
# IK
check_ik(self, bone, exp_bone)
# Constraints
check_constraints(self, input_arm, expected_arm, bone, exp_bone)
def assertEqualVector(self, vec1, vec2, check_str, bone_name) -> None:
for idx, value in enumerate(vec1):
self.assertAlmostEqual(
value, vec2[idx], 3, "%s does not match with expected value on bone %s" % (check_str, bone_name))
def main():
global args
import argparse
if '--' in sys.argv:
argv = [sys.argv[0]] + sys.argv[sys.argv.index('--') + 1:]
else:
argv = sys.argv
parser = argparse.ArgumentParser()
parser.add_argument('--testdir', required=True, type=pathlib.Path)
args, remaining = parser.parse_known_args(argv)
unittest.main(argv=remaining)
if __name__ == "__main__":
main()
|