diff --git a/pandatool/src/mayaegg/mayaApi.cxx b/pandatool/src/mayaegg/mayaApi.cxx index e894f908eb..e6d1752d5a 100644 --- a/pandatool/src/mayaegg/mayaApi.cxx +++ b/pandatool/src/mayaegg/mayaApi.cxx @@ -77,6 +77,8 @@ MayaApi:: ~MayaApi() { nassertv(_global_api == this); if (_is_valid) { + // Caution! Calling this function seems to call exit() somewhere + // within Maya code. MLibrary::cleanup(); } _global_api = (MayaApi *)NULL; @@ -123,6 +125,21 @@ is_valid() const { return _is_valid; } +#ifdef WIN32 +static string +back_to_front_slash(const string &str) { + string result = str; + string::iterator si; + for (si = result.begin(); si != result.end(); ++si) { + if ((*si) == '\\') { + (*si) = '/'; + } + } + + return result; +} +#endif // WIN32 + //////////////////////////////////////////////////////////////////// // Function: MayaApi::read // Access: Public @@ -136,6 +153,12 @@ read(const Filename &filename) { mayaegg_cat.info() << "Reading " << filename << "\n"; // Load the file into Maya string os_filename = filename.to_os_specific(); + +#ifdef WIN32 + // Actually, Maya seems to want forward slashes, even on Windows. + os_filename = back_to_front_slash(os_filename); +#endif + MStatus stat = MFileIO::open(os_filename.c_str()); if (!stat) { stat.perror(filename.c_str()); diff --git a/pandatool/src/mayaegg/mayaToEggConverter.cxx b/pandatool/src/mayaegg/mayaToEggConverter.cxx index 990a379d9b..477aea221a 100644 --- a/pandatool/src/mayaegg/mayaToEggConverter.cxx +++ b/pandatool/src/mayaegg/mayaToEggConverter.cxx @@ -66,9 +66,9 @@ //////////////////////////////////////////////////////////////////// MayaToEggConverter:: MayaToEggConverter(const string &program_name) : - _shaders(this) + _shaders(this), + _program_name(program_name) { - _maya = MayaApi::open_api(program_name); _polygon_output = false; _polygon_tolerance = 0.01; _ignore_transforms = false; @@ -93,9 +93,7 @@ MayaToEggConverter(const MayaToEggConverter ©) : //////////////////////////////////////////////////////////////////// MayaToEggConverter:: ~MayaToEggConverter() { - // We have to clear the shaders before we release the Maya API. - _shaders.clear(); - _maya.clear(); + close_api(); } //////////////////////////////////////////////////////////////////// @@ -144,7 +142,7 @@ get_extension() const { //////////////////////////////////////////////////////////////////// bool MayaToEggConverter:: convert_file(const Filename &filename) { - if (!_maya->is_valid()) { + if (!open_api()) { mayaegg_cat.error() << "Maya is not available.\n"; return false; @@ -166,6 +164,16 @@ convert_file(const Filename &filename) { //////////////////////////////////////////////////////////////////// bool MayaToEggConverter:: convert_maya() { + _textures.clear(); + _shaders.clear(); + _groups.clear(); + + if (!open_api()) { + mayaegg_cat.error() + << "Maya is not available.\n"; + return false; + } + if (_egg_data->get_coordinate_system() == CS_default) { _egg_data->set_coordinate_system(_maya->get_coordinate_system()); } @@ -210,6 +218,35 @@ convert_maya() { return all_ok; } +//////////////////////////////////////////////////////////////////// +// Function: MayaToEggConverter::open_api +// Access: Public +// Description: Attempts to open the Maya API if it was not already +// open, and returns true if successful, or false if +// there is an error. +//////////////////////////////////////////////////////////////////// +bool MayaToEggConverter:: +open_api() { + if (_maya == (MayaApi *)NULL || !_maya->is_valid()) { + _maya = MayaApi::open_api(_program_name); + } + return _maya->is_valid(); +} + +//////////////////////////////////////////////////////////////////// +// Function: MayaToEggConverter::close_api +// Access: Public +// Description: Closes the Maya API, if it was previously opened. +// Caution! Maya appears to call exit() when its API is +// closed. +//////////////////////////////////////////////////////////////////// +void MayaToEggConverter:: +close_api() { + // We have to clear the shaders before we release the Maya API. + _shaders.clear(); + _maya.clear(); +} + //////////////////////////////////////////////////////////////////// // Function: MayaToEggConverter::process_node // Access: Private diff --git a/pandatool/src/mayaegg/mayaToEggConverter.h b/pandatool/src/mayaegg/mayaToEggConverter.h index 527bc81732..a4deb91a5b 100644 --- a/pandatool/src/mayaegg/mayaToEggConverter.h +++ b/pandatool/src/mayaegg/mayaToEggConverter.h @@ -63,6 +63,9 @@ public: virtual bool convert_file(const Filename &filename); bool convert_maya(); + bool open_api(); + void close_api(); + private: bool process_node(const MDagPath &dag_path, EggData &data); void get_transform(const MDagPath &dag_path, EggGroup *egg_group); @@ -90,6 +93,8 @@ private: typedef pmap Groups; Groups _groups; + string _program_name; + public: MayaShaders _shaders; EggTextureCollection _textures; diff --git a/pandatool/src/mayaprogs/mayaToEgg.cxx b/pandatool/src/mayaprogs/mayaToEgg.cxx index dab2e9bb4a..a4dd046929 100644 --- a/pandatool/src/mayaprogs/mayaToEgg.cxx +++ b/pandatool/src/mayaprogs/mayaToEgg.cxx @@ -87,7 +87,7 @@ run() { nout << "Initializing Maya.\n"; MayaToEggConverter converter(_program_name); - if (!converter._maya->is_valid()) { + if (!converter.open_api()) { nout << "Unable to initialize Maya.\n"; exit(1); }