16 #include "config_maya.h"
17 #include "string_utils.h"
20 #include "pre_maya_include.h"
21 #include <maya/MGlobal.h>
22 #include <maya/MDistance.h>
23 #include <maya/MFileIO.h>
24 #include <maya/MLibrary.h>
25 #include <maya/MStatus.h>
26 #include <maya/MFnAnimCurve.h>
27 #include "post_maya_include.h"
38 static MFnAnimCurve force_link_with_OpenMayaAnim;
47 MayaApi(
const string &program_name,
bool view_license,
bool revert_dir) {
48 if (program_name ==
"plug-in") {
68 MStatus stat = MLibrary::initialize(
false, (
char *)program_name.c_str(), view_license);
70 int error_count = init_maya_repeat_count;
71 while (!stat && error_count > 1) {
72 stat.perror(
"MLibrary::initialize");
74 stat = MLibrary::initialize(
false, (
char *)program_name.c_str(), view_license);
82 if (chdir(dirname.c_str()) < 0) {
84 <<
"Unable to restore current directory to " << _cwd
85 <<
" after initializing Maya.\n";
87 if (maya_cat.is_debug()) {
89 <<
"Restored current directory to " << _cwd <<
"\n";
96 stat.perror(
"MLibrary::initialize");
121 operator = (
const MayaApi ©) {
132 nassertv(_global_api ==
this);
133 if (_is_valid && !_plug_in) {
158 open_api(
string program_name,
bool view_license,
bool revertdir) {
159 if (_global_api == (
MayaApi *)NULL) {
161 if (program_name.empty()) {
163 if (program_name.empty()) {
164 program_name =
"Panda";
168 _global_api =
new MayaApi(program_name, view_license, revertdir);
180 string runtime_version = MGlobal::mayaVersion().asChar();
181 string simple_runtime_version = runtime_version;
182 runtime_version = trim(runtime_version);
186 size_t space = runtime_version.find(
' ');
187 if (space != string::npos) {
188 runtime_version = runtime_version.substr(0, space);
191 int rtver_a, rtver_b;
192 size_t dot1 = runtime_version.find(
'.');
193 if (dot1 == string::npos) {
194 string_to_int(runtime_version, rtver_a);
198 string_to_int(runtime_version.substr(0, dot1), rtver_a);
200 size_t dot2 = runtime_version.find(
'.', dot1 + 1);
201 if (dot2 == string::npos) {
204 string_to_int(runtime_version.substr(dot1, dot2 - dot1), rtver_b);
205 simple_runtime_version = runtime_version.substr(0, dot2);
209 int runtime_version_int = rtver_a * 100 + rtver_b * 10;
211 if (maya_cat.is_debug()) {
213 <<
"Compiled with Maya library version "
214 << (MAYA_API_VERSION / 100) <<
"." << (MAYA_API_VERSION / 10) % 10
215 <<
" (" << MAYA_API_VERSION <<
"); running with library version "
216 << runtime_version <<
".\n";
219 if (MAYA_API_VERSION / 10 != runtime_version_int / 10) {
221 <<
"This program was compiled using Maya version "
222 << (MAYA_API_VERSION / 100) <<
"." << (MAYA_API_VERSION / 10) % 10
223 <<
", but you are now running it with Maya version "
224 << simple_runtime_version
225 <<
". The program may crash or produce incorrect results.\n\n";
245 back_to_front_slash(
const string &str) {
248 for (si = result.begin(); si != result.end(); ++si) {
266 MFileIO::newFile(
true);
268 maya_cat.info() <<
"Reading " << filename <<
"\n";
275 if (maya_cat.is_debug()) {
276 maya_cat.debug() <<
"cwd(read:before): " << dirname.c_str() << endl;
279 MFileIO::newFile(
true);
280 MStatus stat = MFileIO::open(os_filename.c_str());
286 if (chdir(dirname.c_str()) < 0) {
288 <<
"Unable to restore current directory after ::read to " << _cwd
289 <<
" after initializing Maya.\n";
291 if (maya_cat.is_debug()) {
293 <<
"Restored current directory after ::read to " << _cwd <<
"\n";
297 stat.perror(os_filename.c_str());
311 maya_cat.info() <<
"Writing " << filename <<
"\n";
315 if (maya_cat.is_debug()) {
316 maya_cat.debug() <<
"cwd(write:before): " << dirname.c_str() << endl;
319 const char *type =
"mayaBinary";
321 if (extension ==
"ma") {
325 MStatus stat = MFileIO::saveAs(os_filename.c_str(), type,
true);
327 stat.perror(os_filename.c_str());
335 if (chdir(dirname.c_str()) < 0) {
337 <<
"Unable to restore current directory after ::write to " << _cwd
338 <<
" after initializing Maya.\n";
340 if (maya_cat.is_debug()) {
342 <<
"Restored current directory after ::write to " << _cwd <<
"\n";
357 MStatus stat = MFileIO::newFile(
true);
359 stat.perror(
"clear");
372 switch (MDistance::internalUnit()) {
373 case MDistance::kInches:
375 case MDistance::kFeet:
377 case MDistance::kYards:
379 case MDistance::kMiles:
380 return DU_statute_miles;
381 case MDistance::kMillimeters:
382 return DU_millimeters;
383 case MDistance::kCentimeters:
384 return DU_centimeters;
385 case MDistance::kKilometers:
386 return DU_kilometers;
387 case MDistance::kMeters:
404 MDistance::setUIUnit(MDistance::kInches);
407 MDistance::setUIUnit(MDistance::kFeet);
410 MDistance::setUIUnit(MDistance::kYards);
412 case DU_statute_miles:
413 MDistance::setUIUnit(MDistance::kMiles);
416 MDistance::setUIUnit(MDistance::kMillimeters);
419 MDistance::setUIUnit(MDistance::kCentimeters);
422 MDistance::setUIUnit(MDistance::kKilometers);
425 MDistance::setUIUnit(MDistance::kMeters);
440 if (MGlobal::isYAxisUp()) {
void set_units(DistanceUnit unit)
Set Maya's UI units.
bool read(const Filename &filename)
Reads the indicated maya file into the global model space.
static string get_binary_name()
Returns the name of the binary executable that started this program, if it can be determined...
bool write(const Filename &filename)
Writes the global model space to the indicated file.
DistanceUnit get_units()
Returns Maya's internal units in effect.
bool clear()
Resets the global model space to the empty state, for instance in preparation for building a new file...
The name of a file, such as a texture file or an Egg file.
bool is_valid() const
Returns true if the API has been successfully opened and may be used, or false if there is some probl...
string to_os_generic() const
This is similar to to_os_specific(), but it is designed to generate a filename that can be understood...
string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
static void sleep(double seconds)
Suspends the current thread for at least the indicated amount of time.
static Filename get_cwd()
Returns the name of the current working directory.
CoordinateSystem get_coordinate_system()
Returns Maya's internal coordinate system in effect.
string get_extension() const
Returns the file extension.
This class presents a wrapper around the global Maya interface.