SirJames SirJames - 1 month ago 8
Python Question

Replace text in textField with object name - Pymel

I've realized that there were similar questions located

here:
textfield query and prefix replacing

and

here:
Python - Change the textField after browsing - MAYA

However, these do not address the issue if you have two definitions and need the text in the

textField
to be queried (actually CHANGE the text in the textField).

I know from experience that doing what I have below in MelScript actually works, but for the sake of Python, and learning how to do it in Python, it seems to not work. Am I missing something? Do I need a lambda to get the name of the object selected and query the textField?

I have an example (a snip-bit of what needs to be fixed):

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

Answer Source

GUI objects can always be edited -- including changing their commands -- as long as you store their names. So your mainWindow() could return the name(s) of gui controls you wanted to edit again and a second function could use those names to change the looks or behaviors of the created objects.

However, this is all much easier if you use a python class to 'remember' the names of the objects and any other state information: it's easy for the class to 'see' all the relevant info and state. Here's your original converted to classes:

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

As for the callbacks: useful reference here