Skip to main content

Python G-Code Tools library with complete* G-Code Reader and Writer

*as per 3D-Printing needs

This library is under development - method names, workflow and logic will differ between releases!

Ensure your printer software can catch illegal g-code moves, as this library has still very large amount of bugs! Also keep an eye on your print.

Installation

pip install GcodeTools

Available G-Code Tools

Feature Status command
Translate Gcode ✅ Tools.translate(gcode, Vector)
Rotate Gcode ✅ Tools.rotate(gcode, int)
Scale Gcode ✅ Tools.scale(gcode, Vector|float)
subdivide Gcode ✅ move.subdivide(step)
Get move's flowrate ✅ move.get_flowrate()
Set flowrate
(in mm^2, use scale to set in %)
✅ move.set_flowrate(float)
Detect Gcode features ✅ block.layer, block.object, block.move_type
Split layers ✅ Gcode.layers[n]
Split bodies 🔜 Tools.split(gcode)
Insert custom Gcode ✅ Gcode.(insert, append, extend, __add__)
Read Thumbnails (raw PNG data) ✅ Tools.read_thumbnails(gcode)
Write Thumbnails (raw PNG data) ✅ Tools.write_thumbnail(gcode, data, width, height, textwidth)
Generate configuration files for slicer ✅ Tools.generate_config_files(gcode)
Convert from/to Arc Moves ❌ currently auto-translation to G1 in GcodeParser
Find body bounds ✅ Tools.get_bounding_box(gcode)
Trim unused Gcode 🔜 Tools.trim(gcode)
Offset Gcodes in time ❌
Create custom travel movement ❌
convert to firmware retraction 🔜 Tools.regenerate_travels(gcode)

Legend:

  • ✅ Fully supported
  • ❌ Not yet supported, to be implemented
  • 🔜 Partially supported, to be implemented

More features soon! Feel free to open feature request

G-Code

Current G-Code object relation:

Gcode (list[Block])
│
├─ slicing config (precision, speed): Config
│
├─ single Gcode instruction: Block
│  │
│  ├─ Position: Vector
│  │
│  ├─ Other Gcode related properties
│  │
│  └─ Original command and if it's to be emitted: command, emit_command
└─ ...

In each block, every G-Code variable is contained. That means, blocks can be taken out of Gcode, rearranged, etc.

That however does not take move origin (move starting position) in count! That will be adressed in future.

Gcode structure and its components will be changing heavily during beta!

  • Current target is to get rid of original command (work on trimmed Gcode) to decrease RAM usage and computation time
  • Gcode is in the first tests of linked-list approach for simplification of iterating methods

G-Code Parser

from GcodeTools import Gcode

gcode = Gcode('file.gcode')

Progress Callback example implementation

my_tqdm = tqdm(unit="lines", desc="Reading Gcode")
update = lambda i, length: (setattr(my_tqdm, 'total', length), my_tqdm.update(1))
gcode = Gcode().from_file('file.gcode', update)

Example usage

Example to move objects that have benchy in their name, by translation vector. It will also trim gcode (minify).

from GcodeTools import Gcode, Tools, Vector, Config

verbose = False

config = Config(speed=1200) # initial speed before first Gcode's `F` parameter

gcode = Gcode('file.gcode', config=config)

out_gcode: Gcode = Tools.trim(gcode)

translation = Vector(-200, -100, 0)

for x in out_gcode:
    if 'benchy' in x.object.lower():
        x.move.translate(translation)

out_gcode.write_file('out.gcode', verbose)

Change tool to T1 when printing sparse infill, otherwise change to T0. For bridges set fan speed to 100%.

from GcodeTools import *

gcode = Gcode('file.gcode')

for block in gcode:
    if block.move_type == MoveTypes.SPARSE_INFILL:
        block.T = 1
    else:
        block.T = 0
    
    if block.move_type == MoveTypes.BRIDGE:
        block.fan = 255

gcode.write_file('out.gcode')

Plot histogram of flow ratios. Useful for checking arachne settings.

from GcodeTools import Gcode, Tools
import matplotlib.pyplot as plt

gcode = Gcode('file.gcode')

flowrates = []
for i in range(len(gcode)):
    if flowrate := Tools.get_flowrate(gcode, i):
        flowrates.append(flowrate)

plt.figure(figsize=(12, 6))
plt.hist(flowrates, bins=100)
plt.xlabel("Flowrate (mm E / mm XYZ)")
plt.ylabel("Frequency")
plt.title(f"Flowrate Distribution")
plt.grid(axis='y', alpha=0.75)
plt.show()
plt.close()

Generate configuration files for slicer

gcode = Gcode('gcode.gcode')

config = Tools.generate_config_files(gcode, './config')

Generate thumbnails (experimental, and in current version slow)

# For this feature you need to install additional package:
# pip install GcodeTools[thumbnails]

# If this code fails, try setting `backend` to one of: ['openGL3_glfw', 'openGL3_egl', 'openGL_mock']

backend = 'auto'

import GcodeTools
from GcodeTools.Thumbnails import gcode_thumbnails

gcode = Gcode('gcode.gcode')

img = gcode_thumbnails.Thumbnails.generate_thumbnail(gcode, backend=backend)
thumb_gcode = gcode_thumbnails.Thumbnails.set_thumbnail(gcode, img)

img.save('thumb.png')
thumb_gcode.write_file('out.gcode')

# gcode_thumbnails.Thumbnails.interactive(gcode, color_moves = True, backend=backend) # to open gcode viewer

Supported Slicers

Tested with:

  • Prusa Slicer 2.8.1, 2.9.3
  • Orca Slicer 2.1.1
  • Super Slicer 2.5.59.12, 2.7.61.10
  • Slic3r 1.3.0
  • Cura 5.8.1
  • Simplify3D 4.0.0
  • Bambu Studio 2.0.3.54
Any slicer Cura Prusa Slicer Orca Slicer Slic3r Super Slicer Simplify3D Bambu Studio
Reading Gcode ✅
Keep track of coordinates ✅
Temperature control ✅
Fan control ✅
Spliting Objects ➖ ✅ ✅1 ✅ ❌ ✅ ✅ ✅
Extracting features ➖ ➖ ✅ ✅ ❌ ✅ ✅ ✅
Arc Moves 🔜2

Legend:

1: Turn on LABEL_OBJECTS
2: Arc moves currently automatically translate to G1 moves

  • ✅ Fully supported
  • ❌ Not supported, limited by slicer
  • 🔜 To be implemented
  • ➖ Partially supported, limited by slicer

Metadata

Release files for GcodeTools 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for GcodeTools 0.3.2
File Size Uploaded Provenance Actions
gcodetools-0.3.2.tar.gz 26.1 kB With provenance Details for gcodetools-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for GcodeTools 0.3.2
File Interpreter ABI Platform Actions
gcodetools-0.3.2-py3-none-any.whl Python 3 none any Details for gcodetools-0.3.2-py3-none-any.whl

Total release size: 49.5 kB

Release files / gcodetools-0.3.2.tar.gz

Download URL gcodetools-0.3.2.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
91ba540c40cb6a91b45110fae27194b3ebafb3d35227ffc3ac5a43419bc0f78e
BLAKE2b-256 checksum
How to use checksums
bb7f8637d8320c5d69d51561f358c58b7855712da3e25782d273896a80a416c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 22, 2026.

Transparency log

Release files / gcodetools-0.3.2-py3-none-any.whl

Download URL gcodetools-0.3.2-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2f8e24ab72e294af5a2e83638d065dc412d16de71022692efa82548958d5548
BLAKE2b-256 checksum
How to use checksums
4bdfd83d97293479fdb81bed3b7d1f73b542db7b7291abf80e26cca86d0d7b25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 22, 2026.

Transparency log

Security for GcodeTools 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for gcodetools-0.3.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page