UI and query?

81 views
Skip to first unread message

maya2000

unread,
Nov 17, 2014, 12:50:16 AM11/17/14
to python_in...@googlegroups.com
hi....
after running this cide:
#...create new button Window:
mc.window('ss')
mc.frameLayout ('ABC')
mc.button(label='createNewButton', command="mc.setParent('ABC');mc.button(label='newButton')")
mc.showWindow('ss')
#end.....
when press "createNewButton" button,maya in the "ABC" frameLayout craete a new button....now how query new button name, when press it?
sorry...my english is bad!....

Justin Israel

unread,
Nov 17, 2014, 4:54:19 AM11/17/14
to python_in...@googlegroups.com
Hi,

What problem are you trying to solve, exactly? The reason I ask is because there are multiple ways to answer this question depending on what your goal is, and whether this is just a trivial example of testing out UI, or if you actually have a use case.

Here are two examples:

import maya.cmds as mc def create_button(*args): mc.setParent('ABC') name = mc.button(label='newButton') # Do something with button name print name mc.window('ss') mc.frameLayout ('ABC') mc.button(label='create1', command="mc.setParent('ABC');mc.button(label='newButton')") mc.button(label='create2', command=create_button) mc.showWindow('ss') # Get the last button that was created in the layout lastButton = mc.layout('ABC', q=True, childArray=True)[-1]

You can query for the last button that was created, by asking the layout. But it would seem the more useful approach is to not use string commands, but rather a callable. This callable will give you more control over creating the button by actually being able to save the name of the button and do something with it. 



--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/66259fd7-ea88-481a-a6ef-0779a7c2f560%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

maya2000

unread,
Nov 18, 2014, 12:05:39 PM11/18/14
to python_in...@googlegroups.com
Thank!
i dont want query for the last button....
i write a programe...it create a new menu Item(or button):
the new menuItem run a function...now the function have a argument...i want>>>the argument=menuItem name:
example:
import maya.cmds as mc mc.window() mc.frameLayout ('ABC') mc.button(label='create1', command="NI()") mc.showWindow() import random def NI(): mc.setParent('ABC') mc.symbolButton(image='setKeyframe.png', command='pass' ) mc.popupMenu(button=1 ) ind=random.randrange(1,1000) mc.menuItem('new'+str(ind),label='new root',c='mc.polyCube(n="new")')## new=menuItem Name thet press....<<<<<<<<<
now i want when press menuItem(example) , my program create new cube thet...cube name is menuItem name that press.... 

Justin Israel

unread,
Nov 18, 2014, 3:31:54 PM11/18/14
to python_in...@googlegroups.com
First, like my previous example, you should ditch using strings for your callbacks when they are just calling your own functions, and just reference the callable object:
def someCallback(*args):
    print "callback!"

mc.button(command=someCallback)
That being said, you can name your polyCube the same way you are generating a name for the menuItem:
name = 'new%d' % ind
mc.menuItem(name, label=name, c='mc.polyCube(n=%r)' % name)



--
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/3dca7864-bf32-4de0-9ec8-6d8a090ad704%40googlegroups.com.

maya2000

unread,
Nov 18, 2014, 5:45:05 PM11/18/14
to python_in...@googlegroups.com
ok....Thank,.Thank

Jesse Kretschmer

unread,
Nov 19, 2014, 4:48:02 PM11/19/14
to python_in...@googlegroups.com
Thank you Justin for so consistently providing some great explanations and examples. However, I would like to point out that the command parameter for the menuItem() and button() methods can be any callable python object.

I prefer to avoid using strings for the command parameter. This allows for my text editor to maintain syntax highlighting and also some basic code inspection (pylint).
def button_clicked(name):
    print "Clicked button: '%s'" % name

for name in ['one','two','three']:
    mc.button(label=name ,command=lambda x=name: button_clicked(x))
Note:  If you use a lambda, be aware that it is important to localize any variables used within the lambda. The x=name creates a new reference to the name variable which belongs to the lambda function.

Justin Israel

unread,
Nov 20, 2014, 1:18:29 AM11/20/14
to python_in...@googlegroups.com
No problem. But I did point out in my first reply that it is preferable to use callable object ;-)

--
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.
Reply all
Reply to author
Forward
0 new messages