一半君的总结纸

听话只听一半君

#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 ‘optionVar’ command to save and restore all user settings. This offers a few advantages:

  1. No global variables smattered throughout the script.
  2. Settings are preserved between Maya sessions.
  3. Settings are user-specific.

The following demonstrates saving the user’s preferred filetype to the optionVar ‘MayaDefaultFileType’:

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 $defaultFileType:

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 ‘optionVar’ to control preferences related to a script’s UI controls, in this case the selected tabLayout tab and the collapse status of a frameLayout.

// ** 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;

Wednesday, July 4, 2001

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: