使用Python刷新maya中的textScrollList

时间:2018-05-10 08:24:29

标签: maya

我有一个带灯的数组,每当我创建一个它存储的灯是我的数组。 我有一个textScrollList,显示我的数组中的所有灯光。

当我添加灯光时,它不会引用textScrollList。

有人可以告诉我如何做到这一点,所以每当我点灯时它都会在textScrollList中显示出来。或者使用刷新按钮。

谢谢!

我现在的代码:

import maya.cmds as cmds
lights=[]

myWindow = cmds.window(title='My Lights', wh=(200,400),sizeable =False )
cmds.columnLayout()
cmds.showWindow(myWindow)

 LightsButton = cmds.button(label='Make Lights', command = "makeLights()", width =200,height = 25,align='center')


def makeLights():
    lights.append(cmds.shadingNode('aiAreaLight', asLight=True))




LightSelector = cmds.textScrollList( numberOfRows=8, allowMultiSelection=True,append=(lights), showIndexedItem=4, selectCommand = 'selectInTextList()' )

1 个答案:

答案 0 :(得分:0)

您可以添加一个用灯刷新列表的功能。这可以在您创建新灯后调用,以便添加到列表中。您还可以添加刷新按钮来调用此功能,以防您在场景中添加/删除灯光,并且它将正确更新。

您无需将灯光添加到列表中并跟踪它。相反,您可以使用cmds.ls()来收集场景中的所有灯光。除非您确实因某些原因需要列表,否则很容易编辑下面的示例来使用它:

import maya.cmds as cmds


# Clear the listview and display the current lights in the scene.
def refreshList():
    # Clear all items in list.
    cmds.textScrollList(lightSelector, e=True, removeAll=True)

    # Collect all lights in the scene.
    allLights = cmds.ls(type='aiAreaLight')

    # Add lights to the listview.
    for obj in allLights:
        cmds.textScrollList(lightSelector, e=True, append=obj)


# Create a new light and add it to the listview.
def makeLights():
    lights.append(cmds.shadingNode('aiAreaLight', asLight=True))

    refreshList()


def selectInTextList():
    # Collect a list of selected items.
    # 'or []' converts it to a list when nothing is selected to prevent errors.
    selectedItems = cmds.textScrollList(lightSelector, q=True, selectItem=True) or []

    # Use a list comprehension to remove all lights that no longer exist in the scene.
    newSelection = [obj for obj in selectedItems if cmds.objExists(obj)]

    cmds.select(newSelection)


# Create window.
myWindow = cmds.window(title='My Lights', wh=(200,400), sizeable=False)
cmds.columnLayout()
cmds.showWindow(myWindow)

# Create interface items.
addButton = cmds.button(label='Make Lights', command='makeLights()', width=200, height=25, align='center')
lightSelector = cmds.textScrollList(numberOfRows=8, allowMultiSelection=True, append=cmds.ls(type='aiAreaLight'), showIndexedItem=4, selectCommand='selectInTextList()')
refreshButton = cmds.button(label='Refresh list', command='refreshList()', width=200, height=25, align='center')

希望有所帮助。