Error in for loop python script maya


#1

Hi guys
I have a problem running the following code
With the implementation of the loop code for all objects Attribute makes the expression only to the first object is selected
Thanks

test = cmds.ls(sl = True)

for i in test :
cmds.addAttr(ln = “teste” , at = ‘float’ , k = True ,dv = 1 )
cmds.addAttr(ln = “fre” , at = ‘float’ , k = True , dv = 1 )
mc.expression (o = i , s = ‘rotateX = sin(time*’+i+’.teste)*’+i+’.fre;’)


#2

Hi,
Try

import maya.cmds as mc
test = mc.ls(sl = True)
for i in range(len(sel)):
    mc.addAttr(ln = 'teste' , at = 'float' , k = True ,dv = 1 )
    mc.addAttr(ln = 'fre' , at = 'float' , k = True , dv = 1 )

Regards
Prem

Blog
vehicleRig


#3

addAttr adds an attribute to all the selected objects. So, if you put that in a loop it will try to add the same attribute every iteration and since it can’t, the loop fails.

Put addAttr outside the loop, so it will be executed only once before the loop. And only use the expression command inside the loop.