How can I get a bone position in pose mode?

Hey,

I’m trying to write a script for automatic animation, but I’m stuck in getting the bone position in pose mode. Could anyone point me the good method ?

So far, I

Get the armature using bpy.context.objects[‘Armature’]
Reference the bones by armature.data.bones[‘NameOfTheBone’]
I’m able to move the bones by script by selecting them through armature.data.bones[‘NameOfTheBone’].select = True and bpy.ops.transform.translate/rotate
However, I can’t figure out how to get their positions in pose mode
Any tips ?

Thanks a lot !

Pos = armature.worldTransform @ Armature.blenderObject.pose.bones[‘BoneName’].location

This line “Armature.blenderObject.pose.bones[‘BoneName’].location” gives a vector with zeros as its value.

The thing is i want to pair an object to a bone in an armature but each time i change scene and go back to the previous scene where the paired object is, the object is moved away from its Original position while still being paired to its parent. How can i stop this object from moving when i change scenes.
Note: it’s parent object has an animation being played in run time

You can do something like this to get the bone’s head, tail or mid point:

import bpy, os

Armature = bpy.context.scene.objects[‘Armature’]
BoneHead = Armature.data.bones[‘Bone’].head_local + Armature.location
BoneTail = Armature.data.bones[‘Bone’].tail_local + Armature.location
BoneMedPoint = (BoneHead + BoneTail) / 2
print(BoneHead)
print(BoneTail)
print(BoneMedPoint)

It didn’t work :pensive::sob:

Bone Test.blend (895.4 KB)
Sorry, I just read you asked for pose mode, here’s an example for that, move Bone.002 in pose mode, and then run the script and it will put the empty over that bone.

I am sorry but i am having problems downloading the blend files. Please could you copy and paste the script here. Please :pleading_face::pray:

Is almost the same as before, but here is:

import bpy

Empty = bpy.context.scene.objects[‘Empty’]
Armature = bpy.context.scene.objects[‘Armature’]

BoneHead = Armature.pose.bones[‘Bone.002’].head + Armature.location
BoneTail = Armature.pose.bones[‘Bone.002’].tail + Armature.location
BoneMedPoint = (BoneHead + BoneTail) / 2
print(BoneHead)
print(BoneTail)
print(BoneMedPoint)

Empty.location = BoneMedPoint

I finally figured what the problem was.

The armature was posed with a different animation (A) but the game is set in a way that the armature plays a different animation (B) during runtime. Due to this, any object paired to the armature during scene change (back and forth scene change) gets displaced or slightly moved away from its original point.