Last active
July 7, 2019 20:57
-
-
Save dskjal/3d6ca41d5776d3636756e88fed3e788a to your computer and use it in GitHub Desktop.
Blender で IK/FK を一致させる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BEGIN MIT LICENSE BLOCK // | |
// | |
// Copyright (c) 2019 dskjal | |
// This software is released under the MIT License. | |
// http://opensource.org/licenses/mit-license.php | |
// | |
// END MIT LICENSE BLOCK // | |
import bpy | |
from bpy.props import * | |
import mathutils | |
num_bones = 2 | |
armature_name = 'Armature' | |
ik_target_name = 'T_IK' | |
pole_target_name = 'P_IK' | |
elbow_name = 'D_b1' | |
fk_tarminal_name = 'D_b2' | |
ik_tarminal_name = 'IK_b2' | |
class UI(bpy.types.Panel): | |
bl_label = "Snap FK/IK" | |
bl_space_type = "VIEW_3D" | |
bl_region_type = "UI" | |
@classmethod | |
def poll(self, context): | |
o = context.active_object | |
return o and o.type == 'ARMATURE' and o.mode == 'POSE' | |
def draw(self, context): | |
self.layout.operator("my.fktoik") | |
self.layout.operator("my.iktofk") | |
class FKtoIKButton(bpy.types.Operator): | |
bl_idname = "my.fktoik" | |
bl_label = "FK -> IK" | |
bl_options = {'REGISTER', 'UNDO'} | |
def execute(self, context): | |
amt = bpy.data.objects[armature_name] | |
ik_target = amt.pose.bones[ik_target_name] | |
pole_target = amt.pose.bones[pole_target_name] | |
elbowBone = amt.pose.bones[elbow_name] | |
fk_tarminal = amt.pose.bones[fk_tarminal_name] | |
ik_tarminal = amt.pose.bones[ik_tarminal_name] | |
bones = [] | |
fk_it = fk_tarminal | |
ik_it = ik_tarminal | |
for i in range(num_bones): | |
bones.append((fk_it,ik_it)) | |
fk_it = fk_it.parent | |
ik_it = ik_it.parent | |
for b in reversed(bones): | |
b[0].matrix = b[1].matrix | |
bpy.context.scene.update() | |
return {'FINISHED'} | |
def set_translation(matrix, loc): | |
trs = matrix.decompose() | |
rot = trs[1].to_matrix().to_4x4() | |
scale = mathutils.Matrix.Scale(1, 4, trs[2]) | |
return mathutils.Matrix.Translation(loc) * (rot * scale) | |
class IKtoFKButton(bpy.types.Operator): | |
bl_idname = "my.iktofk" | |
bl_label = "IK -> FK" | |
bl_options = {'REGISTER', 'UNDO'} | |
def execute(self, context): | |
amt = bpy.data.objects[armature_name] | |
ik_target = amt.pose.bones[ik_target_name] | |
pole_target = amt.pose.bones[pole_target_name] | |
elbowBone = amt.pose.bones[elbow_name] | |
fk_tarminal = amt.pose.bones[fk_tarminal_name] | |
ik_tarminal = amt.pose.bones[ik_tarminal_name] | |
#move IK target to FK terminal | |
ik_target.matrix = set_translation(ik_target.matrix, fk_tarminal.tail) | |
#move pole target to elbow | |
pole_target.matrix = set_translation(pole_target.matrix, elbowBone.tail) | |
return {'FINISHED'} | |
classes = ( | |
UI, | |
FKtoIKButton, | |
IKtoFKButton | |
) | |
def register(): | |
for cls in classes: | |
bpy.utils.register_class(cls) | |
def unregister(): | |
for cls in reverse(classes): | |
bpy.utils.unregister_class(cls) | |
if __name__ == "__main__": | |
register() |
I have forgot to add a licence. This script can be used freely at your own risk.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi I stumbled with your blog and "stole" your FK/IK snapping code XD, but in your solution the hands won't snap with each other, I mean if my FK hand is 90 degrees to the left, when I snap the IK to it, the IK won't rotate 90 degrees to the left, I don't know anything about python so I can't solve it myself, but I'll be very thankful if you can do it. (Also I will REALLY want to invite you a coffee, your script was all I need to improve my rigging, don't you have any donation link or PayPal?) thanks again (sorry for posting this here, I know it has nothing to do with your video, but I couldn't find any other form of contact)