Dasteel
Dasteel

Reputation: 103

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

So I've been playing around with for loops in Maya: but I've run into a peculiar error: after I try to run my script apparently an error appears in line 72 saying "// Error: No object matches name" I dont get it. I'm not asking for an object, I dont know why it's looking for an object. This is the script:

'''
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

I want to know whats causing this error, and if possible I would also like to create a for loop to delete the groups named "curve" beneath the follicles.

Upvotes: 1

Views: 3573

Answers (1)

Green Cell
Green Cell

Reputation: 4777

The error occurs in this portion:

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

If you add a print statement to print name and run cmds.objExists on it, it'll give you:

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 is unable to find that last object, and thus, throws that error. Notice that the last one is a shape? That's because when you're calling cmds.ls('spineRibbon_loftSurfaceFollicle*') it's returning you a list of the transforms AND shapes. So as you're iterating through your loop and renaming each follicle, Maya is also auto-renaming its shape. Then eventually your loop iterates to the first shape, which was already auto-renamed, and fails.

The fix is easy: just make sure you grab the follicle's transform, not its shapes! You can do this by replacing line 72 with this: folList = cmds.ls('spineRibbon_loftSurfaceFollicle*', transforms=True), then it will work as expected.

Upvotes: 2

Related Questions