Panda3D
|
00001 // Filename: mayaEggImport.cxx 00002 // Created by: jyelon (20Jul05) 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 is the wrapper code for the maya importer plugin. 00016 // It includes: 00017 // 00018 // - user interface dialogs and popups 00019 // - plugin initialization/registration 00020 // 00021 // It does not include the actual code to traverse the EggData. 00022 // 00023 //////////////////////////////////////////////////////////////////// 00024 00025 #include <string.h> 00026 #include <sys/types.h> 00027 00028 #include "dtoolbase.h" 00029 00030 // We must define this to prevent Maya from doubly-declaring its 00031 // MApiVersion string in this file as well as in libmayaegg. 00032 #define _MApiVersion 00033 00034 #include "pre_maya_include.h" 00035 #include <maya/MStatus.h> 00036 #include <maya/MPxCommand.h> 00037 #include <maya/MString.h> 00038 #include <maya/MStringArray.h> 00039 #include <maya/MArgList.h> 00040 #include <maya/MGlobal.h> 00041 #include <maya/MFnPlugin.h> 00042 #include <maya/MObject.h> 00043 #include <maya/MPlug.h> 00044 #include <maya/MPxFileTranslator.h> 00045 #include "post_maya_include.h" 00046 00047 #include "mayaEggLoader.h" 00048 #include "notifyCategoryProxy.h" 00049 00050 ////////////////////////////////////////////////////////////// 00051 00052 class MayaEggImporter : public MPxFileTranslator 00053 { 00054 public: 00055 MayaEggImporter () {}; 00056 virtual ~MayaEggImporter () {}; 00057 static void* creator(); 00058 00059 MStatus reader ( const MFileObject& file, 00060 const MString& optionsString, 00061 FileAccessMode mode); 00062 00063 MStatus writer ( const MFileObject& file, 00064 const MString& optionsString, 00065 FileAccessMode mode ); 00066 00067 bool haveReadMethod () const { return true; } 00068 bool haveWriteMethod () const { return false; } 00069 MString defaultExtension () const { return "egg"; } 00070 MFileKind identifyFile ( const MFileObject& fileName, 00071 const char* buffer, 00072 short size) const; 00073 }; 00074 00075 00076 void* MayaEggImporter::creator() 00077 { 00078 return new MayaEggImporter(); 00079 } 00080 00081 MStatus MayaEggImporter::reader ( const MFileObject& file, 00082 const MString& options, 00083 FileAccessMode mode) 00084 { 00085 MString fileName = file.fullName(); 00086 bool model=false; 00087 bool anim=false; 00088 00089 if (options.length() > 0) { 00090 const MString flagModel("model"); 00091 const MString flagAnim("anim"); 00092 00093 // Start parsing. 00094 // 00095 MStringArray optionList; 00096 MStringArray theOption; 00097 options.split(';', optionList); 00098 00099 unsigned nOptions = optionList.length(); 00100 for (unsigned i = 0; i < nOptions; i++) { 00101 00102 theOption.clear(); 00103 optionList[i].split('=', theOption); 00104 if (theOption.length() < 1) { 00105 continue; 00106 } 00107 00108 if (theOption[0] == flagModel && theOption.length() > 1) { 00109 model = atoi(theOption[1].asChar()) ? true:false; 00110 } else if (theOption[0] == flagAnim && theOption.length() > 1) { 00111 anim = atoi(theOption[1].asChar()) ? true:false; 00112 } 00113 } 00114 } 00115 00116 if ((mode != kImportAccessMode)&&(mode != kOpenAccessMode)) 00117 return MS::kFailure; 00118 00119 bool merge = (mode == kImportAccessMode); 00120 std::ostringstream log; 00121 Notify::ptr()->set_ostream_ptr(&log, false); 00122 bool ok = MayaLoadEggFile(fileName.asChar(), merge, model, anim, false); 00123 string txt = log.str(); 00124 if (txt != "") { 00125 MGlobal::displayError(txt.c_str()); 00126 } else { 00127 if (!ok) MGlobal::displayError("Cannot import Egg file, unknown reason"); 00128 } 00129 return ok ? MS::kSuccess : MS::kFailure; 00130 } 00131 00132 MStatus MayaEggImporter::writer ( const MFileObject& file, 00133 const MString& options, 00134 FileAccessMode mode ) 00135 00136 { 00137 fprintf(stderr, "MayaEggImporter::writer called in error\n"); 00138 return MS::kFailure; 00139 } 00140 00141 MPxFileTranslator::MFileKind MayaEggImporter::identifyFile ( 00142 const MFileObject& fileName, 00143 const char* buffer, 00144 short size) const 00145 { 00146 const char * name = fileName.name().asChar(); 00147 int nameLength = strlen(name); 00148 00149 if ((nameLength > 4) && !strcmp(name+nameLength-4, ".egg")) 00150 return kCouldBeMyFileType; 00151 else 00152 return kNotMyFileType; 00153 } 00154 00155 EXPCL_MISC MStatus initializePlugin( MObject obj ) 00156 { 00157 MFnPlugin plugin( obj, "Alias", "3.0", "Any"); 00158 00159 // Register the translator with the system 00160 return plugin.registerFileTranslator( "Panda3D Egg Import", "none", 00161 MayaEggImporter::creator, 00162 00163 "eggImportOptions", 00164 "merge=1;model=1;anim=0;"); 00165 } 00166 00167 EXPCL_MISC MStatus uninitializePlugin( MObject obj ) 00168 { 00169 MFnPlugin plugin( obj ); 00170 return plugin.deregisterFileTranslator( "Panda3D Egg Import" ); 00171 }