public static void WriteFile(this DMesh3 mesh, string filePath, WriteOptions opts) { var writer = new StandardMeshWriter(); var m = new WriteMesh(mesh); var result = writer.Write(filePath, new List <WriteMesh> { m }, opts); if (!result.Equals(IOWriteResult.Ok)) { throw new Exception($"Failed to write file to {filePath} with result {result.ToString()}"); } }
private void bake_unwrap_uvs_Click(object sender, EventArgs e) { mmRemote = new mm.RemoteControl(); if (mmRemote.Initialize()) { statusLabel.Text = "Connected!"; } else { statusLabel.Text = "Could not connect!"; return; } List <int> vSelected = mmRemote.ListSelectedObjects(); if (vSelected.Count == 1) { statusLabel.Text = "Reading..."; DenseMeshUVSet uvSet; DMesh3 mesh = g3Conversion.ConvertUnwrapToUVs(mmRemote, out uvSet); //statusLabel.Text = "Writing..."; StandardMeshWriter writer = new StandardMeshWriter(); WriteOptions options = WriteOptions.Defaults; options.bWriteGroups = true; options.bWriteUVs = true; options.AsciiHeaderFunc = () => { return("mtllib default.mtl\r\nusemtl Texture_0\r\n"); }; WriteMesh wm = new WriteMesh(mesh) { UVs = uvSet }; writer.Write("c:\\scratch\\___EXPORT_UV.obj", new List <WriteMesh>() { wm }, options); statusLabel.Text = "Done!"; } else { MessageBox.Show("This command only works if a single object is selected"); } mmRemote.Shutdown(); statusLabel.Text = "(disconnected)"; }
protected override void Recompute(DGArguments args) { string path = Inputs[0].Value <string>(args); DMesh3 mesh = Inputs[1].Value <DMesh3>(args); StandardMeshWriter writer = new StandardMeshWriter(); IOWriteResult result = writer.Write(path, new List <WriteMesh>() { new WriteMesh(mesh) }, WriteOptions.Defaults); if (result.code != IOCode.Ok) { // what?? } }
private void mesh_to_obj_button_Click(object sender, EventArgs e) { mmRemote = new mm.RemoteControl(); if (mmRemote.Initialize()) { statusLabel.Text = "Connected!"; } else { statusLabel.Text = "Could not connect!"; return; } List <int> vSelected = mmRemote.ListSelectedObjects(); if (vSelected.Count == 1) { statusLabel.Text = "Reading..."; DMesh3 mesh = g3Conversion.ReadMeshFromMM(mmRemote, vSelected[0]); statusLabel.Text = "Writing..."; StandardMeshWriter writer = new StandardMeshWriter(); WriteOptions options = WriteOptions.Defaults; options.bWriteGroups = true; writer.Write("c:\\scratch\\___EXPORT.obj", new List <WriteMesh>() { new WriteMesh(mesh) }, options); statusLabel.Text = "Done!"; } else { MessageBox.Show("This command only works if a single object is selected"); } mmRemote.Shutdown(); statusLabel.Text = "(disconnected)"; }
public IOWriteResult RunBackgroundWrite() { // transform meshes gParallel.ForEach(Interval1i.Range(ExportMeshes.Length), (i) => { if (MeshFrames[i].Origin != Vector3f.Zero || MeshFrames[i].Rotation != Quaternionf.Identity) { MeshTransforms.FromFrame(ExportMeshes[i], MeshFrames[i]); } MeshTransforms.FlipLeftRightCoordSystems(ExportMeshes[i]); if (ExportYUp == false) { MeshTransforms.ConvertYUpToZUp(ExportMeshes[i]); } }); List <WriteMesh> writeMeshes = new List <WriteMesh>(); for (int i = 0; i < ExportMeshes.Length; ++i) { writeMeshes.Add(new WriteMesh(ExportMeshes[i])); } WriteOptions options = WriteOptions.Defaults; options.bWriteBinary = true; options.ProgressFunc = BackgroundProgressFunc; StandardMeshWriter writer = new StandardMeshWriter(); IOWriteResult result = writer.Write(WritePath, writeMeshes, options); return(result); }
// // [TODO] // - binary output option // - option to strip data from inputs (eg remove normals/colors/uv/material from obj) // - option to remove material props from OBJ // - option to combine input meshes // - option to set float precision // - option to estimate normals for writing (eg for obj) // - option to set constant color for vertices // static void Main(string[] args) { if (args.Length != 2) { System.Console.WriteLine("gsMeshConvert v1.0 - Copyright gradientspace / Ryan Schmidt 2017"); System.Console.WriteLine("Questions? Comments? www.gradientspace.com or @gradientspace"); System.Console.WriteLine("usage: gsMeshConvert <input_mesh.format> (output_mesh.format)"); return; } string sInputFile = args[0]; if (!File.Exists(sInputFile)) { System.Console.WriteLine("cannot find file " + sInputFile); return; } string sOutputFile = args[1]; // check that can write output file DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; ReadOptions read_options = ReadOptions.Defaults; IOReadResult readOK = reader.Read(sInputFile, read_options); if (readOK.code != IOCode.Ok) { System.Console.WriteLine("Error reading " + sInputFile); System.Console.WriteLine(readOK.message); return; } if (builder.Meshes.Count == 0) { System.Console.WriteLine("did not find any valid meshes in " + sInputFile); return; } List <WriteMesh> write_meshes = new List <WriteMesh>(); foreach (DMesh3 mesh in builder.Meshes) { write_meshes.Add(new WriteMesh(mesh)); } StandardMeshWriter writer = new StandardMeshWriter(); WriteOptions write_options = WriteOptions.Defaults; IOWriteResult writeOK = writer.Write(sOutputFile, write_meshes, write_options); if (writeOK.code != IOCode.Ok) { System.Console.WriteLine("Error writing " + sOutputFile); System.Console.WriteLine(writeOK.message); return; } // ok done! //System.Console.ReadKey(); }
// make sure format writers all minimally function, and properly close file when completed public static void test_write_formats() { string out_path = Program.TEST_OUTPUT_PATH + "format_test"; DMesh3 mesh = StandardMeshReader.ReadMesh(Program.TEST_FILES_PATH + "bunny_solid.obj"); StandardMeshWriter writer = new StandardMeshWriter(); var list = new List <WriteMesh>() { new WriteMesh(mesh) }; if (writer.Write(out_path + ".obj", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: obj failed"); } if (writer.Write(out_path + ".stl", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: stl failed"); } if (writer.Write(out_path + ".off", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: off failed"); } if (writer.Write(out_path + ".g3mesh", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: g3mesh failed"); } if (writer.Write(out_path + ".obj", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: obj failed on second pass"); } if (writer.Write(out_path + ".stl", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: stl failed on second pass"); } if (writer.Write(out_path + ".off", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: off failed on second pass"); } if (writer.Write(out_path + ".g3mesh", list, WriteOptions.Defaults).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: g3mesh failed on second pass"); } MemoryStream fileStream = new MemoryStream(); MemoryStream mtlStream = new MemoryStream(); writer.OpenStreamF = (filename) => { return(filename.EndsWith(".mtl") ? mtlStream : fileStream); }; writer.CloseStreamF = (stream) => { }; WriteOptions opt = WriteOptions.Defaults; opt.bWriteMaterials = true; opt.MaterialFilePath = out_path + ".mtl"; if (writer.Write(out_path + ".obj", list, opt).code != IOCode.Ok) { System.Console.WriteLine("test_write_formats: write to memory stream failed"); } //string s = Encoding.ASCII.GetString(fileStream.ToArray()); if (fileStream.Length == 0) { System.Console.WriteLine("test_write_formats: write to memory stream produced zero-length stream"); } }
// // [TODO] // static void Main(string[] args) { CommandArgumentSet arguments = new CommandArgumentSet(); arguments.Register("-output", ""); if (arguments.Parse(args) == false) { return; } if (arguments.Filenames.Count != 1) { print_usage(); return; } string sInputFile = arguments.Filenames[0]; string sFilenameRoot = Path.GetFileNameWithoutExtension(sInputFile); if (!File.Exists(sInputFile)) { System.Console.WriteLine("cannot find file " + sInputFile); return; } DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; ReadOptions read_options = ReadOptions.Defaults; read_options.ReadMaterials = true; IOReadResult readOK = reader.Read(sInputFile, read_options); if (readOK.code != IOCode.Ok) { System.Console.WriteLine("Error reading " + sInputFile); System.Console.WriteLine(readOK.message); return; } if (builder.Meshes.Count == 0) { System.Console.WriteLine("did not find any valid meshes in " + sInputFile); return; } // [TODO] out if count == 0 string sOutRoot = arguments.Strings["-output"]; if (sOutRoot.Length > 0) { bool bOutIsFolder = Directory.Exists(sOutRoot); if (!bOutIsFolder) { System.Console.WriteLine("-output folder {0} does not exist", sOutRoot); return; } } Dictionary <int, List <int> > MeshesByMaterial = new Dictionary <int, List <int> >(); MeshesByMaterial[-1] = new List <int>(); for (int i = 0; i < builder.Materials.Count; ++i) { MeshesByMaterial[i] = new List <int>(); } int N = builder.Meshes.Count; for (int i = 0; i < N; ++i) { int mati = builder.MaterialAssignment[i]; if (mati >= builder.Materials.Count) { mati = -1; } MeshesByMaterial[mati].Add(i); } int file_i = 0; foreach (int mat_i in MeshesByMaterial.Keys) { List <int> mesh_idxs = MeshesByMaterial[mat_i]; if (mesh_idxs.Count == 0) { continue; } WriteMesh[] write_meshes = new WriteMesh[mesh_idxs.Count]; for (int i = 0; i < mesh_idxs.Count; ++i) { write_meshes[i] = new WriteMesh(builder.Meshes[mesh_idxs[i]]); } string suffix = string.Format("_material{0}", file_i++); string sOutPath = Path.Combine(sOutRoot, sFilenameRoot + suffix + ".obj"); StandardMeshWriter writer = new StandardMeshWriter(); WriteOptions write_options = WriteOptions.Defaults; if (mat_i != -1) { write_options.bWriteMaterials = true; write_options.bPerVertexUVs = true; write_options.MaterialFilePath = Path.Combine(sOutRoot, sFilenameRoot + suffix + ".mtl"); GenericMaterial mat = builder.Materials[mat_i]; List <GenericMaterial> matList = new List <GenericMaterial>() { mat }; ConstantIndexMap idxmap = new ConstantIndexMap(0); for (int i = 0; i < write_meshes.Length; ++i) { write_meshes[i].Materials = matList; write_meshes[i].TriToMaterialMap = idxmap; } } IOWriteResult writeOK = writer.Write(sOutPath, new List <WriteMesh>(write_meshes), write_options); if (writeOK.code != IOCode.Ok) { System.Console.WriteLine("Error writing " + sOutPath); System.Console.WriteLine(writeOK.message); } } // ok done! //System.Console.ReadKey(); }