How to check if an object exists and then add that to a new variable

857 views
Skip to first unread message

David Clabaugh

unread,
Feb 18, 2017, 5:18:20 AM2/18/17
to Python Programming for Autodesk Maya
Hi, 
Trying to make a script I created be able to run multiple times. If I run it once it works, but the second time I get:

# Error: setAttr: Not enough data was provided. The last 0 items will be skipped.
# Traceback (most recent call last):
#   File "<maya console>", line 8, in <module>



Here is my script

import maya.cmds as mc

#Create and place Spiral DNA elements
for x in range (0,20):
    strandLName = "strandL" +str(x)
    nucleoName = "nucleo" +str(x)
    strandRName = "strandR" +str(x)
    strandL,strandHistory = mc.polySphere(name=strandLName, ch=1)
    nucleo,nucleoHistory = mc.polyCylinder(name=nucleoName, ch=1)
    mc.setAttr(nucleoName + '.translateX', 5)
    mc.setAttr(nucleoName + '.rotateZ', -90)
    mc.setAttr(nucleoName + '.scaleX', 0.5)
    mc.setAttr(nucleoName + '.scaleY', 5)
    mc.setAttr(nucleoName + '.scaleZ', 0.5)
    strandR,strandHistory = mc.polySphere(name=strandRName, ch=1)
    mc.setAttr(strandRName + '.translateX', 10)
    mc.select(deselect=1)
    
    #create empty group
    grp = mc.group(n=strandLName + 'NULL', em=1)
    mc.select(deselect=1)
    
    #Parent Elements to Group
    nucleotide = mc.parent(strandL, nucleo, strandR, grp)[0]
    
    #Move and rotate groups
    mc.setAttr(grp + '.translateX', -5.5)
    mc.xform(grp, cp=1)
    mc.setAttr(grp + ".translateY", x * 2)
    mc.setAttr(grp + ".ry", 15 * x)
    mc.select(deselect=1)





# I have 3 objects that have been grouped, I would like to make the names relative so that if the object exists it will create a new object based on what already exists. It would be great if I could do the same with position so that the strand continuously grows each time the script is run.

Michael Boon

unread,
Feb 19, 2017, 10:01:51 PM2/19/17
to Python Programming for Autodesk Maya
Maya is adding a pipe '|' to the start of each object's name, to distinguish it from the previously grouped object with the same name. If you don't include that pipe, anything you do using that name later won't work.

So any time you do something like 
nucleo,nucleoHistory = mc.polyCylinder(name=nucleoName, ch=1)
you should use the returned value (nucleo, in this case) instead of nucleoName for future operations with that object. eg:
mc.setAttr(nucleo + '.translateX', 5)

You're already doing that for grp, but you need to do it for nucleo and the strands too.
Reply all
Reply to author
Forward
0 new messages