Printer beds – construction, flatness, levelling and compensation (is it needed?)

I just wanted to jot down some thoughts I have about printer beds. You can see some pictures of my one here My CoreXY Printer build.

It’s fairly obviously to me that the surface on which we wish to print something has to be flat and level. Assuming we start with a typical layer height of around 0.3mm, then that means the surface needs to be flat and level to a tolerance of around 0.1mm. That is to say that the distance from the tip of the printer nozzle to the build surface needs to be the same within 0.1mm or better, wherever the nozzle is in the X or Y direction. The first layer of a print is absolutely critical. If the nozzle is too close to the bed, then the filament will be squashed and ooze out sideways. In extreme circumstances, the print surface may completely block the nozzle. Conversely, if the nozzle is too far from the bed, then the filament simply won’t stick.

The other thing that we may wish to do is heat the bed. If we were only going to print with PLA, this is not strictly necessary as it is possible to print PLA on an unheated bed and use something like blue painters’ tape. The trouble with that is that the object will often stick to the tape better than the tape sticks to the bed, and it can easily get torn so it has be constantly re-applied. So heating the bed means that we don’t have to use blue tape and we can also print with a variety of different plastics.

So what can we use that is flat, level and can be heated? The most common materials are aluminium and glass. Glass on it’s own is maybe not such a good idea as it is easily broken and it isn’t the best thermal conductor. One common arrangement is to use glass with a thin aluminium plate underneath which acts as a heat spreader to the heating element that is attached underneath. Sometimes a wooden (mdf or ply) carrier is used which forms a sandwich with the aluminium heat spreader with the heating element as the filling, and provides some rigidity to the entire assembly. Providing the glass is fairly thick and “unbendable” over the size of the bed, this arrangement could take care of the flatness issue, which just leaves the issue of levelling to be addressed.

On the subject of glass, a word or two of warning. Various 3D printing forums are littered with comments saying that you must use Borosilicate glass because “normal” glass won’t withstand the high temperatures. In my opinion, this is just not true. The important thing is thermal shock. That is to say that if you suddenly heat the glass up (almost impossible to do on a 3D printer) or suddenly cool it by say taking it off the printer and putting in straight in the freezer, then it might well crack. Under normal circumstances, where the glass is allowed to cool naturally at ambient temperatures then there really shouldn’t be a problem. I have certainly never had a problem with any of my 400mm x 400mm x 6mm thick pieces of “normal” float glass, even when heated to around 90 deg C.

Still on the subject of glass, not all glass is flat especially that used for horticultural purposes (greenhouses and the like) and which is probably made from rolled glass. The glass we need is float glass. This glass can additionally be toughened which makes is much more difficult to break. However, I know from hard earned experience that the toughening process will deform the glass such that it is no longer flat. So toughened glass may be safer, but it isn’t flat.

Another common myth that is often propagated on various forums is that glass is an insulator. As someone once pointed out, if that were so we wouldn’t need double glazing. Most of the people who propagate such myths haven’t actually done any testing and they are just repeating what they have read elsewhere. I have done some tests and can say that with the bed heated to 60 deg C  and at an ambient temperature of 21 deg C, the top surface of my 6mm thick glass was measured at 58 deg C. A drop of only 2 deg C.

Now of course, we don’t have to use glass at all. We could simply use thicker aluminium that is guaranteed flat and print directly on to it, and many people do. The aluminium to ask for is “tooling plate” which is guaranteed to be flat. One disadvantage of this is that aluminium is a fairly soft metal and therefore susceptible to damage from knocks and scrapes. So, in my opinion, protecting the surface in some way is a good idea as aluminium tooling plate is expensive to replace.

My personal preference is a combination of thick aluminium tooling plate with a removable glass print surface. Although I could print directly on to the aluminium, the reasons for using glass are that I can quickly swap one piece for another, which enables to start printing another object while the first one is cooling down. Another reason is that I can apply different surfaces to each piece of glass and swap between them. For example, I can have blue tape on one, another could be plain glass with 3dLac applied, a third could have some other “exotic” material attached.

So, now we have a bed that is flat all that remains is to ensure that it is level. Or to put it more correctly in engineering terms “tram” which is derived from the term “trammelling” That is to say that in the X direction, the bed must be parallel with the print head X axis and that in the Y direction, the bed must be parallel with the print head Y axis. Strictly speaking, the entire printer might not be level with respect to the floor on which it stands so the bed might not be level but neither would the axes and they would be “out” by the same amount so it isn’t critical.

I’m not going to go into details of how this should be accomplished. Suffice to say that 3 points define a plane (providing they are not on the same line) and 4 points define a hyperplane if not coplanar (on the same plane).  So 4 (or more) points might define a shape that isn’t a flat plane. i.e. 4 point levelling could impart an undesirable twist to the bed. So ideally, we need to support the bed at 3 points and there should be some adjustment for at least two of these points so that they can be adjusted with respect to the fixed point. This is what is commonly referred to as 3 point levelling. For the best accuracy, these 3 points should be as far away from each other as possible. On my printer, I support the bed using 3 screws, one close to each corner at the front, and one close to the centre at the back. These screws provide lift as well as levelling but I’m not going to go into the mechanics of Z axis movement in this post.

Finally, many people will say that most of the above is irrelevant because many modern printers or printer controllers use firmware to compensate for bed inaccuracies. The latest Duet electronics (of which I am a big fan by the way), even has grid based bed compensation that will compensate not only for the fact that the bed might not be level, but also for the fact that it might not be flat. It will probe the bed over a number of points that the user defines and generate a height map which gets applied to the nozzle height during a print. My concern with this approach is that it encourages people and manufacturers to get sloppy with their design and build quality. It really doesn’t cost much more to “do it right”. In my opinion, any cost saving from a sloppy design of print bed that isn’t inherently level and flat will be outweighed by increases in time spent running the compensation software. People will do doubt argue that running the software need only be done once, as the height map can be saved and re-used. My response is that if a printer bed is inherently non planer or not level, then those errors are unlikely to be constant so it will be necessary to re-run the compensation software periodically. Also, when using something like an Infra Red probe, the probe itself might be affected by dust or debris on the print surface that affects the reflectivity at different points on the surface which would generate an inaccurate height map and make compensation worse than if none were applied.

So although I am a big fan of Duet electronics and firmware, I personally do not use any form of bed compensation. I have built a printer that has a bed that is flat and level and stays that way. My first printer had a flimsy bed that needed constant attention. Every time I used it, I had to home the z axis, then run bed compensation. Now, all I have to do is turn the printer on, select the file to print and walk away. Having tried both approaches, I know which I prefer. Here is a little video that shows how it works in practice. Printing without bed compensation

Further update to blog 6th Jan – Printing without wipe and prime towers

Further to my original post, I have found some inconsistency in results. I had written earlier that my thoughts were that the required purge amount was somehow related to print speed. My suspicion was that there is more molten filament in the melt chamber and above, (before the heat break) when printing at low speed compared to higher speeds. I also said that I needed to do more testing to confirm this.

I have now done that testing and can now confirm that this is not the case and the cause is something else – more later.

Firstly, I should explain my test methodology. I have designed a test piece which is a cube, 60mm x 60mm x 3mm thick. This cube has two rectangular cut-outs, 15mm wide x 40mm long spaced 10mm from each edge and with 10mm between them. This part forms one stl file and is all one colour. Then there are two rectangular shapes which fit into these cut-outs, each one being a separate stl file which is assigned a different colour.

This combined object was sliced in Slic3r. The post-processing script was then run on the gcode file with purge amounts from 2.0 to 4.0 in 0.5 mm steps giving me 5 new files, each with different tool change positions. Each one was then printed with all other print settings the same.

Here is a picture which shows them all.


From left to right, they are with 2.0mm purge, 2.5, 3.0,3.5 and 4.0. Ignore the two upper prints for now. If you look very closely, 2.5mm is the optimum. At 3.0 mm you can just see some bleeding creeping in to the upper right corner of the white rectangle. This is where it changed to black filament just a fraction too early. The same is noticeable on the lower left corner of the black rectangle but this has some red creeping in and does not show very well in the photograph.

Here is a close up picture of what 4mm purge looks like.


Please ignore the overall print quality. These we all printed at 90mm/sec and I did not pay any particular attention to the first layer height. From this picture, it is very clear when the purge amount is too much. You can see some black in the top right corner of the white rectangle and maybe a slight smudging in the bottom right hand corner of the red rectangle. Depending on how good your screen is, you might also be able to see the red smudge on the lower left corner of the black rectangle.

This next picture is a close up of what I deem to be the optimum.


This is with 2.5mm of purge which is the maximum that doesn’t lead to smudging due to the tool change being premature. If you look very closely you’ll see why this isn’t a perfect technique. The left hand edge of the outline of the black square is grey, rather than black and the bottom edge and part of the right hand edge of the white rectangle is dark grey rather than white. This is the transition period from one colour to another and short of using a some sort of a wipe or prime tower, this is a good as it gets.

Once I had established the optimum purge amount, I then printed the same file at lower speeds to investigate the issue that I had observed and laid theses prints out in the same column. So going back to the first picture, the print immediately above the horizontal row was printed at 50% speed (about 45mm/sec) and the one above that was printed extremely slowly at 20% speed (about 18mm/sec).

Here is a close up of 2.5mm purge but at 20% speed. 2-5at20percentspeed

As you can see, in terms of the purge amount, it is unaffected by print speed. Looking closely, you can still see the light grey on the top and left hand edge of the black rectangle, and the dark grey on the bottom and right hand edge of the white rectangle. As I mentioned above, this is the transition period from one colour to another and short of using a some sort of a wipe or prime tower, this is a good as it gets. Personally, I don’t think it’s bad. Whether it is good enough depends on people’s expectations and the criteria that the final, printed object has to meet.

So, what was my cause for concern? It was printing an object that consisted of a lot of small parts. The nature of the object was that it had to be printed much slower than I normally print and so, when I observed that something wasn’t right with the colour changes, I assumed it was to do with the lower speed. I have now proven that this is not the case. Further analysis of the post processed gcode file for that object shows that there is an error (at least one) in my python script. Basically, it won’t move a tool change further back than the original position of the tool change that came before it. So, if the amount of filament to be extruded between tool changes is less than the amount needed to purge the hot end, it won’t work. I need to address this or (more likely) take a different approach and re-write the script.

So for now, the script works where there is a reasonable amount of filament extruded between tool changes but cannot cope with tool changes that are too close together.



Using the Diamond Hotend with DuetWifi

Julia vase #11 Heatwave by Virtox
Printed Ian Pegg

This is a copy of a post I did as guest on Think3dPrint3d ‘s blog back in December 2016. Now that I have my own blog up and running, I thought I’d post it here as well, just to keep all my posts together in one place.

I have been using a Diamond hotend (3 inputs and one output) on my custom built CoreXY printer, controlled by the DuetWifi for some time now. This blog post is documentation on how to setup the DuetWifi with a Diamond hotend however it can be generalized to other multiple input – single output hotends. They might use different configurations but the principles will be the same. I have taken the definition of a mixing hot end as one which has multiple filament inputs and a single output (nozzle), although see the “issues with mixing” section at the end.

Hardware requirements.

Assuming you have the necessary number of extruders for your particular hot end, you will need to be able to drive them all. The new Duet WiFi and the older Duet v0.8.5 both have support for 2 extruders. If you have 3 or more extruders, then you will need to procure an expansion board or use another method to connect additional stepper drivers to the Duet expansion connector.
Important note. A mixing hot end must have filament loaded into all inputs at all times. Failure to do this will mean that extruded filament will find it’s way up into any unused inputs where it will cool and solidify causing a blockage which will be very difficult to clear.

Software and Firmware requirements.

You should have a slicer that is capable of supporting multi part objects. Although some things can be done by post processing the gcode file, objects which share the same Z position, would be very difficult to deal with using this method. Also, the slicer and the printer firmware should ideally be capable of supporting firmware retraction (G10) – more on this later. At the time of writing, Slic3r (version 1.2.9) is known to support these features. David’s (DC42) branch of RepRapFirmware supports firmware retraction as of version 1.10.

Some slicers support multiple extruders but not necessarily multicoloured objects (although there may be workarounds). It is often possible to use different extruders (tools) for perimeters or infill or support materials.


Refer to the Duet wiring diagrams and connect the first extruder motor to E0, the second to E1 and any others to the relevant connectors on the expansion board. I highly recommend that you put a label on each extruder at this time to act as a reminder of which is which when you come to load filament. Remember that by default, the first extruder drive is referred to extruder 0(zero) not 1. This may initially confuse you when you start using slic3r which refers to the first extruder as “1” not rather than “0”. I believe that the latest version of Duet firmware does allow you to “remap” extruders (tools) so that the numbering system start with 1. (See reference to M563 S1 later in this post). New users might want to adopt that approach but i have become accustomed to using the 2 different numbering systems so personally, I’ll stick with it.
Extruders 1 and 2 connected to the main Duet WiFi board
Extruder 2 connected to the Duex5 expansion board
A mixing hot end will only have one heater so connect this to the first heater (marked E0). Similarly, connect the thermistor to the first thermistor connector (also E0). It’s not strictly necessary to use these exact connections but if you decide to use some other terminals on the Duet board, make a note of what you have connected to where. Finally connect the hot end cooling fan to wherever you prefer. The most common configuration is to connect the fan to one of the “always on” terminals but my fan is very high air flow and so quite noisy. Therefore I prefer to have it connected to one of the PWM fan terminals and run it in thermostatic mode so that it only comes on when the hot end is above 45deg C.

Configuration settings – tool definitions.

The first thing to do is define the tools. To do this, it is necessary to edit the confg.g file. The way I prefer to this is to keep a copy of all the sd card files on my PC where I can maintain back up copies. So I would edit the config.g file on my PC then upload it to the Duet through the web interface. One could also physically remove the sd  card, pop it into a card reader and copy the file across. Another way to do it is to edit the file directly, either through having the card in a card reader or via the system editor in the web interface. This is the quickest way to do it but of course this means that you have no backup copy.

There are a few different ways that you can define the tools. You will probably need a tool for each of the “solid” colours. That is to say, tools which will only use 100% of one filament. Then you will need a tool or tools that combine different filaments in various proportions.

So to define a tool which uses only one colour, it is only necessary to set which extruder and which heater it will use. The gcode to define a tool is M563. So for the first 3 tools you could use something like this;
M563 P0 D0 H1
M563 P1 D1 H1
M563 P2 D2 H1
This would define the first 3 tools (P0 to P2) to each use one of the extruders (D0 to D2) but the same heater (H1).
IMPORTANT. Be aware that Slic3r and perhaps other slicers, may use a different numbering system and the first tool is defined as 1 not 0. This can be easily rectified by either defining tools starting with tool 1 and up, or by using the M563 S1 command in config.g to tell the firmware to add a 1 to every tools number
If we want to mix filaments, we’d need create more tools and turn on mixing. So, we might have another tool defined like so;
M563 P3 D0:1:2 H1 ; Define tool 3 (P3) to use all three extruders (D0:1:2) and heater 1
M568 P3 S1 ; Enable mixing for tool 3
M567 P3 E0.34:0.33:0.33 ; Set mixing ratio for tool 3.
What this does is to define the tool T3 to use all 3 extruders (D0:1:2) then it uses M568 to enable mixing for that tool. Finally, it sets the mixing ratio using M567. In this case roughly the same quantity of each of the 3 filaments. However, this mixing ratio should always add up to 1. So, I’ve used 0.34 of extruder 0, and 0.33 of extruders 1 and 2 (0.34+0.33+0.33 = 1.00).
We could also have other tools. Say for example we had Red filament in extruder 0 and Yellow in extruder 1, we could define another tool which would blend equal amounts of the tool to create Orange. It would look like this:
M563 P4 D0:1:2 H1 ; Define tool 4 (P4) to use all three extruders (D0:1:2) and heater 1
M568 P4 S1 ; Enable mixing for tool 4

M567 P4 E0.5:0.50:0.00 ; Set mixing ratio for tool 4.

Of course, we can define as many tools as we like. The upper limit of tool numbers is constrained only by the Duet’s free memory so over 400 tools are possible on the DuetWifi.

Managing a large number of tools would become cumbersome though and there is another way. Once a tool has been defined to use all the extruders and mixing has been enabled, we can simply change the mixing ratio before or during the print. It can be done before the print commences by putting the mixing ratio into the start gcode file. It can also be done during a print by entering the required M567 command in the web interface or by post processing the gcode file that the slicer generates. In theory the tool mix ratio can be set differently for each gcode move.

So we can simply have one tool which is defined as a mixing tool. In practice, this won’t work with slicers which expect different tools for different (coloured) parts of an object. So, I have found the best compromise is to just define 4 tools (or one more than the number of extruders), one for each of the primary filaments and one which is a combination of all three. Coincidentally, in slic3r, if you set your printer to have 3 extruders, it will give you 4 tools to choose from for any object.
Taking this a step further, it is still useful to be able to use any combination of filaments for any tool. For example I could have red green and blue filaments loaded but want to print with Cyan, Magenta and Yellow. So all of my tools are defined as mixing tools but the first three use 100% of only one filament (actually this is not strictly true but we need to consider retraction before I elaborate more). Then if I subsequently want to change a tool to use a different filament or combination of filaments, I can do so but simply changing the mixing ratio at the start of the gcode file, without having to bother with all the other configuration settings. It can also be done “on the fly” from the web interface. This can also be used if one wanted to print a number of copies of the same object but in different colours. Simply slice the object once, then edit the start of the gcode file to change the mixing ratio and/or tool number.
Here is what the tool configuration part of a config.G file might look like.
; Tools
M563 P0 D0:1:2 H1 ; Define tool 0
G10 P0 X0 Y0 ; Set tool 0 axis offsets
G10 P0 R0 S0 ; Set initial tool 0 active and standby temperatures to 0C
M568 P0 S1 ; Enable mixing for tool 0
M567 P0 E1:0:0 ; Set mixing ratios for tool 0
M563 P1 D0:1:2 H1 ; Define tool 1
G10 P1 X0 Y0 ; Set tool 1 axis offsets
G10 P1 R0 S0 ; Set initial tool 1 active and standby temperatures to 0C
M568 P1 S1 ; Enable mixing for tool 1
M567 P1 E0:1:0 ; Set mixing ratios for tool 1
M563 P2 D0:1:2 H1 ; Define tool 2
G10 P2 X0 Y0 ; Set tool 2 axis offsets
G10 P2 R0 S0 ; Set initial tool 2 active and standby temperatures to 0C
M568 P2 S1 ; Enable mixing for tool 2
M567 P2 E0:0:1 ; Set mixing ratios for tool 2
M563 P3 D0:1:2 H1 ; Define tool 3
G10 P3 X0 Y0 ; Set tool 3 axis offsets
G10 P3 R0 S0 ; Set initial tool 3 active and standby temperatures to 0C
M568 P3 S1 ; Enable mixing for tool 3

M567 P3 E0.34:0.33:0.33 ; Set mixing ratios for tool 3

This is not quite how I have my tools defined but to understand the reason for that, we need to look at extruder retraction which is discussed later in this document.
The 3 E3D Titan extruders suspended centrally above the bed in a sort of counter balance “flying” arrangement

Tool offsets

You will notice that in the above tool definitions, there is no X or Y offset, or more precisely the X and Y offsets are set to zero (G10 Pn X0 Y0). That is because there is only one nozzle. X and Y offsets are only necessary where there are more than one nozzle and where they are physically offset from each other. Theoretically these offsets should default to zero but I always like to set them to zero, just in case something should get changed in firmware which might affect the default settings. 

Heating and standby temperatures.

You will also notice that in the tool definitions above, the initial active and standby temperatures are set to zero (G10 Pn R0 S0). That is because I don’t want the hot end to start heating whenever a tool is selected. Instead, I have the heating and standby temperatures set in my start gcode where I can also put the specific temperature as needed for a specific filament.
With a mixing hot end, we only have one heater and one nozzle so if we are using the same type of filament in all 3 inputs, we can instantly switch between tools without having for the next one to warm up, or the previous one to cool down. Therefore, we can (should) set the tool active and standby temperatures to the same values.
This is what I have in my start gcode for PLA in all 3 inputs.
G10 P0 S195 R195     ; Set tool 0 operating and standby temperatures
G10 P1 S195 R195     ; Set tool 1 operating and standby temperatures
G10 P2 S195 R195     ; Set tool 2 operating and standby temperatures
G10 P3 S195 R195     ; Set tool 3 operating and standby temperatures

Then when the print runs and a tool change is needed it can be instantly switched with no warm up or cool down delay.
Obviously, if we had different types of filament in one or more of the extruders, we would need to change these values for those extruders (tools).

Extruder retraction.

With “normal” retraction, only the “active” extruder will retract. That is to say that if we are only pushing one filament into a mixing nozzle, normal retraction will only pull that single filament back. In effect, all that happens is that filament is drawn from the unused inputs rather than from the nozzle tip. In practice, this is just like having no retraction at all.
What is needed is for all filaments to be retracted, regardless of whether they are actively in use or not. Fortunately Duet hardware and firmware give us the ability to accomplish this. It is done by using firmware retraction using the codes G10 (retract) and G11 (unretract). This might be slightly confusing because G10 is also used for tool offsets but without a tool number, it is used for firmware retraction.
G10 is used in conjunction with M207 to define the retraction amount and speed.
Here is what I have in my config.g file.
M207 S1.5 F3000       ;set firmware retraction

S is the amount in mm. F is the Feed rate mm/min (divide by /60 to get mm/sec).
Optionally one could also have R (additional length on unretract) and Z (additional Z lift in mm)
It is vitally important to use this firmware retraction with a mixing hot end so, the slicer software must also be configured to use this. In Slic3r this is simply a matter of ticking a check box which is in the Printer Settings tab under “General”. Whenever retraction is needed, Slic3r will insert a G10 command (and a G11 command to unretract). For other slicers, there is sometimes a facility to post process the gcode output and one could use this to replace whatever retraction codes was output with G10. Another option would be to use some sort of text editor to do a “search and replace”.

Tool Definitions revisited. 

Now that we have set our firmware retraction to retract all 3 filaments simultaneously there is another little issue that will become apparent but can be avoided. With a mixing hot end we must have all the inputs loaded with filament at all times otherwise extruded filament will simply find it’s way up into unused inputs where it will cool and solidify. We must also retract all filaments simultaneously for reasons discussed above. Therefore, when we print using just a single filament and extruder for a prolonged period of time, what happens is that on the unused inputs, the same piece of filament is constantly being retracted and unretracted. Eventually it will just get worn away and will cease to move and retraction stops working and our prints get stringy.
There is also another possibly even more important issue, which is that most filaments will degrade if reheated and cooled a number of times which is what will happen if we primarily use just a single filament for an extended period of time. I’ve seen with PLA that if it is left in the mixing chamber too long without being extruded, it gets completely cooked and when it comes time to extrude the first few millimetres come out as a mixture of liquid and vapour.
There are a couple of things which can help. The first one is, before starting a print heat the nozzle and extrude some filament from each of the extruders. This will ensure that there is a fresh piece of filament in each of the extruders which may well last for enough retract and unretract cycles without getting so worn that it fails to move.
The way that I prefer to do it is to define the tools so that every tool always uses the main filament plus a small proportion of all of the other filaments. In theory, one might think that this would result in muddy and muddled colours. In practice, it is hardly noticeable and it uses the main filament but just enough of the other inputs to keep them moving so that retraction isn’t always moving the same piece of filament back and forth and the same piece of filament isn’t being constantly reheated and cooled.

Here is what my final tool definition section looks like.

; Tools
M563 P0 D0:1:2 H1 ; Define tool 0
G10 P0 X0 Y0 ; Set tool 0 axis offsets
G10 P0 R0 S0 ; Set initial tool 0 active and standby temperatures to 0C
M568 P0 S1 ; Enable mixing for tool 0
M567 P0 E0.90:0.05:0.05 ; Set mixing ratios for tool 0 (90%,5%,5%)
M563 P1 D0:1:2 H1 ; Define tool 1
G10 P1 X0 Y0 ; Set tool 1 axis offsets
G10 P1 R0 S0 ; Set initial tool 1 active and standby temperatures to 0C
M568 P1 S1 ; Enable mixing for tool 1
M567 P1 E0.05:0.90:0.05 ; Set mixing ratios for tool 1 (5%,90%,5%)
M563 P2 D0:1:2 H1 ; Define tool 2
G10 P2 X0 Y0 ; Set tool 2 axis offsets
G10 P2 R0 S0 ; Set initial tool 2 active and standby temperatures to 0C
M568 P2 S1 ; Enable mixing for tool 2
M567 P2 E0.05:0.05:0.90 ; Set mixing ratios for tool 2 (5%,5%,90%)
M563 P3 D0:1:2 H1 ; Define tool 3
G10 P3 X0 Y0 ; Set tool 3 axis offsets
G10 P3 R0 S0 ; Set initial tool 3 active and standby temperatures to 0C
M568 P3 S1 ; Enable mixing for tool 3

M567 P3 E0.34:0.33:0.33 ; Set mixing ratios for tool 3 (34%,33%,33%)

If it is critical that the finished print has to be a single colour and cannot be mixed with any other colours, and it is going to be a longish print time, I cut a couple of lengths of filament off of the main roll (say about 1 metre). Then I load the main roll into the first extruder and each of the 1 metre lengths into the other two extruders. So, the machine will use 90% of the filament from the main roll and 5% from each of the offcuts, which means that all the filaments in all the inputs will be kept moving enough to prevent any of the problems mentioned above.
These are what my mixing ratios will always default to. If I want to print a 2 or 3 coloured object using “solid” colours with no mixing for each part, then what I tend to do is simply add M567 commands to the start gcode which will override the default values that I set in my config.g file.
i.e. M567 P0 E1.00:0.00:0.00, M567 P1 E0.00:1.00:0.00, M567 P2 E0.00:0.00:1.00.
For example the snowflakes were printed with White on Gold. In this case, I had two tools. Tool 1 was 100% Gold, Tool 2″ was 100% White. The base and the flake are separate stls. I added the base (gold part) to Slic3R platter, then in settings set it to use Tool 1. Then I used “Add part” to add the flake and set it to use Tool 2. So, when it printed, the first part of the flake started out Gold until the White filament purged through but that was only about an eighth of the first layer and there are 3 layers of white. Of course, this was all before I had developed the technique for moving tool changes forward in the file detailed here. Multi colour printing without using wipe or prime towers
Two colour print using the Diamond hot end

Tuning and tweaking

With the exception of getting the mixing ratios right for whatever printed outcome is desired, there is very little tuning and tweaking necessary for a mixing hot end compared to a non-mixing hot end. The only thing that springs to mind is that, because we retract all filaments together, the retraction distance can be less.
On my particular machine, with Bowden tubes around 250mm long for PLA at 195 deg C and print speed of around 60mm/sec, retraction of 1.5 mm at 3000mm/min works well.
Of course, using firmware retraction makes setting it up a breeze. Simply print two small cubes spaced about 50 mm or more apart then during the print use Duet Web Control to change the retraction on the fly and observe the difference. Use M207 Sn.n Fnnn where S is the amount in mm and F is the speed (feed rate) in mm/min. Start with a small number and increase it slightly until signs of stringing disappear. Repeat for other materials, print speeds and temperature if necessary but you’ll likely find one value that works well for most situations.

Scripting mixing ratios

Another way to use a mixing hot end is to post process the gcode file to enter mixing commands at various places. I have a little python script which an M567 command after “n” layer changes. Tony from Think3dPrint3d kindly put it on github for me. It runs through loops, each one progressively decreasing the mixing ratio for one filament and increasing another. So the colour changes throughout the height of the printed object from colour A to colour B, then colour B to colour C. This can be extended to go from C back to A and then the entire sequence repeated. This is how I produced the following objects:

One of my own designs
Big Julia vase design by Virtox on Thingiverse


The big Julia vase is printed with Red, Yellow and Clear translucent filaments. Starting at 100% red, reducing the Red by 1% and increased the yellow until 100% Yellow, then repeat going from Yellow to Clear. The mixing is changed at every 5th layer to get one complete iteration over the height of the object. The smaller rainbow Julia vase shown at the beginning of this post used Red, Blue and Yellow and changed the mixing at every layer change so ended up with Red,Purple (Red and Blue), Blue, Green(Blue and Yellow),Yellow, Orange (Yellow and Red) then back to Red and repeat the sequence.

Thoughts and ideas.

Most people think of multi coloured objects but even if your slicer doesn’t support multi part objects it may well support multiple extruders. This means that one could choose to use one extruder for the infill but a different extruder for the perimeters. One could then use an exotic or expensive material on the outside but cheaper “everyday” filament for the inside. Or possibly use a clear filament for the perimeters to give the object a clear coating.
Printing support material using a different extruder is another possibility that springs to mind.
Then there are other exotic materials such as electrically conductive filament. It could be possible to print an electrical circuit within an object.

Issues with mixing.

There is a fundamental restriction with the Diamond hot end. This is that there is no mixing chamber as such. The disadvantage is that when filaments are “mixed” they come out of the nozzle like stripy toothpaste. So the colour is biased towards each input. This effect can be partly negated by using translucent filament. Or it can be exploited for decorative purposes. I have printed a 3 sided pyramid with each face a different colour by using a single tool with a mixing ration of 0.34:0.33:0.33 and orientating the pyramid on the build plate such that each face is directly opposite a filament input.
The same pot viewed from3 different angles showing the effect of incomplete mixing


Finally, this is a video of my custom CoreXY printing the large Julia Vase by virtox shown in the picture above:

Making a multicolour gcode file

Here is the method I use to generate gcode files for multi coloured objects

Following on from my post on printing multi coloured objects without using wipe or priming towers, people have asked “how do you get the gcode file to start with?.”

This is the way that I do it. There is nothing ground breaking here and it’s all well documented but this post might help by pulling it all together.

I use OpenScad to generate the .stl files and Slic3r to pull them all together. So, starting with the design, I’ve found it best to create separate modules for each coloured part. Below is the OpenScad code for a simple test piece that I designed to help me find the optimum purge setting to use in the post processing script.


This picture shows what all 3 parts look like.


By commenting out two of the modules like this …..


… it’ll leave just a single module. In this case the outer part only.


Then hit render (the left of the two highlighted buttons above) and once it has finished rendering, hit the export stl button (the other highlighted one) and give it a name like “myThingBlackPart.stl” or some such.

Do the same for the other parts e.g.


Hit Render then export stl with a unique name like “MyThingRedPart.stl” and you’ll have the 3 separate stl files.

The next thing is to load them into Slic3r. So add the first part to the plate like this


Then make sure the part is highlighted (click on it to select – it should show as Green on the build plate, if it’s Yellow, it isn’t selected). Then click on “Object” and then “Settings” which will look like this (without the highlights)


You’ll notice that a tree has started to form with “Object” being the main part and the part we have loaded being the first branch. Click on this branch as denoted by the highlight, then use the drop down to select the Extruder to assign to this part – in this case number 1.

If you don’t see a drop list of extruders, you need to tell Slic3r that you have multiple extruders which you will find under the “Printer Settings” tab.

Note also that in Slic3R the first extruder is referred to as extruder 1 and that your firmware may use a different numbering system such that the first extruder is 0. I believe the latest version of Duet firmware will allow me to “re-map” the extruder numbering so that it’s the same as Slic3R but I’ve become accustomed to using 1,2 and 3 in Slic3R and relating these to 0,1 and 2 on my machine.

So while we are in this screen, we can now press “Load part” and select the next stl to load. If you forget and close this screen, don’t worry. Just click on “Object” again, and then “settings”.


Now select this second part from the tree as highlighted and set the extruder to use for this part (in this case Extruder 2).

Click load part again and repeat for the third part (if thee is one). I find it’s a good idea to click through the “tree” and double check that each part is assigned to the correct extruder. Note that the “Object” itself can have an extruder assigned to it. I’m not sure why and tend to leave it as “default”.

So, once you are happy that all the parts have the correct extruders assigned to them, hit “OK” and slice\export gcode as normal. Slic3R will insert “tool” (Tn) commands in the gcode file at all the points where a new colour (Extruder) is needed.

Hope that helps.


Multi colour printing without using wipe or prime towers

Most of the problems with multicolour printing revolve around the fact that when switching from one filament to another there is still some of the old filament in the system that needs to be purged before the new filament can be extruded cleanly. The usual approach is to use some sort of wipe or prime tower which is wasteful both in terms of print time and filament usage, so this is an alternative approach to the problem.

I’ve marked this as a feature post. If you are interested in this, check my blog for the latest updates.

As I have promised on a couple of forums, this post will explain in detail the method I am developing to enable 3D printing of multi colour objects without resorting to the use of wipe or prime towers.

For those of you who haven’t been following my endeavours on any forums, here is a link to the video I posted on YouTube which shows the technique in action.

Now before we get too far into the details, I need to explain few things about myself. I was born in 1953 and left school/started work in 1969 at age 16. I learnt my maths using log tables and a slide rule. I’m a mechanical engineer by training but currently (career number 5) I have a small business make designing and building garden decks. What I know about computers, 3D printers, electronics, writing code etc is all self taught through a lot of internet searches and reading a few books and online tutorials so I’m no expert.

I started setting up this blog/web site on 4th January 2017 so I’m still finding may way around. Apart from a brief introductory post, this is the first blog post I have ever made. Therefore, the layout of this blog site may well change (and please excuse any typos)

That’s the excuses out of the way, now down to business…..

The rest of this post assumes that the reader has the tools and techniques available to generate or otherwise obtain a .gcode file for a multicoloured object. I plan to do a separate write up on the method I use, which involves separate .stl files (which I generate using OpenScad) and slic3r but that will have to wait.

There are a number of issues which have to be addressed when printing multicoloured objects and most of them revolve around what happens when switching from one filament to another. My first forays into multicolour printing were with a tricolour Mendel variant which used 3 separate hot ends. I found that there were 4 major drawbacks to this approach. Firstly it was very difficult to get all 3 nozzles at exactly the same height (within less than 0.1mm) and for this setting to be maintained in such a way that inactive nozzles didn’t drag across the printed part. Secondly, it was very difficult to set and maintain  X and Y offsets get the output from all 3 nozzles perfectly aligned. Thirdly, it was necessary to set unused nozzles to a lower standby temperature in order to prevent filament from oozing out and dragging on the print but this was never completely successful. Also, the waiting time for one nozzle to heat up and another to cool down significantly increased the print duration. Lastly, there was the problem of filament oozing from one nozzle as it cooled down and before it reached it’s standby temperature, or another nozzle as it heated up before it reached it’s active temperature.

All of the above problems can by overcome by using a single nozzle – i.e. a mixing hot end. For the purpose of this post, I will define a mixing hot end as one which has multiple filament inputs but a single output (nozzle). I am currently using a Diamond hot end but I am aware that there are other designs.

However, this approach generates a new problem which is that when switching between filaments, there is still some of the old filament left in the nozzle and/or mixing chamber which has to be purged before the next filament comes through. If the colour change coincides with a layer change, this is rarely an issue. Unless the object being printed is really small, what happens is that the purge occurs during the inner perimeter(s) so by the time the outer perimeters are being laid down, the purge has already been completed. Effectively the change over period is hidden inside the object and isn’t visible.

The real problem is when the colour change occurs within the same layer, as in the union flag example that is in the YouTube video, because there is nowhere to hide and any defects will be visible. The usual approach is to move the print head away from the main object, and print some sort of sacrificial tower or use some sort of wiping mechanism to purge out the old filament, then move back to the object and resume printing. This is wasteful both in terms of filament and time, and can lead to a less than satisfactory print finish if filament retraction and recovery are not fully optimised.

The technique I’m developing and which I am about to share here, simply moves the filament change point to a position earlier in the gcode file. The new position equates to the amount of filament that needs to be extruded in order to purge out the old. The method isn’t perfect because in theory, there is still a “transition” period where what is being extruded is a mixture of the old colour and the new. In practice, by observation I’d estimate this transition to be about 10% of the total “purge amount” and I have found that with the Diamond hot end the optimum purge amount is about 2.0 to 2.5mm of filament so there is around 0.2 to 0.25mm of “transition” filament which is hardly noticeable. Other designs of hot end may have larger mixing chambers, in which it may still be necessary to use some sort of wipe or prime tower. However, my technique could still be applied to take account of much of the purging so any further priming could be achieved by using a very much smaller tower or priming mechanism. One of my planned improvements is to build a small priming tower or similar mechanism into the post processing script, although as I have said earlier, the transition between one colour and the next with the Diamond hot end is really small and hardly noticeable.

As mentioned earlier, the only coding I know how to do is what I have been able to teach myself from internet searches, on-line tutorials and the odd book (some have been very odd). I have used Python just because I found it less of a pain to learn than some other languages that I’ve tried. I am aware that this might not be the best tool and I am deeply aware of my own shortcomings so I think the best approach is for me to post my work flow and explanation of what the code attempts to do. Others with more skill than I have can then use it as basis to write something more efficient (and probably less dangerous). I’ll make the python script available for those want to try it but please don’t blame me if it crashes your PC or worse.

So here goes.

The approach I’m taking is to leave the original file untouched and generate a new file with the colour change commands moved forward in the file but otherwise it’s an exact copy. This enables me to run the script several times on the same file but with a different “purge” setting in order to test and fine tune. The change from one colour to another in the gcode file is accomplished by selecting a new tool. It could be done by having a single tool and changing the mixing ratio but that’s not how slc3r works. So for the rest of this post colour changes will be referred to as tool changes. Note that the code could be adapted to look for M563 commands (mixing ratio changes) instead or as well as “Tn” (tool change) commands.

I haven’t yet figured out how to cut and paste the python text while retaining the font colours so I’ll change them manually – hopefully I won’t miss any. Comments are in red and start with a hash (#) String values are in green. My other comments are in blue to denote the fact that are not part of the script.

These first lines should be changed to suit the user. Hopefully the comments make them self explanatory.

sourceFile = “D:\RepRap\G Code Files Diamond hot end\Python test file.gcode” # The full path and file name of the file to be worked on
destinationFile=“D:\RepRap\G Code Files Diamond hot end\Python output.gcode” # the full path and file name of the destination (output) file
temporaryFile =“D:\RepRap\G Code Files Diamond hot end\Python temp.gcode” # A temporary file used to hold smallish sections of the original and which gets read into memory.
purgeAmount =2.5 # This is the amount of filament to be purged when a tool change is needed

The next thing I struggled with was how to detect the start of a print proper. Slic3r inserts all sorts of comments both before and after the actual print and I use quite a complex start gcode in my slic3r setting. In the end I decided to use flags in my start and end gcodes. So the last line of my start gcode is a comment “;begin” and the first line of my end gcode is the comment “;end”. The main reason for doing this is that the script simply looks for the text “T” to detect a tool change and as “T” can be found in comments and both pre and post print sections, I need to restrict the search.  

beginText=“begin” #string to look for to detect start of gcodes proper. This should be the last line of slicer start gcode
endText=“end” #string to look for to detect end of gcodes proper. This should be the first line of slicer start gcode
searchToolText=“T” # String to look for to detect a tool command (which is why we need to restrict the search to between “begin” and “end”
# Tool number could be anything after the “T” so we can’t can’t search for “Tnn” explicitly

Here are all the other variables that I have used. I know that I probably shouldn’t have declared them as global variables but at least they are all in this section so easy to find what they do. I also know that I’ve probably used a lot of unnecessary flags and that they could be boolean rather than integers etc etc.

beginFlag=0 # flag to detect if the word “begin” is present
endFlag=0 # flag to detect if the word “end” is present
#There is nothing to check that begin is indeed before end – maybe do this check too.
toolFlag=0 # flag to detect that tool changes are present
toolChanges=0 # number of tool changes in the file
currentTool=“T0” #default this to “T0”
lineText=“” # text read in from file
startLineNo=0 # the line number in the file immediately after the word “begin”
toolChangeLineNo=0 # the line number in the file where a tool change occurs

newToolPos=0 # the position where the tool command will be inserted
lastLine = 0 # the position of the last line in the temp file
lineToRead=0 # pointer to which line to read in the temp file
estart=0 # position in the line after the letter “E” which denotes the position of the start of digits for extrusion
estring=“” # the string of 7 digits following the letter E
EAmount=0.0 # estring converted to a number
totEAmount=0.0 # the sum of all EAmounts
EAmountNeeded=0 # the purge amount less the totEAmount
notEnoughFlag=0 # flag to set if the the totEAmount is less than needed for a full purge between tool changes

xStartPrev=0 # the position in the previous line where X is
xPrevString=“” # read the 7 characters after “X”
xPrevNum=0.00 # convert to number – First X value

xStartHere=0 # the position in this line where X is
xHereString=“” # read the 7 characters after “X”
xHereNum=0.0 # convert to number – Second X value

xMove=0 #xHereNum-xPrevNum

yStartPrev=0 # the position in the previous line where Y is
yPrevString=“” # read the 7 characters after “Y”
yPrevNum=0.0 # convert to number – First Y value

yStartHere=0 # the position in this line where Y is
yHereString=“” # read the 7 characters after “Y”
yHereNum=0.0 # convert to number – Second Y value

yMove=0 # yHereNum-yPrevNum

E1 = 0 # EAmount-EAmountNeeded
E2 = 0 # EAmountNeeded

X2 =0 # xHereNum
X1 =0 # PrevNum +(xMove/eAmount*E2)
Y2 =0 # yHereNum
Y1 =0 # yPrevNum +(yMove/eAmount*E2)

E1string=“” # used to build insertString1 – 1st E amount
E2string=“” # used to build insertString2 – 2nd E amount
X1string=“” # as above but X values
Y1string=“” # as above but Y values

insertCheck=0 # used in loop to check lines are valid for inserting text
firstInsert=0 # modified from newToolPos if that position contains something other than a normal XYE move
secondInsert=0# as above

insertString1=“” # 1st (new) move string prior to tool change i.e. “G1 Xnnn.nnn Ynnn.nnn En.nnnnn”
insertString2=“” # then the tool change
insertString3=“” # then the second move string

changesDone=0 #number of tool changes done

So now we start the script proper. This section should all be self explanatory. I don’t know how to indent individual lines within a paragraph using this WordPress thingy so I’m having to do it by starting a new paragraph wherever the text needs to be indented.  Therefore there are what look like a lot of blank lines in what follows but Python relies on indentation for the code to work so it’s important that I show it.

# Start by reading through the file to check that “;begin” and “;end” are present
startfile=open(sourceFile,‘r’) # Open file as read only
numLines=sum(1 for line in (startfile)) set to start of file (just in case)
print (“Checking File…”)
for x in range (0,numLines):

if endText in lineText:

break # exit loop on first instance of “end”

for x in range (0,numLines):

if beginText in lineText:

break # exit loop on first instance of “begin”

print (“Number of lines =”,numLines) # Just for info
if beginFlag>0:

print (“\”;begin\” text is present at line “,beginFlag) # Just for info – print a message to say that the search string was present.


print (“There is no \”begin\” statement in the file”) # If not print a different message to say that the string wasn’t found…….
print (“Add \”begin\” to the end of the start gcode in Slicer”) # ….and tell the user what to do about it.
print (“And/Or edit the file and start again”)
exit() pr# if there is no “begin” text string in the source file then end the script after printing the message and closing the file.

if endFlag>0:

print (“\”;end\” text is present at line “,endFlag) # comments as above


print (“There is no \”end\” statement in the file”)
print (“Add \”end\” to the end of the start gcode in Slicer”)
print (“And/Or edit the file and start again”)
exit()# if there is no “end” text string in the source file then end the script after printing the message and closing the file.

startfile.close() # I always like to close a file when an operation on it has finished – just in case.

This next bit is just for information really although the number of tool changes is used further on in the code and if there aren’t any tool changes found, it will print a message and exit. 

#so now read through and count the number of tool changes between begin and end

print (“Checking for tool changes…”)
for x in range (beginFlag,endFlag):

if searchToolText in lineText:


toolChanges=toolFlag-1 # the first occurrence will be the first tool, so take 1 off to get the number of tool changes.
if toolChanges>0:

print (“There are “,toolChanges-1,“tool changes in the file”)


print (“There are no tool changes in this file”)
print (“So no changes will be made.”)


Now I read through the file and copy everything up to the first tool change into the output file

# Now read through the file and copy everything up to the first tool change
startfile=open(sourceFile,‘r’) # open source file again for reading # make sure we start at the beginning
endfile = open(destinationFile,‘w’)# open the destination file for writing
print (“Reading file and writing lines up to first tool change”)
#start by copying everything up to the begin statement
for x in range (0,numLines):

lineText=(startfile.readline())#read line from input file
# uncomment the following line with caution – it really slows things down.
#print (lineText)
endfile.write(lineText) # copy line to output file

if beginText in lineText: #is there a tool string present in this line?

startLineNo=x # if so, set the variable StartLineNo to x which will be the text file line number

break #end the loop when the first tool line number has been reached

# end of for loop


# So now check for first instance of a tool (check gcode file for a “T”)
# also, append all the lines up to this point to the end file
# Start by opening the end file again in append mode

endfile = open (destinationFile, ‘a’) # open again in append mode

for x in range (startLineNo,numLines): # miss out the first lines from 0 to StartLine No

lineText=(startfile.readline())#read line from input file
# uncomment the following line with caution – it really slows things down.
#print (lineText)
endfile.write(lineText) # copy line to output file but this time we are amending it to the end of the file
if searchToolText in lineText: #is the string present in this line?
currentTool=lineText # if so, set the variable CurrentToolText to whatever this line holds (string value)
break #exit loopif


# *********** Now it gets tricky************

What I do next is copy chunks of the source file between tool changes into a temporary file. The reason for doing so is that I wanted to read this file into a list so that I can work on it. Maybe I could read the entire file into memory but potentially it could be a huge file and gobble up all the PCs resources. David Crocker (DC42 of Duet fame) has said that if he were doing this in firmware, he would have two streams – the original and the output with the a kind of buffer that would hold just the number of commands that make up the purge amount, rather than this approach which holds all the commands between one tool change and the next before they are written to the output file. Thinking about it, that would be a far better approach but the following is how it is now. So, to continue…

(Oh by the way, there are quite a few commented out print statements in what follows which were only used for debugging)

# Read lines from latest tool to next tool and stick them in a temporary file

for changesDone in range (1,toolChanges):# repeat the rest of this until we reach the end of the file

print (“Doing tool change “,changesDone)
totEAmount=0 # reset to zero
newToolPos=0 # reset to zero

tempfile = open(temporaryFile, ‘w’) # This will create a file if it doesn’t exist and/or overwrite.
print (“Writing to temporary file……”)
for x in range (toolChangeLineNo,numLines): # miss out the first lines from 0 to StartLine No

#print (“Tool change line number = “,toolChangeLineNo)
lineText=(startfile.readline())#read line from input file

# uncomment the following line with caution – it really slows things down.
#print (LineText)
tempfile.write(lineText) # copy line to temporary output file
if searchToolText in lineText: #is the string present in this line?

currentTool=lineText # if so, set the variable CurrentTool (Text) to whatever this line holds (string value)
toolChangeLineNo=x # so update the pointer to the current line number…
break #… and exit loop

if x==numLines: # if we reach the end of the file (we should never get here)
print (“No more tool changes”) # print message, close files and quit


#end of x loop – still in changesDone loop

tempfile.close()# Close the file for writing to

#now read the whole tempfile into a list so we can work on it
templines =f.readlines()
lastLine =(len(templines)-1) # Len is the number of lines in the file but the last line will be 1 less because the first line is indexed as 0.
#print (templines[lastline]) # just for debugging
print(“Calculating new tool position”)
#Start at the end of the file and work backwards
# read the 7 characters after the “E” in a line
notEnoughFlag=0 # Reset flag (start at zero)
for j in range(1,lastLine): # set range to the number of lines in the file so that it can’t go back beyond that

lineToRead=lastLine-j # read the line
#print (templines[lineToRead]) # for debugging only – comment out later
if “E” in templines[lineToRead]: # check that the line does actually have an E value

estart=templines[lineToRead].find(“E”) # find the position in the line where E is
estring=templines[lineToRead][estart+1:estart+8] # read the 7 string characters after the E
#print(estring) # just for debugging – comment out later
EAmount=float(estring)# convert the string to a number
#print (EAmount) # just for debugging – comment out later
totEAmount=totEAmount+EAmount # add this amount to the previous amount to get a total
#print (totEAmount) # just for dbugging – comment out later
if totEAmount > purgeAmount:

newToolPos=lineToRead #Set the new tool position to lastline -j (the point in the line where the sum of all the extruder commands excedes the purge amount).
#print(“new tool position “,j)
break #exit the loop

if j==lastLine: # if we go all the way back

newToolPos=lineToRead # then there aren’t enough moves to purge fully so set new tool pos to the earliest position
print (“WARNING Not enough extrusion between tool changes to fully purge”)


# so now we have the position for the tool where adding the Eamount from the line after excedes the amount needed to purge
# but it’s counting backwards from the end
print(“Splitting Gcode line at insert point”)
# so I want to use only the amount needed, therefore I have to split the line
#the amount needed will be the ToteAmount minus the purgeAmount
EAmountNeeded = totEAmount-purgeAmount
#get the x an y totals for the line that needs to be split

# ************Trap for the line not being a normal XYE move **************
# ************If it isn’t go back and keep going back until we find one **************

if notEnoughFlag==1: # skip the next bit if flag set


while insertCheck ==0:

if “G1 X” in templines[secondInsert]:



# Now get the x value from this line

xStartHere=templines[secondInsert].find(“X”) # the position in this line where X is
xHereString=templines[secondInsert][xStartHere+1:xStartHere+8] # read the 7 characters after “X”
xHereNum=float(xHereString) # convert to number – Second X value

# and do the same for the y value
yStartHere=templines[secondInsert].find(“Y”) # the position in this line where Y is
yHereString=templines[secondInsert][yStartHere+1:yStartHere+8] # read the 7 characters after “Y”
yHereNum=float(yHereString) # convert to number – Second Y value

#Now do it all again for the previous line

firstInsert=newToolPos-1 #take 1 away to get the previous line
if notEnoughFlag==1: # skip the next bit if flag set


while insertCheck ==0:

if “G1 X” in templines[firstInsert]:



xStartPrev=templines[firstInsert].find(“X”) # the position in the previous line where X is
xPrevString=templines[firstInsert][xStartPrev+1:xStartPrev+8] # read the 7 characters after “X”
xPrevNum=float(xPrevString) # convert to number – First X value

yStartPrev=templines[firstInsert].find(“Y”) # the position in the previous line where Y is
yPrevString=templines[firstInsert][yStartPrev+1:yStartPrev+8] # read the 7 characters after “Y”
yPrevNum=float(yPrevString) # convert to number – First Y value

#Now take one from the other

# Now split the values

E1 = EAmount-EAmountNeeded
E2 = EAmountNeeded

X2 = xHereNum
X1 = xPrevNum +(xMove/EAmount*E2)
Y2 = yHereNum
Y1 = yPrevNum +(yMove/EAmount*E2)

# Now convert the values back to strings

E1string=“{:.5f}”.format(E1)# 5 decimal places
#print (E1string)
#print (E2string)
X1string=“{:.3f}”.format(X1)#3 decimal places
#print (X1string)
#print (X2string)
#print (Y1string)
#print (Y2string)

#build strings to insert
insertString1=“G1 X”+X1string+” Y”+Y1string+” E”+E1string+”\n” # 1st (new) move prior to tool change
insertString2=currentTool # then the tool change
insertString3=“G1 X”+X2string+” Y”+Y2string+” E”+E2string+”\n” # then the second (new) move

#print (insertString1)
#print (insertString2)
#print (insertString3)

#determine the point where the lines need to be inserted
print (“Appending lines to output file”)
#now open the end file and append this lot to it.
endfile = open(destinationFile,’a’)
for x in range (0,insertPoint):


for x in range (insertPoint+1,lastLine):


print (“Tool change “,changesDone,“done”)
changesDone=changesDone+1 # so go back and do it all again for the next tool change

# End of changes Done loop

print(changesDone-1,“Tool changes moved”)
if notEnoughFlag==1: # skip the next bit if flag set
print(“WARNING there was insufficient filament extruded to fully purge between at least one of the tool changes”)

# So now all that remains is to append the rest of the source file to the output file

print (“Copying remaining lines to output file…”)
endfile = open(destinationFile,’a’)
for x in range (toolChangeLineNo,numLines):

lineText=(startfile.readline())#read line from input file

print (“Done..”)

And that my friends is it!!

I don’t suppose anyone will want to copy and paste all the above code so I’ve uploaded the file to my Google drive.

Edit. 24th Jan 2017. I have removed the link to the Python script because there were a number of bugs – see later posts.

Introductory first post

The intended purpose of this blog/web site is that it will be a place where I can share my thoughts, ideas, experiments and general 3D printing related stuff. I have no affiliation to any 3D printer brands or manufacturers. What I write here will just be my own personal views and experiences. Hopefully some of the stuff I put on here will be useful or inspirational to the wider RepRap community. If not, well nobody is forcing to read any of it……..