Adding a custom menu via a Maya Module

3,901 views
Skip to first unread message

Eric Thivierge

unread,
Jan 18, 2015, 4:28:17 PM1/18/15
to python_in...@googlegroups.com
Is it possible to add a menu to the top of the Maya UI from a Module? I'm trying to build a small tool that I can distribute and don't want the users to have to edit their userSetup.py or similar to have it work. I just want them to drop a module into their module path and be done with it.

Haven't found any info on this being available.

Thanks,

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com

Marcus Ottosson

unread,
Jan 19, 2015, 3:34:20 AM1/19/15
to python_in...@googlegroups.com
+1

Johnny Cochrane

unread,
Jan 19, 2015, 5:09:55 AM1/19/15
to python_in...@googlegroups.com
Is this what you want?

import pymel.core as pm

# Name of the global variable for the Maya window
MainMayaWindow = pm.language.melGlobals['gMainWindow'] 

# Build a menu and parent underthe Maya Window
customMenu = pm.menu('Custom Menu', parent=MainMayaWindow)
# Build a menu item and parent under the 'customMenu'
pm.menuItem(label="menu item 'hihi'", command="print 'hihi'", parent=customMenu)


Inline images 1



On 19 January 2015 at 19:34, Marcus Ottosson <konstr...@gmail.com> wrote:
+1

--
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/CAFRtmOA0zaCBF%3DEwbuE-7t%3DN3A%2B8HQ-ed8NRp97HBB2czTVKvA%40mail.gmail.com.

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

owen burgess

unread,
Jan 19, 2015, 7:50:24 AM1/19/15
to python_in...@googlegroups.com
Hey Eric,

How about using a plug-in to manage your custom menu ?

If your module contains a plug-in, you would include the commands for creating your custom menu in the initializePlugin() function (and the commands for removing it in the uninitializePlugin() function)

If your module doesn't contain a plug-in you might want to create a python plug-in with the sole purpose of managing your custom menu as mentioned above. I just tried this in Maya 2015 and it works.

The Plug-in Manager provides a simple UI for (re)loading/removing your custom menu. All the artist has to do is set the plug-in to Autoload and they're done, no scripting required.

Owen

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

Eric Thivierge

unread,
Jan 19, 2015, 9:04:02 AM1/19/15
to python_in...@googlegroups.com
Hey Owen,

If that's the standard way to go I'll take that route. I'll have to
figure out the Maya plug-in format but shouldn't be too bad. The only
other way I've found to add a custom menu without having the user run a
command / method is to put some code in the userSetup.py file which
won't be good for easy distribution.

I'll have a look thanks.

Eric T.

Marcus Ottosson

unread,
Jan 19, 2015, 9:35:51 AM1/19/15
to python_in...@googlegroups.com
Hey Eric,

You can use multiple userSetup.py files, they will get read in the order they appear on the PYTHONPATH. Then you can have your users append a custom path to their PYTHONPATH, say the one pointing to your distribution, which will then get read durign startup, without the user having copy any files.

Not sure which way is better, but it is an alternative. It's how we do it for Pyblish and it has worked rather well; the PYTHONPATH is typically set during launch of an application, which basically makes it a method of exposing distributions on a per-project basis.

See for example.

Best,
Marcus

Eric Thivierge

unread,
Jan 19, 2015, 10:26:18 AM1/19/15
to python_in...@googlegroups.com
Thanks Marcus,

Sure adding to the PYTHONPATH can work too, however if I release a tool into the public space I don't want to have to support the artists that know nothing about setting that stuff up. I just want them to drop a directory in somewhere / exctract it, and have it just work. Activating the plug-in isn't a big issue either as it's standard.

Anything with editing env vars for artists is a tricky business and only leads to me not wanting to release tools to the public as I have to answer tech support emails. I've been through it too many times.

Thanks,
Eric T.
--
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.

Marcus Ottosson

unread,
Jan 19, 2015, 11:06:22 AM1/19/15
to python_in...@googlegroups.com
Sounds wise, Eric. I'll follow your lead on this one, haven't dug into modules before.

Could you let me know here how it goes?

Best,
Marcus

Siew Yi Liang

unread,
Jan 19, 2015, 9:17:30 PM1/19/15
to python_in...@googlegroups.com
Hi Eric:

Here's some code from my scripts to get you going, I had to deal with this recently, though I dealt with the userSetup config differently (I expected my animators to be able to do that, but I imagine it wouldn't be too difficult to write a quick batch file to just append the lines you need :P ):

http://pastebin.com/5cxxU9rL

Ignore the evalDeferred, you can just use a mel.eval() if you're going to be running it after MayaWindow is already built. Really the only important thing is just getting that gMainWindow var from MEL, then everything else is easy. :)

http://pastebin.com/0nd05atr

In Maya, if I just call the rigTools.start() method, it will build it into Maya, so you can just assign that to a shelf button if you want and then your artists can just click that button to have the new menu added to the menubar, if that's acceptable. Otherwise the only other easier way I can think of is to have it as a scripted plugin like Owen said.

Hope that helps!
Yours sincerely,
Siew Yi Liang
--
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.

Eric Thivierge

unread,
Jan 20, 2015, 9:44:32 AM1/20/15
to python_in...@googlegroups.com, Siew Yi Liang
Thanks Siew, I'll have a look.

Eric T.
>> <mailto:python_inside_m...@googlegroups.com>.
>> <https://groups.google.com/d/msgid/python_inside_maya/CAAjrnHs8fUyoo4b_Uy%3Driant4E8aiN-u6dhyxOJHTfbc4hLsQQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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
> <mailto:python_inside_m...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/54BDBAB4.4000905%40gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/54BDBAB4.4000905%40gmail.com?utm_medium=email&utm_source=footer>.

Roy Nieterau

unread,
Jan 20, 2015, 5:17:48 PM1/20/15
to python_in...@googlegroups.com, son...@gmail.com, ethiv...@hybride.com
Just quickly tested another workflow with the new 'module' setup for Maya.
If you distribute your tool/plug-in as a maya module (with the .mod) file you can include a userSetup.py within its own script folder.
No need to change any environment variables.

*Note I'm not sure how 'new' this module workflow exactly is with Maya; it might only work on some of the newer versions like (2014+?)

Though they won't be copying over your 'module' into the scripts folder, but to the designated spot for modules.
Also modules would allow you to contain your scripts, documentation, plug-ins, icons all in its own folder not cluttering anything else of the user.

And I think you know how to use the userSetup.py to create the menu. Aside from getting the global mel variable: gMainWindow you don't need to use mel commands.
I'm using maya.utils.executeDeferred() and just pass it a python function as a reference.

Another thing to keep in mind is that the stuff you do in userSetup.py runs in the global interpreter; if you rely on function names and someone else overwrites it in their code in the global namespace of the interpreter it will mess with your code.
I also tend to run the code in userSetup.py within its own function just so I won't mess up anything globally, like so:

def __toolsetXUserInit():
   
print 'create menu and do awesome startup stuff'

__toolsetXUserInit
()
del __toolsetXUserInit()

Hope this helps.

Cheers,
Roy
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAAjrnHs8fUyoo4b_Uy%3Driant4E8aiN-u6dhyxOJHTfbc4hLsQQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/python_inside_maya/CAAjrnHs8fUyoo4b_Uy%3Driant4E8aiN-u6dhyxOJHTfbc4hLsQQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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

Eric Thivierge

unread,
Jan 21, 2015, 10:17:12 PM1/21/15
to python_in...@googlegroups.com
Quickly tested this tonight and putting a userSetup.py in the module worked and will load the menu as needed! Thanks for the help all. I'll still look into the plug-in for the menu though as the the userSetup.py method for loading a menu seems a bit unclean to me.

Eric T.

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com

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/a91509b7-aae2-41c3-ae22-21acc3dfc648%40googlegroups.com.

vento...@gmail.com

unread,
Oct 30, 2017, 2:26:53 PM10/30/17
to Python Programming for Autodesk Maya
Hiya.

Could you recommend some solution if I would like to highlight certain main menu items for easier access/visibility in the already cluttered menus? Say like you have the new features highlighted. Thanks

tezha...@gmail.com

unread,
Aug 9, 2019, 3:46:28 PM8/9/19
to Python Programming for Autodesk Maya
On Monday, January 19, 2015 at 5:09:55 AM UTC-5, Johnny Cochrane wrote:
> Is this what you want?
>
> import pymel.core as pm
>
> # Name of the global variable for the Maya window
> MainMayaWindow = pm.language.melGlobals['gMainWindow']
>
> # Build a menu and parent underthe Maya Window
> customMenu = pm.menu('Custom Menu', parent=MainMayaWindow)
> # Build a menu item and parent under the 'customMenu'
> pm.menuItem(label="menu item 'hihi'", command="print 'hihi'", parent=customMenu)
>
>
>
>
>
>
>
>
>
>
>
>
>
> On 19 January 2015 at 19:34, Marcus Ottosson <konstr...@gmail.com> wrote:
>
> +1
>
>
>
>
> --
>
> 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/CAFRtmOA0zaCBF%3DEwbuE-7t%3DN3A%2B8HQ-ed8NRp97HBB2czTVKvA%40mail.gmail.com.
>
>
>
> For more options, visit https://groups.google.com/d/optout.

Hi Johnny,

Do you have to run the script every time you open Maya? Is there any way you don't need to run any code and get the custom menu working?

Thanks!
Reply all
Reply to author
Forward
0 new messages