Adding a QMenu to Maya??

605 views
Skip to first unread message

Benjam901

unread,
Dec 2, 2015, 4:57:49 AM12/2/15
to Python Programming for Autodesk Maya
Hello all,

I am rather stuck trying to add a QMenu to the main Maya window.

The aim is to have a custom menu that acts like the other menus on the top bar i.e. drop down and sub options appear and inside them various other sub options etc. etc. but I cannot seem to get my menu to pin up just yet I think I may be missing something.

I haven't added any functions to execute to it yet, still trying to get it to show up first

Here is my code:

import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI

def getMayaWindow():
	ptr = apiUI.MQtUtil.mainWindow()
	if ptr is not None:
		return shiboken.wrapInstance(long(ptr), QtGui.QWidget)

def addMenuItem(myMenu=None, title='', action=''):
	myMenu.addMenu(title)
	#menu.addAction(action)

def addSeparator(myMenu):
	myMenu.addSeparator()

def setupMenuBar():
	print 'setting up menu bar'
	mayaWindow = getMayaWindow()
	mainMenu = QtGui.QMenu('Overkill_Tools', mayaWindow)
	addMenuItem(mainMenu, 'TESTING', lambda: test)
	#mayaWindow.menuBar().addMenu(mainMenu)

def test():
	print 'testing qt-ness'
	
setupMenuBar()


Cheers,

Ben

Benjam901

unread,
Dec 2, 2015, 5:02:32 AM12/2/15
to Python Programming for Autodesk Maya
SOLVED:

I was defo missing something:

this line needed to be changed from returning QWidget to return QMainWindow:

return shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)

Adding the menu using: mayaWindow.menuBar().addMenu(mainMenu) works well now :)

But in the meantime if anyone has any solid tips to consider while creating something like this please feel free to post 

Cheers,

Ben

Marcus Ottosson

unread,
Dec 4, 2015, 2:35:43 AM12/4/15
to python_in...@googlegroups.com

How about the goold ol’ cmds.menu?

import maya.cmds as cmds

cmds.menu( label='MyMenu', tearOff=True, parent='MayaWindow' )
cmds.menuItem( label='New' )
cmds.menuItem( label='Open', subMenu=True )
cmds.menuItem( label='Cheese')
cmds.menuItem( label='Pasta')

Qt is great and all, but sometimes the built-in methods aren’t actually that bad either.


--
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/e5e17196-fcda-47ad-be89-0a5a5e61427d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Ben Hearn

unread,
Dec 4, 2015, 4:25:52 AM12/4/15
to python_in...@googlegroups.com
My original plan was to use the standard cmds/pm menus for Maya but I was recommended to stick with Qt as it ties into the Python ecosystem much better.

I have the drop down menu set up now but is there much a difference functionality wise and performance wise if using Qt vs cmds/pm?

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/IcMpXXmDnSM/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAFRtmOAe%3DYCT7n%2BrxoxwybELuaGQKLOteZOg_wwOjzLovgid5g%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--

Tel - +46 76245 92 90 (Sweden)

Marcus Ottosson

unread,
Dec 4, 2015, 9:13:34 AM12/4/15
to python_in...@googlegroups.com

Who recommended that? And are you really worried about the performance of your menu?

Functionality-wise, you could embed Quake 3 into Google Maps and draw your menu into it with Qt so it’s safe to say it wins there. But whether you need it or not is another matter.

Usability-wise, the cmds module offers less lines if all you’re looking for is a menu. Some might say, that doing a plain menu using QMenu is “re-inventing the wheel”. But again, maybe you are looking for something more than just a menu, in which case Qt is a good choice.



For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Ben Hearn

unread,
Dec 4, 2015, 10:18:35 AM12/4/15
to python_in...@googlegroups.com
It was a colleague of mine, I quizzed about solutions and the suggestion was go with Qt if inline with generic tools.

Performance for drop down menus I was not really worried about I was just wondering :)

I will be experimenting with both and see which one I prefer to go with :)


For more options, visit https://groups.google.com/d/optout.

Fredrik Averpil

unread,
Dec 4, 2015, 1:59:09 PM12/4/15
to python_in...@googlegroups.com
I concur with Marcus. If your Maya menus are not inheriting or being inherited by something PySide/PyQt-based, it would be faster to just use the facilities offered by Adsk/the docs: http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/CommandsPython/menu.html

// Fredrik


Justin Israel

unread,
Dec 4, 2015, 2:56:13 PM12/4/15
to python_in...@googlegroups.com

I also agree. If you can avoid doing custom Qt modifications to Maya that circumvent the official API, then you can minimize the potential for strange behavior to crop up. Especially across versions of Maya.
Maya is free to do any number of extra operations surrounding its procedure for adding a menu via its commands api, such as registering information about it. You may miss that behavior if you do it with Qt directly.


Ben Hearn

unread,
Dec 7, 2015, 3:37:54 AM12/7/15
to python_in...@googlegroups.com
Thanks for the sage advice guys. I have taken said advice and changed my menus to Maya API style menus.

I'll keep in mind what you guys mentioned about circumventing the official API 

Cheers,

Ben


For more options, visit https://groups.google.com/d/optout.

Ben Hearn

unread,
Dec 7, 2015, 12:06:32 PM12/7/15
to python_in...@googlegroups.com
Hello all,

I have another query about Maya menus. You can pin them to the shelf which is great but what gets pinned is the callback which appears to be a memory address. When Maya is closed and re-opened the callback no longer works.

I am invoking my commands like so:

def addMenuItem(parentMenu=None, labelName='', callback=None, sub=False, tearable=False):
	if callback == None:
		newMenu = pm.menuItem(parent=parentMenu, label=labelName, subMenu=sub, tearOff=tearable)
		return newMenu
	else:
		pm.menuItem(p=parentMenu, label=labelName, command=pm.Callback(callback))

Is there a better way to pin our menu items i.e. pin the script itself or would that be based around the command being something other that pm.Callback?

Cheers,

Ben

Justin Israel

unread,
Dec 7, 2015, 1:25:41 PM12/7/15
to python_in...@googlegroups.com

You could use the string form in this case, and make sure your callback is globally executable. That means either the function you use is in Maya's main namespace or you use something self contained

"import myPackage; myPackage.run()"


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