Created
December 8, 2020 23:27
-
-
Save OlafHaag/271341d8c3272e31a1945b2b0ec0a1da to your computer and use it in GitHub Desktop.
Maya MEL script to prepare Interior Furniture Pack from CGTrader for export to Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prepares the models from the Interior Furniture pack (CGTrader) for export to Unity. | |
// Combine all meshes if applicable and move to origin on top of grid/floor. | |
// Remap the texture paths to where they are on the hard drive. | |
// Embed the textures into the exported FBX. | |
// | |
// Usage: | |
// MAYA LT | |
// It does seem Maya LT doesn't come with the batch mode, so you have to start maya, source the script and run | |
// the below command manually in the MEL prompt. | |
// | |
// For a single file export: | |
// batchExportFile("<path_to_file>", "<path_to_export_folder>", "C:/Work/Turbosquid/Gardizi/", "<path_to_texture_folder>"); | |
// | |
// For a exporting a folder with files: | |
// batchExportFile("<path_to_folder>", "<path_to_export_folder>", "C:/Work/Turbosquid/Gardizi/", "<path_to_texture_folder>", <int skip first n files>); | |
// | |
// Examples: | |
// batchExportFile("C:/project/src_files/cgtrader/Interior Furniture/furniture_fbx/0931.fbx", | |
// "C:/temp_projects/InteriorFurniture/Assets/InteriorFurniture/Models/", | |
// "C:/Work/Turbosquid/Gardizi/", | |
// "C:/project/src_files/cgtrader/Interior Furniture/Textures/"); | |
// | |
// batchExportFiles("C:/project/src_files/cgtrader/Interior Furniture/furniture_fbx/", | |
// "C:/project/temp_projects/InteriorFurniture/Assets/InteriorFurniture/Models/", | |
// "C:/Work/Turbosquid/Gardizi/", | |
// "C:/project/src_files/cgtrader/Interior Furniture/Textures/", | |
// 372 | |
// ); | |
// | |
// Full MAYA Version | |
// You can use the script in batch mode without opening the maya visual interface. | |
// from the Windows commandline type | |
// For a single file export: | |
// mayabatch -script <path_to_script> -command "batchExportFile(\"<path_to_file>\", \"<path_to_export_folder>\", \"C:/Work/Turbosquid/Gardizi/\", \"<path_to_texture_folder>\");" | |
// | |
// For a exporting a folder with files: | |
// mayabatch -script <path_to_script> -command "batchExportFile(\"<path_to_folder>\", \"<path_to_export_folder>\", \"C:/Work/Turbosquid/Gardizi/\", \"<path_to_texture_folder>\", <int skip first>);" | |
// | |
// MAKE SURE YOU USE FORWARD SLASHES IN PATHS AND ESCAPE QUOTE SIGNS! | |
// MIT License | |
// | |
// Copyright (c) 2019 Olaf Haag | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
proc dirMap(string $oldPath, string $newPath) | |
{ | |
dirmap -en true; | |
dirmap -m $oldPath $newPath; | |
} | |
proc remapTextures(string $oldPath, string $newPath) | |
{ | |
string $SceneTextures[] = `ls -tex`; | |
for ($tex in $SceneTextures) | |
{ | |
$Test = catchQuiet(getAttr ($tex + ".fileTextureName")); | |
if ($Test == 0) | |
{ | |
string $TexturePath = getAttr ($tex + ".fileTextureName"); | |
string $NewPath = substituteAllString($TexturePath, $oldPath, $newPath); | |
setAttr -type "string" ($tex + ".fileTextureName") $NewPath; | |
catchQuiet (AEfileTextureReloadCmd ($tex + ".fileTextureName")); | |
//print $TexturePath; | |
}//end if | |
}//end for i | |
} | |
proc deleteLocators() | |
{ | |
string $locators[] = `ls -ni -typ "locator"`; | |
if (size($locators) > 0) | |
{ | |
string $locatorRelatives[] = `listRelatives -p $locators`; | |
delete $locatorRelatives; | |
} | |
} | |
proc string combine(string $newName) | |
{ | |
// Select all geometry in scene. | |
string $geometry[] = `ls -ni -typ "mesh"`; | |
// Is there even geometry? | |
if (size($geometry) == 0) | |
return ""; | |
// polyUnite only works if there are more than 1 child nodes. | |
else if (size($geometry) == 1) | |
{ | |
string $transform[] = `listRelatives -parent -path $geometry[0]`; | |
// Unparent and just give a new name. | |
if (size($transform) > 0) | |
{ | |
string $parent[] = `listRelatives -parent -path $transform[0]`; | |
if (size($parent) != 0) | |
parent -world $transform[0]; | |
} | |
rename $transform[0] $newName; | |
return $newName; | |
} | |
polyUnite -ch 0 -mergeUVSets 1 -objectPivot -name $newName $geometry; | |
// Get rid of locator left-overs. | |
deleteLocators(); | |
return $newName; | |
} | |
proc moveToFloor(string $obj) | |
{ | |
// Center pivot. | |
xform -cp $obj; | |
move -rpr 0 0 0 $obj; | |
//vector $pivotPos = `xform -q -rp $obj`; | |
// Move. | |
float $vertBoundingBox[6] = `polyEvaluate -b $obj`; | |
float $height = ( $vertBoundingBox[3] - $vertBoundingBox[2] ); | |
setAttr ($obj + ".translateY") ($height*0.5); | |
// Clear animation before freeze transform. | |
cutKey -clear $obj; | |
// Freeze Rotation. | |
makeIdentity -apply true -rotate true -scale true $obj; | |
} | |
global proc batchExportFile(string $importFilePath, string $exportPath, string $oldTexPath, string $newTexPath) | |
{ | |
file -f -new; | |
FBXImport -f $importFilePath; | |
string $basename = basename( $importFilePath, ".fbx" ); | |
// Change texture directory. | |
dirMap($oldTexPath, $newTexPath); | |
remapTextures($oldTexPath, $newTexPath); | |
// Combine meshes to one. | |
string $newName = "furniture_" + $basename; | |
string $combinedName = combine($newName); | |
if (size($combinedName) == 0) | |
{ | |
print ("Error: failed to combine file " + $importFilePath + ". No geometry."); | |
return; | |
} | |
moveToFloor($combinedName); | |
// Export to FBX. | |
string $subFolder = $basename + "/"; | |
string $exportSubPath = $exportPath + $subFolder; | |
sysFile -makeDir $exportSubPath; | |
string $fname = (string)$combinedName + ".fbx"; | |
string $exFilePath = $exportSubPath + $fname; | |
select -r $combinedName; | |
print ("Export: " + $exFilePath); | |
FBXExportEmbeddedTextures -v true; | |
FBXExport -f $exFilePath -s; | |
} | |
global proc batchExportFiles(string $importPath, string $exportPath, string $oldTexPath, string $newTexPath, int $skipN) | |
{ | |
int $i = 0; | |
// Loop through files. | |
string $files[] = `getFileList -folder $importPath -filespec "*.fbx"`; | |
print ("Processing " + (string)size($files) + " files..."); | |
for ($file in $files) | |
{ | |
$i = $i +1; | |
if ($i <= $skipN) | |
continue; | |
string $inFilePath = $importPath + $file; | |
batchExportFile($inFilePath, $exportPath, $oldTexPath, $newTexPath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment