Community
Maya Forum
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to reference Python TextField objects

1 REPLY 1
Reply
Message 1 of 2
leknarf
437 Views, 1 Reply

How to reference Python TextField objects

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

1 REPLY 1
Message 2 of 2
leknarf
in reply to: leknarf

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report