Monday, 22 November 2010

Bone Roll in Blender

It's well known that setting bone roll to Z-axis up in blender often doesn't work well, if at all.  If you find yourself having to set bone roll manually using Ctrl+R then this script might help you out.  Tab into bone edit mode, select the bones you want to roll, copy and paste the below script to a text editor, set your target roll vector as new_z_axis_vector in the script below (at the moment its set for z up) and hit run script.

import bpy,mathutils,math

new_z_axis_vector=mathutils.Vector((0,0,1))

for each_bone in bpy.context.selected_bones:
    new_x_axis=each_bone.y_axis.copy().cross(new_z_axis_vector)
    if new_x_axis.copy().angle(each_bone.x_axis)>90:
        new_x_axis=(-1)*new_x_axis
    previous_z_angle=each_bone.z_axis.angle(new_z_axis_vector)
    roll_adjust=each_bone.x_axis.angle(new_x_axis)
    each_bone.roll+=roll_adjust
    if each_bone.x_axis.angle(new_x_axis)>0.01:
        each_bone.roll-=2*roll_adjust


Of course the correct rigging practice is to make sure the bone roll of all the bones in a chain is such that the z axes vectors of all the bones are in the same plane so you might want to set new_z_axis_vector to something other than (0,0,1) or world z, for example a leg IK chain might need a value of (0,1,0) to make all the bone z-axes point forward.

No comments:

Post a Comment