Move Bone Tail to a certain location I have an armature where Automatic IK is enabled. I want to move my bone in pose mode using python instead of grabbing it using mouse. I tried the below but it didn't work as it says tail location is a read only. How I can move the bone's tail to a certain location in the scene without stretching the bone and in pose mode not edit mode?

import bpy

ob = bpy.data.objects['Armature']

ob.pose.bones['Bone.002'].tail = bpy.context.scene.cursor_location


I've also tried selecting the bone in Pose Mode then using ops.transform.translate but it didn't move the bone to the cursor:

bpy.ops.transform.translate(value=bpy.context.scene.cursor_location, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)




As shown below I can do this manually by selecting the bone and grabbing it to the correct position while auto-ik is enabled without resizing or stretching the bones:
Here is a solution using a temporary IK constraint:



The principle is to:


Set an empty at cursor position
Add a IK constraint to the bone targeting the empty
Get the resulting transformation
Remove the constraint
Assign the previously obtained transformations


The commented code:

import bpy

#Bones and its parents
def BoneAndParents( bone ):
    yield bone
    while bone.parent:
        bone = bone.parent
        yield bone

arm = bpy.data.objects['Armature']

bpy.ops.object.mode_set(mode='OBJECT')

#Add a temporary empty
bpy.ops.object.add()
empty = bpy.context.scene.objects.active

bpy.context.scene.objects.active = arm

bpy.ops.object.mode_set(mode='POSE')

bone = arm.pose.bones['Bone.003']

#Add IK constraint and update
constraint = bone.constraints.new( 'IK' )
constraint.target = empty
bone.constraints.update()
bpy.context.scene.update() #Important: update the scene

#Get the bones and transformation due to the constraint
bones = [b for b in BoneAndParents( bone )]
transformations = [b.matrix.copy() for b in bones]

#Get rid of the temporary constraint and update
bone.constraints.remove( constraint )
bone.constraints.update()

#Assign transformation
for b, t in zip( bones, transformations ):
    b.matrix = t

#Remove temporary empty
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.scene.objects.active = empty
bpy.ops.object.delete()

#Back to pose
bpy.context.scene.objects.active = arm
bpy.ops.object.mode_set(mode='POSE')