How to update list of buttons

94 views
Skip to first unread message

Maria Jesus Penella

unread,
Sep 5, 2015, 12:26:52 PM9/5/15
to Python Programming for Autodesk Maya

I'm trying to update a list of lights on the scene. For this I keep the last list and delete all the elements, and create new ones based on the new list... It deletes all the elements just fine, but when I try to update with the new ones.. it just crashes and says:

RuntimeError: Object's name 'textName1' is not unique. #

It's suposed to be deleted, isn't it??

this is the code of this part..


def updateList(self, name, totalLgt, *args):
    print "update %s" % name
    #create new buttons

    self.text = pm.text('textName'+str(totalLgt), label="Name: %s" %name)
    self.button = pm.button('btnName'+str(totalLgt), label="ON" ,command = lambda *args: self.turnOn(totalLgt, name))
    self.button = pm.button('btnNameS'+str(totalLgt), label="SOLO", command = lambda *args: self.turnSolo(totalLgt, name))

def update(self, totalLgt,lis, *args):
   totalLgt = 0
   for lgt in lis:
        totalLgt += 1

   i = 1
   print "updatea"
   print lis
   for i in range(totalLgt):
      if (pm.window('textName'+str(i+1), exists=True)):
         pm.deleteUI('textName'+str(i+1), control = True)
         pm.deleteUI('btnName'+str(i+1), control = True ) 
         pm.deleteUI('btnNameS'+str(i+1), control = True )

   lisN = pm.ls(type='light') 
   totalLgt = 0 
   #list all lights in scene
   for lgt in lisN: 
      totalLgt += 1
      nameLgt = lgt.longName()
      name = nameLgt.split("|")[1]
      self.updateList(name, totalLgt)

How should I do it so pressing update button I can get the new lights of the scene?? 

Thanks 1

Justin Israel

unread,
Sep 5, 2015, 4:15:47 PM9/5/15
to Python Programming for Autodesk Maya
From the looks of this code, it a pretty flimsy way to manage the list of buttons, by trying to iterate from 0-(nLights-1), and deleting a bunch of controls in each window with dynamically generated names. What happens if you previously had 10 lights and now you have 8? Does it leave behind a window/controls?

How do you create your window? Are you sure you want to create a dynamically named window, as opposed to having a static name that you can always look for, and update the light count? If you pick something unique, you should be able to keep dealing with it ("fooTools_lightManager_window").

Another suggestion is to delete a single layout in the window, instead of a bunch of named controls. Why can't the controls all be named statically, as opposed to a number based on the last light count? If you are trying to update a window (windows?) with the new current list of lights, why not just have one window, with a set of controls that can change? If you delete a layout with an expected name, it will clear the controls in it, and you can just create a new layout with new controls under the parent you want.

You are referring to relative control names. This means you may have naming conflicts when you try to operate on them. Your window elements actually look more like this:

textName8
textName8|someLayout
textName8|someLayout|textName8
textName8|someLayout|btnName8
textName8|someLayout|btnNameS8

If you make sure to deal with full names, it would be safer. 

Also, is it a typo that in your updateList(), you save the solo button to self.button, replacing the previous button reference?

If you can't get the bug resolved, you may need to provide a small, complete example that shows the problem, so that we can give it a test and pin-point the actual location of the bug. But the best I can say right now is that you have an issue in managing your dynamically named controls, using relative control names.

Justin



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a6036cfe-30e3-4b2a-9b5f-1bbd35bb6450%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maria Jesus Penella

unread,
Sep 5, 2015, 5:21:56 PM9/5/15
to Python Programming for Autodesk Maya
Thanks !  I think my problem it that I don't know exactly how layout and parents works in python... i'm just improvising a bit... Now I have created a layout just for the buttons and it deletes them ok, but if I do any change ( add or delete light ) I have a blank space where the buttons should be.. I attach this time the whole code for this to work =)

import maya.cmds as cmds
import maya.mel as mel
import pymel.core as pm

class createWindowClass(object): 
    def __init__(self, *args):
        pass
    def show(self):
        self.createWindow()
        
    def turnOn(self, totalLgt, name, *args):
        # turns on/off the light  
    def turnSolo(self,totalLgt, name, *args):
        # just that light affect
               
    def updateList(self, name, lgt, totalLgt, *args):
       
        #create new buttons
        self.text = pm.text(name, label="Name: %s" %name)
        visibilityLgt = pm.getAttr(lgt+'.visibility')
        if visibilityLgt == True:
          
           self.button = pm.button(name, label="ON" ,command = lambda *args: self.turnOn(totalLgt, name))
        else:
           self.button = pm.button(name, label="OFF" ,command = lambda *args: self.turnOn(totalLgt, name)) 
        self.button = pm.button('btnNameS'+str(totalLgt), label="SOLO", command = lambda *args: self.turnSolo(totalLgt, name))
        pm.attrColorSliderGrp(at='%s.color' % name )
        pm.attrFieldSliderGrp( min=0.0, max=10.0, at='%s.intensity' % name )
        
    def update(self, totalLgt,lis, butLayout, *args):
       if pm.layout(butLayout, exists = True):
          pm.deleteUI(butLayout)
       self.lightLst()
           
    def lightLst(self, *args):
       butLayout = pm.rowColumnLayout(numberOfColumns=1, columnWidth=[(10,120)], columnOffset=[10,"right",5])
       totalLgt = 0 
       lis = pm.ls(type='light')
       pm.button('buttonMain',label="UPDATE", e= True ,command = lambda * args: self.update(totalLgt,lis, butLayout ))
       #list all lights in scene
       for lgt in lis: 
          totalLgt += 1
          nameLgt = lgt.longName()
          name = nameLgt.split("|")[1]
          self.updateList(name, lgt, totalLgt)
       pm.setParent('..')
           
    #CREATE WINDOW 
    
    def createWindow(self):
        
        windowID = 'Light Control'
        if pm.window(windowID, exists = True):
            pm.deleteUI(windowID)

        self.window =  pm.window(windowID, title = "Modify Lights", width = 100, sizeable = True)
        pm.rowColumnLayout(numberOfColumns=1, columnWidth=[(10,120)], columnOffset=[10,"right",5])
        pm.text(label=" ********  Light list ******** \n")
        
        pm.button('buttonMain', label="CREATE" ,enable = True , command = lambda *args: self.lightLst())
           
        pm.text(label= " \n ***************************** \n ")
        pm.setParent('..')
        pm.showWindow(self.window)

cls = createWindowClass()
cls.show() 
 


Thank you!! =) I'm sorry my code is kinda messy thou.. 

Justin Israel

unread,
Sep 5, 2015, 7:03:19 PM9/5/15
to python_in...@googlegroups.com
On Sun, Sep 6, 2015 at 9:22 AM Maria Jesus Penella <mjpe...@gmail.com> wrote:
Thanks !  I think my problem it that I don't know exactly how layout and parents works in python... i'm just improvising a bit... Now I have created a layout just for the buttons and it deletes them ok, but if I do any change ( add or delete light ) I have a blank space where the buttons should be.. I attach this time the whole code for this to work =)

Yea, it was not possible to run this code, as pasted. I had to fix and format things. Also the parenting was not correct and I would get all the light buttons created within Mayas attribute editor (last parent). Please use pastebin.com or some other code pasting site for long code in the future. It will definitely help.

I just took the liberty of refactoring your code:

Things to note:

Can't use spaces in window name. It will always correct it and you will never match.

Moved layouts around to make deleting the buttons easier and cleaner, and using a scroll layout now.

Various other adjustments there as well. Let me know if you need specific details. Maybe this works a bit better for you without having to change too much of your original code?

Justin
 
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Maria Jesus Penella

unread,
Sep 6, 2015, 5:23:33 AM9/6/15
to Python Programming for Autodesk Maya
Works perfect now!! =) 

Thanks !! It's clearer now ^^
Reply all
Reply to author
Forward
0 new messages