To link a mesh to an armature bone that the mesh retains its current position through the Blender Python API, we need to execute the following code:
For example, let’s link the cube to the first bone of the armature.
1 2 3 4 5 6 7 8 9 10 11 12 |
import bpy obj = bpy.data.objects['Cube'] arm = bpy.data.objects['Armature'] bone = arm.pose.bones[0] obj.parent = arm obj.parent_bone = bone.name obj.parent_type = 'BONE' m = obj.matrix_world.copy() obj.matrix_local @= m |
To unlink the cube from the bone, we need to execute the following:
1 2 3 4 5 6 7 8 9 10 11 |
import bpy obj = bpy.data.objects['Cube'] arm = bpy.data.objects['Armature'] bone = arm.pose.bones[0] obj.parent_bone = '' obj.parent = None m = obj.matrix_world.copy() obj.matrix_local = m |