[Maya Python] Opinions on a Light Selection/Interaction interface

Hi, everyone. I’ve just made my first interface in Maya, and I would like to know how Pythonic my code is (I do detect some redundancy, but pymel core’s selected/ls commands gave me some minor inconveniences that I learned throughout development), how memory-friendly it may be and suggestions on how I can improve the layout in Pyside.

The interface lists alphabetically all current lights in the scene with a column of checkboxes; by clicking on them, you can add/remove that light from the current selection, and selecting/deselecting directly in the scene updates the checkboxes through Maya’s SelectionChanged EventCallback; there’s also a button to select all lights in the scene, update the list of checkboxes, and set a new keyable intensity on the selected lights, either by adding, multiplying or increasing percentage.

The interface’s main files are windowTemplate.py and mayaWindow.py in this GitHub link: https://github.com/Astrocesped/Maya-Light-Selector-Modifier

Thanks in advance :D:

a couple of thoughts,

the GUI is confusing at first glance, because the list of lights looks like a list of options.

there are many examples of Light Managers floating around the internet, I suggest looking at few for ideas on layout.

http://www.creativecrash.com/maya/downloads/search?search=lights&x=0&y=0

Also the lights should be in a scroll list, this will make them look visually different and obvious they are not the settings and will stop the ui from getting too big if there are lots of lights. Also a search function would be nice

The layout is confusing. Maya’s convention is to have the label of a *Field be the left, so I assumed that “Current Lights in Scene” was referring to the IntField to the right. Also you have the intensity control broken up into 2 columns, which is confusing. My suggestion would be to (as passerby suggests) put all the lights in a textScrollList on the left, and have the intensity controls in a single column to the right

One thing I strive for - with mixed success – is to mimic the behavior of the regular maya UI. My first instinct in a case like this would be to provide a selection mechanism that looks like a Maya outliner so that users can do all the things they are expecting.

As regards code style: I’d see if you can structure the way you pick options so that there are fewer special cases. For example, you have this:


#Get Intensity attribute of this specific light
        value = pmc.getAttr('%s.intensity' % light)

        #Modifying operator's value
        if option is 0:
            value += quantity
        elif option is 1:
            value *= quantity
        elif option is 2:
            value += value * quantity / 100

        if keyframe is True:
            #cutKey deletes animation found in a certain frame
            pmc.cutKey(light, time=(currentFrame, currentFrame), attribute="intensity")

            #Set a keyframe in the current light
            pmc.setKeyframe(light, time=currentFrame, attribute="intensity", value=value)

        #Set Intensity value
        pmc.setAttr('%s.intensity' % light, value)

you could get more or less the same thing like this, but without the elifs (which are hard to maintain when things get complicated



def modified_value (value, quantity, option):
       add_option =  lambda a:  a + quantity
       mult_option = lambda a: a * quantity
       percentage_option = lambda a: a * quantity/ 100
  
       operation = {
       0: add_option
       1: mult_option
       2: percentage_option
       }[option]

       return operation(value)

 # and elsewhere 
light_value = pmc.getAttr('%s.intensity' % light)
pmc.setAttr('%s.intensity' % light, modified_value(light, option, quantity)


Ideally you can split the modifications from the GUI altogether - you always want to strive to keep code which modifies the scene out of the GUI and vice-versa so you can distinguish between UI bugs and functional ones – and also so you can script and automate without the dialog box in your way.

Thanks a lot for the feedback, everyone! This will be very helpful for a beginner :wink:

Hello, everyone.

Thanks to your advice, I’ve been looking into QListWidget, and I’m really liking it. It’s making things smooth and easy to display to display to the user.

…But this image is showing the right performance of the interface, when I have only ONE signal reacting to the List Widget selection AND the current scene selection (as in, one or the other). The Maya controller callback and the list’s callback function can’t work together, since activating both at the same time messes up the list selection (you cannot make multiple selections anymore, and the list behaves oddly overall). Only when I deactivate one, the other works flawlessly, with the list’s callback selecting the lights in the current ls selection, and the Maya Controller selecting and deselecting the items in the list automatically.

The two callback functions are connected like this:

_window.connect(_window.widgetlist, QtCore.SIGNAL('itemSelectionChanged()'), update_maya_selection)

and:

OpenMaya.MEventMessage.addEventCallback('SelectionChanged', emit_selchanged)

I didn’t have this issue with the checkboxes before… do you think it’s an issue regarding the way the QListWidget handles item selection? Or am I overloading things with both callbacks at the same time for Maya?

P.S. I’m also working on the other suggestions, thanks everyone!