Skip to content

Commit 6aa72d6

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master' into UNI-19962-copy-over-components
# Conflicts: # Assets/FbxExporters/Editor/ConvertToModel.cs
2 parents eb62260 + 057f6f9 commit 6aa72d6

File tree

3 files changed

+108
-48
lines changed

3 files changed

+108
-48
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,40 @@ private static List<GameObject> OnConvertInPlace ()
5757

5858
GameObject [] unityActiveGOs = Selection.GetFiltered<GameObject> (SelectionMode.Editable | SelectionMode.TopLevel);
5959

60+
var exportSet = ModelExporter.RemoveDuplicateObjects (unityActiveGOs);
61+
GameObject[] gosToExport = new GameObject[exportSet.Count];
62+
exportSet.CopyTo (gosToExport);
63+
6064
// find common ancestor root & filePath;
61-
string filePath = "";
65+
string[] filePaths = new string[gosToExport.Length];
6266
string dirPath = Path.Combine (Application.dataPath, "Objects");
6367

64-
GameObject unityCommonAncestor = null;
65-
int siblingIndex = -1;
66-
67-
foreach (GameObject goObj in unityActiveGOs) {
68-
siblingIndex = goObj.transform.GetSiblingIndex ();
69-
unityCommonAncestor = (goObj.transform.parent != null) ? goObj.transform.parent.gameObject : null;
70-
filePath = Path.Combine (dirPath, goObj.name + ".fbx");
68+
Transform[] unityCommonAncestors = new Transform[gosToExport.Length];
69+
int[] siblingIndices = new int[gosToExport.Length];
7170

72-
break;
71+
for(int n = 0; n < gosToExport.Length; n++){
72+
GameObject goObj = gosToExport[n];
73+
unityCommonAncestors[n] = goObj.transform.parent;
74+
siblingIndices [n] = goObj.transform.GetSiblingIndex ();
75+
filePaths[n] = Path.Combine (dirPath, goObj.name + ".fbx");
7376
}
7477

75-
string fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filePath, unityActiveGOs) as string;
78+
string[] fbxFileNames = new string[filePaths.Length];
79+
80+
for (int j = 0; j < gosToExport.Length; j++) {
81+
fbxFileNames[j] = FbxExporters.Editor.ModelExporter.ExportObjects (filePaths[j],
82+
new UnityEngine.Object[] {gosToExport[j]}) as string;
83+
}
7684

77-
if (fbxFileName != null)
85+
List<GameObject> selection = new List<GameObject> ();
86+
for(int i = 0; i < fbxFileNames.Length; i++)
7887
{
88+
var fbxFileName = fbxFileNames [i];
89+
if (fbxFileName == null) {
90+
Debug.Log (string.Format ("Warning: Export failed for GameObject {0}", gosToExport [i].name));
91+
continue;
92+
}
93+
7994
// make filepath relative to project folder
8095
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
8196
{
@@ -96,19 +111,14 @@ private static List<GameObject> OnConvertInPlace ()
96111
GameObject unityGO = unityObj as GameObject;
97112
Transform unityGOTransform = unityGO.transform;
98113

99-
// configure name
100-
const string cloneSuffix = "(Clone)";
101-
102-
if (unityGO.name.EndsWith (cloneSuffix, System.StringComparison.CurrentCulture)) {
103-
unityGO.name = unityGO.name.Remove (cloneSuffix.Length - 1);
104-
}
114+
// Set the name to be the name of the instantiated asset.
115+
// This will get rid of the "(Clone)" if it's added
116+
unityGO.name = unityMainAsset.name;
105117

106118
// configure transform and maintain local pose
107-
if (unityCommonAncestor != null) {
108-
unityGOTransform.SetParent (unityCommonAncestor.transform, false);
109-
}
119+
unityGO.transform.SetParent (unityCommonAncestors[i], false);
110120

111-
unityGOTransform.SetSiblingIndex (siblingIndex);
121+
unityGO.transform.SetSiblingIndex (siblingIndices[i]);
112122

113123
// copy the components over, assuming that the hierarchy order is unchanged
114124
if (unityActiveGOs.Length == 1) {
@@ -125,25 +135,23 @@ private static List<GameObject> OnConvertInPlace ()
125135

126136
result.Add (unityObj as GameObject);
127137

128-
// remove (now redundant) gameobjects
129-
for (int i = 0; i < unityActiveGOs.Length; i++) {
138+
// remove (now redundant) gameobject
130139
#if UNI_19965
131-
Object.DestroyImmediate (unityActiveGOs [i]);
140+
Object.DestroyImmediate (unityActiveGOs [i]);
132141
#else
133-
// rename and put under scene root in case we need to check values
134-
unityActiveGOs [i].name = "_safe_to_delete_" + unityActiveGOs [i].name;
135-
unityActiveGOs [i].transform.parent = null;
136-
unityActiveGOs [i].SetActive (false);
142+
// rename and put under scene root in case we need to check values
143+
gosToExport [i].name = "_safe_to_delete_" + gosToExport[i].name;
144+
gosToExport [i].SetActive (false);
137145
#endif
138-
}
139-
140146
// select the instanced Model Prefab
141-
Selection.objects = new GameObject[] {unityGO};
147+
selection.Add(unityGO);
142148
}
143149
}
144150

145151
}
146152

153+
Selection.objects = selection.ToArray ();
154+
147155
return result;
148156
}
149157

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,58 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
484484
/// A count of how many GameObjects we are exporting, to have a rough
485485
/// idea of how long creating the scene will take.
486486
/// </summary>
487-
/// <returns>The object count.</returns>
487+
/// <returns>The hierarchy count.</returns>
488488
/// <param name="exportSet">Export set.</param>
489-
public int GetGameObjectCount (IEnumerable<UnityEngine.Object> exportSet)
489+
public int GetHierarchyCount (HashSet<GameObject> exportSet)
490490
{
491491
int count = 0;
492-
foreach (var obj in exportSet) {
492+
Queue<GameObject> queue = new Queue<GameObject> (exportSet);
493+
while (queue.Count > 0) {
494+
var obj = queue.Dequeue ();
495+
var objTransform = obj.transform;
496+
foreach (Transform child in objTransform) {
497+
queue.Enqueue (child.gameObject);
498+
}
499+
count++;
500+
}
501+
return count;
502+
}
503+
504+
/// <summary>
505+
/// Removes objects that will already be exported anyway.
506+
/// E.g. if a parent and its child are both selected, then the child
507+
/// will be removed from the export set.
508+
/// </summary>
509+
/// <returns>The revised export set</returns>
510+
/// <param name="unityExportSet">Unity export set.</param>
511+
protected HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine.Object> unityExportSet)
512+
{
513+
// basically just remove the descendents from the unity export set
514+
HashSet<GameObject> toExport = new HashSet<GameObject> ();
515+
HashSet<UnityEngine.Object> hashedExportSet = new HashSet<Object> (unityExportSet);
516+
517+
foreach(var obj in unityExportSet){
493518
var unityGo = GetGameObject (obj);
519+
494520
if (unityGo) {
495-
count += unityGo.transform.hierarchyCount;
521+
// if any of this nodes ancestors is already in the export set,
522+
// then ignore it, it will get exported already
523+
bool parentInSet = false;
524+
var parent = unityGo.transform.parent;
525+
while (parent != null) {
526+
if (hashedExportSet.Contains (parent.gameObject)) {
527+
parentInSet = true;
528+
break;
529+
}
530+
parent = parent.parent;
531+
}
532+
533+
if (!parentInSet) {
534+
toExport.Add (unityGo);
535+
}
496536
}
497537
}
498-
return count;
538+
return toExport;
499539
}
500540

501541
/// <summary>
@@ -561,16 +601,13 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
561601
// export set of object
562602
FbxNode fbxRootNode = fbxScene.GetRootNode ();
563603
int i = 0;
564-
int count = GetGameObjectCount (unityExportSet);
565-
foreach (var obj in unityExportSet) {
566-
var unityGo = GetGameObject (obj);
567-
568-
if (unityGo) {
569-
i = this.ExportComponents (unityGo, fbxScene, fbxRootNode, i, count);
570-
if (i < 0) {
571-
Debug.LogWarning ("Export Cancelled");
572-
return 0;
573-
}
604+
var revisedExportSet = RemoveRedundantObjects(unityExportSet);
605+
int count = GetHierarchyCount (revisedExportSet);
606+
foreach (var unityGo in revisedExportSet) {
607+
i = this.ExportComponents (unityGo, fbxScene, fbxRootNode, i, count);
608+
if (i < 0) {
609+
Debug.LogWarning ("Export Cancelled");
610+
return 0;
574611
}
575612
}
576613

@@ -801,7 +838,7 @@ public MeshInfo (GameObject gameObject, Mesh mesh)
801838
/// <summary>
802839
/// Get the GameObject
803840
/// </summary>
804-
private GameObject GetGameObject (Object obj)
841+
private static GameObject GetGameObject (Object obj)
805842
{
806843
if (obj is UnityEngine.Transform) {
807844
var xform = obj as UnityEngine.Transform;

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ export PROJECT_PATH=~/Development/FbxExporters
2020
export UNITY3D_PATH=/Applications/Unity\ 5.6.1f1/Unity.app/Contents/MacOS/Unity
2121
export PACKAGE_NAME=FbxExporters
2222
export PACKAGE_VERSION=0.0.3a
23+
export FBXSDK_PACKAGE_PATH=/path/to/FbxSdk.unitypackage
2324
24-
"${UNITY3D_PATH}" -batchmode -projectPath "${PROJECT_PATH}" -exportPackage Assets/FbxExporters ${PROJECT_PATH}/${PACKAGE_NAME}_${PACKAGE_VERSION}.unitypackage -quit
25+
"${UNITY3D_PATH} -projectPath "${PROJECT_PATH}" -importPackage ${FBXSDK_PACKAGE_PATH} -quit
26+
"${UNITY3D_PATH}" -batchmode -projectPath "${PROJECT_PATH}" -exportPackage Assets/FbxExporters Assets/FbxSdk ${PROJECT_PATH}/${PACKAGE_NAME}_${PACKAGE_VERSION}.unitypackage -quit
27+
```
28+
29+
**On Windows**
30+
31+
```
32+
set PROJECT_PATH=/path/to/FbxExporters
33+
set UNITY3D_PATH="C:/Program Files/Unity/Editor/Unity.exe"
34+
set PACKAGE_NAME=FbxExporters
35+
set PACKAGE_VERSION=0.0.3a
36+
set FBXSDK_PACKAGE_PATH=/path/to/FbxSdk.unitypackage
37+
38+
%UNITY3D_PATH% -projectPath "%PROJECT_PATH%" -importPackage %FBXSDK_PACKAGE_PATH% -quit
39+
%UNITY3D_PATH% -batchmode -projectPath "%PROJECT_PATH%" -exportPackage Assets/FbxExporters Assets/FbxSdk %PROJECT_PATH%/%PACKAGE_NAME%_%PACKAGE_VERSION%.unitypackage -quit
2540
```

0 commit comments

Comments
 (0)