Skip to content

Instantly share code, notes, and snippets.

@flufy3d
Created October 23, 2012 09:20
Show Gist options
  • Save flufy3d/3937823 to your computer and use it in GitHub Desktop.
Save flufy3d/3937823 to your computer and use it in GitHub Desktop.
How to convert global to local coordinates in blender
from mathutils import Matrix
from mathutils import Vector
from mathutils import Quaternion
import bpy
print('start')
rVector = Vector((0.0, 0.0, 0.0))
rquat = Quaternion((0.707,0.707,0,0)).to_matrix().to_4x4()
mw = bpy.data.armatures['Armature'].bones['Bone'].matrix.copy()
hymat = Quaternion((0.707,-0.707,0,0)).inverted().to_matrix().to_4x4()
mat = Matrix.Translation(rVector) * rquat * mw.to_4x4()
bpy.context.object.pose.bones['Bone'].matrix = mat
squat = Quaternion((1.0,0,0,0)).to_matrix().to_4x4()
globalVector = Vector((0.0, 0.0, 1.0))
mw = bpy.data.armatures['Armature'].bones['Bone.001'].matrix.copy()
hymat = Quaternion((0.707,-0.707,0,0)).inverted().to_matrix().to_4x4()
mat = Matrix.Translation(globalVector) * squat * hymat * mw.to_4x4()
bpy.context.object.pose.bones['Bone.001'].matrix = mat
mw = bpy.data.armatures['Armature'].bones['Bone.002'].matrix.copy()
hymat = Quaternion((0.707,-0.707,0,0)).inverted().to_matrix().to_4x4()
mat = Matrix.Translation(globalVector) * squat * hymat * mw.to_4x4()
bpy.context.object.pose.bones['Bone.002'].matrix = mat
print('end')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment