Mathutils: add a Matrix.LocRotScale constructor for combining channels.

Combining location, rotation and scale channels into a matrix is
a standard task, so while it is easily accomplished by constructing
and multiplying 3 matrices, having a standard utility allows for
more clear code.

The new constructor builds a 4x4 matrix from separate location,
rotation and scale values. Rotation can be represented as a 3x3
Matrix, Quaternion or Euler value, while the other two inputs
are vectors. Unneeded inputs can be replaced with None.

Differential Revision: https://developer.blender.org/D11264
This commit is contained in:
2021-05-14 19:46:19 +03:00
parent f09606cc68
commit a86e815dd8
4 changed files with 134 additions and 2 deletions

View File

@@ -2,7 +2,7 @@
# ./blender.bin --background -noaudio --python tests/python/bl_pyapi_mathutils.py -- --verbose
import unittest
from mathutils import Matrix, Vector, Quaternion
from mathutils import Matrix, Vector, Quaternion, Euler
from mathutils import kdtree, geometry
import math
@@ -233,6 +233,27 @@ class MatrixTesting(unittest.TestCase):
self.assertEqual(mat @ mat, prod_mat)
def test_loc_rot_scale(self):
euler = Euler((math.radians(90), 0, math.radians(90)), 'ZYX')
expected = Matrix(((0, -5, 0, 1),
(0, 0, -6, 2),
(4, 0, 0, 3),
(0, 0, 0, 1)))
result = Matrix.LocRotScale((1, 2, 3), euler, (4, 5, 6))
self.assertAlmostEqualMatrix(result, expected, 4)
result = Matrix.LocRotScale((1, 2, 3), euler.to_quaternion(), (4, 5, 6))
self.assertAlmostEqualMatrix(result, expected, 4)
result = Matrix.LocRotScale((1, 2, 3), euler.to_matrix(), (4, 5, 6))
self.assertAlmostEqualMatrix(result, expected, 4)
def assertAlmostEqualMatrix(self, first, second, size, *, places=6, msg=None, delta=None):
for i in range(size):
for j in range(size):
self.assertAlmostEqual(first[i][j], second[i][j], places=places, msg=msg, delta=delta)
class VectorTesting(unittest.TestCase):