This module provides access to matrices, eulers, quaternions and vectors.
import mathutils
from math import radians
vec = mathutils.Vector((1.0, 2.0, 3.0))
mat_rot = mathutils.Matrix.Rotation(radians(90.0), 4, 'X')
mat_trans = mathutils.Matrix.Translation(vec)
mat = mat_trans * mat_rot
mat.invert()
mat3 = mat.to_3x3()
quat1 = mat.to_quaternion()
quat2 = mat3.to_quaternion()
quat_diff = quat1.rotation_difference(quat2)
print(quat_diff.angle)
This object gives access to Colors in Blender.
import mathutils
# color values are represented as RGB values from 0 - 1, this is blue
col = mathutils.Color((0.0, 0.0, 1.0))
# as well as r/g/b attribute access you can adjust them by h/s/v
col.s *= 0.5
# you can access its components by attribute or index
print("Color R:", col.r)
print("Color G:", col[1])
print("Color B:", col[-1])
print("Color HSV: %.2f, %.2f, %.2f", col[:])
# components of an existing color can be set
col[:] = 0.0, 0.5, 1.0
# components of an existing color can use slice notation to get a tuple
print("Values: %f, %f, %f" % col[:])
# colors can be added and subtracted
col += mathutils.Color((0.25, 0.0, 0.0))
# Color can be multiplied, in this example color is scaled to 0-255
# can printed as integers
print("Color: %d, %d, %d" % (col * 255.0)[:])
# This example prints the color as hexidecimal
print("Hexidecimal: %.2x%.2x%.2x" % (col * 255.0)[:])
Returns a copy of this color.
Returns: | A copy of the color. |
---|---|
Return type: | Color |
Note
use this to get a copy of a wrapped color with no reference to the original data.
Blue color channel.
Type: | float |
---|
Green color channel.
Type: | float |
---|
HSV Hue component in [0, 1].
Type: | float |
---|
HSV Values in [0, 1].
Type: | float triplet |
---|
True when this object wraps external data (read-only).
Type: | boolean |
---|
The item this is wrapping or None (read-only).
Red color channel.
Type: | float |
---|
HSV Saturation component in [0, 1].
Type: | float |
---|
HSV Value component in [0, 1].
Type: | float |
---|
This object gives access to Eulers in Blender.
import mathutils
import math
# create a new euler with default axis rotation order
eul = mathutils.Euler((0.0, math.radians(45.0), 0.0), 'XYZ')
# rotate the euler
eul.rotate_axis('Z', math.radians(10.0))
# you can access its components by attribute or index
print("Euler X", eul.x)
print("Euler Y", eul[1])
print("Euler Z", eul[-1])
# components of an existing euler can be set
eul[:] = 1.0, 2.0, 3.0
# components of an existing euler can use slice notation to get a tuple
print("Values: %f, %f, %f" % eul[:])
# the order can be set at any time too
eul.order = 'ZYX'
# eulers can be used to rotate vectors
vec = mathutils.Vector((0.0, 0.0, 1.0))
vec.rotate(eul)
# often its useful to convert the euler into a matrix so it can be used as
# transformations with more flexibility
mat_rot = eul.to_matrix()
mat_loc = mathutils.Matrix.Translation((2.0, 3.0, 4.0))
mat = mat_loc * mat_rot.to_4x4()
Returns a copy of this euler.
Returns: | A copy of the euler. |
---|---|
Return type: | Euler |
Note
use this to get a copy of a wrapped euler with no reference to the original data.
Make this euler compatible with another, so interpolating between them works as intended.
Note
the rotation order is not taken into account for this function.
Rotates the euler a by another mathutils value.
Parameters: | other (Euler, Quaternion or Matrix) – rotation component of mathutils value |
---|
Rotates the euler a certain amount and returning a unique euler rotation (no 720 degree pitches).
Parameters: |
|
---|
Return a matrix representation of the euler.
Returns: | A 3x3 roation matrix representation of the euler. |
---|---|
Return type: | Matrix |
Return a quaternion representation of the euler.
Returns: | Quaternion representation of the euler. |
---|---|
Return type: | Quaternion |
Set all values to zero.
True when this object wraps external data (read-only).
Type: | boolean |
---|
Euler rotation order.
Type: | string in [‘XYZ’, ‘XZY’, ‘YXZ’, ‘YZX’, ‘ZXY’, ‘ZYX’] |
---|
The item this is wrapping or None (read-only).
Euler axis angle in radians.
Type: | float |
---|
Euler axis angle in radians.
Type: | float |
---|
Euler axis angle in radians.
Type: | float |
---|
This object gives access to Matrices in Blender.
import mathutils
import math
# create a location matrix
mat_loc = mathutils.Matrix.Translation((2.0, 3.0, 4.0))
# create an identitiy matrix
mat_sca = mathutils.Matrix.Scale(0.5, 4, (0.0, 0.0, 1.0))
# create a rotation matrix
mat_rot = mathutils.Matrix.Rotation(math.radians(45.0), 4, 'X')
# combine transformations
mat_out = mat_loc * mat_rot * mat_sca
print(mat_out)
# extract components back out of the matrix
loc, rot, sca = mat_out.decompose()
print(loc, rot, sca)
# 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
mat[0][0:3] = 0.0, 1.0, 2.0
# each item in a matrix is a vector so vector utility functions can be used
mat[0].xyz = 0.0, 1.0, 2.0
Create an identity matrix.
Parameters: | size (int) – The size of the identity matrix to construct [2, 4]. |
---|---|
Returns: | A new identity matrix. |
Return type: | Matrix |
Create a matrix to represent an orthographic projection.
Parameters: |
|
---|---|
Returns: | A new projection matrix. |
Return type: |
Create a matrix representing a rotation.
Parameters: |
|
---|---|
Returns: | A new rotation matrix. |
Return type: |
Create a matrix representing a scaling.
Parameters: |
|
---|---|
Returns: | A new scale matrix. |
Return type: |
Create a matrix to represent an shear transformation.
Parameters: |
|
---|---|
Returns: | A new shear matrix. |
Return type: |
Create a matrix representing a translation.
Parameters: | vector (Vector) – The translation vector. |
---|---|
Returns: | An identity matrix with a translation. |
Return type: | Matrix |
Set the matrix to its adjugate.
Note
When the matrix cant be adjugated a ValueError exception is raised.
See also
Return an adjugated copy of the matrix.
Returns: | the adjugated matrix. |
---|---|
Return type: | Matrix |
Note
When the matrix cant be adjugated a ValueError exception is raised.
Return the location, rotation and scale components of this matrix.
Returns: | loc, rot, scale triple. |
---|---|
Return type: | (Vector, Quaternion, Vector) |
Return the determinant of a matrix.
Returns: | Return a the determinant of a matrix. |
---|---|
Return type: | float |
See also
Set the matrix to the identity matrix.
Note
An object with zero location and rotation, a scale of one, will have an identity matrix.
See also
Set the matrix to its inverse.
Parameters: | fallback (Matrix) – Set the matrix to this value when the inverse can’t be calculated (instead of raising a ValueError exception). |
---|
See also
Set the matrix to its inverse, will never error. If degenerated (e.g. zero scale on an axis), add some epsilon to its diagonal, to get an invertible one. If tweaked matrix is still degenerated, set to the identity matrix instead.
See also
Return an inverted copy of the matrix.
Parameters: | fallback (any) – return this value when the inverse can’t be calculated (instead of raising a ValueError exception). |
---|---|
Returns: | the inverted matrix or fallback when given. |
Return type: | Matrix |
Return an inverted copy of the matrix, will never error. If degenerated (e.g. zero scale on an axis), add some epsilon to its diagonal, to get an invertible one. If tweaked matrix is still degenerated, return the identity matrix instead.
Returns: | the inverted matrix. |
---|---|
Return type: | Matrix |
Returns the interpolation of two matrices.
Parameters: |
|
---|---|
Returns: | The interpolated matrix. |
Return type: |
Normalize each of the matrix columns.
Return a column normalized matrix
Returns: | a column normalized matrix |
---|---|
Return type: | Matrix |
Resize the matrix to 4x4.
Rotates the matrix a by another mathutils value.
Parameters: | other (Euler, Quaternion or Matrix) – rotation component of mathutils value |
---|
Note
If any of the columns are not unit length this may not have desired results.
Return an Euler representation of the rotation matrix (3x3 or 4x4 matrix only).
Parameters: |
|
---|---|
Returns: | Euler representation of the matrix. |
Return type: |
Return a quaternion representation of the rotation matrix.
Returns: | Quaternion representation of the rotation matrix. |
---|---|
Return type: | Quaternion |
Return a the scale part of a 3x3 or 4x4 matrix.
Returns: | Return a the scale of a matrix. |
---|---|
Return type: | Vector |
Note
This method does not return negative a scale on any axis because it is not possible to obtain this data from the matrix alone.
Return a the translation part of a 4 row matrix.
Returns: | Return a the translation of a matrix. |
---|---|
Return type: | Vector |
Set the matrix to its transpose.
See also
Access the matix by colums, 3x3 and 4x4 only, (read-only).
Type: | Matrix Access |
---|
True if this matrix results in a negative scale, 3x3 and 4x4 only, (read-only).
Type: | bool |
---|
True if this matrix is orthogonal, 3x3 and 4x4 only, (read-only).
Type: | bool |
---|
True if this matrix has got orthogonal axis vectors, 3x3 and 4x4 only, (read-only).
Type: | bool |
---|
True when this object wraps external data (read-only).
Type: | boolean |
---|
The average scale applied to each axis (read-only).
Type: | float |
---|
The item this is wrapping or None (read-only).
Access the matix by rows (default), (read-only).
Type: | Matrix Access |
---|
The translation component of the matrix.
Type: | Vector |
---|
This object gives access to Quaternions in Blender.
import mathutils
import math
# a new rotation 90 degrees about the Y axis
quat_a = mathutils.Quaternion((0.7071068, 0.0, 0.7071068, 0.0))
# passing values to Quaternion's directly can be confusing so axis, angle
# is supported for initializing too
quat_b = mathutils.Quaternion((0.0, 1.0, 0.0), math.radians(90.0))
print("Check quaternions match", quat_a == quat_b)
# like matrices, quaternions can be multiplied to accumulate rotational values
quat_a = mathutils.Quaternion((0.0, 1.0, 0.0), math.radians(90.0))
quat_b = mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(45.0))
quat_out = quat_a * quat_b
# print the quat, euler degrees for mear mortals and (axis, angle)
print("Final Rotation:")
print(quat_out)
print("%.2f, %.2f, %.2f" % tuple(math.degrees(a) for a in quat_out.to_euler()))
print("(%.2f, %.2f, %.2f), %.2f" % (quat_out.axis[:] +
(math.degrees(quat_out.angle), )))
Set the quaternion to its conjugate (negate x, y, z).
Return a new conjugated quaternion.
Returns: | a new quaternion. |
---|---|
Return type: | Quaternion |
Returns a copy of this quaternion.
Returns: | A copy of the quaternion. |
---|---|
Return type: | Quaternion |
Note
use this to get a copy of a wrapped quaternion with no reference to the original data.
Return the cross product of this quaternion and another.
Parameters: | other (Quaternion) – The other quaternion to perform the cross product with. |
---|---|
Returns: | The cross product. |
Return type: | Quaternion |
Return the dot product of this quaternion and another.
Parameters: | other (Quaternion) – The other quaternion to perform the dot product with. |
---|---|
Returns: | The dot product. |
Return type: | Quaternion |
Set the quaternion to an identity quaternion.
Returns: | an instance of itself. |
---|---|
Return type: | Quaternion |
Set the quaternion to its inverse.
Return a new, inverted quaternion.
Returns: | the inverted value. |
---|---|
Return type: | Quaternion |
Set the quaternion to its negative.
Returns: | an instance of itself. |
---|---|
Return type: | Quaternion |
Normalize the quaternion.
Return a new normalized quaternion.
Returns: | a normalized copy. |
---|---|
Return type: | Quaternion |
Rotates the quaternion a by another mathutils value.
Parameters: | other (Euler, Quaternion or Matrix) – rotation component of mathutils value |
---|
Returns a quaternion representing the rotational difference.
Parameters: | other (Quaternion) – second quaternion. |
---|---|
Returns: | the rotational difference between the two quat rotations. |
Return type: | Quaternion |
Returns the interpolation of two quaternions.
Parameters: |
|
---|---|
Returns: | The interpolated rotation. |
Return type: |
Return the axis, angle representation of the quaternion.
Returns: | axis, angle. |
---|---|
Return type: | (Vector, float) pair |
Return Euler representation of the quaternion.
Parameters: |
|
---|---|
Returns: | Euler representation of the quaternion. |
Return type: |
Return a matrix representation of the quaternion.
Returns: | A 3x3 rotation matrix representation of the quaternion. |
---|---|
Return type: | Matrix |
Angle of the quaternion.
Type: | float |
---|
True when this object wraps external data (read-only).
Type: | boolean |
---|
Size of the quaternion (read-only).
Type: | float |
---|
The item this is wrapping or None (read-only).
Quaternion axis value.
Type: | float |
---|
Quaternion axis value.
Type: | float |
---|
Quaternion axis value.
Type: | float |
---|
Quaternion axis value.
Type: | float |
---|
This object gives access to Vectors in Blender.
import mathutils
# zero length vector
vec = mathutils.Vector((0.0, 0.0, 1.0))
# unit length vector
vec_a = vec.normalized()
vec_b = mathutils.Vector((0.0, 1.0, 2.0))
vec2d = mathutils.Vector((1.0, 2.0))
vec3d = mathutils.Vector((1.0, 0.0, 0.0))
vec4d = vec_a.to_4d()
# other mathutuls types
quat = mathutils.Quaternion()
matrix = mathutils.Matrix()
# Comparison operators can be done on Vector classes:
# greater and less then test vector length.
vec_a > vec_b
vec_a >= vec_b
vec_a < vec_b
vec_a <= vec_b
# ==, != test vector values e.g. 1,2,3 != 3,2,1 even if they are the same length
vec_a == vec_b
vec_a != vec_b
# Math can be performed on Vector classes
vec_a + vec_b
vec_a - vec_b
vec_a * vec_b
vec_a * 10.0
matrix * vec_a
quat * vec_a
vec_a * vec_b
-vec_a
# You can access a vector object like a sequence
x = vec_a[0]
len(vec)
vec_a[:] = vec_b
vec_a[:] = 1.0, 2.0, 3.0
vec2d[:] = vec3d[:2]
# Vectors support 'swizzle' operations
# See http://en.wikipedia.org/wiki/Swizzling_(computer_graphics)
vec.xyz = vec.zyx
vec.xy = vec4d.zw
vec.xyz = vec4d.wzz
vec4d.wxyz = vec.yxyx
Create a vector of length size with all values set to fill.
Parameters: |
|
---|
Create a vector of the specified size which is filled with linearly spaced values between start and stop values.
Parameters: |
|
---|
Create a filled with a range of values.
Parameters: |
|
---|
Create a vector by repeating the values in vector until the required size is reached.
Parameters: |
|
---|
Return the angle between two vectors.
Parameters: |
|
---|---|
Returns: | angle in radians or fallback when given |
Return type: | float |
Note
Zero length vectors raise an ValueError.
Return the signed angle between two 2D vectors (clockwise is positive).
Parameters: |
|
---|---|
Returns: | angle in radians or fallback when given |
Return type: | float |
Note
Zero length vectors raise an ValueError.
Returns a copy of this vector.
Returns: | A copy of the vector. |
---|---|
Return type: | Vector |
Note
use this to get a copy of a wrapped vector with no reference to the original data.
Return the cross product of this vector and another.
Parameters: | other (Vector) – The other vector to perform the cross product with. |
---|---|
Returns: | The cross product. |
Return type: | Vector or float when 2D vectors are used |
Note
both vectors must be 2D or 3D
Return the dot product of this vector and another.
Parameters: | other (Vector) – The other vector to perform the dot product with. |
---|---|
Returns: | The dot product. |
Return type: | Vector |
Returns the interpolation of two vectors.
Parameters: |
|
---|---|
Returns: | The interpolated vector. |
Return type: |
Set all values to their negative.
Normalize the vector, making the length of the vector always 1.0.
Warning
Normalizing a vector where all values are zero has no effect.
Note
Normalize works for vectors of all sizes, however 4D Vectors w axis is left untouched.
Return a new, normalized vector.
Returns: | a normalized copy of the vector |
---|---|
Return type: | Vector |
Return a perpendicular vector.
Returns: | a new vector 90 degrees from this vector. |
---|---|
Return type: | Vector |
Note
the axis is undefined, only use when any orthogonal vector is acceptable.
Return the projection of this vector onto the other.
Parameters: | other (Vector) – second vector. |
---|---|
Returns: | the parallel projection vector |
Return type: | Vector |
Return the reflection vector from the mirror argument.
Parameters: | mirror (Vector) – This vector could be a normal from the reflecting surface. |
---|---|
Returns: | The reflected vector matching the size of this vector. |
Return type: | Vector |
Resize the vector to have size number of elements.
Resize the vector to 2D (x, y).
Resize the vector to 3D (x, y, z).
Resize the vector to 4D (x, y, z, w).
Return a resized copy of the vector with size number of elements.
Returns: | a new vector |
---|---|
Return type: | Vector |
Rotate the vector by a rotation value.
Parameters: | other (Euler, Quaternion or Matrix) – rotation component of mathutils value |
---|
Returns a quaternion representing the rotational difference between this vector and another.
Parameters: | other (Vector) – second vector. |
---|---|
Returns: | the rotational difference between the two vectors. |
Return type: | Quaternion |
Note
2D vectors raise an AttributeError.
Returns the interpolation of two non-zero vectors (spherical coordinates).
Parameters: |
|
---|---|
Returns: | The interpolated vector. |
Return type: |
Return a quaternion rotation from the vector and the track and up axis.
Parameters: |
|
---|---|
Returns: | rotation from the vector and the track and up axis. |
Return type: |
Return this vector as a tuple with.
Parameters: | precision (int) – The number to round the value to in [-1, 21]. |
---|---|
Returns: | the values of the vector rounded by precision |
Return type: | tuple |
Set all values to zero.
True when this object wraps external data (read-only).
Type: | boolean |
---|
Vector Length.
Type: | float |
---|
Vector length squared (v.dot(v)).
Type: | float |
---|
Vector Length.
Type: | float |
---|
The item this is wrapping or None (read-only).
Vector W axis (4D Vectors only).
Type: | float |
---|
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Vector X axis.
Type: | float |
---|
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Vector Y axis.
Type: | float |
---|
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Vector Z axis (3D Vectors only).
Type: | float |
---|
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented