Skip to content

Commit 8835913

Browse files
committed
return bool from all export functions
1 parent 7781f92 commit 8835913

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ public static string GetVersionFromReadme()
184184
/// <summary>
185185
/// Export the mesh's attributes using layer 0.
186186
/// </summary>
187-
void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTriangles)
187+
private bool ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTriangles)
188188
{
189+
// return true if any attribute was exported
190+
bool exportedAttribute = false;
191+
189192
// Set the normals on Layer 0.
190193
FbxLayer fbxLayer = GetOrCreateLayer(fbxMesh);
191194

@@ -202,6 +205,7 @@ void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTr
202205
}
203206

204207
fbxLayer.SetNormals (fbxLayerElement);
208+
exportedAttribute = true;
205209
}
206210

207211
/// Set the binormals on Layer 0.
@@ -218,6 +222,7 @@ void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTr
218222
fbxElementArray.Add (ConvertNormalToRightHanded (mesh.Binormals [unityTriangle]));
219223
}
220224
fbxLayer.SetBinormals (fbxLayerElement);
225+
exportedAttribute = true;
221226
}
222227

223228
/// Set the tangents on Layer 0.
@@ -239,9 +244,10 @@ void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTr
239244
)));
240245
}
241246
fbxLayer.SetTangents (fbxLayerElement);
247+
exportedAttribute = true;
242248
}
243249

244-
ExportUVs (fbxMesh, mesh, unmergedTriangles);
250+
exportedAttribute = exportedAttribute || ExportUVs (fbxMesh, mesh, unmergedTriangles);
245251

246252
using (var fbxLayerElement = FbxLayerElementVertexColor.Create (fbxMesh, "VertexColors"))
247253
{
@@ -271,7 +277,9 @@ void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTr
271277
fbxIndexArray.SetAt (i, unmergedTriangles [i]);
272278
}
273279
fbxLayer.SetVertexColors (fbxLayerElement);
280+
exportedAttribute = true;
274281
}
282+
return exportedAttribute;
275283
}
276284

277285
/// <summary>
@@ -280,7 +288,7 @@ void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unmergedTr
280288
/// <param name="fbxMesh">Fbx mesh.</param>
281289
/// <param name="mesh">Mesh.</param>
282290
/// <param name="unmergedTriangles">Unmerged triangles.</param>
283-
static void ExportUVs(FbxMesh fbxMesh, MeshInfo mesh, int[] unmergedTriangles)
291+
private static bool ExportUVs(FbxMesh fbxMesh, MeshInfo mesh, int[] unmergedTriangles)
284292
{
285293
Vector2[][] uvs = new Vector2[][] {
286294
mesh.UV,
@@ -321,6 +329,9 @@ static void ExportUVs(FbxMesh fbxMesh, MeshInfo mesh, int[] unmergedTriangles)
321329
}
322330
k++;
323331
}
332+
333+
// if we incremented k, then at least on set of UV's were exported
334+
return k > 0;
324335
}
325336

326337
/// <summary>
@@ -366,26 +377,26 @@ public static FbxVector4 ConvertPositionToRightHanded(Vector3 leftHandedVector)
366377
/// <param name="unityPropName">Unity property name, e.g. "_MainTex".</param>
367378
/// <param name="fbxMaterial">Fbx material.</param>
368379
/// <param name="fbxPropName">Fbx property name, e.g. <c>FbxSurfaceMaterial.sDiffuse</c>.</param>
369-
public void ExportTexture (Material unityMaterial, string unityPropName,
380+
public bool ExportTexture (Material unityMaterial, string unityPropName,
370381
FbxSurfaceMaterial fbxMaterial, string fbxPropName)
371382
{
372383
if (!unityMaterial) {
373-
return;
384+
return false;
374385
}
375386

376387
// Get the texture on this property, if any.
377388
if (!unityMaterial.HasProperty (unityPropName)) {
378-
return;
389+
return false;
379390
}
380391
var unityTexture = unityMaterial.GetTexture (unityPropName);
381392
if (!unityTexture) {
382-
return;
393+
return false;
383394
}
384395

385396
// Find its filename
386397
var textureSourceFullPath = AssetDatabase.GetAssetPath (unityTexture);
387398
if (textureSourceFullPath == "") {
388-
return;
399+
return false;
389400
}
390401

391402
// get absolute filepath to texture
@@ -399,7 +410,7 @@ public void ExportTexture (Material unityMaterial, string unityPropName,
399410
var fbxMaterialProperty = fbxMaterial.FindProperty (fbxPropName);
400411
if (fbxMaterialProperty == null || !fbxMaterialProperty.IsValid ()) {
401412
Debug.Log ("property not found");
402-
return;
413+
return false;
403414
}
404415

405416
// Find or create an fbx texture and link it up to the fbx material.
@@ -411,6 +422,8 @@ public void ExportTexture (Material unityMaterial, string unityPropName,
411422
TextureMap.Add (textureSourceFullPath, fbxTexture);
412423
}
413424
TextureMap [textureSourceFullPath].ConnectDstProperty (fbxMaterialProperty);
425+
426+
return true;
414427
}
415428

416429
/// <summary>
@@ -431,15 +444,16 @@ public FbxDouble3 GetMaterialColor (Material unityMaterial, string unityPropName
431444
/// <summary>
432445
/// Export (and map) a Unity PBS material to FBX classic material
433446
/// </summary>
434-
public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxScene)
447+
public bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode fbxNode)
435448
{
436449
if (!unityMaterial) {
437450
unityMaterial = DefaultMaterial;
438451
}
439452

440453
var unityName = unityMaterial.name;
441454
if (MaterialMap.ContainsKey (unityName)) {
442-
return MaterialMap [unityName];
455+
fbxNode.AddMaterial (MaterialMap [unityName]);
456+
return true;
443457
}
444458

445459
var fbxName = ExportSettings.mayaCompatibleNames
@@ -482,7 +496,8 @@ public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxSc
482496
}
483497

484498
MaterialMap.Add (unityName, fbxMaterial);
485-
return fbxMaterial;
499+
fbxNode.AddMaterial (fbxMaterial);
500+
return true;
486501
}
487502

488503
/// <summary>
@@ -558,10 +573,10 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh, Mesh mesh, int material
558573
///
559574
/// Use fbxNode.GetMesh() to access the exported mesh.
560575
/// </summary>
561-
public void ExportMesh (Mesh mesh, FbxNode fbxNode, Material[] materials = null)
576+
public bool ExportMesh (Mesh mesh, FbxNode fbxNode, Material[] materials = null)
562577
{
563578
var meshInfo = new MeshInfo(mesh, materials);
564-
ExportMesh(meshInfo, fbxNode);
579+
return ExportMesh(meshInfo, fbxNode);
565580
}
566581

567582
/// <summary>
@@ -649,8 +664,7 @@ bool ExportMesh (MeshInfo meshInfo, FbxNode fbxNode)
649664

650665
// Set up materials per submesh.
651666
foreach (var mat in meshInfo.Materials) {
652-
var fbxMaterial = ExportMaterial (mat, fbxScene);
653-
fbxNode.AddMaterial (fbxMaterial);
667+
ExportMaterial (mat, fbxScene, fbxNode);
654668
}
655669
AssignLayerElementMaterial (fbxMesh, meshInfo.mesh, meshInfo.Materials.Length);
656670

@@ -687,7 +701,7 @@ public static FbxDouble3 ConvertQuaternionToXYZEuler(Quaternion q)
687701
}
688702

689703
// get a fbxNode's global default position.
690-
protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbxNode, Vector3 newCenter, TransformExportType exportType)
704+
protected bool ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbxNode, Vector3 newCenter, TransformExportType exportType)
691705
{
692706
// Fbx rotation order is XYZ, but Unity rotation order is ZXY.
693707
// This causes issues when converting euler to quaternion, causing the final
@@ -726,7 +740,7 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
726740
fbxNode.LclRotation.Set (fbxRotate);
727741
fbxNode.LclScaling.Set (fbxScale);
728742

729-
return;
743+
return true;
730744
}
731745

732746
/// <summary>
@@ -779,6 +793,7 @@ protected bool ExportCamera (GameObject unityGO, FbxScene fbxScene, FbxNode fbxN
779793

780794
float aspectRatio = unityCamera.aspect;
781795

796+
#region Configure Film Camera from Game Camera
782797
// Configure FilmBack settings: 35mm TV Projection (0.816 x 0.612)
783798
float apertureHeightInInches = 0.612f;
784799
float apertureWidthInInches = aspectRatio * apertureHeightInInches;
@@ -804,6 +819,7 @@ protected bool ExportCamera (GameObject unityGO, FbxScene fbxScene, FbxNode fbxN
804819

805820
// FarPlane
806821
fbxCamera.SetFarPlane (unityCamera.farClipPlane*UnitScaleFactor);
822+
#endregion
807823

808824
// Export backgroundColor as a custom property
809825
// NOTE: export on fbxNode so that it will show up in Maya
@@ -839,7 +855,7 @@ protected void SetDefaultCamera (FbxScene fbxScene)
839855
/// <summary>
840856
/// Export Component's color property
841857
/// </summary>
842-
FbxProperty ExportColorProperty (FbxObject fbxObject, Color value, string name, string label)
858+
bool ExportColorProperty (FbxObject fbxObject, Color value, string name, string label)
843859
{
844860
// create a custom property for component value
845861
var fbxProperty = FbxProperty.Create (fbxObject, Globals.FbxColor4DT, name, label);
@@ -855,13 +871,13 @@ FbxProperty ExportColorProperty (FbxObject fbxObject, Color value, string name,
855871
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eUserDefined, true);
856872
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eAnimatable, true);
857873

858-
return fbxProperty;
874+
return true;
859875
}
860876

861877
/// <summary>
862878
/// Export Component's int property
863879
/// </summary>
864-
FbxProperty ExportIntProperty (FbxObject fbxObject, int value, string name, string label)
880+
bool ExportIntProperty (FbxObject fbxObject, int value, string name, string label)
865881
{
866882
// create a custom property for component value
867883
var fbxProperty = FbxProperty.Create (fbxObject, Globals.FbxIntDT, name, label);
@@ -874,7 +890,7 @@ FbxProperty ExportIntProperty (FbxObject fbxObject, int value, string name, stri
874890
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eUserDefined, true);
875891
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eAnimatable, true);
876892

877-
return fbxProperty;
893+
return true;
878894
}
879895

880896
/// <summary>

Assets/FbxExporters/Editor/UnitTests/ModelExporterTest.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ public void TestBasics ()
5252
// Test non-static functions.
5353
using (var fbxManager = FbxManager.Create()) {
5454
var fbxScene = FbxScene.Create(fbxManager, "scene");
55+
var fbxNode = FbxNode.Create (fbxScene, "node");
5556
var exporter = new ModelExporter();
5657

5758
// Test ExportMaterial: it exports and it re-exports
58-
var fbxMaterial = exporter.ExportMaterial(ModelExporter.DefaultMaterial, fbxScene)
59-
as FbxSurfaceLambert;
59+
bool result = exporter.ExportMaterial (ModelExporter.DefaultMaterial, fbxScene, fbxNode);
60+
Assert.IsTrue (result);
61+
var fbxMaterial = fbxNode.GetMaterial (0);
6062
Assert.That(fbxMaterial, Is.Not.Null);
61-
var fbxMaterial2 = exporter.ExportMaterial(ModelExporter.DefaultMaterial, fbxScene);
63+
64+
result = exporter.ExportMaterial(ModelExporter.DefaultMaterial, fbxScene, fbxNode);
65+
var fbxMaterial2 = fbxNode.GetMaterial (1);
6266
Assert.AreEqual(fbxMaterial, fbxMaterial2);
6367

6468
// Test ExportTexture: it finds the same texture for the default-material (it doesn't create a new one)

0 commit comments

Comments
 (0)