I am trying to make a button in Maya using Python that when you type in a number the for loop would loop for that many times. For example, I would put 5 in the box so the for loop would loop 5 times resulting in 50 cubes since it is for i in range (1,10).
This is my code:
import maya.cmds as cmds
import random
handle = "cubeUI"
if cmds.window(handle, exists=True):
print ("deleting old window...\n")
cmds.deleteUI(handle)
cmds.window(handle, title = "make random cubes")
cmds.columnLayout()
cmds.text(label = "amount")
amount_range_text_field = cmds.intField()
cmds.button(label = "random cube", command = "giveMeCube()")
cmds.showWindow(handle)
def giveMeCube():
cmds.polyCube()
amount_range = cmds.intField( amount_range_text_field, query=True, value = True )
for i in range (1,10):
print i
temp = cmds.polyCube()
cmds.xform(temp, t = (random.uniform(-1 *amount_range, amount_range),
random.uniform(-1 * amount_range, amount_range), random.uniform(-1 *
amount_range, amount_range) ) )
You already got the value from your spinner in the
amount_range
variable, so just use that in an another loop. You can also remove thecmds.polyCube()
at the beginning of your function since there's no reason for it. Yourfor
loop is actually iterating 9 times right now, instead just change it tofor i in range(10)
and that will iterate 10 times. You also need to indent the last portion of the code so that it's in thefor
loop.My answer is a bit complex, Green Cell answer should work for you. Here is an example on how you should think your scripts to be more 'clean' I've put some annotation to help to understand why this