public static void pasteFromJSONString(AXModel m, string json_string) { List <AXParametricObject> poList = new List <AXParametricObject>(); List <AXRelation> relationList = new List <AXRelation>(); AXParametricObject po = null; Debug.Log(json_string); AX.SimpleJSON.JSONNode jn = AX.SimpleJSON.JSON.Parse(json_string); if (jn == null || jn["parametric_objects"] == null) { return; } foreach (AX.SimpleJSON.JSONNode pn in jn["parametric_objects"].AsArray) { po = JSONSerializersAX.ParametricObjectFromJSON(pn); po.rect = new Rect(po.rect.x + 125, po.rect.y + 125, po.rect.width, po.rect.height); poList.Add(po); } //Debug.Log ("TEST: " + poList.Count + " - " + poList[0].isHead() + " - " + poList[0].generator + " .... " + jn["model_guid"].Value + " - " + m.Guid); if (poList.Count == 1 && poList[0].isHead() && jn["model_guid"].Value == m.Guid) { // common case of copying and pasting a single palette which is a head into the same model // as the source po // Find the source and instance it. AXParametricObject src_po = m.getParametricObjectByGUID(poList[0].Guid); List <AXParametricObject> src_List = new List <AXParametricObject>(); src_List.Add(src_po); m.instancePOsFromList(src_List); } else { foreach (AX.SimpleJSON.JSONNode rNode in jn["relations"].AsArray) { relationList.Add(AXRelation.fromJSON(rNode)); } Debug.Log("COPYINGING: " + poList.Count + ", " + relationList.Count); // really duplicate m.addAndRigUp(poList, relationList); } }
public void saveParametricObjectMetadata(AXParametricObject head_po) { //Debug.Log("SAVING head_po.author="+head_po.author); AX.SimpleJSON.JSONNode hn = AX.SimpleJSON.JSON.Parse(JSONSerializersAX.ParametricObjectAsJSON(head_po)); string filename = ArchimatixUtils.getAbsoluteLibraryPath(head_po.readIntoLibraryFromRelativeAXOBJPath); AX.SimpleJSON.JSONNode jn = AX.SimpleJSON.JSON.Parse(File.ReadAllText(filename)); jn["parametric_objects"][0] = hn; File.WriteAllText(filename, jn.ToString()); rebuildTagListsFromExistingPOs(); }
// It would be preferable to have these serializers in // the classes to keep there private parts private. // Unfortunately, since there are cases where the AssetDatabase needs to be accessed, // these must be in an editor script :-( #region Model JSON // MODEL JSON SERIALIZATION public static string Model_asJSON(AXModel m) { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"guid\":\"" + m.Guid + "\""); sb.Append(", \"name\":\"" + m.name + "\""); string thecomma = ""; // PARAMETRIC_OBJECTS sb.Append(", \"parametric_objects\": ["); // begin parametric_objects thecomma = ""; foreach (AXParametricObject po in m.parametricObjects) { sb.Append(thecomma + JSONSerializersAX.ParametricObjectAsJSON(po)); thecomma = ", "; } sb.Append("]"); // end parametric_objects // RELATIONS if (m.relations != null && m.relations.Count > 0) { sb.Append(", \"relations\": ["); // begin parametric_objects thecomma = ""; foreach (AXRelation r in m.relations) { sb.Append(thecomma + r.asJSON()); thecomma = ", "; } } sb.Append("]"); // end parametric_objects // end model sb.Append("}"); return(sb.ToString()); }
public static AXParametricObject pasteParametricObjectFromString(String json_string) { if (string.IsNullOrEmpty(json_string)) { json_string = EditorGUIUtility.systemCopyBuffer; } if (string.IsNullOrEmpty(json_string)) { return(null); } AXModel model = null; // 1. First choice is to use a selected model.... if (ArchimatixEngine.currentModel != null) { model = ArchimatixEngine.currentModel; } bool doInstantiation = true; if (model == null) { // ok, are there other models in the scene that the user doesn't realize arene't selected? AXModel[] axModels = GameObject.FindObjectsOfType(typeof(AXModel)) as AXModel[]; if (axModels != null) { if (axModels.Length == 1) { // Offer this model as a button, but also give opportunity to create a new one... int option = EditorUtility.DisplayDialogComplex("Instantiating Library Item", "There is no AX model currently selected", "Cancel", "Add to " + axModels[0], "Create a new Model"); switch (option) { // Save Scene case 0: doInstantiation = false; break; // Save and Quit. case 1: model = axModels[0]; break; case 2: GameObject axgo = AXEditorUtilities.createNewModel(); model = axgo.GetComponent <AXModel>(); doInstantiation = false; break; default: doInstantiation = false; break; } } else if (axModels.Length > 1) { // Throw up a dialog to see if use wants to go select one of the existing models or create a new one. if (EditorUtility.DisplayDialog("Instantiating Library Item", "There is no AX model currently selected", "Select and Existing Model", "Create a new Model")) { return(null); } } } } if (!doInstantiation) { return(null); } if (model == null) { // Well, then, we tried everything.... GameObject axgo = AXEditorUtilities.createNewModel(); model = axgo.GetComponent <AXModel>(); } List <AXParametricObject> poList = new List <AXParametricObject>(); List <AXRelation> relationList = new List <AXRelation>(); AX.SimpleJSON.JSONNode json = null; try { json = AX.SimpleJSON.JSON.Parse(json_string); } catch { return(null); } if (json == null) { return(null); } if (json["parametric_objects"] != null) { foreach (AX.SimpleJSON.JSONNode poNode in json["parametric_objects"].AsArray) { poList.Add(JSONSerializersAX.ParametricObjectFromJSON(poNode)); } } foreach (AX.SimpleJSON.JSONNode rNode in json["relations"].AsArray) { relationList.Add(AXRelation.fromJSON(rNode)); } //Debug.Log (rNode); // make sure there is a game object ready to instantiate this on if (model == null) { // check if the user really wants to create a new model... GameObject axgo = AXEditorUtilities.createNewModel(); model = axgo.GetComponent <AXModel>(); } Undo.RegisterCompleteObjectUndo(model, "Add Library Object"); AXParametricObject head_po = poList[0]; if (model.currentWorkingGroupPO != null && model.currentWorkingGroupPO.Name != head_po.Name) { head_po.grouperKey = model.currentWorkingGroupPO.Guid; } else { head_po.grouperKey = null; } // ADD AND RIGUP // !!! Actually add the library item (as a list of PO's and Relations) to the model model.addAndRigUp(poList, relationList); head_po.startStowInputs(); //if (poList[0].is2D() && ArchimatixEngine.workingAxis != (int) Axis.NONE) // poList[0].intValue ("Axis", ArchimatixEngine.workingAxis); model.setRenderMode(AXModel.RenderMode.GameObjects); model.generate(); ArchimatixEngine.currentModel = model; Selection.activeGameObject = model.gameObject; model.selectOnlyPO(head_po); head_po.focusMe = true; //AXParametricObject mostRecentPO = model.recentlySelectedPO; AXParametricObject mostRecentPO = model.recentlySelectedPO; //getPOSelectedBefore(head_po); //Debug.Log ("model.focusPointInGraphEditor="+model.focusPointInGraphEditor); float pos_x = (model.focusPointInGraphEditor.x) + 100; // + UnityEngine.Random.Range(-100, 300); float pos_y = (model.focusPointInGraphEditor.y - 250) + UnityEngine.Random.Range(-10, 0); if (mostRecentPO != null) { //pos_x = mostRecentPO.rect.x + 200; //pos_y = mostRecentPO.rect.y; } //Debug.Log (new Rect(pos_x, pos_y, 150, 100)); head_po.rect = new Rect(pos_x, pos_y, 150, 500); //Debug.Log ("OK "+po.Name+".focusMe = " + po.focusMe); // make sure it is in view in the editor window.... //model.startPanningToRect(head_po.rect); //model.cleanGraph(); model.autobuild(); head_po.generator.adjustWorldMatrices(); model.selectOnlyPO(head_po); // Paste inside grouper? if (model.currentWorkingGroupPO != null && model.currentWorkingGroupPO.Name != head_po.Name) { model.currentWorkingGroupPO.addGroupee(head_po); } if (model.currentWorkingGroupPO != null && model.currentWorkingGroupPO.Name == head_po.Name) { model.currentWorkingGroupPO = model.currentWorkingGroupPO.grouper; } //model.beginPanningToRect(head_po.rect); //AXNodeGraphEditorWindow.zoomToRectIfOpen(head_po.rect); //Debug.Log(" 1 model.selectedPOs.Count="+model.selectedPOs.Count + " " + model.selectedPOs[0].Name ); // EDITOR WINDOW ArchimatixEngine.repaintGraphEditorIfExistsAndOpen(); // SCENEVIEW if (SceneView.lastActiveSceneView != null) { //Debug.Log("REPAINT"); SceneView.lastActiveSceneView.Repaint(); } //Debug.Log("--> ArchimatixEngine.currentModel=" + ArchimatixEngine.currentModel); return(head_po); }
/* READ_LIBRARY FROM FILES * If the cache is there, use it. * Otherwise... * Parse all the files in the Library data folder * and, using the head object, extract meta data and build indices. */ public void readLibraryFromFiles() { //Debug.Log("read library"); //StopWatch sw = new StopWatch(); /* Maintain a live list of all the files found * as head POs * */ parametricObjects = new List <AXParametricObject>(); libraryItems = new List <LibraryItem>(); DirectoryInfo info = new DirectoryInfo(Application.dataPath); FileInfo[] files = info.GetFiles("*.axobj", SearchOption.AllDirectories); //Debug.Log ("LIBRARY READ " + files.Length); try { //foreach (var filepathname in files) for (int i = 0; i < files.Length; i++) { //Debug.Log ("file: " + files[i].ToString()); string filepathname = files[i].ToString(); AX.SimpleJSON.JSONNode jn = AX.SimpleJSON.JSON.Parse(File.ReadAllText(filepathname)); AXParametricObject head_po = null; if (jn ["parametric_objects"] != null) { head_po = JSONSerializersAX.ParametricObjectFromJSON(jn ["parametric_objects"] [0]); } else { head_po = JSONSerializersAX.ParametricObjectFromJSON(jn [0]); } //head_po.readIntoLibraryFromPathname = filepathname; head_po.readIntoLibraryFromRelativeAXOBJPath = ArchimatixUtils.getRelativeFilePath(filepathname); parametricObjects.Add(head_po); libraryItems.Add(new LibraryItem(head_po)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } rebuildTagListsFromExistingPOs(); cacheLibraryItems(); //Debug.Log ("LIBRARY READ IN "+sw.stop()+" MILLISECONDS " + parametricObjects.Count + " items."); }
// COPY OPERATION public static string allSelectedPOsAsJson(AXModel m) { Debug.Log("allSelectedPOsAsJson"); //List<string> registry = new List<string>(); // gather relations as you go... List <AXRelation> rels = new List <AXRelation>(); StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"model_guid\": \"" + m.Guid + "\""); sb.Append(", \"parametric_objects\": ["); // begin parametric_objects for (int i = 0; i < m.selectedPOs.Count; i++) { AXParametricObject po = m.selectedPOs[i]; // if this po is an instance, get its source as json to store in the clipboard if (po.Type == "Instance") { AXParametricObject src_po = po.getParameter("Input Mesh").DependsOn.Parent; if (!m.isSelected(src_po)) { po = src_po; } } sb.Append(((i > 0)?",":"") + JSONSerializersAX.ParametricObjectAsJSON(po)); foreach (AXParameter p in po.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } // SUB_NODES if (po.inputsStowed) { // copy them too! po.gatherSubnodes(); foreach (AXParametricObject spo in po.subnodes) { sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo)); // gather relations foreach (AXParameter p in spo.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } } } } sb.Append("]"); // end parametric_objects // add relations to json string thecomma; // RELATIONS if (m.relations != null && m.relations.Count > 0) { sb.Append(", \"relations\": ["); // begin parametric_objects thecomma = ""; foreach (AXRelation rr in m.relations) { sb.Append(thecomma + rr.asJSON()); thecomma = ", "; } } sb.Append("]"); /* to retrieve later... * if (jn["relations"] != null) * { * foreach(AX.SimpleJSON.JSONNode jn_r in jn["relations"].AsArray) * model.addRelationJSON(jn_r); * } */ sb.Append("}"); // end collection return(sb.ToString()); }
public static AXParametricObject ParametricObjectFromJSON(AX.SimpleJSON.JSONNode jn) { AXParametricObject po = new AXParametricObject(jn["type"], jn["name"]); po.Guid = jn["guid"].Value; po.basedOnAssetWithGUID = jn["basedOnAssetWithGUID"].Value; po.description = jn["description"].Value; po.author = jn["author"].Value; po.tags = jn["tags"].Value; po.documentationURL = jn["documentationURL"].Value; if (jn["includeInSidebarMenu"] != null) { po.includeInSidebarMenu = jn["includeInSidebarMenu"].AsBool; } else { po.includeInSidebarMenu = true; } po.isInitialized = jn["isInitialized"].AsBool; po.grouperKey = jn["grouperKey"].Value; if (jn["sortval"] != null) { po.sortval = jn["sortval"].AsFloat; } po.curve = AXJson.CurveFromJSON(jn["curve"]); po.rect = AXJson.RectFromJSON(jn["rect"]); po.bounds = AXJson.BoundsFromJSON(jn["bounds"]); po.transMatrix = AXJson.Matrix4x4FromJSON(jn["transMatrix"]); po.combineMeshes = jn["combineMeshes"].AsBool; po.isRigidbody = jn["isRigidbody"].AsBool; po.colliderType = (AXGeometryTools.ColliderType)jn["colliderType"].AsInt; po.axStaticEditorFlags = (AXStaticEditorFlags)jn["axStaticEditorFlags"].AsInt; //Debug.Log(po.Name + " " + po.grouperKey); // material // look to see if there is a material matching this name.... #if UNITY_EDITOR if (jn["material_assetGUID"] != null) { string path = AssetDatabase.GUIDToAssetPath(jn["material_assetGUID"].Value); //Debug.Log("AssetDatabase.GUIDToAssetPath("+jn["material_assetGUID"].Value+") has path: " + path); // REDO if (path != null) { //Debug.Log(jn["Name"] + ": path="+path); Material matter = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material)); //Debug.Log("..."+matter); //po.axMat.mat = (Material) AssetDatabase.LoadAssetAtPath(path, typeof(Material)) ; if (po.axMat == null) { po.axMat = new AXMaterial(); } po.axMat.mat = matter; } } #endif // code if (jn["code"] != null) { po.code = jn["code"].Value.Replace(";", "\n"); } // parameters if (jn["parameters"] != null) { po.parameters = new List <AXParameter>(); foreach (AX.SimpleJSON.JSONNode jn_p in jn["parameters"].AsArray) { po.addParameter(AXParameter.fromJSON(jn_p)); } } // shapes if (jn["shapes"] != null) { po.shapes = new List <AXShape>(); foreach (AX.SimpleJSON.JSONNode jn_p in jn["shapes"].AsArray) { po.shapes.Add(AXShape.fromJSON(po, jn_p)); } } // handles if (jn["handles"] != null) { po.handles = new List <AXHandle>(); foreach (AX.SimpleJSON.JSONNode jn_h in jn["handles"].AsArray) { po.handles.Add(AXHandle.fromJSON(jn_h)); } } // cameraSettings if (jn["cameraSettings"] != null) { po.cameraSettings = JSONSerializersAX.AXCameraFromJSON(jn["cameraSettings"]); po.cameraSettings.setPosition(); } // make the generator for this po po.instantiateGenerator(); return(po); }
// JSON SERIALIZATION public static string ParametricObjectAsJSON(AXParametricObject po) { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append(" \"basedOnAssetWithGUID\":\"" + po.basedOnAssetWithGUID + "\""); sb.Append(",\"author\":\"" + po.author + "\""); sb.Append(",\"tags\":\"" + po.tags + "\""); sb.Append(",\"guid\":\"" + po.Guid + "\""); sb.Append(", \"name\":\"" + po.Name + "\""); sb.Append(", \"description\":\"" + po.description + "\""); sb.Append(", \"type\":\"" + po.Type + "\""); sb.Append(", \"isInitialized\":\"" + po.isInitialized + "\""); sb.Append(", \"documentationURL\":\"" + po.documentationURL + "\""); sb.Append(", \"includeInSidebarMenu\":\"" + po.includeInSidebarMenu + "\""); sb.Append(",\"grouperKey\":\"" + po.grouperKey + "\""); sb.Append(", \"rect\":" + AXJson.RectToJSON(po.rect)); sb.Append(", \"bounds\":" + AXJson.BoundsToJSON(po.bounds)); sb.Append(", \"transMatrix\":" + AXJson.Matrix4x4ToJSON(po.transMatrix)); if (po.curve != null && po.curve.Count > 0) { sb.Append(", \"curve\":" + AXJson.CurveToJson(po.curve)); } sb.Append(", \"sortval\":\"" + po.sortval + "\""); sb.Append(", \"combineMeshes\":\"" + po.combineMeshes + "\""); sb.Append(", \"isRigidbody\":\"" + po.isRigidbody + "\""); sb.Append(", \"colliderType\":\"" + po.colliderType.GetHashCode() + "\""); sb.Append(", \"axStaticEditorFlags\":\"" + po.axStaticEditorFlags.GetHashCode() + "\""); // material //Debug.Log ("ENCODE MATERIAL::::"); #if UNITY_EDITOR if (po.axMat != null && po.axMat.mat != null) { string matPath = AssetDatabase.GetAssetOrScenePath(po.axMat.mat); if (!String.IsNullOrEmpty(matPath)) { sb.Append(", \"material_assetGUID\":\"" + AssetDatabase.AssetPathToGUID(matPath) + "\""); } } #endif // code if (po.code != null) { sb.Append(", \"code\":\"" + po.code.Replace("\n", ";") + "\""); } // parameters sb.Append(", \"parameters\": ["); // begin parameters string the_comma = ""; foreach (AXParameter p in po.parameters) { sb.Append(the_comma + p.asJSON()); the_comma = ", "; } sb.Append("]"); // end parameters // shapes if (po.shapes != null) { sb.Append(", \"shapes\": ["); // begin parameters the_comma = ""; foreach (AXShape shp in po.shapes) { sb.Append(the_comma + shp.asJSON()); the_comma = ", "; } sb.Append("]"); // end parameters } // handles if (po.handles != null && po.handles.Count > 0) { sb.Append(", \"handles\": ["); // begin parameters the_comma = ""; foreach (AXHandle h in po.handles) { sb.Append(the_comma + h.asJSON()); the_comma = ", "; } sb.Append("]"); } // end parameters // cameraSettings if (po.cameraSettings != null) { sb.Append(",\"cameraSettings\":" + JSONSerializersAX.AXCameraAsJSON(po.cameraSettings)); } // finish sb.Append("}"); // end parametri_object //Debug.Log (sb.ToString()); return(sb.ToString()); }
public static string allSelectedPOsAndInputsAsJson(AXModel m) { Debug.Log("allSelectedPOsAndInputsAsJson"); /* We don't know if some of the supart PO's are already selected * so maintain a registry and exclude double entries into the json * */ List <string> registry = new List <string>(); // gather relations as you go... List <AXRelation> rels = new List <AXRelation>(); StringBuilder sb = new StringBuilder(); sb.Append("{"); Debug.Log("ASIGN:: model_guid=" + m.Guid); sb.Append("\"model_guid\": \"" + m.Guid + "\""); // ALL PARAMETRIC_OBJECTS IN THIS TREE sb.Append(", \"parametric_objects\": ["); // begin parametric_objects for (int i = 0; i < m.selectedPOs.Count; i++) { AXParametricObject po = m.selectedPOs[i]; if (!registry.Contains(po.Guid)) { sb.Append(((i > 0)?",":"") + JSONSerializersAX.ParametricObjectAsJSON(po)); registry.Add(po.Guid); } foreach (AXParameter p in po.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } // SUB_NODES po.gatherSubnodes(); foreach (AXParametricObject spo in po.subnodes) { if (registry.Contains(spo.Guid)) // don't copy subparts that were selected and already added { continue; } sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo)); registry.Add(spo.Guid); foreach (AXParameter p in spo.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } } } sb.Append("]"); // end parametric_objects // add relations to json string thecomma; // RELATIONS if (m.relations != null && m.relations.Count > 0) { sb.Append(", \"relations\": ["); // begin parametric_objects thecomma = ""; foreach (AXRelation rr in m.relations) { sb.Append(thecomma + rr.asJSON()); thecomma = ", "; } } sb.Append("]"); sb.Append("}"); // end collection return(sb.ToString()); }
// DO THE ACTUAL SAVE public static void saveParametricObject(AXParametricObject po, bool withInputSubparts, string filepathname) { //Debug.Log(filepathname); //EditorUtility.DisplayDialog("Archimatix Library", "Saving to Library: This may take a few moments.", "Ok"); po.readIntoLibraryFromRelativeAXOBJPath = ArchimatixUtils.getRelativeFilePath(filepathname); Library.recentSaveFolder = System.IO.Path.GetDirectoryName(filepathname); // If this head po has a grouperKey, lose it! // This is because when it is later instantiated, // that grouper may not exist or may not be the desired place for this. po.grouperKey = null; // gather relations as you go... List <AXRelation> rels = new List <AXRelation>(); // BUILD JSON STRING StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"parametric_objects\":["); sb.Append(JSONSerializersAX.ParametricObjectAsJSON(po)); // gather rels foreach (AXParameter p in po.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } // SUB NODES if (withInputSubparts) { // GATHER SUB_NODES //Debug.Log("Start gather"); po.gatherSubnodes(); //Debug.Log("End gather"); foreach (AXParametricObject spo in po.subnodes) { //Debug.Log("-- " + spo.Name); sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo)); // gather rels foreach (AXParameter p in spo.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } } } sb.Append("]"); // add relations to json string thecomma; // RELATIONS if (rels != null && rels.Count > 0) { sb.Append(", \"relations\": ["); // begin parametric_objects thecomma = ""; foreach (AXRelation rr in rels) { sb.Append(thecomma + rr.asJSON()); thecomma = ", "; } sb.Append("]"); } sb.Append("}"); // *** SAVE AS ASSET *** // Since we have added the newItem to the live library, // generating this new file will not force a recreation of the library from the raw JSON file. File.WriteAllText(filepathname, sb.ToString()); // THUMBNAIL TO PNG if (po.is2D()) { Library.cache2DThumbnail(po); } else { Thumbnail.BeginRender(); Thumbnail.render(po, true); Thumbnail.EndRender(); } //string thumb_filename = System.IO.Path.ChangeExtension(filepathname, ".png"); string filename = System.IO.Path.GetFileNameWithoutExtension(filepathname); string prefix = (po.is2D()) ? "zz-AX-2DLib-" : "zz-AX-3DLib-"; string thumb_filename = System.IO.Path.ChangeExtension(filepathname, ".jpg"); thumb_filename = thumb_filename.Replace(filename, prefix + filename); byte[] bytes = po.thumbnail.EncodeToJPG(); //File.WriteAllBytes(libraryFolder + "data/"+ po.Name + ".png", bytes); File.WriteAllBytes(thumb_filename, bytes); //AssetDatabase.Refresh(); string thumbnailRelativePath = ArchimatixUtils.getRelativeFilePath(thumb_filename); //Debug.Log(path); AssetDatabase.ImportAsset(thumbnailRelativePath); TextureImporter importer = AssetImporter.GetAtPath(thumbnailRelativePath) as TextureImporter; if (importer != null) { importer.textureType = TextureImporterType.GUI; importer.maxTextureSize = 256; #if UNITY_5_5_OR_NEWER importer.textureCompression = TextureImporterCompression.Uncompressed; #else importer.textureFormat = TextureImporterFormat.AutomaticTruecolor; #endif } AssetDatabase.WriteImportSettingsIfDirty(thumbnailRelativePath); // Save the recent path to prefs EditorPrefs.SetString("LastLibraryPath", System.IO.Path.GetDirectoryName(filepathname)); EditorUtility.DisplayDialog("Archimatix Library", "ParametricObject saved to " + System.IO.Path.GetDirectoryName(thumbnailRelativePath) + " as " + filename, "Great, thanks!"); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(thumbnailRelativePath, typeof(Texture2D)); LibraryItem newItem = new LibraryItem(po); newItem.icon = tex; ArchimatixEngine.library.addLibraryItemToList(newItem); ArchimatixEngine.library.sortLibraryItems(); ArchimatixEngine.saveLibrary(); AssetDatabase.Refresh(); //Debug.Log("yo 2"); //ArchimatixEngine.library.readLibraryFromFiles(); }
/* * public static void pasteFromJSONstringToSelectedModel(string json_string) * { * if (ArchimatixUtils.lastPastedPO != null) * Debug.Log ("Archimatix.lastPastedPO="+ArchimatixUtils.lastPastedPO.Name); * * //Debug.Log ("PASTE FROM JSON**********************************************************************"); * * List<AXParametricObject> poList = new List<AXParametricObject>(); * List<AXRelation> relationList = new List<AXRelation>(); * * AX.SimpleJSON.JSONNode json = AX.SimpleJSON.JSON.Parse(json_string); * * if (json == null || json ["parametric_objects"] == null) * return; * * foreach(AX.SimpleJSON.JSONNode poNode in json["parametric_objects"].AsArray) * poList.Add (JSONSerializersAX.ParametricObjectFromJSON(poNode)); * * foreach(AX.SimpleJSON.JSONNode rNode in json["relations"].AsArray) * { * //Debug.Log (rNode); * relationList.Add (AXRelation.fromJSON(rNode)); * } * //Debug.Log("relationList: " + relationList.Count); * * * * AXModel model = null; * * // make sure there is a game object * if(ArchimatixEngine.currentModel != null) * model = ArchimatixEngine.currentModel; * * if (model == null) * { * GameObject axgo = AXEditorUtilities.createNewModel(); * model = axgo.GetComponent<AXModel>(); * * } * * Undo.RegisterCompleteObjectUndo (model, "Paste Object"); * * * // !!! Actually add the library item (as a list of PO's and Relations) to the model * //Debug.Log ("ADD AND RIGUP"); * * model.addAndRigUp(poList, relationList); * * poList[0].startStowInputs(); * * * * //model.generate(); * * * //Selection.activeGameObject = model.gameObject; * ArchimatixEngine.currentModel = model; * * model.selectOnlyPO(poList[0]); * * //model.startPanningToRect(poList[0].rect); * * AXParametricObject lastPO = ArchimatixUtils.lastPastedPO; * if (lastPO == null) * lastPO = ArchimatixUtils.lastCopiedPO; * * int pos_x = Screen.width/2-50; * int pos_y = Screen.height/2-100; * * if(lastPO != null) * { * pos_x = (int) lastPO.rect.x + 50; * pos_y = (int) lastPO.rect.y + 50; * } * poList[0].rect = new Rect(pos_x, pos_y, 150, 100); * * * // if last po has only one ouput, connect to the same consumer. * AXParameter op = lastPO.getPreferredOutputSplineParameter(); * //if (op != null && op.Dependents != null && op.Dependents.COunt > 0) * //Debug.Log (op.Name + " .................... "+ op.Dependents[0].Parent.Name + " ::: " + op.Dependents[0].Parent.generator.isOfType("ShapeMerger")); * * if (op != null && op.Dependents != null && op.Dependents.Count == 1 && op.Dependents[0].Parent.generator.isOfType("ShapeMerger")) * op.Dependents[0].Parent.generator.getInputShape().addInput().makeDependentOn(poList[0].getPreferredOutputSplineParameter()); * * * ArchimatixUtils.lastPastedPO = poList[0]; * * model.setAllAltered(); * * model.autobuild(); * poList[0].generator.adjustWorldMatrices(); * * ArchimatixEngine.repaintGraphEditorIfExistsAndOpen(); * * // SCENEVIEW * if (SceneView.lastActiveSceneView != null) * SceneView.lastActiveSceneView.Repaint(); * * * * } * * * * * * public static string poWithSubNodes_2_JSON_OLD(AXParametricObject po, bool withInputSubparts) * { * ArchimatixUtils.lastCopiedPO = po; * ArchimatixUtils.lastPastedPO = null; * * // gather relations as you go... * List<AXRelation> rels = new List<AXRelation>(); * * // BUILD JSON STRING * StringBuilder sb = new StringBuilder(); * sb.Append("{"); * sb.Append("\"parametric_objects\":["); * sb.Append(JSONSerializersAX.ParametricObjectAsJSON(po)); * * // gather rels * foreach(AXParameter p in po.parameters) * { * foreach (AXRelation rr in p.relations) * if (! rels.Contains(rr)) * rels.Add(rr); * } * * * * if (withInputSubparts) * { * po.gatherSubnodes(); * foreach (AXParametricObject spo in po.subnodes) * { * sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo)); * * // gather rels * foreach(AXParameter p in spo.parameters) * { * foreach (AXRelation rr in p.relations) * if (! rels.Contains(rr)) * rels.Add(rr); * } * * } * } * sb.Append("]"); * * // add relations to json * string thecomma; * // RELATIONS * if (rels != null && rels.Count > 0) * { * sb.Append(", \"relations\": ["); // begin parametric_objects * * thecomma = ""; * foreach(AXRelation rr in rels) * { * sb.Append(thecomma + rr.asJSON()); * thecomma = ", "; * } * sb.Append("]"); * } * * * * sb.Append("}"); * * return sb.ToString(); * * } */ public static string poWithSubNodes_2_JSON(AXParametricObject po, bool withInputSubparts) { string origGrouperKey = po.grouperKey; po.grouperKey = null; // gather relations as you go... List <AXRelation> rels = new List <AXRelation>(); // BUILD JSON STRING StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"parametric_objects\":["); sb.Append(JSONSerializersAX.ParametricObjectAsJSON(po)); // gather rels foreach (AXParameter p in po.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } // SUB NODES if (withInputSubparts) { // GATHER SUB_NODES //Debug.Log("Start gather"); po.gatherSubnodes(); //Debug.Log("End gather"); foreach (AXParametricObject spo in po.subnodes) { //Debug.Log("-- " + spo.Name); sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo)); // gather rels foreach (AXParameter p in spo.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } } } sb.Append("]"); // add relations to json string thecomma; // RELATIONS if (rels != null && rels.Count > 0) { sb.Append(", \"relations\": ["); // begin parametric_objects thecomma = ""; foreach (AXRelation rr in rels) { sb.Append(thecomma + rr.asJSON()); thecomma = ", "; } sb.Append("]"); } sb.Append("}"); // SAVE AS ASSET po.grouperKey = origGrouperKey; return(sb.ToString()); }