13. How to write File translator

Source: Internet
Author: User

---restore content starts---

1. File Translator can import and export information from Maya.

2. Creating a file translator requires inheriting from Mpxfiletranslator.

3. Function Description:

(1):: Canbeopened () method determines whether the file translator can open files, if it is just a importer then return flase, and vice versa.

(2) Importer: Must contain:: Havereadmethod (),:: Reader ().

(3) Exporter: Must contain:: Havewritemethod (),:: Writer ().

4. Set translator to allow access to all Mel commands: Set the Requirefullmel parameter value to true in the Mfnplugin::registertranslator () function.

5. Example, a polygon exporter:

classPolyexporter: PublicMpxfiletranslator { Public: Polyexporter (); Virtual~Polyexporter (); VirtualMstatuswriter(Constmfileobject&file,Constmstring&optionsstring, Mpxfiletranslator::fileaccessmode mode); Virtual BOOL         Havewritemethod () const; Virtual BOOLHavereadmethod ()Const; Virtual BOOL           canbeopened () const; VirtualMstring defaultextension ()Const=0; protected:            Virtual BOOLIsVisible (mfndagnode& Fndag, mstatus&status);  Virtual mstatus exportall (ostream& OS);        Virtual mstatus exportselection (ostream& OS); Virtual voidWriteheader (ostream&OS); Virtual voidWritefooter (ostream&OS);  Virtual mstatus Processpolymesh (const Mdagpath dagpath, ostream& OS);  Virtual polywriter* createpolywriter (const Mdagpath dagpath, mstatus& status) = 0 /c1>;};

6. Mfnplugin::registerfiletranslator () function:

There are 6 parameters, the last three are optional,

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's can use Mglobal::executecommand () method in translator.

7. Reader:

If you want to read the file using the reader () method, the Havereadmethod () method returns True

The reader () method reads each line of the file and returns Ms::kfailture if the read fails

Mstatus Leptranslator::reader (Constmfileobject&file,Constmstring&options, Mpxfiletranslator::fileaccessmode mode) {    ConstMstring fname =File.fullname ();

mstatus Rval (ms::ksuccess); Const intMaxlinesize =1024x768; CharBuf[maxlinesize];
Ifstream Inputfile (Fname.aschar (), iOS::inch); if(!inputfile) { //Open FailedCerr << fname <<": Could not being opened for reading\n"; returnms::kfailure; } if(!inputfile.getline (buf, maxlinesize)) {Cerr<<"file"<< fname <<"contained no lines ... aborting\n"; returnms::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"; returnms::kfailure; } while(Inputfile.getline (buf, maxlinesize)) {//processing Each line of the filemstring cmdstring; Cmdstring.Set(BUF); if(!Mglobal::executecommand (cmdstring)) Rval=ms::kfailure; } inputfile.close (); returnRval;}  

8. Writer () method, similar to Reader () method:

Using the Script Editor to provide a message, in this example only the export all and export selection options are provided, the other options will output failure message.

Mstatus Polyexporter::writer (Constmfileobject&file,Constmstring&/*Options*/, mpxfiletranslator::fileaccessmode mode) {        ConstMstring FileName =file.fullname ();ofstream NewFile (Filename.aschar (), iOS:: out); if(!newFile) {mglobal::d isplayerror (FileName+": Could not being opened for reading"); returnms::kfailure; }
NEWFILE.SETF (IOS::UNITBUF); Writeheader (NewFile);
if(Mpxfiletranslator::kexportaccessmode = = mode) { if(Mstatus::kfailure = =Exportall (NewFile)) { returnmstatus::kfailure; } } Else if(Mpxfiletranslator::kexportactiveaccessmode = =mode) { if(Mstatus::kfailure = =exportselection (NewFile)) { returnmstatus::kfailure; } } Else { returnmstatus::kfailure; } writefooter (NewFile); Newfile.flush (); Newfile.close (); Mglobal::d isplayinfo ("Export to"+ FileName +"successful!"); returnms::ksuccess;}

9. File extention:

Const {    return mstring ("raw");}

Ten. File access mode:

13. How to write File translator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.