Skip to content

Commit 220710c

Browse files
authored
Merge pull request #26 from Unity-Technologies/UNI-20152-reliable-naming
Uni 20152 reliable naming
2 parents 329cfd3 + d9be185 commit 220710c

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class ConvertToModel : System.IDisposable
2121
const string MenuItemName1 = "Assets/Convert To Model";
2222
const string MenuItemName2 = "GameObject/Convert To Model";
2323

24+
const string RegexCharStart = "[";
25+
const string RegexCharEnd = "]";
26+
2427
/// <summary>
2528
/// Clean up this class on garbage collection
2629
/// </summary>
@@ -76,7 +79,8 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
7679
string dirPath = Path.Combine (Application.dataPath, "Objects");
7780

7881
for(int n = 0; n < gosToExport.Length; n++){
79-
filePaths[n] = Path.Combine (dirPath, gosToExport[n].name + ".fbx");
82+
string filename = ConvertToValidFilename (gosToExport [n].name + ".fbx");
83+
filePaths[n] = Path.Combine (dirPath, filename);
8084
}
8185

8286
string[] fbxFileNames = new string[filePaths.Length];
@@ -136,6 +140,12 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
136140
return result;
137141
}
138142

143+
private static string ConvertToValidFilename(string filename)
144+
{
145+
return System.Text.RegularExpressions.Regex.Replace (filename,
146+
RegexCharStart + new string(Path.GetInvalidFileNameChars()) + RegexCharEnd, "_");
147+
}
148+
139149
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
140150
{
141151
Transform importedTransform = imported.transform;

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ public class ExportSettingsEditor : UnityEditor.Editor {
1111
public override void OnInspectorGUI() {
1212
ExportSettings exportSettings = (ExportSettings)target;
1313

14+
// Increasing the label width so that none of the text gets cut off
15+
EditorGUIUtility.labelWidth = 300;
16+
1417
exportSettings.weldVertices = EditorGUILayout.Toggle ("Weld Vertices:", exportSettings.weldVertices);
1518
exportSettings.embedTextures = EditorGUILayout.Toggle ("Embed Textures:", exportSettings.embedTextures);
19+
exportSettings.mayaCompatibleNames = EditorGUILayout.Toggle (
20+
new GUIContent("Convert to Maya Compatible Naming:",
21+
"In Maya some symbols such as spaces and accents get replaced when importing an FBX " +
22+
"(e.g. \"foo bar\" becomes \"fooFBXASC032bar\"). " +
23+
"On export, convert the names of GameObjects so they are Maya compatible." +
24+
(exportSettings.mayaCompatibleNames? "" :
25+
"\n\nWARNING: Disabling this feature may result in lost material connections," +
26+
" and unexpected character replacements in Maya.")
27+
),
28+
exportSettings.mayaCompatibleNames);
1629

1730
if (GUI.changed) {
1831
EditorUtility.SetDirty (exportSettings);
@@ -26,6 +39,7 @@ public class ExportSettings : FbxExporters.EditorTools.ScriptableSingleton<Expor
2639
{
2740
public bool weldVertices = true;
2841
public bool embedTextures = false;
42+
public bool mayaCompatibleNames = true;
2943

3044
[MenuItem("Edit/Project Settings/Fbx Export", priority = 300)]
3145
static void ShowManager()

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class ModelExporter : System.IDisposable
3636

3737
const string ProgressBarTitle = "Fbx Export";
3838

39+
const char InvalidCharReplacement = '_';
40+
41+
const char MayaNamespaceSeparator = ':';
42+
3943
/// <summary>
4044
/// Create instance of example
4145
/// </summary>
@@ -469,6 +473,10 @@ protected int ExportComponents (
469473
{
470474
int numObjectsExported = exportProgress;
471475

476+
if (FbxExporters.EditorTools.ExportSettings.instance.mayaCompatibleNames) {
477+
unityGo.name = ConvertToMayaCompatibleName (unityGo.name);
478+
}
479+
472480
// create an FbxNode and add it as a child of parent
473481
FbxNode fbxNode = FbxNode.Create (fbxScene, unityGo.name);
474482
NumNodes++;
@@ -838,6 +846,11 @@ public Material Material {
838846
if (!renderer) {
839847
return null;
840848
}
849+
850+
if (FbxExporters.EditorTools.ExportSettings.instance.mayaCompatibleNames) {
851+
renderer.sharedMaterial.name = ConvertToMayaCompatibleName (renderer.sharedMaterial.name);
852+
}
853+
841854
// .material instantiates a new material, which is bad
842855
// most of the time.
843856
return renderer.sharedMaterial;
@@ -1011,6 +1024,48 @@ private static void EnsureDirectory (string path)
10111024
Directory.CreateDirectory (fileInfo.Directory.FullName);
10121025
}
10131026
}
1027+
1028+
/// <summary>
1029+
/// Removes the diacritics (i.e. accents) from letters.
1030+
/// e.g. é becomes e
1031+
/// </summary>
1032+
/// <returns>Text with accents removed.</returns>
1033+
/// <param name="text">Text.</param>
1034+
private static string RemoveDiacritics(string text)
1035+
{
1036+
var normalizedString = text.Normalize(System.Text.NormalizationForm.FormD);
1037+
var stringBuilder = new System.Text.StringBuilder();
1038+
1039+
foreach (var c in normalizedString)
1040+
{
1041+
var unicodeCategory = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c);
1042+
if (unicodeCategory != System.Globalization.UnicodeCategory.NonSpacingMark)
1043+
{
1044+
stringBuilder.Append(c);
1045+
}
1046+
}
1047+
1048+
return stringBuilder.ToString().Normalize(System.Text.NormalizationForm.FormC);
1049+
}
1050+
1051+
private static string ConvertToMayaCompatibleName(string name)
1052+
{
1053+
string newName = RemoveDiacritics (name);
1054+
1055+
if (char.IsDigit (newName [0])) {
1056+
newName = newName.Insert (0, InvalidCharReplacement.ToString());
1057+
}
1058+
1059+
for (int i = 0; i < newName.Length; i++) {
1060+
if (!char.IsLetterOrDigit (newName, i)) {
1061+
if (i < newName.Length-1 && newName [i] == MayaNamespaceSeparator) {
1062+
continue;
1063+
}
1064+
newName = newName.Replace (newName [i], InvalidCharReplacement);
1065+
}
1066+
}
1067+
return newName;
1068+
}
10141069
}
10151070
}
10161071
}

0 commit comments

Comments
 (0)