使用 Python 刷新 Maya 中的 textScrollList

refresh the textScrollList in maya with Python

我有一个带灯的数组,每次我创建一个灯时,它存储的就是我的数组。 我有一个 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()' )

您可以添加使用灯光刷新列表的功能。这可以在您创建新灯后调用,以便将其添加到列表中。您还可以添加一个刷新按钮来调用相同的功能,以防您 add/delete 在场景中点亮,它会正确更新。

您不需要将灯光添加到列表中并对其进行跟踪。相反,您可以使用 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')

希望对您有所帮助。