Maya Python UI callback wont work if wrapped in function


#1

Hey there!

I have some python code (for now just a test), when I run it in Maya, it works as intended, prints both textfields.

import maya.cmds as cmds

def checkBoxes():
    x = cmds.textField(bt8, q=True, tx=True)
    y = cmds.textField(bt9, q=True, tx=True)
    
    print(str(x) + ' ' + str(y))

window = cmds.window(widthHeight=(500, 50))
cmds.rowLayout(adjustableColumn=5, numberOfColumns=5 )
bt8 = cmds.textField()
bt9 = cmds.textField('bt9')
bt7 = cmds.button('bt7', label='Planar', width=55, c='checkBoxes()')

cmds.setParent( '..' )
cmds.showWindow( window )

^THIS WORKS.

However, in my real script I want to wrap this into another function.
Doing that results in an Error when clicking the button:
# Error: RuntimeError: file <maya console> line 4: Object 'window1|rowLayout42|textField20' not found. #

This is the code resulting in the error:

import maya.cmds as cmds

def ui():
    def checkBoxes():
        x = cmds.textField(bt8, q=True, tx=True)
        y = cmds.textField(bt9, q=True, tx=True)
        
        print(str(x) + ' ' + str(y))
        
    window = cmds.window(widthHeight=(500, 50))
    cmds.rowLayout(adjustableColumn=5, numberOfColumns=5 )
    bt8 = cmds.textField()
    bt9 = cmds.textField('bt9')
    bt7 = cmds.button('bt7', label='Planar', width=55, c='checkBoxes()')
    
    cmds.setParent( '..' )
    cmds.showWindow( window )
ui()

^THIS DOESNT WORK.

I am really thankful for any help or tips. Thanks! ^^


#2
import maya.cmds as cmds

def ui():
  def checkBoxes(*_):
      x = cmds.textField(bt8, q=True, tx=True)
      y = cmds.textField(bt9, q=True, tx=True)
    
      print(str(x) + ' ' + str(y))
    
  window = cmds.window(widthHeight=(500, 50))
  cmds.rowLayout(adjustableColumn=5, numberOfColumns=5 )
  bt8 = cmds.textField()
  bt9 = cmds.textField('bt9')
  bt7 = cmds.button('bt7', label='Planar', width=55, c=checkBoxes)

  cmds.setParent( '..' )
  cmds.showWindow( window )
ui()