一半君的总结纸

听话只听一半君

#102 如何转换Maya的内部单位 (‘cm’) 和UI上显示的数值?

下面这两个procedure可以用来在 Maya的 internal unit 和 UI unit之间进行转换.

proc float internalToUI( float $value )
{
  // Query Maya's UI linear unit.
  //
  string $unit = `currentUnit -q -l`;

  // Default ('cm') is unchanged.
  //
  float $factor = 1.0;

  // Factor used to convert Maya's internal "cm"
  // to the following units.
  //
  switch ( $unit )
  {
      case "in":
          $factor =  0.393700787;
          break;
      case "ft":
          $factor =  0.032808399;
          break;
      case "yd":
          $factor =  0.010936133;
          break;
      case "mi":
          $factor =  0.000006214;
          break;
      case "mm":
          $factor = 10.000000000;
          break;
      case "km":
          $factor =  0.000010000;
          break;
      case "m":
          $factor =  0.010000000;
          break;
  }

  // Adjust the value by the appropriate factor.
  //
  $value *= $factor;

  return $value;
}

proc float uiToInternal( float $value )
{
  // Query Maya's UI linear unit.
  //
  string $unit = `currentUnit -q -l`;

  // Default ('cm') is unchanged.
  //
  float $factor = 1.0;

  // Factor used to convert the following units
  // to Maya's internal "cm".
  //
  switch ( $unit )
  {
      case "in":
          $factor =      2.540000000;
          break;
      case "ft":
          $factor =     30.480000000;
          break;
      case "yd":
          $factor =     91.440000000;
          break;
      case "mi":
          $factor = 160934.400000000;
          break;
      case "mm":
          $factor =      0.100000000;
          break;
      case "km":
          $factor = 100000.000000000;
          break;
      case "m":
          $factor =    100.000000000;
          break;
  }

  // Adjust the value by the appropriate factor.
  //
  $value *= $factor;

  return $value;
}

使用举例:

  // Maya is configured to use 'meter' as its UI unit.
  //
  currentUnit -q -l;
  // Result: m //

  internalToUI( 3.14 );
  // Result: 0.0314 //

  uiToInternal( 3.14 );
  // Result: 314 //

 

下面是使用 API 同样达成上面的效果的写法:

const unsigned int numUnits = 8;
unsigned int u;

const MDistance::Unit unit[numUnits] =
{
    MDistance::kInches,
    MDistance::kFeet,
    MDistance::kYards,
    MDistance::kMiles,
    MDistance::kMillimeters,
    MDistance::kKilometers,
    MDistance::kMeters
};

const char* unitNames[numUnits] =
{
    "in",
    "ft",
    "yd",
    "mi",
    "mm",
    "km",
    "m"
};

MDistance distance( 1.0, MDistance::kCentimeters );

// internalToUI
//

clog << "// internalToUI" << endl;
clog << "// Factor used to convert Maya's internal \"cm\"" << endl;
clog << "// to the following units." << endl;
clog << "//" << endl;
clog << "switch ( $unit )" << endl << "{" << endl;
for ( u = 0; u < numUnits; u++ )
{
    clog << "    case \"" << unitNames[u] << "\":" << endl;
    clog << "        $factor = ";
    clog << setiosflags(ios::fixed) << setprecision(9);
    clog << distance.as( unit[u] ) << ";" << endl;
    clog << "        break;" << endl;
}
clog << "}" << endl;

// uiToInternal
//

clog << "// uiToInternal" << endl;
clog << "// Factor used to convert the following units" << endl;
clog << "// to Maya's internal \"cm\"." << endl;
clog << "//" << endl;
clog << "switch ( $unit )" << endl << "{" << endl;
for ( u = 0; u < numUnits; u++ )
{
    distance.setUnit( unit[u] );
    clog << "    case \"" << unitNames[u] << "\":" << endl;
    clog << "        $factor = ";
    clog << setiosflags(ios::fixed) << setprecision(9);
    clog << distance.as( MDistance::kCentimeters ) << ";" << endl;
    clog << "        break;" << endl;
}
clog << "}" << endl;

31 Jul 2004

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 博主赞过: