public static void ReimportModel(LXFMLDoc lxfml, Model model, DictionaryIntToModelGroupImportSettings importSettings) { var brickBuilding = SceneBrickBuilder.GetToggleBrickBuildingStatus(); if (brickBuilding) { SceneBrickBuilder.ToggleBrickBuilding(); } // FIXME Next version could include option to match groups up manually. var groups = model.GetComponentsInChildren <ModelGroup>(); for (var i = groups.Length - 1; i >= 0; i--) { var group = groups[i]; if (group.autoGenerated) { Undo.DestroyObjectImmediate(group.gameObject); } else if (group.number >= lxfml.groups.Length) { Debug.LogWarning("Group " + group.number + " " + group.groupName + " does not match up with files. Wiping."); Undo.DestroyObjectImmediate(group.gameObject); } } groups = model.GetComponentsInChildren <ModelGroup>(); var removedGroups = new List <LXFMLDoc.BrickGroup>(); for (var i = 0; i < lxfml.groups.Length; i++) { LXFMLDoc.BrickGroup group = lxfml.groups[i]; bool exists = false; for (var j = 0; j < groups.Length; j++) { if (groups[j].number == group.number) { exists = true; break; } } if (!exists) { removedGroups.Add(group); } } // Assign the new model import settings to the model. model.importSettings = new DictionaryIntToModelGroupImportSettings(importSettings); var bricksWithConnectivity = new HashSet <Brick>(); if (removedGroups.Count > 0) { var resultBricks = new Dictionary <int, Brick>(lxfml.bricks.Count); foreach (var group in removedGroups) { var number = group.number; InstantiateModelBricks(lxfml, model.importSettings, ref resultBricks, number); var groupGO = InstantiateModelGroup(group, number, model.gameObject, model.absoluteFilePath, model.relativeFilePath, ref resultBricks, false, importSettings[number]); groupGO.transform.position = model.transform.position; Undo.RegisterCreatedObjectUndo(groupGO, "Re-creating model group"); if (importSettings[number].connectivity) { bricksWithConnectivity.UnionWith(groupGO.GetComponentsInChildren <Brick>()); } } } foreach (var group in groups) { group.absoluteFilePath = model.absoluteFilePath; group.relativeFilePath = model.relativeFilePath; ReimportModelGroup(lxfml, group, importSettings[group.number]); if (group.importSettings.connectivity) { bricksWithConnectivity.UnionWith(group.GetComponentsInChildren <Brick>()); } } if (bricksWithConnectivity.Count > 0) { var sceneBricks = new HashSet <Brick>(StageUtility.GetCurrentStageHandle().FindComponentsOfType <Brick>()); DetectConnectivity(bricksWithConnectivity, sceneBricks); } if (brickBuilding) { SceneBrickBuilder.ToggleBrickBuilding(); } if (SceneBrickBuilder.GetAutoUpdateHierarchy()) { groups = model.GetComponentsInChildren <ModelGroup>(); var bricks = new HashSet <Brick>(); foreach (var group in groups) { if (group.importSettings.connectivity) { var groupBricks = group.GetComponentsInChildren <Brick>(); foreach (var brick in groupBricks) { bricks.Add(brick); } } } // On reimport, the model will be positioned weirdly compared to the rest of the scene, so we just ignore all other bricks ModelGroupUtility.RecomputeModelGroups(bricks); } EditorUtility.ClearProgressBar(); }
/// <summary> /// Instantiate all bricks and groups in an LXFML document /// </summary> /// <param name="lxfml">The LXFML document</param> /// <param name="nameOfObject">Path of the LXFML document</param> public static GameObject InstantiateModel(LXFMLDoc lxfml, string filePath, Model.Pivot pivot, DictionaryIntToModelGroupImportSettings importSettings) { //Create "root" LXFML gameobject GameObject parent = new GameObject(Path.GetFileNameWithoutExtension(filePath)); Undo.RegisterCreatedObjectUndo(parent, "Model"); parent.transform.position = Vector3.zero; var model = parent.AddComponent <Model>(); model.absoluteFilePath = filePath; model.relativeFilePath = PathUtils.GetRelativePath(Directory.GetCurrentDirectory(), filePath); model.pivot = pivot; model.importSettings = new DictionaryIntToModelGroupImportSettings(importSettings); EditorUtility.DisplayProgressBar("Importing", "Creating bricks.", 0.0f); var resultBricks = new Dictionary <int, Brick>(lxfml.bricks.Count); InstantiateModelBricks(lxfml, importSettings, ref resultBricks); EditorUtility.DisplayProgressBar("Importing", "Creating groups.", 0.8f); if (resultBricks.Count > 0) { var bricksWithConnectivity = new HashSet <Brick>(); var groups = lxfml.groups; for (int i = 0; i < groups.Length; i++) { var number = groups[i].number; GameObject groupParent = InstantiateModelGroup(groups[i], i, parent, filePath, model.relativeFilePath, ref resultBricks, false, importSettings[number]); if (importSettings[number].connectivity) { bricksWithConnectivity.UnionWith(groupParent.GetComponentsInChildren <Brick>()); } } if (bricksWithConnectivity.Count > 0) { var sceneBricks = new HashSet <Brick>(StageUtility.GetCurrentStageHandle().FindComponentsOfType <Brick>()); DetectConnectivity(bricksWithConnectivity, sceneBricks); } if (SceneBrickBuilder.GetAutoUpdateHierarchy()) { var bricks = new HashSet <Brick>(); foreach (var pair in resultBricks) { foreach (var part in pair.Value.parts) { if (part.connectivity) { bricks.Add(pair.Value); break; } } } // On import, the model will be positioned weirdly compared to the rest of the scene, so we just ignore all other bricks ModelGroupUtility.RecomputeModelGroups(bricks, false, ModelGroupUtility.UndoBehavior.withoutUndo); } } // Change the pivot. if (pivot != Model.Pivot.Original) { EditorUtility.DisplayProgressBar("Importing", "Computing bounds.", 0.9f); var bounds = ComputeBounds(parent.transform); var newPivot = bounds.center; switch (pivot) { case Model.Pivot.BottomCenter: { newPivot += -parent.transform.up * bounds.extents.y; break; } } foreach (Transform child in parent.transform) { child.position -= newPivot; } parent.transform.position += newPivot; } // Add LEGOModelAsset component. parent.AddComponent <LEGOModelAsset>(); EditorUtility.ClearProgressBar(); return(parent); }