Created
April 14, 2024 15:00
-
-
Save Defozo/87f12e31de278e1aa4357c36e678e1fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# G-code Filament Management Enhancement Post-processing Script | |
# This script modifies G-code files by removing the initial 'M600' filament change command and all 'T' series toolhead change commands, facilitating better filament management during prints. | |
# | |
# Problem Description: | |
# The standard "Color change" feature in PrusaSlicer has significant limitations: | |
# 1. It only allows filament changes between layers, which restricts the timing of such changes and can impact print quality and detail. | |
# 2. It requires that all used filaments share similar properties, such as melting temperature and extrusion rates, limiting material versatility. | |
# | |
# Alternative Approach: | |
# An alternative method involves using "Custom G-code" settings to manage filament changes: | |
# 1. This technique allows for filament changes at any point in the print, offering greater flexibility. | |
# 2. It accommodates the use of varied filament types with diverse properties (e.g., varying printing temperatures and extrusion multipliers) within a single print job. | |
# However, setting this up introduces an unwanted 'M600' command at the beginning of the print and multiple 'T' commands throughout the G-code, which can result in errors when using interfaces like OctoPrint. | |
# | |
# Quickstart Guide: | |
# Download and save this script. | |
# Setup in PrusaSlicer: | |
# - Go to: Printer Settings -> General -> Capabilities. | |
# Set "Extruders" to the desired count for different filaments and ensure "Single Extruder Multi Material" is enabled. | |
# - In Printer Settings -> Custom G-code: | |
# Input the following as "Tool change G-code": | |
# | |
# M600 | |
# | |
# - In Print Settings -> Post-processing scripts: | |
# Specify the path to your Python interpreter and this script as follows: | |
# | |
# C:\Python312\python.exe C:\Users\defozo\Downloads\remove_first_M600_and_remove_T_commands.py | |
# | |
# - In "Plater": | |
# Use "Multimaterial painting [N]" to designate geometries for printing with various filaments, or split your print into different objects/parts and assign them to the corresponding "extruders". | |
# | |
# Note: This script eliminates toolchange commands, potentially impacting how G-code appears in PrusaSlicer's G-code Viewer. It is specifically designed to address OctoPrint error scenarios. | |
import os | |
import sys | |
import re | |
def remove_first_occurrences_and_T_commands(input_gcode_path): | |
# Initialize flag for first occurrence of 'M600' | |
found_M600 = False | |
# Regular expression pattern to match lines starting with 'T' followed by any digits | |
pattern_T_commands = re.compile(r'^T\d+') | |
with open(input_gcode_path, 'r') as file: | |
lines = file.readlines() | |
new_lines = [] | |
for line in lines: | |
# Remove first occurrence of 'M600' | |
if 'M600' in line and not found_M600: | |
found_M600 = True | |
continue # Skip this line | |
# Remove all lines starting with 'T' followed by any digits | |
if pattern_T_commands.match(line): | |
continue # Skip this line | |
# Add the line if it does not match the conditions above | |
new_lines.append(line) | |
# Write the modified content back to the G-code file | |
with open(input_gcode_path, 'w') as file: | |
file.writelines(new_lines) | |
if __name__ == '__main__': | |
# The first command-line argument is the path to the temporary G-code file | |
input_gcode_path = sys.argv[1] | |
remove_first_occurrences_and_T_commands(input_gcode_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment