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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have forgot to add a licence. This script can be used freely at your own risk.