2.2.12. Customizing PrusaSlicer g-code

Customize gcode in PrusaSlicer for greater control of the 3D printing process.

PrusaSlicer allows customizing g-code to be inserted at different times during a print:

  • Start G-code is inserted at the start of a print job. It is used to heat the nozzle and bed, perform mesh bed leveling, print a prime line and any other steps that need to be done before a print starts.

  • End G-code is inserted at the end of a print job. It is used to turn of heaters, park the nozzle and turn off stepper motors.

  • Before layer change G-code is inserted into a print job before each new layer is printed. It is usually left blank, but can be used to insert comments or change settings such as temperatures when printing temperature towers.

Important

If you are calibrating your filament settings for a Prusa i3 Mk3-series printer, read up on extrusion multiplier adjustment notes for the PrusaSlicer default system presets.

2.2.12.1. Slicer Placeholders

If you’re coming to 3D printing with a background in programming, you’re probably looking for variables. These are known as “placeholders” in 3D printing, and are only interpreted and applied at slice time. You can’t create new placeholders, and you can’t change the values of placeholders. They are essentially immutable objects, or constants.

  • Placeholders are a feature of the slicer, not the g-code language.

  • The available placeholders depend entirely on the slicer you are using. Every slicer supports different features and configuration settings. Equivalent features may not be available between slicers.

  • Placeholder names for identical settings differ between slicers.

  • The syntax for using placeholder varies with every slicer. In PrusaSlicer, placeholder names must be surrounded by square brackets ([]). In Ultimaker Cura, placeholder names must be surrounded by curly braces ({}).

Unfortunately, this means that you will need to spend some time learning the supported features and syntax for every slicer.

2.2.12.2. PrusaSlicer placeholders

PrusaSlicer offers a large number of placeholders that you can use in custom g-code.

  • Profile settings for printers, prints, and filaments are available as placeholders. Any value that can be set in a profile is available.

  • A small number of slice-time placeholders are available to identify the current layer number or z-height during slicing.

2.2.12.2.1. Configuration placeholders

Most settings in PrusaSlicer can used as placeholders. There are two ways to identify the available setting names.

  • When using PrusaSlicer, hover your mouse over a setting field, the pop-up tooltip help will provide a summary of the setting and identify a parameter name.

  • If you open a .gcode file sliced with PrusaSlicer, the settings used for slicing are listed at the end of the file. Any parameter name (to the left of the equals sign (=) can be used as a placeholder.

When writing gcode, enclose placeholders within square brackets (e.g. [layer_height]). At slice time, the placeholder will be replaced with the value of that setting in the generated gcode. (An exception is when using placeholders in conditional checks. See below.)

Here are the most commonly used PrusaSlicer settings placeholders.

Table 2.1 PrusaSlicer Settings 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]

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.

2.2.12.2.2. Slice-time placeholders

A number of values can be used to insert values into your gcode while slicing. These are useful, for example, to insert gcode based on the current layer number or layer height.

Here are the most commonly used PrusaSlicer slice-time placeholders.

Table 2.2 PrusaSlicer Settings Placeholders

Setting

Placeholder

Notes

Current layer number

[layer_num]

The height of the current layer in mm.

Current layer height

[layer_z]

The height of the top of current layer in mm.

2.2.12.3. Conditional g-code

PrusaSlicer – originally based on Slic3r – supports some basic conditional g-code generation. You can do to a very limited amount of “programming” using PrusaSlicer’s if, elsif, else, or endif syntax.

A few notes on syntax:

  • Each if, elsif, else, or endif statement must be enclosed within curly braces ({}).

  • Any gcode appearing between statements will be included if the preceding condition is true.

  • A single line of gcode can appear between conditions on a single line.

  • Multiple lines of gcode can appear on multiple lines, surrounded by conditions.

  • Placeholders within condition checks (within curly braces) are not surrounded by square brackets ([]).

  • Else-if conditions are written as elsif.

Here’s a working example showing basic if-else syntax:

1{if printer_model=~/.*MK3S.*/}
2M117 Mk3S detected, no warmup required
3{else}
4M117 Mk3 Waiting for PINDA warmup
5M860 S35 ; wait for PINDA temp to stabilize
6{endif}

Here’s another example showing if-elsif syntax:

 1{if layer_z <= 10}M104 S235
 2{elsif layer_z <= 20}M104 S230
 3{elsif layer_z <= 30}M104 S225
 4{elsif layer_z <= 40}M104 S220
 5{elsif layer_z <= 50}M104 S215
 6{elsif layer_z <= 60}M104 S210
 7{elsif layer_z <= 70}M104 S205
 8{elsif layer_z <= 80}M104 S200
 9{elsif layer_z <= 90}M104 S195
10{elsif layer_z <= 100}M104 S190
11{elsif layer_z <= 110}M104 S185
12{endif}

Todo

  • Finish conditional g-code

  • Operators

  • Regular expressions

  • Functions (min, max, int)

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. I occasionally drop into the Official Prusa 3D discord server where I can be reached as bobstro (bobstro#9830). You can email me directly at projects@ttlexceeded.com.

Last edited on Apr 03, 2021. Last build on Oct 22, 2021.