I'm trying to use python to get world space location and rotation values of an animated camera, including transformations applied trough parents and constraints.
I need the Values as XYZ coordinates and XYZ rotation in degrees. I can achieve this by manually selecting object>animation>bake action with the options "Visual Keying", "Clear Constraints" and "Clear Parents" enabled:
And then accessing the data from the baked camera with python using
import bpy import sys import mathutils from math import degrees cameraRotationX = degrees(bpy.data.objects["Camera"].rotation_euler) cameraRotationY = degrees(bpy.data.objects["Camera"].rotation_euler) cameraRotationZ = degrees(bpy.data.objects["Camera"].rotation_euler) cameraLocationX = bpy.data.objects["Camera"].location cameraLocationY = bpy.data.objects["Camera"].location cameraLocationZ = bpy.data.objects["Camera"].location print("Camera rotation X: ","%.7f" %cameraRotationX) print("Camera rotation Y: ","%.7f" %cameraRotationY) print("Camera rotation Z: ","%.7f" %cameraRotationZ) print("Camera Location X: ","%.7f" %cameraLocationX) print("Camera Location Y: ","%.7f" %cameraLocationY) print("Camera Location Z: ","%.7f" %cameraLocationZ)
This works fine and it gives me the correct Values, including multiple rotations of the camera, for example 720°. However, I need to go through the process of baking first and I can't change the animation after that, unless I duplicate the object before baking to preserve the editable animation.
Is there a way to get the same values directly in python without going through the baking process first? I tried
rotation=bpy.data.objects['Camera'].matrix_world.to_euler('XYZ') location=bpy.data.objects['Camera'].matrix_world.to_translation() rotationX=degrees(rotation) rotationY=degrees(rotation) rotationZ=degrees(rotation) locationX=location locationY=location locationZ=location
But this does not result in the same rotation values.
I am a newbie to python and blender, as well as to matrices and the corresponding calculations. So maybe I am missing something obvious.
Can somebody please pint me in the right direction? If there is a way to programmatically bake the animation without losing the original animation, that would help as well.
The reason for this is that I am using the location and rotation values to control a robot arm. I want wo be able to adjust the animation and get the resulting Values without having to go back and forth with baking. I hope that makes sense.
Any input would be appreciated. Thanks.