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 <MRMesh/MRMeshLoad.h>
    #include <MRMesh/MRMeshSave.h>
    #include <MRIOExtras/MRCtm.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
    Further examples won't check return values for sake of clarity
    See also
    MR::MeshLoad
    MR::MeshSave
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    import sys
    # Load mesh
    try:
    mesh = mrmeshpy.loadMesh("mesh.stl")
    except ValueError as e:
    print(e)
    sys.exit(1)
    # Save mesh
    try:
    mrmeshpy.saveMesh(mesh, "mesh.ply")
    except ValueError as e:
    print(e)
    sys.exit(1)
  • C
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRMeshTopology.h>
    #include <MRCMesh/MRString.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>
    #include <string.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Load mesh.
    MR_expected_MR_Mesh_std_string* meshEx = MR_MeshLoad_fromAnySupportedFormat_2( "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_3( 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;
    }
    rc = EXIT_SUCCESS;
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_load:
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }
  • C#
    public class MeshLoadSaveExample
    {
    public static void Run(string[] args)
    {
    try
    {
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    MR.MeshSave.toAnySupportedFormat(mesh, "mesh.ply");
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }