Automatic M104->G10 script Toolchanger / Multi Extruder Printer

  • Hey - I did a search and I couldn't find some postprocessing scripts that would automagically insert G10 Rnnn Snnn based on the filaments I had chosen, and after having to remember to change them after a while, I figured I'd have some fun and put together a postprocessing script to automatically convert M104 to G10.

    This works for Prusaslicer, Superslic3r, all the forks. I personally use superslicer by supermerill

    When you execute the completed gcode, it should turn on the Tool but not pick it up, and then put all tools into standby before beginning the print gcode. The standby temp is chosen by a simple subtraction. For toolchangers where you don't want to use all of the tools or commonly swap filaments, its nice to have something that makes the process a little less manual.

    To use it in the slicer, all you need to do is remove M104/M109 commands from your start_gcode, and define your "safe word" that tells the script to put all the tools into standby before going into other actions.

    Let me know what issues you find.

    	Slic3r post-processing script for RepRap firmware printers which prefer G10, particularily useful on toolchangers/IDEX
    	Two inputs -
    	1)delta_temp - The amount of temperature you want to be subtracted from your active temp. 
    		1a) FUTURE WORK - Make this dependent on filament type - more to come
    	2)end_phrase - this is what you want to use to end your start gcode so the script can "close up" and deselect the tools.
    Slic3r automagically puts M104 before any of your starting gcode, so as long as you have your extruders set up, all of this will be performed before any homing or other motion. 	
    	Path: The path parameter will be provided by Slic3r.
    	The latest version of Python.
    	Note that I use this on windows and haven't tried it on any other platform.
    	Based on code originally posted by CCS86 on - this is mostly just to import the file automagically, Wasn't sure how to immediately run the file based on slic3r's input.
    import sys
    import re
    import math
    import os
    delta_temp = 20   # amount to subtract from the active temp
    end_phrase ="Prantin" # String to search for to end your start gcode and insert the T-1 gcode
    def main(fname, delta_temp, end_phrase):	
    	print("BeepBopBoop Scanning...")
    		_Slic3rFile = open(fname, encoding='utf-8')
    	except TypeError:
    			_Slic3rFile = open(fname)
    			print("Open file exception. Exiting.")
    	except FileNotFoundError:
    		print('File not found. Exiting.')
    	lines = _Slic3rFile.readlines()
    	linesNew = g10_swap(lines,delta_temp, end_phrase)
    	_Slic3rFile = open(fname, "r+")                       
    	for element in linesNew:
    	print("press any key to close")
    def error():
    	# remove the next 2 lines to close console automatically
    	print("Press Enter to close") 
    def g10_swap(lines,delta_temp, end_phrase):
    	# Replace M104 with G10 Gcode
        linesNew = []
        tools_used = []
        start_gcode_ended = False
        for line in lines:
            if line.startswith('M104'): 
                #tools_used.append(tool) #stores tools
                linesNew.append(f"G10 P{tool} S{activetemp} R{standbytemp}" + "\n") #actual conversion
                if start_gcode_ended==False: #activates tool to get it warmed up - for initial warmups
                    linesNew.append(f"T{tool} P0" + "\n")
                    count=count+1 #counts quantity of tools, used in removing m109's
                print(f"M104 converted over to G10 T{tool} S{activetemp} R{standbytemp}")
            elif line.startswith("M109") and count>0 and start_gcode_ended==True:
                    count = count-1
                if end_phrase in line: # Check for end of start gcode
                    linesNew.append("T-1 P0" + "\n") #deactivates all tools w/out macros
                    start_gcode_ended = True
        return linesNew
    #main("foo.gcode", delta_temp, end_phrase) #testtesttest
    if __name__ == '__main__':
        if sys.argv[1]:
            fname = sys.argv[1]
            main(fname, delta_temp, end_phrase)
            print('Error: Proper Slic3r post processing command is python3')

Log in to reply