4
$\begingroup$

I'm trying to parent an object to a bone in python without using operator So I did this :

object = bpy.data.objects['object']
armature = bpy.data.objects['Armature']
bone = armature.pose.bones['bone']

object.parent = armature 
object.parent_bone = bone.name
object.matrix_parent_inverse = bone.matrix_local.inverted()

enter image description here

the object is on the tail of the bone it must be on the head What should I do?

$\endgroup$
1
  • $\begingroup$ Would be nice to know the answer to this. It's quite a simple concept, but appears to be impossible to do in Blener?? $\endgroup$
    – Logic1
    Commented Jul 13, 2018 at 2:40

2 Answers 2

2
$\begingroup$

I had the same problem (2.91), solved this way (seems to work):

    mpi = empty.matrix_parent_inverse
    ti = mathutils.Matrix.Translation([0, bone.length, 0]).inverted()
    empty.matrix_parent_inverse = mpi @ ti

empty is the object, I'm currently placing a sphere, so I'm not watching out for rotations, you may require some.

A couple of answers were useful:

$\endgroup$
2
$\begingroup$

I have been having a similar problem and using bone.length only works if the head of the bone is on the origin.

A more general solution is to find the vector from the tail back to the head and add that to the parent_matrix_inverted.

This function appears to work for bones regardless of their orientation. Note the rig must be in pose mode for this work otherwise rig.pose.bones will be empty.

def parentBone(rig, boneName, geo):
    bpy.context.evaluated_depsgraph_get().update()
    bone = rig.pose.bones[boneName]

    geo.parent=rig
    geo.parent_type = "BONE"
    geo.parent_bone = boneName

    vec = bone.head - bone.tail
    trans = mathutils.Matrix.Translation(vec)
    geo.matrix_parent_inverse = bone.matrix.inverted() @ trans
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .