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 <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;
    }
    MRMESH_API Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    MRIOEXTRAS_API Expected< void > toCtm(const Mesh &mesh, const std::filesystem::path &file, const CtmSaveOptions &options)
    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)
    None saveMesh(Mesh mesh, os.PathLike|str|bytes file, SaveSettings settings='{}')
    Mesh loadMesh(os.PathLike|str|bytes file, MeshLoadSettings settings='{}')
  • C
    #include <MRMeshC/MRMesh.h>
    #include <MRMeshC/MRMeshLoad.h>
    #include <MRMeshC/MRMeshSave.h>
    #include <MRMeshC/MRMeshTopology.h>
    #include <MRMeshC/MRString.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( int argc, char* argv[] )
    {
    int rc = EXIT_FAILURE;
    // error messages will be stored here
    MRString* errorString = NULL;
    // Load mesh
    MRMesh* mesh = mrMeshLoadFromAnySupportedFormat( "mesh.stl", &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to load mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out;
    }
    // Save mesh
    mrMeshSaveToAnySupportedFormat( mesh, "mesh.ply", &saveSettings, &errorString);
    if ( errorString )
    {
    fprintf( stderr, "Failed to save mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out_mesh;
    }
    rc = EXIT_SUCCESS;
    out_mesh:
    mrMeshFree( mesh );
    out:
    return rc;
    }
    struct MRMesh MRMesh
    typedefMR_EXTERN_C_BEGIN struct MRString MRString
    MR_EXTERN_C_BEGIN MRMESHC_API MRMesh * mrMeshLoadFromAnySupportedFormat(const char *file, MRString **errorStr)
    MR_EXTERN_C_BEGIN MRMESHC_API void mrMeshSaveToAnySupportedFormat(const MRMesh *mesh, const char *file, const MRSaveSettings *settings, MRString **errorStr)
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    MRMESHC_API MRSaveSettings mrSaveSettingsNew(void)
    MRMESHC_API void mrStringFree(MRString *str)
    MR_EXTERN_C_BEGIN MRMESHC_API const char * mrStringData(const MRString *str)