如何使用textScrollList选择物料?

2024-09-30 10:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用textScrollList做一个物料管理器。这是我的第一个代码,我用这个工具自学Python for 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()

Tags: 工具代码true管理器for场景物料cmds
2条回答

剧本注释中的解释!你知道吗

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

错误发生在这里:

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)

希望有帮助

相关问题 更多 >

    热门问题