Мой ответ немного сложен, ответ Green Cell должен работать на вас.Вот пример того, как вы должны думать, что ваши скрипты более «чисты». Я поместил аннотацию, чтобы понять, почему это
import maya.cmds as cmds
# This module can pass data throughts ui
from functools import partial
import random
# your function that have the amount set as variable that you can set easily :
# giveMeCube(2) result into 20 cubes
def giveMeCube(amount_range = 1):
nb = amount_range * 10
for i in range (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 function
def uiGiveMeCube(fieldname, *args):
amount = cmds.intField(fieldname, q=True, value=True)
giveMeCube(amount)
def showUI():
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()