learning Python


#1

hi im a newbie trying to learn Python and I have a stupid error.

can anyone see what is obviously wrong (I cant lol)

i greatly appreciate your help

Error: TypeError: file <maya console> line 8: Invalid flag ‘radius’


import maya.cmds as mc
import random



for i in range (1,10):
    randX=random.uniform(-20,20)
    randZ=random.uniform(-20,20)
    mc.polySphere(r=6)
    mc.move(randX,0,randZ)
    
    
sphereList=mc.ls('polySphere*')

print sphereList

for i in range (1,len(sphereList)):
    print 'polySphere'+ str(i)
    #mc.select ('polySphere'+ str(i))
    mc.setAttr('polySphere'+ str(i), e=True, radius=2)

#2

The last line is incorrect syntax. There are a few ways you could write it. This is one…

mc.setAttr(‘polySphere%i.radius’%(i), 2)

I prefer to use pymel where you can work with the class methods on the python objects that are returned by most functions.

import random
import pymel.core as pm

for i in range(1,10):
    randX=random.uniform(-20,20)
    randZ=random.uniform(-20,20)
    randRadius=random.uniform(0.25,5)
    
    sphereTransform, polySphere = pm.polySphere(r=6)
    sphereTransform.tx.set(randX)
    sphereTransform.tz.set(randZ)
    polySphere.radius.set(randRadius)
#
#
#


In this example I could have dropped the last line and simply do pm.polySphere(r=randRadius), but I thought it would be a useful example to show how pm.polySphere() returns both the transform of the new sphere as well as the polySphere creation node which can be assigned to two variables and used directly in the last 3 lines.

David