---恢复内容开始---
1. File Translator可以将信息从maya中导入和导出。
2. 创建一个file translator需要从MPxFileTranslator继承。
3. 函数介绍:
(1)::canBeOpened()方法决定了file translator是否可以打开文件,如果只是一个importer那么就return flase,反之。
(2)importer:必须包含::haveReadMethod(), ::reader().
(3)exporter: 必须包含::haveWriteMethod(), ::writer().
4.设置translator允许访问所有的MEL命令: 在MFnPlugin::registerTranslator()函数中将requireFullMel参数值设置成true.
5. 例子, 一个polygon的exporter:
class polyExporter:public MPxFileTranslator
{
public:
polyExporter();
virtual ~polyExporter();
virtual MStatus writer (const MFileObject& file,
const MString& optionsString, MPxFileTranslator::FileAccessMode mode);
virtual bool haveWriteMethod () const;
virtual bool haveReadMethod () const;
virtual bool canBeOpened () const;
virtual MString defaultExtension () const = ;
protected:
virtual bool isVisible(MFnDagNode& fnDag, MStatus& status);
virtual MStatus exportAll(ostream& os);
virtual MStatus exportSelection(ostream& os);
virtual void writeHeader(ostream& os);
virtual void writeFooter(ostream& os);
virtual MStatus processPolyMesh(const MDagPath dagPath, ostream& os);
virtual polyWriter* createPolyWriter(const MDagPath dagPath, MStatus& status) = ;
};
6. MFnPlugin::registerFileTranslator()函数:
有6个参数,最后三个是可选的,
status = plugin.registerFileTranslator("RawText", "", polyRawExporter::creator, "", "option1=1", true);
RawText: is a name;
option1=1: default value for the option box for the translator;
true: means that you can use MGlobal::executeCommand() method in translator.
7. Reader:
如果要用reader()方法读取文件,那么haveReadMethod()方法要返回true
reader()方法读取文件的每一行,如果读取失败返回MS::kFailture
MStatus LepTranslator::reader ( const MFileObject& file,
const MString& options, MPxFileTranslator::FileAccessMode mode)
{ const MString fname = file.fullName(); MStatus rval(MS::kSuccess);
const int maxLineSize = ;
char buf[maxLineSize];
ifstream inputfile(fname.asChar(), ios::in);
if (!inputfile) {
// open failed
cerr << fname << ": could not be opened for reading\n";
return MS::kFailure;
}
if (!inputfile.getline (buf, maxLineSize)) {
cerr << "file " << fname << " contained no lines ... aborting\n";
return MS::kFailure;
}
//the first line has the magic chars.
if ( != strncmp(buf, magic.asChar(), magic.length())) {
cerr << "first line of file " << fname;
cerr << " did not contain " << magic.asChar() << " ... aborting\n";
return MS::kFailure;
}
while (inputfile.getline (buf, maxLineSize)) {
//processing each line of the file
MString cmdString;
cmdString.set(buf);
if (!MGlobal::executeCommand(cmdString))
rval = MS::kFailure;
}
inputfile.close();
return rval;
}
8. writer()方法, 类似reader()方法:
通过script editor来提供message,在这个例子中只提供export all 和export selection选项,其他的选项将输出failure message.
MStatus polyExporter::writer(const MFileObject& file,
const MString& /*options*/, MPxFileTranslator::FileAccessMode mode)
{ const MString fileName = file.fullName();
ofstream newFile(fileName.asChar(), ios::out);
if (!newFile) {
MGlobal::displayError(fileName + ": could not be opened for reading");
return MS::kFailure;
}
newFile.setf(ios::unitbuf);
writeHeader(newFile);
if (MPxFileTranslator::kExportAccessMode == mode) {
if (MStatus::kFailure == exportAll(newFile)) {
return MStatus::kFailure;
}
}
else if (MPxFileTranslator::kExportActiveAccessMode == mode) {
if (MStatus::kFailure == exportSelection(newFile)) {
return MStatus::kFailure;
}
}
else {
return MStatus::kFailure;
}
writeFooter(newFile);
newFile.flush();
newFile.close();
MGlobal::displayInfo("Export to " + fileName + " successful!");
return MS::kSuccess;
}
9. file extention:
MString polyRawExporter::defaultExtension () const
{
return MString("raw");
}
10. file access mode: