MEL How-To #12 | ||
| ||
How do I save preferences for my scripts so they are recalled in subsequent sessions?I'll start with a homework assignment. Study up on: optionVar I'm not a fan of smattering global variables throughout a script. I prefer to use the ‘
The following demonstrates saving the user's preferred filetype to the optionVar ‘ string $defaultFileType = "MayaAscii"; optionVar -stringValue MayaDefaultFileType $defaultFileType; Note: If you query an optionVar that has not been set it will return an int value of 0. Always provide a suitable default value for all preferences, then check if the optionVar has been set before reading it into your preference settings. The following demonstrates restoring the user's preferred filetype to the string variable string $defaultFileType = "MayaBinary"; // Ensure optionVar has been set by user if ( `optionVar -exists MayaDefaultFileType` ) $defaultFileType = `optionVar -q MayaDefaultFileType`; // If optionVar has not been set, initialize with default else optionVar -stringValue MayaDefaultFileType $defaultFileType; I try to create all UIs to be as user-friendly as possible. This includes the saving of state information that reflect the user's interaction with the UI. The following demonstrates the use of ‘ // ** Restore preferences ** int $tab = 1; int $collapse = 1; if ( `optionVar -exists myTab` ) $tab = `optionVar -q myTab`; else optionVar -intValue myTab $tab; if ( `optionVar -exists collapseStatus` ) $collapse = `optionVar -q collapseStatus`; else optionVar -intValue collapseStatus $collapse; // ** Propogate preferences to UI ** tabLayout -e -selectTabIndex $tab myTabLayout; frameLayout -e -collapse $collapse myFrameLayout; // ... // // ** Save preferences ** $tab = `tabLayout -q -selectTabIndex myTabLayout`; $collapse = `frameLayout -q -collapse myFrameLayout`; optionVar -intValue myTab $tab; optionVar -intValue collapseStatus $collapse; | ||
Copyright ©2005 by Bryan Ewert, maya@ewertb.com Maya is a Registered Trademark of Alias |