我正在尝试使用Python在Maya中创建一个按钮,当您键入一个数字时,for循环将循环多次。例如,我会在框中放置5,因此for循环将循环5次,从而产生50个立方体,因为它在范围内是1(1,10)。
这是我的代码:
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) ) )
我的答案有点复杂,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()
你已经在amount_range
变量中得到了微调器的值,所以只需在另一个循环中使用它。您也可以在函数开头删除cmds.polyCube()
,因为没有理由。您的for
循环现在实际上是迭代9次,而只是将其更改为for i in range(10)
并且将迭代10次。您还需要缩进代码的最后部分,以便它在for
循环中。
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():
# Remove cube creation that was here.
amount_range = cmds.intField(amount_range_text_field, query=True, value=True)
for j in range(amount_range): # Use your spinner value to loop x many times.
for i in range(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)))