ok, so you tried …
but i dont need to switch the modes (pose/edit).
This works for me (blender-2.58 rxxxx)
and is still far away from full usage:
If the bone has a parent, this is still wrong!!
And i did not test it full for an animated armature-object, my object-location
was at the center …
If the bone has a parent and is NOT connected (free translation), then
it should be possible to set it to a global position to, but what bone-space
to calculate in? Its parent-rotation, parent-tail as offset too …??
from mathutils import Vector, Matrix
def set_bonelocation(armaturename="Armature.001", bonename="Bone", newlocation=Vector((0,0,0))):
print("=========", armaturename, bonename, "========")
a = bpy.data.objects[armaturename]
bpose = a.pose.bones[bonename]
bdata = a.data.bones[bonename]
hasParent = False
if bdata.use_local_location == False:
print("bone used NOT in local location ... what now?")
if bdata.parent != None :
#has a parent-bone
parentname = bdata.parent.name
hasParent = True
if bdata.use_connect : #is True, we cannot move the bone
print("bone has parent:", parentname, " and is connected!")
parent_tail = a.data.bones[parentname].tail
parent_mat = a.data.bones[parentname].matrix.copy()
parent_mat_inv = parent_mat.inverted()
print(bpose.matrix) #the world-matrix that is with armature-object-matrix
print(bpose.matrix_basis) #the bone-matrix before any ?constraints, etc.
b_head = a.data.bones[bonename].head
b_mat = b_rotmat.copy()
b_mat_inv = b_mat.inverted()
print(bonename, bpose.location, b_head)
print( b_mat * b_mat_inv)
print( b_head * b_mat )
print( b_head * b_mat_inv)
print( "to newlocation..")
# a bone without parent, subtract its armature-loc and its bone-loc, than use translation in bone-space
location = (newlocation - a.location - b_head ) * b_mat_inv
location = (newlocation - a.location - b_head - parent_tail) * b_mat_inv * parent_mat_inv
bpose.location = location
set_bonelocation("Armature.001") #test with simple one bone
#set_bonelocation("Armature", "hips") #this is for the biped ...
and what to do if there is NOT activated “use_local_location” for a bone?
I never used this. Could this be used for easy setting the location in global-world-coords?