0
$\begingroup$

I'm working on a project in Blender and need to reset the rotation for all bones in an armature along specific axes. I want to achieve this using a Python script. Specifically, I want to remove rotation along the X and Y axes for all bones in the selected armature.

Can someone provide a script or guidance on how to accomplish this?

$\endgroup$

1 Answer 1

0
$\begingroup$
import bpy

# Make sure you are in Pose mode
bpy.ops.object.mode_set(mode='POSE')

# Specify the axes you want to reset: 'X', 'Y', or 'Z'
selected_axes = ['X', 'Y']  # Change this list to include the desired axes, e.g., ['X', 'Y', 'Z']

# Iterate through all bones in the armature
for bone in bpy.context.object.pose.bones:
    if bone.rotation_mode == 'QUATERNION':
        # If the bone uses quaternions for rotation
        quat = bone.rotation_quaternion
        if 'X' in selected_axes:
            quat.x = 0.0
        if 'Y' in selected_axes:
            quat.y = 0.0
        if 'Z' in selected_axes:
            quat.z = 0.0
        quat.normalize()
    elif bone.rotation_mode == 'AXIS_ANGLE':
        # If the bone uses axis-angle for rotation
        axis_angle = bone.rotation_axis_angle
        if 'X' in selected_axes:
            axis_angle[1] = 0.0
        if 'Y' in selected_axes:
            axis_angle[2] = 0.0
        if 'Z' in selected_axes:
            axis_angle[3] = 0.0
    else:
        # If the bone uses Euler angles for rotation
        euler = bone.rotation_euler
        if 'X' in selected_axes:
            euler.x = 0.0
        if 'Y' in selected_axes:
            euler.y = 0.0
        if 'Z' in selected_axes:
            euler.z = 0.0

# Update the scene
bpy.context.view_layer.update()

```
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .