【问题标题】:Maya Python // Error: No object matches name, result of For LoopMaya Python // 错误:没有对象匹配名称,For 循环的结果
【发布时间】:2020-06-08 20:21:41
【问题描述】:

所以我一直在 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”的组。

【问题讨论】:

  • 我也忘了提一下,你可以通过点击“generate Spine Proxy Locators”来使用脚本,然后你可以使用“spineLoc_2_PRX”来确定脊柱放样表面的长度。当您点击“构建脊柱关节”时会发生错误

标签: python for-loop rename maya pymel


【解决方案1】:

此部分出现错误:

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),然后它将按预期工作。

【讨论】:

  • 非常感谢@Green Cell,它运行良好!不过,我还有一个问题,是否可以创建一个 For Loop 来查看 spin_follicles_offset 组内部并删除它生成的曲线组?
  • 当然可以使用cmds.listRelatives获取卵泡的孩子。
  • 好吧,这是我得到的@Green Cell:grpList = cmds.listRelatives('spine_follicle*') for name in grpList: print(grpList) cmds.delete('curve*') The only问题是现在我得到了同样的错误(没有对象匹配名称)只有这次我可以使用转换标志
  • 1.开始养成使用print 来查看您的命令输出的习惯。 2.文档实际上非常好,所以检查一下,看看你可以使用哪些参数来过滤掉结果 3.你需要停止直接引用名称,因为它会使事情变得更加困难。在变量中捕获结果并改用它们。 4. 这已经偏离了原始主题,所以如果您有其他问题,请尽可能少尝试发布另一个问题。
猜你喜欢
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
相关资源
最近更新 更多