예제 #1
0
        public override void LoadFile(string sNewPath)
        {
            // save this path because it is super-annoying otherwise...
            SceneGraphConfig.LastFileOpenPath = ListView.FolderPath;
            if (IsMeshFile(sNewPath))
            {
                FContext controller = ActiveCockpit.Context;

                // read mesh file
                SceneMeshImporter import = new SceneMeshImporter();
                bool bOK = import.ReadFile(sNewPath);
                if (bOK == false && import.LastReadResult.code != IOCode.Ok)
                {
                    Debug.Log("[Import] import failed: " + import.LastReadResult.message);
                    HUDUtil.ShowCenteredPopupMessage("Import Failed!", "Sorry, could not import " + sNewPath, ActiveCockpit);
                    return;
                }
                import.AppendReadMeshesToScene(controller.Scene, true);
                // save undo/redo checkpoint
                controller.Scene.History.PushInteractionCheckpoint();

                // once we have opened file, done with cockpit
                controller.PopCockpit(true);

                // emit this message after we pop cockpit
                if (import.SomeMeshesTooLargeForUnityWarning)
                {
                    Debug.Log("[Import] some meshes too large! ignored!");
                    MessageStream.Get.AddMessage(new Message()
                    {
                        Type = Message.Types.Warning, Code = FileBrowserMessageCodes.MESHES_TOO_LARGE
                    });
                }
            }
        }
예제 #2
0
        public virtual SceneObject BuildMeshReference(FScene scene, string sSceneFilePath, TypedAttribSet attributes)
        {
            string sAbsPath = "", sRelPath = "";
            bool   bAbsPathOK = safe_set_property_s(attributes, IOStrings.AReferencePath, (str) => { sAbsPath = str; });
            bool   bRelPathOK = safe_set_property_s(attributes, IOStrings.ARelReferencePath, (str) => { sRelPath = str; });

            string sScenePathDir = Path.GetDirectoryName(sSceneFilePath);

            // ok we are going to try really hard to find references...
            string sUsePath      = "";
            string sBaseFilename = "";

            if (bRelPathOK)
            {
                sBaseFilename = Path.GetFileName(sRelPath);

                // first we check if relative path exists
                string sAbsRelPath = Path.Combine(sScenePathDir, sRelPath);
                if (System.IO.File.Exists(sAbsRelPath))
                {
                    DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using relative path " + sRelPath);
                    sUsePath = sAbsRelPath;
                }

                // if not, we try appending filename to scene path
                if (sUsePath == "")
                {
                    string sLocalPath = Path.Combine(sScenePathDir, sBaseFilename);
                    if (System.IO.File.Exists(sLocalPath))
                    {
                        DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using local path " + sLocalPath);
                        sUsePath = sLocalPath;
                    }
                }

                // if that fails we accumulate relative path segments (from last folder backwards)
                // and see if we can find the file in any of those
                List <string> subdirs    = new List <string>(sRelPath.Split(Path.DirectorySeparatorChar).Reverse());
                int           N          = subdirs.Count;
                string        sAccumPath = "";
                for (int i = 1; i < N && sUsePath == ""; ++i)
                {
                    sAccumPath = Path.Combine(subdirs[i], sAccumPath);
                    string sLocalPath = Path.Combine(Path.Combine(sScenePathDir, sAccumPath), sBaseFilename);
                    DebugUtil.Log(2, "trying " + sLocalPath);
                    if (System.IO.File.Exists(sLocalPath))
                    {
                        DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using local path " + sLocalPath);
                        sUsePath = sLocalPath;
                    }
                }
            }

            // ok if all that failed, try absolute path
            if (sUsePath == "" && bAbsPathOK)
            {
                sBaseFilename = Path.GetFileName(sAbsPath);
                if (System.IO.File.Exists(sAbsPath))
                {
                    DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using absolute path " + sAbsPath);
                    sUsePath = sAbsPath;
                }

                // try appending filename to scene path in case we didn't have a relative path at all (bRelPathOK = false)
                if (sUsePath == "")
                {
                    string sLocalPath = Path.Combine(sScenePathDir, sBaseFilename);
                    if (System.IO.File.Exists(sLocalPath))
                    {
                        DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using local path " + sLocalPath);
                        sUsePath = sLocalPath;
                    }
                }
            }

            if (sUsePath == "")
            {
                emit_message("referenced mesh does not exist at path [" + sAbsPath + "] or [" + sRelPath + "] ");
                return(null);
            }

            SceneMeshImporter import = new SceneMeshImporter();
            bool bOK = import.ReadFile(sUsePath);

            if (bOK == false && import.LastReadResult.code != g3.IOCode.Ok)
            {
                emit_message("import of mesh [" + sUsePath + "] failed: " + import.LastReadResult.message);
                // [TODO] how can we show this message?
                //HUDUtil.ShowCenteredStaticPopupMessage("popups/error_reading_file", activeCockpit);
                return(null);
            }
            MeshReferenceSO refSO = import.GetMeshReference(scene.DefaultMeshSOMaterial);

            safe_set_property_s(attributes, IOStrings.ASOName, (s) => { refSO.Name = s; });
            RestoreTransform(refSO, attributes);
            return(refSO);
        }