Search code examples
pythonargumentsuser-defined-functionsmaya

How to use textScrollList to select a material?


I'm trying to make a materials manager using textScrollList. This is my first code and I'm using this tool to teach myself Python for Maya. Currently, my code lists the materials in the scene, but they are not selectable.

The problem I am running into is making the listed materials selectable. I think I am incorrectly defining 'dcc'.

Any help at all in what I'm misunderstanding or doing incorrectly would be awesome! Thanks in advance.

Here is my code:

import maya.cmds as cmds

def getSelectedMaterial(*arg):
    selection = cmds.textScrollList(materialsList, query=True, si=True)
    print selection

cmds.window(title='My Materials List', width=200)

cmds.columnLayout(adjustableColumn=True)

materialsList = cmds.ls(mat=True)

cmds.textScrollList('materials', append=materialsList, dcc="getSelectedMaterial()") 
cmds.showWindow()

Solution

  • The error is happening here:

    selection = cmds.textScrollList(materialsList, query=True, si=True)
    

    You defined materialsList to be all the materials but the cmds.textScrollList() is expecting the instance of the textScrollList that you are trying to query, which you called 'materials'.

    Replace that line with this one:

    selection = cmds.textScrollList('materials', query=True, si=True)
    

    Generally with GUI elements I like to make a variable that captures the result of the element creation then you can use the variable later to query or edit.

    Like this:

    myTextList = cmds.textScrollList('materials', append=materialsList, dcc="getSelectedMaterial()") 
    print cmds.textScrollList(myTextList, query=True, si=True)
    

    Hope that helps