This is probably more akin to "moving the object origin without changing the location". The object's origin is the location of (0, 0, 0) in local coordinates.
Basically, find the minimum z location (minz
) of all bones' heads and tails, then translate all such that the lowest local z is now 0.
With operators, select all bones, translate.
import bpy
context = bpy.context
arm_obj = context.object
arm = arm_obj.data
bpy.ops.object.mode_set(mode='EDIT')
# get min z
minz = min(min(b.tail.z, b.head.z) for b in arm.edit_bones)
bpy.ops.armature.select_all(action='SELECT')
bpy.ops.transform.translate(
value=(0, 0, -minz),
constraint_orientation='LOCAL')
bpy.ops.object.mode_set(mode='OBJECT')
Without operators (except edit toggle), create a translation matrix (T = Matrix.Translation((0, 0, -minz))
) and use it to translate the matrix of each bone.
import bpy
from mathutils import Matrix
context = bpy.context
arm_obj = context.object
arm = arm_obj.data
bpy.ops.object.mode_set(mode='EDIT')
# get min z
minz = min(min(b.tail.z, b.head.z) for b in arm.edit_bones)
# calculte the translation matrix
T = Matrix.Translation((0, 0, -minz))
# translate all bones.
for b in arm.edit_bones:
b.matrix = T * b.matrix
bpy.ops.object.mode_set(mode='OBJECT')