Skip to content

Instantly share code, notes, and snippets.

@pink-vertex
Created April 1, 2016 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pink-vertex/5cd991bad102d3c260af8205ba989464 to your computer and use it in GitHub Desktop.
Save pink-vertex/5cd991bad102d3c260af8205ba989464 to your computer and use it in GitHub Desktop.
import bpy
from mathutils import Matrix, Vector
# ported from blenkernel/intern/armature.c to python
# --------------------------------------------------------------------
def get_mat_offs(pose_bone):
bone = pose_bone.bone
mat_offs = bone.matrix.to_4x4()
mat_offs.translation = bone.head
mat_offs.translation.y += bone.parent.length
return mat_offs
def get_mat_rest(pose_bone, mat_pose_parent):
bone = pose_bone.bone
if pose_bone.parent:
mat_offs = get_mat_offs(bone)
# --------- rotscale
if (not bone.use_inherit_rotation and
not bone.use_inherit_scale):
mat_rotscale = bone.parent.matrix_local * mat_offs
elif not bone.use_inherit_rotation:
mat_size = Matrix.Identity(4)
for i in range(3):
mat_size[i][i] = mat_pose_parent.col[i].magnitude
mat_rotscale = mat_size * bone.parent.matrix_local * mat_offs
elif not bone.use_inherit_scale:
mat_rotscale = mat_pose_parent.normalized() * mat_offs
else:
mat_rotscale = mat_pose_parent * mat_offs
# --------- location
if not bone.use_local_location:
mat_a = Matrix.Translation(
mat_pose_parent * mat_offs.translation)
mat_b = mat_pose_parent.copy()
mat_b.translation = Vector()
mat_loc = mat_a * mat_b
elif (not bone.use_inherit_rotation or
not bone.use_inherit_scale):
mat_loc = mat_pose_parent * mat_offs
else:
mat_loc = mat_rotscale.copy()
else:
mat_rotscale = bone.matrix_local
if not bone.use_local_location:
mat_loc = Matrix.Translation(bone.matrix_local.translation)
else:
mat_loc = mat_rotscale.copy()
return mat_rotscale, mat_loc
@default_to_existing_values
def get_mat_pose(pose_bone, mat_pose_parent, mat_basis):
mat_rotscale, mat_loc = get_mat_rest(pose_bone, mat_pose_parent)
mat_pose = mat_rotscale * mat_basis
mat_pose.translation = mat_loc * mat_basis.translation
return mat_pose
def default_to_existing_values(func):
def func_new(pose_bone, mat_pose_parent=None, mat_basis=None):
if pose_bone.parent and not mat_pose_parent:
mat_pose_parent = pose_bone.parent.matrix
if not mat_basis:
mat_basis = pose_bone.matrix_basis
func(pose_bone, mat_pose_parent, mat_basis)
return func_new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment