2
$\begingroup$

path following camera

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.

enter image description here

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)
$\endgroup$
2
  • 2
    $\begingroup$ 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) $\endgroup$
    – WhatAMesh
    Aug 20, 2018 at 17:27
  • $\begingroup$ Get the fcurves for the animation and use fcurve.evaluate() to get the values at a given frame. $\endgroup$
    – sambler
    Aug 22, 2018 at 4:34

1 Answer 1

3
$\begingroup$

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.)

$\endgroup$
2
  • 4
    $\begingroup$ Note too the decompose method loc, rot, scale = obj.matrix_world.decompose() $\endgroup$
    – batFINGER
    Aug 21, 2018 at 15:44
  • $\begingroup$ 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. $\endgroup$ Aug 22, 2018 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.