コード例 #1
0
ファイル: ModelPackExporter.cs プロジェクト: D3fau4/image2gnf
        public static void ExportFile(Ai.Scene scene, string path)
        {
            var aiContext = new Ai.AssimpContext();

            aiContext.XAxisRotation = 90;
            aiContext.ExportFile(scene, path, "collada", Ai.PostProcessSteps.FlipUVs);
        }
コード例 #2
0
        public static void Export(Ai.Scene aiScene, string fileName, Ai.PostProcessSteps postProcessSteps = Ai.PostProcessSteps.None)
        {
            var aiContext = new Ai.AssimpContext();

            var formatExtension = Path.GetExtension(fileName).Substring(1);
            var formatId        = aiContext.GetSupportedExportFormats()
                                  .First(x => x.FileExtension.Equals(formatExtension, StringComparison.OrdinalIgnoreCase)).FormatId;

            aiContext.ExportFile(aiScene, fileName, formatId, postProcessSteps);
        }
コード例 #3
0
        private void Export(object parameter)
        {
            FileExtension = FileExtension.ToString();
            string exportedFile = FilePath + "\\" + FileName + '.' + FileExtension;

            if (FileExtension == "")
            {
                System.Windows.MessageBox.Show("Select a file extension");
                return;
            }
            if (FileName == "")
            {
                System.Windows.MessageBox.Show("Choose a name for your file");
                return;
            }
            if (!Directory.Exists(FilePath))
            {
                System.Windows.MessageBox.Show("Path is invalid");
                return;
            }
            if (File.Exists(exportedFile))
            {
                System.Windows.MessageBox.Show("File already exists at " + FilePath);
                return;
            }
            else
            {
                bool result = false;

                if (Session.CurrentSession.HasCurrentProject() && Session.CurrentSession.CurrentProject.CurrentModel3D != null)
                    scene = Session.CurrentSession.CurrentProject.CurrentModel3D.GetScene();
                
                if (Scene == null)
                {
                    System.Windows.MessageBox.Show("You must open a scene in order to export it");
                    return;
                }

                using (AssimpContext exporter = new AssimpContext())
                {
                    ExportFormatDescription[] exportFormat = exporter.GetSupportedExportFormats();

                    foreach (ExportFormatDescription format in exportFormat)
                    {
                        if (format.FileExtension == FileExtension)
                        {
                            result = exporter.ExportFile(scene, exportedFile, format.FormatId);
                            break;
                        }
                    }
                    if (result == false)
                    {
                        System.Windows.MessageBox.Show("Bad export file extension");
                        return;
                    }
                }
                    
                if (result == false)
                {
                    System.Windows.MessageBox.Show("Export failed");
                    return;
                }
                Window currentWindow = parameter as Window;
                McWindow.CloseWindow(currentWindow);
            }
        }
コード例 #4
0
ファイル: ExportDialog.cs プロジェクト: Kolky/open3mod
        private void DoExport(Scene scene, string id) 
        {          
            var overwriteWithoutConfirmation = checkBoxNoOverwriteConfirm.Checked;

            var path = textBoxPath.Text.Trim();
            path = (path.Length > 0 ? path : scene.BaseDir);
            var name = textBoxFileName.Text.Trim();
            var fullPath = Path.Combine(path, name);
            if (!overwriteWithoutConfirmation && Path.GetFullPath(fullPath) == Path.GetFullPath(scene.File))
            {
                if (MessageBox.Show("This will overwrite the current scene's source file. Continue?", "Warning", 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    PushLog("Canceled");
                    return;
                }
            }

            var copyTextures = checkBoxCopyTexturesToSubfolder.Checked;
            var relativeTexturePaths = checkBoxUseRelativeTexturePaths.Checked;
            var includeAnimations = checkBoxIncludeAnimations.Checked;
            var includeSceneHierarchy = checkBoxIncludeSceneHierarchy.Checked;

            var textureCopyJobs = new Dictionary<string, string>();
            var textureDestinationFolder = textBoxCopyTexturesToFolder.Text;

            PushLog("*** Export: " + scene.File);

            if(copyTextures)
            {
                try
                {
                    Directory.CreateDirectory(Path.Combine(path, textureDestinationFolder));
                }
                catch (Exception)
                {
                    PushLog("Failed to create texture destination directory " + Path.Combine(path, textureDestinationFolder));
                    return;
                }
            }

            progressBarExport.Style = ProgressBarStyle.Marquee;
            progressBarExport.MarqueeAnimationSpeed = 5;

            // Create a shallow copy of the original scene that replaces all the texture paths with their
            // corresponding output paths, and omits animations if requested.
            var sourceScene = new Assimp.Scene
                              {
                                  Textures = scene.Raw.Textures,
                                  SceneFlags = scene.Raw.SceneFlags,
                                  RootNode = scene.Raw.RootNode,
                                  Meshes = scene.Raw.Meshes,
                                  Lights = scene.Raw.Lights,
                                  Cameras = scene.Raw.Cameras
                              };

            if (includeAnimations)
            {
                sourceScene.Animations = scene.Raw.Animations;
            }

            var uniques = new HashSet<string>();
            var textureMapping = new Dictionary<string, string>();

            PushLog("Locating all textures");
            foreach (var texId in scene.TextureSet.GetTextureIds())
            {
                // TODO(acgessler): Verify if this handles texture replacements and GUID-IDs correctly.
                var destName = texId;

                // Broadly skip over embedded (in-memory) textures
                if (destName.StartsWith("*"))
                {
                    PushLog("Ignoring embedded texture: " + destName);
                    continue;
                }

                // Locate the texture on-disk
                string diskLocation;
                try
                {
                    TextureLoader.ObtainStream(texId, scene.BaseDir, out diskLocation).Close();
                } catch(IOException)
                {
                    PushLog("Failed to locate texture " + texId);
                    continue;
                }
             
                if (copyTextures)
                {
                    destName = GeUniqueTextureExportPath(path, textureDestinationFolder, diskLocation, uniques);
                }

                if (relativeTexturePaths)
                {
                    textureMapping[texId] = GetRelativePath(path + "\\", destName);
                }
                else
                {
                    textureMapping[texId] = destName;
                }
                textureCopyJobs[diskLocation] = destName;

                PushLog("Texture " + texId + " maps to " + textureMapping[texId]);              
            }

            foreach (var mat in scene.Raw.Materials)
            {
                sourceScene.Materials.Add(CloneMaterial(mat, textureMapping));
            }

            var t = new Thread(() =>
            {
                using (var v = new AssimpContext())
                {
                    PushLog("Exporting using Assimp to " + fullPath + ", using format id: " + id);
                    var result = v.ExportFile(sourceScene, fullPath, id, includeSceneHierarchy 
                        ? PostProcessSteps.None 
                        : PostProcessSteps.PreTransformVertices);
                    _main.BeginInvoke(new MethodInvoker(() =>
                    {
                        progressBarExport.Style = ProgressBarStyle.Continuous;
                        progressBarExport.MarqueeAnimationSpeed = 0;

                        if (!result)
                        {
                            // TODO: get native error message
                            PushLog("Export failure");
                            MessageBox.Show("Failed to export to " + fullPath, "Export error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            if (copyTextures)
                            {
                                PushLog("Copying textures");
                                foreach (var kv in textureCopyJobs)
                                {
                                    PushLog(" ... " + kv.Key + " -> " + kv.Value);
                                    try
                                    {
                                        File.Copy(kv.Key, kv.Value, false);
                                    }                               
                                    catch (IOException)
                                    {
                                        if (!File.Exists(kv.Value))
                                        {
                                            throw;
                                        }
                                        if (!overwriteWithoutConfirmation && MessageBox.Show("Texture " + kv.Value +
                                                " already exists. Overwrite?", "Overwrite Warning",
                                                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                                        {
                                            PushLog("Exists already, skipping");
                                            continue;
                                        }
                                        PushLog("Exists already, overwriting");
                                        File.Copy(kv.Key, kv.Value, true);
                                    }
                                    catch (Exception ex)
                                    {
                                        PushLog(ex.Message);
                                    }
                                }
                            }
                            if (checkBoxOpenExportedFile.Checked)
                            {
                                _main.AddTab(fullPath, true, false);
                            }
                            PushLog("Export completed");
                        }
                    }));
                }
            });

            t.Start();
        }