Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Created August 12, 2025 21:36
Show Gist options
  • Save stephenfeather/b53d67525574c58845ffe92aa2dbe434 to your computer and use it in GitHub Desktop.
Save stephenfeather/b53d67525574c58845ffe92aa2dbe434 to your computer and use it in GitHub Desktop.
Little example that parses gcode to pull out tool changes. This information can be used to manually weld filament together for multi-color prints without an AMS
from collections import defaultdict
def parse_gcode(path):
usage = defaultdict(float)
curr_t = None
prev_e = 0.0
with open(path) as f:
for line in f:
if line.startswith('T') and line[1].isdigit():
curr_t = line.strip().split()[0]
elif 'G1' in line and 'E' in line:
parts = line.split()
for p in parts:
if p.startswith('E'):
e = float(p[1:])
if curr_t is not None:
usage[curr_t] += max(0, e - prev_e)
prev_e = e
break
return usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment