Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/maya/mayaApi.cxx

Go to the documentation of this file.
00001 // Filename: mayaApi.cxx
00002 // Created by:  drose (15Apr02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "mayaApi.h"
00020 #include "config_maya.h"
00021 
00022 #include "pre_maya_include.h"
00023 #include <maya/MGlobal.h>
00024 #include <maya/MDistance.h>
00025 #include <maya/MFileIO.h>
00026 #include <maya/MLibrary.h>
00027 #include <maya/MStatus.h>
00028 #include "post_maya_include.h"
00029 
00030 #ifdef WIN32_VC
00031 #include <direct.h>  // for chdir()
00032 #endif
00033 
00034 MayaApi *MayaApi::_global_api = (MayaApi *)NULL;
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: MayaApi::Constructor
00038 //       Access: Protected
00039 //  Description: Don't attempt to create this object directly;
00040 //               instead, use the open_api() method.
00041 ////////////////////////////////////////////////////////////////////
00042 MayaApi::
00043 MayaApi(const string &program_name) {
00044   if (program_name == "plug-in") {
00045     // In this special case, we are invoking the code from within a
00046     // plug-in, so we need not (and should not) call
00047     // MLibrary::initialize().
00048     _plug_in = true;
00049     _is_valid = true;
00050     return;
00051   }
00052 
00053   // Otherwise, if program_name is any other name, we are invoking the
00054   // code from a standalone application and we do need to call
00055   // MLibrary::initialize().
00056   _plug_in = false;
00057 
00058   // Beginning with Maya4.5, the call to initialize seems to change
00059   // the current directory!  Yikes!
00060 
00061   // Furthermore, the current directory may change during the call to
00062   // any Maya function!  Egad!
00063   Filename cwd = ExecutionEnvironment::get_cwd();
00064   MStatus stat = MLibrary::initialize((char *)program_name.c_str());
00065   
00066   // Restore the current directory.
00067   string dirname = cwd.to_os_specific();
00068   if (chdir(dirname.c_str()) < 0) {
00069     maya_cat.warning()
00070       << "Unable to restore current directory to " << cwd
00071       << " after initializing Maya.\n";
00072   } else {
00073     if (maya_cat.is_debug()) {
00074       maya_cat.debug()
00075         << "Restored current directory to " << cwd << "\n";
00076     }
00077   }
00078 
00079   if (!stat) {
00080     stat.perror("MLibrary::initialize");
00081     _is_valid = false;
00082   } else {
00083     _is_valid = true;
00084   }
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: MayaApi::Copy Constructor
00089 //       Access: Protected
00090 //  Description: Don't attempt to copy MayaApi objects.  There should
00091 //               be only one of these in the world at a time.
00092 ////////////////////////////////////////////////////////////////////
00093 MayaApi::
00094 MayaApi(const MayaApi &copy) {
00095   nassertv(false);
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: MayaApi::Copy Assignment Operator
00100 //       Access: Protected
00101 //  Description: Don't attempt to copy MayaApi objects.  There should
00102 //               be only one of these in the world at a time.
00103 ////////////////////////////////////////////////////////////////////
00104 void MayaApi::
00105 operator = (const MayaApi &copy) {
00106   nassertv(false);
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: MayaApi::Destructor
00111 //       Access: Public
00112 //  Description: 
00113 ////////////////////////////////////////////////////////////////////
00114 MayaApi::
00115 ~MayaApi() {
00116   nassertv(_global_api == this);
00117   if (_is_valid && !_plug_in) {
00118     // Caution!  Calling this function seems to call exit() somewhere
00119     // within Maya code.
00120     MLibrary::cleanup();
00121   }
00122   _global_api = (MayaApi *)NULL;
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: MayaApi::open_api
00127 //       Access: Public, Static
00128 //  Description: Opens the Maya API, if it is not already open, and
00129 //               returns a pointer representing this connection.  When
00130 //               you are done using the Maya API, let the pointer
00131 //               destruct.
00132 //
00133 //               If program_name is supplied, it is passed to Maya as
00134 //               the name of the currently-executing program.
00135 //               Otherwise, the current program name is extracted from
00136 //               the execution environment, if possible.  The special
00137 //               program_name "plug-in" is used for code that is
00138 //               intended to be invoked as a plug-in only; in this
00139 //               case, the maya library is not re-initialized.
00140 ////////////////////////////////////////////////////////////////////
00141 PT(MayaApi) MayaApi::
00142 open_api(string program_name) {
00143   if (_global_api == (MayaApi *)NULL) {
00144     // We need to create a new MayaApi object.
00145     if (program_name.empty()) {
00146       program_name = ExecutionEnvironment::get_binary_name();
00147       if (program_name.empty()) {
00148         program_name = "Panda";
00149       }
00150     }
00151 
00152     _global_api = new MayaApi(program_name);
00153   }
00154 
00155   return _global_api;
00156 }
00157 
00158 ////////////////////////////////////////////////////////////////////
00159 //     Function: MayaApi::is_valid
00160 //       Access: Public
00161 //  Description: Returns true if the API has been successfully opened
00162 //               and may be used, or false if there is some problem.
00163 ////////////////////////////////////////////////////////////////////
00164 bool MayaApi::
00165 is_valid() const {
00166   return _is_valid;
00167 }
00168 
00169 #ifdef WIN32
00170 static string
00171 back_to_front_slash(const string &str) {
00172   string result = str;
00173   string::iterator si;
00174   for (si = result.begin(); si != result.end(); ++si) {
00175     if ((*si) == '\\') {
00176       (*si) = '/';
00177     }
00178   }
00179 
00180   return result;
00181 }
00182 #endif  // WIN32
00183 
00184 ////////////////////////////////////////////////////////////////////
00185 //     Function: MayaApi::read
00186 //       Access: Public
00187 //  Description: Reads the indicated maya file into the global model
00188 //               space.  Returns true if successful, false otherwise.
00189 ////////////////////////////////////////////////////////////////////
00190 bool MayaApi::
00191 read(const Filename &filename) {
00192   MFileIO::newFile(true);
00193 
00194   maya_cat.info() << "Reading " << filename << "\n";
00195 
00196   // Load the file into Maya
00197   string os_filename = filename.to_os_specific();
00198 
00199 #ifdef WIN32
00200   // Actually, Maya seems to want forward slashes, even on Windows.
00201   os_filename = back_to_front_slash(os_filename);
00202 #endif
00203 
00204   MStatus stat = MFileIO::open(os_filename.c_str());
00205   if (!stat) {
00206     stat.perror(os_filename.c_str());
00207     return false;
00208   }
00209   return true;
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: MayaApi::write
00214 //       Access: Public
00215 //  Description: Writes the global model space to the indicated file.
00216 //               Returns true if successful, false otherwise.
00217 ////////////////////////////////////////////////////////////////////
00218 bool MayaApi::
00219 write(const Filename &filename) {
00220   maya_cat.info() << "Writing " << filename << "\n";
00221   string os_filename = filename.to_os_specific();
00222 #ifdef WIN32
00223   os_filename = back_to_front_slash(os_filename);
00224 #endif
00225 
00226   const char *type = "mayaBinary";
00227   string extension = filename.get_extension();
00228   if (extension == "ma") {
00229     type = "mayaAscii";
00230   }
00231 
00232   MStatus stat = MFileIO::saveAs(os_filename.c_str(), type, true);
00233   if (!stat) {
00234     stat.perror(os_filename.c_str());
00235     return false;
00236   }
00237   return true;
00238 }
00239 
00240 ////////////////////////////////////////////////////////////////////
00241 //     Function: MayaApi::clear
00242 //       Access: Public
00243 //  Description: Resets the global model space to the empty state, for
00244 //               instance in preparation for building a new file.
00245 //               Returns true if successful, false otherwise.
00246 ////////////////////////////////////////////////////////////////////
00247 bool MayaApi::
00248 clear() {
00249   MStatus stat = MFileIO::newFile(true);
00250   if (!stat) {
00251     stat.perror("clear");
00252     return false;
00253   }
00254   return true;
00255 }
00256 
00257 ////////////////////////////////////////////////////////////////////
00258 //     Function: MayaApi::get_units
00259 //       Access: Public
00260 //  Description: Returns Maya's internal units in effect.
00261 ////////////////////////////////////////////////////////////////////
00262 DistanceUnit MayaApi::
00263 get_units() {
00264   switch (MDistance::internalUnit()) {
00265   case MDistance::kInches:
00266     return DU_inches;
00267   case MDistance::kFeet:
00268     return DU_feet;
00269   case MDistance::kYards:
00270     return DU_yards;
00271   case MDistance::kMiles:
00272     return DU_statute_miles;
00273   case MDistance::kMillimeters:
00274     return DU_millimeters;
00275   case MDistance::kCentimeters:
00276     return DU_centimeters;
00277   case MDistance::kKilometers:
00278     return DU_kilometers;
00279   case MDistance::kMeters:
00280     return DU_meters;
00281 
00282   default:
00283     return DU_invalid;
00284   }
00285 }
00286 
00287 ////////////////////////////////////////////////////////////////////
00288 //     Function: MayaApi::get_coordinate_system
00289 //       Access: Public
00290 //  Description: Returns Maya's internal coordinate system in effect.
00291 ////////////////////////////////////////////////////////////////////
00292 CoordinateSystem MayaApi::
00293 get_coordinate_system() {
00294   if (MGlobal::isYAxisUp()) {
00295     return CS_yup_right;
00296   } else {
00297     return CS_zup_right;
00298   }
00299 }

Generated on Fri May 2 03:21:18 2003 for Panda-Tool by doxygen1.3