AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
FileTranslatorBase.h
Go to the documentation of this file.
1 #pragma once
2 //----------------------------------------------------------------------------------------------------------------------
73 //----------------------------------------------------------------------------------------------------------------------
74 #include "AL/maya/FileTranslatorOptions.h"
75 
76 #include "maya/MPxFileTranslator.h"
77 #include "maya/MGlobal.h"
78 #include "maya/MFileObject.h"
79 
80 namespace AL {
81 namespace maya {
82 
84 #define AL_MAYA_TRANSLATOR_BEGIN(ClassName, TranslatorName, HaveRead, HaveWrite, DefaultExtension, FilterString) \
85  class ClassName : public AL::maya::FileTranslatorBase<ClassName> { \
86  public: \
87  static constexpr const char* const kTranslatorName = TranslatorName; \
88  static constexpr const char* const kClassName = #ClassName; \
89  static void* creator() { return new ClassName; } \
90  private: \
91  bool haveReadMethod() const { return HaveRead; } \
92  bool haveWriteMethod() const { return HaveWrite; } \
93  MString defaultExtension() const { return DefaultExtension; } \
94  MString filter() const { return FilterString; } \
95  public:
96 
98 #define AL_MAYA_TRANSLATOR_END() };
99 
100 //----------------------------------------------------------------------------------------------------------------------
102 //----------------------------------------------------------------------------------------------------------------------
103 template<typename T>
104 class FileTranslatorBase
105  : public MPxFileTranslator
106 {
107 public:
108 
111  template<typename FnPlugin>
112  static MStatus registerTranslator(FnPlugin& plugin)
113  {
114  FileTranslatorOptions options(T::kClassName);
115  if(MS::kSuccess == T::specifyOptions(options))
116  {
117  options.generateScript(m_optionParser, m_defaultOptionString);
118 
119  MStatus status = plugin.registerFileTranslator(
120  T::kTranslatorName,
121  "",
122  T::creator,
123  T::kClassName,
124  m_defaultOptionString.asChar());
125 
126  if(!status) {
127  MGlobal::displayError(MString("Failed to register translator: ") + T::kTranslatorName);
128  }
129  return status;
130  }
131  MGlobal::displayError(MString("Failed to generate options for translator: ") + T::kTranslatorName);
132  return MS::kFailure;
133  }
134 
137  template<typename FnPlugin>
138  static MStatus deregisterTranslator(FnPlugin& plugin)
139  { return plugin.deregisterFileTranslator(T::kTranslatorName); }
140 
144  static MStatus specifyOptions(FileTranslatorOptions& options)
145  { return MS::kSuccess; }
146 
152  virtual MStatus reader(const MFileObject& file, const OptionsParser& options, FileAccessMode mode)
153  { return MS::kFailure; }
154 
160  virtual MStatus writer(const MFileObject& file, const OptionsParser& options, FileAccessMode mode)
161  { return MS::kFailure; }
162 
163 private:
164 
165  MStatus reader(const MFileObject &file, const MString &optionsString, const FileAccessMode mode) override
166  {
167  MStatus status = m_optionParser.parse(optionsString);
168  if(MS::kSuccess == status)
169  {
170  return reader(file, m_optionParser, mode);
171  }
172  MGlobal::displayError("Unable to parse the file translator options");
173  return status;
174  }
175 
176  MStatus writer(const MFileObject &file, const MString &optionsString, const FileAccessMode mode) override
177  {
178  MStatus status = m_optionParser.parse(optionsString);
179  if(MS::kSuccess == status)
180  {
181  return writer(file, m_optionParser, mode);
182  }
183  MGlobal::displayError("Unable to parse the file translator options");
184  return status;
185  }
186 
187  static MString m_defaultOptionString;
188  static OptionsParser m_optionParser;
189 };
190 
191 template<typename T> MString FileTranslatorBase<T>::m_defaultOptionString;
192 template<typename T> OptionsParser FileTranslatorBase<T>::m_optionParser;
193 
194 //----------------------------------------------------------------------------------------------------------------------
195 } // usdmaya
196 } // AL
197 //----------------------------------------------------------------------------------------------------------------------
static MStatus specifyOptions(FileTranslatorOptions &options)
default fall back in case no options are needed in the derived translator
Definition: FileTranslatorBase.h:144
virtual MStatus reader(const MFileObject &file, const OptionsParser &options, FileAccessMode mode)
Override this method to read your files (do not use the version from MPxFileTranslator!) ...
Definition: FileTranslatorBase.h:152
static MStatus registerTranslator(FnPlugin &plugin)
unregister the file translator
Definition: FileTranslatorBase.h:112
Utility class that constructs the file translator export GUI from the export options you want to supp...
Definition: FileTranslatorOptions.h:148
Utility class that parses the file translator options passed through by Maya.
Definition: FileTranslatorOptions.h:17
virtual MStatus writer(const MFileObject &file, const OptionsParser &options, FileAccessMode mode)
Override this method to write your files (do not use the version from MPxFileTranslator!) ...
Definition: FileTranslatorBase.h:160
static MStatus deregisterTranslator(FnPlugin &plugin)
unregister the file translator
Definition: FileTranslatorBase.h:138
MStatus generateScript(OptionsParser &optionParser, MString &defaultOptionString)
This method generates the MEL script for the import/export GUI, and evaluates it behind the scenes...
MStatus parse(const MString &optionString)
Given a string containing a semi-colon separated list of options passed to a file translator plugin...