Commonplace kernel API
    Preparing search index...

    Save and load

    A kernel model saves as plain JSON and loads back with every entity under the id it had. A host stores the JSON however it likes: a file, a database row, a request body.

    import { loadMesh, saveMesh } from '@commonplace-technologies/kernel';

    const text = JSON.stringify(saveMesh(mesh));

    const loaded = loadMesh(JSON.parse(text));
    if (loaded.ok) use(loaded.mesh);
    else console.log(loaded.error.code, loaded.error.message);

    saveMesh(mesh) returns a MeshDocument:

    {
    "format": "commonplace-kernel-mesh",
    "formatVersion": "2026-05-06",
    "mesh": { "vertices": [ ... ], "halfEdges": [ ... ], "faces": [ ... ] }
    }
    Field Meaning
    format Always "commonplace-kernel-mesh" (MESH_DOCUMENT_FORMAT).
    formatVersion The shape of mesh, an ISO date (MESH_FORMAT_VERSION).
    mesh HalfEdgeMesh.toJSON(): every vertex, half-edge and face, with the curve each edge lies on and the surface each face lies on.

    The mesh payload is the one the host application stores under each definition's geometry in its own documents, so a host document's geometry and a kernel document's mesh are interchangeable. Its records are records of vertices with positions, half-edges with their twin, loop and ring links, faces with their outline and holes, and the exact curve and surface records (arcs, Béziers, rails, developable and NURBS surfaces). Positions are Z up and unitless; the host says what a unit is.

    formatVersion versions the mesh records. A host application stores the same records, so the two move together. It changes only when the shape of the records needs a forward migration; a new optional key (a new curve kind, a tag) is added without a bump, and an older kernel reading it ignores the key.

    loadMesh reads:

    • the current version and every earlier one, migrating older shapes on the way in;
    • a bare toJSON() payload with no wrapper, as found in a host document's geometry.

    A document from a newer kernel is refused (newer-format) rather than read in part.

    loadMesh never throws. It returns { ok: true, mesh } or { ok: false, error: { code, message } }:

    code When
    not-a-mesh Not a mesh document (another format, no formatVersion) and not a bare payload with vertices, halfEdges and faces arrays.
    newer-format Saved by a newer kernel, in a shape this one does not know.
    unreadable The records do not join up: a half-edge names a vertex the document does not hold, or rebuilding the links fails.

    A load checks that the records make a mesh, not that the mesh is a valid model. For a document from outside your own program, follow the load with validate(loaded.mesh) and decide what to do with its problems.

    • Ids. Every vertex, half-edge and face keeps its id, so selections, annotations or links a host stores beside the document still resolve after a load. An edge's canonical id (the smaller of its two half-edge ids, as kernelQuery names it) is stable for the same reason.
    • Geometry. Positions, curves and surfaces are written as numbers, not re-derived, and saveMesh(loadMesh(doc).mesh) writes the same JSON again.
    • Order. Records are written in the mesh's own order and loaded back in it, so iteration order (and the order of a tessellation frame) is the same after a load.
    • Undo history. A loaded mesh starts with an empty history: make a fresh meshVerbContext(loaded.mesh) for it.
    • Anything of the host's. Scenes, placements, cameras, selections, materials beyond a face's color and tags: those go in the host's own document, beside the mesh, keyed by the kernel's ids.