Metadata-Version: 2.4
Name: gcode2mesh
Version: 0.1.0
Summary: Reconstruct a 3D mesh (STL, OBJ, GLB) from G-code toolpaths — FDM and CNC dialects.
Author-email: MiConvert <dev@miconvert.com>
License: MIT
Project-URL: Homepage, https://miconvert.com/en/gcode-to-stl?utm_source=pypi&utm_medium=listing&utm_campaign=gcode2mesh
Project-URL: Convert online, https://miconvert.com/en/gcode-to-3mf?utm_source=pypi&utm_medium=listing&utm_campaign=gcode2mesh
Keywords: gcode,g-code,stl,obj,glb,mesh,3d-printing,cnc,toolpath,trimesh
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Manufacturing
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Multimedia :: Graphics :: 3D Modeling
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.20
Requires-Dist: trimesh>=3.9
Provides-Extra: voxel
Requires-Dist: scipy>=1.7; extra == "voxel"
Requires-Dist: scikit-image>=0.19; extra == "voxel"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"

# gcode2mesh

Reconstruct a 3D mesh — STL, OBJ, GLB, PLY — from G-code toolpaths.

G-code is a list of machine movements, not a geometry format. There is no model
inside the file to extract, so the shape has to be inferred from where the
machine was told to go. That makes this a **reconstruction**, not a conversion:
the output approximates the printed or machined part, and it will not match the
model the slicer started from.

```bash
pip install gcode2mesh          # tube reconstruction
pip install "gcode2mesh[voxel]" # solid reconstruction (recommended)
```

```bash
gcode2mesh benchy.gcode benchy.stl
gcode2mesh pocket.nc pocket.glb --radius 0.5
```

```python
from gcode2mesh import convert, parse_paths, paths_to_mesh

convert("benchy.gcode", "benchy.stl", radius=0.2)

# or work with the geometry directly
paths = parse_paths("benchy.gcode")   # list of [x, y, z] polylines
mesh = paths_to_mesh(paths)           # a trimesh.Trimesh
```

## Two dialects, detected automatically

Nothing in a G-code file declares which kind it is, so `gcode2mesh` sniffs for a
real `E` word before deciding how to read it.

| | How material is identified | What you get |
|---|---|---|
| **FDM / 3D printing** | moves where the `E` axis advances | the printed object, travel moves discarded |
| **CNC / subtractive** | every non-rapid move | a visualisation of the cutting path |

Getting this wrong is not subtle. Treat a CNC file as FDM and every move looks
like a travel move, leaving nothing at all; treat an FDM file as CNC and the
model comes out webbed together by lines that were never printed. Detection
deliberately requires `E` to be followed by a number on a non-comment line —
CNC headers contain comments like `;ModelNumber=` and `;ActualInstep=`, and a
bare letter match on those produced empty output.

## How the mesh is built

With `scipy` and `scikit-image` installed (the `voxel` extra), paths are
rasterised into a grid, a distance transform measures the distance to the
nearest deposited point, and marching cubes takes the surface at `radius`.
Overlapping beads fuse into a single shell, which is what a printed part looks
like.

Without them, each segment is swept into its own tube and the tubes are
concatenated. It works with `trimesh` alone, but beads do not merge — expect a
bundle of touching tubes rather than a solid.

`--radius` is half the deposited bead width in the file's own units, usually mm.
The default of `0.2` suits a 0.4 mm nozzle. Raise it for CNC files, where the
tool is much wider than a nozzle.

## Limitations

- Arc moves (`G2`/`G3`) are treated as straight segments between endpoints.
- Multi-material and multi-extruder files are flattened into one mesh.
- Relative extrusion (`M83`) is not tracked; absolute `E` is assumed.
- The tube fallback keeps only the 2000 longest paths on very large files.

## License

MIT.

---

Built by [MiConvert](https://miconvert.com/en/gcode-to-stl?utm_source=pypi&utm_medium=readme&utm_campaign=gcode2mesh),
which runs the same reconstruction in the browser if you would rather not
install anything —
[G-code to STL](https://miconvert.com/en/gcode-to-stl?utm_source=pypi&utm_medium=readme&utm_campaign=gcode2mesh) ·
[G-code to 3MF](https://miconvert.com/en/gcode-to-3mf?utm_source=pypi&utm_medium=readme&utm_campaign=gcode2mesh)
