[2.82] Best method of parenting a object to a bone

Hy everyone,

There are many posts that ask the question about the relationship between an object and a bone. The methods differ. Some people do parenting without going through a change of mode(object, edit, pose,…).

without mode change

And others go through the mode changes

with mode change

Is one method more valuable than the other in the 2.82? Can we vary the keep_transform in both cases?

Good Day,

Hy everyone,
Do you know how to stop the object from moving at the time of parenting?

def ParentBones() :
    print("def ParentBones start")
    
    context = bpy.context
    scene = context.scene
    
    objs_select = context.selected_objects
    
    if objs_select is not None :
        
        #ARMATURE
        arma = scene.arma_props.obj
        
        bone_name = scene.bones_map[scene.bones_map_index].name
        
        bone = arma.pose.bones.get(bone_name)
        
        for i in objs_select :
            
            if i is arma :
                continue
            i.parent = arma
            bone = arma.pose.bones.get("Bone")
            i.parent_bone = bone_name
            i.parent_type = 'BONE'

Good day,

from mathutils import Matrix

i.matrix_parent_inverse = (arma.matrix_world @ Matrix.Translation(bone.tail - bone.head) @ bone.matrix).inverted()

thanks for all your explanations, otherwise I probably wouldn’t have made it.

I don’t know if we can introduce him to the community once he’s clean. Knowing that, not surprisingly, it’s a beginner’s add-on. Just to get a feedback on it of course.