Create sphere grid

I want to create a grid of spheres for that I wrote a script and got user values for no. of spheres, radius of sphere and distance between spheres. but when i run the script the spheres are being created over one another. I also get an error warning saying those 3 variables are not defined in global scope, and undefined variables.
‘’’
import maya.cmds as mc

global sField,rField,dField

def showUi():

global sField,rField,dField



if mc.window('Win',exists=True):

    mc.deleteUI('Win')

mywin = mc.window('Win',title='My Window',widthHeight=(300,200))

mc.columnLayout()

sField = mc.intField(minValue=1)

rField = mc.floatField(minValue=0.1)

dField = mc.intField(minValue=2)

mc.button(label="Make Spheres",command=makeSphere)

mc.showWindow(mywin)

return sField,rField,dField

def makeSphere(*args):

global sField,rField,dField



spField = mc.intField(sField,q=True,value=True)

raField = mc.floatField(rField,q=True,value=True)

diField = mc.intField(dField,q=True,value=True)

for i in range(spField):

    mc.polySphere(radius=raField)

    mc.move((i*raField*diField),0,0)

    for i in range(spField):

        mc.polySphere(radius=raField)

        mc.move(0,(i*raField*diField),0)

showUi()

‘’’

import maya.cmds as mc

global sField,rField,dField

def showUi():

    global sField,rField,dField
    
    
    
    if mc.window('Win',exists=True):
    
        mc.deleteUI('Win')
    
    mywin = mc.window('Win',title='My Window',widthHeight=(300,200))
    
    mc.columnLayout()
    
    sField = mc.intField(minValue=1)
    
    rField = mc.floatField(minValue=0.1)
    
    dField = mc.intField(minValue=2)
    
    mc.button(label="Make Spheres",command=makeSphere)
    
    mc.showWindow(mywin)
    
    return sField,rField,dField

def makeSphere(*args):

    global sField,rField,dField
   
    spField = mc.intField(sField,q=True,value=True)
    
    raField = mc.floatField(rField,q=True,value=True)
    
    diField = mc.intField(dField,q=True,value=True)
    
    print(spField, raField, diField)
    
    for i in range(spField):
    
        mc.polySphere(radius=raField)
    
        mc.move((i*raField*diField),0,0)
    
        for i in range(spField):
    
            mc.polySphere(radius=raField)
    
            mc.move(0,(i*raField*diField),0)
showUi()

Only thing I did was add a print statement, but the code “does” do something, it’s more that your intial values are ridiculously small?

just the print statement worked out .? nothing wrong with the code.?ok. but i’m not getting the grid, it just being created in a single row and a column, suppose i enter 10 no. spheres then i should get 10x10 spheresin the xy plane . that is what i’m trying to achieve.?

The print statement definitely makes no difference, I think your values were just far too small. I only put in more spheres and more offset I guess?
I didn’t get any global scope errors, but I did see warnings that the values had to be set to at least the minimum which I guess means you haven’t actually defined the start values, just the minimums.

If you want it in the xy plane and one sphere per offset amount you have some more work to do, not much but definitely something more. I’m not super versed in mc ui myself so I can’t help much in that aspect.
You should start with increasing your values like I have so you can get that far at least, the next steps is just to start adding the extra spheres in the other spots you haven’t done yet :slight_smile:

As u suggested I increased the values and implemented the code it worked for me well. so I worked on it for a while and got a decent output. similiar to the above window instead of a intField, chose the intSliderGrp in the window. I get the spheres exactly, but if I want to update the created spheres with new values then I have to delete the spheres and enter the new values and then create again. instead I want to update the exsiting spheres. could you show or suggest me a way.?

‘’’
import maya.cmds as cmds
global row,Radius_Sphere,Column,depth,dist
def showUI():

global row,Radius_Sphere,Column,depth,dist

name = "aWin"

if cmds.window(name, q=True, exists=True):

    cmds.deleteUI(name)

myWin = cmds.window(name,title="Make Spheres", widthHeight=(300, 200))

cmds.columnLayout()

cmds.rowLayout(numberOfColumns=2)

cmds.text(label="Radius of Sphere:")

Radius_Sphere = cmds.floatSliderGrp(minValue=0.1,field=True)

cmds.setParent("..")

cmds.rowLayout(numberOfColumns=2)

cmds.text(label="Number of Rows:")

row = cmds.intSliderGrp(minValue=2,field=True)

cmds.setParent("..")

cmds.rowLayout(numberOfColumns=2)

cmds.text(label="Number of Columns:")

Column = cmds.intSliderGrp(minValue=2,field=True)

cmds.setParent("..")

cmds.rowLayout(numberOfColumns=2)

cmds.text(label="Number of depth:")

depth = cmds.intSliderGrp(minValue=2,field=True)

cmds.setParent("..")

cmds.rowLayout(numberOfColumns=2)

cmds.text(label="Distance between Spheres:")

dist = cmds.intSliderGrp(minValue=2,field=True)

cmds.setParent("..")

cmds.button(label="Make Spheres", command=makeSpheres)

cmds.showWindow(myWin)

def makeSpheres(self,*args):

global row,Radius_Sphere,Column,depth,dist

rows = cmds.intSliderGrp(row, q=True,value=True)

radSphere = cmds.floatSliderGrp(Radius_Sphere, q=True,value=True)

columns = cmds.intSliderGrp(Column, q=True,value=True)

depths = cmds.intSliderGrp(depth, q=True,value=True)

distance = cmds.intSliderGrp(dist, q=True,value=True)

print (rows,radSphere,columns,depths,distance)

for i in range(rows):

    for j in range(columns):

        for k in range(depths):

            sphere, _shape = cmds.polySphere(radius=radSphere)

            cmds.move(i*distance, j*distance, k*distance, sphere) 

showUI()
‘’’

I think you’d have to start making an actual class or store them (or maybe add something to the name to make it so you can find them in the scene)
There’s lots of different ways you can approach it just depends how deep you want to go.

As a first draft, I’d make a list that you can add each sphere to as you create them. That way you “could” go and update the position of them if you wanted to.

I’d probably really start putting the variables from the UI into a class so you can stop doing the globals approach and that will help you be able to access/retain the spheres and their data etc.