Пример #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);
        }
        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);
        }
Пример #3
0
        public static void Create(string filename)
        {
            Robot robot = new Robot();

            robot.ConstructFromFile(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);

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

            GameObjectUtility.SetParentAndAlign(robotGameObject, Selection.activeObject as GameObject);
            Undo.RegisterCreatedObjectUndo(robotGameObject, "Create " + robotGameObject.name);
            Selection.activeObject = robotGameObject;
        }
Пример #4
0
        public static IEnumerator Create(string filename, ImportSettings settings, bool loadStatus = false)
        {
            CreateTag();
            importsettings = settings;
            Robot robot = new Robot(filename);

            settings.totalLinks = robot.links.Count;

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

            GameObject robotGameObject = new GameObject(robot.name);

            robotGameObject.tag = tagName;

            robotGameObject.AddComponent <UrdfRobot>();


            robotGameObject.AddComponent <RosSharp.Control.Controller>();

            robotGameObject.GetComponent <UrdfRobot>().SetAxis(settings.choosenAxis);

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

            Stack <Tuple <Link, Transform, Joint> > importStack = new Stack <Tuple <Link, Transform, Joint> >();

            importStack.Push(new Tuple <Link, Transform, Joint>(robot.root, robotGameObject.transform, null));
            while (importStack.Count != 0)
            {
                Tuple <Link, Transform, Joint> currentLink = importStack.Pop();
                GameObject importedLink = UrdfLinkExtensions.Create(currentLink.Item2, currentLink.Item1, currentLink.Item3);
                settings.linksLoaded++;
                foreach (Joint childJoint in currentLink.Item1.joints)
                {
                    Link child = childJoint.ChildLink;
                    importStack.Push(new Tuple <Link, Transform, Joint>(child, importedLink.transform, childJoint));
                }

                if (loadStatus)
                {
                    yield return(null);
                }
            }

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

            CorrectAxis(robotGameObject);
            CreateCollisionExceptions(robot, robotGameObject);
        }
Пример #5
0
        public static void CreateFromString(string urdfString)
        {
            Robot robot = new Robot();

            robot.ConstructFromString(urdfString);

            UrdfAssetPathHandler.SetPackageRoot(Application.dataPath + "/Urdf/FromString");

            Create(robot);
        }
Пример #6
0
        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);
     }
 }
Пример #8
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));
        }
Пример #9
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 void CreateSimulatedUrdfObject()
        {
            string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (Path.GetExtension(assetPath)?.ToLower() == ".urdf")
            {
                UrdfSimulatedRobotExtensions.CreateFromFile(UrdfAssetPathHandler.GetFullAssetPath(assetPath));
            }
            else
            {
                EditorUtility.DisplayDialog("Urdf Import As Simulation",
                                            "The file you selected was not a URDF file. A robot can only be imported from a valid URDF file.", "Ok");
            }
        }
        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);
        }
Пример #12
0
        private static void CreateUrdfObject()
        {
            //Get path to asset, check if it's a urdf file
            string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (Path.GetExtension(assetPath)?.ToLower() == ".urdf")
            {
                UrdfRobotExtensions.Create(UrdfAssetPathHandler.GetFullAssetPath(assetPath));
            }
            else
            {
                EditorUtility.DisplayDialog("URDF Import",
                                            "The file you selected was not a URDF file. A robot can only be imported from a valid URDF file.", "Ok");
            }
        }
Пример #13
0
        public static void CreateFromFile(string filename)
        {
            Robot robot = new Robot();

            robot.ConstructFromFile(filename);

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

            UrdfAssetPathHandler.SetPackageRoot(Path.GetDirectoryName(robot.filename));

            Create(robot);
        }
Пример #14
0
        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));
        }
Пример #15
0
        private static void CreateUrdfObject()
        {
            //Get path to asset, check if it's a urdf file
            string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (Path.GetExtension(assetPath)?.ToLower() == ".urdf")
            {
                // Get existing open window or if none, make a new one:
                FileImportMenu window = (FileImportMenu)EditorWindow.GetWindow(typeof(FileImportMenu));
                window.urdfFile = UrdfAssetPathHandler.GetFullAssetPath(assetPath);
                window.minSize  = new Vector2(500, 200);
                window.Show();
            }
            else
            {
                EditorUtility.DisplayDialog("URDF Import",
                                            "The file you selected was not a URDF file. Please select a valid URDF file.", "Ok");
            }
        }
Пример #16
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);
         }
     }
 }
Пример #17
0
        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);
        }
Пример #18
0
        public static void Create(string filename, ImportSettings settings)
        {
            CreateTag();
            importsettings = settings;
            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.tag = tagName;

            robotGameObject.AddComponent <UrdfRobot>();


            robotGameObject.AddComponent <RosSharp.Control.Controller>();

            robotGameObject.GetComponent <UrdfRobot>().SetAxis(settings.choosenAxis);

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

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

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

            CorrectAxis(robotGameObject);
            CreateCollisionExceptions(robot, robotGameObject);
        }