使用python在Maya中放置极点矢量的定位器

时间:2019-06-29 15:06:34

标签: python maya

我一直在遵循教程(https://vimeo.com/240328348),该教程涉及创建定位器并将其放置在极向量的正确位置。由于某种原因,当我运行脚本时,它始终将定位器直接放置在中间关节上,而不是将其移动到关节后面指定的长度。

我们将不胜感激。

    import maya.cmds as cmds
import maya.OpenMaya as om

def create_loc(pos):
    loc = cmds.spaceLocator()
    cmds.move(pos.x, pos.y, pos.z, loc)

def get_pole_vec_pos(root_pos,rot_pos, mid_pos, low_pos, end_pos):
    root_joint_vec = om.MVector(root_pos[0], root_pos[1], root_pos[2])
    rot_joint_vec = om.MVector(rot_pos[0], rot_pos[1], rot_pos[2])
    mid_joint_vec = om.MVector(mid_pos[0], mid_pos[1], mid_pos[2])
    low_joint_vec = om.MVector(low_pos[0], low_pos[1], low_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 - rot_joint_vec - root_joint_vec).length()
    mid_to_end_len = (end_joint_vec - low_joint_vec - mid_joint_vec).length()
    total_length = (root_to_mid_len + mid_to_end_len)

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

    return pole_vec_pos

def get_ikh_pole_vec_pos(ik_handle):

    ik_joint_list = cmds.ikHandle(ik_handle, q=True, jointList=True)
    ik_joint_list.append(cmds.listRelatives(ik_joint_list[1],children=True, type='joint')[0])

    root_joint_pos = cmds.xform(ik_joint_list[0], q=True, ws=True, t=True)
    rot_joint_pos = cmds.xform(ik_joint_list[1], q=True, ws=True, t=True)
    mid_joint_pos = cmds.xform(ik_joint_list[2], q=True, ws=True, t=True)
    low_joint_pos = cmds.xform(ik_joint_list[3], q=True, ws=True, t=True)
    end_joint_pos = cmds.xform(ik_joint_list[4], q=True, ws=True, t=True)
    pole_vec_pos = get_pole_vec_pos(root_joint_pos,rot_joint_pos, mid_joint_pos, low_joint_pos, end_joint_pos)
    return pole_vec_pos

pole_vec_pos = get_ikh_pole_vec_pos('ikHandle1')
create_loc(pole_vec_pos)

0 个答案:

没有答案