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;
    }
    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)
  • C