Skip to main content

Parse, analyse, and transform G-code files — plain-text .gcode and Prusa .bgcode

Project description

gcode-lib

A general-purpose Python library for parsing, analysing, and transforming G-code files. Supports both plain-text .gcode and Prusa binary .bgcode formats, with a full planar post-processing toolkit optimised for PrusaSlicer FDM workflows.

Requirements: Python 3.10+ · stdlib only (no third-party dependencies)


Installation

Copy gcode_lib.py into your project (or onto PYTHONPATH). No package installation required.

cp gcode_lib.py /your/project/

Quick start

import gcode_lib as gl

# Load any .gcode or .bgcode file
gf = gl.load("benchy.gcode")

# Inspect basic statistics
stats = gl.compute_stats(gf.lines)
print(f"Moves: {stats.move_count}")
print(f"Layers: {stats.layer_count}")
print(f"Total extrusion: {stats.total_extrusion:.2f} mm")

bounds = stats.bounds
print(f"Print size: {bounds.width:.1f} x {bounds.height:.1f} mm")
print(f"Centred at: ({bounds.center_x:.1f}, {bounds.center_y:.1f})")

# Shift the print 10 mm to the right, 5 mm forward (arc-safe, no linearization needed)
lines = gl.translate_xy_allow_arcs(gf.lines, dx=10.0, dy=5.0)
gf.lines = lines

gl.save(gf, "benchy_shifted.gcode")

Features

  • Loading and savingload() / save() for .gcode and .bgcode, from_text() / to_text(), line and word parsing
  • State trackingModalState, advance_state(), iterators for moves, arcs, and extruding segments
  • Transforms — arc linearization, translate, rotate, skew, arbitrary XY transform, layer-selective transforms, G91→G90 conversion, transform analysis
  • Bed placement — out-of-bounds detection, recenter/fit to bed
  • Statistics — bounding box, move/arc/travel counts, layer iteration
  • Presets — printer bed dimensions and filament parameters for Prusa printers, auto-detection from G-code
  • Utilities — template rendering, thumbnail encoding
  • Binary .bgcode — full read/write support including DEFLATE, Heatshrink, and MeatPack
  • PrusaSlicer CLI — executable discovery, capability probing, single and batch slicing, slicer compatibility notes
  • API reference — complete function signatures, data classes, and constants

Concepts

GCodeLine

Every line of text becomes a GCodeLine:

line = gl.parse_line("G1 X10.5 Y20.0 E0.12345 F3600 ; perimeter")
print(line.command)          # "G1"
print(line.words)            # {"X": 10.5, "Y": 20.0, "E": 0.12345, "F": 3600.0}
print(line.comment)          # "; perimeter"
print(line.raw)              # "G1 X10.5 Y20.0 E0.12345 F3600 ; perimeter"
print(line.is_move)          # True  (G0 or G1)
print(line.is_arc)           # False
print(line.is_blank)         # False

ModalState

G-code is stateful. ModalState tracks the printer's current mode and position:

state = gl.ModalState()
print(state.abs_xy)    # True  (G90 absolute XY by default)
print(state.abs_e)     # True  (M82 absolute E by default)
print(state.x, state.y, state.z)   # 0.0, 0.0, 0.0

GCodeFile

The top-level container returned by load() or from_text():

gf = gl.load("print.gcode")
print(gf.source_format)         # "text" or "bgcode"
print(len(gf.lines))            # number of GCodeLine objects
print(len(gf.thumbnails))       # populated for .bgcode and plain-text files with embedded thumbnails

Limitations

  • G91 relative XY in transforms: apply_xy_transform, apply_skew, translate_xy, and translate_xy_allow_arcs raise ValueError if a G0/G1 move with X/Y words is encountered while the modal state is in G91 mode. Use to_absolute_xy() to convert relative segments to absolute before transforming — see G91 and relative-mode handling.
  • Arc endpoint tracking only. advance_state updates position to the G2/G3 endpoint but does not interpolate intermediate arc positions. Use linearize_arcs if you need full path coverage.
  • No helical arc support. G2/G3 with a simultaneous Z move (helical arcs) are not supported. All arcs are treated as planar (XY only).
  • No G-code validation. The library parses and transforms; it does not validate that the resulting G-code is printable or within machine limits. Use find_oob_moves for basic bed boundary checking.

Running the tests

python -m pytest tests/ -v

Tests cover I/O, parsing, state tracking, statistics, all transform functions, bed placement, layer iteration, presets, preset detection, template rendering, thumbnail encoding, BGCode round-trips (including Heatshrink decompression and MeatPack decoding), and PrusaSlicer CLI helpers — using only stdlib and pytest.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcode_lib-1.1.1.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gcode_lib-1.1.1-py3-none-any.whl (71.0 kB view details)

Uploaded Python 3

File details

Details for the file gcode_lib-1.1.1.tar.gz.

File metadata

  • Download URL: gcode_lib-1.1.1.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for gcode_lib-1.1.1.tar.gz
Algorithm Hash digest
SHA256 3e1c7f24a9963a5537fcdc1530882006d815f56138660e64cc6967928e634cfd
MD5 080ad22585511cc1654415774f103f1e
BLAKE2b-256 4873eb19535c3664e50a1ba11d894e802baf2b0bfe16b83e340ade0a1bc693f3

See more details on using hashes here.

File details

Details for the file gcode_lib-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: gcode_lib-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 71.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for gcode_lib-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89d42f92ba09299ee0fa92c216d41887e382f0630d2613ed5eaaad363ae11605
MD5 82a07f0fa453b6f6db09f2b57cc352c6
BLAKE2b-256 ccd78236b10c9b4a01abecf6bce1626d625f1689e2e4b9dcca925c1c22806ffc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page