Bone Locations

I’m kind of new with blender… and I really need help because this issue is really giving me very hard time …

I want to get the upper limb joint angles (elbow, shoulder) continuously during the game … but I cant even get the location of the bones … I use this code but it always return me zero vector no matter what :

bpy.data.objects[“armature”].pose.bones[“elbow_l”].location.xyz

can any body help please ?!

Individual bones are not seperate objects so dont have positions.
You should use ;

bone[‘mybone’].head - the position of the base of the bone, relative to parent.
bone[‘mybone’].head_local - the position of the base of the bone, relative to armature (local space)

You probably want the second one. Multiply with the armature’s world matrix to get world space pos.
See also;

.tail - the position of the end of the bone relative to parent
.tail_local - the position of the end of the bone relative to armature
.center - the postion between the two above
.vector - the direction of the bone

blender_python_api_2_71_release/bpy.types.Bone.html#bpy.types.Bone

To get the _local options, access through;
object.data.bones[n]
object.pose.bones[n].bone

1 Like

Thanks very much .That was very helpful
but i still have a problem

Although it gets the correct bone angle at the beginning of the game … it Keeps getting the same angle although the armature moves during the game

do u have any idea how can I overcome this problem!!

Can any one answer me ?!
this is really critical

ont = G.getCurrentController()
own = cont.owner
target=own[“Targets”]
path = logic.expandPath("//")

controller = bge.logic.getCurrentController()
scene=bge.logic.getCurrentScene()

#act = controller.actuators[“EditObject”]

get object being tracked

obj = act.object

#loc,size,quat = act.getChannel(obj)
#print(own.getDistanceTo(“Elbow_L”))
cont = G.getCurrentController()
own = cont.owner
print(own)

target=own.actuators[“level”]
v1=bpy.data.objects[“Armature”].pose.bones[‘Elbow_L’].vector
v2=bpy.data.objects[“Armature”].pose.bones[‘Shoulder_L’].vector
v3=bpy.data.objects[“Armature”].pose.bones[‘Hand_L’].vector
v4=bpy.data.objects[“Armature”].pose.bones[‘Neck’].vector

def dotproduct(v1, v2):
return sum((a*b) for a, b in zip(v1, v2))

def length(v):
return math.sqrt(dotproduct(v, v))

def angle(v1, v2):
return math.acos(dotproduct(v1, v2) / (length(v1) * length(v2)))

elbow=int(180-angle(v1,v2))
shoulder=int(angle(v2,v4))
wrist= int(180-angle(v3,v1))

print(elbow+","+shoulder+","+wrist)

it always return the same angle at the game start even the armature moves during the game