Skip to content

Commit 3d4a00c

Browse files
committed
map by ID instead of name for materials and meshes
- also force unique names for materials and textures
1 parent ba90d0c commit 3d4a00c

File tree

1 file changed

+84
-23
lines changed

1 file changed

+84
-23
lines changed

com.unity.formats.fbx/Editor/FbxExporter.cs

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,38 @@ internal enum FbxNodeRelationType
165165
Dictionary<FbxNode, Dictionary<FbxConstraint, System.Type>> MapConstrainedObjectToConstraints = new Dictionary<FbxNode, Dictionary<FbxConstraint, System.Type>>();
166166

167167
/// <summary>
168-
/// Map Unity material name to FBX material object
168+
/// Map Unity material ID to FBX material object
169169
/// </summary>
170-
Dictionary<string, FbxSurfaceMaterial> MaterialMap = new Dictionary<string, FbxSurfaceMaterial> ();
170+
Dictionary<int, FbxSurfaceMaterial> MaterialMap = new Dictionary<int, FbxSurfaceMaterial> ();
171171

172172
/// <summary>
173173
/// Map texture filename name to FBX texture object
174174
/// </summary>
175175
Dictionary<string, FbxTexture> TextureMap = new Dictionary<string, FbxTexture> ();
176176

177177
/// <summary>
178-
/// Map the name of a prefab to an FbxMesh (for preserving instances)
178+
/// Map the ID of a prefab to an FbxMesh (for preserving instances)
179179
/// </summary>
180-
Dictionary<string, FbxMesh> SharedMeshes = new Dictionary<string, FbxMesh> ();
180+
Dictionary<int, FbxMesh> SharedMeshes = new Dictionary<int, FbxMesh> ();
181181

182182
/// <summary>
183183
/// Map for the Name of an Object to number of objects with this name.
184184
/// Used for enforcing unique names on export.
185185
/// </summary>
186186
Dictionary<string, int> NameToIndexMap = new Dictionary<string, int> ();
187187

188+
/// <summary>
189+
/// Map for the Material Name to number of materials with this name.
190+
/// Used for enforcing unique names on export.
191+
/// </summary>
192+
Dictionary<string, int> MaterialNameToIndexMap = new Dictionary<string, int>();
193+
194+
/// <summary>
195+
/// Map for the Texture Name to number of textures with this name.
196+
/// Used for enforcing unique names on export.
197+
/// </summary>
198+
Dictionary<string, int> TextureNameToIndexMap = new Dictionary<string, int>();
199+
188200
/// <summary>
189201
/// Format for creating unique names
190202
/// </summary>
@@ -620,7 +632,8 @@ internal bool ExportTexture (Material unityMaterial, string unityPropName,
620632

621633
// Find or create an fbx texture and link it up to the fbx material.
622634
if (!TextureMap.ContainsKey (textureSourceFullPath)) {
623-
var fbxTexture = FbxFileTexture.Create (fbxMaterial, fbxPropName + "_Texture");
635+
var textureName = GetUniqueTextureName(fbxPropName + "_Texture");
636+
var fbxTexture = FbxFileTexture.Create (fbxMaterial, textureName);
624637
fbxTexture.SetFileName (textureSourceFullPath);
625638
fbxTexture.SetTextureUse (FbxTexture.ETextureUse.eStandard);
626639
fbxTexture.SetMappingType (FbxTexture.EMappingType.eUV);
@@ -655,20 +668,23 @@ internal bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode
655668
unityMaterial = DefaultMaterial;
656669
}
657670

658-
var unityName = unityMaterial.name;
659-
if (MaterialMap.ContainsKey (unityName)) {
660-
fbxNode.AddMaterial (MaterialMap [unityName]);
671+
var unityID = unityMaterial.GetInstanceID();
672+
if (MaterialMap.ContainsKey (unityID)) {
673+
fbxNode.AddMaterial (MaterialMap [unityID]);
661674
return true;
662675
}
663676

677+
var unityName = unityMaterial.name;
664678
var fbxName = ExportOptions.UseMayaCompatibleNames
665679
? ConvertToMayaCompatibleName(unityName) : unityName;
666680

681+
fbxName = GetUniqueMaterialName(fbxName);
682+
667683
if (Verbose) {
668684
if (unityName != fbxName) {
669-
Debug.Log (string.Format ("exporting material {0} as {1}", unityName, fbxName));
685+
Debug.Log (string.Format ("exporting material {0} as {1}", unityID, fbxName));
670686
} else {
671-
Debug.Log(string.Format("exporting material {0}", unityName));
687+
Debug.Log(string.Format("exporting material {0}", unityID));
672688
}
673689
}
674690

@@ -700,7 +716,7 @@ internal bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode
700716
ExportTexture (unityMaterial, "_SpecGlosMap", fbxMaterial, FbxSurfaceMaterial.sSpecular);
701717
}
702718

703-
MaterialMap.Add (unityName, fbxMaterial);
719+
MaterialMap.Add (unityID, fbxMaterial);
704720
fbxNode.AddMaterial (fbxMaterial);
705721
return true;
706722
}
@@ -1219,7 +1235,7 @@ internal bool ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbx
12191235
/// if this game object is a model prefab then export with shared components
12201236
/// </summary>
12211237
[SecurityPermission(SecurityAction.LinkDemand)]
1222-
private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
1238+
private bool ExportInstance(GameObject unityGo, FbxScene fbxScene, FbxNode fbxNode)
12231239
{
12241240
if (!unityGo || fbxNode == null)
12251241
{
@@ -1242,10 +1258,10 @@ private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
12421258

12431259
FbxMesh fbxMesh = null;
12441260

1245-
if (!SharedMeshes.TryGetValue (unityPrefabParent.name, out fbxMesh))
1261+
if (!SharedMeshes.TryGetValue (unityPrefabParent.GetInstanceID(), out fbxMesh))
12461262
{
12471263
if (ExportMesh (unityGo, fbxNode) && fbxNode.GetMesh() != null) {
1248-
SharedMeshes [unityPrefabParent.name] = fbxNode.GetMesh ();
1264+
SharedMeshes [unityPrefabParent.GetInstanceID()] = fbxNode.GetMesh ();
12491265
return true;
12501266
}
12511267
return false;
@@ -1259,10 +1275,15 @@ private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
12591275
if (materials != null)
12601276
{
12611277
foreach (var mat in materials) {
1262-
if (MaterialMap.TryGetValue(mat.name, out newMaterial))
1278+
if (MaterialMap.TryGetValue(mat.GetInstanceID(), out newMaterial))
12631279
{
12641280
fbxNode.AddMaterial(newMaterial);
12651281
}
1282+
else
1283+
{
1284+
// create new material
1285+
ExportMaterial(mat, fbxScene, fbxNode);
1286+
}
12661287
}
12671288
}
12681289

@@ -2515,18 +2536,58 @@ private void SetDefaultCamera (FbxScene fbxScene)
25152536
/// </summary>
25162537
/// <returns>Unique name</returns>
25172538
/// <param name="name">Name</param>
2518-
private string GetUniqueName(string name)
2539+
/// <param name="nameToIndexMap">The dictionary to use to map name to # of occurences</param>
2540+
private string GetUniqueName(string name, ref Dictionary<string, int> nameToIndexMap)
25192541
{
25202542
var uniqueName = name;
2521-
if (NameToIndexMap.ContainsKey (name)) {
2522-
uniqueName = string.Format (UniqueNameFormat, name, NameToIndexMap [name]);
2523-
NameToIndexMap [name]++;
2524-
} else {
2525-
NameToIndexMap [name] = 1;
2543+
if (nameToIndexMap.ContainsKey(name))
2544+
{
2545+
uniqueName = string.Format(UniqueNameFormat, name, nameToIndexMap[name]);
2546+
nameToIndexMap[name]++;
2547+
}
2548+
else
2549+
{
2550+
nameToIndexMap[name] = 1;
25262551
}
25272552
return uniqueName;
25282553
}
25292554

2555+
/// <summary>
2556+
/// Ensures that the inputted name is unique.
2557+
/// If a duplicate name is found, then it is incremented.
2558+
/// e.g. Sphere becomes Sphere_1
2559+
/// </summary>
2560+
/// <returns>Unique name</returns>
2561+
/// <param name="name">Name</param>
2562+
private string GetUniqueFbxNodeName(string name)
2563+
{
2564+
return GetUniqueName(name, ref NameToIndexMap);
2565+
}
2566+
2567+
/// <summary>
2568+
/// Ensures that the inputted material name is unique.
2569+
/// If a duplicate name is found, then it is incremented.
2570+
/// e.g. mat becomes mat_1
2571+
/// </summary>
2572+
/// <param name="name">Name</param>
2573+
/// <returns>Unique material name</returns>
2574+
private string GetUniqueMaterialName(string name)
2575+
{
2576+
return GetUniqueName(name, ref MaterialNameToIndexMap);
2577+
}
2578+
2579+
/// <summary>
2580+
/// Ensures that the inputted texture name is unique.
2581+
/// If a duplicate name is found, then it is incremented.
2582+
/// e.g. tex becomes tex_1
2583+
/// </summary>
2584+
/// <param name="name">Name</param>
2585+
/// <returns>Unique texture name</returns>
2586+
private string GetUniqueTextureName(string name)
2587+
{
2588+
return GetUniqueName(name, ref TextureNameToIndexMap);
2589+
}
2590+
25302591
/// <summary>
25312592
/// Create a fbxNode from unityGo.
25322593
/// </summary>
@@ -2546,7 +2607,7 @@ private FbxNode CreateFbxNode(GameObject unityGo, FbxScene fbxScene)
25462607
}
25472608
}
25482609

2549-
FbxNode fbxNode = FbxNode.Create(fbxScene, GetUniqueName(fbxName));
2610+
FbxNode fbxNode = FbxNode.Create(fbxScene, GetUniqueFbxNodeName(fbxName));
25502611

25512612
// Default inheritance type in FBX is RrSs, which causes scaling issues in Maya as
25522613
// both Maya and Unity use RSrs inheritance by default.
@@ -3126,7 +3187,7 @@ private bool ExportComponents(FbxScene fbxScene)
31263187
var fbxNode = entry.Value;
31273188

31283189
// try export mesh
3129-
bool exportedMesh = ExportInstance (unityGo, fbxNode);
3190+
bool exportedMesh = ExportInstance (unityGo, fbxScene, fbxNode);
31303191

31313192
if (!exportedMesh) {
31323193
exportedMesh = ExportMesh (unityGo, fbxNode);

0 commit comments

Comments
 (0)