Skip to content Skip to sidebar Skip to footer

Need Help Making Buttons To Perform For Loops When You Input A Number

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

Solution 1:

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

import maya.cmds as cmds
# This module can pass data throughts uifrom functools import partial
import random

# your function that have the amount set as variable that you can set easily : # giveMeCube(2) result into 20 cubesdefgiveMeCube(amount_range = 1):
    nb  = amount_range * 10for i inrange (nb):
        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) ) )

# this function is just to separate your function from ui control# so if you want to use giveMeCube in command line or in another script, you don't have your ui polluting the function# *args is here because the command flag from maya ui give a default True as last argument that need to be dismissed# most of the time, im putting the intfield query in another functiondefuiGiveMeCube(fieldname, *args):
    amount = cmds.intField(fieldname, q=True, value=True)
    giveMeCube(amount)

defshowUI():
    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(value=1, min=1)
    # you should not use string to set your function# you could have write : cmds.button(label = "random cube", command = giveMeCube)# so how partial is used : partial(function, argument1, argument2, ...etc)
    cmds.button(label = "random cube", command = partial(uiGiveMeCube, amount_range_text_field))
    cmds.showWindow(handle)

showUI()

Solution 2:

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 the cmds.polyCube() at the beginning of your function since there's no reason for it. Your for loop is actually iterating 9 times right now, instead just change it to for 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 the for loop.

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)


defgiveMeCube():
    # Remove cube creation that was here.

    amount_range = cmds.intField(amount_range_text_field, query=True, value=True)

    for j inrange(amount_range): # Use your spinner value to loop x many times.for i inrange(10):  # Just need to specify max count to get the proper amount.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)))

Post a Comment for "Need Help Making Buttons To Perform For Loops When You Input A Number"