-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBXUpdater.cs
382 lines (286 loc) · 13.5 KB
/
FBXUpdater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#if UNITY_EDITOR
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using VRC.Core;
using VRC.SDK3.Avatars.Components;
using static UnityEngine.EventSystems.EventTrigger;
[CustomEditor(typeof(FBXUpdater))]
public class FBXUpdaterInspector : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
GUILayout.Label("");
((FBXUpdater)target).UpdatingTo = EditorGUILayout.ObjectField("Parent Object Of New Ver:", ((FBXUpdater)target).UpdatingTo, typeof(GameObject), true) as GameObject;
GUILayout.Label("Note: This Is To Be Added To The OLD PARENT GameObject Of Your Model.");
if (GUILayout.Button("Update To New FBX!"))
{
((FBXUpdater)target).UpdateFBX();
}
}
}
[DisallowMultipleComponent]
[RequireComponent(typeof(VRCAvatarDescriptor))]
public class FBXUpdater : MonoBehaviour
{
public GameObject UpdatingTo;
private static string GetPath(Transform obj, bool MakeRelative = false)
{
var text = "/" + obj.name;
while (obj.parent != null)
{
obj = obj.parent.transform;
text = "/" + obj.name + text;
}
text = text.Substring(1); // Remove / At Start?
if (MakeRelative)
{
text = text.Substring(text.IndexOf("/") + 1);
}
return text;
}
public void UpdateFBX()
{
Debug.Log("Init!");
if (PrefabUtility.IsPartOfPrefabInstance(UpdatingTo))
{
Debug.Log("Unpacking UpdatingTo Object!");
PrefabUtility.UnpackPrefabInstance(UpdatingTo, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
}
var AllComponentsOld = gameObject.GetComponentsInChildren<Component>(true);
var AllComponentsNew = UpdatingTo.GetComponentsInChildren<Component>(true);
var AllOldTransforms = ToComponentList<Transform>(AllComponentsOld);
var AllNewTransforms = ToComponentList<Transform>(AllComponentsNew);
Debug.Log("Got All Components!");
#region Update SkinnedMeshRenderers' Materials
var skinnedMeshRenderersOld = ToComponentList<SkinnedMeshRenderer>(AllComponentsOld).Select(Old => (Old, ToComponentList<SkinnedMeshRenderer>(AllComponentsNew).FirstOrDefault(p => (p.sharedMesh.name == Old.sharedMesh.name && !AssetDatabase.GetAssetPath(p).Contains("unity default resources")) || p.name == Old.name))).Where(o => o.Old != null && o.Item2 != null).ToList();
Debug.Log($"Got {skinnedMeshRenderersOld.Count} SkinnedMeshRenderers With Matching Mesh Names & SubMesh Counts!");
for (var index = 0; index < skinnedMeshRenderersOld.Count; index++)
{
var entry = skinnedMeshRenderersOld[index];
// Copy Shape Key Weights
for (var i = 0; i < entry.Old.sharedMesh.blendShapeCount; i++)
{
var NameForID = entry.Old.sharedMesh.GetBlendShapeName(i);
var IDOfNew = -1;
for (var i2 = 0; i2 < entry.Item2.sharedMesh.blendShapeCount; i2++)
{
var NewNameForID = entry.Item2.sharedMesh.GetBlendShapeName(i2);
if (NewNameForID == NameForID)
{
IDOfNew = i2;
break;
}
}
if (IDOfNew != -1)
{
entry.Item2.SetBlendShapeWeight(IDOfNew, entry.Old.GetBlendShapeWeight(i));
}
}
var AnchorPath = GetPath(entry.Old.probeAnchor, true);
entry.Item2.probeAnchor = UpdatingTo.transform.Find(AnchorPath);
entry.Item2.gameObject.SetActive(entry.Old.gameObject.activeSelf);
entry.Item2.gameObject.name = entry.Old.gameObject.name;
//Hierarchy Matching
if (entry.Old.transform.parent.name != entry.Old.transform.root.name)
{
var PathToCreate = "";
var CurrentObject = entry.Old.transform.parent;
while (CurrentObject != entry.Old.transform.root) // Create Path String - Loop
{
if (CurrentObject == null || string.IsNullOrWhiteSpace(CurrentObject.name))
{
break;
}
if (string.IsNullOrWhiteSpace(PathToCreate))
{
PathToCreate = CurrentObject.name;
}
else
{
PathToCreate = CurrentObject.name + "/" + PathToCreate;
}
CurrentObject = CurrentObject.parent;
}
Debug.Log($"Path To Create: {PathToCreate} - This Always Should Show Above A Non Object ForEach Set Inside");
if (FindOrNull(UpdatingTo.transform, PathToCreate) == null)
{
GameObject CurrentDupedObj = null;
foreach (var ObjName in PathToCreate.Split('/')) // Left to right, begins at rootmost
{
if (string.IsNullOrWhiteSpace(ObjName))
{
break;
}
var NewDupe = UpdatingTo.transform.Find(ObjName)?.gameObject ?? new GameObject(ObjName);
Debug.Log($"[ForEach] Created Object {NewDupe.name}! - [D]: Path: {PathToCreate}");
if (CurrentDupedObj != null)
{
NewDupe.transform.SetParent(CurrentDupedObj.transform);
Debug.Log($"[ForEach] Set Object {NewDupe.name} Inside {CurrentDupedObj.name}! - [D]: Path: {PathToCreate}");
}
CurrentDupedObj = NewDupe;
}
if (CurrentDupedObj != null)
{
entry.Item2.transform.SetParent(CurrentDupedObj.transform);
Debug.Log($"[ForEach] Set {entry.Item2.transform.name} Inside {CurrentDupedObj.name}! - [D]: Path: {PathToCreate}");
CurrentDupedObj.transform.root.SetParent(UpdatingTo.transform);
Debug.Log($"Set Path Inside Root!");
}
Debug.LogWarning($"Created Path As It Never Existed!");
}
else
{
Debug.Log($"Path To Create Found!");
entry.Item2.transform.SetParent(FindOrNull(UpdatingTo.transform, PathToCreate));
}
}
entry.Item2.sharedMaterials = CopyMaterials(entry.Old.sharedMaterials, entry.Item2.sharedMaterials);
}
Debug.Log($"Finished Updating {skinnedMeshRenderersOld.Count} SkinnedMeshRenderers' Materials!");
#endregion
#region Update MeshRenderers' / MeshFilters' Materials
var meshRenderersOld = ToComponentList<MeshFilter>(AllComponentsOld).Select(Old => (Old, ToComponentList<MeshFilter>(AllComponentsNew).FirstOrDefault(p => p.sharedMesh.name == Old.sharedMesh.name && !AssetDatabase.GetAssetPath(p).Contains("unity default resources")))).Where(o => o.Old != null && o.Item2 != null).ToList();
Debug.Log($"Got {meshRenderersOld.Count} MeshRenderers With Matching Mesh Names & That Aren't Unity Default Meshes!");
for (var index = 0; index < meshRenderersOld.Count; index++)
{
var entry = meshRenderersOld[index];
var OldRenderer = entry.Old.GetComponent<MeshRenderer>();
var NewRenderer = entry.Item2.GetComponent<MeshRenderer>();
NewRenderer.sharedMaterials = CopyMaterials(OldRenderer.sharedMaterials, NewRenderer.sharedMaterials);
}
Debug.Log($"Finished Updating {meshRenderersOld.Count} MeshRenderers' Materials!");
#endregion
#region Move VRC Components To New
var Descriptor = ToComponentList<VRCAvatarDescriptor>(AllComponentsOld).FirstOrDefault();
var NewDescriptor = UpdatingTo.AddComponent<VRCAvatarDescriptor>();
EditorUtility.CopySerialized(Descriptor, NewDescriptor);
Debug.Log("Copied Old VRCAvatarDescriptor To New!");
var PipelineMan = ToComponentList<PipelineManager>(AllComponentsOld).FirstOrDefault();
var NewPipelineMan = UpdatingTo.AddComponent<PipelineManager>();
EditorUtility.CopySerialized(PipelineMan, NewPipelineMan);
Debug.Log("Copied Old PipelineManager To New!");
#endregion
//var AllNonMatchingObjects = AllOldTransforms.Where(u => u?.parent != null && u?.parent?.name != "Armature" && u?.name != "Armature" && u.parent != u.root && AllNewTransforms.Where(o => o?.parent != null && o?.parent?.name != "Armature" && o?.name != "Armature" && o.parent != o.root).All(o => (o.parent.name.Replace(".", "_") + "/" + o.name.Replace(".", "_")) != (u.parent.name.Replace(".", "_") + "/" + u.name.Replace(".", "_")))).OrderBy(i => i.GetParentCount());
//var PrevCopied = new List<GameObject>();
//foreach (var entry in AllNonMatchingObjects)
//{
// if (PrevCopied.Any(o => o.transform.IsChildOf(o.transform)))
// {
// continue;
// }
// var path = GetPath(entry.parent, true);
// var ToPath = path.Replace(".", "_");
// Debug.Log($"ToPath: {ToPath}");
// if (string.IsNullOrEmpty(ToPath))
// {
// Debug.LogError("Empty Path!");
// continue;
// }
// var ToPathObj = UpdatingTo.transform.Find(ToPath);
// if (ToPathObj == null)
// {
// Debug.LogError("ToPathObj == null!");
// continue;
// }
// var copy = Object.Instantiate(entry.gameObject);
// copy.transform.SetParent(ToPathObj);
// PrevCopied.Add(entry.gameObject);
//}
#region Organize Transforms And Match Scales
var AllMatchingObjects = AllOldTransforms.Where(i => i != null && i.parent != null).Select(Old => (Old, AllNewTransforms.Where(u => u.parent != null).FirstOrDefault(o => (Old.parent.name + "/" + Old.name) == (o.parent.name + "/" + o.name)))).Where(p => p.Item2 != null);
foreach (var entry in AllMatchingObjects)
{
entry.Item2.SetSiblingIndex(entry.Old.GetSiblingIndex());
entry.Item2.localScale = entry.Old.localScale;
}
UpdatingTo.transform.localScale = transform.localScale;
#endregion
}
private Material[] CopyMaterials(Material[] From, Material[] To)
{
if (To == null && From != null)
{
To = From;
return To;
}
if (From == null)
{
return To;
}
if (From.Length == To.Length)
{
To = From;
}
else
{
var MatchingMaterialsOnEntry = From.Where(z => z != null).Select(Old => (Old, To.FirstOrDefault(p => p?.name != null && (p.name.Replace(" (Instance)", "").Contains(Old.name.Replace(" (Instance)", "")) || (p.mainTexture != null && Old.mainTexture != null && p.mainTexture.name.Replace(" (Instance)", "").Contains(Old.mainTexture.name.Replace(" (Instance)", ""))))))).Where(o => o.Old != null && o.Item2 != null).ToList();
for (var i = 0; i < MatchingMaterialsOnEntry.Count; i++)
{
var matching = MatchingMaterialsOnEntry[i];
//Debug.Log($"Updating Material: {matching.Item2.name.Replace(" (Instance)", "")} On Mesh Object: {entry.Item2.name} To: {matching.Old.name.Replace(" (Instance)", "")}!");
if (matching.Item2 != null)
{
var IndexToUpdate = To.ToList().FindIndex(o => o.name == matching.Item2.name);
To[IndexToUpdate] = matching.Old;
}
}
}
return To;
}
public static Transform FindOrNull(Transform transform, string path)
{
try
{
path = path.Replace("\\", "/");
if (path[0] == '/')
{
path = path.Substring(1, path.Length);
}
if (path[path.Length - 1] == '/')
{
path = path.Substring(0, path.Length - 1);
}
var EndObject = transform;
foreach (var child in path.Split('/'))
{
if (!string.IsNullOrWhiteSpace(child) && child.Length > 1)
{
EndObject = EndObject.Find(child.Replace("/", ""));
}
}
if (EndObject == null || EndObject == transform)
{
Debug.LogError($"Failed To Find Child Object At Path: {path}");
}
return EndObject;
}
catch
{
}
Debug.LogError($"Failed To Find Child Object At Path: {path}");
return null;
}
public static List<T> ToComponentList<T>(IEnumerable<Component> list) where T : Component
{
return OfILCastedType<T>(list);
}
public static List<T> OfILCastedType<T>(IEnumerable<Component> source) where T : Component
{
return OfTypeIterator<T>(source).ToList();
}
private static IEnumerable<T> OfTypeIterator<T>(IEnumerable<Component> source) where T : Component
{
foreach (var obj in source)
{
if (obj != null && obj is T result)
{
yield return result;
}
}
}
}
#endif