Panda3D

mayapath.cxx

00001 // Filename: mayapath.cxx
00002 // Created by:  drose (07Apr08)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 // This program works as a stub to launch maya2egg, egg2maya, and
00016 // similar programs that invoke OpenMaya and require certain
00017 // environment variables to be set first.
00018 
00019 #include "dtoolbase.h"
00020 #include "filename.h"
00021 #include "dSearchPath.h"
00022 #include "executionEnvironment.h"
00023 #include <stdlib.h>
00024 
00025 #if defined(_WIN32)
00026 #define WIN32_LEAN_AND_MEAN
00027 #include <windows.h>
00028 #endif
00029 
00030 int 
00031 main(int argc, char *argv[]) {
00032   // First, get the command line and append _bin, so we will actually
00033   // run maya2egg_bin.exe, egg2maya_bin.exe, etc.
00034   Filename command = Filename::from_os_specific(argv[0]);
00035   if (!command.is_fully_qualified()) {
00036     DSearchPath path;
00037     path.append_path(ExecutionEnvironment::get_environment_variable("PATH"));
00038 #ifdef _WIN32
00039     command.set_extension("exe");
00040 #endif
00041     command.resolve_filename(path);
00042   }
00043 
00044 #ifdef _WIN32
00045   if (command.get_extension() == "exe") {
00046     command.set_extension("");
00047   }
00048 #endif
00049 
00050   command = command.get_fullpath() + string("_bin");
00051 #ifdef _WIN32
00052   command.set_extension("exe");
00053 #endif
00054   string os_command = command.to_os_specific();
00055 
00056 
00057   // Now look up $MAYA_LOCATION.  We insist that it be set and
00058   // pointing to an actual Maya installation.
00059   Filename maya_location = Filename::expand_from("$MAYA_LOCATION");
00060   cerr << "MAYA_LOCATION: " << maya_location << endl;
00061   if (maya_location.empty()) {
00062     cerr << "$MAYA_LOCATION is not set!\n";
00063     exit(1);
00064   }
00065   if (!maya_location.is_directory()) {
00066     cerr << "The directory referred to by $MAYA_LOCATION does not exist!\n";
00067     exit(1);
00068   }
00069 
00070   // Reset maya_location to its full long name, since Maya seems to
00071   // require that.
00072   maya_location.make_canonical();
00073   maya_location = Filename::from_os_specific(maya_location.to_os_long_name());
00074   
00075   // Look for OpenMaya.dll as a sanity check.
00076 #ifdef IS_OSX
00077   Filename openMaya = Filename::dso_filename(Filename(maya_location, "MacOS/libOpenMaya.dylib"));
00078   if (!openMaya.is_regular_file()) {
00079     cerr << "Could not find $MAYA_LOCATION/MacOS/" << openMaya.get_basename() << "!\n";
00080     exit(1); 
00081   }
00082 
00083 #else  // IS_OSX
00084   Filename openMaya = Filename::dso_filename(Filename(maya_location, "bin/OpenMaya.so"));
00085   if (!openMaya.is_regular_file()) {
00086     cerr << "Could not find $MAYA_LOCATION/bin/" << Filename(openMaya.get_basename()).to_os_specific() << "!\n";
00087     exit(1);
00088   }
00089 #endif
00090 
00091   // Re-set MAYA_LOCATION to its properly sanitized form.
00092   {
00093     string putenv_str = "MAYA_LOCATION=" + maya_location.to_os_specific();
00094     char *putenv_cstr = strdup(putenv_str.c_str());
00095     putenv(putenv_cstr);
00096   }
00097 
00098   // Now set PYTHONHOME & PYTHONPATH.  Maya2008 requires this to be
00099   // set and pointing within $MAYA_LOCATION, or it might get itself
00100   // confused with another Python installation (e.g. Panda's).
00101   Filename python = Filename(maya_location, "Python");
00102   if (python.is_directory()) {
00103     {
00104       string putenv_str = "PYTHONHOME=" + python.to_os_specific();
00105       char *putenv_cstr = strdup(putenv_str.c_str());
00106       putenv(putenv_cstr);
00107     }
00108     {
00109       string putenv_str = "PYTHONPATH=" + python.to_os_specific();
00110       char *putenv_cstr = strdup(putenv_str.c_str());
00111       putenv(putenv_cstr);
00112     }
00113   }
00114 
00115   // Also put the Maya bin directory on the PATH.
00116   Filename bin = Filename(maya_location, "bin");
00117   if (bin.is_directory()) {
00118     char *path = getenv("PATH");
00119     if (path == NULL) {
00120       path = "";
00121     }
00122 #ifdef WIN32
00123     string sep = ";";
00124 #else
00125     string sep = ":";
00126 #endif
00127     string putenv_str = "PATH=" + bin.to_os_specific() + sep + path;
00128     char *putenv_cstr = strdup(putenv_str.c_str());
00129     putenv(putenv_cstr);
00130   }
00131 
00132 #ifdef IS_OSX
00133   // And on DYLD_LIBRARY_PATH.
00134   if (bin.is_directory()) {
00135     char *path = getenv("DYLD_LIBRARY_PATH");
00136     if (path == NULL) {
00137       path = "";
00138     }
00139     string sep = ":";
00140     string putenv_str = "DYLD_LIBRARY_PATH=" + bin.to_os_specific() + sep + path;
00141     char *putenv_cstr = strdup(putenv_str.c_str());
00142     putenv(putenv_cstr);
00143   }
00144 
00145 #elif !defined(_WIN32)
00146   // Linux (or other non-Windows OS) gets it added to LD_LIBRARY_PATH.
00147   if (bin.is_directory()) {
00148     char *path = getenv("LD_LIBRARY_PATH");
00149     if (path == NULL) {
00150       path = "";
00151     }
00152     string sep = ":";
00153     string putenv_str = "LD_LIBRARY_PATH=" + bin.to_os_specific() + sep + path;
00154     char *putenv_cstr = strdup(putenv_str.c_str());
00155     putenv(putenv_cstr);
00156   }
00157 
00158 #endif // IS_OSX
00159 
00160   // When this is set, Panda3D will try not to use any functions from the
00161   // CPython API.  This is necessary because Maya links with its own copy
00162   // of Python, which may be incompatible with ours.
00163   putenv("PANDA_INCOMPATIBLE_PYTHON=1");
00164 
00165   // Now that we have set up the environment variables properly, chain
00166   // to the actual maya2egg_bin (or whichever) executable.
00167 
00168 #ifdef _WIN32
00169   // Windows case.
00170   char *command_line = strdup(GetCommandLine());
00171   STARTUPINFO startup_info;
00172   PROCESS_INFORMATION process_info;
00173   GetStartupInfo(&startup_info);
00174   BOOL result = CreateProcess(os_command.c_str(),
00175                               command_line, 
00176                               NULL, NULL, true, 0,
00177                               NULL, NULL,
00178                               &startup_info,
00179                               &process_info);
00180   if (result) {
00181     WaitForSingleObject(process_info.hProcess, INFINITE);
00182     DWORD exit_code = 0;
00183 
00184     if (GetExitCodeProcess(process_info.hProcess, &exit_code)) {
00185       if (exit_code != 0) {
00186         cerr << "Program exited with status " << exit_code << "\n";
00187       }
00188     }
00189 
00190     CloseHandle(process_info.hProcess);
00191     CloseHandle(process_info.hThread);
00192     exit(exit_code);
00193   }
00194   cerr << "Couldn't execute " << command << ": " << GetLastError() << "\n";
00195 
00196 #else
00197   // Unix case.
00198   execvp(os_command.c_str(), argv);
00199 #endif
00200 
00201   // Couldn't execute for some reason.
00202   return 1;
00203 }
 All Classes Functions Variables Enumerations