00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "mayaApi.h"
00016 #include "config_maya.h"
00017 #include "string_utils.h"
00018 #include "thread.h"
00019
00020 #include "pre_maya_include.h"
00021 #include <maya/MGlobal.h>
00022 #include <maya/MDistance.h>
00023 #include <maya/MFileIO.h>
00024 #include <maya/MLibrary.h>
00025 #include <maya/MStatus.h>
00026 #include <maya/MFnAnimCurve.h>
00027 #include "post_maya_include.h"
00028
00029 #ifdef WIN32_VC
00030 #include <direct.h>
00031 #endif
00032
00033 MayaApi *MayaApi::_global_api = (MayaApi *)NULL;
00034
00035
00036
00037
00038 static MFnAnimCurve force_link_with_OpenMayaAnim;
00039
00040
00041
00042
00043
00044
00045
00046 MayaApi::
00047 MayaApi(const string &program_name, bool view_license, bool revert_dir) {
00048 if (program_name == "plug-in") {
00049
00050
00051
00052 _plug_in = true;
00053 _is_valid = true;
00054 return;
00055 }
00056
00057
00058
00059
00060 _plug_in = false;
00061
00062
00063
00064
00065
00066
00067 _cwd = ExecutionEnvironment::get_cwd();
00068 MStatus stat = MLibrary::initialize(false, (char *)program_name.c_str(), view_license);
00069
00070 int error_count = init_maya_repeat_count;
00071 while (!stat && error_count > 1) {
00072 stat.perror("MLibrary::initialize");
00073 Thread::sleep(init_maya_timeout);
00074 stat = MLibrary::initialize(false, (char *)program_name.c_str(), view_license);
00075 --error_count;
00076 }
00077
00078
00079
00080 if( revert_dir ){
00081 string dirname = _cwd.to_os_specific();
00082 if (chdir(dirname.c_str()) < 0) {
00083 maya_cat.warning()
00084 << "Unable to restore current directory to " << _cwd
00085 << " after initializing Maya.\n";
00086 } else {
00087 if (maya_cat.is_debug()) {
00088 maya_cat.debug()
00089 << "Restored current directory to " << _cwd << "\n";
00090 }
00091 }
00092 }
00093
00094
00095 if (!stat) {
00096 stat.perror("MLibrary::initialize");
00097 _is_valid = false;
00098 } else {
00099 _is_valid = true;
00100 }
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 MayaApi::
00110 MayaApi(const MayaApi ©) {
00111 nassertv(false);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 void MayaApi::
00121 operator = (const MayaApi ©) {
00122 nassertv(false);
00123 }
00124
00125
00126
00127
00128
00129
00130 MayaApi::
00131 ~MayaApi() {
00132 nassertv(_global_api == this);
00133 if (_is_valid && !_plug_in) {
00134
00135
00136 MLibrary::cleanup();
00137 }
00138 _global_api = (MayaApi *)NULL;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 PT(MayaApi) MayaApi::
00158 open_api(string program_name, bool view_license, bool revertdir) {
00159 if (_global_api == (MayaApi *)NULL) {
00160
00161 if (program_name.empty()) {
00162 program_name = ExecutionEnvironment::get_binary_name();
00163 if (program_name.empty()) {
00164 program_name = "Panda";
00165 }
00166 }
00167
00168 _global_api = new MayaApi(program_name, view_license, revertdir);
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 string runtime_version = MGlobal::mayaVersion().asChar();
00181 string simple_runtime_version = runtime_version;
00182 runtime_version = trim(runtime_version);
00183
00184
00185
00186 size_t space = runtime_version.find(' ');
00187 if (space != string::npos) {
00188 runtime_version = runtime_version.substr(0, space);
00189 }
00190
00191 int rtver_a, rtver_b;
00192 size_t dot1 = runtime_version.find('.');
00193 if (dot1 == string::npos) {
00194 string_to_int(runtime_version, rtver_a);
00195 rtver_b = 0;
00196
00197 } else {
00198 string_to_int(runtime_version.substr(0, dot1), rtver_a);
00199
00200 size_t dot2 = runtime_version.find('.', dot1 + 1);
00201 if (dot2 == string::npos) {
00202 rtver_b = 0;
00203 } else {
00204 string_to_int(runtime_version.substr(dot1, dot2 - dot1), rtver_b);
00205 simple_runtime_version = runtime_version.substr(0, dot2);
00206 }
00207 }
00208
00209 int runtime_version_int = rtver_a * 100 + rtver_b * 10;
00210
00211 if (maya_cat.is_debug()) {
00212 maya_cat.debug()
00213 << "Compiled with Maya library version "
00214 << (MAYA_API_VERSION / 100) << "." << (MAYA_API_VERSION / 10) % 10
00215 << " (" << MAYA_API_VERSION << "); running with library version "
00216 << runtime_version << ".\n";
00217 }
00218
00219 if (MAYA_API_VERSION / 10 != runtime_version_int / 10) {
00220 maya_cat.warning()
00221 << "This program was compiled using Maya version "
00222 << (MAYA_API_VERSION / 100) << "." << (MAYA_API_VERSION / 10) % 10
00223 << ", but you are now running it with Maya version "
00224 << simple_runtime_version
00225 << ". The program may crash or produce incorrect results.\n\n";
00226 }
00227 }
00228
00229 return _global_api;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238 bool MayaApi::
00239 is_valid() const {
00240 return _is_valid;
00241 }
00242
00243 #ifdef WIN32
00244 static string
00245 back_to_front_slash(const string &str) {
00246 string result = str;
00247 string::iterator si;
00248 for (si = result.begin(); si != result.end(); ++si) {
00249 if ((*si) == '\\') {
00250 (*si) = '/';
00251 }
00252 }
00253
00254 return result;
00255 }
00256 #endif // WIN32
00257
00258
00259
00260
00261
00262
00263
00264 bool MayaApi::
00265 read(const Filename &filename) {
00266 MFileIO::newFile(true);
00267
00268 maya_cat.info() << "Reading " << filename << "\n";
00269
00270
00271
00272 string os_filename = filename.to_os_generic();
00273
00274 string dirname = _cwd.to_os_specific();
00275 if (maya_cat.is_debug()) {
00276 maya_cat.debug() << "cwd(read:before): " << dirname.c_str() << endl;
00277 }
00278
00279 MFileIO::newFile(true);
00280 MStatus stat = MFileIO::open(os_filename.c_str());
00281
00282
00283
00284
00285
00286 if (chdir(dirname.c_str()) < 0) {
00287 maya_cat.warning()
00288 << "Unable to restore current directory after ::read to " << _cwd
00289 << " after initializing Maya.\n";
00290 } else {
00291 if (maya_cat.is_debug()) {
00292 maya_cat.debug()
00293 << "Restored current directory after ::read to " << _cwd << "\n";
00294 }
00295 }
00296 if (!stat) {
00297 stat.perror(os_filename.c_str());
00298 return false;
00299 }
00300 return true;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309 bool MayaApi::
00310 write(const Filename &filename) {
00311 maya_cat.info() << "Writing " << filename << "\n";
00312 string os_filename = filename.to_os_generic();
00313
00314 string dirname = _cwd.to_os_specific();
00315 if (maya_cat.is_debug()) {
00316 maya_cat.debug() << "cwd(write:before): " << dirname.c_str() << endl;
00317 }
00318
00319 const char *type = "mayaBinary";
00320 string extension = filename.get_extension();
00321 if (extension == "ma") {
00322 type = "mayaAscii";
00323 }
00324
00325 MStatus stat = MFileIO::saveAs(os_filename.c_str(), type, true);
00326 if (!stat) {
00327 stat.perror(os_filename.c_str());
00328 return false;
00329 }
00330
00331
00332
00333
00334
00335 if (chdir(dirname.c_str()) < 0) {
00336 maya_cat.warning()
00337 << "Unable to restore current directory after ::write to " << _cwd
00338 << " after initializing Maya.\n";
00339 } else {
00340 if (maya_cat.is_debug()) {
00341 maya_cat.debug()
00342 << "Restored current directory after ::write to " << _cwd << "\n";
00343 }
00344 }
00345 return true;
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355 bool MayaApi::
00356 clear() {
00357 MStatus stat = MFileIO::newFile(true);
00358 if (!stat) {
00359 stat.perror("clear");
00360 return false;
00361 }
00362 return true;
00363 }
00364
00365
00366
00367
00368
00369
00370 DistanceUnit MayaApi::
00371 get_units() {
00372 switch (MDistance::internalUnit()) {
00373 case MDistance::kInches:
00374 return DU_inches;
00375 case MDistance::kFeet:
00376 return DU_feet;
00377 case MDistance::kYards:
00378 return DU_yards;
00379 case MDistance::kMiles:
00380 return DU_statute_miles;
00381 case MDistance::kMillimeters:
00382 return DU_millimeters;
00383 case MDistance::kCentimeters:
00384 return DU_centimeters;
00385 case MDistance::kKilometers:
00386 return DU_kilometers;
00387 case MDistance::kMeters:
00388 return DU_meters;
00389
00390 default:
00391 return DU_invalid;
00392 }
00393 }
00394
00395
00396
00397
00398
00399
00400 void MayaApi::
00401 set_units(DistanceUnit unit) {
00402 switch (unit) {
00403 case DU_inches:
00404 MDistance::setUIUnit(MDistance::kInches);
00405 break;
00406 case DU_feet:
00407 MDistance::setUIUnit(MDistance::kFeet);
00408 break;
00409 case DU_yards:
00410 MDistance::setUIUnit(MDistance::kYards);
00411 break;
00412 case DU_statute_miles:
00413 MDistance::setUIUnit(MDistance::kMiles);
00414 break;
00415 case DU_millimeters:
00416 MDistance::setUIUnit(MDistance::kMillimeters);
00417 break;
00418 case DU_centimeters:
00419 MDistance::setUIUnit(MDistance::kCentimeters);
00420 break;
00421 case DU_kilometers:
00422 MDistance::setUIUnit(MDistance::kKilometers);
00423 break;
00424 case DU_meters:
00425 MDistance::setUIUnit(MDistance::kMeters);
00426 break;
00427
00428 default:
00429 ;
00430 }
00431 }
00432
00433
00434
00435
00436
00437
00438 CoordinateSystem MayaApi::
00439 get_coordinate_system() {
00440 if (MGlobal::isYAxisUp()) {
00441 return CS_yup_right;
00442 } else {
00443 return CS_zup_right;
00444 }
00445 }