PDA

View Full Version : FBX to maya mb Batch Export script...help!


ninjoid
12-22-2010, 08:35 PM
Greetings all,
I am a mel script newbie
I have hundreds of files in FBX that need conversion to maya binary (mb). They are all in subfolders within a folder.
After looking through many codes, I came up with this:

string $path = ("C:/test2" + "/") ;
string $filelist[] = `getFileList -folder $path -filespec "*.fbx"` ;
getFileList -folder $path -filespec "*.fbx" ;
for ($i = 0; $i < `size ($filelist)` ; $i ++)
{
string $output = ("/data/" + $filelist[$i] ) ;
//select $i ;
file -force -options "v=0" -type "mayaBinary" -pr -ea ;
}

Thing is nothing happens after I run the script. No feed back, nothing!!!
I have been at this for hours.:banghead: Am I going at this all wrong? Someone help.:cry:

sincerely, Charlie
PS: if this is a re post, sorry, previous post went missin!!

Mondo
12-22-2010, 11:55 PM
First problem is your syntax. The code below cleans that up, but your logic is wrong and I would probably write a plugin to do this as it would be a cleaner solution. The code below is dirty but it should do what you need.


string $path = "C:/test2/";
string $filelist[] = `getFileList -folder $path -filespec "*.fbx"`;
string $newFileList[];
int $cnt;
string $parts[];

for ($i=0;$i<size($filelist);$i++){

$cnt = `tokenize $filelist[$i] "." $parts`; //strip out "." from fbx suffix
$newFileList[$i] = $parts[0];

FBXImport -f ($path + $filelist[$i]) -t; //open the fbx file
file -rename ($path + ($newFileList[$i]+".mb")); //rename to .mb
file -f -save -options "v=0" -type "mayaBinary"; //save the .mb
file -f -new; //create a new scene
}


Basically, to convert a file you need to open/import it in first. FBX will launch a UI and cause an error an error in your loop. To get around it you need to use the FBXImport command. This command will fail if you dont have the FBX plugin loaded (dirty solution). I would add a condition to check for this before you enter the loop.

Stepping through, you need to rename the scene to .mb and then save the scene. End the loop with creating a new scene so the next imported .fbx will be in a fresh scene.

I removed your $output string to simplify things, meaning your new .mb will be saved into the original destination. You'll have to manually move them out.

Also if the .fbx you're converting is heavy, it might break the code - But this should get you started.

Good times!

ninjoid
12-23-2010, 12:25 AM
Thanks Mondo for your help. The script runs, but nothing happens.No result no conversion.Silent. Was there a step I missed or some extra setting?

Sincerely,
Charlie

Mondo
12-23-2010, 04:38 PM
I dont know what to say, I just tested and it works fine for me. What's the average size of your .fbx files? What version of Maya are you using? Do you have any security restrictions on your folder system?

Every time the script converts a scene it will create a log file.
C:\Users\[username]\Documents\maya\FBX\Logs\2011.1\maya2011imp.log

Anyways, I've added a confirmation dialog window to the script. If the script is running correctly you'll be prompted to hit OK after every conversion.



string $path = "C:/test2/";
string $filelist[] = `getFileList -folder $path -filespec "*.fbx"`;
string $newFileList[];
int $cnt;
string $parts[];

for ($i=0;$i<size($filelist);$i++){

$cnt = `tokenize $filelist[$i] "." $parts`; //strip out "." from fbx suffix
$newFileList[$i] = $parts[0];

FBXImport -f ($path + $filelist[$i]) -t; //open the fbx file
file -rename ($path + ($newFileList[$i]+".mb")); //rename to .mb
file -f -save -options "v=0" -type "mayaBinary"; //save the .mb

string $confirmUI = `confirmDialog -title "Convert FBX"
-message ($path + $filelist[$i] + "has been converted to " + $path + $newFileList[$i]+".mb")
-button "OK"`;

file -f -new; //create a new scene
}

ninjoid
12-23-2010, 06:48 PM
Thanks Mondo for your help. Much appreciated.
The script does work, but not recursively. It only converts files in the main folder not sub-folders.

Basically I am using the lastest version of LayoutTools for 2011, it is suppose to support FBX but is not working. It'll only load ma or mb files. I am using it as asset manager. It renders small previews of your props and allows you to easily populate your scene.

Moving all the models into one top folder is asking for trouble, because some textures share names. Hence why i am trying to executed a recursing script.

Your script works wonderfully only on the top layer.

I found this other script online, it is recursive, BUT it adds some middle extension before the file extension. Like say, source.FBX to destination.fmb.mb

here it is:

//
//--------------------------------------------------------------------------------- SETTINGS

// change this to the name of you plugin you want to use.
global string $g_plugin = "C:/Program Files/Alias/Maya6.0/bin/plug-ins/objExport.mll";

// change this to the input directory
global string $g_InDirectory = "C:/GameData/";

// change this to the correct output extension, obj in this case
global string $g_OutExt = "obj";

//---------------------------------------------------------------------------------

//--------------------------------------------------------------------------------- DIRECTORY RECURSION
// we simply now want to recurse through the directory structure
// exporting any Maya Binary files as obj files.
// to do this we can use a number of handy mel functions,
//
proc RecursiveDirExport(string $dir)
{
// redeclare the global so we can access it
global string $g_OutExt;

// get a directory listing
string $dir_list[] = `getFileList -folder $dir -fs "*.mb"`;

// loop through each maya binary file in the directory
for($i=0;$i<size($dir_list);++$i)
{
// print ($dir+$dir_list[$i]);
// first open the file
file -open -force ($dir+$dir_list[$i]);

// strip the mb from the filename and append a new extension
$filename = ($dir + `substring $dir_list[$i] 1 (size($dir_list[$i])-2)` + $g_OutExt);

// export the file as an objfile
file -exportAll -force -type "OBJexport" $filename;
}

// now get a second file list and check each one.
$dir_list = `getFileList -folder $dir`;

// loop through each file returned.
for($i=0;$i<size($dir_list);++$i)
{
// if we have a directory, recurse into it and export files
if( `filetest -d ($dir+$dir_list[$i])` )
{
RecursiveDirExport( ($dir + $dir_list[$i] + "/") );
}
}
}


//--------------------------------------------------------------------------------- MAIN

// load the plugin
loadPlugin $g_plugin;

// output all of the files in the directory
RecursiveDirExport( $g_InDirectory );
//


Thanks again Mondo, if you have tips on making your script recursive or eliminating the naming issue of the other, It'll be a valueable learning experience for me. Thank you.

Sincerely,
Charles

Mondo
12-23-2010, 11:25 PM
I wasn't aware that you had sub folders. You're right, a recursive function is the way to go in that case. Instead of adding onto my code it was faster to modify the code you posted (see below). I only added maybe 3 lines and commented out some stuff you dont need. It seems to be functioning on my end. If this something you plan on doing a lot of in the future, I recommend you build a UI where you can choose the root folder prior to execution.



//
//--------------------------------------------------------------------------------- SETTINGS

// change this to the name of you plugin you want to use.
//global string $g_plugin = "C:/Program Files/Alias/Maya6.0/bin/plug-ins/objExport.mll";

// change this to the input directory
global string $g_InDirectory = "C:/test2/";

// change this to the correct output extension, obj in this case
global string $g_OutExt = "mb";

//---------------------------------------------------------------------------------

//--------------------------------------------------------------------------------- DIRECTORY RECURSION
// we simply now want to recurse through the directory structure
// exporting any Maya Binary files as obj files.
// to do this we can use a number of handy mel functions,
//
proc RecursiveDirExport(string $dir)
{
// redeclare the global so we can access it
global string $g_OutExt;

// get a directory listing
string $dir_list[] = `getFileList -folder $dir -fs "*.fbx"`;

// loop through each maya binary file in the directory
for($i=0;$i<size($dir_list);++$i)
{
// print ($dir+$dir_list[$i]);
// first open the file
FBXImport -f ($dir+$dir_list[$i]);

// strip the mb from the filename and append a new extension
$filename = ($dir + `substring $dir_list[$i] 1 (size($dir_list[$i])-2)` + $g_OutExt);
file -rename $filename;
// export the file as an objfile
file -f -save -options "v=0" -type "mayaBinary";
}

// now get a second file list and check each one.
$dir_list = `getFileList -folder $dir`;

// loop through each file returned.
for($i=0;$i<size($dir_list);++$i)
{
// if we have a directory, recurse into it and export files
if( `filetest -d ($dir+$dir_list[$i])` )
{
RecursiveDirExport( ($dir + $dir_list[$i] + "/") );
}
}
}


//--------------------------------------------------------------------------------- MAIN

// load the plugin
//loadPlugin $g_plugin;

// output all of the files in the directory
RecursiveDirExport( $g_InDirectory );
//

Mondo
12-23-2010, 11:28 PM
Crap I forgot to strip out the middle extension. I'll have to come back to this in a couple of days.

ninjoid
12-24-2010, 01:10 AM
Thanks Mondo, I patiently await your return and Happy Holidays.
Sincerely,
Charlie

CGTalk Moderation
12-24-2010, 01:10 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.