Overview
The MeshInspector Python API allows you to manage geometry directly — load meshes from disk, add them to the scene, select them programmatically, and export the results.
This functionality lets you automate geometry preparation and file I/O tasks without relying on manual interaction through the user interface.
In this article, you’ll learn how to:
-
Load meshes into memory
-
Add them to the MeshInspector scene
-
Select objects programmatically
-
Export meshes back to files
These operations combine mrmeshpy (for geometry) and mrviewerpy (for scene and UI control) — together, they form the foundation for automated geometry workflows.
Loading Meshes into Memory
To bring a mesh file into MeshInspector’s memory, use the loadMesh() function from mrmeshpy.
from meshlib import mrmeshpy as mm
mesh = mm.loadMesh("your_file_path/Dolphin_original.stl")
print(mesh)
Output Example:
<meshlib.mrmeshpy.Mesh object at 0x12e99e8f0>
At this point, the mesh exists in memory but is not yet visible in the viewer.
The print() output confirms that the mesh was successfully loaded and is ready for further processing.
Adding a Mesh to the Scene
To display a mesh in the viewer, pass the loaded object to addMeshToScene() from mrviewerpy.
from meshlib import mrmeshpy as mm
from meshlib import mrviewerpy as mvmesh = mm.loadMesh("your_file_path/Dolphin_original.stl")
mv.addMeshToScene(mesh, "Dolphin")
mv.Viewer().preciseFitDataViewport()
Explanation:
-
addMeshToScene(mesh, "Dolphin")— adds the mesh to the scene and labels it Dolphin in the scene tree. -
preciseFitDataViewport()— centers and zooms the viewport to fit the mesh in view.
Now the object appears in MeshInspector just as if you opened it manually.
Listing Current UI Elements
To confirm which UI elements and objects are active, list all top-level entries:
from meshlib import mrviewerpy as mv
print(mv.uiListEntries([]))
Output Example:
[<mrmesh.mrviewerpy.UiEntry 'Python' of type 'group'>,
<mrmesh.mrviewerpy.UiEntry 'RibbonTabs' of type 'group'>,
<mrmesh.mrviewerpy.UiEntry 'Toolbar' of type 'group'>,
<mrmesh.mrviewerpy.UiEntry 'Ribbon' of type 'group'>]
This verifies that the viewer environment and scene tree are accessible through the scripting API.
Selecting Objects in the Scene
Meshes added to the scene can be selected programmatically, exactly as if clicked in the interface.
from meshlib import mrviewerpy as mv
# Select the object named "Dolphin"
mv.selectByName("Dolphin")# Check which meshes are currently selected
print(mv.getSelectedMeshes())
Explanation:
-
selectByName("Dolphin")highlights the mesh in the scene tree. -
getSelectedMeshes()returns a Python list of selected mesh objects.
Output Example:
[<meshlib.mrmeshpy.Mesh object at 0x13e60c0d0>]
You can now apply tools, transformations, or repairs directly to these selected meshes.
Exporting Meshes to Files
Once you’ve processed a mesh, you can export it using saveMesh() from mrmeshpy.
from meshlib import mrmeshpy as mm
from meshlib import mrviewerpy as mv# Assume "Dolphin" is already in the scene and selected
selected = mv.getSelectedMeshes()# Save the first selected mesh as an OBJ file
mm.saveMesh(selected[0], "your_file_path/Dolphin_out.obj")print("Export complete.")
Explanation:
-
mv.getSelectedMeshes()returns the currently selected meshes. -
mm.saveMesh(mesh, "path")writes the mesh to the specified file.
Tip:
Use a full file path such as:
/Users/yourname/Desktop/Dolphin_out.obj
You can export to common formats supported by MeshLib (STL, OBJ, PLY, etc.).
Combining the Steps
Here’s a full example that automates loading, adding, selecting, repairing, and exporting a mesh:
from meshlib import mrmeshpy as mm
from meshlib import mrviewerpy as mv# Load and add a mesh
mesh = mm.loadMesh("C:/Data/Dolphin_original.stl")
mv.addMeshToScene(mesh, "Dolphin")
mv.Viewer().preciseFitDataViewport()# Select the object
mv.selectByName("Dolphin")# Open the Mesh Healer tool and run automatic repair
mv.uiPressButton(['RibbonTabs', 'Mesh Repair'])
mv.Viewer().skipFrames(1)
mv.uiPressButton(['Ribbon', 'Mesh Healer'])
mv.Viewer().skipFrames(1)
mv.uiPressButton(['Mesh Healer', 'Repair Selected'])
mv.Viewer().skipFrames(1)# Export the repaired result
selected = mv.getSelectedMeshes()
mm.saveMesh(selected[0], "C:/Data/Dolphin_repaired.obj")
print("Repaired mesh exported successfully.")
This workflow demonstrates how to integrate UI control and mesh operations seamlessly.
Additional Notes
-
Mesh vs. Scene:
loadMesh()only loads into memory — to visualize or manipulate it, useaddMeshToScene(). -
Viewport Control:
preciseFitDataViewport()ensures that newly added meshes are visible. -
Batch Operations: You can automate multi-file workflows by looping through directories and repeating these steps.
-
Compatibility: MeshInspector supports STL, OBJ, PLY, and other formats via MeshLib.
Summary
You’ve learned how to:
-
Load meshes into memory using
mm.loadMesh() -
Add them to the scene with
mv.addMeshToScene() -
Select them programmatically with
mv.selectByName() -
Save the results using
mm.saveMesh()
With these fundamentals, you can now automate file import/export and scene management tasks entirely from Python.
Next, in Advanced Automation, you’ll learn how to chain these steps into repeatable workflows and integrate MeshInspector scripting with the MeshLib SDK for deeper functionality.