I’m trying to get a bone’s location at every single frame of an animation, and I’m not sure where I am going wrong. I’m attaching a file to show what I have been attempting.
Basically, there is a bone that has an animation that moves it 10 units on the y-axis across 20 frames. There are 3 scripts in the text editor, two of them are attempts at getting the location of the bone at each frame. Both of those scripts fail to get the bone’s location as I loop thru the frames of animation - the only data they return is the location of the bone at the current frame when the script is run.
Yes - this is a blender 2.76 project. The reason is beacuse that’s the last version I used before I took a break from blender. I wanted to finish this project in 2.76 before bringing it forward into current version of blender as a way of getting back into blender without learning all the changes that have taken place.
I was in a hurry when I made my first post and realize I should have posted some of the code, to save people from downloading my file. So here’s the code I am using:
import bpy
from bpy import context
armature = "Armature" # change this to the name of your armature
for index in range(1, 21):
tail = bpy.data.objects[armature].location + context.active_pose_bone.tail
head = bpy.data.objects[armature].location + context.active_pose_bone.head
print("Frame # ", index)
print("head x = ", head[0])
print("head y = ", head[1])
print("head z = ", head[2])
#bpy.ops.screen.keyframe_jump(next=True)
bpy.data.scenes['Scene'].frame_current = index
With this code, I was trying to get the location of a bone’s head at each frame in a 20 frame animation. However, the code prints the same location for each frame. When the code is done, the current frame is frame 21, so it is changing frames, but not updating the position of the bone. I tried bpy.ops.screen.keyframe_jump(next=True) because I thought operators were supposed to update blender when executed. Still no luck…
Looking around, I think I found a better way to get what I wanted, by looking at the f-curves:
import bpy
import math
total_dist = 0
action = bpy.data.actions.get("Action")
y_curve = action.fcurves[1]
prev_y = y_curve.evaluate(1) # get starting value
for index in range (2,21):
curr_y = y_curve.evaluate(index)
dist = math.sqrt( ((curr_y - prev_y) ** 2) )
total_dist = total_dist + dist
prev_y = curr_y
print(total_dist)
With this code, I was able to calculate the distance the bone traveled in the 20 frame animation. So now I should be able to figure out the distance the bone travels in all 3 axis for each frame, which is what I needed to know.
Still don’t understand why the first bit of code didn’t update per frame change, any ideas?