How to reference Python TextField objects

How to reference Python TextField objects

Anonymous
Not applicable
1,111 Views
1 Reply
Message 1 of 2

How to reference Python TextField objects

Anonymous
Not applicable

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

0 Likes
1,112 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

SOLVED

If there's a cleaner way than using global variables, I'd love to know. I also switched to PyMEL. The code below works (if the forum post retains indentation)

 

import pymel.core as pm

def test(*args):
    val = intv.getValue()
    txt = name.getText()

    print('val: {}'.format(val))
    print('txt: {}'.format(txt))

 

def testUI():
    window = pm.window()
    pm.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 100)] )
    global intv
    global name
    intv = pm.intField('intField')
    name = pm.textField(tx='fubar')
    button = pm.button(l='[ DOIT ]', c=test)

    pm.showWindow(window)

 

testUI()

0 Likes