How to get a ANIMATED bones rotation ?

I have an robotic arm in real life that is also modelled and animated in blender using IK.

I would like to write a script that sends positional information to my actual robot.

there is just one pice of python code im missing
it would look something like this

joint1 = bpy.data.objects[“Armature”].bone1.rotation.z # assign joint1 with the rotation of bone1 's z axis

The script will iterate through each frame of the animation getting and sending
the position of each joint in the arm.

Look at:


bpy.data.objects["Armature"].pose.bones

That has a list of all the bones in the armature in their currently posed position. You can access the bones and pull the euler rotations from them.

This is as far as i can get… I dont really understand how to list the options after each period.

bpy.data.objects[“Armature”].pose.bones[“Bone”].bone

Thanks for you help Kastoria !!

I did this but it Just returns zero despite my the fact that it moving all over the place.

bpy.data.objects[‘Armature’].pose.bones[‘Bone’].rotation_euler.z

here is my blend file in case any one wants to try it out.

http://blenducation.com/students/Mr%20Grayston/automatedstopmotion/

Thankyou SO SO MUCH batFINGER

That bit of code is exactly what I needed !!
Works just as you said.

PS if any one is interested this project is in another form here:
http://sktechworks.ca/forum/viewtopic.php?f=10&t=456

Ok here is my first wokring vertion of the script.

It writes the position of each joint at each frame into a txt file.
Also you can get the hole blend file here( rev 10…blend):

Thanks for your help Everyone
further develpment will be in this form
http://sktechworks.ca/forum/viewtopic.php?f=10&t=456&p=2853#p2853

import bpy
from mathutils import Matrix, Vector
from math import acos, degrees

#########################################

“Visual Transform” helper functions

#########################################

def get_pose_matrix_in_other_space(mat, pose_bone):
“”" Returns the transform matrix relative to pose_bone’s current
transform space. In other words, presuming that mat is in
armature space, slapping the returned matrix onto pose_bone
should give it the armature-space transforms of mat.
TODO: try to handle cases with axis-scaled parents better.
“”"
rest = pose_bone.bone.matrix_local.copy()
rest_inv = rest.inverted()
if pose_bone.parent:
par_mat = pose_bone.parent.matrix.copy()
par_inv = par_mat.inverted()
par_rest = pose_bone.parent.bone.matrix_local.copy()
else:
par_mat = Matrix()
par_inv = Matrix()
par_rest = Matrix()

# Get matrix in bone's current transform space
smat = rest_inv * (par_rest * (par_inv * mat))
# Compensate for non-local location
#if not pose_bone.bone.use_local_location:
#    loc = smat.to_translation() * (par_rest.inverted() * rest).to_quaternion()
#    smat.translation = loc
return smat

def get_local_pose_matrix(pose_bone):
“”" Returns the local transform matrix of the given pose bone.
“”"
return get_pose_matrix_in_other_space(pose_bone.matrix, pose_bone)

def frame(f):
bpy.data.scenes[0].frame_set(f)
bpy.data.scenes[0].update()

def get_bones_rotation(armature,bone,axis):

mat = get_local_pose_matrix(bpy.data.objects[armature].pose.bones[bone])
if axis == 'z':
    return degrees(mat.to_euler().z)
elif axis == 'y':
    return degrees(mat.to_euler().y)
elif axis == 'x':
    return degrees(mat.to_euler().x)

Start = bpy.data.scenes[‘Scene’].frame_start
End = bpy.data.scenes[‘Scene’].frame_end

############################################################## main program setup

frame(1) # set the frame to 1
frame(Start)

log = open(‘leg.txt’,‘wt’)

log.write('frameA----Aservo1(hip)B----Bservo2(knee)C----Cservo3(ankle)
')

for q in range(Start, End): #####################3###################### main program loop

frame(q)

servo1 = get_bones_rotation('Arm','Bone','z')
servo2 = get_bones_rotation('Arm','Bone.001','x')
servo3 = get_bones_rotation('Arm','Bone.002','x')

log.write(str(q))
log.write('A----A')
log.write(str(servo1))
log.write('B----B')
log.write(str(servo2))
log.write('C----C')
log.write(str(servo3))
log.write('

')

print(q,"----",servo1,servo2,servo3)  

log.close()