Skip to content

Commit c26fd32

Browse files
committed
store export settings in FbxExporter
- pass export settings to fbx exporter using ExportObjects
1 parent 6e9cbce commit c26fd32

10 files changed

+107
-78
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public static GameObject Convert (
104104
string fbxDirectoryFullPath = null,
105105
string fbxFullPath = null,
106106
string prefabDirectoryFullPath = null,
107-
string prefabFullPath = null
107+
string prefabFullPath = null,
108+
EditorTools.IExportOptions exportOptions = null
108109
)
109110
{
110111
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
@@ -154,7 +155,11 @@ public static GameObject Convert (
154155

155156
// Export to FBX. It refreshes the database.
156157
{
157-
var fbxActualPath = ModelExporter.ExportObject (fbxFullPath, toConvert, lodExportType: EditorTools.ExportModelSettingsSerialize.LODExportType.All);
158+
var fbxActualPath = ModelExporter.ExportObject (
159+
fbxFullPath, toConvert,
160+
exportOptions != null ? exportOptions :
161+
(ScriptableObject.CreateInstance <EditorTools.ConvertToPrefabSettings> () as EditorTools.ConvertToPrefabSettings)
162+
);
158163
if (fbxActualPath != fbxFullPath) {
159164
throw new System.Exception ("Failed to convert " + toConvert.name);
160165
}

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ protected override void OnEnable ()
4141
{
4242
base.OnEnable ();
4343
if (!m_innerEditor) {
44-
var ms = ExportSettings.instance.convertToPrefabSettings;
45-
if (!ms) {
46-
ms = ExportSettings.instance.convertToPrefabSettings;
47-
}
48-
m_innerEditor = UnityEditor.Editor.CreateEditor (ms);
44+
m_innerEditor = UnityEditor.Editor.CreateEditor (ExportSettings.instance.convertToPrefabSettings);
4945
}
5046
}
5147

@@ -68,12 +64,16 @@ protected override void Export ()
6864
}
6965

7066
if (m_toConvert.Length == 1) {
71-
ConvertToModel.Convert (m_toConvert[0], fbxFullPath: fbxPath, prefabFullPath: prefabPath);
67+
ConvertToModel.Convert (
68+
m_toConvert[0], fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings
69+
);
7270
return;
7371
}
7472

7573
foreach (var go in m_toConvert) {
76-
ConvertToModel.Convert (go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath);
74+
ConvertToModel.Convert (
75+
go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.convertToPrefabSettings
76+
);
7777
}
7878
}
7979

Assets/FbxExporters/Editor/ConvertToPrefabSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public ExportModelSettingsSerialize.LODExportType GetLODExportType(){
9393
return ExportModelSettingsSerialize.LODExportType.All;
9494
}
9595
public ExportModelSettingsSerialize.ObjectPosition GetObjectPosition(){
96-
return ExportModelSettingsSerialize.ObjectPosition.LocalCentered;
96+
return ExportModelSettingsSerialize.ObjectPosition.Reset;
9797
}
9898
public void SetObjectPosition(ExportModelSettingsSerialize.ObjectPosition objPos){
9999
// nothing to set

Assets/FbxExporters/Editor/ExportModelEditorWindow.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,24 +272,39 @@ protected bool OverwriteExistingFile(string filePath){
272272

273273
public class ExportModelEditorWindow : ExportOptionsEditorWindow
274274
{
275-
public static void Init (string filename = "", bool singleHierarchyExport = true, ModelExporter.AnimationExportType exportType = ModelExporter.AnimationExportType.all)
275+
private UnityEngine.Object[] m_toExport;
276+
277+
public static void Init (IEnumerable<UnityEngine.Object> toExport, string filename = "", ModelExporter.AnimationExportType exportType = ModelExporter.AnimationExportType.all)
276278
{
277279
ExportModelEditorWindow window = CreateWindow<ExportModelEditorWindow> ();
278-
window.InitializeWindow (filename, singleHierarchyExport, exportType);
280+
int numObjects = window.SetGameObjectsToExport (toExport);
281+
if (string.IsNullOrEmpty (filename)) {
282+
filename = window.GetFilenameFromObjects ();
283+
}
284+
window.InitializeWindow (filename, singleHierarchyExport: numObjects == 1, exportType: exportType);
279285
window.Show ();
280286
}
281287

288+
protected int SetGameObjectsToExport(IEnumerable<UnityEngine.Object> toExport){
289+
m_toExport = System.Linq.Enumerable.ToArray (toExport);
290+
return m_toExport.Length;
291+
}
292+
293+
protected string GetFilenameFromObjects(){
294+
var filename = "";
295+
if (m_toExport.Length == 1) {
296+
filename = m_toExport [0].name;
297+
} else {
298+
filename = "Untitled";
299+
}
300+
return filename;
301+
}
302+
282303
protected override void OnEnable ()
283304
{
284305
base.OnEnable ();
285-
286306
if (!m_innerEditor) {
287-
var ms = ExportSettings.instance.exportModelSettings;
288-
if (!ms) {
289-
ms = ExportSettings.instance.exportModelSettings;
290-
}
291-
m_innerEditor = UnityEditor.Editor.CreateEditor (ms);
292-
this.SetSingleHierarchyExport (m_singleHierarchyExport);
307+
m_innerEditor = UnityEditor.Editor.CreateEditor (ExportSettings.instance.exportModelSettings);
293308
}
294309
}
295310

@@ -302,7 +317,7 @@ protected override void Export(){
302317
return;
303318
}
304319

305-
if (ModelExporter.ExportObjects (filePath, exportType: m_animExportType, lodExportType: ExportSettings.GetLODExportType()) != null) {
320+
if (ModelExporter.ExportObjects (filePath, m_toExport, ExportSettings.instance.exportModelSettings, exportType: m_animExportType) != null) {
306321
// refresh the asset database so that the file appears in the
307322
// asset folder view.
308323
AssetDatabase.Refresh ();

Assets/FbxExporters/Editor/ExportModelSettings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public ExportModelSettingsSerialize.Include GetModelAnimIncludeOption(){
110110
public ExportModelSettingsSerialize.LODExportType GetLODExportType(){
111111
return info.lodLevel;
112112
}
113+
public void SetLODExportType(ExportModelSettingsSerialize.LODExportType lodType){
114+
info.lodLevel = lodType;
115+
}
113116
public ExportModelSettingsSerialize.ObjectPosition GetObjectPosition(){
114117
return info.objectPosition;
115118
}
@@ -131,7 +134,7 @@ public enum ExportFormat { ASCII = 0, Binary = 1}
131134

132135
public enum Include { Model = 0, Anim = 1, ModelAndAnim = 2 }
133136

134-
public enum ObjectPosition { LocalCentered = 0, WorldAbsolute = 1 }
137+
public enum ObjectPosition { LocalCentered = 0, WorldAbsolute = 1 , Reset = 2 }
135138

136139
public enum LODExportType { All = 0, Highest = 1, Lowest = 2 }
137140

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public static string[] DCCVendorLocations
419419
private ConvertToPrefabSettingsSerialize convertToPrefabSettingsSerialize;
420420

421421
// ---------------- get functions for options in ExportModelSettings ----------------
422-
public static ExportModelSettingsSerialize.ExportFormat GetExportFormat(){
422+
/*public static ExportModelSettingsSerialize.ExportFormat GetExportFormat(){
423423
return instance.exportModelSettings.info.exportFormat;
424424
}
425425
@@ -449,7 +449,7 @@ public static bool AnimateSkinnedMesh(){
449449
450450
public static bool UseMayaCompatibleNames(){
451451
return instance.exportModelSettings.info.mayaCompatibleNaming;
452-
}
452+
}*/
453453
// ---------------------------------------------------------------------------------
454454

455455
protected override void LoadDefaults()

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ public static EditorTools.ExportSettings ExportSettings {
195195
get { return EditorTools.ExportSettings.instance; }
196196
}
197197

198+
199+
private EditorTools.IExportOptions m_exportOptions;
200+
private EditorTools.IExportOptions ExportOptions {
201+
get {
202+
if (m_exportOptions == null) {
203+
// get default settings;
204+
m_exportOptions = ScriptableObject.CreateInstance <ExportModelSettings>() as ExportModelSettings;
205+
}
206+
return m_exportOptions;
207+
}
208+
set { m_exportOptions = value; }
209+
}
210+
198211
/// <summary>
199212
/// Gets the Unity default material.
200213
/// </summary>
@@ -621,7 +634,7 @@ public bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode f
621634
return true;
622635
}
623636

624-
var fbxName = ExportSettings.UseMayaCompatibleNames()
637+
var fbxName = ExportOptions.UseMayaCompatibleNames()
625638
? ConvertToMayaCompatibleName(unityName) : unityName;
626639

627640
if (Verbose) {
@@ -1928,7 +1941,7 @@ protected int ExportTransformHierarchy(
19281941
{
19291942
int numObjectsExported = exportProgress;
19301943

1931-
if (ExportSettings.UseMayaCompatibleNames()) {
1944+
if (ExportOptions.UseMayaCompatibleNames()) {
19321945
unityGo.name = ConvertToMayaCompatibleName (unityGo.name);
19331946
}
19341947

@@ -2189,7 +2202,7 @@ private bool ExportGameObjectAndParents(
21892202
return true;
21902203
}
21912204

2192-
if (ExportSettings.UseMayaCompatibleNames()) {
2205+
if (ExportOptions.UseMayaCompatibleNames()) {
21932206
unityGo.name = ConvertToMayaCompatibleName (unityGo.name);
21942207
}
21952208

@@ -2705,9 +2718,7 @@ public enum TransformExportType { Local, Global, Reset };
27052718
/// </summary>
27062719
public int ExportAll (
27072720
IEnumerable<UnityEngine.Object> unityExportSet,
2708-
Dictionary<GameObject, AnimationOnlyExportData> animationExportData,
2709-
TransformExportType exportType = TransformExportType.Global,
2710-
ExportModelSettingsSerialize.LODExportType lodExportType = ExportModelSettingsSerialize.LODExportType.All)
2721+
Dictionary<GameObject, AnimationOnlyExportData> animationExportData)
27112722
{
27122723
exportCancelled = false;
27132724

@@ -2740,7 +2751,7 @@ public int ExportAll (
27402751
// Initialize the exporter.
27412752
// fileFormat must be binary if we are embedding textures
27422753
int fileFormat = -1;
2743-
if (EditorTools.ExportSettings.GetExportFormat() == ExportModelSettingsSerialize.ExportFormat.ASCII)
2754+
if (ExportOptions.GetExportFormat() == ExportModelSettingsSerialize.ExportFormat.ASCII)
27442755
{
27452756
fileFormat = fbxManager.GetIOPluginRegistry().FindWriterIDByDescription("FBX ascii (*.fbx)");
27462757
}
@@ -2811,23 +2822,25 @@ public int ExportAll (
28112822
}
28122823

28132824
Vector3 center = Vector3.zero;
2814-
if(exportType == TransformExportType.Global){
2815-
switch(ExportSettings.GetObjectPosition()){
2816-
case ExportModelSettingsSerialize.ObjectPosition.LocalCentered:
2817-
// one object to export -> move to (0,0,0)
2818-
if(revisedExportSet.Count == 1){
2819-
var tempList = new List<GameObject>(revisedExportSet);
2820-
center = tempList[0].transform.position;
2821-
break;
2822-
}
2823-
// more than one object to export -> get bounding center
2824-
center = FindCenter(revisedExportSet);
2825-
break;
2826-
// absolute center -> don't do anything
2827-
default:
2828-
center = Vector3.zero;
2825+
TransformExportType exportType = TransformExportType.Global;
2826+
switch(ExportOptions.GetObjectPosition()){
2827+
case ExportModelSettingsSerialize.ObjectPosition.LocalCentered:
2828+
// one object to export -> move to (0,0,0)
2829+
if(revisedExportSet.Count == 1){
2830+
var tempList = new List<GameObject>(revisedExportSet);
2831+
center = tempList[0].transform.position;
28292832
break;
28302833
}
2834+
// more than one object to export -> get bounding center
2835+
center = FindCenter(revisedExportSet);
2836+
break;
2837+
case ExportModelSettingsSerialize.ObjectPosition.Reset:
2838+
exportType = TransformExportType.Reset;
2839+
break;
2840+
// absolute center -> don't do anything
2841+
default:
2842+
center = Vector3.zero;
2843+
break;
28312844
}
28322845

28332846
foreach (var unityGo in revisedExportSet) {
@@ -2837,7 +2850,7 @@ public int ExportAll (
28372850
}
28382851
else {
28392852
exportProgress = this.ExportTransformHierarchy (unityGo, fbxScene, fbxRootNode,
2840-
exportProgress, count, center, exportType, lodExportType);
2853+
exportProgress, count, center, exportType, ExportOptions.GetLODExportType());
28412854
}
28422855
if (exportCancelled || exportProgress < 0) {
28432856
Debug.LogWarning ("Export Cancelled");
@@ -3001,7 +3014,7 @@ public static void ExportSingleTimelineClip(TimelineClip timelineClipSelected, G
30013014
animationTrackGObject,
30023015
timelineClipSelected.animationClip
30033016
};
3004-
ExportObjects (filePath, myArray, AnimationExportType.timelineAnimationClip);
3017+
//ExportObjects (filePath, myArray, AnimationExportType.timelineAnimationClip);
30053018
}
30063019

30073020
/// <summary>
@@ -3060,7 +3073,7 @@ public static void ExportAllTimelineClips(GameObject objectWithPlayableDirector,
30603073
foreach (TimelineClip timeLineClip in at.GetClips()) {
30613074
string filePath = string.Format(AnimFbxFileFormat, folderPath, atObject.name, timeLineClip.displayName);
30623075
UnityEngine.Object[] myArray = new UnityEngine.Object[] { atObject, timeLineClip.animationClip };
3063-
ExportObjects (filePath, myArray, AnimationExportType.timelineAnimationClip);
3076+
//ExportObjects (filePath, myArray, AnimationExportType.timelineAnimationClip);
30643077
}
30653078
}
30663079
}
@@ -3611,18 +3624,9 @@ private static string GetExportFilePath(string filenameSuggestion = ""){
36113624
private static void OnExport (AnimationExportType exportType = AnimationExportType.all)
36123625
{
36133626
GameObject [] selectedGOs = Selection.GetFiltered<GameObject> (SelectionMode.TopLevel);
3614-
string filename = null;
3615-
if (selectedGOs.Length == 1) {
3616-
filename = ConvertToValidFilename (selectedGOs [0].name + "." + kFBXFileExtension);
3617-
} else {
3618-
filename = string.IsNullOrEmpty (LastFilePath)
3619-
? MakeFileName (basename: FileBaseName, extension: kFBXFileExtension)
3620-
: System.IO.Path.GetFileName (LastFilePath);
3621-
}
36223627

3623-
var hierarchyCount = ModelExporter.RemoveRedundantObjects(selectedGOs).Count;
3624-
3625-
ExportModelEditorWindow.Init (filename, singleHierarchyExport: hierarchyCount == 1, exportType: exportType);
3628+
var toExport = ModelExporter.RemoveRedundantObjects(selectedGOs);
3629+
ExportModelEditorWindow.Init (System.Linq.Enumerable.Cast<UnityEngine.Object> (toExport), exportType: exportType);
36263630
}
36273631

36283632
/// <summary>
@@ -3632,21 +3636,20 @@ private static void OnExport (AnimationExportType exportType = AnimationExportTy
36323636
public static string ExportObjects (
36333637
string filePath,
36343638
UnityEngine.Object[] objects = null,
3635-
AnimationExportType exportType = AnimationExportType.all,
3636-
TransformExportType transformExportType = TransformExportType.Global,
3637-
ExportModelSettingsSerialize.LODExportType lodExportType = ExportModelSettingsSerialize.LODExportType.All)
3639+
IExportOptions exportOptions = null,
3640+
AnimationExportType exportType = AnimationExportType.all)
36383641
{
36393642
LastFilePath = filePath;
36403643

36413644
using (var fbxExporter = Create ()) {
36423645
// ensure output directory exists
36433646
EnsureDirectory (filePath);
3647+
fbxExporter.ExportOptions = exportOptions;
36443648

36453649
if (objects == null) {
36463650
objects = Selection.objects;
36473651
}
36483652

3649-
36503653
Dictionary<GameObject, AnimationOnlyExportData> animationExportData = null;
36513654
switch (exportType)
36523655
{
@@ -3675,7 +3678,7 @@ public static string ExportObjects (
36753678
break;
36763679
}
36773680

3678-
if (fbxExporter.ExportAll (objects, animationExportData, transformExportType, lodExportType) > 0) {
3681+
if (fbxExporter.ExportAll (objects, animationExportData) > 0) {
36793682
string message = string.Format ("Successfully exported: {0}", filePath);
36803683
UnityEngine.Debug.Log (message);
36813684

@@ -3687,11 +3690,10 @@ public static string ExportObjects (
36873690

36883691
public static string ExportObject (
36893692
string filePath, UnityEngine.Object root,
3690-
AnimationExportType exportType = AnimationExportType.all,
3691-
TransformExportType transformExportType = TransformExportType.Reset,
3692-
ExportModelSettingsSerialize.LODExportType lodExportType = ExportModelSettingsSerialize.LODExportType.All)
3693+
IExportOptions exportOptions = null,
3694+
AnimationExportType exportType = AnimationExportType.all)
36933695
{
3694-
return ExportObjects(filePath, new Object[] { root }, exportType, transformExportType, lodExportType);
3696+
return ExportObjects(filePath, new Object[] { root }, exportOptions, exportType: exportType);
36953697
}
36963698

36973699
private static void EnsureDirectory (string path)

0 commit comments

Comments
 (0)