00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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>
00032 #endif
00033
00034 MayaApi *MayaApi::_global_api = (MayaApi *)NULL;
00035
00036
00037
00038
00039
00040
00041
00042 MayaApi::
00043 MayaApi(const string &program_name) {
00044 if (program_name == "plug-in") {
00045
00046
00047
00048 _plug_in = true;
00049 _is_valid = true;
00050 return;
00051 }
00052
00053
00054
00055
00056 _plug_in = false;
00057
00058
00059
00060
00061
00062
00063 Filename cwd = ExecutionEnvironment::get_cwd();
00064 MStatus stat = MLibrary::initialize((char *)program_name.c_str());
00065
00066
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
00089
00090
00091
00092
00093 MayaApi::
00094 MayaApi(const MayaApi ©) {
00095 nassertv(false);
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 void MayaApi::
00105 operator = (const MayaApi ©) {
00106 nassertv(false);
00107 }
00108
00109
00110
00111
00112
00113
00114 MayaApi::
00115 ~MayaApi() {
00116 nassertv(_global_api == this);
00117 if (_is_valid && !_plug_in) {
00118
00119
00120 MLibrary::cleanup();
00121 }
00122 _global_api = (MayaApi *)NULL;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 PT(MayaApi) MayaApi::
00142 open_api(string program_name) {
00143 if (_global_api == (MayaApi *)NULL) {
00144
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
00160
00161
00162
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
00186
00187
00188
00189
00190 bool MayaApi::
00191 read(const Filename &filename) {
00192 MFileIO::newFile(true);
00193
00194 maya_cat.info() << "Reading " << filename << "\n";
00195
00196
00197 string os_filename = filename.to_os_specific();
00198
00199 #ifdef WIN32
00200
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
00214
00215
00216
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
00242
00243
00244
00245
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
00259
00260
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
00289
00290
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 }