To manipulate object transformations with matrices, Blender includes the “mathutils” module in which the “Matrix” class is defined. With this class, you can simply create the necessary transformation matrices – translation, rotation, and scale.
The following command is used to create a scale matrix:
1 2 3 |
from mathutils import Matrix scale_matrix = Matrix.Scale(SCALE_VALUE, MATRIX_SIZE, AXIS) |
where:
SCALE_VALUE – the multiplier. How many times you need to change the scale with the resulting matrix,
MATRIX_SIZE – the size of the resulting matrix,
AXIS – a vector that determines on which axes the object will be scaled with the resulting matrix.
For example, to create a matrix of 2x scaling along the X-axis, you need to execute the following:
1 |
scale_matrix_x2 = Matrix.Scale(2, 4, (1.0, 0.0, 0.0)) |
Everything looks simple, but this method has one non-obvious feature.
If you need to create a scaling matrix along all three axes simultaneously, it seems logical to specify all the axes in the “AXES” parameter when calling this method :
1 |
scale_matrix_all2 = Matrix.Scale(2, 4, (1.0, 1.0, 1.0)) |
But this doesn’t work, the resulting matrix is not correct!
To create a scaling matrix along all three axes, you need to create separate scaling matrices for each of the axes and then multiply them:
1 2 3 4 |
scale_matrix_x2 = Matrix.Scale(2, 4, (1.0, 0.0, 0.0)) scale_matrix_y2 = Matrix.Scale(2, 4, (0.0, 1.0, 0.0)) scale_matrix_z2 = Matrix.Scale(2, 4, (0.0, 0.0, 1.0)) scale_matrix_xall = scale_matrix_x2 @ scale_matrix_y2 @ scale_matrix_z2 |
Now we get the correct matrix for scaling along three axes.
try
scale = Matrix.Diagonal( Vector( [dims[0], dims[1], dims[2], 1] ) )
The shortest way?