20 #include <maya/MGlobal.h>
21 #include <maya/MDistance.h>
22 #include <maya/MFileIO.h>
23 #include <maya/MLibrary.h>
24 #include <maya/MStatus.h>
25 #include <maya/MFnAnimCurve.h>
34 MayaApi *MayaApi::_global_api =
nullptr;
39 static MFnAnimCurve force_link_with_OpenMayaAnim;
46 MayaApi(
const string &program_name,
bool view_license,
bool revert_dir) {
47 if (program_name ==
"plug-in") {
66 MStatus stat = MLibrary::initialize(
false, (
char *)program_name.c_str(), view_license);
68 int error_count = init_maya_repeat_count;
69 while (!stat && error_count > 1) {
70 stat.perror(
"MLibrary::initialize");
72 stat = MLibrary::initialize(
false, (
char *)program_name.c_str(), view_license);
80 if (chdir(dirname.c_str()) < 0) {
82 <<
"Unable to restore current directory to " << _cwd
83 <<
" after initializing Maya.\n";
85 if (maya_cat.is_debug()) {
87 <<
"Restored current directory to " << _cwd <<
"\n";
94 stat.perror(
"MLibrary::initialize");
115 operator = (
const MayaApi ©) {
124 nassertv(_global_api ==
this);
125 if (_is_valid && !_plug_in) {
130 _global_api =
nullptr;
145 open_api(
string program_name,
bool view_license,
bool revertdir) {
146 if (_global_api ==
nullptr) {
148 if (program_name.empty()) {
150 if (program_name.empty()) {
151 program_name =
"Panda";
155 _global_api =
new MayaApi(program_name, view_license, revertdir);
167 string runtime_version = MGlobal::mayaVersion().asChar();
168 string simple_runtime_version = runtime_version;
169 runtime_version =
trim(runtime_version);
173 size_t space = runtime_version.find(
' ');
174 if (space != string::npos) {
175 runtime_version = runtime_version.substr(0, space);
178 int rtver_a, rtver_b;
179 size_t dot1 = runtime_version.find(
'.');
180 if (dot1 == string::npos) {
187 size_t dot2 = runtime_version.find(
'.', dot1 + 1);
188 if (dot2 == string::npos) {
192 string_to_int(runtime_version.substr(dot1 + 1, dot2 - dot1 - 1), rtver_b);
193 simple_runtime_version = runtime_version.substr(0, dot2);
197 int runtime_version_int = rtver_a * 100 + rtver_b * 10;
199 if (maya_cat.is_debug()) {
201 <<
"Compiled with Maya library version "
202 << (MAYA_API_VERSION / 100) <<
"." << (MAYA_API_VERSION / 10) % 10
203 <<
" (" << MAYA_API_VERSION <<
"); running with library version "
204 << runtime_version <<
".\n";
207 if (MAYA_API_VERSION / 10 != runtime_version_int / 10) {
209 <<
"This program was compiled using Maya version "
210 << (MAYA_API_VERSION / 100) <<
"." << (MAYA_API_VERSION / 10) % 10
211 <<
", but you are now running it with Maya version "
212 << simple_runtime_version
213 <<
". The program may crash or produce incorrect results.\n\n";
231 back_to_front_slash(
const string &str) {
234 for (si = result.begin(); si != result.end(); ++si) {
250 MFileIO::newFile(
true);
252 maya_cat.info() <<
"Reading " << filename <<
"\n";
259 if (maya_cat.is_debug()) {
260 maya_cat.debug() <<
"cwd(read:before): " << dirname.c_str() << std::endl;
263 MFileIO::newFile(
true);
264 MStatus stat = MFileIO::open(os_filename.c_str());
270 if (chdir(dirname.c_str()) < 0) {
272 <<
"Unable to restore current directory after ::read to " << _cwd
273 <<
" after initializing Maya.\n";
275 if (maya_cat.is_debug()) {
277 <<
"Restored current directory after ::read to " << _cwd <<
"\n";
281 stat.perror(os_filename.c_str());
293 maya_cat.info() <<
"Writing " << filename <<
"\n";
297 if (maya_cat.is_debug()) {
298 maya_cat.debug() <<
"cwd(write:before): " << dirname.c_str() << std::endl;
301 const char *type =
"mayaBinary";
303 if (extension ==
"ma") {
307 MStatus stat = MFileIO::saveAs(os_filename.c_str(), type,
true);
309 stat.perror(os_filename.c_str());
317 if (chdir(dirname.c_str()) < 0) {
319 <<
"Unable to restore current directory after ::write to " << _cwd
320 <<
" after initializing Maya.\n";
322 if (maya_cat.is_debug()) {
324 <<
"Restored current directory after ::write to " << _cwd <<
"\n";
337 MStatus stat = MFileIO::newFile(
true);
339 stat.perror(
"clear");
350 switch (MDistance::internalUnit()) {
351 case MDistance::kInches:
353 case MDistance::kFeet:
355 case MDistance::kYards:
357 case MDistance::kMiles:
358 return DU_statute_miles;
359 case MDistance::kMillimeters:
360 return DU_millimeters;
361 case MDistance::kCentimeters:
362 return DU_centimeters;
363 case MDistance::kKilometers:
364 return DU_kilometers;
365 case MDistance::kMeters:
380 MDistance::setUIUnit(MDistance::kInches);
383 MDistance::setUIUnit(MDistance::kFeet);
386 MDistance::setUIUnit(MDistance::kYards);
388 case DU_statute_miles:
389 MDistance::setUIUnit(MDistance::kMiles);
392 MDistance::setUIUnit(MDistance::kMillimeters);
395 MDistance::setUIUnit(MDistance::kCentimeters);
398 MDistance::setUIUnit(MDistance::kKilometers);
401 MDistance::setUIUnit(MDistance::kMeters);
414 if (MGlobal::isYAxisUp()) {