Skip to content

Instantly share code, notes, and snippets.

@hathawsh
Last active November 17, 2017 15:29
Show Gist options
  • Save hathawsh/29c0a6629ec33d819bd951e07a50d182 to your computer and use it in GitHub Desktop.
Save hathawsh/29c0a6629ec33d819bd951e07a50d182 to your computer and use it in GitHub Desktop.
Alter Original Prusa i3 MMU tool changes to stop internal stringing
#!/usr/bin/env python
"""Alter Original Prusa i3 MMU tool changes to stop internal stringing.
This changes the filament unload procedure so that right after pulling out,
the extruder pauses (for a little cooling), pushes back in
(to compress the string), and pulls out again (to break the string.)
See: https://github.com/prusa3d/Slic3r/issues/396
"""
import logging
import os.path
import re
import sys
log = logging.getLogger('reram')
unload_comment_re = re.compile(r'; CP TOOLCHANGE UNLOAD')
pull_re = re.compile(r'^G1 E-15\.0000(\s+F\d{4,})?$')
def main():
logging.basicConfig(level=logging.INFO, file=sys.stderr)
if len(sys.argv) >= 2:
# Read from the filename specified in the first arg.
infn = sys.argv[1]
infile = open(infn, 'rt')
outfn = infn + '.tmp'
outfile = open(outfn, 'wt')
else:
# Read from stdin and write to stdout.
infile = sys.stdin
infn = None
outfile = sys.stdout
outfn = None
unload_line_no = None
line_no = 0
change_count = 0
write = outfile.write
for line in infile:
line_no += 1
if unload_comment_re.match(line):
unload_line_no = line_no
match = pull_re.match(line)
if match is not None and unload_line_no is not None:
speed_arg = match.group(1) or ''
# Pull out.
write('G1 E-12.0000%s\n' % speed_arg)
# Pause for a short time for cooling.
write('G4 P400\n')
# Ram back in to compress the string.
write('G1 E12.0000%s\n' % speed_arg)
# Pull out again, hopefully breaking the string.
write('G1 E-15.0000%s\n' % speed_arg)
change_count += 1
# Don't match again until we see the toolchange unload comment.
unload_line_no = None
else:
write(line)
infile.close()
outfile.close()
# Edit the file in place so this script can be used as a Slic3r
# post-processing script. See:
# http://manual.slic3r.org/advanced/post-processing
if infn and outfn:
os.rename(outfn, infn)
log.info('%s changes made', change_count)
if __name__ == '__main__':
main()
#!/bin/sh
unset PYTHONHOME
unset PYTHONPATH
exec /usr/bin/python "$(dirname $0)/reram.py" "$1"
@hathawsh
Copy link
Author

The script can now be used as a Slic3r post-processing script. I also added the reram.sh wrapper; it's required on Linux because the Prusa Slic3r appimage creates a broken environment for running Python code.

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