Maya Python Light control UI

Hello all,
I have a question about making control for lights.
So I’m trying to make an UI to chage light in the specific scene.
I got to work the primary visibility contorl, and color change control
but, I can’t make the intensity to work right.

Also, Can Checkbox start with its checked when I execute?

The script goes like this.


import maya .cmds as cmds
from functools import partial

class lightUI():
def init(self, winName=“window”):
self.winTitle = “Lighting room”
self.winName = winName

def create(self):
    if cmds.window(self.winName,exists=True):
        cmds.deleteUI(self.winName)
        
    cmds.window(self.winName, title=self.winTitle)
    cmds.columnLayout( adjustableColumn=True )
    
    cmds.frameLayout( label = 'File' )
    cmds.button( label = 'Import scene', command=self.importScene )
    
    cmds.frameLayout( label = 'Lighting option' )
    cmds.checkBox( 'lampCt', label='Visibility', cc=self.changeVis )
    cmds.colorSliderGrp( 'csg', label='Color', rgb=(1, 0.745, 0.635), columnWidth=(5, 30), cc=self.changeColor)
    cmds.floatSliderGrp( 'fsg', label='Intensity', field=True, minValue=0.0, maxValue=80.0, fieldMinValue=-100.0, fieldMaxValue=100.0, value=0, cc=self.changeInt )
    cmds.separator( height=10, style='singleDash' )
    cmds.separator( height=10, style='singleDash' )
    cmds.setParent( '..' )
    cmds.setParent( '..' )
    cmds.showWindow(self.winName)

##########################################################################################################################

def importScene(self,args=None):
    cmds.file( 'C:\Users\DJ\Desktop\Livingroom\scenes\dj001.ma', o=True )
    
def changeVis(self,args=None):
    if cmds.checkBox( 'lampCt', query=True, value=True ):
        cmds.setAttr( 'lampLight1.visibility', 1 )
    else:
        cmds.setAttr( 'lampLight1.visibility', 0 )
        
def changeColor(self,args=None,arg=None):
    cmds.colorSliderGrp( 'csg', e=True, query=True, vis=True)
    rgb = cmds.colorSliderGrp( 'csg', query=True, rgb=True)
    cmds.setAttr("lampLightShape1.color",rgb[0],rgb[1],rgb[2], type="double3" )
    
def changeInt(self,args=None):
    cmds.floatSliderGrp('fsg', e=True, vis=True )
    cmds.setAttr("lampLightShape1.intensity", type="dobule3" ) 

myLight = lightUI()
myLight.create()

its probably better to pass in the light, instead of naming the light specificly

You didn’t get/set a value for the intensity. And “value” for the checkbox command sets the initial state of the checkbox. See modified code below.

import maya .cmds as cmds
from functools import partial

class lightUI():
    def __init__(self, light, winName="window"):
        self.winTitle = "Lighting room"
        self.winName = winName
        self.light = light
        self.setup()
    
    def setup(self):
        if cmds.window(self.winName,exists=True):
            cmds.deleteUI(self.winName)
        
        cmds.window(self.winName, title=self.winTitle)
        cmds.columnLayout( adjustableColumn=True )
        
        cmds.frameLayout( label = 'Lighting option' )
        cmds.checkBox( 'lampCt', label='Visibility', cc=self.changeVis ,value=1)
        color = cmds.getAttr(self.light + '.color')[0]
        cmds.colorSliderGrp( 'csg', label='Color', rgb=color, columnWidth=(5, 30), cc=self.changeColor)
        intensity = cmds.getAttr(self.light + '.intensity')
        cmds.floatSliderGrp( 'fsg', label='Intensity', field=True, minValue=0.0, maxValue=80.0, fieldMinValue=-100.0,
        fieldMaxValue=100.0, cc=self.changeInt, value = intensity)
        cmds.separator( height=10, style='singleDash' )
        cmds.separator( height=10, style='singleDash' )
        cmds.setParent( '..' )
        cmds.setParent( '..' )
        cmds.showWindow(self.winName)

    def changeVis(self,args=None):
        if cmds.checkBox( 'lampCt', query=True, value=True ):
            cmds.setAttr( self.light + '.visibility', 1 )
        else:
            cmds.setAttr( self.light + '.visibility', 0 )
    
    def changeColor(self,args=None,arg=None):
        cmds.colorSliderGrp( 'csg', e=True, query=True, vis=True)
        rgb = cmds.colorSliderGrp( 'csg', query=True, rgb=True)
        cmds.setAttr(self.light + ".color",rgb[0],rgb[1],rgb[2], type="double3" )
    
    def changeInt(self,args=None):
        cmds.floatSliderGrp('fsg', e=True, vis=True )
        value = cmds.floatSliderGrp( 'fsg', query=True, value=True)
        cmds.setAttr(self.light + ".intensity", value)

light = cmds.pointLight()
lightUI(light)

For this case I’d use attrFieldSliderGrp or attrFieldGrp and attrColorSliderGrp so you don’t have do get-set logic manually.

But, how can I use it to control the existing pointlight?
what you did is that it creates a pointlight and give control.

I already have a scene that has all the lights set up.
I just need a UI to control it like a mood changer or something.

But how am I supposed to use that to an existing light.
what you did is that it creates new pointlight and control that.
I already have a scene that has all the lights set up.

What Theodox is saying is that you should just iterate all lights in your scene and add “attrControl” style controller for each light.

Here is a small example:

import maya.cmds as cmds
from functools import partial

class lightUI():
    def __init__(self, winName="window"):
        self.winTitle = "Lighting room"
        self.winName = winName
    
    def create(self):
        myLightList = cmds.ls(lights=True)
        if cmds.window(self.winName,exists=True):
            cmds.deleteUI(self.winName)
        
        cmds.window(self.winName, title=self.winTitle)
        cmds.columnLayout( adjustableColumn=True )
        
        
        for eachLight in myLightList:
            cmds.attrControlGrp( attribute='%s.visibility' % eachLight )
            cmds.attrColorSliderGrp(at='%s.color' % eachLight )
            cmds.attrFieldSliderGrp( min=0.0, max=10.0, at='%s.intensity' % eachLight )
            
         
        cmds.showWindow(self.winName)
   

myLight = lightUI()
myLight.create()

Note that this doesn’t use any function calls on the controllers, but it still works because it is connected directly to the lights attributes.