Skip to content

Instantly share code, notes, and snippets.

@seancmonahan
Created May 30, 2022 20:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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) 2022 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
"""
from sys import argv
from os import environ
import shutil
from tempfile import NamedTemporaryFile
def main(argv=argv):
# note: G1 moves speeds are in mm per minute, so multiply by 60 seconds-per-minute
travel_speed = 60 * int(environ["SLIC3R_TRAVEL_SPEED"])
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{first_layer_travel_speed}")
return line
with NamedTemporaryFile(
mode="w+t",
prefix="limit_first_layer_travel_speed_output",
delete=False,
) as output_gcode, open(input_gcode_filename) as input_gcode:
output_gcode_filename = output_gcode.name
# pass through unchanged all the lines until the first ';LAYER_CHANGE'
for line in input_gcode:
output_gcode.write(line)
if line == ";LAYER_CHANGE\n":
break
# now we're in the first layer, until we hit the next ';LAYER_CHANGE'
for line in input_gcode:
output_gcode.write(adjust_speed_if_travel_move(line))
if line == ";LAYER_CHANGE\n":
break
# pass the rest of the lines through unchanged
for line in input_gcode:
output_gcode.write(line)
# add one final comment line stating that we rewrote first layer travel moves
output_gcode.write(
f"; first layer travel moves changed from {travel_speed} to {first_layer_travel_speed}\n"
)
# replace the original gcode file with our modified gcode
shutil.move(output_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