Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 14, 2011

Blender 2.5 Python Vector Arithmetic

Blender has a sweet library of maths functions / classes, one of them is Vector. (from mathutils import Vector)
because coordinates (co) are Vector datatypes

# default cube in edit mode
>>> bpy.context.object.data.vertices[0].co
Vector((1.0, 0.9999999403953552, -1.0))
you can simply do

# let's pretend vec1 and vec2 are already defined.
>>> vec1 = Vector((2.0,2.0,2.0))
>>> vec2 = Vector((3.0,3.0,3.0))

>>> vec1+vec2
Vector((5.0, 5.0, 5.0))
that beats having to do

>>> Vector((vec1[0] + vec2[0], vec1[1] + vec2[1], vec1[2] + vec2[2]))
Vector((5.0, 5.0, 5.0))
...etc