Python for Maya, solar system generator

Viewed 67

The end goal is to create a solar system that is semi-randomly generated, using polySphere and Curves, assigning colours, etc. Right now I've got the background size (a polyPlane with four different options in an optionsMenu) sorted out, and the next step is adding in pointLights to illuminate the background like a starry backdrop. But in trying to run the pointLights through a for loop to duplicate them results in the polyPlane getting duplicated, and not the light.

I am using Maya 2022.4, on Windows 10.

import maya.cmds as cmds
import random
from random import randint

star_count = 10

def solar_UI(): 

#delete previous iteration of UI
    if cmds.window("MainWindow", exists = True):
        cmds.deleteUI("MainWindow")

#UI Window buttons and creation
    cmds.window("MainWindow", t = "Custom Solar System Generator", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
    cmds.columnLayout("MainLayout", w = 300, cal = ('center'), adj = False, en = True)
    cmds.rowColumnLayout(nc = 2)
    
#starfield background     
    cmds.optionMenu("BackgroundCreationList", w = 250, label = "Polygon Selection:")
    cmds.menuItem(label = " ")
    cmds.menuItem(label = "Small")
    cmds.menuItem(label = "Medium")
    cmds.menuItem(label = "Large")
    cmds.menuItem(label = "XL")

    cmds.button("Create_Background", l = "Create", w = 150, align = 'middle', c = BackgroundCreation)
    cmds.setParent('..')    
    
    cmds.separator(h = 2, en = True)
    
#rename star
    cmds.rowColumnLayout(nc = 3)
    cmds.text(l = "Star name:", w = 75)
    star_text = cmds.textField('star_name', w = 100, pht = 'Type Here')    
    cmds.button(l = "Rename", w = 100, c = rename_star)
    cmds.setParent('..')

#Select and delete all
    cmds.columnLayout()
    cmds.button("Delete", l = "Delete", w = 200, align = 'middle', bgc = [0.5, 0, 0], c = DeleteButton)
    cmds.setParent('..')   

    cmds.showWindow("MainWindow")

##
## Define Functions
##

#starfield background creation and sizes
def BackgroundCreation(*args):
    cmds.shadingNode('pointLight', asLight = True)
    bgValue = cmds.optionMenu("BackgroundCreationList", q = True, v = True)
    if bgValue == "Small": 
        cmds.polyPlane(n = "Small_Background", w = 75, h = 75)
        for pointLight in cmds.ls(sl = True):
            for i in range(10):
                cmds.duplicate(pointLight, rc = True, ic = True, st = True)[0]
                for axis in ['.tx', '.tz']:
                    cmds.setAttr(pointLight + axis, random.uniform(-15,15))
                    cmds.setAttr(pointLight + '.ty', 3)
    elif bgValue == "Medium":
        cmds.polyPlane(n = "Medium_Background", w = 200, h = 200)
    elif bgValue == "Large":
        cmds.polyPlane(n = "Large_Background", w = 500, h = 500)
    elif bgValue == "XL":
        cmds.polyPlane(n = "XL_Background", w = 1000, h = 1000)

#star rename function
def rename_star():
    for obj in cmds.ls(sl = True):
        star_name = cmds.textField(obj, q = True, text = True)
        cmds.rename(obj, star_name)

#change colour, 10 selections
#def colourChange():
#    if cmds.getAttr('pSphere1.color') == 0: #Blue
#        cmds.polyColorPerVertex(r = 0.159, g = 0.3811, b = 0.86, a = 1, cdo = True)
#            
#    elif cmds.getAttr('pSphere1.color') == 1: #Red
#        cmds.polyColorPerVertex(r=.86,g=.159,b=.3811,a=1, cdo = True)
#            
#    elif cmds.getAttr('pSphere1.color') == 2: #Green
#        cmds.polyColorPerVertex(r = 0.3811, g = 0.86, b = 0.159, a = 1, cdo = True)
#cmds.scriptJob(attributeChange=['pSphere1.color',colorChange])

#delete all function
def DeleteButton(*args):
    for obj in cmds.ls(l = True):
        cmds.select(all = True)
        cmds.delete()
    
solar_UI()


#
## Alternate code, just in case
#

#        for point in range(star_count):
#            pointy  = cmds.pointLight(intensity = 10, decayRate = 2, edit = True, rgb = [1, 1, 0])
#            cmds.setAttr(pointy + '.ry', -90)
#            cmds.setAttr(pointy + '.ty', random.uniform(3, 15))
#            cmds.setAttr(pointy + '.tx', random.uniform(-75, 75))
#            cmds.setAttr(pointy + '.tz', random.uniform(-75, 75))
#            cmds.setAttr(pointy + '.useDepthMapShadows', 2)

1 Answers

So if I understand you correctly, your problem is that you want to duplicate the pointLight, not the polyplane. With ls(sl=True) the last selected objects are returned and since you create a polyPlane just before the command, this is automatically selected. The simplest solution would be to use the return values of the commands:

pointLight = cmds.shadingNode('pointLight', asLight = True)
for i in range(10):
    newLight = cmds.duplicate(pointLight, rc = True, ic = True, st = True)[0]
    cmds.setAttr(newLight + ".tx", random.uniform(-15, 15))

And so on. Keep in mind that if you duplicate or create an object it becomes automatically selected.

Related