1
$\begingroup$

I have a bone and two empties, how in edit mode I can move the head of the bone to the location of empty1 and the tip to the location of empty2 using python?

I'm trying the below script but it doesn't work as I want it to move to the empty location as highlighted below.

import bpy

bpy.ops.object.select_all(action='DESELECT')
obj = bpy.data.objects['metarig']
obj.select = True
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
emp = bpy.data.objects['Empty']
for bone in obj.data.edit_bones:
    if bone.name == 'hips':
        bone.head = emp.location

My .blend file can be found here:

enter image description here

$\endgroup$
  • 1
    $\begingroup$ edit bones are in "local" coordinates, eg a head of (0, 0, 0) is at the location of the armature object origin etc. To convert multiply by the armature object's world matrix inverse. Does your armature have scale? $\endgroup$ – batFINGER Feb 18 '17 at 13:07
2
$\begingroup$

Calculate the local coordinates by applying the inverse matrix of the armature to the global coordinates.

# obj       the armature (scaled)
# emp       the empty in world space (no parent)

bone.head =  obj.matrix_world.inverted() * emp.location
$\endgroup$
  • $\begingroup$ emp.matrix_world.to_translation() is global location of empty with / without parent, constraint etc etc. $\endgroup$ – batFINGER Feb 19 '17 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.