Skip to content

Instantly share code, notes, and snippets.

@ApexArray
Last active March 3, 2024 22:33
Show Gist options
  • Save ApexArray/038f7c515b526f8e8144fa61f516112a to your computer and use it in GitHub Desktop.
Save ApexArray/038f7c515b526f8e8144fa61f516112a to your computer and use it in GitHub Desktop.
PrusaSlicer: Set EM rate by file name (for Ellis' print tuning guide)

Set EM rates by file name

Summary

Ellis' Print Tuning Guide provides a great process for tuning Extrusion Multipler (EM), but I recently moved back to PrusaSlicer (RIP SuperSlicer) and I didn't want to print the objects one at a time (as suggested in the guide).

This script simplifies the process by automatically adjusting EM according to each file/object name.

How it works

PrusaSlicer's Verbose G-Code feature adds comments to the gcode file indicating the start/stop of each object. This script finds those comments and inserts a subsequent M221 command so that each object is printed with the correct EM.

For example, EM_Cube-1.000.stl prints at 100% EM, EM_Cube-0.995.stl prints at 99.5% EM (and so on...)

image

image

Installation

  1. Install Python 3.x (tested with 3.11)
  2. Download set_em_rates.py to a folder of your choosing

Instructions

  1. Configure your slicer settings according to Ellis' guide
  2. Enable the Output options -> Verbose G-code option
  3. Add the the script to Output options -> Post-processing scripts:
    • Syntax: "[PATH_TO_PYTHON_EXE]" "[PATH_TO_SCRIPT]";
    • Example: "C:\Python311\python.exe" "C:\path\to\set_em_rates.py";
  4. Add desired STLs from Ellis' labeld EM cubes
    • NOTE: Do not change the file/object names, otherwise the script will not work.
  5. Slice and print as usual.

Compatibility

Tested with:

  • Windows 11
  • Python 3.11
  • PrusaSlicer 2.7.1

Should also be compatiblie with different OS and versions of Python/PrusaSlicer, but I have not tested yet. Please leave a comment if it works for you.

Future development ideas?

  • Customizable file prefix (instead of EM_Cube-)
  • Option to customize other tuning options (temperature, retraction, pressure advance etc.)
#!/usr/bin/python
import sys
import re
from pathlib import Path
# Filepath passed from PrusaSlicer
gcode_fp = Path(sys.argv[1])
# Define regex pattern for EM_Cube objects and extract em_rate to a named regex group
re_start_obj = re.compile("(^; printing object EM_Cube-(?P<em_rate>\d\.\d+).stl .+$)")
# Read g-code lines into list
with open(gcode_fp, "r") as f:
old_lines = f.readlines()
# Build new list of lines, including our flow rate adjustments
new_lines = []
for line in old_lines:
# Add line as-is to new_lines
new_lines.append(line)
# Adjust flow rate if starting a new EM_Cube
match = re.search(re_start_obj, line)
if match:
# Convert decimal to percent
new_em_rate = float(match.group("em_rate")) * 100 # percent
new_lines.append(
f"M221 S{new_em_rate} ; set_em_rates.py setting flow rate to {new_em_rate}"
)
# Write changes to file
with open(gcode_fp, "w") as file:
# file.write(new_content)
file.writelines(new_lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment