Skip to content

Commit fe26d46

Browse files
author
David Lassonde
committed
Clean-up of acessibility level for the ModelExporter class. (Almost) everything is internal now except the ExportObject and ExportObjects method (and some leftovers to discuss)
1 parent 4c8251f commit fe26d46

File tree

6 files changed

+320
-106
lines changed

6 files changed

+320
-106
lines changed

Assets/com.unity.formats.fbx.tests/DefaultSelectionTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void TestDefaultSelection ()
107107

108108
// test with centered objects
109109
m_centerObjectsSetting.SetObjectPosition(ExportSettings.ObjectPosition.LocalCentered);
110-
var newCenter = FbxExporters.Editor.ModelExporter.FindCenter (goExportSet);
110+
var newCenter = ModelExporterReflection.FindCenter (goExportSet);
111111

112112
exportedRoot = ExportSelection (exportSet, m_centerObjectsSetting);
113113
children = new List<GameObject> ();
@@ -146,7 +146,7 @@ public void TestDefaultSelection ()
146146
/// <param name="center">New center for global transform.</param>
147147
private FbxAMatrix ConstructTRSMatrix (Transform t, bool local = true, Vector3 center = default(Vector3))
148148
{
149-
var translation = local ? t.localPosition : FbxExporters.Editor.ModelExporter.GetRecenteredTranslation (t, center);
149+
var translation = local ? t.localPosition : ModelExporterReflection.GetRecenteredTranslation (t, center);
150150
var rotation = local ? t.localEulerAngles : t.eulerAngles;
151151
var scale = local ? t.localScale : t.lossyScale;
152152
return new FbxAMatrix (

Assets/com.unity.formats.fbx.tests/ExportTimelineClipTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void ExportSingleTimelineClipTest()
3232
// One file by animation clip
3333
foreach (TimelineClip timeLineClip in at.GetClips()) {
3434
var filePath = string.Format ("{0}/{1}@{2}", folderPath, atObject.name, "Recorded.fbx");
35-
ModelExporter.ExportSingleTimelineClip (timeLineClip, atObject, filePath);
35+
ModelExporterReflection.ExportSingleTimelineClip (timeLineClip, atObject, filePath);
3636
FileAssert.Exists (filePath);
3737
}
3838
}
@@ -48,7 +48,7 @@ public void ExportAllTimelineClipTest()
4848

4949
foreach(GameObject obj in Selection.objects)
5050
{
51-
ModelExporter.ExportAllTimelineClips(obj, folderPath);
51+
ModelExporterReflection.ExportAllTimelineClips(obj, folderPath);
5252
FileAssert.Exists(string.Format("{0}/{1}@{2}", folderPath, obj.name, "Recorded.fbx"));
5353
}
5454
}

Assets/com.unity.formats.fbx.tests/FbxAnimationTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class AnimationTestDataClass
3232
public static IEnumerable<System.Type> m_componentTypes =
3333
typeof (Component).Assembly.GetTypes ().
3434
Where (t => typeof (Component).IsAssignableFrom (t) &&
35-
ModelExporter.MapsToFbxObject.ContainsKey(t)).Except(m_exceptionTypes);
35+
ModelExporterReflection.MapsToFbxObject.ContainsKey(t)).Except(m_exceptionTypes);
3636

3737
public static string [] m_rotationQuaternionNames = new string [4] { "m_LocalRotation.x", "m_LocalRotation.y", "m_LocalRotation.z", "m_LocalRotation.w" };
3838
public static string [] m_rotationEulerNames = new string [3] { "localEulerAnglesRaw.x", "localEulerAnglesRaw.y", "localEulerAnglesRaw.z" };
@@ -785,7 +785,7 @@ public int ComponentAnimTest (System.Type componentType)
785785
Debug.Log (string.Format ("ComponentAnimTest {0}", componentType.ToString()));
786786
#endif
787787

788-
if (!ModelExporter.MapsToFbxObject.ContainsKey(componentType))
788+
if (!ModelExporterReflection.MapsToFbxObject.ContainsKey(componentType))
789789
{
790790
#if DEBUG_UNITTEST
791791
Debug.Log (string.Format ("skipping {0}; fbx export not supported", componentType.ToString()));
@@ -883,7 +883,7 @@ private HashSet<string> GetAnimatedGameObjects(AnimationClip[] animClips, GameOb
883883
continue;
884884
}
885885

886-
GameObject unityGo = ModelExporter.GetGameObject (uniObj);
886+
GameObject unityGo = ModelExporterReflection.GetGameObject(uniObj);
887887
if (!unityGo) {
888888
continue;
889889
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
using FbxExporters.Editor;
2+
using FbxExporters.EditorTools;
3+
using System;
4+
using System.Linq;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
using Unity.FbxSdk;
8+
using UnityEngine;
9+
using UnityEngine.Timeline;
10+
11+
namespace FbxExporters.UnitTests
12+
{
13+
public static class ModelExporterReflection
14+
{
15+
/// <summary>
16+
// This class allows accessing the ModelExporter methods that this
17+
// unit test needs to validate
18+
/// </summary>
19+
20+
/////////////
21+
// Properties
22+
/////////////
23+
public static Material DefaultMaterial
24+
{
25+
get
26+
{
27+
return (Material)GetStaticProperty("DefaultMaterial");
28+
}
29+
}
30+
31+
/////////////
32+
// Fields
33+
/////////////
34+
public static float UnitScaleFactor
35+
{
36+
get
37+
{
38+
return (float)GetStaticField("UnitScaleFactor");
39+
}
40+
}
41+
public static Dictionary<System.Type, KeyValuePair<System.Type,ModelExporter.FbxNodeRelationType>> MapsToFbxObject
42+
{
43+
get
44+
{
45+
return (Dictionary<System.Type, KeyValuePair<System.Type,ModelExporter.FbxNodeRelationType>>) GetStaticField("MapsToFbxObject");
46+
}
47+
}
48+
49+
/////////////
50+
// Methods
51+
/////////////
52+
public static GameObject GetGameObject(UnityEngine.Object obj)
53+
{
54+
return (GameObject) InvokeMethod("GetGameObject", new object[] { obj });
55+
}
56+
57+
public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine.Object> unityExportSet)
58+
{
59+
return (HashSet<GameObject>) InvokeMethod("RemoveRedundantObjects", new object[] {unityExportSet});
60+
}
61+
62+
public static string GetVersionFromReadme()
63+
{
64+
return (string)InvokeMethod("GetVersionFromReadme", null);
65+
}
66+
67+
public static string ConvertToValidFilename(string filename)
68+
{
69+
return (string)InvokeMethod("ConvertToValidFilename", new object[] {filename});
70+
}
71+
72+
public static Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
73+
{
74+
return (Vector3)InvokeMethod("FindCenter", new object[] {gameObjects});
75+
}
76+
77+
public static Vector3 GetRecenteredTranslation(Transform t, Vector3 center)
78+
{
79+
return (Vector3)InvokeMethod("GetRecenteredTranslation", new object[] {t, center});
80+
}
81+
82+
public static FbxLayer GetOrCreateLayer(FbxMesh fbxMesh, int layer = 0 /* default layer */)
83+
{
84+
return (FbxLayer)InvokeMethod("GetOrCreateLayer", new object[] {fbxMesh,layer});
85+
}
86+
87+
public static FbxVector4 ConvertToRightHanded(Vector3 leftHandedVector, float unitScale = 1f)
88+
{
89+
return (FbxVector4)InvokeMethod("ConvertToRightHanded", new object[] {leftHandedVector,unitScale});
90+
}
91+
92+
public static bool ExportMaterial(ModelExporter instance, Material unityMaterial, FbxScene fbxScene, FbxNode fbxNode)
93+
{
94+
return (bool)InvokeMethod("ExportMaterial", new object[] {unityMaterial,fbxScene,fbxNode},instance);
95+
}
96+
97+
// Redefinition of the internal delegate. There might be a way to re-use the one in ModelExporter
98+
public static void RegisterMeshObjectCallback(ModelExporter.GetMeshForObject callback)
99+
{
100+
InvokeMethod("RegisterMeshObjectCallback", new object[] {callback});
101+
}
102+
103+
public static void RegisterMeshCallback<T>(ModelExporter.GetMeshForComponent<T> callback, bool replace = false)
104+
where T : UnityEngine.MonoBehaviour
105+
{
106+
// Get the template method first (we assume there is only one
107+
// templated RegisterMeshCallback method in ModelExporter
108+
var methods = from methodInfo in typeof(ModelExporter).GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
109+
where methodInfo.Name == "RegisterMeshCallback"
110+
&& methodInfo.IsGenericMethodDefinition
111+
select methodInfo;
112+
113+
MethodInfo templateMethod = methods.Single();
114+
MethodInfo genericMethod = templateMethod.MakeGenericMethod(new Type[]{typeof(T)});
115+
genericMethod.Invoke(null, new object[]{callback, replace});
116+
}
117+
118+
public static void UnRegisterMeshCallback<T>()
119+
{
120+
// Get the template method first (we assume there is only one
121+
// templated RegisterMeshCallback method in ModelExporter
122+
var methods = from methodInfo in typeof(ModelExporter).GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
123+
where methodInfo.Name == "UnRegisterMeshCallback"
124+
&& methodInfo.IsGenericMethodDefinition
125+
select methodInfo;
126+
127+
MethodInfo templateMethod = methods.Single();
128+
MethodInfo genericMethod = templateMethod.MakeGenericMethod(new Type[]{typeof(T)});
129+
genericMethod.Invoke(null, null);
130+
}
131+
132+
public static void UnRegisterMeshCallback(ModelExporter.GetMeshForObject callback)
133+
{
134+
InvokeMethodOverload("UnRegisterMeshCallback",
135+
new object[] {callback},
136+
new Type[] {typeof(ModelExporter.GetMeshForObject)});
137+
}
138+
139+
140+
public static bool ExportTexture(ModelExporter instance, Material unityMaterial, string unityPropName,
141+
FbxSurfaceMaterial fbxMaterial, string fbxPropName)
142+
{
143+
return (bool)InvokeMethod("ExportTexture",
144+
new object[] {unityMaterial,unityPropName,fbxMaterial,fbxPropName},
145+
instance);
146+
}
147+
148+
public static FbxDouble3 ConvertQuaternionToXYZEuler(Quaternion q)
149+
{
150+
return (FbxDouble3)InvokeMethodOverload("ConvertQuaternionToXYZEuler",
151+
new object[] {q},
152+
new Type[] {typeof(Quaternion)});
153+
}
154+
155+
public static bool ExportMesh(ModelExporter instance, Mesh mesh, FbxNode fbxNode, Material[] materials = null)
156+
{
157+
return (bool)InvokeMethodOverload("ExportMesh",
158+
new object[] {mesh, fbxNode, materials},
159+
new Type[] {typeof(Mesh), typeof(FbxNode), typeof(Material [])},
160+
instance);
161+
162+
}
163+
164+
public static void ExportSingleTimelineClip(TimelineClip timelineClipSelected, GameObject animationTrackGObject, string filePath = null)
165+
{
166+
InvokeMethod("ExportSingleTimelineClip",
167+
new object[] {timelineClipSelected, animationTrackGObject, filePath});
168+
}
169+
170+
public static void ExportAllTimelineClips(GameObject objectWithPlayableDirector, string folderPath, IExportOptions exportOptions = null)
171+
{
172+
InvokeMethod("ExportAllTimelineClips",
173+
new object[] {objectWithPlayableDirector, folderPath, exportOptions});
174+
}
175+
176+
/////////// Helpers ///////////
177+
private static object InvokeMethod(string methodName, object[] argsToPass, ModelExporter instance = null)
178+
{
179+
// Use reflection to call the internal ModelExporter.GetGameObject static method
180+
var internalMethod = typeof(ModelExporter).GetMethod(methodName,
181+
(instance == null ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.NonPublic);
182+
return internalMethod.Invoke(instance, argsToPass);
183+
}
184+
185+
// Same as InvokeMethod, but for a specific overload
186+
private static object InvokeMethodOverload(string methodName,
187+
object[] argsToPass,
188+
Type[] overloadArgTypes,
189+
ModelExporter instance = null)
190+
{
191+
var internalMethod = typeof(ModelExporter).GetMethod(methodName,
192+
(instance == null ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.NonPublic,
193+
null,
194+
overloadArgTypes,
195+
null);
196+
return internalMethod.Invoke(instance, argsToPass);
197+
}
198+
199+
private static object GetStaticProperty(string propertyName)
200+
{
201+
// Use reflection to get the internal property
202+
var internalProperty = typeof(ModelExporter).GetProperty(propertyName,
203+
BindingFlags.NonPublic | BindingFlags.Static);
204+
return internalProperty.GetValue(null,null);
205+
}
206+
207+
private static object GetStaticField(string fieldName)
208+
{
209+
// Use reflection to get the internal property
210+
var internalField = typeof(ModelExporter).GetField(fieldName,
211+
BindingFlags.NonPublic | BindingFlags.Static);
212+
return internalField.GetValue(null);
213+
}
214+
}
215+
}

0 commit comments

Comments
 (0)