# How to compute and visualize a Vector?

I would like to use Blender to visualize what I'm calculating with linear algebra formulas, I need to perform linear functions starting with a 2D or 3D vector and multiply this entity with a custom defined matrix; the result of this is a vector that I would like to visualize in some sort of way on the scene ( an arrow, a cilinder, a 3D shape a 2D shape, something ... possibly with colors so I can visualize multiple vector on the same scene ).

Now I know how to get the x,y,z coordinates for each vertex,

# 1 possible way to get x
bpy.context.object.data.vertices[0].co.x


I don't know how to get an object of type Vector out of this ( 2D or 3D vector ) and fill a matrix ( tipically a 3x3 matrix ) that I can use to perform the computation.

I'm basically having a little trouble understanding how to get the types that I want for the task that I need to perform.

And in the end I also need to keep the result of this computation, tipically an array of vectors/vertices, on the scene in an easy-to-visualize way.

I'm using Blender 2.68a.

Thanks.

The documents for the Matrix and Vector classes are invaluable reading.

from mathutils import Vector

# zero vector, a default
v1 = Vector()

# quick-hand reference to the class.
print(dir(v1))

# using integers or floats
v2 = Vector((1, 0, 0))
v3 = Vector((1.0, 3.0, 0.0))

x, y, z = v3.to_tuple()

v3.xyz
# Vector((1.0, 3.0, 0.0))

v3.zyx
# Vector((0.0, 3.0, 1.0))

# normalize the vector in place, overwrites the value of original vector
v3.normalize()

# normalized returns a normalized copy but does not modify the original.
v3.normalized()


There are many more methods in the Vector class, all covered in the documentation. Unfortunately there are no ready made tools for blender that will draw the head and tail of vectors in the way that you describe. I can see how they would be useful for teaching about vectors initially.

Math Viz addon, doesn't have much documentation but it is straight forward enough. Enable the addon then use the python console to enter these one by one while observing what happens in the 3d view.

v1 = Vector()
v2 = Vector((1,1,0))
line = [v1, v2]   # brackets are optional


If you want to delete any you can do del v1 or del line

I would suggest to add empty objects, or create a mesh to visualize Matrix/Euler/Vector types.

As a handy alternative to this, Blender comes with the MathVis addon that draws python defined variables in the view-port.

• what is the namespace for this types ? – user1154 Aug 23 '13 at 7:54
• @user1154, For MathVis - the consoles global namespace is currently used. – ideasman42 Aug 23 '13 at 9:42

If you wanna fill a matrix with a location vector, then it needs to be 4x4 transformation matrix of course (3x3 is used for rotation, CryEngine uses 3x4 for loc+rot, 4x4 is loc+rot+scale).

There's a utility function to create a 4x4 translation matrix from a 3d vector:

Matrix.Translation(bpy.context.object.data.vertices[0].co)