输入数字时需要帮助按钮来执行循环 [英] Need Help Making Buttons to perform for loops when you input a number

查看:116
本文介绍了输入数字时需要帮助按钮来执行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python在Maya中创建一个按钮,当您输入数字时,for循环将循环多次.例如,我将5放入框中,因此for循环将循环5次,得到50个多维数据集,因为它是针对i的范围(1,10).

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).

这是我的代码:

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的答案应该对您有用. 这是一个有关您应该如何认为脚本更干净"的示例 我添加了一些注释来帮助理解为什么

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 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()

这篇关于输入数字时需要帮助按钮来执行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆