# save location vector of an object

i like to measure the distance a object has traveled over time. But i fail already at saving the current vector. The result for test and location are always (except first frame change where test = At any other frame change test and location show equal results. Where is my mistake?

import bpy
from mathutils import *
from math import *
test = Vector((0,0,0))
def my_handler(scene):
global test
print('test:'+str(test))
location = bpy.data.objects['cube1'].location
print(location)
test = location

for i in range(len(bpy.app.handlers.frame_change_post)):
bpy.app.handlers.frame_change_post.pop()
bpy.app.handlers.frame_change_post.append(my_handler)


An example "print" result: test: test:

The print statement worked fine for me. My approach would be to collect the positions in a list and iterate at the last frame over the list to determine the length.

import bpy
from mathutils import *
from math import *

test = Vector((0,0,0))
locations = [] # list to hold all locations between start and end frame

def my_handler(scene):
if scene.frame_current == scene.frame_start:
locations.clear()
if scene.frame_current == scene.frame_end:
total_len = 0
for i in range(len(locations)-1):
d = locations[i] - locations[i+1]
total_len += d.length
print("length %f" % total_len )

location = bpy.data.objects['cube1'].location
locations.append(Vector(location))
#print(location)
#print(locations)

bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(my_handler)


I encountered the same problem while trying to search for the location of a sphere. I fixed it by multiplying the location by the world matrix, i.e. :

location = bpy.data.objects['cube1'].matrix_world * bpy.data.objects['cube1'].location


Maybe it will work for you too.

• i'll try it. But i never understand what the matrix_world or bone is! Is there anything more than the explanation from blender api? Jun 19, 2014 at 14:31
• @user2488 I don't know about the bone as I'm fairly new to Blender and animation in general, but you can consider that there are several coordinate systems in a scene: for ex, imagine you are sitting in a train; in reference to the train, you are still: your position is constant in the object coordinate system of the train; but compared to the ground, you are moving: your "world" coordinates change according to the speed/etc of the train. In this example, the world matrix transform the coordinates so that you know your position compared to the ground. Jun 19, 2014 at 15:58
• Okay i already understood this. But i don' know what values the single elements of the matrix are representing. I know there are 3 elements for scale, 3 elements for location. But for what are the other 10 for? (4 rotation quat?). Is there any page that is explaining that elements? Jun 20, 2014 at 14:32
• Actually this is not as simple. The first 3 columns represent the 3 basis vectors of the new coordinate system in the old, and the last column represents its origin. I found this article. The matrix is the result of rotation/translation/scaling applied in the desired order. You can deduce the translation because it's the same as the origin, but the scale coefficients can be affected by rotation. Better examples here Jun 23, 2014 at 20:21