[Maya-Python] get the cmds.intField when button pressed?

1,143 views
Skip to first unread message

gnn

unread,
Apr 16, 2017, 4:11:13 PM4/16/17
to Python Programming for Autodesk Maya
Hello, i need to make an ui layout in python inside maya,
it is asking some questions and when the button is pressed,
put the entries of intField or textField into variables...
very simple but i can't find the solution: "if button pressed?"...
thanks for any help!!

import maya.cmds as cmds
window = cmds.window(title = "Open Last scene", width = 150, height = 100)
cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'left', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.text( label='Episode:  ', align='left' )
cmds.intField( "Episode", minValue = 0, maxValue = 100, value = 0)
cmds.separator( h=5, style='none' )
cmds.rowColumnLayout( numberOfColumns=1, columnAttach=(1, 'left', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.button('Open', l="Open Last scene", w=180, h=30)
cmds.showWindow()

#if button pressed??
    #put the intField into a variable:
    EpisodeName = `cmds.intField( "Episode", query = True, value = True)`
    print EpisodeName

Justin Israel

unread,
Apr 16, 2017, 5:02:11 PM4/16/17
to python_in...@googlegroups.com
If you take a glance at the docs for "button", at the bottom you will see an example of making use of the 'command' parameter. This param takes either a python string to evaluate, or a callback object (preferred)


So in your case, to fill in the "#if button pressed" section, you would put that into a callback function, and then tell the button to call it when clicked:

def buttonPressed(*args):
    print "button pressed"

cmds.button('Open', l="Open Last scene", w=180, h=30, command=buttonPressed)



 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/66af5f58-f7f6-4173-ab4e-0b8b842c0cfe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gnn

unread,
Apr 16, 2017, 7:18:59 PM4/16/17
to Python Programming for Autodesk Maya
Thank you Justin,
I'm sorry, but i'm a real newbie in Python, i'm not understand why i can't assign a variable inside the function like this:

def buttonLGTPressed(*args):
    buttonPressed = 'LGT'
    print "button pressed"

cmds.button('Open', l="Open Last scene", w=180, h=30, command=buttonLGTPressed)

if buttonPressed == 'LGT':
    print "button LGT pressed"

it's not the good way to do this?

Justin Israel

unread,
Apr 16, 2017, 7:34:56 PM4/16/17
to python_in...@googlegroups.com
The use of a callback function, in this case your 'buttonLGTPressed()', is meant to be the point at which you know the button has been pressed and can react. I am not sure what you are trying to achieve by having that extra "if buttonPressed" at the end. I will assume you want the button press to change some form of persistent state, which you can then check at another point in time? If that is the case, you are going to have to set up some way to properly store state. Two possible approaches to this are to either use a class for your UI. Or to use optionVars which is more of a global variable storage provided by Maya. In the MEL world, because of a lack of classes, optionVar is used more frequently. 

A class might take the form of:

class MyDialog(object):

    def __init__(self):
        self._buttonPressValue = ""
        # Now set up the rest of your UI.

        # Create a button and set the command
        # to use  self.buttonPressed

    def show(self):
        # Show the UI

    def buttonPressed(self, *args):
        self._buttonPressValue = 'LGT'

    def getButtonPressValue(self):
        return self._buttonPressValue

Does this help clarify the issue a bit more?

Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

gnn

unread,
Apr 17, 2017, 5:25:00 AM4/17/17
to Python Programming for Autodesk Maya
i had tried to set up the rest of your UI like that:
self.textField( "Episode", text = "000")
and Show the UI in this way;
self.showWindow()
nothing appened...
i need the library? (import maya.cmds as cmds)
that true, i just want the button pressed to make variable path with the texts fields and an action (open a scene)

walk_fish

unread,
May 16, 2017, 3:45:48 PM5/16/17
to Python Programming for Autodesk Maya
在 2017年4月17日星期一 UTC+8上午4:11:13,gnn写道:
my english is very poor

i think you want this:

import maya.cmds as cmds
import random
window = cmds.window(title = "Open Last scene", width = 150, height = 100)
cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'left', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.text( label='Episode: ', align='left' )
cmds.intField( "Episode", minValue = 0, maxValue = 100, value = 0)
cmds.separator( h=5, style='none' )
cmds.rowColumnLayout( numberOfColumns=1, columnAttach=(1, 'left', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.button('Open', l="Open Last scene", w=180, h=30,c='check_value()')
cmds.showWindow()
def check_value():
i=random.uniform(0,100)
cmds.intField( "Episode", e = True, value = i)#EpisodeName
Reply all
Reply to author
Forward
0 new messages