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