代码之家  ›  专栏  ›  技术社区  ›  SirJames

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

  •  0
  • SirJames  · 技术社区  · 11 年前

    我意识到有类似的问题

    在这里: 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 回复  |  直到 9 年前
        1
  •  2
  •   theodox    11 年前

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

    至于回调:有用的参考 here

        2
  •  1
  •   kartikg3    11 年前

    您需要分配 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的回调比lambda更有优势。查看文档: http://download.autodesk.com/global/docs/maya2014/zh_cn/PyMel/generated/classes/pymel.core.windows/pymel.core.windows.Callback.html