Skip to content

Instantly share code, notes, and snippets.

@adhihargo
Last active December 18, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adhihargo/5842868 to your computer and use it in GitHub Desktop.
Save adhihargo/5842868 to your computer and use it in GitHub Desktop.
Bone snapping tool. Works for parented bone, only if there's no Child Of constraint involved, and all parent's scale are uniform across all axis.
import bpy
class VIEW3D_OT_BoneSnapToObject(bpy.types.Operator):
"""Snap active bone to selected object."""
bl_idname = 'object.snap_to_object'
bl_label = 'Snap Bone to Object'
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(self, context):
return len(context.selected_objects) == 2\
and context.mode == 'POSE'
def execute(self, context):
target = [o for o in context.selected_objects
if o != context.active_object][0]
active = context.active_object
mat = active.matrix_world.inverted() * target.matrix_world
pbone = context.active_pose_bone
for p in pbone.parent_recursive:
mat = p.matrix_basis * mat
mat[3][0] = mat[3][1] = mat[3][2] = 0
pbone.matrix = mat
return {'FINISHED'}
def register():
bpy.utils.register_class(VIEW3D_OT_BoneSnapToObject)
def unregister():
bpy.utils.unregister_class(VIEW3D_OT_BoneSnapToObject)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment