Search code examples
pythonbuttondynamicmayapymel

Create dynamic buttons in window


I'm trying to create buttons in a window depending on the number of lights that exist on the scene. When I press the create button I get this error ( # RuntimeError: No object name specified. # ) :

Traceback (most recent call last): File "C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\internal\factories.py", line 779, in callback res = origCallback( *newargs )

File "", line 31, in lightLst

File "", line 17, in updateList

File "C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\internal\factories.py", line 806, in newUiFunc return beforeUiFunc(*args, **kwargs)

File "C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\internal\factories.py", line 947, in newFuncWithReturnFunc res = beforeReturnFunc(*args, **kwargs)

File "C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\internal\pmcmds.py", line 134, in wrappedCmd res = new_cmd(*new_args, **new_kwargs)

RuntimeError: No object name specified. #

I'm kinda stuck with it, and I cannot find an answer that works for me. Here is the code:

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(totalLgt, *args):
        print "Enter turnOn"

    def turnSolo(totalLgt, *args):
        print "Enter turnSolo"

    def updateList(name, totalLgt, *args):
        print "update %s" % name

        self.button = pm.button(label="ON", e=True, command = lambda *args: turnOn(totalLgt))
        self.button = pm.button(label="SOLO", e=True, command = lambda *args: turnSolo(totalLgt))

    def lightLst(*args):
        totalLgt = 0 

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

    #CREATE WINDOW 
    def createWindow(self):
        windowID = 'Light Control'
        if pm.window(windowID, exists = True):
            pm.deleteUI(windowID)

        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(label="CREATE", command = lightLst)
        pm.text(label= " \n ***************************** \n ")
        window_obj = pm.window(windowID)
        window_obj.show()

cls = createWindowClass()
cls.show()

If someone could bring some light on it I'd really apreciate it!


Solution

  • You have to write pm.button(label="CREATE", command = self.lightLst) and you have to pass self as first parameter of your methods inside the class.