MeshLib Documentation
Loading...
Searching...
No Matches
Loading and saving mesh files

Following code presents example of loading and saving mesh file

  • C++
    #include <MRMesh/MRMesh.h>
    #include <iostream>
    int main()
    {
    // Load mesh
    std::filesystem::path inFilePath = "mesh.stl";
    auto loadRes = MR::MeshLoad::fromAnySupportedFormat( inFilePath );
    if ( !loadRes.has_value() )
    {
    std::cerr << loadRes.error() << std::endl;
    return 1;
    }
    // Save mesh
    std::filesystem::path outFilePath = "mesh.ply";
    auto saveRes = MR::MeshSave::toAnySupportedFormat( loadRes.value(), outFilePath );
    if ( !saveRes.has_value() )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    // More mesh formats are available in the MRIOExtras library
    std::filesystem::path outCtmFilePath = "mesh.ctm";
    saveRes = MR::MeshSave::toCtm( loadRes.value(), outCtmFilePath );
    if ( !saveRes.has_value() )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    Expected< void > toCtm(const Mesh &mesh, const std::filesystem::path &file, const CtmSaveOptions &options)
    Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    Further examples won't check return values for sake of clarity
    See also
    MR::MeshLoad
    MR::MeshSave
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    import os
    import sys
    # This example needs an input mesh; create one if you do not have it already
    if not os.path.exists("mesh.stl"):
    mrmeshpy.saveMesh(mrmeshpy.makeCube(), "mesh.stl")
    # Load mesh
    try:
    mesh = mrmeshpy.loadMesh("mesh.stl")
    except RuntimeError as e:
    print(f"Failed to load mesh: {e}")
    sys.exit(1)
    print(f"Loaded {mesh.topology.numValidVerts()} vertices, "
    f"{mesh.topology.numValidFaces()} faces")
    # Save mesh
    try:
    mrmeshpy.saveMesh(mesh, "mesh.ply")
    except RuntimeError as e:
    print(f"Failed to save mesh: {e}")
    sys.exit(1)
    print("Saved mesh.ply")
  • C
    #include <MRCIOExtras/MRCtm.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Load mesh
    MR_expected_MR_Mesh_std_string* meshEx = MR_MeshLoad_fromAnySupportedFormat( "mesh.stl", NULL, NULL );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( meshEx );
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshEx ) ) );
    goto fail_load;
    }
    // Save mesh
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( mesh, "mesh.ply", NULL, NULL );
    if ( MR_expected_void_std_string_error( saveEx ) )
    {
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveEx ) ) );
    goto fail_save;
    }
    // More mesh formats are available in the MRIOExtras library
    MR_expected_void_std_string* saveCtmEx = MR_MeshSave_toCtm_const_std_filesystem_path_ref_MR_SaveSettings( mesh, "mesh.ctm", NULL, NULL );
    if ( MR_expected_void_std_string_error( saveCtmEx ) )
    {
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveCtmEx ) ) );
    goto fail_save_ctm;
    }
    rc = EXIT_SUCCESS;
    fail_save_ctm:
    MR_expected_void_std_string_Destroy( saveCtmEx );
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_load:
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }
    const std::shared_ptr< const Mesh > & mesh() const
  • C#
    public static class MeshLoadSaveExample
    {
    public static void Main(string[] args)
    {
    // Load mesh
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    // Save mesh
    MR.MeshSave.toAnySupportedFormat(mesh, "mesh.ply");
    // More mesh formats are available in the MRIOExtras library
    MR.MeshSave.toCtm(mesh, "mesh.ctm");
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // Load mesh
    using mesh = ml.MeshLoad.fromAnySupportedFormat('mesh.stl');
    // Save mesh (the format is chosen from the file extension)
    ml.MeshSave.toAnySupportedFormat(mesh, 'mesh.ply');
    Note
    The using declaration requires Node.js 24+ or a modern browser. On older runtimes, call .delete() on each object instead (see Memory Management in the JavaScript Setup Guide).
  • Rust
    fn main() -> Result<(), String>
    {
    let args: Vec<String> = std::env::args().collect();
    let input_path = args.get(1).map_or("mesh.stl", String::as_str);
    let output_path = args.get(2).map_or("mesh.ply", String::as_str);
    // Load mesh. `into_result()` turns the returned `expected` into a Rust `Result`.
    let mesh = meshlib::mesh_load::from_any_supported_format(input_path, None).into_result().map_err(|error|
    {
    if args.len() <= 1
    {
    eprintln!("Usage: MeshLoadSave [input mesh] [output mesh]");
    }
    format!("Failed to load mesh: {error}")
    })?;
    // Save mesh.
    meshlib::mesh_save::to_any_supported_format(&mesh, output_path, None).into_result().map_err(|error| format!("Failed to save mesh: {error}"))?;
    Ok(())
    }
  • Java
    import meshlib.Mesh;
    import meshlib.SaveSettings;
    import meshlib.UnexpectedResultException;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshLoadSave
    {
    public static void main(String[] args)
    {
    // Load mesh. The C++ function returns `Expected<Mesh>`: the Java wrapper returns the mesh, or throws the error.
    Mesh mesh;
    try
    {
    mesh = meshlib.meshload.Functions.fromAnySupportedFormat("mesh.stl", null);
    }
    catch (UnexpectedResultException e)
    {
    System.err.println("Failed to load mesh: " + e.getMessage());
    System.exit(1);
    return;
    }
    // Save mesh. An error is thrown the same way.
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "mesh.ply", null);
    // More mesh formats are available in the MRIOExtras library
    meshlib.meshsave.Functions.toCtm(mesh, "mesh.ctm", new SaveSettings());
    }
    }