Panda3D
mayaEggImport.cxx
1 // Filename: mayaEggImport.cxx
2 // Created by: jyelon (20Jul05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 //
15 // This is the wrapper code for the maya importer plugin.
16 // It includes:
17 //
18 // - user interface dialogs and popups
19 // - plugin initialization/registration
20 //
21 // It does not include the actual code to traverse the EggData.
22 //
23 ////////////////////////////////////////////////////////////////////
24 
25 #include <string.h>
26 #include <sys/types.h>
27 
28 #include "dtoolbase.h"
29 
30 // We must define this to prevent Maya from doubly-declaring its
31 // MApiVersion string in this file as well as in libmayaegg.
32 #define _MApiVersion
33 
34 #include "pre_maya_include.h"
35 #include <maya/MStatus.h>
36 #include <maya/MPxCommand.h>
37 #include <maya/MString.h>
38 #include <maya/MStringArray.h>
39 #include <maya/MArgList.h>
40 #include <maya/MGlobal.h>
41 #include <maya/MFnPlugin.h>
42 #include <maya/MObject.h>
43 #include <maya/MPlug.h>
44 #include <maya/MPxFileTranslator.h>
45 #include "post_maya_include.h"
46 
47 #include "mayaEggLoader.h"
48 #include "notifyCategoryProxy.h"
49 
50 //////////////////////////////////////////////////////////////
51 
52 class MayaEggImporter : public MPxFileTranslator
53 {
54 public:
55  MayaEggImporter () {};
56  virtual ~MayaEggImporter () {};
57  static void* creator();
58 
59  MStatus reader ( const MFileObject& file,
60  const MString& optionsString,
61  FileAccessMode mode);
62 
63  MStatus writer ( const MFileObject& file,
64  const MString& optionsString,
65  FileAccessMode mode );
66 
67  bool haveReadMethod () const { return true; }
68  bool haveWriteMethod () const { return false; }
69  MString defaultExtension () const { return "egg"; }
70  MFileKind identifyFile ( const MFileObject& fileName,
71  const char* buffer,
72  short size) const;
73 };
74 
75 
76 void* MayaEggImporter::creator()
77 {
78  return new MayaEggImporter();
79 }
80 
81 MStatus MayaEggImporter::reader ( const MFileObject& file,
82  const MString& options,
83  FileAccessMode mode)
84 {
85  MString fileName = file.fullName();
86  bool model=false;
87  bool anim=false;
88 
89  if (options.length() > 0) {
90  const MString flagModel("model");
91  const MString flagAnim("anim");
92 
93  // Start parsing.
94  //
95  MStringArray optionList;
96  MStringArray theOption;
97  options.split(';', optionList);
98 
99  unsigned nOptions = optionList.length();
100  for (unsigned i = 0; i < nOptions; i++) {
101 
102  theOption.clear();
103  optionList[i].split('=', theOption);
104  if (theOption.length() < 1) {
105  continue;
106  }
107 
108  if (theOption[0] == flagModel && theOption.length() > 1) {
109  model = atoi(theOption[1].asChar()) ? true:false;
110  } else if (theOption[0] == flagAnim && theOption.length() > 1) {
111  anim = atoi(theOption[1].asChar()) ? true:false;
112  }
113  }
114  }
115 
116  if ((mode != kImportAccessMode)&&(mode != kOpenAccessMode))
117  return MS::kFailure;
118 
119  bool merge = (mode == kImportAccessMode);
120  std::ostringstream log;
121  Notify::ptr()->set_ostream_ptr(&log, false);
122  bool ok = MayaLoadEggFile(fileName.asChar(), merge, model, anim, false);
123  string txt = log.str();
124  if (txt != "") {
125  MGlobal::displayError(txt.c_str());
126  } else {
127  if (!ok) MGlobal::displayError("Cannot import Egg file, unknown reason");
128  }
129  return ok ? MS::kSuccess : MS::kFailure;
130 }
131 
132 MStatus MayaEggImporter::writer ( const MFileObject& file,
133  const MString& options,
134  FileAccessMode mode )
135 
136 {
137  fprintf(stderr, "MayaEggImporter::writer called in error\n");
138  return MS::kFailure;
139 }
140 
141 MPxFileTranslator::MFileKind MayaEggImporter::identifyFile (
142  const MFileObject& fileName,
143  const char* buffer,
144  short size) const
145 {
146  const char * name = fileName.name().asChar();
147  int nameLength = strlen(name);
148 
149  if ((nameLength > 4) && !strcmp(name+nameLength-4, ".egg"))
150  return kCouldBeMyFileType;
151  else
152  return kNotMyFileType;
153 }
154 
155 EXPCL_MISC MStatus initializePlugin( MObject obj )
156 {
157  MFnPlugin plugin( obj, "Alias", "3.0", "Any");
158 
159  // Register the translator with the system
160  return plugin.registerFileTranslator( "Panda3D Egg Import", "none",
161  MayaEggImporter::creator,
162 
163  "eggImportOptions",
164  "merge=1;model=1;anim=0;");
165 }
166 
167 EXPCL_MISC MStatus uninitializePlugin( MObject obj )
168 {
169  MFnPlugin plugin( obj );
170  return plugin.deregisterFileTranslator( "Panda3D Egg Import" );
171 }
static Notify * ptr()
Returns the pointer to the global Notify object.
Definition: notify.cxx:337
void set_ostream_ptr(ostream *ostream_ptr, bool delete_later)
Changes the ostream that all subsequent Notify messages will be written to.
Definition: notify.cxx:73