Hi everyone, I’m brand new to Python and pretty much new to programming (I’ve done some very basic c# before). I’m trying to make a little tool to change the intensity of selected lights, and what I’m stuck on at the moment is getting all of the selected lights to display in my UI. I’ve made it work to the point where it shows one of the selected lights, but I don’t know how to make it show all of them.
I’d really like it to list them downwards in the field, and I’ve looked at the Maya Python Command Reference extensively but I think I need someone to point me in the right direction. Here’s my code:
import maya.cmds as mc
#Create a list of the lights in the scene
lights = cmds.ls(lights=True)
#Instantiate the UI
def createUI(pWindowTitle, pApplyCallback):
windowID = 'ba_lightIntensityWindow'
if cmds.window(windowID, exists=True):
cmds.deleteUI(windowID)
cmds.window(windowID, title=pWindowTitle, sizeable=True, resizeToFitChildren=True)
#Layout the columns
cmds.rowColumnLayout(numberOfColumns=3, columnWidth=[(1,75), (2,60), (3,60)], columnOffset=[(20,'right',3)])
cmds.text(label='Selected lights:',)
cmds.textFieldGrp(nbg=True, w=300, editable=False, tx=lights[0])
cmds.separator(h=10, style='none')
cmds.text(label='Intensity:')
userIntens = cmds.textField(text='')
#Bunch of separators to make a gap between the field and buttons
cmds.separator(h=10, style='none')
cmds.separator(h=10, style='none')
cmds.separator(h=10, style='none')
cmds.separator(h=10, style='none')
cmds.separator(h=10, style='none')
cmds.button(label='Apply', command=pApplyCallback)
def cancelCallback(*pArgs):
if cmds.window(windowID, exists=True):
cmds.deleteUI(windowID)
cmds.button(label='Cancel', command=cancelCallback)
cmds.showWindow()
def applyCallback(*pArgs):
print 'Apply button pressed.'
createUI ('ba_lightIntensity', applyCallback)
I’d really appreciate any help anyone can give me, thanks !
It’s perfectly OK to stick a loop into your layout code to handle repetitive layout tasks. You just need to remember to call setParent() to close out any layout elements you might have created inside the loop and reset the location of the next element.
Here’s a really miminalist example:
def create_closure(light):
# return a callback function pre-bound to the name of <light>
def closure(*args):
print light, args
cmds.setAttr(light + ".intensity", args[0])
return closure
w = cmds.window()
c = cmds.columnLayout(adj=True)
for light in cmds.ls(sl=True):
cmds.rowLayout(nc =2)
cmds.nameField(object = light)
cmds.floatSlider(v = cmds.getAttr(light + ".intensity"), cc = create_closure(light))
cmds.setParent("..")
cmds.button("Close", c = lambda _ : cmds.deleteUI(w))
cmds.showWindow(w)
Two things to note:
The ‘create closure’ function here returns a function, bound to the name of the light you’ve passed in. That way each slider gets a custom callback, pre-bound to the correct light. That’s why I didn’t need an ‘apply’ button, moving the slider applies the change.
The ‘lambda’ creates another custom callback, this time bound to the name of the window. Lambdas are easy but they don’t work in loops
[QUOTE=Theodox;26221]It’s perfectly OK to stick a loop into your layout code to handle repetitive layout tasks. You just need to remember to call setParent() to close out any layout elements you might have created inside the loop and reset the location of the next element.
Here’s a really miminalist example:
def create_closure(light):
# return a callback function pre-bound to the name of <light>
def closure(*args):
print light, args
cmds.setAttr(light + ".intensity", args[0])
return closure
w = cmds.window()
c = cmds.columnLayout(adj=True)
for light in cmds.ls(sl=True):
cmds.rowLayout(nc =2)
cmds.nameField(object = light)
cmds.floatSlider(v = cmds.getAttr(light + ".intensity"), cc = create_closure(light))
cmds.setParent("..")
cmds.button("Close", c = lambda _ : cmds.deleteUI(w))
cmds.showWindow(w)
Two things to note:
The ‘create closure’ function here returns a function, bound to the name of the light you’ve passed in. That way each slider gets a custom callback, pre-bound to the correct light. That’s why I didn’t need an ‘apply’ button, moving the slider applies the change.
The ‘lambda’ creates another custom callback, this time bound to the name of the window. Lambdas are easy but they don’t work in loops[/QUOTE]