View Full Version : Dynamic Shelf Loading

01 January 2013, 09:12 PM
Hey guys!

Just had a question that I'm having problems googling/searching on the forum...I'm trying to dynamically load a shelf so that it always appears in its most updated state on the user's computer. Here's what I've figured out so far:
I am using Maya.env and added a line

$MAYA_STARTUP_SCRIPT_PATH = (shared directory where I have a startup.mel file)

The startup.mel file says:
//check to see if shelf exists
string $install_shelfName = "andres";
string $install_shelfPath = "/usr/people/andres-w/andresShelfMaster/shelf_andres.mel";
int $install_shelfExists = `layout -q -ex $install_shelfName`;
if ($install_shelfExists == 1){
print "Deleting existing Andres shelf\n";
deleteUI -layout $install_shelfName;

//add shelf
print "Adding Andres Shelf\n";
loadNewShelf $install_shelfPath;

So this doesn't quite work to remove the old shelf cleanly...and using the command deleteShelfTab requires hitting OK and doesn't work either. Any help would be most appreciated! Also I'm probably doing this completely wrong so...thanks for pointing me in the right direction ahead of time!

Background: Can't use userSetup.mel since it's getting overridden by our pipeline in some way that I'm not sure of...

01 January 2013, 09:59 PM
So I fixed the issue. Took the command deleteShelfTab from the maya install and removed all confirmDialog mentions and now the shelf deletes properly (I think the other UI command doesn't do it cleanly because it leaves the .deleted file).

New Issue: The loadNewShelf command, if done at startup seems to load a shelf that's blank. When I run the exact same code to delete and repopulate the shelf when maya is already running it works as intended...anyone know a workaround?

01 January 2013, 10:08 PM
For the startup code, try wrapping it in an evalDeferred command.

01 January 2013, 10:27 PM
Thanks for the reply. Looks like using evalDeferred delays the creation of the shelf but messes up start up code:

// Error: file: /software/maya/2012.4.5/linux.centos5.x86_64/scripts/startup/shelf.mel line 81: Object 'andres' not found. //

Looks like this mel gets confused because our shelf doesn't exist at startup but for some reason Maya thinks it still was. Line 81 in that code is just trying to determine whether a shelf is unpopulated with buttons and populates it if it isn't.

Edit: Oh! You're the author of Charcoal Editor! I totally bought that awhile back. Thanks!

01 January 2013, 10:57 PM
Hi Daemonecles,

I've been here before and have a few hints.

* if you're looking to update a shelf, don't try and delete the entire shelf. You end up with shelves that won't update properly, and general mysteriousness. Instead, delete all the buttons, but leave the shelf there. Then you can add the updated buttons.

* loading in new shelves from the disk: i use a method where you open up the shelf mel file and replace some stuff and eval it. python code here:

fReObj ="(shelfButton.*\;[\r\n])", shelfContents, re.DOTALL)
if fReObj:
shelfBtnStr =
n = shelfBtnStr.replace('shelfButton', 'shelfButton\n -parent ' + shelfName)

that's all i got. it works pretty well once you get it going. best of luck.


01 January 2013, 11:02 PM
Phew! I was looking for some good stuff like this! Thanks man, I'll test it out tomorrow but I'm sure it works.

01 January 2013, 11:38 PM
Here is some abstracted code I use to manage Shelves.
If you can put your Shelves in source control and update Maya's Shelve Directory, then you dont have to worry about the user altering the Shelf and saving a copy in the user folder. Since the shelf is write protected by source control, Maya will be unable to save it when exiting.
This makes updating shelves and ensuring everyone has the latest changes really easy.

Unfortunately, I dont have that luxurty currently, so i made these wrapper functions to manage updating the shelves for me. I used the same trick of leaving the shelf and clearing the buttons.

def createMyShelf():
shelfName = 'My_Shelf'
test = cmds.shelfLayout(shelfName, ex=True)
if test:
# If the shelf already exists, clear the contents and re-add the buttons.
newShelf = shelfName
buttons = cmds.shelfLayout(newShelf, query=True, childArray=True)
cmds.deleteUI(buttons, control=True)
newShelf = mel.eval('addNewShelfTab %s' % shelfName)
# add buttons here

def removeShelf():
shelfName = 'My_Shelf'
test = cmds.shelfLayout(shelfName, ex=True)
if test:
mel.eval('deleteShelfTab %s' % shelfName)
gShelfTopLevel = mel.eval('$tmpVar=$gShelfTopLevel')

CGTalk Moderation
01 January 2013, 11:38 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.