From BlenderWiki
[edit] General Math
Python itself has a math module that contains common math functions like sin, cos, and sqrt among others. If you need a basic math operator, it is probably in there. You can read about the math module here.
[edit] A quick discussion of Vector math
The BPy API contains a Mathutils module for doing vector and matrix math. An in-depth discussion of vector and matrix math is beyond our scope here. One quick on-line intro is here.
For our purposes now, a Vector is a set of 3 numbers that represents a point or direction in 3D space. Treating the 3 coordinates as a set makes them easier to manipulate. Vectors can be added, subtracted and multiplied using their own set of rules.
A matrix is a set of numbers ( often a 4x4 array ) that can describe the location, position, and scale of an object. Basic math operators can be applied to matrices using their own set of rules.
Both Vector and Matrix are types in the Mathutils module
[edit] How to create a vector
The code snippet below creates a new vector and initializes it to zeros.
# import Mathutils and create an alias
from Blender import Mathutils as Math
# create new vector 'v'
v = Math.Vector( 0.0, 0.0, 0.0 )
print v
The method Math.Vector() is called a constructor. You can initialize the vector with 3 numbers, a tuple or a list. If you do not provide any arguments, you get a vector with 0.0, 0.0, 0.0. The following snippet is an example of each argument type
import Blender as B
from Blender import Mathutils as Math
print Math.Vector( 0.0, 1.0, 2.0 ) # 3 floats
print Math.Vector( ( 0.0, 1.0, 2.0 ) ) # a tuple
print Math.Vector( [ 0.0, 1.0, 2.0 ] ) # a list
print Math.Vector() # no arguments
[edit] A vector example
Remember our program to move a cube? Here is how it looks using Vectors
import Blender as B
from Blender import Mathutils as Math
ob = B.Object.Get('Cube')
# create vector from loc
loc_vec = Math.Vector(ob.loc)
# create update vector
delta_pos = Math.Vector( 0.5, 0.0, 0.0 )
# add delta_pos to loc_vec
loc_vec += delta_pos
# update object location to new value
ob.loc = loc_vec
B.Redraw()







![[]](/skins/blender/open.png)
