Need Help Making Buttons to perform for loops when you input a number

2024/11/15 17:24:16

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 itemp = 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) ) )
Answer

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 * 10for 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()
https://en.xdnf.cn/q/119391.html

Related Q&A

Combining multiple conditional expressions in a list comprehension

I utf-8 encode characters like \u2013 before inserting them into SQLite.When I pull them out with a SELECT, they are back in their unencoded form, so I need to re-encode them if I want to do anything w…

Arduino Live Serial Plotting with a MatplotlibAnimation gets slow

I am making a live plotter to show the analog changes from an Arduino Sensor. The Arduino prints a value to the serial with a Baudrate of 9600. The Python code looks as following: import matplotlib.pyp…

Hide lines on tree view - openerp 7

I want to hide all lines (not only there cointaner) in sequence tree view (the default view). I must hide all lines if code != foo but the attrs atribute dont work on tree views, so how can i filter/hi…

Python Append dataframe generated in nested loops

My program has two for loops. I generate a df in each looping. I want to append this result. For each iteration of inner loop, 1 row and 24 columns data is generated. For each iteration of outer loop, …

bError could not find or load main class caused by java.lang.classnotfoundation error

I am trying to read the executable jar file using python. That jar file doesnt have any java files. It contains only class and JSON files. So what I tried is from subprocess import Popen,PIPEjar_locati…

Invalid value after matching string using regex [duplicate]

This question already has answers here:Incrementing a number at the end of string(2 answers)Closed 3 years ago.I am trying to match strings with an addition of 1 at the end of it and my code gives me t…

How to fetch specific data from same class div using Beautifulsoup

I have a link : https://www.cagematch.net/?id=2&nr=448&gimmick=Adam+Pearce In this link there data in divs with same class name. But I want to fetch specifi div. Like I want to fetch current g…

Python Matplotlib Box plot

This is my dataframe:{Parameter: {0: A, 1: A, 2: A, 3: A, 4: A, 5: A, 6: A, 7: A},Site: {0: S1,1: S2,2: S1,3: S2,4: S1,5: S2,6: S1,7: S2},Value: {0: 2.3399999999999999,1: 2.6699999999999999,2: 2.560000…

How to send turtle to random position?

I have been trying to use goto() to send turtles to a random position but I get an error when running the program.I am lost on how else to do this and not sure of other ways. My current code is:t1.shap…

How to scrape all p-tag and its corresponding h2-tag with selenium?

I want to get title and content of article: example web :https://facts.net/best-survival-movies/ I want to append all p in h2[tcontent-title]and the result expected is: title=[title1, title2, title3]co…