Customizing PrusaSlicer g-code

PrusaSlicer supports the conditional g-code capabilities inherited from PrusaSlicer. This allows you do to a very limited amount of “programming” within the slicer. Note that any conditional gcode is executed by the slicer and not the printer. This means that any values will be determined during slicing. The Slic3r manual page on conditional gcode provides a good summary of the available options for conditional gcode generation.

Start G-Code

The default g-code that ships with PrusaSlicer works just fine, but I found myself wanting to tweak a few things to my liking. I have updated my startup g-code to incorporate several functions that I want to use on every print:

  • A 2-step “no ooze” nozzle warmup routine to expedite startup, but avoid dots and strings caused by molten filament dripping out during the mesh bed leveling and bed warmup processes.

  • Position the nozzle away from the PEI print surface during warmup and startup motion to avoid damaging the print surface.

  • PINDA warmup to ensure a consistent 35C PINA temperature before performing mesh bed leveling to ensure consistent results.

  • A modified prime line print routine to catch errant nozzle ooze, test extrusion and perform a final wipe action to avoid stringing between the prime line and start of the print.

Note

These notes are based on my experiences with the Prusa i3 Mk3 and Artiller/Evnovo Sidewinder X1 printers. If you are using a different printer, please verify the hardware details are same. These pages may be a bit rough as I revise them and add new material. Please check back regularly for updates.

I’ve incorporated these features into my own customized g-code settings for PrusaSlicer.

Caution

Please be sure that you’ve calibrated your printer, and particularly your Live-Z settings before using any of the following examples. These routines move the nozzle around close to the bed, and could cause damage if your printer is not adjusted properly.

I’m not a fan of loud peizo beeps, much less music, but I have inserted several chirp sounds (M300 S100 P10) to indicate progress throughout the print.

Here’s a line-by-line breakdown:

Version control

The first lines are simple date, firmware and hardware setting checks.

  • Line 3 does a printer model check. If you generated gcode using PrusaSlicer configured for a different printer, you’ll receive a warning.

  • Line 4 does a nozzle diameter check. If you generated gcode using PrusaSlicer for a different nozzle size than you’ve set in your printer settings menu, you’ll receive a warning. Note that the firmware currently only supports 0.25, 0.40 and 0.60mm nozzle sizes. I disable this check in the printer settings.

  • Line 5 does a firmware version check. If you print generated gcode on a printer running an older firmware version, you’ll get a warning message to update. You can press the front knob to continue and ignore this warning, but updating to recent firmware is always recommended to take full advantage of enhanced features that Prusa is continually adding to the printer firmware and PrusaSlicer.

Listing 1 PrusaSlicer start g-code version control
1
2
3
4
5
; Last updated 20200215
M300 S40 P10 ; chirp
M862.3 P "[printer_model]" ; printer model check
M862.1 P[nozzle_diameter] ; nozzle diameter check
M115 U3.7.2 ; tell printer latest fw version

Initialize movement and extrusion coordinates

The next stanza sets the initial movement and extrusion coordinates. These aren’t technically required, but if they’re improperly set, they can cause problems with a print.

  • Line 8 sets absolute move coordinates for movement.

  • Line 9 sets relative extruder coordinates.

Listing 2 PrusaSlicer start g-code coordinate initialization
6
7
8
9
M117 Initializing
; Set coordinate modes
G90 ; use absolute coordinates
M83 ; extruder relative mode

Initialization

The next block resets printing parameters that can persist after an aborted job.

  • Line 11 disables volumetric e (autospeed) printing.

  • Line 12 resets the print speed to 100% in case it was changed using the front knob.

  • Line 13 adjusts the extrusion rate (M221) to 90% for layer heights over 0.32mm. I find this necessary using large nozzles with very high layers.

Listing 3 PrusaSlicer start g-code print parameter initialization
10
11
12
13
; Reset speed and extrusion rates
M200 D0 ; disable volumetric e
M220 S100 ; reset speed
M221 S{if layer_height >= 0.32}90{else}100{endif} ; compensate for thick layer heights

Initialize warmup temperatures

The next block sets initial “two step” extruder warmup temperatures to avoid oozing during mesh bed leveling.

  • Line 16 sets the extruder (nozzle) temperature to 160C (S160), a good “no ooze” temp that will soften most filament, but not hot enough to cause filament to trickle out of the nozzle. This will prevent dots of filament and stringing during mesh bed leveling.

  • Line 17 sets the bed temperature to the higher of the specified bed temperature or 80C. This is to ensure that the bed is hot enough to warm up the PINDA even on cold days.

Note

I recently increased this to 80C due to cold weather.) See the PINDA warmup notes for details.

  • Line 19 waits for the nozzle to hit the no-ooze temp before proceeding. This ensures any filament stuck to the nozzle will be soft enough to bend if it hits the PEI print surface.

Listing 4 PrusaSlicer start g-code initial warmup temps
14
15
16
17
18
19
20
; Set initial warmup temps
M117 Nozzle preheat
M104 S160 ; set extruder no-ooze temp
M140 S{max(first_layer_bed_temperature[0],80)}  ; set bed PINDA warmup temp
; Nozzle warmup before home to avoid driving hardened ooze into PEI surface
M109 S160 ; wait for extruder no-ooze warmup temp before mesh bed leveling, cool hot PINDA
M300 S40 P10 ; chirp

Initial homing

The next stanza prepares for initial homing. On startup, the printer has no way of knowing where the extruder is in 3D space, so homing is necessary for it to establish a known position. This is to ensure that any filament hanging from the nozzle is sufficiently soft to either break off or bend when the nozzle is lowered in the next line. This avoids creating dents in the PEI sheet at the lower-left corner.

  • Line 23 performs homing (G28 W), moving the printer to the lowest (Z 0) left (X 0) front (Y 210) coordinates to establish the current position.

Listing 5 PrusaSlicer start g-code nozzle warmup
21
22
23
; Home
M117 Homing
G28 W ; home all without mesh bed level

PINDA warmup

The next stanza waits for the PINDA probe to reach a consistent temperature before automatic mesh bed leveling. For more info on the PINDA warmup procedure, refer to the about PINDA warmup notes.

  • Line 25 raises the nozzle 3mm to avoid possibly damaging the PEI sheet during nozzle movement in the steps that follow.

  • Line 26 moves the nozzle to the center (X 125) back (Y 180) to allow access to the bed for final inspection and cleaning while waiting for the PINDA warmup in the following steps.

Note

I recently moved the resting Y position closer to the bed center to improve PINDA warming.

  • Line 27 lowers the nozzle and PINDA probe close to the bed (Z 0.15mm) to aid in warming up the PINDA probe.

  • Line 30 issues to command (M860 S35) to wait for the PINDA probe to reach at least 35C, an all-around good temperature for mesh bed leveling. Note that this can take a long time (5-10 minutes) on the first print on a cold day. For subsequent prints, it should warm up much more quickly.

  • Line 31 sets the desired bed temp before mesh bed leveling to let the bed cool down to close to the final print temperature.

  • Line 32 raises the nozzle 3mm (Z3) for movement as a precaution before rapid moves close to the print surface.

Listing 6 PrusaSlicer start g-code PINDA warmup
24
25
26
27
28
29
30
31
32
33
; Present bed for final cleaning
G0 Z3; Raise nozzle before move
G0 X125 Y180 F10200; Move nozzle to PINDA warming position
G0 Z0.15 F10200; Lower nozzle to PINDA warming position
; Wait for PINDA warmup
M117 PINDA warmup
M860 S35 ; wait for PINDA temp to stabilize
M140 S[first_layer_bed_temperature] ; set target bed temp
G0 Z3; Raise nozzle before move
M300 S40 P10 ; chirp

Mesh bed leveling and final warmup

Now that the PINDA is warmed up to a consistent temperature, we can perform mesh bed leveling.

  • Line 36 performs the actual leveling (G80). With the latest firmware releases, this will do either 3X3 or 7X7 grid leveling as configured in the printer setup menu. After leveling, the nozzle will return to the home position.

  • Line 37 stores the leveling results for use with the Octoprint Prusa mesh bed leveling plugin.

  • Line 41 raises the nozzle 5mm (Z5) while we wait for the nozzle to come up to full print temperature. This both avoids possibly damaging the PEI sheet under the nozzle, and also provides a clean prime line in the steps that follow.

  • Line 42 sets the final bed temperature to the user-specified temperature specified in the slicer settings. (Redundant with line 31 - to be removed)

  • Lines 43 and 44 set (M104) and wait for (M109) the final nozzle print temperatures specified in the slicer settings.

  • Line 45 waits for the bed temperature to settle before printing.

Listing 7 PrusaSlicer start g-code mesh bed leveling & final warmup
34
35
36
37
38
39
40
41
42
43
44
45
46
; Mesh bed leveling
M117 Mesh bed leveling
G80 ; mesh bed leveling
M117 Saving results
G81 ; save mesh leveling results
; Final warmup routine
M117 Final warmup
G0 Z5; Raise nozzle to avoid denting bed while nozzle heats
M140 S[first_layer_bed_temperature] ; set bed final temp
M104 S[first_layer_temperature] ; set extruder final temp
M109 S[first_layer_temperature] ; wait for extruder final temp
M190 S[first_layer_bed_temperature] ; wait for bed final temp
M300 S40 P10 ; chirp

Prime line

Once the hotend comes up to temp, a custom prime line will be printed. I’ve made some tweaks to the bog-standard line.

  • Line 49 lowers the nozzle (Z0.15) to initial printing position.

  • Line 50 disables Linear Advance (M900 K0) for printing the prime line. With LA enabled, I often get distracting extruder clicks and skips when printing the initial thick line.

  • Line 52 moves to the initial prime line position at the left (X=0) front (Y=-3) bed position.

  • Line 53 extrudes an intial 2mm “blort” of filament without moving (G1 E2 with no X or Y movement) to trap any ooze or string remaing on the nozzle.

  • Line 54 extrudes 6mm of filament (E6) while moving 20mm along the X axis (X20.0).

  • Line 55 extrudes 3.2mm of filament (E3.2) while moving another 40mm along the X axis (X60).

  • Line 56 extrudes 6mm of filament (E6) while moving another 40mm along the X axis (X100).

  • Line 57 retracts 0.8mm of filament (E-0.8) to reduce stringing.

  • Line 58 performs a wipe action back over the printed line along the X axis (X99.5) without extruding (E0).

  • Line 59 moves another 10mm along the X axis (X110.0) without extruding (E0) to complete the wiping action and eliminate any stringing.

  • Line 60 de-retracts (pushes) 0.6mm of filament (E0.6) to re-prime after the previous retraction.

Listing 8 PrusaSlicer start g-code prime line
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
; Prime line routine
M117 Printing prime line
G0 Z0.15 ; Restore nozzle position - (thanks tim.m30)
M900 K0; Disable Linear Advance for prime line
G92 E0.0 ; reset extrusion distance
G1 Y-3.0 F1000.0 ; go outside print area
G1 E2 F1000 ; de-retract and push ooze
G1 X20.0 E6  F1000.0 ; fat 20mm intro line @ 0.30
G1 X60.0 E3.2  F1000.0 ; thin +40mm intro line @ 0.08
G1 X100.0 E6  F1000.0 ; fat +40mm intro line @ 0.15
G1 E-0.8 F3000; retract to avoid stringing
G1 X99.5 E0 F1000.0 ; -0.5mm wipe action to avoid string
G1 X110.0 E0 F1000.0 ; +10mm intro line @ 0.00
G1 E0.6 F1500; de-retract
G92 E0.0 ; reset extrusion distance

Final print adjustments

Finally, we do any last minute print adjustments. I’m not doing anything here. Adjust for your printer.

Listing 9 PrusaSlicer start g-code final print adjustments
62
63
64
65
; Final print adjustments
M117 Preparing to print
M300 S40 P10 ; chirp
M117 Print in progress

At this point, the print will proceed normally.

Full startup g-code example

Here’s the full start g-code:

Listing 10 PrusaSlicer start g-code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
; Last updated 20200215
M300 S40 P10 ; chirp
M862.3 P "[printer_model]" ; printer model check
M862.1 P[nozzle_diameter] ; nozzle diameter check
M115 U3.7.2 ; tell printer latest fw version
M117 Initializing
; Set coordinate modes
G90 ; use absolute coordinates
M83 ; extruder relative mode
; Reset speed and extrusion rates
M200 D0 ; disable volumetric e
M220 S100 ; reset speed
M221 S{if layer_height >= 0.32}90{else}100{endif} ; compensate for thick layer heights
; Set initial warmup temps
M117 Nozzle preheat
M104 S160 ; set extruder no-ooze temp
M140 S{max(first_layer_bed_temperature[0],80)}  ; set bed PINDA warmup temp
; Nozzle warmup before home to avoid driving hardened ooze into PEI surface
M109 S160 ; wait for extruder no-ooze warmup temp before mesh bed leveling, cool hot PINDA
M300 S40 P10 ; chirp
; Home
M117 Homing
G28 W ; home all without mesh bed level
; Present bed for final cleaning
G0 Z3; Raise nozzle before move
G0 X125 Y180 F10200; Move nozzle to PINDA warming position
G0 Z0.15 F10200; Lower nozzle to PINDA warming position
; Wait for PINDA warmup
M117 PINDA warmup
M860 S35 ; wait for PINDA temp to stabilize
M140 S[first_layer_bed_temperature] ; set target bed temp
G0 Z3; Raise nozzle before move
M300 S40 P10 ; chirp
; Mesh bed leveling
M117 Mesh bed leveling
G80 ; mesh bed leveling
M117 Saving results
G81 ; save mesh leveling results
; Final warmup routine
M117 Final warmup
G0 Z5; Raise nozzle to avoid denting bed while nozzle heats
M140 S[first_layer_bed_temperature] ; set bed final temp
M104 S[first_layer_temperature] ; set extruder final temp
M109 S[first_layer_temperature] ; wait for extruder final temp
M190 S[first_layer_bed_temperature] ; wait for bed final temp
M300 S40 P10 ; chirp
; Prime line routine
M117 Printing prime line
G0 Z0.15 ; Restore nozzle position - (thanks tim.m30)
M900 K0; Disable Linear Advance for prime line
G92 E0.0 ; reset extrusion distance
G1 Y-3.0 F1000.0 ; go outside print area
G1 E2 F1000 ; de-retract and push ooze
G1 X20.0 E6  F1000.0 ; fat 20mm intro line @ 0.30
G1 X60.0 E3.2  F1000.0 ; thin +40mm intro line @ 0.08
G1 X100.0 E6  F1000.0 ; fat +40mm intro line @ 0.15
G1 E-0.8 F3000; retract to avoid stringing
G1 X99.5 E0 F1000.0 ; -0.5mm wipe action to avoid string
G1 X110.0 E0 F1000.0 ; +10mm intro line @ 0.00
G1 E0.6 F1500; de-retract
G92 E0.0 ; reset extrusion distance
; Final print adjustments
M117 Preparing to print
M300 S40 P10 ; chirp
M117 Print in progress

End G-Code

My end g-code is pretty standard. Any setting changes made from the front menu will persist between prints if not reset, so I like to ensure everything is set back to 100% when a print job completes. I’ve also added a couple of routines to minimize final stringing, though with mixed success.

  • Line 7 retracts 0.8mm of filament (E-0.8) to reduce final stringing.

  • Lines 8-11 do a little wiping motion with no extrusion to reduce final stringing.

  • Line 13 raises the nozzle 60mm or up to the maximum print area of 210mm, whichever is lower.

  • Line 14 moves the extruder to the far left (X0) and forward (Y210) to present the print for easy removal.

  • Line 16 disables volumetric e extrusion (an experimental setting).

  • Line 17 resets print speed over-rides made with the front knob.

  • Line 18 resets extruder factor adjustments made with the front knob.

  • Line 19 resets Linear Advance.

  • Lines 20-25 shut down the printer hardware.

Note

Thanks to Steve_G for catching the error on that last line.

Here’s my current end g-code:

Listing 11 PrusaSlicer end g-code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
; Last updated 20200215
G4 ; wait
; Raise nozzle and present bed
M117 Printing complete
G4 ; wait
G92 E0 ; prepare to retract
G1 E-0.8 F3000; retract to avoid stringing
; Anti-stringing end wiggle
G91 ; use relative coordinates
G1 X-0.5 Y-0.5 F1200
G1 X1 Y1 F1200
G90 ; use absolute coordinates
{if layer_z < max_print_height}G1 Z{z_offset+min(layer_z+60, max_print_height)}{endif} ; Move print head up
G0 X0 Y210 F10200; present bed
; Reset print setting overrides
M200 D0 ; disable volumetric e
M220 S100 ; reset speed factor to 100%
M221 S100 ; reset extruder factor to 100%
M900 K0 ; reset linear acceleration
; Shut down printer
M104 S0 ; turn off temperature
M140 S0 ; turn off heatbed
M107 ; turn off fan
M84 ; disable motors
M300 S40 P10 ; chirp

Other custom G-Code

You can also embed custom g-code at before and after every layer change, on tool (nozzle) changes and between sequentially-printed objects.

Here’s an example of using before layer change G-code for changing temperatures on a temperature tower. This model consists of 10mm segments which are printed starting at 235C and decreasing by 5C at each 10mm layer. Note that layer_z is the PrusaSlicer placeholder for the current nozzle height in mm, not layers.

When inserted int PrusaSlicer under Printer Settings->Custom G-code->Before layer change G-code, this code compares the current height to the height at which each segment begins (multiples of 10mm at the start of each layer. The first range matched will terminate the tests, so we start testing for the lowest tiers first (10mm) and working our way up to the highest (110mm), settings extruder temps along the way (M104 Sxxx).

Listing 12 PrusaSlicer layer change g-code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
;BEFORE_LAYER_CHANGE
; layer_z is height of current layer in mm
G92 E0.0
;[layer_z]
{if layer_z <= 10}M104 S235
{elsif layer_z <= 20}M104 S230
{elsif layer_z <= 30}M104 S225
{elsif layer_z <= 40}M104 S220
{elsif layer_z <= 50}M104 S215
{elsif layer_z <= 60}M104 S210
{elsif layer_z <= 70}M104 S205
{elsif layer_z <= 80}M104 S200
{elsif layer_z <= 90}M104 S195
{elsif layer_z <= 100}M104 S190
{elsif layer_z <= 110}M104 S185
{endif}

PrusaSlicer placeholders

Todo

More placeholder documentation.

Here are the most commonly used placeholders (slice-time variables) used by PrusaSlicer.

Table 1 PrusaSlicer G-Code Placeholders

Setting

Placeholder

Notes

Bed Temperature

[bed_temperature]

Bed temperature for all layers, over-ridden by first layer setting.

First Layer Temperature

[first_layer_temperature]

First Layer Bed Temperature

[first_layer_bed_temperature]

Prusa Printer Model

[printer_model]

Configured nozzle diameter

[nozzle_diameter]

Used at print time to compare slicer and current printer nozzle sizes. Currently limited to 0.25, 0.40 and 0.60mm nozzle sizes.

Current layer number

[layer_num]

Used only in layer change gcode.

Current layer height

[layer_z]

Used only in layer change gcode.

Contact and feedback

You can find me on the Prusa support forums or Reddit where I lurk in many of the 3D printing-related subreddits.

Last updated 20200505