Procedurally determining the number of submenus

56 views
Skip to first unread message

Kenneth Ibrahim

unread,
Oct 8, 2015, 9:36:32 PM10/8/15
to python_in...@googlegroups.com
Silly question here but I haven't figured this out yet nor come across the answer online.

What's the best way to determine the number of submenus a menuItem has after it's been created? I have a system that dynamically adds menu items based on certain criteria. After that work is done I'd like to disable the menu (or remove it) if the total number of children is zero.

Assume that I cannot get the number of items explicitly before creating the menu and that I'm using PyMEL's "with" nested structure to create the menu hierarchy.

Thx!

Ken

--
"God is a metaphor for a mystery that absolutely transcends all human categories of thought. It's as simple as that!" - Joseph Campbell

Robert White

unread,
Oct 9, 2015, 3:03:22 AM10/9/15
to Python Programming for Autodesk Maya
So, subMenus are really just menus, which means you can query the itemArray value and get a list of their children, if that list is empty, you've got an empty subMenu. 

I've got an example of how to search a top level menu recursively and return a list of all the empty subMenus:

One annoyance with the itemArray query, is that it returns the short name of the menuItem, so you need to combine it with it's parent to query any of it's children. But otherwise it just works.

Kenneth Ibrahim

unread,
Oct 9, 2015, 12:03:14 PM10/9/15
to python_in...@googlegroups.com
Thx for the response. I had thought that was the case and had tried using PyMEL's 'getNumberOfItems' method to no avail. I'll have a look at your code reference and see what I was doing wrong.

Cheers! Ken

--
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/ed6170c2-5521-41c3-9c59-45f9b58e1895%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Oct 9, 2015, 12:57:09 PM10/9/15
to python_in...@googlegroups.com
Just to perhaps state the obvious, I suppose ideally you would look at the items you are *about* to create and determine whether or not to create the menu in the first place, but maybe that's not an option in this case?


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



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

Kenneth Ibrahim

unread,
Oct 9, 2015, 1:11:32 PM10/9/15
to python_in...@googlegroups.com
Correct, I only know after the fact.

I got it working by running the following: pm.menu(my_menu, q=True, numberOfItems=True)

I was trying to be more Pythonic via PyMEL by running this: my_menu.getNumberOfItems() but that method apparently doesn't exist for the menu item (which is of type commandMenuItem I think). Again I must be overlooking something silly in my access attempt here.


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

Robert White

unread,
Oct 9, 2015, 1:24:35 PM10/9/15
to Python Programming for Autodesk Maya
Your not overlooking anything, MenuItem, and CommandMenuItem don't have the same methods on them that Menu does. Basically MenuItems and CommandMenuItems can't have any children, because they aren't Menus, but once you've set subMenu=True, the item returned is now a Menu, it can possibly have children.

So another thing you could attempt, would be just duck typing the whole affair by wrapping your call to getNumberOfItems() in a try / except block that catch's AttributeErrors, these would obviously fail on MenuItems/CommandMenuItems, but would return properly for Menus, this would let you continue to use the PyMel convenience methods, and is certainly considered 'pythonic'.

On Friday, October 9, 2015 at 12:11:32 PM UTC-5, Ken Ibrahim wrote:
Correct, I only know after the fact.

I got it working by running the following: pm.menu(my_menu, q=True, numberOfItems=True)

I was trying to be more Pythonic via PyMEL by running this: my_menu.getNumberOfItems() but that method apparently doesn't exist for the menu item (which is of type commandMenuItem I think). Again I must be overlooking something silly in my access attempt here.
On Fri, Oct 9, 2015 at 9:57 AM, Marcus Ottosson <konstr...@gmail.com> wrote:
Just to perhaps state the obvious, I suppose ideally you would look at the items you are *about* to create and determine whether or not to create the menu in the first place, but maybe that's not an option in this case?
On 9 October 2015 at 18:02, Kenneth Ibrahim <kenib...@gmail.com> wrote:
Thx for the response. I had thought that was the case and had tried using PyMEL's 'getNumberOfItems' method to no avail. I'll have a look at your code reference and see what I was doing wrong.

Cheers! Ken
On Fri, Oct 9, 2015 at 12:03 AM, Robert White <robert....@gmail.com> wrote:
So, subMenus are really just menus, which means you can query the itemArray value and get a list of their children, if that list is empty, you've got an empty subMenu. 

I've got an example of how to search a top level menu recursively and return a list of all the empty subMenus:

One annoyance with the itemArray query, is that it returns the short name of the menuItem, so you need to combine it with it's parent to query any of it's children. But otherwise it just works.

On Thursday, October 8, 2015 at 8:36:32 PM UTC-5, Ken Ibrahim wrote:
Silly question here but I haven't figured this out yet nor come across the answer online.

What's the best way to determine the number of submenus a menuItem has after it's been created? I have a system that dynamically adds menu items based on certain criteria. After that work is done I'd like to disable the menu (or remove it) if the total number of children is zero.

Assume that I cannot get the number of items explicitly before creating the menu and that I'm using PyMEL's "with" nested structure to create the menu hierarchy.

Thx!

Ken

--
"God is a metaphor for a mystery that absolutely transcends all human categories of thought. It's as simple as that!" - Joseph Campbell

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



--
"God is a metaphor for a mystery that absolutely transcends all human categories of thought. It's as simple as that!" - Joseph Campbell

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



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

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

Kenneth Ibrahim

unread,
Oct 9, 2015, 3:39:16 PM10/9/15
to python_in...@googlegroups.com
Thx for the clarification!

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.



--
"God is a metaphor for a mystery that absolutely transcends all human categories of thought. It's as simple as that!" - Joseph Campbell

--
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
konstr...@gmail.com

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



--
"God is a metaphor for a mystery that absolutely transcends all human categories of thought. It's as simple as that!" - Joseph Campbell

--
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/ceba8f5a-437b-4c01-aba1-f3c7a37693f9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages