Skip to content

Package API

The top-level gmshparser namespace contains the supported entry points, modern value types, structured errors, element metadata, and compatibility aliases most applications need.

Entry points

gmshparser.read(source, *, name=None)

Read a path or text stream into the modern API.

Top-level :func:gmshparser.parse intentionally retains the mutable compatibility API. Within :mod:gmshparser.api, :func:parse is an alias for this modern reader.

Source code in gmshparser/api.py
def read(
    source: str | os.PathLike[str] | TextIO,
    *,
    name: str | None = None,
) -> Mesh:
    """Read a path or text stream into the modern API.

    Top-level :func:`gmshparser.parse` intentionally retains the mutable
    compatibility API. Within :mod:`gmshparser.api`, :func:`parse` is an alias
    for this modern reader.
    """
    if hasattr(source, "read"):
        stream = cast(TextIO, source)
        mesh_name = name or str(getattr(stream, "name", "<stream>"))
        return _read_stream(stream, mesh_name)

    path = os.fspath(source)
    with open(path, encoding="utf-8") as stream:
        return _read_stream(stream, name or path)

gmshparser.parse(filename)

Parse a file into the compatibility data model.

The compatibility model preserves the original get_* and set_* API. New code should normally use :func:read, which returns the modern, immutable model from :mod:gmshparser.api.

Source code in gmshparser/__init__.py
def parse(filename: str) -> Mesh:
    """Parse a file into the compatibility data model.

    The compatibility model preserves the original ``get_*`` and ``set_*`` API.
    New code should normally use :func:`read`, which returns the modern,
    immutable model from :mod:`gmshparser.api`.
    """
    mesh = Mesh()
    mesh.set_name(filename)
    parser = MainParser()
    with open(filename, encoding="utf-8") as io:
        parser.parse(mesh, io)
    return mesh

read() returns the modern immutable gmshparser.api.Mesh. parse() retains the original mutable gmshparser.mesh.Mesh behavior.

Package metadata

gmshparser.__version__ is read from the installed distribution metadata. gmshparser.__author__ identifies the package author.

import gmshparser

print(gmshparser.__version__)

Top-level modern exports

The following modern types are available directly from gmshparser and canonically defined in gmshparser.api:

  • ModernMesh
  • Version
  • Node and NodeCollection
  • Element and ElementCollection
  • Entity and EntityCollection
  • PhysicalGroup and PhysicalGroupCollection
  • PeriodicLink and PeriodicLinkCollection

See Modern API for their complete members.

Element metadata

The top-level namespace exports ElementType, ElementFamily, and ElementTypeInfo. They are documented with the modern model because elements and collection filters use them throughout the public API.

Errors

All public parser errors are available directly from gmshparser, including ParseError and its specialized subclasses. See Errors.

Compatibility exports

gmshparser.Mesh is the original mutable model. gmshparser.MainParser, gmshparser.MshFormatVersion, and gmshparser.VersionManager remain available for compatibility and parser development. See Compatibility API and Parser Internals.

Submodules

  • gmshparser.api — modern immutable model and modern parse() alias
  • gmshparser.numpy — optional NumPy conversion
  • gmshparser.helpers — 2D visualization adapters and line parsers
  • gmshparser.errors — structured error hierarchy