Skip to content

Instantly share code, notes, and snippets.

@matteoferla
Created August 24, 2023 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteoferla/5b867dd3fb8627213319f228467b7686 to your computer and use it in GitHub Desktop.
Save matteoferla/5b867dd3fb8627213319f228467b7686 to your computer and use it in GitHub Desktop.
Add random M104 codes to a gcode for a 3D print in wood PLA to make stripes (old script, may be better out there)
# Randomize layer temperatures for wood PLA.
import argparse
import re
import random
import os
gcode_folder = '../../3DPrints/gcodes/'
filename = 'scrabble_hold_10v3.gcode'
assert os.path.splitext(filename)[1] == '.gcode'
tmin:int=190
tmax:int=220
Zpattern = re.compile("^[^;]*(G(0|1)).*Z.*$")
Tpattern = re.compile("^[^;]*(M104|M109).*")
# ---------
layerchange = False
with open(os.path.join(gcode_folder, filename), 'r') as fh:
lines = fh.read().splitlines()
fh = open(os.path.join(gcode_folder, filename.replace('.gcode', f'.wooble{tmin}-{tmax}.gcode')), 'w')
for line in lines:
if layerchange == True:
layerchange = False
if not Tpattern.match(line):
fh.write(f"M104 S{random.randint(tmin, tmax)}\n")
if layerchange == False and Zpattern.match( line ):
layerchange = True
fh.write(f"{line}\n")
fh.close()
# -------------
layerchange = False
with open(os.path.join(gcode_folder, filename), 'r') as fh:
lines = fh.read().splitlines()
fh = open(os.path.join(gcode_folder, filename.replace('.gcode', f'.wobble{tmin}-{tmax}.v2.gcode')), 'w')
tally = 0
for line in lines:
if layerchange == True or tally == 200:
layerchange = False
tally = 0
if not Tpattern.match(line):
fh.write(f"M104 S{random.randint(tmin, tmax)}\n")
if layerchange == False and Zpattern.match( line ):
layerchange = True
tally += 1
fh.write(f"{line}\n")
fh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment