/// <summary> /// Initializes the paths of the python scripts. /// </summary> public void Initialize() { UpdaterUtility.ProjectFolder = Application.dataPath; UpdaterUtility.PathToDownloadScript = UpdaterUtility.ProjectFolder + @"/ExternalTools/ModelDownloader.py"; UpdaterUtility.PathToScanScript = UpdaterUtility.ProjectFolder + @"/ExternalTools/ModelScanner.py"; UpdaterUtility.PathToSDFreader = UpdaterUtility.ProjectFolder + @"/ExternalTools/SDF_reader.py"; UpdaterUtility.showWarnings(); }
/// <summary> /// Creates prefabs for every model which were downloaded. /// </summary> public void CreatePrefab() { foreach (string modelName in m_ModelNames) { string pathToSDFFile = UpdaterUtility.ProjectFolder + @"/temp" + modelName + "SDFs.txt"; if (!File.Exists(pathToSDFFile)) { Debug.LogWarning("Scan file not found! Check whether it exists or if python script is working!"); return; } // get file content of format title:url string[] sdfContent = File.ReadAllLines(pathToSDFFile); File.Delete(pathToSDFFile); List <string[]> linkList = new List <string[]>(); foreach (string line in sdfContent) { string[] SDFline = line.Split(';'); linkList.Add(SDFline); } GameObject modelParent = null; string absoluteModelPath = UpdaterUtility.ProjectFolder + @"/SimulationModels/" + modelName + "/OriginModels"; for (int i = 0; i < linkList.Count; i++) //foreach (string[] line in linkList) { if (linkList[i][0] == "model_name") { //Create GameObject where everything will be attached modelParent = new GameObject(linkList[i][1]); linkList.Remove(linkList[i]); //continue; } } //List for all downloaded visuals List <string> visMeshList = new List <string>(); if (absoluteModelPath != "") { visMeshList = UpdaterUtility.getFilePathsFBX(absoluteModelPath + @"/visual"); } //List for all downloaded colliders List <string> colMeshList = new List <string>(); if (absoluteModelPath != "") { colMeshList = UpdaterUtility.getFilePathsFBX(absoluteModelPath + @"/collision"); } foreach (string name in visMeshList) { string[] currentLine = null; foreach (string[] line in linkList) { for (int j = 0; j < line.Length; j++) { string name1 = name.Replace(".fbx", ""); Debug.Log(name1 + " : " + name); if (line[j].Contains(name1)) { currentLine = line; Debug.Log(currentLine.Length); } } } string[] pose = null; string[] VIS_scale = null; //string[] COL_scale = null; for (int i = 0; i < currentLine.Length; i++) { if (currentLine[i].Contains("link_pose")) { pose = currentLine[i + 1].Split(' '); } if (currentLine[i].Contains("VIS_mesh_scale")) { VIS_scale = currentLine[i + 1].Split(' '); } //if (currentLine[i].Contains("COL_mesh_scale")) //{ // COL_scale = currentLine[i + 1].Split(' '); //} } GameObject meshPrefab = null; string relativeModelPath = "Assets/SimulationModels/" + modelName + "/OriginModels/"; // import Mesh meshPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(relativeModelPath + @"visual/" + name, typeof(UnityEngine.Object)); //StartCoroutine(importModelCoroutine(path, (result) => { meshPrefab = result; })); if (meshPrefab == null) { Debug.Log("Could not import model!"); continue; } GameObject meshCopy = Instantiate(meshPrefab); Debug.Log(pose.Length); meshCopy.tag = "RoboyPart"; meshCopy.transform.position = GazeboUtility.GazeboPositionToUnity(new Vector3(float.Parse(pose[0], CultureInfo.InvariantCulture.NumberFormat), float.Parse(pose[1], CultureInfo.InvariantCulture.NumberFormat), float.Parse(pose[2], CultureInfo.InvariantCulture.NumberFormat))); meshCopy.transform.eulerAngles = GazeboUtility.GazeboPositionToUnity(new Vector3(Mathf.Rad2Deg * float.Parse(pose[3], CultureInfo.InvariantCulture.NumberFormat), Mathf.Rad2Deg * float.Parse(pose[4], CultureInfo.InvariantCulture.NumberFormat), Mathf.Rad2Deg * float.Parse(pose[5], CultureInfo.InvariantCulture.NumberFormat))); if (VIS_scale != null) { meshCopy.transform.localScale = GazeboUtility.GazeboPositionToUnity(new Vector3(100 * float.Parse(VIS_scale[0], CultureInfo.InvariantCulture.NumberFormat), 100 * float.Parse(VIS_scale[1], CultureInfo.InvariantCulture.NumberFormat), 100 * float.Parse(VIS_scale[2], CultureInfo.InvariantCulture.NumberFormat))); } UpdaterUtility.attachCollider(meshCopy, relativeModelPath, name); var regex1 = new Regex(Regex.Escape("(Clone)")); meshCopy.name = regex1.Replace(meshCopy.name, "", 1); var regex2 = new Regex(Regex.Escape("VIS_")); meshCopy.name = regex2.Replace(meshCopy.name, "", 1); SelectableObject selectableObjectComponent = meshCopy.AddComponent <SelectableObject>(); selectableObjectComponent.TargetedMaterial = Resources.Load("RoboyMaterials/TargetedMaterial") as Material; selectableObjectComponent.SelectedMaterial = Resources.Load("RoboyMaterials/SelectedMaterial") as Material; meshCopy.AddComponent <RoboyPart>(); // Attach Model with mesh to parent GO meshCopy.transform.parent = modelParent.transform; } modelParent.tag = "Roboy"; //Create Prefab of existing GO UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/SimulationModels/" + modelName + "/" + modelName + ".prefab"); PrefabUtility.ReplacePrefab(modelParent, prefab, ReplacePrefabOptions.ConnectToPrefab); //Destroy GO after prefab is created DestroyImmediate(modelParent); } m_ModelNames.Clear(); }
/// <summary> /// Creates a GameObject for each .world file and attaches related models, with their given pose. /// </summary> public void CreateWorld() { //stores every individual model in a list, to later create prefabs List <string> modelNames = new List <string>(); foreach (ModelTransformation model in modellist) { if (!modelNames.Contains(model.name)) { modelNames.Add(model.name); } } //Create Prefab for individual models foreach (string modelName in modelNames) { string absoluteModelPath = UpdaterUtility.ProjectFolder + @"/SimulationWorlds/Models/" + modelName + @"/meshes"; //Create GameObject where everything will be attached GameObject modelParent = new GameObject(modelName); //List for all downloaded visuals List <string> MeshList = new List <string>(); if (absoluteModelPath != "") { MeshList = UpdaterUtility.getFilePathsFBX(absoluteModelPath); } foreach (string name in MeshList) { GameObject meshPrefab = null; string relativeModelPath = @"Assets/SimulationWorlds/Models/" + modelName + @"/meshes/"; //Debug.Log(relativeModelPath + name); // import Mesh (from visual if we changed folder structure of roboy_worlds/models meshPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(relativeModelPath + name, typeof(Object)); if (meshPrefab == null) { Debug.Log("Could not import model!"); continue; } GameObject meshCopy = Instantiate(meshPrefab); // set scale of childs to (1,1,1) in case the .stl file has childs with different scale foreach (Transform child in meshCopy.transform) { child.localScale = Vector3.one; } meshCopy.transform.localScale = Vector3.one; //foreach (ModelTransformation model in modellist) //{ // string name1 = name.Replace(".fbx", ""); // if (model.link.VIS_meshName.Contains(name1)) // { // if (model.link.position != null) // { // meshCopy.transform.position = model.link.position; // } // if (model.link.rotation != null) // { // meshCopy.transform.eulerAngles = model.link.rotation; // } // if (model.link.VIS_scale != null) // { // meshCopy.transform.localScale = model.link.VIS_scale; // } // } //} //meshCopy.tag = "WorldPart"; // !!!! the following part could be shortened if we change folder structure of roboy_worlds/models //UpdaterUtility.attachCollider(meshCopy, relativeModelPath, name); // get the object which serves as mesh collider GameObject colliderPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(relativeModelPath + name, typeof(Object)); if (colliderPrefab == null) { Debug.Log("Did not find a collider object for mesh: " + name); return; } // set scale of childs to (1,1,1) in case the .stl file has childs with different scale foreach (Transform child in colliderPrefab.transform) { child.localScale = Vector3.one; } colliderPrefab.transform.localScale = Vector3.one; // go through each mesh filter and add all mesh references as mesh colliders to the gameObject List <MeshFilter> collRenderers = colliderPrefab.GetComponentsInChildren <MeshFilter>().ToList(); foreach (MeshFilter collRenderer in collRenderers) { MeshCollider meshCollider = meshCopy.AddComponent <MeshCollider>(); meshCollider.sharedMesh = collRenderer.sharedMesh; } var regex1 = new Regex(Regex.Escape("(Clone)")); meshCopy.name = regex1.Replace(meshCopy.name, "", 1); //var regex2 = new Regex(Regex.Escape("VIS_")); //meshCopy.name = regex2.Replace(meshCopy.name, "", 1); // Attach Model with mesh to parent GO meshCopy.transform.parent = modelParent.transform; } //Create Prefab of existing GO Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/SimulationWorlds/Models/" + modelName + "/" + modelName + ".prefab"); GameObject test1 = PrefabUtility.ReplacePrefab(modelParent, prefab, ReplacePrefabOptions.ConnectToPrefab); //add to dictionary to instanciate later m_PrefabDictionary.Add(modelName, test1); //Destroy GO after prefab is created DestroyImmediate(modelParent); } List <KeyValuePair <string, bool> > tempURLList = WorldChoiceDictionary.Where(entry => entry.Value == true).ToList(); foreach (var urlEntry in tempURLList) { //this is the parent object of the entire world GameObject worldParent = new GameObject(urlEntry.Key); //foreach model in the .world files foreach (ModelTransformation model in modellist) { string absoluteWorldPath = UpdaterUtility.ProjectFolder + @"/SimulationWorlds/" + model.name; //if model is part of the world urlEntry.Key if (model.worldname == urlEntry.Key) { GameObject model1 = Instantiate(m_PrefabDictionary[model.name]); var regex1 = new Regex(Regex.Escape("(Clone)")); model1.name = regex1.Replace(model1.name, "", 1); //set transform with the values gathered in .world file model1.transform.position = model.position; model1.transform.eulerAngles = model.rotation; //model1.transform.localScale = model.scale; model1.transform.parent = worldParent.transform; } } // save whole world as prefab Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/SimulationWorlds/" + urlEntry.Key + "/" + urlEntry.Key + ".prefab"); GameObject test1 = PrefabUtility.ReplacePrefab(worldParent, prefab, ReplacePrefabOptions.ConnectToPrefab); //Destroy GO after prefab is created DestroyImmediate(worldParent); } }