Index

右のサイト、HOW TO APPLY VECTOR MATH IN MAYA FOR POLE VECTOR CONTROL
を、一通りやってみた。
https://vimeo.com/240328348

コード化。

# -*- coding: utf-8 -*-
import maya.cmds as cmds
import maya.OpenMaya as om

degree = 4.0  # default is 'total_length'

# ①
def create_loc(pos):
    u''' <任意のポジションへ locator を作成する関数>'''
    loc = cmds.spaceLocator()
    cmds.move(pos.x, pos.y, pos.z, loc)

# ②
def get_pole_vec_pos(root_pos, mid_pos, end_pos):
    u''' <3つの joint のポジションから, poleVector の ポジションを計算する関数>'''
    root_joint_vec = om.MVector(root_pos[0], root_pos[1], root_pos[2])
    mid_joint_vec = om.MVector(mid_pos[0], mid_pos[1], mid_pos[2])
    end_joint_vec = om.MVector(end_pos[0], end_pos[1], end_pos[2])

    line = (end_joint_vec - root_joint_vec)
    point = (mid_joint_vec - root_joint_vec)

    scale_value = (line * point) / (line * line)
    proj_vec = line * scale_value + root_joint_vec

    root_to_mid_len = (mid_joint_vec - root_joint_vec).length()
    mid_to_end_len = (end_joint_vec - mid_joint_vec).length()
    total_length = root_to_mid_len + mid_to_end_len

    print('joints, total length is %s.' % total_length)

    pole_vec_pos = (mid_joint_vec - proj_vec).normal() * total_length + mid_joint_vec

    # create_loc(pole_vec_pos)
    # create_loc(point)

    return pole_vec_pos

# ③    
def get_ikh_pole_vec_pos(ik_handle):
    u''' <ik_handle を手掛かりとして, poleVector の ポジションを計算する関数>'''
    ik_joint_list = cmds.ikHandle(ik_handle, q = True, jointList = True)
    # print(ik_joint_list)
    ik_joint_list.append(cmds.listRelatives(ik_joint_list[-1], children = True, type = 'joint')[0])
    # print(ik_joint_list)

    root_joint_pos = cmds.xform(ik_joint_list[0], q = True, ws = True, t = True)
    mid_joint_pos = cmds.xform(ik_joint_list[1], q = True, ws = True, t = True)
    end_joint_pos = cmds.xform(ik_joint_list[2], q = True, ws = True, t = True)

    # ② 3つの joint  の ポジションから, poleVector の ポジションを計算する関数 を利用
    pole_vec_pos = get_pole_vec_pos(root_joint_pos, mid_joint_pos, end_joint_pos)

    # print(pole_vec_pos)
    return pole_vec_pos

# root_joint_pos = cmds.xform('root_jnt', q = True, ws = True, t = True)
# mid_joint_pos = cmds.xform('mid_jnt', q = True, ws = True, t = True)
# end_joint_pos = cmds.xform('end_jnt', q = True, ws = True, t = True)

# test = get_pole_vec_pos(root_joint_pos, mid_joint_pos, end_joint_pos)
# print(test)


def serchIK(sels):
    u''' <既存のpoleVector を選択して, poleVector から ik を探す関数>'''
    hisLists = cmds.listHistory(sels[0], leaf = False, levels = 2, future = True)
    # listHistory -leaf 0 -levels 2 -future 1
    lists = []

    for index in hisLists:
        type = cmds.objectType(index)
        # print(type)
        if type == 'ikHandle':
            lists.append(index)
        # type = cmds.objectType(index, isType = 'ikHandle')

    if len(lists):
        ik = lists[0]
        cmds.select(ik, r = True)
        print(u"ik -> %s がヒットしました。" % ik)
        om.MGlobal.displayInfo(u"ik -> %s がヒットしました。" % ik)
        return ik
    else:
        print(u"ik はありませんよ")
        om.MGlobal.displayInfo(u"ik はありませんよ")
        return

################################################      
# poleVector から ik を探す
# 既存のpoleVector を選択して、実行       
sels = cmds.ls(sl = True)
serchIK(sels)
################################################

# 継続で,下を実行か、もしくは、下のみ!!で実行

################################################
# 仮に既存の ik を選択して、
# getIkPoleVectL = cmds.ls(sl = True)
ik = cmds.ls(sl = True) 
# ③ 既存の ik を手掛かりとして、poleVector の ポジションを計算する関数を実行し、
pole_vec_pos = get_ikh_pole_vec_pos(ik[0])
# pole_vec_pos = get_ikh_pole_vec_pos('leg_ikHandle_L')
# ① 任意のポジションへlocator の作成
create_loc(pole_vec_pos)

19:00 2020/03/23



最終更新日: 2024年4月18日 09:56:42