十3、File Translator怎麼寫

---恢復內容開始---ios

1. File Translator能夠將信息從maya中導入和導出。函數

2. 建立一個file translator須要從MPxFileTranslator繼承。ui

3. 函數介紹:spa

  (1)::canBeOpened()方法決定了file translator是否能夠打開文件,若是隻是一個importer那麼就return flase,反之。code

  (2)importer:必須包含::haveReadMethod(), ::reader().blog

  (3)exporter: 必須包含::haveWriteMethod(), ::writer().繼承

 

4.設置translator容許訪問全部的MEL命令: 在MFnPlugin::registerTranslator()函數中將requireFullMel參數值設置成true.ip

5. 例子, 一個polygon的exporter:get

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 = 0;
    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) = 0; };

 

6. MFnPlugin::registerFileTranslator()函數:input

有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 = 1024; 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 (0 != 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:

相關文章
相關標籤/搜索