Bambu X1 and P1 minimal toolchange G-code

Hi, I would like to manually trigger a toolchange on a layer by adding a toolchange to the exported G-code directly. I looked at a generated toolchange G-code and the reference toolchange G-code in the printer settings in Bambu Studio and came up with the below snippet.

Would anyone be able to please tell me if this toolchange code is correct or if you have manually added a filament change in G-code in the past? Thanks.

I replaced the Z offset movements with relative Z moves through G92.
M620 SXXA and TXX and M621 SXXA would have XX replaced with the new filament position.

The purpose of this is to automatically add a specific color change a specific layers of 3D topo map models for Bambu users of the 3D map models here. I have some experience with Marlin and made a script to determine the correct places to insert the toolchange and relocate the prime tower. I would run this myself to test but I don’t have a Bambu printer at the moment.

G1 E-.1 F1800

M106 P3 S0

M620 SXXA

M204 S9000

G17

;G2 Z0.6 I0.86 J0.86 P1 F10000 ; spiral lift a little from second lift

G2 I0.86 J0.86 P1 F10000 ; spiral lift a little from second lift

G91

G1 Z0.4

G90

;G1 Z3.2 F1200

G91

G1 Z2.6

G90

G1 X70 F21000

G1 Y245

G1 Y265 F3000

M400

M106 P1 S0

M106 P2 S0

M104 S220

G1 X90

G1 Y255 F4000

G1 X100 F5000

G1 X120 F15000

G1 X20 Y50 F21000

G1 Y-3

M620.1 E F523 T240

TXX

M620.1 E F523 T240

M400

G92 E0

; FLUSH_START

; always use highest temperature to flush

M400

M109 S240

G1 E23.7 F523 ; do not need pulsatile flushing for start part

G1 E0.690105 F50

G1 E7.9362 F523

G1 E0.690105 F50

G1 E7.9362 F523

G1 E0.690105 F50

G1 E7.9362 F523

G1 E0.690105 F50

G1 E7.9362 F523

; FLUSH_END

G1 E-2 F1800

G1 E2 F300

; FLUSH_START

G1 E10.4769 F523

G1 E1.1641 F50

G1 E10.4769 F523

G1 E1.1641 F50

G1 E10.4769 F523

G1 E1.1641 F50

G1 E10.4769 F523

G1 E1.1641 F50

G1 E10.4769 F523

G1 E1.1641 F50

; FLUSH_END

G1 E-2 F1800

G1 E2 F300

; FLUSH_START

M400

M109 S220

G1 E2 F523 ;Compensate for filament spillage during waiting temperature

; FLUSH_END

M400

G92 E0

G1 E-2 F1800

M106 P1 S255

M400 S3

G1 X80 F15000

G1 X60

G1 X80

G1 X60; shake to put down garbage

G1 X70 F5000

G1 X90 F3000

G1 Y255 F4000

G1 X100 F5000

G1 Y265

G1 X70 F10000

G1 X100 F5000

G1 X70 F10000

G1 X100 F5000

G1 X165 F15000; wipe and shake

G1 Y256 ; move Y to aside, prevent collision

M400

;G1 Z3.2 F3000

M204 S500

M621 SXXA

M106 S178.5

M106 P2 S178

M106 P3 S200

G92 E0

G1 E-.04 F1800

G91

G1 Z-2.6 F30000

G90

G91

G1 Z-0.4

G90

G1 E.8 F1800

My modified gcode test file previews correctly in Bambustudio as you can see the new colors inserted.


Have you tried to use the right slider and just add a filament change then export it ?

filament_change

How do I activate the right slider with the change filament at a layer?

I was able to get the menu to show up but Change Filament is grayed out.

Sorry my bad I just noticed you are painting also

I was just thinking, can you use the paint ( height range ) tool on your part design to get the design you want ?

Oh, I am not using painting. I am using the 2 object dice model at Dual Color Die V2 by RobertW - MakerWorld as a multiobject 3MF and assigned them filament 1 and 2.

O ok so I downloaded the raw files and painted them then selected assemble and slice plate this is what it looks like but the assemble adds overlaps :frowning:

Without assemble it looks better but has empty layers but looks like it would print.

I was able to get the change filament enabled when I added a simple primitive cube in one color. I then looked at the exported gcode to see what it added. Seems to look similar to my first gcode above.

The filament index specific gcodes where the same as I observed before:

M620 SXXA and TXX and M621 SXXA would have XX replaced with the new filament position.

Painting each model does not work in this case because I have multiple topo map models with many details and each person printing the map would like elevation lines at different places. For example below from someone who did a manual filament change on my map with their P1S at heights (but they also want to add a third color a X height and higher so that is what my script does in addition to isoline contours).

In case anyone has a need for the script I made for other 3D models, I have it at GitHub - ansonl/topo-map-post-processing: Add map isolines and elevation color changes to 3D model G-code

1 Like

Nice model and interesting scripts.

I’ve been working on some similar things relating to amending gcode and colours.

I managed to write a python script that extracts, processes and alters the gcode within the gcode.3mf files that are created from the ‘export all sliced file’ ‘print plate’ option if is it of any use. I presume you must have done something like that in order to get Bambu Studio to preview your amended gcode.

Other thoughts on possibly simpler ways to achieve your ‘different coloured contours’ without altering the gcode are via creating multiple STLs.

Either a) One STL per colour - which are via a script merged or split as required before being imported,
or b) loads of STLs to cover all of the possible layer options - with a script to go through a Bambu Studio project 3mf file to automatically set the correct colours for each of the individual STLs based on input parameters.

I am working on scripts related to both of these options too if you are interested.

Finally it is probably possible to automate the ‘height range’ painting within Bambu Studio - either via a UI macro running on top of Bambu Studio (such as Keyboard Maestro on a Mac), or again patching the Bambu Studio project 3mf files to add in the layer painting externally.

1 Like

@Ukdavewood @3DTech I figured out the gcode needed to do a minimal toolchange that includes the filament cutting and flush. An example of a toolchange for the P1S is below and also at topo-map-post-processing/minimal_toolchanges/toolchange-bare-bambu-p1s.gcode at master · ansonl/topo-map-post-processing · GitHub

A successful test print from a user of my script adding 2 additional colors to an existing 2 color print is below:

; MFPP MINIMAL TOOLCHANGE START
G1 E-.1 F1800
M106 P3 S0

M620 SXXA
M204 S9000

G17
;G2 Z0.6 I0.86 J0.86 P1 F10000 ; spiral lift a little from second lift
G2 I0.86 J0.86 P1 F10000 ; spiral lift a little from second lift
G91
G1 Z0.4
G90
M83

;G1 Z3.2 F1200
G91
G1 Z2.6
G90
M83

G1 X70 F21000
G1 Y245
G1 Y265 F3000
M400
M106 P1 S0
M106 P2 S0

M104 S220

G1 X90
G1 Y255 F4000
G1 X100 F5000
G1 X120 F15000

G1 X20 Y50 F21000
G1 Y-3

M620.1 E F523 T240
TXX
M620.1 E F523 T240


M400

G92 E0

; FLUSH_START
; always use highest temperature to flush
M400

M109 S240


G1 E23.7 F523 ; do not need pulsatile flushing for start part
G1 E0.690105 F50
G1 E7.9362 F523
G1 E0.690105 F50
G1 E7.9362 F523
G1 E0.690105 F50
G1 E7.9362 F523
G1 E0.690105 F50
G1 E7.9362 F523

; FLUSH_END
G1 E-2 F1800

G1 E2 F300



; FLUSH_START
G1 E10.4769 F523
G1 E1.1641 F50
G1 E10.4769 F523
G1 E1.1641 F50
G1 E10.4769 F523
G1 E1.1641 F50
G1 E10.4769 F523
G1 E1.1641 F50
G1 E10.4769 F523
G1 E1.1641 F50
; FLUSH_END
G1 E-2 F1800

G1 E2 F300





; FLUSH_START
M400
M109 S220
G1 E2 F523 ;Compensate for filament spillage during waiting temperature
; FLUSH_END
M400
G92 E0
G1 E-2 F1800
M106 P1 S255
M400 S3
G1 X80 F15000
G1 X60
G1 X80
G1 X60; shake to put down garbage

G1 X70 F5000
G1 X90 F3000
G1 Y255 F4000
G1 X100 F5000
G1 Y265
G1 X70 F10000
G1 X100 F5000
G1 X70 F10000
G1 X100 F5000
G1 X165 F15000; wipe and shake
G1 Y256 ; move Y to aside, prevent collision
M400
;G1 Z3.2 F3000

M204 S500


M621 SXXA

M106 S178.5
M106 P2 S178
M106 P3 S200
G92 E0

G1 E-.04 F1800

G91
G1 Z-2.6 F30000
G90
M83

G91
G1 Z-0.4
G90
M83

G1 E.8 F1800
; MFPP MINIMAL TOOLCHANGE END

The main part to keep in mind is that BambuStudio generates gcode with Extruder position set to relative mode while the printhead XYZ is absolute mode. This isn’t super clear until I saw M83 being used to set only the extruder to relative mode. Coming from Cura, I’m used to the extruder using absolution mode.

There is still confusion on what the Bambu proprietary gcodes actually do. A couple that I’m not sure about but have guesses for are.
M625 Signals the end of a part and a nonprinting portion of code for some Bambu component.
M624 Signals the start of a part and has what may be a base64 encoded data after it but it doesn’t seem to be dynamic or anything meaningful at first glance.
T255 Switch to no filament selected.
T1000 ???
M620.X ?
M621.X ?
Some of these gcode obviously relate to triggering a toolchange in the AMS but I don’t have a Bambu myself.
It would be great if Bambu released a description of their proprietary Gcodes so that users understand what the machine is doing. There isn’t any competitive advantage to having mystery gcodes and Bambu would be able to walk the talk on open source by being more transparent about the gcode. As Bambu already uses an open source slicer to generate the Gcode so the mystery gcode functionality resides in the firmware.

3 Likes

Looks good.

M624/M625 is I think something it uses to separate all of the different objects on the plate that can be individually cancelled if they fail during the print. I would imagine within the encoded part is the object Id (from the comment line above in gcode) - on GitHub it says start_str += ("<mark>M624</mark> " + _encode_label_ids_to_base64({ instance_to_print.label_object_id }));

M620’s are I think commands to the AMS. M620 S255 for example unloads the AMS at the end of the print job (there is another thread about this - and I have just created a little script to post process the gcode and comment this line out).

1 Like

For anyone finding this, I have gathered the Bambu toolchange specific Gcode that affect toolchange functionality at this list topo-map-post-processing/Bambu-gcodes.md at master · ansonl/topo-map-post-processing · GitHub

3 Likes

I got the toolchanges on demand working + cross compatible with PrusaSlicer. See below for a map piece that a friend printed with a X1C.

I’m still doing some more testing with users with Bambu printers. I primarily made this script so that people who download my collection of dual color 3D topo maps can add 2 additional colors through isolines and hi-elevation. If anyone wants to be the first to print a 4 color 3D topo of their state, just send me a message.


The processing ended up being pretty involved since I decided to filter recoloring by feature type such as walls, infill, and top surface and needed to rearrange the features to maintain nozzle pressure and optimize toolchanges.

I hope Bambu’s next printer release has a multi-extruder toolchanger for faster and more consistent toolchanges as each color flush adds to the printing time by a minute.

@3DTech @Ukdavewood

2 Likes