Exemple #1
0
        private static Material CreateMaterial(this Link.Visual.Material urdfMaterial)
        {
            if (urdfMaterial.name == "")
            {
                urdfMaterial.name = GenerateMaterialName(urdfMaterial);
            }

            var material = AssetDatabase.LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(urdfMaterial.name));

            if (material != null) //material already exists
            {
                return(material);
            }

            material = InitializeMaterial();

            if (urdfMaterial.color != null)
            {
                material.color = CreateColor(urdfMaterial.color);
            }
            else if (urdfMaterial.texture != null)
            {
                material.mainTexture = LoadTexture(urdfMaterial.texture.filename);
            }

            AssetDatabase.CreateAsset(material, UrdfAssetPathHandler.GetMaterialAssetPath(urdfMaterial.name));
            return(material);
        }
Exemple #2
0
        private static void CopyDaeTextureToExportDestination(string prefabPath, string newFolderLocation)
        {
            //Get material from Collada prefab
            Material material = AssetDatabase.LoadAssetAtPath <Material>(prefabPath);

            if (material.mainTexture == null)
            {
                return;
            }

            //Get relative subfolder where texture is, compared to the DAE file.
            string commonFolder     = Path.GetDirectoryName(prefabPath).SetSeparatorChar();
            string texturePath      = 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);
        }
        private static string LocateAssetFile(string invalidPath)
        {
            string fileExtension = Path.GetExtension(invalidPath)?.Replace(".", "");

            string newPath = EditorUtility.OpenFilePanel(
                "Couldn't find asset at " + invalidPath + ". Select correct file.",
                UrdfAssetPathHandler.GetPackageRoot(),
                fileExtension);

            return UrdfAssetPathHandler.GetRelativeAssetPath(newPath);
        }
 private static void MoveMaterialsToNewLocation(string oldPackageRoot)
 {
     if (AssetDatabase.IsValidFolder(Path.Combine(oldPackageRoot, MaterialFolderName)))
     {
         AssetDatabase.MoveAsset(
             Path.Combine(oldPackageRoot, MaterialFolderName),
             Path.Combine(UrdfAssetPathHandler.GetPackageRoot(), MaterialFolderName));
     }
     else
     {
         AssetDatabase.CreateFolder(UrdfAssetPathHandler.GetPackageRoot(), MaterialFolderName);
     }
 }
Exemple #5
0
        private static void CreateDefaultMaterial()
        {
            var material = AssetDatabase.LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName));

            if (material != null)
            {
                return;
            }

            material       = InitializeMaterial();
            material.color = new Color(0.33f, 0.33f, 0.33f, 0.0f);

            AssetDatabase.CreateAsset(material, UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName));
        }
Exemple #6
0
        private static Link.Visual.Material.Texture ExportTextureData(Texture texture)
        {
            string oldTexturePath = UrdfAssetPathHandler.GetFullAssetPath(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 string LocateRootAssetFolder<T>(string urdfFileName) where T : UnityEngine.Object
        {
            string newAssetPath = 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);
        }
Exemple #8
0
        private static string CopyMeshToExportDestination(string prefabPath)
        {
            string newPrefabPath = UrdfExportPathHandler.GetNewMeshPath(Path.GetFileName(prefabPath));

            if (Path.GetExtension(prefabPath)?.ToLower() == ".dae")
            {
                CopyDaeTextureToExportDestination(prefabPath, Path.GetDirectoryName(newPrefabPath));
            }

            prefabPath = UrdfAssetPathHandler.GetFullAssetPath(prefabPath);

            CopyFileToNewLocation(prefabPath, newPrefabPath);

            return(newPrefabPath);
        }
Exemple #9
0
 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)
         {
             var defaultMaterial = AssetDatabase.LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName));
             SetMaterial(gameObject, defaultMaterial);
         }
     }
 }
        public static T FindUrdfAsset<T>(string urdfFileName) where T : UnityEngine.Object
        {
            string fileAssetPath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName);
            T assetObject = AssetDatabase.LoadAssetAtPath<T>(fileAssetPath);

            if (assetObject != null)
                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 = 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) AssetDatabase.LoadAssetAtPath(fileAssetPath, typeof(T));
            if (assetObject != null)
                return assetObject;

            ChooseFailureOption(urdfFileName);
            return null;
        }
Exemple #11
0
        public static void Create(string filename)
        {
            Robot robot = new Robot(filename);

            if (!UrdfAssetPathHandler.IsValidAssetPath(robot.filename))
            {
                Debug.LogError("URDF file and ressources must be placed in Assets Folder:\n" + Application.dataPath);
                return;
            }

            GameObject robotGameObject = new GameObject(robot.name);

            robotGameObject.AddComponent <UrdfRobot>();

            UrdfAssetPathHandler.SetPackageRoot(Path.GetDirectoryName(robot.filename));
            UrdfMaterial.InitializeRobotMaterials(robot);
            UrdfPlugins.Create(robotGameObject.transform, robot.plugins);

            UrdfLink.Create(robotGameObject.transform, robot.root);

            GameObjectUtility.SetParentAndAlign(robotGameObject, Selection.activeObject as GameObject);
            Undo.RegisterCreatedObjectUndo(robotGameObject, "Create " + robotGameObject.name);
            Selection.activeObject = robotGameObject;
        }