Maya Python:创建减少关节链旋转

2024-10-01 13:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我尝试构建一个python脚本,为肢体创建一个扭曲链。不幸的是,我缺乏让脚本实现我想要的行为的知识。我试图解决三个主要问题:

问题1

第118行复制了我想要的肩关节。肩部关节具有扭转所需的正确关节方向。我在下面建立的范围列表工作得很好:但它创建了全新的关节,没有任何与肩部匹配的方向。我希望I in range列表基于gui中的数字滑块而不是新关节创建肩关节的副本

问题2

我不知道如何解决这个问题:但由于关节链要模拟扭曲运动,我需要链中的每个关节以其上关节在层次中旋转的一半进行扭曲:假设有4个关节,4是链中最低的:关节4将以关节3的一半距离旋转,它将以关节2的距离旋转,以关节1距离的一半旋转

我知道我可以将multilydivide节点设置为.5以将移动减半:但我不知道如何在I in range循环中设置multilydivide节点,以便它们连接到链中的每个连续关节

问题3

一旦这些乘法除法节点连接到各自的扭曲关节:它们需要连接到在buildDemoArm函数中创建的scaleCompensate乘法除法节点的input1X。如果我把问题2解决掉,这个问题我可能可以自己解决。我知道如何调用简单的命令:但不知道如何调用范围循环

我把脚本和如何使用它的说明放在下面,供任何愿意尝试的人使用。谢谢你抽出时间

'''
import DS_limbTwistBuilder_V4
reload (DS_limbTwistBuilder_V4)
DS_limbTwistBuilder_V4.gui()

How to use this script:
    
    01.) create a python file in c:\Users\userName\Documents\maya\2020\scripts, name it DS_limbTwistBuilder_V4.py
    
    02.) create a new python tab in the maya script editor and paste the following:
        
        import DS_limbTwistBuilder_V4
        reload (DS_limbTwistBuilder_V4)
        DS_limbTwistBuilder_V4.gui()
        
    03.) highlight the script you pasted and hit enter on the numpad to run the script
    
    04.) hit "Build Demo Joints" to create the neccisary skeleton for the demo
    
    05.) set the slider to how many twist joints hit "Block Out Up Arm Twist"
    
    06.) orient the twist joints so they match the orientation of the shoulder joints
    
    07.) press "Finalise Up Arm Twist" to finish the arm twist
'''
import maya.cmds as cmds

if cmds.window("buildWin", exists =True):
    cmds.deleteUI("buildWin", window = True)

myWindow = cmds.window("buildWin",t='DS_twistOmatic_V4',rtf=1,w=100, h=100, toolbox=True)
column = cmds.columnLayout(adj=True)

def gui(*args):
    
    cmds.separator(h=9)
    cmds.button(w=300,label= "Build Demo Joints",c=buildDemoArm)
    cmds.separator(h=9)
    
    cmds.rowLayout(numberOfColumns = 3,adjustableColumn=2)
    cmds.optionMenu('sidePref',label='Select Side      ',w=168)
    cmds.menuItem( label='ct' )
    cmds.menuItem( label='rt' )
    cmds.menuItem( label='lf' )
    cmds.text(l='IncPref')
    cmds.textField('incText',it = '1',editable=True)
    cmds.setParent('..')
    
    cmds.separator(h=9)
    cmds.intSliderGrp('jntAmount',field=True, minValue=3, maxValue=100, value=5)
    
    cmds.button(w=300,label= "Block Out Up Arm Twist",c=buildUpLimbTemplate)
    cmds.separator()
    cmds.button(w=300,label= "Finalise Up Arm Twist",bgc=(0.850,0.534,0.151),c=finaliseUpLimbTwist)
    
    cmds.showWindow(myWindow)
    
def buildDemoArm(*args):
    jntNumber = cmds.intSliderGrp('jntAmount',query=True,value=True)
    namePref = cmds.optionMenu('sidePref',query=True,value=True)
    incPref = cmds.textField('incText', query=True, text=True)
    ctlName = namePref + incPref + '_'
    
    count = 4  # Amount of joints to create.
    
    steps = 1.0 / (count - 1)  # Will use count to calculate how much it should increase percentage by each iteration. Need to do -1 so the joints reach both start and end points.
    perc = 0  # This will always go between a range of 0.0 - 1.0, and we'll use this in the constraint's weights.
    
    jntChain = ""

    for i in range(count):
        count = i+1
        jnt = cmds.joint(n='demoJoint_{}_BIND'.format(count),p=[i,0,0])  # Create a new joint.
        
        perc += steps  # Increase percentage for next iteration.
        
    child = cmds.listRelatives('demoJoint_1_BIND',ad=1,f=True,children=True,type='joint')
    child.append('demoJoint_1_BIND')
    child.reverse()
    limbJnt = child
    print(child)
        
    cmds.rename(child[3], ctlName + 'wrist_BIND')
    cmds.rename(child[2], ctlName + 'elbow_BIND')
    cmds.rename(child[1], ctlName + 'shoulder_BIND')
    cmds.rename(child[0], ctlName + 'clavicle_BIND')
    
    cmds.createNode('multiplyDivide',n="scaleCompensate")
    
    
def buildUpLimbTemplate(*args):
    jntNumber = cmds.intSliderGrp('jntAmount',query=True,value=True)
    namePref = cmds.optionMenu('sidePref',query=True,value=True)
    incPref = cmds.textField('incText', query=True, text=True)
    ctlName = namePref + incPref + '_'
    
    cmds.select(cl=True)
    
    '''
    -Joint need to be properly oriented
    -joints need to be renamed
    -end joint needs to be deleted
    -joints need transforms need to be frozen
    '''
        
    count = jntNumber  # Amount of joints to create.

    start = ctlName + "shoulder_BIND"  # Change to object to start from.
    end = ctlName + "elbow_BIND"  # Chagne to object to end to.
    upTwistUp = ctlName+'upTwistUp_BIND'

    steps = 1.0 / (count - 1)  # Will use count to calculate how much it should increase percentage by each iteration. Need to do -1 so the joints reach both start and end points.
    perc = 0  # This will always go between a range of 0.0 - 1.0, and we'll use this in the constraint's weights.
    
    jntChain = ""
    
    #list all FK joints in chain
    cmds.duplicate(start, n= upTwistUp,parentOnly=True)

    for i in range(count):
        count = i+1
        jnt = cmds.joint(n=ctlName + 'upArmTwist_{}_BIND'.format(count))  # Create a new joint.
        cmds.setAttr(jnt + ".displayLocalAxis", True)  # Display its orientation.

        constraint = cmds.parentConstraint(start, jnt, weight=1.0 - perc)[0]  # Apply 1st constraint, with inverse of current percentage.
        cmds.parentConstraint(end, jnt, weight=perc)  # Apply 2nd constraint, with current percentage as-is.
        cmds.delete(constraint)  # Don't need this anymore.

        perc += steps  # Increase percentage for next iteration.

        
def finaliseUpLimbTwist(*args):
    pass
    

Tags: thetoinchildtruebindcountds