Page MenuHome

rotate_test_multi_280.py

Authored By
nBurn (nBurn)
Apr 12 2021, 8:09 AM
Size
7 KB
Subscribers
None

rotate_test_multi_280.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_0(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
#orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
#constraint_axis=(False, False, True),
use_accurate=True)
def do_rotate_1(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
#constraint_axis=(False, False, True),
use_accurate=True)
def do_rotate_2(piv_co, piv_norm, ang_diff_r, o_mat):
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)
def do_rotate_3(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(True, True, True),
use_accurate=True)
def do_rotate_4(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(True, True, False),
use_accurate=True)
def do_rotate_5(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(False, False, True),
use_accurate=True)
def do_rotate_6(piv_co, piv_norm, ang_diff_r, o_mat):
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)
def do_rotate_7(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
#orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(True, True, True),
use_accurate=True)
def do_rotate_8(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
#orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(True, True, False),
use_accurate=True)
def do_rotate_9(piv_co, piv_norm, ang_diff_r, o_mat):
bpy.ops.transform.rotate(
value=ang_diff_r,
#orient_axis='Z',
orient_matrix=o_mat,
center_override=Vector(piv_co),
constraint_axis=(False, False, True),
use_accurate=True)
def do_rotate_tests(beg_m_w, end_m_w, 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 = piv_co
o_mat = create_z_orient(piv_norm)
#
#print("results: ", end="")
funcs = (do_rotate_0, do_rotate_1, do_rotate_2, do_rotate_3, do_rotate_4,
do_rotate_5, do_rotate_6, do_rotate_7, do_rotate_8, do_rotate_9)
for i, f in enumerate(funcs):
bpy.context.object.matrix_world = beg_m_w.copy()
f(piv_co, piv_norm, ang_diff_r, o_mat)
mat_match = mat_world_match_existing(end_m_w)
res = 'P' if mat_match else 'F'
print(i, res, end=" ")
print()
#
# 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):
obj = bpy.context.object
orig_m_w = obj.matrix_world.copy()
print()
for i in range(len(testing_data)):
print("running test " + str(i) + ": ", end="")
t_dat = testing_data[i]
mat_beg = Matrix(t_dat["matrix_world_beg"])
mat_end = Matrix(t_dat["matrix_world_exp"])
piv_co = Vector(t_dat["piv_co"])
piv_norm = Vector(t_dat["piv_norm"])
ang_diff_rad = t_dat["ang_diff_rad"]
do_rotate_tests(mat_beg, mat_end, piv_co, piv_norm, ang_diff_rad)
obj.matrix_world = orig_m_w.copy()
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
08/a9/db56f5ca462fb5a3fc003911ed83

Event Timeline