Skip to content

Start End G‐Code

Erwan MATHIEU edited this page Sep 6, 2024 · 2 revisions

Cura allows you to add some custom G-Code parts into your generated files. This can be very handy to setup some specific features of a printer, or to customize the way your printer starts according to your workflow. Custom G-Code can be inserted at different points:

  • Machine Start G-Code: inserted at the beginning of the file, before any other G-Code command
  • Machine End G-Code: inserted at the end of the file, after any other G-Code command
  • Extruder Start G-Code: inserted every time an extruder starts being used (can be different per extruder)
  • Extruder End G-Code: inserted every time an extruder stop being used (can be different per extruder)

⚠️ Editing the G-Code manually can damage your printer. Always check the G-Code output before sending it to the printer.

How to set custom G-Code

In Cura, select your active printer, then go to Preferences > Configure Cura... > Printers > Machine Settings

image

In the Printer tab, you can set the Machine Start and End G-Code.

In each extruder's tab, you can set the Extruder Start and End G-Code.

⚠️ Some printers have custom G-Code setup by the manufacturers, with custom commands that may be necessary. Remove them only if you know what they are used for.

Syntax

Static G-Code

The simplest syntax is to just add static G-Code commands that will be directly inserted into the file without any transformation, for example:

G90 ; use absolute positioning
M82 ; absolute extrusion mode
G28 ; home all
G29 ; mesh bed leveling

Settings values

Classic settings

In order to make the custom G-Code more flexible, you can also insert the value of settings that will be resolved at run-time with the proper output. Just use the raw name of the setting (that can be found in the definition files) and add {} around it:

{<setting_name>}

For example, you can write the following:

M140 S{material_bed_temperature_layer_0} ; set bed temp

The output will be something like:

M140 S50.0 ; set bed temp

When doing so, the value of the setting will be retrieved from the global stack, which contains the settings that are global to the printer. However, sometimes you may want to get the value of a setting specific to an extruder, especially when you have a multi-extruders printer. You can then use the following syntax:

{<setting_name>, <extruder_index>}

ℹ️ The extruder index is 0-based, which means "Extruder 1" in the UI will have index 0, "Extruder 2" will have index 1, and so on.

You can then write the following:

M109 T0 S{material_print_temperature_layer_0, 0}
M109 T1 S{material_print_temperature_layer_0, 1}

Which could output to:

M109 T0 S230.0
M109 T1 S210.0

ℹ️ The extruder_index can also be retrieved from a setting value

⚠️ This does not apply to settings that are marked as "settable_per_extruder": false, for them we will always use the value from the global stack

Extra settings

In addition to settings that are declared in definition files, some extra settings are added at run-time that you can use for convenience:

  • material_id: ID of the used material (per extruder)
  • material_type: Type of used material (per extruder)
  • material_name: Name of used material (per extruder)
  • material_brand: Brand of used material (per extruder)
  • quality_name: Name of the used quality profile
  • quality_changes_name: Name of the specific applied quality changes profile
  • print_bed_temperature: Alias to material_bed_temperature
  • print_temperature: Alias to material_print_temperature
  • travel_speed: Alias to speed_travel
  • time: Current time, using format %H:%M:%S, e.g. 09:17:35
  • date: Current date, using format %d-%m-%Y, e.g. 06-09-2024
  • day: First three letters of the current day, e.g. Fri
  • initial_extruder_nr: Index of the first used extruder (which may sometimes be incorrect)

Formulas

Starting with Cura 5.6, you can also write simple formulas to tweak the values of the setting for a specific purpose. Formulas are based on Python syntax and are widely used in printers settings definitions. For example, you can write:

M104 T0 S{material_print_temperature_layer_0 - 50, 0}
M104 T1 S{material_print_temperature_layer_0 - 50, 1}
M190 S{material_bed_temperature_layer_0 / 2}
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0 + 10, initial_extruder_nr}
M109 T{(initial_extruder_nr + 1) % 2} S{material_print_temperature_layer_0 - 20, (initial_extruder_nr + 1) % 2}

Conditional parts of G-Code

Starting with Cura 5.9, you can make some parts of your custom G-Code dependant on some conditions, which can be handy to enable or disable a whole feature of your printer. The syntax for this is slightly more complex:

{if|elif|else|endif <condition_formula>, <extruder_index>}

These statements will be evaluated and not exported to the file, but will enable or disable the following lines, for example:

{if material_bed_temperature_layer_0 < 40}
M190 S{material_bed_temperature_layer_0}
{elif material_bed_temperature_layer_0 >= 40 and material_bed_temperature_layer_0 < 50}
M190 S{material_bed_temperature_layer_0 - 10}
M140 S{material_bed_temperature_layer_0}
{else}
M140 S{material_bed_temperature_layer_0}
{endif}
Clone this wiki locally