From a86e815dd86fb77d910bbc857106bedb4b691874 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Fri, 14 May 2021 19:46:19 +0300 Subject: 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 --- doc/python_api/examples/mathutils.Matrix.LocRotScale.py | 5 +++++ doc/python_api/examples/mathutils.Matrix.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 doc/python_api/examples/mathutils.Matrix.LocRotScale.py (limited to 'doc/python_api/examples') diff --git a/doc/python_api/examples/mathutils.Matrix.LocRotScale.py b/doc/python_api/examples/mathutils.Matrix.LocRotScale.py new file mode 100644 index 00000000000..016a5002e82 --- /dev/null +++ b/doc/python_api/examples/mathutils.Matrix.LocRotScale.py @@ -0,0 +1,5 @@ +# Compute local object transformation matrix: +if obj.rotation_mode == 'QUATERNION': + matrix = mathutils.Matrix.LocRotScale(obj.location, obj.rotation_quaternion, obj.scale) +else: + matrix = mathutils.Matrix.LocRotScale(obj.location, obj.rotation_euler, obj.scale) diff --git a/doc/python_api/examples/mathutils.Matrix.py b/doc/python_api/examples/mathutils.Matrix.py index 26c7ccba27c..84e09b97c15 100644 --- a/doc/python_api/examples/mathutils.Matrix.py +++ b/doc/python_api/examples/mathutils.Matrix.py @@ -14,10 +14,14 @@ mat_rot = mathutils.Matrix.Rotation(math.radians(45.0), 4, 'X') mat_out = mat_loc @ mat_rot @ mat_sca print(mat_out) -# extract components back out of the matrix +# extract components back out of the matrix as two vectors and a quaternion loc, rot, sca = mat_out.decompose() print(loc, rot, sca) +# recombine extracted components +mat_out2 = mathutils.Matrix.LocRotScale(loc, rot, sca) +print(mat_out2) + # it can also be useful to access components of a matrix directly mat = mathutils.Matrix() mat[0][0], mat[1][0], mat[2][0] = 0.0, 1.0, 2.0 -- cgit v1.2.3