0

So i want to print each coordinate of XYZ individually along with the rotation and location. I found some code online structured like this for printing key frame data but i'm not sure how to get it to print individually.

    def get_keyframes(obj_list):
        keyframes = []
        for obj in obj_list:
            anim = obj.animation_data
            if anim is not None and anim.action is not None:
                for fcu in anim.action.fcurves:
                    for keyframe in fcu.keyframe_points:
                        x, y = keyframe.co
                        if x not in keyframes:
                            keyframes.append((math.ceil(x)))
        return keyframes
keys = get_keyframes(selection)
selection = bpy.context.selected_objects

This defines the keys. So lets say i want to define each coodinate as a variable:

for obj in enumerate(selection):
    XRot = round(keys.rotation_euler.x, 3)
    ZRot = round(keys.rotation_euler.z, 3)
    YRot = round(keys.rotation_euler.y, 3)
    xloc = round(keys.location.x, 3)
    yloc = round(keys.location.y, 3)
    zloc = round(keys.location.z, 3)

    EachKeyFrame  =  [key for key in keys]

    for keys in enumerate(EachKeyFrame):
        print(str(ZRot))

I've tried this although it gives a "AttributeError: 'tuple' object has no attribute 'rotation_euler'" error.

I've also tried ints like this but gives the same error.

for obj in enumerate(selection):
    XRot = round(keys.rotation_euler[0], 3)
    ZRot = round(keys.rotation_euler[1], 3)
    YRot = round(keys.rotation_euler[2], 3)
    xloc = round(keys.location[0], 3)
    yloc = round(keys.location[1], 3)
    zloc = round(keys.location[2], 3)

    EachKeyFrame  =  [key for key in keys]

    for keys in enumerate(EachKeyFrame):
        print(str(ZRot))
nathan miller
  • 89
  • 1
  • 8

1 Answers1

1

Avoid the enumerate, change keys to object as Doyousketch2 mentioned.

import bpy
import math

selection = bpy.context.selected_objects

def get_keyframes(obj_list):
    keyframes = []
    for obj in obj_list:
        anim = obj.animation_data
        if anim is not None and anim.action is not None:
            for fcu in anim.action.fcurves:
                for keyframe in fcu.keyframe_points:
                    x, y = keyframe.co
                    if x not in keyframes:
                        keyframes.append(math.ceil(x)) #(math.ceil(x)))
    return keyframes

def print_details(obj_list):
    XRot = round(obj_list.rotation_euler.x, 2)
    ZRot = round(obj_list.rotation_euler.z, 2)
    YRot = round(obj_list.rotation_euler.y, 2)
    xloc = round(obj_list.location.x, 2)
    yloc = round(obj_list.location.y, 2)
    zloc = round(obj_list.location.z, 2)
    print(obj_list.name)
    print("X Rot : ", XRot, '\t'*2, "Y Rot : ", YRot, '\t'*2, "Z Rot : ", ZRot)
    print("X Loc : ", xloc, '\t'*2, "Y Loc : ", yloc, '\t'*2, "Z Loc : ", zloc)


print("-"*25, "starting","-"*25)
for key in get_keyframes(selection):
    bpy.context.scene.frame_set(key)
    print("key : ", key)
    for obj in selection:
        print_details(obj)
print("-"*25, "ending","-"*25)

refer to example How can I get the location of an object at each keyframe?

Ratt
  • 2,126
  • 1
  • 10
  • 17
  • Ah i see. Although I'm trying to use for keys in enumerate(EachKeyFrame):for each existing keyframe to print, seems to print the amount of instances there are but with the same values of the objects current position. So 3 key frames will print the same value 3 times. – nathan miller Dec 01 '17 at 14:41
  • The same data is being printed due to the fact that everything is in reference to 'bpy.contex'. which is not being updated with the script. – Ratt Dec 01 '17 at 21:33