public void PrefabUtility_GetCorrespondingObjectFromSource_AssetAsRoot() { RuntimeUrdf.runtimeModeEnabled = false; var instantiated = PrefabUtility.InstantiatePrefab(testObject); Assert.AreEqual(testObject, RuntimeUrdf.PrefabUtility_GetCorrespondingObjectFromSource(instantiated)); }
public void AssetDatabase_GUIDToAssetPath_AssetAsRoot() { RuntimeUrdf.runtimeModeEnabled = false; string createdGUID = RuntimeUrdf.AssetDatabase_CreateFolder(createAssetPath, "RuntimeUrdf"); Assert.AreEqual(createFolderPath, RuntimeUrdf.AssetDatabase_GUIDToAssetPath(AssetDatabase.AssetPathToGUID(createFolderPath))); }
private static void CopyDaeTextureToExportDestination(string prefabPath, string newFolderLocation) { //Get material from Collada prefab Material material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(prefabPath); if (material == null || material.mainTexture == null) { return; } //Get relative subfolder where texture is, compared to the DAE file. string commonFolder = Path.GetDirectoryName(prefabPath).SetSeparatorChar(); string texturePath = RuntimeUrdf.AssetDatabase_GetAssetPath(material.mainTexture).SetSeparatorChar(); string relativeLocation = ""; if (texturePath.Contains(commonFolder)) { relativeLocation = texturePath.Substring(commonFolder.Length + 1); } string newTexturePath = Path.Combine(newFolderLocation, relativeLocation); Directory.CreateDirectory(Path.GetDirectoryName(newTexturePath)); CopyFileToNewLocation(UrdfAssetPathHandler.GetFullAssetPath(texturePath), newTexturePath); }
public static void CreateMatchingMeshCollision(Transform parent, Transform visualToCopy) { if (visualToCopy.childCount == 0) { return; } GameObject objectToCopy = visualToCopy.GetChild(0).gameObject; GameObject prefabObject = (GameObject)RuntimeUrdf.PrefabUtility_GetCorrespondingObjectFromSource(objectToCopy); GameObject collisionObject; if (prefabObject != null) { collisionObject = (GameObject)RuntimeUrdf.PrefabUtility_InstantiatePrefab(prefabObject); } else { collisionObject = Object.Instantiate(objectToCopy); } collisionObject.name = objectToCopy.name; ConvertMeshToColliders(collisionObject); collisionObject.transform.SetParentAndAlign(parent); }
public static void SetUrdfMaterial(GameObject gameObject, Link.Visual.Material urdfMaterial) { if (urdfMaterial != null) { var material = CreateMaterial(urdfMaterial); SetMaterial(gameObject, material); } else { //If the URDF material is not defined, and the renderer is missing //a material, assign the default material. Renderer renderer = gameObject.GetComponentInChildren <Renderer>(); if (renderer != null && renderer.sharedMaterial == null) { Material material = defaultMaterial; #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName)); } #endif SetMaterial(gameObject, material); } } }
private static Material CreateMaterial(this Link.Visual.Material urdfMaterial) { if (urdfMaterial.name == "") { urdfMaterial.name = GenerateMaterialName(urdfMaterial); } var material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(urdfMaterial.name)); if (material != null) { //material already exists return(material); } material = MaterialExtensions.CreateBasicMaterial(); if (urdfMaterial.color != null) { MaterialExtensions.SetMaterialColor(material, CreateColor(urdfMaterial.color)); } else if (urdfMaterial.texture != null) { material.mainTexture = LoadTexture(urdfMaterial.texture.filename); } RuntimeUrdf.AssetDatabase_CreateAsset(material, UrdfAssetPathHandler.GetMaterialAssetPath(urdfMaterial.name)); return(material); }
public void AssetDatabase_LoadAssetAtPath_Object() { RuntimeUrdf.SetRuntimeMode(false); string path = "Packages/com.unity.robotics.urdf-importer/Tests/Runtime/RuntimeImport/RuntimeUrdfTests.cs"; Assert.IsNotNull(RuntimeUrdf.AssetDatabase_LoadAssetAtPath(path, typeof(UnityEngine.Object))); }
static Material defaultMaterial = null; // used RuntimeURDF private static void CreateDefaultMaterial() { Material material = defaultMaterial; #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName)); } #endif if (material != null) { return; } material = MaterialExtensions.CreateBasicMaterial(); MaterialExtensions.SetMaterialColor(material, new Color(0.33f, 0.33f, 0.33f, 0.0f)); // just keep it in memory while the app is running. defaultMaterial = material; #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { // create the material to be reused RuntimeUrdf.AssetDatabase_CreateAsset(material, UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName)); } #endif }
// Initializes import pipeline and reads the urdf file. private static ImportPipelineData ImportPipelineInit(string filename, ImportSettings settings, bool loadStatus, bool forceRuntimeMode) { ImportPipelineData im = new ImportPipelineData(); im.settings = settings; im.loadStatus = loadStatus; im.wasRuntimeMode = RuntimeUrdf.IsRuntimeMode(); im.forceRuntimeMode = forceRuntimeMode; if (forceRuntimeMode) { RuntimeUrdf.SetRuntimeMode(true); } im.robot = new Robot(filename); if (!UrdfAssetPathHandler.IsValidAssetPath(im.robot.filename)) { Debug.LogError("URDF file and ressources must be placed in Assets Folder:\n" + Application.dataPath); if (forceRuntimeMode) { // set runtime mode back to what it was RuntimeUrdf.SetRuntimeMode(im.wasRuntimeMode); } return(null); } return(im); }
public void Create_CylinderMesh_AssetCreated() { // Force runtime mode to set testing package root RuntimeUrdf.runtimeModeEnabled = true; UrdfAssetPathHandler.SetPackageRoot("Assets/Tests/Runtime/GeometryTests"); RuntimeUrdf.runtimeModeEnabled = false; var parent = new GameObject("Parent").transform; UrdfGeometryCollision.Create(parent, GeometryTypes.Cylinder); // Verify Cylinder created in Hierarchy var createdCylinder = parent.Find("Cylinder").gameObject; Assert.IsTrue(createdCylinder.activeInHierarchy); Assert.IsNotNull(createdCylinder.GetComponent <MeshCollider>()); // Check for standard values on collider Assert.AreEqual(1864, createdCylinder.GetComponent <MeshCollider>().sharedMesh.vertexCount); Assert.IsTrue(Vector3.Distance(Vector3.zero, createdCylinder.GetComponent <MeshCollider>().sharedMesh.bounds.center) < centerDelta); Assert.IsTrue(Vector3.Distance(new Vector3(0.5f, 1f, 0.5f), createdCylinder.GetComponent <MeshCollider>().sharedMesh.bounds.extents) < scaleDelta); // Verify Cylinder created in Assets Assert.IsNotNull(RuntimeUrdf.AssetDatabase_FindAssets("Cylinder t:mesh", new string[] { "Assets/Tests/Runtime/GeometryTests" })); AssetDatabase.DeleteAsset("Assets/Tests/Runtime/GeometryTests/Cylinder.asset"); Object.DestroyImmediate(parent.gameObject); }
private static GameObject CreateStlGameObject(string meshAssetPath, Material material) { GameObject gameObject = new GameObject(Path.GetFileNameWithoutExtension(meshAssetPath)); gameObject.AddComponent <MeshFilter>().sharedMesh = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Mesh>(meshAssetPath); gameObject.AddComponent <MeshRenderer>().sharedMaterial = material; return(gameObject); }
public void AssetDatabase_MoveAsset_AssetAsRoot() { RuntimeUrdf.runtimeModeEnabled = false; Assert.IsTrue(RuntimeUrdf.AssetDatabase_MoveAsset($"{createAssetPath}/TestAsset.prefab", "Assets/Tests/TestAsset.prefab").Length == 0); // Move back for other tests AssetDatabase.MoveAsset("Assets/Tests/TestAsset.prefab", $"{createAssetPath}/TestAsset.prefab"); }
public void SetUp() { // a robot tag needs to be added in project settings before runtime import can work RuntimeUrdf.SetRuntimeMode(false); UrdfRobotExtensions.CreateTag(); RuntimeUrdf.AssetDatabase_CreateFolder("Assets", "Tests"); RuntimeUrdf.AssetDatabase_CreateFolder("Assets/Tests", "Runtime"); RuntimeUrdf.AssetDatabase_CreateFolder("Assets/Tests/Runtime", "GeometryTests"); }
public void AssetExists_False() { RuntimeUrdf.SetRuntimeMode(false); Assert.IsFalse(RuntimeUrdf.AssetExists($"{createAssetPath}/tEstAsset.Prefab")); // case Assert.IsFalse(RuntimeUrdf.AssetExists($"{createAssetPath}/TestAsset.prefabs", true)); Assert.IsFalse(RuntimeUrdf.AssetExists($"{createAssetPath}/TestAsset.prefa", true)); Assert.IsFalse(RuntimeUrdf.AssetExists($"{createAssetPath}/estAsset.prefab", true)); Assert.IsFalse(RuntimeUrdf.AssetExists($"{createAssetPath}/sub/TestAsset.prefab", true)); }
public static bool IsValidAssetPath(string path) { #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { return(GetRelativeAssetPath(path) != null); } #endif //RuntimeImporter. TODO: check if the path really exists return(true); }
private static GameObject CreateMeshVisual(Link.Geometry.Mesh mesh) { #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { GameObject meshObject = LocateAssetHandler.FindUrdfAsset <GameObject>(mesh.filename); return(meshObject == null ? null : (GameObject)RuntimeUrdf.PrefabUtility_InstantiatePrefab(meshObject)); } #endif return(CreateMeshVisualRuntime(mesh)); }
private static string LocateAssetFile(string invalidPath) { string fileExtension = Path.GetExtension(invalidPath)?.Replace(".", ""); string newPath = RuntimeUrdf.EditorUtility_OpenFilePanel( "Couldn't find asset at " + invalidPath + ". Select correct file.", UrdfAssetPathHandler.GetPackageRoot(), fileExtension); return(UrdfAssetPathHandler.GetRelativeAssetPath(newPath)); }
public static T FindUrdfAsset <T>(string urdfFileName) where T : UnityEngine.Object { string fileAssetPath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName); // check if it is an asset tha requires post processing (AIRO-908) var originalUrdfPath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName, false); if (originalUrdfPath.ToLower().EndsWith(".stl")) { // it is an asset that requires post processing if (UrdfRobotExtensions.importsettings.OverwriteExistingPrefabs || !RuntimeUrdf.AssetExists(fileAssetPath, true)) { // post process again to (re)create prefabs StlAssetPostProcessor.PostprocessStlFile(originalUrdfPath); } } T assetObject = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <T>(fileAssetPath); if (assetObject) { return(assetObject); } //If asset was not found, let user choose whether to search for //or ignore the missing asset. string invalidPath = fileAssetPath ?? urdfFileName; int option = RuntimeUrdf.EditorUtility_DisplayDialogComplex("Urdf Importer: Asset Not Found", "Current root folder: " + UrdfAssetPathHandler.GetPackageRoot() + "\n\nExpected asset path: " + invalidPath, "Locate Asset", "Ignore Missing Asset", "Locate Root Folder"); switch (option) { case 0: fileAssetPath = LocateAssetFile(invalidPath); break; case 1: break; case 2: fileAssetPath = LocateRootAssetFolder <T>(urdfFileName); break; } assetObject = (T)RuntimeUrdf.AssetDatabase_LoadAssetAtPath(fileAssetPath, typeof(T)); if (assetObject != null) { return(assetObject); } ChooseFailureOption(urdfFileName); return(null); }
private static void ConvertMeshToColliders(GameObject gameObject, string location = null, bool setConvex = true) { MeshFilter[] meshFilters = gameObject.GetComponentsInChildren <MeshFilter>(); if (UrdfRobotExtensions.importsettings.convexMethod == ImportSettings.convexDecomposer.unity) { foreach (MeshFilter meshFilter in meshFilters) { GameObject child = meshFilter.gameObject; MeshCollider meshCollider = child.AddComponent <MeshCollider>(); meshCollider.sharedMesh = meshFilter.sharedMesh; meshCollider.convex = setConvex; Object.DestroyImmediate(child.GetComponent <MeshRenderer>()); Object.DestroyImmediate(meshFilter); } } else { string templateFileName = ""; string filePath = ""; int meshIndex = 0; if (!RuntimeUrdf.IsRuntimeMode() && location != null) { string meshFilePath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(location, false); templateFileName = Path.GetFileNameWithoutExtension(meshFilePath); filePath = Path.GetDirectoryName(meshFilePath); } foreach (MeshFilter meshFilter in meshFilters) { GameObject child = meshFilter.gameObject; VHACD decomposer = child.AddComponent <VHACD>(); List <Mesh> colliderMeshes = decomposer.GenerateConvexMeshes(meshFilter.sharedMesh); foreach (Mesh collider in colliderMeshes) { if (!RuntimeUrdf.IsRuntimeMode()) { meshIndex++; string name = $"{filePath}/{templateFileName}_{meshIndex}.asset"; Debug.Log($"Creating new mesh file: {name}"); RuntimeUrdf.AssetDatabase_CreateAsset(collider, name); RuntimeUrdf.AssetDatabase_SaveAssets(); } MeshCollider current = child.AddComponent <MeshCollider>(); current.sharedMesh = collider; current.convex = setConvex; } Component.DestroyImmediate(child.GetComponent <VHACD>()); Object.DestroyImmediate(child.GetComponent <MeshRenderer>()); Object.DestroyImmediate(meshFilter); } } }
private static void ChooseFailureOption(string urdfFilePath) { if (!RuntimeUrdf.EditorUtility_DisplayDialog( "Urdf Importer: Missing Asset", "Missing asset " + Path.GetFileName(urdfFilePath) + " was ignored or could not be found.\n\nContinue URDF Import?", "Yes", "No")) { throw new InterruptedUrdfImportException("User cancelled URDF import. Model may be incomplete."); } }
private static void CreateStlPrefab(string stlFile) { GameObject gameObject = CreateStlParent(stlFile); if (!gameObject) { Debug.LogWarning($"Could not create a mesh prefab for {stlFile}"); return; } RuntimeUrdf.PrefabUtility_SaveAsPrefabAsset(gameObject, GetPrefabAssetPath(stlFile)); Object.DestroyImmediate(gameObject); }
private static void MoveMaterialsToNewLocation(string oldPackageRoot) { if (RuntimeUrdf.AssetDatabase_IsValidFolder(Path.Combine(oldPackageRoot, MaterialFolderName))) { RuntimeUrdf.AssetDatabase_MoveAsset( Path.Combine(oldPackageRoot, MaterialFolderName), Path.Combine(UrdfAssetPathHandler.GetPackageRoot(), MaterialFolderName)); } else { RuntimeUrdf.AssetDatabase_CreateFolder(UrdfAssetPathHandler.GetPackageRoot(), MaterialFolderName); } }
private static Link.Visual.Material.Texture ExportTextureData(Texture texture) { string oldTexturePath = UrdfAssetPathHandler.GetFullAssetPath(RuntimeUrdf.AssetDatabase_GetAssetPath(texture)); string newTexturePath = UrdfExportPathHandler.GetNewResourcePath(Path.GetFileName(oldTexturePath)); if (oldTexturePath != newTexturePath) { File.Copy(oldTexturePath, newTexturePath, true); } string packagePath = UrdfExportPathHandler.GetPackagePathForResource(newTexturePath); return(new Link.Visual.Material.Texture(packagePath)); }
private static Material GetDefaultDiffuseMaterial() { #if UNITY_EDITOR // also save the material in the Assets if (!RuntimeUrdf.IsRuntimeMode() && MaterialExtensions.GetRenderPipelineType() == MaterialExtensions.RenderPipelineType.Standard) { s_DefaultDiffuse = RuntimeUrdf.AssetDatabase_GetBuiltinExtraResource <Material>("Default-Diffuse.mat"); } #endif if (s_DefaultDiffuse) { // Could't use the "Default-Diffuse.mat", either because of HDRP or runtime. so let's create one. s_DefaultDiffuse = MaterialExtensions.CreateBasicMaterial(); } return(s_DefaultDiffuse); }
public static bool AssetExists(string assetPath, bool ignoreCase = false) { #if UNITY_EDITOR string[] foldersToSearch = { Path.GetDirectoryName(assetPath) }; var assetName = Path.GetFileNameWithoutExtension(assetPath); foreach (var guid2 in AssetDatabase_FindAssets(assetName, foldersToSearch)) { var possiblePath = RuntimeUrdf.AssetDatabase_GUIDToAssetPath(guid2); if (string.Equals(possiblePath, assetPath, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) { return(true); } } #endif return(false); }
public void AssetDatabase_CreateFolder_AssetAsRoot() { RuntimeUrdf.runtimeModeEnabled = false; string createdGUID = RuntimeUrdf.AssetDatabase_CreateFolder("Assets/Tests/Runtime", "RuntimeUrdf"); string compareGUID = AssetDatabase.GUIDFromAssetPath(createFolderPath).ToString(); // Verify valid folder Assert.IsTrue(AssetDatabase.IsValidFolder(createFolderPath)); // Verify matching GUID Assert.AreEqual(createdGUID, compareGUID); // Try creating the same folder again createdGUID = RuntimeUrdf.AssetDatabase_CreateFolder("Assets/Tests/Runtime", "RuntimeUrdf"); Assert.AreEqual(createdGUID, compareGUID); }
// Post creation stuff: add to parent, fix axis and add collision exceptions. private static void ImportPipelinePostCreate(ImportPipelineData im) { #if UNITY_EDITOR GameObjectUtility.SetParentAndAlign(im.robotGameObject, Selection.activeObject as GameObject); Undo.RegisterCreatedObjectUndo(im.robotGameObject, "Create " + im.robotGameObject.name); Selection.activeObject = im.robotGameObject; #endif CorrectAxis(im.robotGameObject); CreateCollisionExceptions(im.robot, im.robotGameObject); if (im.forceRuntimeMode) { // set runtime mode back to what it was RuntimeUrdf.SetRuntimeMode(im.wasRuntimeMode); } }
public static void SetPackageRoot(string newPath, bool correctingIncorrectPackageRoot = false) { string oldPackagePath = packageRoot; packageRoot = GetRelativeAssetPath(newPath); if (!RuntimeUrdf.AssetDatabase_IsValidFolder(Path.Combine(packageRoot, MaterialFolderName))) { RuntimeUrdf.AssetDatabase_CreateFolder(packageRoot, MaterialFolderName); } if (correctingIncorrectPackageRoot) { MoveMaterialsToNewLocation(oldPackagePath); } }
private static string LocateRootAssetFolder <T>(string urdfFileName) where T : UnityEngine.Object { string newAssetPath = RuntimeUrdf.EditorUtility_OpenFolderPanel( "Locate package root folder", Path.Combine(Path.GetDirectoryName(Application.dataPath), "Assets"), ""); if (UrdfAssetPathHandler.IsValidAssetPath(newAssetPath)) { UrdfAssetPathHandler.SetPackageRoot(newAssetPath, true); } else { Debug.LogWarning("Selected package root " + newAssetPath + " is not within the Assets folder."); } return(UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName)); }
public static string GetRelativeAssetPath(string absolutePath) { var absolutePathUnityFormat = absolutePath.SetSeparatorChar(); if (!absolutePathUnityFormat.StartsWith(Application.dataPath.SetSeparatorChar())) { #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { return(null); } #endif return(absolutePath); // so that it works in runtime } var assetPath = "Assets" + absolutePath.Substring(Application.dataPath.Length); return(assetPath.SetSeparatorChar()); }