# Reset Rotation , Translation and Scale of bones through Script

Like we do it by using shortcuts

• ALT + R to reset all rotations,
• ALT + G to reset all translations,
• ALT + S to reset all scales.

Is there a proper way to clear the pose through script. Below is my code, I know how to select specific bones, but am not sure about a way to reset the rotations , translations and scales of those bones.

bpy.ops.object.mode_set(mode='POSE')
arm = bpy.data.objects['Armature']
bpy.ops.pose.select_all(action='DESELECT')

for pb in arm.pose.bones:
if "Spine" in pb.name:
arm.data.bones[pb.name].select = True


You can:

import bpy
from mathutils import Vector, Quaternion

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

for pb in arm.pose.bones:
#Set the rotation to 0
identityQuaternion = Quaternion((0, 0, 0), 1 )
pb.rotation_quaternion = identityQuaternion
#Set the scale to 1
pb.scale = Vector( (1, 1, 1) )
#Set the location at rest (edit) pose bone position


Set matrix_basis to Identity

Following will reset transforms on all selected bones of the active object.

import bpy
from mathutils import Matrix

context = bpy.context

for pb in context.selected_pose_bones_from_active_object:
pb.matrix_basis = Matrix() #  == Matrix.Identity(4)


Using matrices per component

The above will reset all. What if we only want to reset one or two of the three components.

A matrix can be decomposed into its components and recomposed.

import bpy
from mathutils import Matrix

context = bpy.context
pb = context.active_pose_bone

M = pb.matrix_basis
loc, rot, scale = M.decompose()

T = Matrix.Translation(loc)
R = rot.to_matrix().to_4x4()
S = Matrix.Diagonal(scale.to_4d())
I = Matrix()

# is it the same?
assert(M == T @ R @ S)


If we wish to reset only rotation

M = T @ I @ S


and since I is the matrix equivalent of 1

M = T @ S


Perhaps set up a helper method

reset_pb(pb, reset_location=True, reset_rotation=True, reset_scale=True):


make a matrix for each component, eg

T = I if reset_location else Matrix.Translation(loc)


and set the matrix

pb.matrix_basis = T @ R @ S


Invoking These methods after selecting bones will clear Rotation , Scale and Transform

bpy.ops.pose.rot_clear() //To Clear Rotation
bpy.ops.pose.scale_clear() //To Clear Scale
bpy.ops.pose.transforms_clear() //To Clear Transform


Have the armature object of which you want to reset the pose bones selected, then run one of these two script.(work in object, edit and pose mode)

Reset all pose bones:

import bpy

for n in bpy.context.object.pose.bones:
n.location = (0, 0, 0)
n.rotation_quaternion = (1, 0, 0, 0)
n.rotation_axis_angle = (0, 0, 1, 0)
n.rotation_euler = (0, 0, 0)
n.scale = (1, 1, 1)


Reset pose bones from a list:

import bpy

resetBoneList = [ "boneName1", "boneName2", "boneName3"]

for n in resetBoneList:
if n in bpy.context.object.pose.bones:
bpy.context.object.pose.bones[n].location = (0, 0, 0)
bpy.context.object.pose.bones[n].rotation_quaternion = (1, 0, 0, 0)
bpy.context.object.pose.bones[n].rotation_axis_angle = (0, 0, 1, 0)
bpy.context.object.pose.bones[n].rotation_euler = (0, 0, 0)
bpy.context.object.pose.bones[n].scale = (1, 1, 1)


One of the solution is to

1. go in pose mode
2. select all bones
3. clear transforms using bpy.ops.clear_transform()

Note: using ops, we need to take care of context. Ensure that you are in pose mode and active object is the armature. Else context error will come. You can ensure that by

meshname = "mesh_1"
bpy.context.view_layer.objects.active = bpy.data.objects[meshname]


Switch to Pose mode using python

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


and now you can use bpy.ops...

def clear_all_transforms():
bpy.ops.pose.select_all(action="SELECT")
bpy.ops.pose.transforms_clear()
bpy.ops.pose.select_all(action="DESELECT")