View Full Version : save button to shelf

05 May 2012, 07:20 PM
Hi all,

I was trying to save a button to the current active shelf with python but with not so much luck.
Let's say I want to save a button doing this

print 'hello I am a button'

Which would be the best way?
I was taking a look at the way maya saves a camera bookmark to the shelf

global proc cameraBookmarkShelfCB(string $parent, string $camera)
setParent $parent;

string $item = selectedBookmark();

if ($item != "") {
global string $gShelfTopLevel;

if (`tabLayout -exists $gShelfTopLevel`) {
string $currentShelf = `tabLayout -q -selectTab $gShelfTopLevel`;

string $cmdStr =
"cameraView -e -camera "+$camera+" -setCamera "+$item;

-p ($gShelfTopLevel + "|" + $currentShelf)
-image1 "commandButton.png"
-label $item
-style `shelfLayout -q -style $currentShelf`
-width `shelfLayout -q -cellWidth $currentShelf`
-height `shelfLayout -q -cellHeight $currentShelf`
-c $cmdStr;

but I don't really know how to treat that $gShelfTopLevel variable (in python) :)

05 May 2012, 08:06 AM
Turning on the echo all commands and saving a script to a shelf gives:

scriptToShelf "tmp" "print 'hi'" "0";

So execute this mel command from python.

05 May 2012, 03:04 PM
Thanks zoharl, this will do the job :beer:

05 May 2012, 10:23 AM
I'd also recommend making use of existing code by executing existing MEL functions via python as suggested, but just in case anyone wanted to know the python to do this:
import maya.cmds as mc
import maya.mel as mel
topShelf = mel.eval('$nul = $gShelfTopLevel')
currentShelf = mc.tabLayout(topShelf, q=1, st=1)
mc.shelfButton(parent=currentShelf, i='commandButton.xpm', c='print "hello I am a button",')

Ironically you still have to execute MEL code to get the global variable which contains the top level shelf layout... :argh: (although hard-coding topShelf to "ShelfLayout" would probably be safe for most normal users)

05 May 2012, 05:16 PM
Thanks Nathan!
I was having some troubles using the mel procedure while this is working perfectly!
I also was quite interested in how to get the value of those global variables :)

CGTalk Moderation
05 May 2012, 05:16 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.