# Get the transform matrix for each frame

I'm new to scripting inside blender (two days). I have a camera folowing a path (a circle).

I have written the following code to get the location and rotation for each frame and write it to a file:

import bpy

transformMetrix=open('C:\\Users\\sunimalm\\Desktop\\transformMetrix.txt','w')

for f in range(bpy.context.scene.frame_start,bpy.context.scene.frame_end):
for obj in bpy.context.selected_objects:
transformMetrix.write('\n'+obj.name+'\n')
transformMetrix.write('\n'+ str(f)+'\n')
transformMetrix.write('Location : ' + str(obj.location.x) +' '+ str(obj.location.y) +' '+ str(obj.location.z) +'\n')
transformMetrix.write('rotation : ' + str(obj.rotation_euler.x) +' '+str(obj.rotation_euler.y) +' '+str(obj.rotation_euler.z)+'\n')
transformMetrix.write('scale : ' + str(int(obj.scale.x)) +' '+str(int(obj.scale.y))+' '+str(int(obj.scale.z)) +'\n')

transformMetrix.close()


When I play and run the script I only get the same rotation and location matrix for every frame.

What am I doing wrong here?

Then I tried to put above code inside a function and append it to a handler, but I still get the same output:

bpy.app.handlers.frame_change_post.append(function)

• Looks like to me that you just iterate through the frames but don't ask where the object is in a certain frame, just where the object is (that will be frame 0 or 1 w/e) – WhatAMesh Aug 20 '18 at 17:27
• Get the fcurves for the animation and use fcurve.evaluate() to get the values at a given frame. – sambler Aug 22 '18 at 4:34

You have to change the frame in your for-loop via Scene.frame_set():
bpy.context.scene.frame_set(f)

By the way, I would not use obj.location, rotation_euler etc. because they do not contain the final location, rotation and so on. For example, parent your camera to an object, then rotate the object - the camera.rotation_euler will not change.
You should use obj.matrix_world instead (if you need it separated, decompose this matrix with to_location, to_euler etc.)
• Note too the decompose method loc, rot, scale = obj.matrix_world.decompose() – batFINGER Aug 21 '18 at 15:44
• Thank you for the answer. Adding bpy.context.scene.frame_set(f) for the initial code didn't gave me the expected output. it was same as the result I posted. I think this is because of the issue mentioned here right? but setting the frame bpy.context.scene.frame_set(f) and taking the transform metrix with obj.matrix_world worked. – Sunimal S.K Malkakulage Aug 22 '18 at 21:11