Using global variables is one way to allow the different parts of your script communicate with each other, but there's a better way. Instead of using globals, you can organize your script using custom class.
Creating a class for your script will not only allow you to easily access UI elements from various functions, but it will also make it easy to neatly contain other kinds of data, useful in more advanced scripts.
Create a new script and name it SpheresClass.py
. Add the following code:
import maya.cmds as cmds class SpheresClass: def __init__(self): self.win = cmds.window(title="Make Spheres", widthHeight=(300,200)) cmds.columnLayout() self.numSpheres = cmds.intField(minValue=1) cmds.button(label="Make Spheres", command=self.makeSpheres) cmds.showWindow(self.win) def makeSpheres(self, *args): number = cmds.intField(self.numSpheres, query=True, value=True) for i in range(0,number...