Maya Python:通过Triple List连接三个关节链



当涉及到IKFK肢体时,我一直在尝试基于Antony Ward方法构建自己的自动触发脚本:https://www.youtube.com/watch?v=uzHn_4ByyjY&t=10s

到目前为止,它进展得非常顺利:但我遇到了减速带。这个脚本的工作原理很简单:

  1. 选择一个具有任意数量关节的关节链,然后点击"重命名关节链">

  2. 在文本字段中设置要构建的肢体的侧前缀和名称前缀,例如:text字段1:"左"text字段2:"腿">

  3. "增量自"(Inc From(末端滑块确定IKFK切换控件在关节链上的位置。零将其置于末端,每次向上递增都会使其沿关节链向上滑动。

  4. 选择你的关节链的根并点击"构建模板">

脚本打嗝的地方是步骤5。我尝试创建一个三重列表来父约束IK和FK关节链之间的BIND关节链:但我一直得到错误

"#错误:类型错误:文件d:/Users/Name/Documents/maya/2020/scripts\DS_03C_autoHumanArm.py第217行:只能将列表(而不是"str"(连接到列表#">

我需要它来对起始关节、结束关节进行父约束,无论有多少关节位于它们各自的IK和FK副本之间,然后将反向节点的outputX连接到生成的父约束的w1属性,并将生成的IKFK切换的IK_toggle连接到生成父约束的w0。问题发生在201线附近:

就我迄今为止所尝试的而言,如果这里详细介绍的双列表zip技术:Maya:使用父约束连接两个关节链,那么它大多是一种变体但我不知道如何将其适用于两个以上的联合链

'''
import DS_03C_autoHumanArm
reload (DS_03C_autoHumanArm)
DS_03C_autoHumanArm.gui()
'''
import maya.cmds as cmds
if cmds.window("moduleWin", exists =True):
cmds.deleteUI("moduleWin", window = True)
myWindow = cmds.window("moduleWin",t='DS_joint_renamerV1',rtf=1,w=100, h=100, toolbox=True)
column = cmds.columnLayout(adj=True)
def gui():  
#cmds.button(label='Print Instructions(Check Script Editor)',c=printInstructions)
cmds.rowLayout(numberOfColumns = 3,adjustableColumn=2)
cmds.textField('prefixText',it = 'lf',editable=True)
cmds.textField('limbText',it='arm',editable=True)
cmds.setParent('..')
cmds.button( label="Rename Joint Chain",c=DS_module_rename)
cmds.separator(h=9)
cmds.columnLayout(adj=True)
cmds.optionMenu('controlShape', label='Control Shape')
cmds.menuItem( label='cog' )
cmds.menuItem( label='cube' )
cmds.menuItem( label='circle' )
cmds.setParent('..')
cmds.intSliderGrp('incDwnChain',
label='Inc From End',
min=0,
max=10,
value=1,
field=True,
columnWidth=[(1,80),(2,40),(3,150)])
cmds.colorIndexSliderGrp('controlColor',
label='Control Color',
min=0,
max=31,
value=1,
columnWidth=[(1,80),(2,40),(3,150)])
cmds.button( label="Build Template",c=buildTemplate)
cmds.separator(h=9)
cmds.button( label="Finalise Limb",c=finaliseArm)
cmds.separator(h=9)
cmds.setParent('..')
cmds.showWindow(myWindow)
def DS_module_rename(*args):
namePref = cmds.textField('prefixText', query=True, text=True)
limbPref = cmds.textField('limbText', query=True, text=True)
ctlName = namePref+'_'+limbPref
#list all joints in chain, this list will be refrenced by all the commands beneath it
root = cmds.ls(sl=True)[0]
child = cmds.listRelatives(root,ad=1,type='joint')
limbJnt = child
#rename the arm joints
for j, name in enumerate(child):
cmds.rename(name,ctlName + 'AJ{0}_BIND_JNT'.format(len(child)-j))
print(child)
root = cmds.ls(sl=True)[0]
child = cmds.listRelatives(root,ad=1,f=True,children=True,type='joint')
cmds.rename(child[0],ctlName +'AJ_BIND_END_JNT')
cmds.rename(root,ctlName +'AJ_BIND_START_JNT')
def buildTemplate(*args):
namePref = cmds.textField('prefixText', query=True, text=True)
limbPref = cmds.textField('limbText', query=True, text=True)
ctlName = namePref+'_'+limbPref
shapePref = cmds.optionMenu('controlShape',query=True,value=True)
incPref = cmds.intSliderGrp('incDwnChain',query=True,value=True)
colorPref = cmds.colorIndexSliderGrp('controlColor',query=True,value=True) #this variable links to the color slider group in the GUI
colorPref = colorPref -1 # this reverses the first variable to ensure you are getting the proper color from the color selector
root = cmds.ls(sl=True)[0] # adding a zero bracket makes sure it counts the head of the herarchy too
#root = cmds.ls(sl=True)[-1] # the brackets negative one gets the end of a list
child = cmds.listRelatives(root,ad=1,type='joint')
limbJnt = child
ctl = ctlName+'_ikfk_toggle'
if shapePref == 'cog':
cmds.circle(n=ctl,nr = (90,0,0))
elif shapePref == 'cube':
cmds.circle(n=ctl,nr = (90,0,0))
elif shapePref == 'circle':
cmds.circle(n=ctl,nr = (90,0,0))
cmds.addAttr(ctl,
ln='IK_Toggle',
defaultValue=1.0,
minValue=0,
maxValue=1,
attributeType='float', 
keyable=True)
cmds.addAttr(ctl,
ln='CONTROLS',
at="enum",
en="---------------:")
cmds.setAttr(ctl + '.CONTROLS',
k=True,
lock=True)
cmds.addAttr(ctl,
ln='rotateAll',
attributeType='float', 
keyable=True)
cmds.addAttr(ctl,
ln='Spread',
attributeType='float', 
keyable=True)
cmds.addAttr(ctl,
ln='Scratch',
attributeType='float', 
keyable=True)
cmds.group(n=ctlName+ 'AttrOrient_GRP')
#forLoop/list increment to last of joint chain to place the IKFK toggle
cmds.select(ctlName+'AJ1_BIND_JNT')
#snap ikfk toggle to proper point on the joint chain without parent constraint
baseCon = cmds.parentConstraint(child[incPref],ctlName+'AttrOrient_GRP',mo=False)
#set color of control
cmds.setAttr(ctl + '.overrideEnabled', 1)
cmds.setAttr(ctl + '.overrideColor', colorPref)#create a textfield for manually enterring the number of the color
cmds.createNode('reverse',n= ctlName + '_reverseNode')
cmds.connectAttr(ctl + '.IK_Toggle', ctlName + '_reverseNode.inputX' )
#list all FK joints in chain
fkChain = cmds.duplicate(ctlName +'AJ_BIND_START_JNT', n= ctlName +'AJ_FK_START_JNT')
fkList = cmds.listRelatives(ctlName +'AJ_FK_START_JNT', ad=True,pa=True)
for fkn, name in enumerate(fkList):
cmds.rename(name, ctlName +'AJ{0}_FK_JNT'.format(len(fkList)-fkn))
#select FK chain, then,set joints size for easy grabbing on FK chain
cmds.select(ctlName +'AJ_FK_START_JNT')
fkRoot = cmds.ls(sl=True)[0] 
fkChild = cmds.listRelatives(ctlName +'AJ_FK_START_JNT', ad=True,pa=True)
fkChild.append(fkRoot)
for r in fkChild:
cmds.setAttr(r + '.radius', 2.0)
ikChain = cmds.duplicate(ctlName +'AJ_BIND_START_JNT', n= ctlName +'AJ_IK_START_JNT')
ikList = cmds.listRelatives(ctlName +'AJ_IK_START_JNT', ad=True,pa=True)
for fkn, name in enumerate(ikList):
cmds.rename(name, ctlName +'AJ{0}_FK_JNT'.format(len(ikList)-fkn))
#select FK chain, then,set joints size for easy grabbing on FK chain
cmds.select(ctlName +'AJ_IK_START_JNT')
ikRoot = cmds.ls(sl=True)[0] 
ikChild = cmds.listRelatives(ctlName +'AJ_IK_START_JNT', ad=True,pa=True)
ikChild.append(ikRoot)
for r in ikChild:
cmds.setAttr(r + '.radius', 2.5)
def finaliseArm(*args):
namePref = cmds.textField('prefixText', query=True, text=True)
limbPref = cmds.textField('limbText', query=True, text=True)
ctlName = namePref+'_'+limbPref
ctl = namePref+limbPref+'_ikfk_toggle'
ikJntChain=cmds.listRelatives(ctlName +'AJ_IK_START_JNT',ad=1,type='joint')
ikJntChain.append(ctlName +'AJ_IK_START_JNT')
ikJntChain.reverse()
ikLimbJnt = ikJntChain
print(ikJntChain)
FKJntChain=cmds.listRelatives(ctlName +'AJ_FK_START_JNT',ad=1,type='joint')
FKJntChain.append(ctlName +'AJ_IK_START_JNT')
FKJntChain.reverse()
FKLimbJnt = FKJntChain
print(FKJntChain)
boundJntChain=cmds.listRelatives(ctlName +'AJ_BIND_START_JNT',ad=1,type='joint')
boundJntChain.append(ctlName +'AJ_BIND_START_JNT')
boundJntChain.reverse()
boundLimbJnt = boundJntChain
print(boundJntChain)
for ik_chain,fk_chain,bound_chain in zip(ikJntChain,FKJntChain,boundJntChain):
spineCons = cmds.parentConstraint(ik_chain,fk_chain,bound_chain,mo=False)
cmds.connectAttr(ctlName + '_reverseNode.outputX',spineCons+'.w1')
cmds.connectAttr(ctl + '.IK_Toggle',spineCons+'.w0')

谢谢你的帮助

您遇到的错误是抱怨您试图用字符串连接列表,而Python无法做到这一点。具体来说,spineCons是列表,您可以简单地访问它的第一个索引,如spineCons[0]

但一旦你解决了这个问题,就会有更多的问题。一种是尝试连接'.w1'以连接到父约束的权重属性,但这不是属性的正确名称。您可以通过调用cmds.parentConstraint(spineCons[0], q=True, weightAliasList=True)来获取其属性名称。

另一个问题是,您有两个地方忘记将IK更改为FK,这导致在模板中构建了2个FK链!

最后,当您尝试连接ctl + '.IK_Toggle'时,最后一个问题就在最后,因为该对象没有存在的属性。我没有在这个问题上挖掘太多,但要修复它,你只需要传递正确的对象。

这是带有修复程序的脚本。你可以在FIXME上搜索,看看我在哪里留下了评论:

'''
import DS_03C_autoHumanArm
reload (DS_03C_autoHumanArm)
DS_03C_autoHumanArm.gui()
'''
import maya.cmds as cmds
if cmds.window("moduleWin", exists =True):
cmds.deleteUI("moduleWin", window = True)
myWindow = cmds.window("moduleWin",t='DS_joint_renamerV1',rtf=1,w=100, h=100, toolbox=True)
column = cmds.columnLayout(adj=True)
def gui():  
#cmds.button(label='Print Instructions(Check Script Editor)',c=printInstructions)
cmds.rowLayout(numberOfColumns = 3,adjustableColumn=2)
cmds.textField('prefixText',it = 'lf',editable=True)
cmds.textField('limbText',it='arm',editable=True)
cmds.setParent('..')
cmds.button( label="Rename Joint Chain",c=DS_module_rename)
cmds.separator(h=9)
cmds.columnLayout(adj=True)
cmds.optionMenu('controlShape', label='Control Shape')
cmds.menuItem( label='cog' )
cmds.menuItem( label='cube' )
cmds.menuItem( label='circle' )
cmds.setParent('..')
cmds.intSliderGrp('incDwnChain',
label='Inc From End',
min=0,
max=10,
value=1,
field=True,
columnWidth=[(1,80),(2,40),(3,150)])
cmds.colorIndexSliderGrp('controlColor',
label='Control Color',
min=0,
max=31,
value=1,
columnWidth=[(1,80),(2,40),(3,150)])
cmds.button( label="Build Template",c=buildTemplate)
cmds.separator(h=9)
cmds.button( label="Finalise Limb",c=finaliseArm)
cmds.separator(h=9)
cmds.setParent('..')
cmds.showWindow(myWindow)
def DS_module_rename(*args):
namePref = cmds.textField('prefixText', query=True, text=True)
limbPref = cmds.textField('limbText', query=True, text=True)
ctlName = namePref+'_'+limbPref
#list all joints in chain, this list will be refrenced by all the commands beneath it
root = cmds.ls(sl=True)[0]
child = cmds.listRelatives(root,ad=1,type='joint')
limbJnt = child
#rename the arm joints
for j, name in enumerate(child):
cmds.rename(name,ctlName + 'AJ{0}_BIND_JNT'.format(len(child)-j))
print(child)
root = cmds.ls(sl=True)[0]
child = cmds.listRelatives(root,ad=1,f=True,children=True,type='joint')
cmds.rename(child[0],ctlName +'AJ_BIND_END_JNT')
cmds.rename(root,ctlName +'AJ_BIND_START_JNT')
def buildTemplate(*args):
namePref = cmds.textField('prefixText', query=True, text=True)
limbPref = cmds.textField('limbText', query=True, text=True)
ctlName = namePref+'_'+limbPref
shapePref = cmds.optionMenu('controlShape',query=True,value=True)
incPref = cmds.intSliderGrp('incDwnChain',query=True,value=True)
colorPref = cmds.colorIndexSliderGrp('controlColor',query=True,value=True) #this variable links to the color slider group in the GUI
colorPref = colorPref -1 # this reverses the first variable to ensure you are getting the proper color from the color selector
root = cmds.ls(sl=True)[0] # adding a zero bracket makes sure it counts the head of the herarchy too
#root = cmds.ls(sl=True)[-1] # the brackets negative one gets the end of a list
child = cmds.listRelatives(root,ad=1,type='joint')
limbJnt = child
ctl = ctlName+'_ikfk_toggle'
if shapePref == 'cog':
cmds.circle(n=ctl,nr = (90,0,0))
elif shapePref == 'cube':
cmds.circle(n=ctl,nr = (90,0,0))
elif shapePref == 'circle':
cmds.circle(n=ctl,nr = (90,0,0))
cmds.addAttr(ctl,
ln='IK_Toggle',
defaultValue=1.0,
minValue=0,
maxValue=1,
attributeType='float', 
keyable=True)
cmds.addAttr(ctl,
ln='CONTROLS',
at="enum",
en="---------------:")
cmds.setAttr(ctl + '.CONTROLS',
k=True,
lock=True)
cmds.addAttr(ctl,
ln='rotateAll',
attributeType='float', 
keyable=True)
cmds.addAttr(ctl,
ln='Spread',
attributeType='float', 
keyable=True)
cmds.addAttr(ctl,
ln='Scratch',
attributeType='float', 
keyable=True)
cmds.group(n=ctlName+ 'AttrOrient_GRP')
#forLoop/list increment to last of joint chain to place the IKFK toggle
cmds.select(ctlName+'AJ1_BIND_JNT')
#snap ikfk toggle to proper point on the joint chain without parent constraint
baseCon = cmds.parentConstraint(child[incPref],ctlName+'AttrOrient_GRP',mo=False)
#set color of control
cmds.setAttr(ctl + '.overrideEnabled', 1)
cmds.setAttr(ctl + '.overrideColor', colorPref)#create a textfield for manually enterring the number of the color
cmds.createNode('reverse',n= ctlName + '_reverseNode')
cmds.connectAttr(ctl + '.IK_Toggle', ctlName + '_reverseNode.inputX' )
#list all FK joints in chain
fkChain = cmds.duplicate(ctlName +'AJ_BIND_START_JNT', n= ctlName +'AJ_FK_START_JNT')
fkList = cmds.listRelatives(ctlName +'AJ_FK_START_JNT', ad=True,pa=True)
for fkn, name in enumerate(fkList):
cmds.rename(name, ctlName +'AJ{0}_FK_JNT'.format(len(fkList)-fkn))
#select FK chain, then,set joints size for easy grabbing on FK chain
cmds.select(ctlName +'AJ_FK_START_JNT')
fkRoot = cmds.ls(sl=True)[0] 
fkChild = cmds.listRelatives(ctlName +'AJ_FK_START_JNT', ad=True,pa=True)
fkChild.append(fkRoot)
for r in fkChild:
cmds.setAttr(r + '.radius', 2.0)
ikChain = cmds.duplicate(ctlName +'AJ_BIND_START_JNT', n= ctlName +'AJ_IK_START_JNT')
ikList = cmds.listRelatives(ctlName +'AJ_IK_START_JNT', ad=True,pa=True)
for fkn, name in enumerate(ikList):
cmds.rename(name, ctlName +'AJ{0}_IK_JNT'.format(len(ikList)-fkn))  # FIXME: This should be ik!
#select FK chain, then,set joints size for easy grabbing on FK chain
cmds.select(ctlName +'AJ_IK_START_JNT')
ikRoot = cmds.ls(sl=True)[0] 
ikChild = cmds.listRelatives(ctlName +'AJ_IK_START_JNT', ad=True,pa=True)
ikChild.append(ikRoot)
for r in ikChild:
cmds.setAttr(r + '.radius', 2.5)
def finaliseArm(*args):
namePref = cmds.textField('prefixText', query=True, text=True)
limbPref = cmds.textField('limbText', query=True, text=True)
ctlName = namePref+'_'+limbPref
ctl = namePref+limbPref+'_ikfk_toggle'
ikJntChain=cmds.listRelatives(ctlName +'AJ_IK_START_JNT',ad=1,type='joint')
ikJntChain.append(ctlName +'AJ_IK_START_JNT')
ikJntChain.reverse()
ikLimbJnt = ikJntChain
print(ikJntChain)
FKJntChain=cmds.listRelatives(ctlName +'AJ_FK_START_JNT',ad=1,type='joint')
FKJntChain.append(ctlName +'AJ_FK_START_JNT')  # FIXME: This was using IK before!
FKJntChain.reverse()
FKLimbJnt = FKJntChain
print(FKJntChain)
boundJntChain=cmds.listRelatives(ctlName +'AJ_BIND_START_JNT',ad=1,type='joint')
boundJntChain.append(ctlName +'AJ_BIND_START_JNT')
boundJntChain.reverse()
boundLimbJnt = boundJntChain
print(boundJntChain)
for ik_chain,fk_chain,bound_chain in zip(ikJntChain,FKJntChain,boundJntChain):
spineCons = cmds.parentConstraint(ik_chain,fk_chain,bound_chain,mo=False)
weightAttrs = cmds.parentConstraint(spineCons[0], q=True, weightAliasList=True)  # FIXME: Get constraint weight attribute names.
cmds.connectAttr(ctlName + '_reverseNode.outputX',spineCons[0] + '.' + weightAttrs[1])  # FIXME: `spineCons` is a list, so make sure to access its first index.
cmds.connectAttr(ctl + '.IK_Toggle',spineCons[0] + '.' + weightAttrs[0])  # FIXME: This will error because `IK_Toggle` doesn't exist

相关内容

最新更新