Basic Python assistance (Adjusting the value of a loop)


#1

I’m wanting to make a slider that changes the amount of times a loop repeats…

Here is a section of the code I have and the error I have…

import maya.cmds as mc

if mc.window(ram, exists =True): #removes existing window if one already exists
mc.deleteUI(ram)

ram = mc.window(“test()”, t = “Test Window”, w = 200 ,h = 100) # Window size T= Title
mc.columnLayout(adj = True)

j = mc.intSliderGrp(l = “Width”, min =0, max =10, field=True) # field=True shows the value

mc.button(l = “Create Cube”,c=“TestLoop()”)

def TestLoop() :
for i in range(j) : #Number at the end is the amount of repeats
mc.polyCube()
mc.move(i1,i1,i*1)

mc.showWindow(ram)

ERROR is # Error: TypeError: file line 21: range() integer end argument expected, got unicode.

I’m afraid I’m not extremely knowledgeable in python so I’m not too sure what this means.


#2

The variable j just holds the name of the intSliderGrp widget, not its value. So the error is saying that you’re trying to pass in a unicode when it expects an int.

import maya.cmds as mc

# Sometimes UI widgets will pass in values so we do a catch-all with *args.
def TestLoop(*args): 
	# Query the current value of the slider.
	count = mc.intSliderGrp('mySlider', query=True, value=True)
	# Use this cound for the loop
	for i in range(count) :
		mc.polyCube()
		mc.move(i*1,i*1,i*1)
		mc.showWindow(ram)

ram = 'myWindow' # Declare the window name.

if mc.window(ram, exists =True):
	mc.deleteUI(ram)

# Name the window
mc.window(ram, t = "Test Window", w = 200 ,h = 100)
mc.columnLayout(adj = True)
# Name the intSlider widget so you can query it by name in TestLoop
mc.intSliderGrp('mySlider', l = "Width", min =0, max =10, field=True)
# Not necessary to pass in the command as a string. No parentheses needed.
mc.button(l = "Create Cube", c=TestLoop)
mc.showWindow(ram)