Skip to content

Instantly share code, notes, and snippets.

@seancmonahan
Last active October 16, 2024 21:05
Show Gist options
  • Save seancmonahan/05f92505162a7081b1539686ad33f535 to your computer and use it in GitHub Desktop.
Save seancmonahan/05f92505162a7081b1539686ad33f535 to your computer and use it in GitHub Desktop.
G-code postprocessing script to modify first layer travel speed
#!/usr/bin/env python3
# Copyright (c) Sean C. Monahan
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# (MIT License)
"""in Post-processing scripts, put:
{full-path-to-this-file} {your-desired-first-layer-travel-speed-in-mm-per-second}
(On Windows, I think it would be
{path-to-your-python-interpreter} {full-path-to-this-file} {your-desired-first-layer-travel-speed-in-mm-per-second}
)
This will modify the gcode output to change first layer travel moves to use the specified speed.
This is hacky as heck. It _should_ still work if you enable verbose gcode output.
This will probably break if the travel speed set in SuperSlicer itself is identical to any first layer
extrusion speeds (unlikely) because it will omit redundant G1 F parameters.
This could be refactored to be able to apply any transformer function to an arbitrary layer
This does not function correctly if "Complete individual objects" is enabled, because it doesn't handle
the resulting multiple "first layers."
"""
from os import environ, replace
from pathlib import Path
from sys import argv
from tempfile import NamedTemporaryFile
def main(argv=argv):
first_layer_accel = environ['SLIC3R_FIRST_LAYER_ACCELERATION']
travel_speed = 60 * int(environ["SLIC3R_TRAVEL_SPEED"]) # G1 moves speeds are in mm per minute
new_first_layer_travel_speed = 60 * int(argv[-2])
input_gcode_filename = argv[-1]
def adjust_speed_if_travel_move(line: str) -> str:
# for matching purposes, strip whitespace and any comment
trimmed = line.split(";", 1)[0].strip()
if (
(trimmed.startswith("G1 ") or trimmed.startswith("G0 "))
and trimmed.endswith(f"F{travel_speed}")
and ("Y" in trimmed or "X" in trimmed)
and "E" not in trimmed
):
return line.replace(f"F{travel_speed}", f"F{new_first_layer_travel_speed}")
if trimmed.startswith("SET_ACCEL") or trimmed.startswith("M204"):
return f";{line}"
return line
with NamedTemporaryFile(
mode="w+t",
prefix="limit_first_layer_travel_speed_output",
dir=str(Path(input_gcode_filename).parent),
delete=False,
) as temp_gcode:
temp_gcode_filename = temp_gcode.name
with open(input_gcode_filename) as input_gcode:
acceleration_lines = [
f"M204 S{environ['SLIC3R_DEFAULT_ACCELERATION']} ; explicitly set default acceleration in case I fubarred something\n"
]
# pass through unchanged all the lines until the first ';LAYER_CHANGE'
for line in input_gcode:
temp_gcode.write(line)
if line.startswith("SET_ACCEL") or line.startswith("M204"):
acceleration_lines.append(line)
if line == ";LAYER_CHANGE\n":
break
# ensure that the first layer acceleration is set even though all following
# acceleration limits will be elided until the second layer
temp_gcode.write(f"M204 S{first_layer_accel}\n")
# now we're in the first layer, until we hit the next ';LAYER_CHANGE'
for line in input_gcode:
temp_gcode.write(adjust_speed_if_travel_move(line))
if line.startswith("SET_ACCEL") or line.startswith("M204"):
acceleration_lines.append(line)
if line == ";LAYER_CHANGE\n":
break
# Replay all of the bundled up first layer acceleration limit adjustments,
# assuming that the machine limits configured during the first layer
# cummulatively will yield the same state as long as their order is retained
temp_gcode.writelines(acceleration_lines)
# pass the rest of the lines through unchanged
for line in input_gcode:
temp_gcode.write(line)
# add final comment lines stating that we rewrote first layer travel moves and acceleration limits
temp_gcode.write(
f"; first layer travel moves changed from {travel_speed} to {new_first_layer_travel_speed}\n"
f"; first layer accelerations limited to {first_layer_accel}\n"
)
# close, then move using os.replace (NOT rename)
temp_gcode.close()
replace(temp_gcode_filename, input_gcode_filename)
if __name__ == "__main__":
main()
@Henk72
Copy link

Henk72 commented Jan 18, 2023

Thank you, very helpful!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment