How to reference Python TextField objects
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello -- What's the best way to reference a textField object in a Python function outside of where it was created?
I get a runtime error when I try to get a TextField's text value when accessing the field in a function outside the scope of the function that defines the UI. Oddly, other objects seem to be accessible.
In the simple program below, the TextField object reference in the main, test() function cannot be found when the script is executed, but the IntField returns its value without issue. Here's the error msg:
RuntimeError: Object 'window5|rowColumnLayout179|textField137' not found.
I've seen how I can pass an explicit reference to the TextField object through the button's command argument. The script I'm writing is large and there are enough text fields that there's gotta be a better way, right?
I'm using Python 3 and Maya 2022 on OSX 11.1. TIA!
import maya.cmds as cmds
def test(*args):
val = cmds.intField('intField', q=1, v=1)
txt = cmds.textField(name, q=1, tx=1)
print('val: {}'.format(val))
print('txt: {}'.format(txt))
def testUI():
window = cmds.window()
cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 100)] )
cmds.intField('intField')
name = cmds.textField(tx='fubar')
button = cmds.button(l='[ DOIT ]', c=test)
cmds.showWindow( window )
testUI()