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.

    #!/usr/bin/python
    """
    	Slic3r post-processing script for RepRap firmware printers which prefer G10, particularily useful on toolchangers/IDEX
    {1}
    Usage:
    	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. 	
    Args:
    	Path: The path parameter will be provided by Slic3r.
    Requirements:
    {1}
    	The latest version of Python.
    	Note that I use this on windows and haven't tried it on any other platform.
    {1}
    Credit:
    {1}
    	Based on code originally posted by CCS86 on https://forum.duet3d.com/topic/15302/cura-script-to-automatically-probe-only-printed-area?_=1587348242875. - 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...")
    	
    	try:
    		_Slic3rFile = open(fname, encoding='utf-8')
    	except TypeError:
    		try:
    			_Slic3rFile = open(fname)
    		except:
    			print("Open file exception. Exiting.")
    			error()
    	except FileNotFoundError:
    		print('File not found. Exiting.')
    		error()
    		
    	lines = _Slic3rFile.readlines()
    	_Slic3rFile.close()
     
    	linesNew = g10_swap(lines,delta_temp, end_phrase)
     
    	_Slic3rFile = open(fname, "r+")
    	_Slic3rFile.seek(0)                       
    	_Slic3rFile.truncate()
    	for element in linesNew:
    		_Slic3rFile.write(element)
    	_Slic3rFile.close()
    	print("press any key to close")
    	input()
    	return
     
    def error():
    	# remove the next 2 lines to close console automatically
    	print("Press Enter to close") 
    	input()
    	sys.exit()
    
    def g10_swap(lines,delta_temp, end_phrase):
    
    	# Replace M104 with G10 Gcode
        linesNew = []
        tools_used = []
        start_gcode_ended = False
        count=0
    
        for line in lines:
            if line.startswith('M104'): 
                tool=line[(str.find(line,"T")+1)]
                #tools_used.append(tool) #stores tools
                activetemp=line[(str.find(line,"S")+1):(str.find(line,"S")+4)]
                standbytemp=int(activetemp)-delta_temp
                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
                    continue
            else:
                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
    
                linesNew.append(line)
        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)
        else:
            
            print('Error: Proper Slic3r post processing command is python3')
            error() 
    
    
    

Log in to reply