Page MenuHome

rotate_test_basic_280b.py

Authored By
nBurn (nBurn)
Apr 8 2021, 3:37 AM
Size
4 KB
Subscribers
None

rotate_test_basic_280b.py

'''
To run the tests with this code from the text editor, it is
recommended you also have a region of Blender in object mode with
only a "default" cube selected (a cube object at position (0, 0, 0)
with no transformations done to it).
'''
from copy import deepcopy
import bpy
from mathutils import Matrix, Vector
test_data = [
{ # [0] around arbitrary axis
"matrix_world_beg": (
( 0.9330, -0.3536, -0.0670, 1.7804),
( 0.3536, 0.8660, 0.3536, 2.1589),
(-0.0670, -0.3536, 0.9330, 0.7804),
( 0.0000, 0.0000, 0.0000, 1.0000)
),
"piv_co": ( 2.2928, 3.7321, 1.2928),
"piv_norm": ( 0.0670, -0.3536, -0.9330),
"ang_diff_rad": -0.6981317007977318,
"matrix_world_exp": (
( 0.4874, -0.8706, -0.0670, 2.7430),
( 0.8275, 0.4361, 0.3536, 2.1149),
(-0.2786, -0.2278, 0.9330, 0.8662),
( 0.0000, 0.0000, 0.0000, 1.0000)
)
}
]
def flt_lists_alm_eq(ls_a, ls_b, tol=0.001):
for i in range(len(ls_a)):
if not (ls_a[i] > (ls_b[i] - tol) and ls_a[i] < (ls_b[i] + tol)):
return False
return True
def mat_world_match_existing(exp_matrix):
m_w = bpy.context.object.matrix_world.copy()
for i, row in enumerate(m_w):
if not flt_lists_alm_eq(row, exp_matrix[i]):
return False
return True
def create_z_orient(rot_vec):
x_dir_p = Vector(( 1.0, 0.0, 0.0))
y_dir_p = Vector(( 0.0, 1.0, 0.0))
z_dir_p = Vector(( 0.0, 0.0, 1.0))
zerovec = Vector(( 0.0, 0.0, 0.0))
if flt_lists_alm_eq(rot_vec, zerovec) or \
flt_lists_alm_eq(rot_vec, z_dir_p):
return Matrix((x_dir_p, y_dir_p, z_dir_p)) # 3x3 identity
new_z = rot_vec.copy() # assume rot_vec already normalized
new_y = new_z.cross(z_dir_p)
if flt_lists_alm_eq(new_y, zerovec):
new_y = y_dir_p
new_x = new_y.cross(new_z)
new_x.normalize()
new_y.normalize()
return Matrix(((new_x.x, new_y.x, new_z.x),
(new_x.y, new_y.y, new_z.y),
(new_x.z, new_y.z, new_z.z)))
def do_rotate(piv_co, piv_norm, ang_diff_r):
# back up settings before changing them
piv_back = deepcopy(bpy.context.tool_settings.transform_pivot_point)
curs_back = bpy.context.scene.cursor.location.copy()
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
bpy.context.scene.cursor.location = Vector(piv_co)
o_mat = create_z_orient(Vector(piv_norm))
bpy.ops.transform.rotate(
value=ang_diff_r,
orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(False, False, False),
use_accurate=True
)
# restore settings back to their pre "do_rotate" state
bpy.context.scene.cursor.location = curs_back.copy()
bpy.context.tool_settings.transform_pivot_point = deepcopy(piv_back)
def load_data_from_file(fpath):
with open(fpath, 'r') as f:
return eval(f.read())
def run_test(testing_data):
test_passed = 0
test_failed = 0
obj = bpy.context.object
orig_m_w = obj.matrix_world.copy()
sep = "=" * 20
start_str = "\n" + sep + "\nrunning test %d:"
for i in range(len(testing_data)):
print(start_str % i)
t_dat = testing_data[i]
obj.matrix_world = Matrix(t_dat["matrix_world_beg"])
do_rotate(t_dat["piv_co"], t_dat["piv_norm"], t_dat["ang_diff_rad"])
mat_match = mat_world_match_existing(t_dat["matrix_world_exp"])
print("\nmatrix_world match expected?", mat_match)
if not mat_match:
test_failed += 1
print("found matrix_world:")
print(obj.matrix_world)
else:
test_passed += 1
print()
obj.matrix_world = orig_m_w
print("\n" + sep)
print("Results:")
print(" tests passed:", test_passed)
print(" tests failed:", test_failed)
print(sep + "\n")
def run_external_tests():
fp = r"C:\Users\SomeUser\Documents\rotate_unit_tests.py"
run_test(load_data_from_file(fp))
run_test(test_data)
#run_external_tests()
#__import__('code').interact(local=dict(globals(), **locals()))

File Metadata

Mime Type
text/x-python
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
d8/31/30d397a82cdc61eb2e0d78667117

Event Timeline