What is Start/End G-code?
When your print starts or finishes, the printer runs G-code - a script that safely starts everything up or shuts everything down. It turns on/off heaters, retracts filament, takes time-lapse photos, and puts the printer into printing or idle mode.
We can modify these scripts to automatically eject parts after each print.
How It Works
After the print completes and heaters turn off, the toolhead positions at the back of the build area. The Z-axis adjusts to different heights, and Y-axis movement creates a sweeping action where the nozzle pushes the part toward the front edge. The part falls off into a collection bin. Multiple sweeps at different Z-heights ensure parts of varying sizes get cleared successfully.
We must also add a purge block and modify the start G-code as the standard purge line is too small to get auto ejected and could mess up future prints.
The Modification
Note: This example is specifically for the Bambu P1S, but the concepts apply to all printers with Y-axis bed movement. More printer-specific examples will be rolling out in the future.
1. Open Bambu Studio
-
Go to Prepare
-
Go to Printer Settings
-
Find Machine end G-code section
2. Locate the start G-code Insertion Point
Find this line in your end G-code:
;===== nozzle load line ===============================
3. Replace it with the Purge Cube Code
;===== nozzle purge rectangle =============================== M975 S1 G90 M83 T1000 G1 X115.35 Y1 Z0.8 F18000 ; Move to start position (front-center) M109 S{nozzle_temperature_initial_layer[initial_extruder]} G1 Z0.2 G92 E0 ; Single layer outline - 1" wide x 0.1" deep (25.4mm x 2.54mm) G1 X140.75 E5.0 F800 ; bottom edge (1" = 25.4mm) G1 Y3.54 E0.5 F800 ; right edge (0.1" = 2.54mm) G1 X115.35 E5.0 F800 ; top edge G1 Y1 E0.5 F800 ; left edge (back to start) M400 ;===== nozzle purge rectangle end =========================== |
4. Locate the end G-code Insertion Point
Find this line in your end G-code:
M104 S0 ; turn off hotend
5. Add the Bed Clearing Code
Insert this immediately AFTER the M104 S0 line:
; --- Auto-Clear Bed Sequence --- M400 ; wait for moves to complete G90 ; absolute positioning ; Sweep 1 - 90% of print height G1 X128 Y220 F6000 ; move to back center G1 Z{max_layer_z * 0.9} F3000 ; adjust based on part height G1 Y0 F1500 ; push forward ; Sweep 2 - 60% of print height G1 X128 Y220 F6000 ; return to back G1 Z{max_layer_z * 0.6} F3000 ; lower position G1 Y0 F1500 ; push forward ; Sweep 3 - 40% of print height G1 X128 Y220 F6000 ; return to back G1 Z{max_layer_z * 0.4} F3000 ; lower position G1 Y0 F1500 ; push forward ; Sweep 4 - 20% of print height G1 X128 Y220 F6000 ; return to back G1 Z{max_layer_z * 0.2} F3000 ; near bottom G1 Y0 F1500 ; final push M400 ; wait for moves to complete ; --- End Auto-Clear --- |
6. Add the Purge Clearing Block
Place a small clearing block or object directly on top of the purge line. This ensures that, at the end of the print, the purge line is removed along with the bed-clearing code. If left behind, the purge line can interfere with the next print.
For example, on the Bambu P1S the purge line is located at the front of the build plate, so I positioned a small block at the front, aligned with the bed-clearing path.
Testing & Usage
First Test:
-
Print a small, cheap part (Example Code For Download: Auto_Eject_Benchy.3mf)
-
Watch the clearing sequence to ensure it works
-
Adjust if needed
Next Steps
Dialing It In
The code above provides a solid starting point, but you'll likely need to adjust the values for your specific parts and setup. Every printer and part geometry is different, so expect to fine-tune:
-
Z-heights - Adjust the percentages based on your part size and shape
-
Y-positions - Modify to match your build area dimensions
-
Push speed - Tune for reliable clearing without jamming
-
Number of sweeps - Some setups may only need 2-3 sweeps instead of 4
-
X-position (centering) - This setup sweeps at X128 (center of bed). If your prints are offset to one side, you may need to adjust the X value to align with where your parts actually sit
Pro Tips:
Tilt Your Printer
Consider tilting your printer slightly forward (raising the back by 1-2 inches). This creates a downward slope that helps parts slide off more easily after being pushed. Gravity becomes your friend, reducing the chances of parts landing back on the build surface.
Secure the Head Cover
The head cover is held on with small magnets and may come loose during operation. In most cases this isn’t an issue, but if you’re running high-volume or continuous jobs, add a piece of tape to keep the cover firmly in place. This simple step helps prevent the cover from falling off unexpectedly while the machine is running.