如何使用textScrollList来select一个material?

How to use textScrollList to select a material?

我正在尝试使用 textScrollList 创建一个材料管理器。这是我的第一个代码,我正在使用这个工具自学 Maya Python。目前,我的代码列出了场景中的材质,但它们是不可选择的。

我 运行 遇到的问题是使列出的材料可选择。我认为我错误地定义了 'dcc'。

对我误解或做错的任何帮助都会很棒!提前致谢。

这是我的代码:

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()

错误发生在这里:

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

您将 materialsList 定义为所有材料,但 cmds.textScrollList() 需要您尝试查询的 textScrollList 的实例,您称之为 'materials' .

用这一行替换那一行:

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

通常对于 GUI 元素,我喜欢制作一个变量来捕获元素创建的结果,然后您可以稍后使用该变量来查询或编辑。

像这样:

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

希望对您有所帮助

脚本评论中的解释!

import maya.cmds as cmds


def getSelectedMaterial(*args):
    selection = cmds.textScrollList("material", query=True, si=True)
    cmds.select( selection )

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

cmds.columnLayout(adjustableColumn=True)

materialsList = cmds.ls(mat=True)
# remove quote for the command flag
# you can give a name to your control but if there is duplicates, it wont work
cmds.textScrollList("material", append=materialsList, dcc=getSelectedMaterial) 

cmds.showWindow()

#  A Better Way to do it :====================================================================================

from functools import partial

def doWhateverMaterial(mat=str):
    # my function are written outside ui scope so i can use them without the ui
    cmds.select(mat)

def ui_getSelectedMaterial(scroll, *args):
    # def ui_function() is used to query ui variable and execute commands
    # for a simple select, i wouldn't separate them into two different command
    # but you get the idea.
    selection = cmds.textScrollList(scroll, query=True, si=True)
    doWhateverMaterial(selection )

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

cmds.columnLayout(adjustableColumn=True)

materialsList = cmds.ls(mat=True)

# we init the scroll widget because we want to give it to the function
# even if you set the name : 'material', there is a slight possibility that this
# name already exists, so we capture the name in variable 'mat_scroll'
mat_scroll = cmds.textScrollList(append=materialsList)
# here we give to our parser the name of the control we want to query
# we use partial to give arguments from a ui command to another function
# template : partial(function, arg1, arg2, ....)
cmds.textScrollList(mat_scroll, e=True, dcc=partial(ui_getSelectedMaterial, mat_scroll))

cmds.showWindow()