Как использовать textScrollList для выбора материала? - PullRequest
1 голос
/ 25 сентября 2019

Я пытаюсь создать менеджер материалов, используя textScrollList.Это мой первый код, и я использую этот инструмент для обучения Python для 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()

Ответы [ 2 ]

1 голос
/ 25 сентября 2019

Ошибка происходит здесь:

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)

Надеюсь, это поможет

0 голосов
/ 25 сентября 2019

Пояснения в комментариях к сценарию!

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