Determining if a shelf button already exists

I have captured the current shelf tab that I am looking at but how can I determine if a shelf button already exists?

What I am trying to querry is below:
if (!'shelfButton -ex -l "The Stupid Button"`)

Do I have to imply what shelf as well?

Thanks,

Morris Olmsted

Alright, I had to put a unique name on the button before it would let me use the -ex querry. I really wish all items in Maya could be querried by any number of ways like shelfButton -ex -l "myStupidButton". However for now I found that if I name the item explicitely, I can querry it.

Thanks,

Morris

you can query any UI item in the same way, but the problem with shelves is that the UI name isn't stored by maya in the shelf.mel file. However, I've used the docTag to great effect here. If you assign a shelf button a docTag string, then you can reliably query if a shelf button exists or not. Its quite cool when your scripts have dynamic shelf buttons. Its also useful to attach right-click popup menus to them for extra options, without taking up an extra shelf button.

Do you have an example of the doc tag and the popup call? I tried using a popup item but ran into some problems.

Morris

well for example, i have my shelf button created using something like:

shelfButton -docTag "usefulButton" -c "some command";

and then i have a proc that goes through all shelf buttons, queries their docTags, and returns the UI name, of the button that matches. Then once I know the name of the button, I can parent a popMenu to that UI control, with something like:

string $shelfButton = getShelfButtonName;
popupMenu -p $shelfButton;
menuItem -l "someCommand" -c( "print "hullo"" );

what sort of problems were you having with popupMenu?

I tried to give a shelfButton a name, not through the docTag. I then parented a popupMenu to the shelf button. It always worked for the current session of maya; however, when I loaded Maya again and tried the shelfButton's popupMenu no longer exists. I am guessing that it needs to be embedded into the the shelfButton command somehow or explicitily written to the shelf file.

Morris

yeah man, thats why I'm saying use the docTag. the docTag is stored when the shelf is saved, on maya exit. so you need to write a proc that on startup, goes through all shelf buttons, finds the button with the specific docTag, and returns that button's UI name.

you can't embed the shelf button name into the file, because maya re-writes it everytime you save shelves. So as far as I see it, this is the easiest way to do it. This way you can also have shelf buttons that change icons when clicked (for example, I have written an autosave script, that saves a scene to a temp file, after a certain number of events have happened. the shelf button i've made for it, shows whether the script is enabled or not)

making sense?
cheers.

I know this thread is SUPER old, but this is a last ditched effort to get an answer. ive been struggling with this for about a year when I have time to work on it. you actually tweaked my interest when you mentioned that with -dtg you could toggle icons of a shelf button using this flag. ive tried over and over and just dont seem to be able to get it to work. [looking for a solution with MEL, I dont do Python. im just a lowly animator, not great with code]

would you be able to give me an example of a script that does this? I understand what you wrote, but I cant figure out how to write it.
im trying to query the button I just clicked, then toggle its icon to display the state of the object that is associated to this button.

I am not expecting a reply as this is SO old. but if I get one…oh boy, you will make my day!

Hey…
A bit rough to translate this to Mel, the Python code I had for a similar case was much more terse and understandable ;D

Anyway here’s my translation of it. Hopefully it can help you out :wink:

global proc string[] findShelfButtonsWithDocTag(string $shelf, string $docTag) {
    string $shelfContent[] = `shelfLayout -query -childArray $shelf`;

    string $buttonsWithDocTag[] = {};
    
    // find shelf button with specified docTag
    //print($shelfContent);
    for ($btn in $shelfContent) {
        int $hasDocTag = $docTag == `shelfButton -query -docTag $btn`;
        if ($hasDocTag) {
            $buttonsWithDocTag[size($buttonsWithDocTag)] = $btn;
        }
    }

    return $buttonsWithDocTag;
}


// find current shelf
string $currentShelf = `tabLayout -query -selectTab $gShelfTopLevel`;

string $DOC_TAG = "arbitrary_unique_tag";

// create shelf button with DOC_TAG 
// (do this ONCE, then you can comment it out!)
string $iconName = "redSelect.png";
shelfButton -parent $currentShelf -docTag $DOC_TAG -image $iconName ("myShelfButton_" + $DOC_TAG);

// find shelf button with specified docTag
string $foundShelfButtons[] = findShelfButtonsWithDocTag($currentShelf, $DOC_TAG);
if (size($foundShelfButtons) <= 0) {
   error("No shelf button with docTag '" + $DOC_TAG + "' found.");
}

string $shelfBtn = $foundShelfButtons[0]; // first found button
//print $shelfBtn;


// actual button code 

// (in this example act based on current icon name,
//  but you can change the logic however you like)
string $currIconName = `shelfButton -query -image $shelfBtn`;


string $newIconName = "";
if ($currIconName == "redSelect.png") {
    print("RED");
    $newIconName = "greenSelect.png";
} else {
    print("GREEN");
    $newIconName = "redSelect.png";
}
shelfButton -edit -image $newIconName $shelfBtn;