Skip to content

Commit 94bb32c

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master' into UNI-20986-default-filename-is-root-object
# Conflicts: # Assets/FbxExporters/Editor/ConvertToModel.cs # Assets/FbxExporters/Editor/FbxExporter.cs
2 parents 9d3e27a + ce44e09 commit 94bb32c

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 10 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>
@@ -109,7 +112,7 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
109112
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
110113

111114
if (unityMainAsset != null) {
112-
Object unityObj = PrefabUtility.InstantiateAttachedAsset (unityMainAsset);
115+
Object unityObj = PrefabUtility.InstantiatePrefab (unityMainAsset);
113116
GameObject unityGO = unityObj as GameObject;
114117
if (unityGO != null)
115118
{
@@ -137,6 +140,12 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
137140
return result;
138141
}
139142

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+
140149
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
141150
{
142151
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: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class ModelExporter : System.IDisposable
3636

3737
const string ProgressBarTitle = "Fbx Export";
3838

39+
const char MayaNamespaceSeparator = ':';
40+
3941
// replace invalid chars with this string
4042
const string InvalidCharReplacement = "_";
4143

@@ -242,13 +244,13 @@ public void ExportTexture (Material unityMaterial, string unityPropName,
242244
/// <summary>
243245
/// Get the color of a material, or grey if we can't find it.
244246
/// </summary>
245-
public FbxDouble3 GetMaterialColor (Material unityMaterial, string unityPropName)
247+
public FbxDouble3? GetMaterialColor (Material unityMaterial, string unityPropName, float defaultValue = 1)
246248
{
247249
if (!unityMaterial) {
248-
return new FbxDouble3 (0.5);
250+
return new FbxDouble3(defaultValue);
249251
}
250252
if (!unityMaterial.HasProperty (unityPropName)) {
251-
return new FbxDouble3 (0.5);
253+
return new FbxDouble3(defaultValue);
252254
}
253255
var unityColor = unityMaterial.GetColor (unityPropName);
254256
return new FbxDouble3 (unityColor.r, unityColor.g, unityColor.b);
@@ -472,6 +474,10 @@ protected int ExportComponents (
472474
{
473475
int numObjectsExported = exportProgress;
474476

477+
if (FbxExporters.EditorTools.ExportSettings.instance.mayaCompatibleNames) {
478+
unityGo.name = ConvertToMayaCompatibleName (unityGo.name);
479+
}
480+
475481
// create an FbxNode and add it as a child of parent
476482
FbxNode fbxNode = FbxNode.Create (fbxScene, unityGo.name);
477483
NumNodes++;
@@ -844,6 +850,11 @@ public Material Material {
844850
if (!renderer) {
845851
return null;
846852
}
853+
854+
if (FbxExporters.EditorTools.ExportSettings.instance.mayaCompatibleNames) {
855+
renderer.sharedMaterial.name = ConvertToMayaCompatibleName (renderer.sharedMaterial.name);
856+
}
857+
847858
// .material instantiates a new material, which is bad
848859
// most of the time.
849860
return renderer.sharedMaterial;
@@ -1024,6 +1035,48 @@ private static void EnsureDirectory (string path)
10241035
}
10251036
}
10261037

1038+
/// <summary>
1039+
/// Removes the diacritics (i.e. accents) from letters.
1040+
/// e.g. é becomes e
1041+
/// </summary>
1042+
/// <returns>Text with accents removed.</returns>
1043+
/// <param name="text">Text.</param>
1044+
private static string RemoveDiacritics(string text)
1045+
{
1046+
var normalizedString = text.Normalize(System.Text.NormalizationForm.FormD);
1047+
var stringBuilder = new System.Text.StringBuilder();
1048+
1049+
foreach (var c in normalizedString)
1050+
{
1051+
var unicodeCategory = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c);
1052+
if (unicodeCategory != System.Globalization.UnicodeCategory.NonSpacingMark)
1053+
{
1054+
stringBuilder.Append(c);
1055+
}
1056+
}
1057+
1058+
return stringBuilder.ToString().Normalize(System.Text.NormalizationForm.FormC);
1059+
}
1060+
1061+
private static string ConvertToMayaCompatibleName(string name)
1062+
{
1063+
string newName = RemoveDiacritics (name);
1064+
1065+
if (char.IsDigit (newName [0])) {
1066+
newName = newName.Insert (0, InvalidCharReplacement.ToString());
1067+
}
1068+
1069+
for (int i = 0; i < newName.Length; i++) {
1070+
if (!char.IsLetterOrDigit (newName, i)) {
1071+
if (i < newName.Length-1 && newName [i] == MayaNamespaceSeparator) {
1072+
continue;
1073+
}
1074+
newName = newName.Replace (newName [i], InvalidCharReplacement);
1075+
}
1076+
}
1077+
return newName;
1078+
}
1079+
10271080
public static string ConvertToValidFilename(string filename)
10281081
{
10291082
return System.Text.RegularExpressions.Regex.Replace (filename, "[" + new string(Path.GetInvalidFileNameChars()) + "]", InvalidCharReplacement);

0 commit comments

Comments
 (0)