How to retrieve in Python the mesh created via GUI from a SHAPER object?

Hi everyone,

I’m using SALOME and I create a geometry in the SHAPER module, then mesh it manually through the SMESH GUI. Now, I would like to write a Python script ( that allows me to retrieve the mesh associated with that SHAPER object—without recreating the geometry or remeshing it via code.

In other words:

  • I model the geometry in SHAPER
  • I mesh it via the GUI
  • I want to retrieve the resulting mesh in Python (e.g., to export it, analyze it, or manipulate it)

Is there a way to get the mesh object linked to a given SHAPER shape via Python after doing everything through the GUI?

If possible, I’d appreciate a small example of how to access that mesh programmatically.

Thanks!

import salome
from salome.smesh import smeshBuilder
smesh = smeshBuilder.New()


Mesh = smesh.Mesh(salome.IDToObject(salome.sg.getSelected(0)))

Hi again, thanks for your help!

I tried your suggestion

But unfortunately, I still don’t get the correct mesh.

Here’s what I’m doing more precisely in my code :slight_smile:

  selection = get_selected_entries()
    if not selection:
        _info("Select a geometry first.")
        return

    geom_name, geom_eid = selection[0]

    study     = getStudy()
    geom_sobj = study.FindObjectID(geom_eid)  # SALOMEDS::SObject
    if geom_sobj is None:
        _info("Internal error – selected entry not found in the study.")
        return

    smesh = smeshBuilder.New()
    mesh = smesh.Mesh(salome.IDToObject(selection[0][1]))

    # Fallback test: mesh may exist but not be computed
    if mesh is None or (mesh.NbNodes() == 0 and mesh.NbElements() == 0):
        _info("No *computed* mesh found for the selected geometry.\n"
              "Compute it in SMESH first, then export again.")
        return

The get_selected_entries() function filters the selection to make sure it’s a SHAPER/Geometry object:

def get_selected_entries():
    import libSALOME_Swig
    sg = libSALOME_Swig.SALOMEGUI_Swig()

    try:
        entries = sg.getAllSelected()
    except AttributeError:
        entries = [sg.getSelected(i) for i in range(sg.SelectedCount())]

    study = getStudy()
    results = []
    for entry in entries:
        sobj = study.FindObjectID(entry) if entry else None
        if not sobj:
            continue

        # Go up to the root (Geometry / Shaper / ShaperResults)
        root = sobj
        while True:
            parent = root.GetFather()
            if not parent or not parent.GetName():
                break
            root = parent
        if root.GetName() not in ("Geometry", "Shaper", "ShaperResults"):
            continue

        label = sobj.GetName() or ""
        if label:
            results.append((label, entry))
    return results

Even though the mesh is computed and visible in the GUI (I created and computed it manually in SMESH), the code above doesn’t retrieve the correct mesh — either NbNodes() and NbElements() are 0, or it’s simply not the same mesh.

Is there a better way to access an existing mesh linked to a selected SHAPER object? Should I look for the SMESH object differently?

Thanks again for your time!

wow, it is a SHAPER issue. if you create your geometry in geom and then go to MESH you can use my method to select a geometry or a mesh object in the GUI but having something in shaper breaks this completly.
@NabilG is it worth it to create an issue in the git about this? if yes, in salome GUI? SHAPER?

Thanks a lot for your answer!

In the meantime, I’ll wait and see what happens regarding the SHAPER situation — whether an issue is opened or if a workaround becomes available.

Let me know if you open an issue — I’d be happy to follow or contribute with details.