BLOGGER TEMPLATES AND TWITTER BACKGROUNDS »

Friday, May 14, 2010

Using evalDeferred() to dynamically update window size

Often times when making tools the GUI changes if you add new buttons/controls, change between tabs, or expand and collapse frameLayouts. Using evalDeferred and querying the size of a top layout in your window is a quick way to update the size of your window (or any other control for that matter) without hardcoding in values.

The reason behind using evalDeferred rather than just calling the procedure is that if you are loading a complex GUI, or building it on the fly, it might execute the bit of code before Maya has updated it's values. This way you make sure that it's not querying the values of the layouts before they have updated.

// evalDeferred("command") WILL WAIT TILL MAYA IS IDLE

// QUERY THE HEIGHT OF THE TOP MOST LAYOUT AND MAKE THAT THE WINDOW HEIGHT
global proc updateMyWinSize() {
int $tabSize = `rowColumnLayout -q -h myTopLayout`;
$tabSize += 30;
window -e -w 160 -h $tabSize autoUpdateWin;
}

// GET ALL THE CHILDREN IN THE LAYOUT AND DELETE THEM
// THEN UPDATE THE WINDOW SIZE
global proc clearButtons() {
string $children[] = `columnLayout -q -ca newButtonLayout`;
for ($c in $children) {
deleteUI $c;
}
evalDeferred("updateMyWinSize");
}

// ADDS A NEW BUTTON
global proc addNewButton() {
setParent newButtonLayout;
button -l "NEW BUTTON YAY!!!!";
evalDeferred("updateMyWinSize");
}

if (`window -q -ex autoUpdateWin`) deleteUI autoUpdateWin;
window -t "AutoMatic Size Updating" autoUpdateWin;
columnLayout;
// LAYOUT THAT WILL BE QUERIED TO FIND THE HEIGHT NEEDED
rowColumnLayout -nc 1 -cw 1 150 myTopLayout;
// MAKES A BUTTON THAT WILL SET THE PARENT, THEN ADD A NEW BUTTON
button -l "ADD MORE BUTTONS!!!!" -c "addNewButton();";
button -l "CLEAR THEM ALL!!!" -c "clearButtons();";
separator -h 15;
// THE LAYOUT THAT WILL HOUSE ALL THE OF THE NEW BUTTONS
columnLayout -adj 1 newButtonLayout;
showWindow autoUpdateWin;
updateMyWinSize();

0 comments: