Skip to content

Commit 25c5853

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master' into UNI-20467-zero-out-root-transform
# Conflicts: # Assets/FbxExporters/Editor/FbxExporter.cs
2 parents dd9dd8d + eb28aeb commit 25c5853

File tree

5 files changed

+80
-25
lines changed

5 files changed

+80
-25
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,16 @@ private static List<GameObject> OnConvertInPlace ()
5757

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

60-
var exportSet = ModelExporter.RemoveDuplicateObjects (unityActiveGOs);
60+
var exportSet = ModelExporter.RemoveRedundantObjects (unityActiveGOs);
6161
GameObject[] gosToExport = new GameObject[exportSet.Count];
6262
exportSet.CopyTo (gosToExport);
6363

6464
// find common ancestor root & filePath;
6565
string[] filePaths = new string[gosToExport.Length];
6666
string dirPath = Path.Combine (Application.dataPath, "Objects");
6767

68-
Transform[] unityCommonAncestors = new Transform[gosToExport.Length];
69-
int[] siblingIndices = new int[gosToExport.Length];
70-
7168
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");
69+
filePaths[n] = Path.Combine (dirPath, gosToExport[n].name + ".fbx");
7670
}
7771

7872
string[] fbxFileNames = new string[filePaths.Length];
@@ -86,6 +80,10 @@ private static List<GameObject> OnConvertInPlace ()
8680
for(int i = 0; i < fbxFileNames.Length; i++)
8781
{
8882
var fbxFileName = fbxFileNames [i];
83+
if (fbxFileName == null) {
84+
Debug.Log (string.Format ("Warning: Export failed for GameObject {0}", gosToExport [i].name));
85+
continue;
86+
}
8987

9088
// make filepath relative to project folder
9189
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
@@ -105,18 +103,24 @@ private static List<GameObject> OnConvertInPlace ()
105103
if (unityObj != null)
106104
{
107105
GameObject unityGO = unityObj as GameObject;
106+
Transform unityGOTransform = unityGO.transform;
107+
Transform origGOTransform = gosToExport [i].transform;
108108

109-
// configure name
110-
const string cloneSuffix = "(Clone)";
111-
112-
if (unityGO.name.EndsWith (cloneSuffix, System.StringComparison.CurrentCulture)) {
113-
unityGO.name = unityGO.name.Remove (cloneSuffix.Length - 1);
114-
}
109+
// Set the name to be the name of the instantiated asset.
110+
// This will get rid of the "(Clone)" if it's added
111+
unityGO.name = unityMainAsset.name;
115112

116113
// configure transform and maintain local pose
117-
unityGO.transform.SetParent (unityCommonAncestors[i], false);
114+
unityGOTransform.SetParent (origGOTransform.parent, false);
118115

119-
unityGO.transform.SetSiblingIndex (siblingIndices[i]);
116+
unityGOTransform.SetSiblingIndex (origGOTransform.GetSiblingIndex());
117+
118+
// copy the components over, assuming that the hierarchy order is unchanged
119+
if (origGOTransform.hierarchyCount != unityGOTransform.hierarchyCount) {
120+
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
121+
origGOTransform.hierarchyCount, unityGOTransform.hierarchyCount));
122+
}
123+
CopyComponentsRecursive (gosToExport[i], unityGO);
120124

121125
result.Add (unityObj as GameObject);
122126

@@ -139,6 +143,36 @@ private static List<GameObject> OnConvertInPlace ()
139143

140144
return result;
141145
}
146+
147+
private static void CopyComponentsRecursive(GameObject from, GameObject to){
148+
if (!to.name.StartsWith(from.name) || from.transform.childCount != to.transform.childCount) {
149+
Debug.LogError (string.Format("Error: hierarchies don't match (From: {0}, To: {1})", from.name, to.name));
150+
return;
151+
}
152+
153+
CopyComponents (from, to);
154+
for (int i = 0; i < from.transform.childCount; i++) {
155+
CopyComponentsRecursive(from.transform.GetChild(i).gameObject, to.transform.GetChild(i).gameObject);
156+
}
157+
}
158+
159+
private static void CopyComponents(GameObject from, GameObject to){
160+
var components = from.GetComponents<Component> ();
161+
for(int i = 0; i < components.Length; i++){
162+
// if to already has this component, then skip it
163+
if(components[i] == null || to.GetComponent(components[i].GetType()) != null){
164+
continue;
165+
}
166+
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (components[i]);
167+
if (success) {
168+
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
169+
}
170+
if (!success) {
171+
Debug.LogWarning (string.Format ("Warning: Failed to copy component {0} from {1} to {2}",
172+
components[i].GetType().Name, from.name, to.name));
173+
}
174+
}
175+
}
142176
}
143177
}
144178
}

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,19 @@ protected int ExportComponents (
503503
/// A count of how many GameObjects we are exporting, to have a rough
504504
/// idea of how long creating the scene will take.
505505
/// </summary>
506-
/// <returns>The object count.</returns>
506+
/// <returns>The hierarchy count.</returns>
507507
/// <param name="exportSet">Export set.</param>
508-
public int GetGameObjectCount (HashSet<GameObject> exportSet)
508+
public int GetHierarchyCount (HashSet<GameObject> exportSet)
509509
{
510510
int count = 0;
511-
foreach (var obj in exportSet) {
512-
count += obj.transform.hierarchyCount;
511+
Queue<GameObject> queue = new Queue<GameObject> (exportSet);
512+
while (queue.Count > 0) {
513+
var obj = queue.Dequeue ();
514+
var objTransform = obj.transform;
515+
foreach (Transform child in objTransform) {
516+
queue.Enqueue (child.gameObject);
517+
}
518+
count++;
513519
}
514520
return count;
515521
}
@@ -521,7 +527,7 @@ public int GetGameObjectCount (HashSet<GameObject> exportSet)
521527
/// </summary>
522528
/// <returns>The revised export set</returns>
523529
/// <param name="unityExportSet">Unity export set.</param>
524-
public static HashSet<GameObject> RemoveDuplicateObjects(IEnumerable<UnityEngine.Object> unityExportSet)
530+
public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine.Object> unityExportSet)
525531
{
526532
// basically just remove the descendents from the unity export set
527533
HashSet<GameObject> toExport = new HashSet<GameObject> ();
@@ -616,8 +622,8 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
616622
// export set of object
617623
FbxNode fbxRootNode = fbxScene.GetRootNode ();
618624
int i = 0;
619-
var revisedExportSet = RemoveDuplicateObjects(unityExportSet);
620-
int count = GetGameObjectCount (revisedExportSet);
625+
var revisedExportSet = RemoveRedundantObjects(unityExportSet);
626+
int count = GetHierarchyCount (revisedExportSet);
621627

622628
if(revisedExportSet.Count == 1){
623629
foreach(var unityGo in revisedExportSet){
File renamed without changes.
File renamed without changes.

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,22 @@ Packaging
1919
export PROJECT_PATH=~/Development/FbxExporters
2020
export UNITY3D_PATH=/Applications/Unity\ 5.6.1f1/Unity.app/Contents/MacOS/Unity
2121
export PACKAGE_NAME=FbxExporters
22-
export PACKAGE_VERSION=0.0.3a
22+
export PACKAGE_VERSION=0.0.4a
23+
export FBXSDK_PACKAGE_PATH=~/Development/FbxSharp/FbxSdk_0.0.4a.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)