# How to move and resize a bone in Python such that the head and the tail are at a certain global position?

I want to move and resize a bone by setting the coordinates of the head of the bone and the tail of the bone explicitly.

For example, let's say that these are my coordinates of my bone head:

[181, 151, 67]


and that these are the coordinates of my bone tail:

[224, 129, 76]


I accessed my bone (actually, the armature) in code as follows:

right_upper_hand_bone = bpy.data.objects["Armature"]


Now I want to move and resize right_upper_hand_bone such that its head is located at [181, 151, 67] and its tail is located at [224, 129, 76].

I Googled around, but I haven't been able to find out how to do this.

Can anyone tell me how to do this?

EDIT: I'm looking how to reshape the bone such that I explicitly set its head and its tail. Currently, I have a structure where I have a lot of armatures and only one bone within them, but I think it shouldn't be hard to adjust my code to work with one armature with many bones etc.

• Changing the position is easy, referencing it to the global coordinate system is what I don’t know about. Look for that. Commented Jan 13, 2023 at 14:34
• @TheLabCat can you describe what you had in mind? Commented Jan 16, 2023 at 8:43

I have an armature called "Armature" containing 1 bone named "Bone". Here's a script that will reposition your bone's head and bone's tail to the positions $$(181, 151, 67)$$ & $$(224, 129, 76)$$ in global space, respectively. Just adjust head_pos & tail_pos as needed. Take note that the Origin of your armature will still remain at whatever position it was after you run the script.

import bpy
from mathutils import Vector

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

armature = bpy.data.objects["Armature"]
bone = armature.data.edit_bones["Bone"]


• Thank you for the script. It works for one bone. However, I tried it for multiple bones and it doesn't work. I get an error KeyError: 'bpy_prop_collection[key]: key "Bone" not found' for any other Bone i try to access, even though the names of all of my bones are Bone under different armatures. Do you know why this code doesn't work for multiple bones? Commented Jan 16, 2023 at 10:41