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()) {
149 program_name = ExecutionEnvironment::get_binary_name();
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()) {
int string_to_int(const string &str, string &tail)
A string-interface wrapper around the C library strtol().
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.
get_cwd
Returns the name of the current working directory.
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...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::string to_os_generic() const
This is similar to to_os_specific(), but it is designed to generate a filename that can be understood...
PT(MayaApi) MayaApi
Opens the Maya API, if it is not already open, and returns a pointer representing this connection.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DistanceUnit
This enumerated type lists all the kinds of units we're likely to come across in model conversion pro...
The name of a file, such as a texture file or an Egg file.
string trim(const string &str)
Returns a new string representing the contents of the given string with both leading and trailing whi...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void sleep(double seconds)
Suspends the current thread for at least the indicated amount of time.
std::string get_extension() const
Returns the file extension.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CoordinateSystem get_coordinate_system()
Returns Maya's internal coordinate system in effect.
std::string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class presents a wrapper around the global Maya interface.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.