I want to add some UI elements when the user presses the button, something like a shape editor in Maya, this code just add a number, for now, I don't know how to receive variable from this function after pressing a button so I use a global variable, this code is working fine, but it's nice to do it without using a global variable, is there a way? image
import maya.cmds as cmds
a =10
def defaultButtonPush():
global a
a=a 1
print a
cmds.window( width=150 )
cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Button 1', command="defaultButtonPush()")
cmds.showWindow()
CodePudding user response:
You can use a class to create your UI. With a class you can keep all needed data inside the class an you can access it easily. It works like this in a simple form:
import maya.cmds as cmds
class MyUI(object):
def __init__(self):
self.value = 0
self.buildUI()
def pressButtonCallback(self, arg):
self.value = 1
print(self.value)
def buildUI(self):
cmds.window( width=150 )
cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Button 1', command=self.pressButtonCallback)
cmds.showWindow()
c = MyUI()