Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

ShelfButton with Dynamic Popup Breaks on Maya Restart

ShelfButton with Dynamic Popup Breaks on Maya Restart

joeTCN99
Participant Participant
370 Views
1 Reply
Message 1 of 2

ShelfButton with Dynamic Popup Breaks on Maya Restart

joeTCN99
Participant
Participant

 

I wonder if any UI gurus can help me out...

 

I've created a maya shelf button in mel and attached a popup menu to it. The popupMenu uses the postMenuCommand flag to run a method that dynamically adds menu items to the popup.

 

Broadly it looks like the code below.

 

This all works well until I restart Maya. At that point maya saves the shelves to prefs and as it does this it only saves information about the buttons and the currently active popup entries. The link to the dynamic is lost. When maya restarts again the shelf button appears with a fixed list of whatever menuItems appeared in the list when it was closed.

 

I'd like it to not do this but so far I've only been able to get close to what I want by creating my own versions of the the built in mel scripts loadNewShelf, addNewShelfTab and deleteShelfTab. Which seems excessive and means I have to provide my own auto load mechanism for this shelf, instead of allowing users to just load it via the maya interface.

 

Thanks!

 

## MEL shelf file
global proc shelf_my_shelf () {
    global string $gBuffStr;
    global string $gBuffStr0;
    global string $gBuffStr1;

    ...
    shelfButton
    ...
        -command "import python_widget; python_widget.open()" 
        -sourceType "python" 
        -noDefaultPopup
        "my_shelf_button"
    ;

    python("import shelf_helpers; shelf_helpers.setup_menus()");
}

## shelf_helpers.py

# Create the popup menu 
def setup_menus():
    cmds.popupMenu(
        "shelf_popup_menu",
        p="my_shelf_button", 
        b=3,
        pmc="shelf_helpers.update_recent_file_menu()"
    )

# update the popup with our dynamic data
def update_recent_file_menu():

    data_list = get_data()
    popup_menu = cmds.popupMenu("shelf_popup_menu", e= True, deleteAllItems=True)
    cmds.menuItem(
            p="shelf_popup_menu", 
            d=True,
            dl="Pick Option..."
            )

   for each in data:
        cmds.menuItem(
            p="shelf_popup_menu", 
            l=data
        )


    








    

 

0 Likes
371 Views
1 Reply
Reply (1)
Message 2 of 2

joeTCN99
Participant
Participant

Thought I'd put up a quick note for anyone who finds this in the future...

 

I didn't find a good way to do what I wanted with the built in Maya commands. I *almost* got something working by hacking the mel scripts that control the prefs around maya shelves, but that isn't much of a solution!

 

Instead, I ended up writing a 'popup' menu in PySide and showing that from the shelf.

 

Using a QWidget with the window flags set gives a window without additional decoration and the behaviour of a popup: it disappears if you click out side it...

 

self.setWindowFlags(
    QtCore.Qt.Window | QtCore.Qt.FramelessWindowHint | QtCore.Qt.Popup
)

 

 
plus positioning at the mouse pointer:

 

point = QtGui.QCursor.pos()

self.setGeometry(QtCore.QRect(point.x(), point.y(), 300,100))

 

 
And you get something that works pretty well. 
 
One remaining problem is that I can only assign this to the left mouse button, I opted for having this on doubleClick and another command on single click. Bit annoying -right click would be more intuitive - but I cant find a way to override the shelf edit popup that maya provides.
 
Hope thats helpful to future shelf builders...
0 Likes