9

这可能是一个相当复杂的问题,因为你们中的很多人可能不知道我正在为其编写它的软件:Autodesk Maya 2011。我正在尝试加快一个乏味缓慢的过程(装配:赋予 3d 角色的能力移动)通过编写一个自动执行它的脚本。

我会尽力说明情况。

我有一个脚本,它接受一个对象,遍历该对象的子对象,将它们存储在一个列表中,然后将初始对象放在列表的末尾,反转列表,因为它是错误的方式,然后放置初始对象在前面。

问题:有三个不同的列表,它们都是相同的对象类型,但名称不同,它们实际上是不同的对象。我的目标是通过生成名为“blendcolors”的节点将它们连接在一起。但是,如果我有一个循环来为列表 A 中的每个对象生成它们,那么我需要将它们也连接到其他列表中的对象的循环,我无法弄清楚这一点。

这是我的代码,它已经被玩过,所以就实际循环而言,它比以前更不完整。

    import maya.cmds as cmds

    def crBC(IKJoint, FKJoint, bindJoint, xQuan, switch):

        # gets children joints of the selected joint
        chHipIK = cmds.listRelatives(IKJoint, ad = True, type = 'joint')
        chHipFK = cmds.listRelatives(FKJoint, ad = True, type = 'joint')
        chHipBind = cmds.listRelatives(bindJoint, ad = True, type = 'joint')
        # list is built backwards, this reverses the list
        chHipIK.reverse()
        chHipFK.reverse()
        chHipBind.reverse()
        # appends the initial joint to the list
        chHipIK.append(IKJoint)
        chHipFK.append(FKJoint)
        chHipBind.append(bindJoint)
        # puts the last joint at the start of the list because the initial joint
        # was added to the end
        chHipIK.insert(0, chHipIK.pop())
        chHipFK.insert(0, chHipFK.pop())
        chHipBind.insert(0, chHipBind.pop())


        # pops off the remaining joints in the list the user does not wish to be blended
        chHipBind[xQuan:] = []

        chHipIK[xQuan:] = []

        chHipFK[xQuan:] = []

       # goes through the bind joints, makes a blend colors for each one, connects
       # the switch to the blender

        for a in chHipBind


            rotBC = cmds.shadingNode('blendColors', asUtility = True, n = a + 'rotate_BC')
            tranBC = cmds.shadingNode('blendColors', asUtility = True, n = a + 'tran_BC')
            scaleBC = cmds.shadingNode('blendColors', asUtility = True, n = a + 'scale_BC')

            cmds.connectAttr(switch + '.ikFkSwitch', rotBC + '.blender')
            cmds.connectAttr(switch + '.ikFkSwitch', tranBC + '.blender')
            cmds.connectAttr(switch + '.ikFkSwitch', scaleBC + '.blender')

        # goes through the ik joints, connects to the blend colors

            for b in chHipIK:
                cmds.connectAttr(b + '.rotate', rotBC + '.color1')
                cmds.connectAttr(b + '.translate', tranBC + '.color1')
                cmds.connectAttr(b + '.scale', scaleBC + '.color1')


            # connects FK joints to the blend colors

            for c in chHipFK:
                cmds.connectAttr(c + '.rotate', rotBC + '.color2')
                cmds.connectAttr(c + '.translate', tranBC + '.color2')
                cmds.connectAttr(c + '.scale', scaleBC + '.color2')

        # connects blend colors to bind joints


            cmds.connectAttr(rotBC + '.output', d + '.rotate')
            cmds.connectAttr(tranBC + '.output', d + '.translate')
            cmds.connectAttr(scaleBC + '.output', d + '.scale')                






    # executes function


    crBC('L_hip_IK', 'L_hip_FK', 'L_hip_JNT', 6, 'L_legSwitch_CTRL')
4

4 回答 4

32

我不太明白这个问题,你在找

import itertools
for a, b, c in itertools.izip(lst1, lst2, lst3):
    ...

?

它的作用是izip接受可变数量的参数并返回一个迭代器,该迭代器总是产生参数的相应项(第一次运行中的第一个参数的元组,第二次运行中的第二个参数的元组,依此类推和等等)。

于 2011-06-27T19:54:13.870 回答
1

为什么会出现“三个不同的列表都属于同一个对象类型”?为什么不能解决此问题以创建一个列表,其中所有三个对象都正确匹配?

一个简单的映射比三个并行列表更好的可能性很大。

具体来说,您需要修复 chHipIK = cmds.listRelatives(IKJoint, ad = True, type = 'joint')才能像这样工作。

chHipIK = [ { 'IK': ik } for ik in mds.listRelatives(IKJoint, ad = True, type = 'joint') ]
for i, fk in enumerate(cmds.listRelatives(FKJoint, ad = True, type = 'joint')):
    chHipIK[i]['FK']= fk
for i, bind in enumerate(cmds.listRelatives(bindJoint, ad = True, type = 'joint')):
    chHipIK[i]['FK']= bind

chHipIK是一个包含所有三个信息的映射列表。

于 2011-06-27T19:30:30.430 回答
1

@florian mayer 使用 zip 或 izip 的方式非常有效,但您也可以使用 enumerate 来获取列表计数,

list_a  = ["x", "y"]
list_b  = ["k", "j"]
list_c  = ["m", "n"]

for count, item in enumerate(list_a):
    print item, list_b[count], list_c[count]
于 2016-09-13T09:36:51.960 回答
1

Python3 答案:

for a, b, c in zip(lst1, lst2, lst3):
    ...
于 2021-01-28T20:29:22.263 回答