Skip to content

Commit 9eef3da

Browse files
committed
code review fixes
* small formatting fixes * move verbose up * move SkinnedMeshToBonesMap declaration into a function and pass it
1 parent 328bbfb commit 9eef3da

File tree

1 file changed

+84
-58
lines changed

1 file changed

+84
-58
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public static class AnimationCurveExtension
3737
// This is an extension method for the AnimationCurve class
3838
// The first parameter takes the "this" modifier
3939
// and specifies the type for which the method is defined.
40-
public static void Dump (this AnimationCurve animCurve, string message, float [] keyTimesExpected = null, float [] keyValuesExpected = null)
40+
public static void Dump (this AnimationCurve animCurve, string message, float[] keyTimesExpected = null, float[] keyValuesExpected = null)
4141
{
4242
int idx = 0;
4343
foreach (var key in animCurve.keys) {
4444
if (keyTimesExpected != null && keyValuesExpected != null) {
4545
Debug.Log (string.Format ("{5} keys[{0}] {1}({3}) {2} ({4})",
46-
idx, key.time, key.value,
47-
keyTimesExpected [idx], keyValuesExpected [idx],
48-
message));
46+
idx, key.time, key.value,
47+
keyTimesExpected [idx], keyValuesExpected [idx],
48+
message));
4949
} else {
5050
Debug.Log (string.Format ("{3} keys[{0}] {1} {2}", idx, key.time, key.value, message));
5151
}
@@ -128,7 +128,6 @@ static ModelExporter Create ()
128128
/// <summary>
129129
/// Which components map from Unity Object to Fbx Object
130130
/// </summary>
131-
///
132131
public enum FbxNodeRelationType
133132
{
134133
NodeAttribute,
@@ -165,7 +164,7 @@ public enum FbxNodeRelationType
165164
/// <summary>
166165
/// Map the name of a prefab to an FbxMesh (for preserving instances)
167166
/// </summary>
168-
Dictionary<string, FbxMesh> SharedMeshes = new Dictionary<string, FbxMesh>();
167+
Dictionary<string, FbxMesh> SharedMeshes = new Dictionary<string, FbxMesh> ();
169168

170169
/// <summary>
171170
/// Map for the Name of an Object to number of objects with this name.
@@ -191,13 +190,14 @@ public static EditorTools.ExportSettings ExportSettings {
191190
public static Material DefaultMaterial {
192191
get {
193192
if (!s_defaultMaterial) {
194-
var obj = GameObject.CreatePrimitive(PrimitiveType.Quad);
195-
s_defaultMaterial = obj.GetComponent<Renderer>().sharedMaterial;
196-
Object.DestroyImmediate(obj);
193+
var obj = GameObject.CreatePrimitive (PrimitiveType.Quad);
194+
s_defaultMaterial = obj.GetComponent<Renderer> ().sharedMaterial;
195+
Object.DestroyImmediate (obj);
197196
}
198197
return s_defaultMaterial;
199198
}
200199
}
200+
201201
static Material s_defaultMaterial = null;
202202

203203
static Dictionary<UnityEngine.LightType, FbxLight.EType> MapLightType = new Dictionary<UnityEngine.LightType, FbxLight.EType> () {
@@ -254,10 +254,10 @@ public static string GetVersionFromReadme()
254254
int newLayerIndex = fbxMesh.CreateLayer();
255255
if (newLayerIndex <= maxLayerIndex) {
256256
// Error!
257-
throw new System.Exception(
258-
"Internal error: Unable to create mesh layer "
259-
+ (maxLayerIndex + 1)
260-
+ " on mesh " + fbxMesh.GetName());
257+
throw new System.Exception (
258+
"Internal error: Unable to create mesh layer "
259+
+ (maxLayerIndex + 1)
260+
+ " on mesh " + fbxMesh.GetName ());
261261
}
262262
maxLayerIndex = newLayerIndex;
263263
}
@@ -773,9 +773,14 @@ SkinnedMeshRenderer unitySkin
773773
return false;
774774
}
775775

776+
if (Verbose)
777+
Debug.Log (string.Format ("exporting {0} {1}", "Skin", fbxNode.GetName ()));
778+
779+
780+
Dictionary<SkinnedMeshRenderer, Transform[]> skinnedMeshToBonesMap;
776781
// export skeleton
777-
if (!ExportSkeleton (unitySkin, fbxScene)) {
778-
Debug.LogWarning("failed to export skeleton");
782+
if (!ExportSkeleton (unitySkin, fbxScene, out skinnedMeshToBonesMap)) {
783+
Debug.LogWarning ("failed to export skeleton");
779784
return false;
780785
}
781786

@@ -796,44 +801,58 @@ SkinnedMeshRenderer unitySkin
796801
ExportSkin (unitySkin, meshInfo, fbxScene, fbxMesh, fbxNode);
797802

798803
// add bind pose
799-
ExportBindPose (unitySkin, fbxNode, fbxScene);
804+
ExportBindPose (unitySkin, fbxNode, fbxScene, skinnedMeshToBonesMap);
800805

801-
if (Verbose)
802-
Debug.Log (string.Format ("exporting {0} {1}", "Skin", fbxNode.GetName ()));
803-
804806
return true;
805807
}
806808

807-
private bool IsBone(Transform t, Dictionary<Transform, int> bones){
809+
/// <summary>
810+
/// Determines whether this Transform is a bone.
811+
/// A transform is a bone if it is in the skinned meshes bone list (represented here as a bones dict),
812+
/// or if it has both an ancestor or descendant that are bones (i.e. if it is sandwiched between two bones,
813+
/// it should be a bone as well).
814+
/// </summary>
815+
/// <returns><c>true</c> if this transform is a bone; otherwise, <c>false</c>.</returns>
816+
/// <param name="t">Transform.</param>
817+
/// <param name="bones">Skinned meshes bones.</param>
818+
private bool IsBone (Transform t, Dictionary<Transform, int> bones)
819+
{
808820
if (bones.ContainsKey (t)) {
809821
return true;
810822
}
811823

812824
foreach (Transform child in t) {
813825
if (IsBone (child, bones)) {
814-
//Debug.LogError ("our child is a bone: " + t.name);
815826
return true;
816827
}
817828
}
818829
return false;
819830
}
820831

821-
private Dictionary<SkinnedMeshRenderer, Transform[]> SkinnedMeshToBonesMap = new Dictionary<SkinnedMeshRenderer, Transform[]> ();
822-
823832
/// <summary>
824833
/// Export bones of skinned mesh, if this is a skinned mesh with
825834
/// bones and bind poses.
826835
/// </summary>
827-
private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
836+
private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene, out Dictionary<SkinnedMeshRenderer, Transform[]> skinnedMeshToBonesMap)
828837
{
829-
if (!skinnedMesh) { return false; }
838+
skinnedMeshToBonesMap = new Dictionary<SkinnedMeshRenderer, Transform[]> ();
839+
840+
if (!skinnedMesh) {
841+
return false;
842+
}
830843
var bones = skinnedMesh.bones;
831-
if (bones == null || bones.Length == 0) { return false; }
844+
if (bones == null || bones.Length == 0) {
845+
return false;
846+
}
832847
var mesh = skinnedMesh.sharedMesh;
833-
if (!mesh) { return false; }
848+
if (!mesh) {
849+
return false;
850+
}
834851

835852
var bindPoses = mesh.bindposes;
836-
if (bindPoses == null || bindPoses.Length != bones.Length) { return false; }
853+
if (bindPoses == null || bindPoses.Length != bones.Length) {
854+
return false;
855+
}
837856

838857
// Three steps:
839858
// 0. Set up the map from bone to index.
@@ -888,7 +907,7 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
888907

889908
var boneList = boneSet.ToArray();
890909

891-
SkinnedMeshToBonesMap.Add (skinnedMesh, boneList);
910+
skinnedMeshToBonesMap.Add (skinnedMesh, boneList);
892911

893912
// Step 2: Get bindposes
894913
var boneToBindPose = new Dictionary<Transform, Matrix4x4>();
@@ -956,8 +975,8 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
956975
/// Export binding of mesh to skeleton
957976
/// </summary>
958977
private bool ExportSkin (SkinnedMeshRenderer skinnedMesh,
959-
MeshInfo meshInfo, FbxScene fbxScene, FbxMesh fbxMesh,
960-
FbxNode fbxRootNode)
978+
MeshInfo meshInfo, FbxScene fbxScene, FbxMesh fbxMesh,
979+
FbxNode fbxRootNode)
961980
{
962981
FbxSkin fbxSkin = FbxSkin.Create (fbxScene, (skinnedMesh.name + "_Skin"));
963982

@@ -1032,7 +1051,8 @@ boneWeights [i].weight3
10321051
/// <summary>
10331052
/// Export bind pose of mesh to skeleton
10341053
/// </summary>
1035-
protected bool ExportBindPose (SkinnedMeshRenderer skinnedMesh, FbxNode fbxMeshNode, FbxScene fbxScene)
1054+
protected bool ExportBindPose (SkinnedMeshRenderer skinnedMesh, FbxNode fbxMeshNode,
1055+
FbxScene fbxScene, Dictionary<SkinnedMeshRenderer, Transform[]> skinnedMeshToBonesMap)
10361056
{
10371057
FbxPose fbxPose = FbxPose.Create (fbxScene, fbxMeshNode.GetName());
10381058

@@ -1041,7 +1061,7 @@ protected bool ExportBindPose (SkinnedMeshRenderer skinnedMesh, FbxNode fbxMeshN
10411061

10421062
// assume each bone node has one weighted vertex cluster
10431063
Transform[] bones;
1044-
if (!SkinnedMeshToBonesMap.TryGetValue (skinnedMesh, out bones)) {
1064+
if (!skinnedMeshToBonesMap.TryGetValue (skinnedMesh, out bones)) {
10451065
return false;
10461066
}
10471067
for (int i = 0; i < bones.Length; i++) {
@@ -1093,13 +1113,13 @@ public static FbxDouble3 ConvertQuaternionToXYZEuler(Quaternion q)
10931113

10941114
public static FbxVector4 ConvertQuaternionToXYZEuler (FbxQuaternion quat)
10951115
{
1096-
FbxAMatrix m = new FbxAMatrix ();
1097-
m.SetQ (quat);
1098-
var vector4 = m.GetR ();
1116+
FbxAMatrix m = new FbxAMatrix ();
1117+
m.SetQ (quat);
1118+
var vector4 = m.GetR ();
10991119

1100-
// Negate the y and z values of the rotation to convert
1101-
// from Unity to Maya coordinates (left to righthanded).
1102-
return new FbxVector4 (vector4.X, -vector4.Y, -vector4.Z, vector4.W);
1120+
// Negate the y and z values of the rotation to convert
1121+
// from Unity to Maya coordinates (left to righthanded).
1122+
return new FbxVector4 (vector4.X, -vector4.Y, -vector4.Z, vector4.W);
11031123
}
11041124

11051125
// get a fbxNode's global default position.
@@ -1450,8 +1470,8 @@ public static bool TryGetValue(string uniPropertyName, out FbxPropertyChannelPai
14501470
return true;
14511471
}
14521472
if (uniPropertyName.StartsWith ("m_LocalPosition.z", ct) || uniPropertyName.EndsWith ("T.z", ct)) {
1453-
prop = new FbxPropertyChannelPair ("Lcl Translation", Globals.FBXSDK_CURVENODE_COMPONENT_Z);
1454-
return true;
1473+
prop = new FbxPropertyChannelPair ("Lcl Translation", Globals.FBXSDK_CURVENODE_COMPONENT_Z);
1474+
return true;
14551475
}
14561476

14571477
if (uniPropertyName.StartsWith("m_Intensity", ct))
@@ -1681,7 +1701,7 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
16811701
if (Verbose)
16821702
{
16831703
Debug.Log (string.Format ("Exporting animation curve bound to {0} {1}",
1684-
uniCurveBinding.propertyName, uniCurveBinding.path));
1704+
uniCurveBinding.propertyName, uniCurveBinding.path));
16851705
}
16861706

16871707
int index = QuaternionCurve.GetQuaternionIndex (uniCurveBinding.propertyName);
@@ -1830,9 +1850,9 @@ protected int ExportNodes(
18301850

18311851
numObjectsExported++;
18321852
if (EditorUtility.DisplayCancelableProgressBar (
1833-
ProgressBarTitle,
1834-
string.Format ("Creating FbxNode {0}/{1}", numObjectsExported, objectCount),
1835-
(numObjectsExported / (float)objectCount) * 0.25f)) {
1853+
ProgressBarTitle,
1854+
string.Format ("Creating FbxNode {0}/{1}", numObjectsExported, objectCount),
1855+
(numObjectsExported / (float)objectCount) * 0.25f)) {
18361856
// cancel silently
18371857
return -1;
18381858
}
@@ -1866,9 +1886,9 @@ protected bool ExportComponents(FbxScene fbxScene)
18661886
foreach (KeyValuePair<GameObject, FbxNode> entry in MapUnityObjectToFbxNode) {
18671887
numObjectsExported++;
18681888
if (EditorUtility.DisplayCancelableProgressBar (
1869-
ProgressBarTitle,
1870-
string.Format ("Exporting Components for GameObject {0}/{1}", numObjectsExported, objectCount),
1871-
((numObjectsExported / (float)objectCount) * 0.25f) + 0.25f)) {
1889+
ProgressBarTitle,
1890+
string.Format ("Exporting Components for GameObject {0}/{1}", numObjectsExported, objectCount),
1891+
((numObjectsExported / (float)objectCount) * 0.25f) + 0.25f)) {
18721892
// cancel silently
18731893
return false;
18741894
}
@@ -1895,7 +1915,7 @@ protected bool ExportComponents(FbxScene fbxScene)
18951915
}
18961916

18971917
// now (try) export animation
1898-
ExportAnimation (unityGo, fbxScene);
1918+
ExportAnimation (unityGo, fbxScene);
18991919

19001920
}
19011921
return true;
@@ -2341,7 +2361,8 @@ public Vector3 [] Normals { get {
23412361
m_normals = mesh.normals;
23422362
}
23432363
return m_normals;
2344-
} }
2364+
}
2365+
}
23452366

23462367
/// <summary>
23472368
/// Gets the binormals for the vertices.
@@ -2382,7 +2403,8 @@ public Vector4 [] Tangents { get {
23822403
m_tangents = mesh.tangents;
23832404
}
23842405
return m_tangents;
2385-
} }
2406+
}
2407+
}
23862408

23872409
/// <summary>
23882410
/// Gets the vertex colors for the vertices.
@@ -2394,7 +2416,8 @@ public Color32 [] VertexColors { get {
23942416
m_vertexColors = mesh.colors32;
23952417
}
23962418
return m_vertexColors;
2397-
} }
2419+
}
2420+
}
23982421

23992422
/// <summary>
24002423
/// Gets the uvs.
@@ -2406,7 +2429,8 @@ public Vector2 [] UV { get {
24062429
m_UVs = mesh.uv;
24072430
}
24082431
return m_UVs;
2409-
} }
2432+
}
2433+
}
24102434

24112435
/// <summary>
24122436
/// The material(s) used.
@@ -2421,7 +2445,8 @@ public BoneWeight[] BoneWeights { get {
24212445
m_boneWeights = mesh.boneWeights;
24222446
}
24232447
return m_boneWeights;
2424-
} }
2448+
}
2449+
}
24252450

24262451
/// <summary>
24272452
/// Set up the MeshInfo with the given mesh and materials.
@@ -2523,10 +2548,10 @@ public static void RegisterMeshCallback<T>(GetMeshForComponent<T> callback, bool
25232548
where T: UnityEngine.MonoBehaviour
25242549
{
25252550
// Under the hood we lose type safety, but don't let the user notice!
2526-
RegisterMeshCallback(typeof(T),
2527-
(ModelExporter exporter, MonoBehaviour component, FbxNode fbxNode) =>
2528-
callback(exporter, (T)component, fbxNode),
2529-
replace);
2551+
RegisterMeshCallback (typeof(T),
2552+
(ModelExporter exporter, MonoBehaviour component, FbxNode fbxNode) =>
2553+
callback (exporter, (T)component, fbxNode),
2554+
replace);
25302555
}
25312556

25322557
/// <summary>
@@ -2728,6 +2753,7 @@ private static void OnExport ()
27282753
: System.IO.Path.GetDirectoryName (LastFilePath);
27292754

27302755
GameObject [] selectedGOs = Selection.GetFiltered<GameObject> (SelectionMode.TopLevel);
2756+
GameObject[] selectedGOs = Selection.GetFiltered<GameObject> (SelectionMode.TopLevel);
27312757
string filename = null;
27322758
if (selectedGOs.Length == 1) {
27332759
filename = ConvertToValidFilename (selectedGOs [0].name + ".fbx");

0 commit comments

Comments
 (0)