在 window 中创建动态按钮
Create dynamic buttons in window
我正在尝试根据场景中存在的灯光数量在 window 中创建按钮。
当我按下创建按钮时,出现此错误(#RuntimeError:未指定对象名称。#):
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. #
我有点受不了,找不到适合我的答案。这是代码:
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()
如果有人能对此有所启发,我将不胜感激!
您必须编写 pm.button(label="CREATE", command = self.lightLst)
并且必须将 self
作为方法的第一个参数传递给 class.
我正在尝试根据场景中存在的灯光数量在 window 中创建按钮。 当我按下创建按钮时,出现此错误(#RuntimeError:未指定对象名称。#):
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. #
我有点受不了,找不到适合我的答案。这是代码:
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()
如果有人能对此有所启发,我将不胜感激!
您必须编写 pm.button(label="CREATE", command = self.lightLst)
并且必须将 self
作为方法的第一个参数传递给 class.