我正在尝试使用textScrollList制作材质管理器。这是我的第一段代码,我正在使用这个工具来自学Pythonfor Maya。目前,我的代码列出了场景中的材质,但它们是可选的。
我遇到的问题是使列出的材料可供选择。我想我错误地定义了"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
的实例,您称之为"材料"。
将该行替换为此行:
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()