将textField中的文本替换为对象名称 - Pymel

时间:2015-03-11 10:52:33

标签: python python-2.7 textfield maya pymel

我已经意识到存在类似的问题

这里: textfield query and prefix replacing

这里: Python - Change the textField after browsing - MAYA

但是,如果您有两个定义并且需要查询textField中的文本(实际上更改textField中的文本),则这些不会解决此问题。

我从经验中知道,在MelScript中执行下面的操作实际上是有效的,但是为了Python,并且学习如何在Python中实现它,它似乎无法工作。我错过了什么吗?我是否需要lambda来获取所选对象的名称并查询textField?

我有一个例子(需要修复的内容):

from pymel.core import *
def mainWindow():
    window('myWin')
    columnLayout(adj=1)
    button('retopoplz', ann='Select a Mesh to Retopologize', bgc=[.15,.15,.15],
           l='START RETOPOLOGY', c='Retopo(TextToMakeLive)')
    TextToMakeLive = textField(ann='Mesh Selected', bgc=[.2,0,0],
                               edit=0, tx='NONE')
    setParent('..')
    showWindow('myWin')
def Retopo(TextToMakeLive):
    #This tool selects the object to retopologize
    MakeLiveField = textField(TextToMakeLive, q=1, tx=1)
    MakeSelectionLive = (ls(sl=1))
    if MakeSelectionLive is None:
        warning('Please select an object to retopologize')
    if MakeSelectionLive == 1:
        TextToMakeLive = textField(TextToMakeLive, ed=1, 
                                   tx=MakeSelectionLive,
                                   bgc=[0,.2,0])
        shape = ls(s=MakeSelectionLive[0])
        setAttr((shape + '.backfaceCulling'),3)
        createDisplayLayer(n='RetopoLayer', num=1, nr=1)
        makeLive(shape)
        print('Retopology Activated!')
    else:
        warning('Select only ONE Object')
mainWindow()

2 个答案:

答案 0 :(得分:2)

只要存储名称,就可以随时编辑GUI对象 - 包括更改命令。所以你的mainWindow()可以返回你想要再次编辑的gui控件的名称,第二个函数可以使用这些名称来改变所创建对象的外观或行为。

但是,如果你使用python类来“记住”对象的名称和任何其他状态信息,这就容易得多:类很容易“看到”所有相关的信息和状态。这是您的原始转换为类:

from pymel.core import *
class RetopoWindow(object):

    def __init__(self):
        self.window = window('myWin')
        columnLayout(adj=1)
        button('retopoplz',ann='Select a Mesh to Retopologize', bgc=[.15,.15,.15],l='START RETOPOLOGY', c = self.do_retopo)
        self.TextToMakeLive=textField(ann='Mesh Selected', bgc=[.2,0,0],edit=0,tx='NONE')


    def show(self):
        showWindow(self.window)

    def do_retopo(self, *_):
        #This tool selects the object to retopologize
        MakeLiveField= textField(self.TextToMakeLive,q=1,tx=1)
        MakeSelectionLive=(ls(sl=1))
        if MakeSelectionLive is None:
            warning('Please select an object to retopologize')
        if len( MakeSelectionLive) == 1:
            TextToMakeLive=textField(self.TextToMakeLive,ed=1,tx=MakeSelectionLive,bgc=[0,.2,0])
            shape=ls(s=MakeSelectionLive[0])
            setAttr((shape+'.backfaceCulling'),3)
            createDisplayLayer(n='RetopoLayer',num=1,nr=1)
            makeLive(shape)
            print('Retopology Activated!')
        else:
            warning('Select only ONE Object')

RetopoWindow()。显示()

至于回调:有用的参考here

答案 1 :(得分:1)

您需要在创建要查询的command后指定textField标记。

所以你会这样做:

my_button = button('retopoplz',ann='Select a Mesh to Retopologize', bgc=[.15,.15,.15],l='START RETOPOLOGY')

TextToMakeLive=textField(ann='Mesh Selected', bgc=[.2,0,0],edit=0,tx='NONE')

button(my_button, e=True,  c=windows.Callback(Retopo, TextToMakeLive))

当你提出lambda时,你是在正确的思想链上。 Pymel的Callback在这里比lambda更有优势。查看文档:{​​{3}}