Maya Python // Error: No object matches name, result of For Loop

Maya Python // Error: No object matches name, result of For Loop

所以我一直在 Maya 中使用 for 循环:但是我 运行 遇到了一个奇怪的错误:在我尝试 运行 我的脚本后,第 72 行显然出现了错误说“//错误:没有对象匹配名称”我不明白。我不是要对象,我不知道它为什么要找对象。这是脚本:

'''
import DS_spineOmatic_ribbonV1
reload (DS_spineOmatic_ribbonV1)
DS_spineOmatic_ribbonV1.gui()
'''

import re
import maya.cmds as cmds
import maya.mel as mel

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

myWindow = cmds.window("spineWin",t='DS_spineOmatic_V1',w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)

'''
To DO:
    -You're going to have a series of scrips splitting into an IKFK spine and a ribon spine: this script will build the ribbon spine
'''

def gui():

    cmds.button( label="Generate Spine Proxy Locators", c = buildProxies)
    cmds.separator( w=200, h=3)
    cmds.button( label="Build Spine Joints", c = buildRibbon)
    cmds.separator( w=200, h=9)

    cmds.setParent('..')
    cmds.showWindow(myWindow)

def buildProxies(*args):
    locAmount = 2
    for i in range(locAmount):
        countLoc = i+1
        spaceLoc = cmds.spaceLocator(n = 'spineLoc_{}_PRX'.format(countLoc), p = [0,i*2.5,0])
        cmds.makeIdentity(spaceLoc, a=1, t=1)
        mel.eval('CenterPivot;')

    #create spine control curves
    cmds.curve(n='upLoftTemp', d=1,p=[(-1,0,0),(1,0,0)])
    cmds.curve(n='dwnLoftTemp', d=1,p=[(-1,0,0),(1,0,0)])
    cmds.curve(n = 'torso_CTRL', d=1, p=[(-2,0,0.7),(-2,0,1.4),(-3.3,0,0),(-2,0,-1.4),(-2,0,-0.7),(-1,0,-1),(-0.7,0,-2),(-1.4,0,-2),(0,0,-3.3),(1.4,0,-2),(0.7,0,-2),(1,0,-1),(2,0,-0.7),(2,0,-1.4),(3.3,0,0),(2,0,1.4),(2,0,0.7),(1,0,1),(0.7,0,2),(1.4,0,2),(0,0,3.3),(-1.4,0,2),(-0.7,0,2),(-1,0,1),(-2,0,0.7),(-2,0,1.4)])
    cmds.parent('spineLoc_2_PRX','spineLoc_1_PRX')
    #change following line to make duplicates of the spine when you figure out the spine joint linkup
    cmds.select(cl=True)
    cmds.joint(n='spine_bound1')


def buildRibbon(*args):

    cmds.select(cl=True) #this line clears your selection

    #create the ribbon
    cmds.pointConstraint('spineLoc_2_PRX','upLoftTemp',mo=False)
    cmds.pointConstraint('spineLoc_1_PRX','dwnLoftTemp',mo=False)
    cmds.loft('upLoftTemp','dwnLoftTemp',ch=False)
    cmds.rename('loftedSurface1','spineRibbon_loftSurface')
    cmds.rebuildSurface('spineRibbon_loftSurface',su=6,sv=1)
    mel.eval('DeleteHistory;')
    cmds.select('spineRibbon_loftSurface')

    #create hair splines
    mel.eval('createHair 7 1 10 0 0 1 0 5 0 1 2 1;')
    cmds.delete('hairSystem1')
    cmds.delete('pfxHair1')
    cmds.delete('nucleus1')
    cmds.rename('hairSystem1Follicles', 'spine_follicles_offset')

    #by putting a star at the end of the follicle in cmds.ls you've told maya to list everything starting with that name
    folName = 'spine_follicle'
    folList = cmds.ls('spineRibbon_loftSurfaceFollicle*')
    for name in folList:
        cmds.rename(name,folName)


    #delete proxy locators
    cmds.delete('spineLoc_1_PRX')
    cmds.delete('upLoftTemp')
    cmds.delete('dwnLoftTemp')

    #create waist curve

我想知道是什么导致了这个错误,如果可能的话我还想创建一个 for 循环来删除毛囊下面名为 "curve" 的组。

错误发生在这部分:

for name in folList:
    cmds.rename(name, folName)

如果你添加一个打印语句来打印 name 和 运行 cmds.objExists,它会给你:

True spineRibbon_loftSurfaceFollicle50
True spineRibbon_loftSurfaceFollicle1750
True spineRibbon_loftSurfaceFollicle3350
True spineRibbon_loftSurfaceFollicle5050
True spineRibbon_loftSurfaceFollicle6650
True spineRibbon_loftSurfaceFollicle8350
True spineRibbon_loftSurfaceFollicle9950
False spineRibbon_loftSurfaceFollicleShape50

Maya 找不到最后一个对象,因此抛出该错误。注意到最后一个是形状了吗?那是因为当您调用 cmds.ls('spineRibbon_loftSurfaceFollicle*') 时,它会返回一个包含变换和形状的列表。因此,当您遍历循环并重命名每个毛囊时,Maya 也会自动重命名其形状。然后最终你的循环迭代到已经自动重命名的第一个形状并失败。

解决方法很简单:只需确保抓住毛囊的变形,而不是它的形状!您可以将第 72 行替换为:folList = cmds.ls('spineRibbon_loftSurfaceFollicle*', transforms=True),然后它将按预期工作。