2025-05-16 14:05:29 -06:00
---
description: Variable Substitutions and Expressions in GCode Macros
---
# GCode Macros
2025-05-17 18:21:09 -06:00
## All Modes
2025-05-16 14:05:29 -06:00
2025-05-17 19:03:05 -06:00
### "Header"/"Footer" Only Macros
2025-05-16 14:05:29 -06:00
- \{top\} = offset in mm of bed top Y axis
- \{left\} = offset in mm of bed left X axis
- \{right\} = offset in mm of bed right X axis
- \{bottom\} = offset in mm of bed bottom Y axis
2025-05-17 19:03:05 -06:00
### "Footer" Only Macros
2025-05-16 14:05:29 -06:00
- \{time\} = job run time (printing/milling) in seconds (fractional)
- \{print-time\} = alias for \{time\} ... deprecated after 2.8
- \{print_time\} = alias for \{time\} ... 2.9 and beyond
2025-05-17 18:21:09 -06:00
## FDM (3D Printing) Mode Only
2025-05-17 19:03:05 -06:00
### FDM Macros
2025-05-16 14:05:29 -06:00
- \{temp\} = hot end temperature
- \{bed_temp\} = bed temperature
- \{fan_speed\} = active cooling fan speed (usually 0-255)
- \{material\} = length in mm of material (filament) used
- \{layers\} = total # of layers
- \{layer\} = current layer number
- \{height\} = current layer height in mm
- \{range(_from_,_to_)\} = evaluated using \{layer\} and \{layers\}
- \{pos_x\} = last output X position
- \{pos_y\} = last output Y position
- \{pos_z\} = last output Z position
- \{progress\} = 0-100% state of print output
- \{tool\} | \{nozzle\} = current tool in use
- \{tool_count\} = number of tools used in print
- \{tool_used\__n_\} = whether tool # _ n _ is used in print
- \{z_max\} = max build height in mm
- \{z\} = current z position
- \{e\} = amount of filament extruded
2025-05-17 19:03:05 -06:00
### "Feature" only Macros (v3.4+)
- \{feature\} = feature region of the print (brims, infill, etc)
- \{minx|miny|maxx|maxy\} = position in mm of extents of the print area
2025-05-16 14:05:29 -06:00
2025-05-17 19:03:05 -06:00
### Logical Code Flow (IF / ELIF / ELSE / END)
2025-05-16 14:05:29 -06:00
```
;; IF { layer >= 10 && layer <= 20 }
;; ..... inside 10-20 layer={layer}
;; ELIF { layer >= 15 && layer <= 25 }
;; ..... inside 15-25 layer={layer}
;; ELSE
;; ..... did not match previous tests layer={layer}
;; END
```
2025-05-17 19:03:05 -06:00
### PREAMBLE control (v3.4+)
Allows for intro comment and config list to be re-positioned after the header or disabled. This was introduced to allow GCode output to work with Ultimaker.
2025-05-16 14:05:29 -06:00
2025-05-17 19:03:05 -06:00
`;; PREAMBLE OFF`
2025-05-16 14:05:29 -06:00
2025-05-17 19:03:05 -06:00
`;; PREAMBLE END`
2025-05-16 14:05:29 -06:00
2025-05-17 18:21:09 -06:00
## CAM Mode Only
2025-05-16 14:05:29 -06:00
2025-05-17 18:21:09 -06:00
### CAM Macros
2025-05-16 14:05:29 -06:00
- \{tool\} = CAM tool #
- \{tool_name\} = CAM tool name
- \{time\} = dwell time in seconds
- \{time_ms\} = dwell time in milliseconds
- \{time_sec\} = dwell time in seconds
- \{speed\} = spindle speed
- \{spindle\}, \{rpm\} = spindle speed (v2.7+)
- \{feed\} = last output feed rate
- \{pos_x\} = last output X position
- \{pos_y\} = last output Y position
- \{pos_z\} = last output Z position
2025-05-17 19:03:05 -06:00
### CAM Header Directives
Comments Rewrite (v3.8+) converts `;` comments into `()` parenthesis format
`;; COMMENT_REWRITE_PARENS`
Minimize the size of GCode output (v3.8+)
`;; COMPACT-OUTPUT`
Set decimal precision (n = integer) (v3.8+)
`;; DECIMALS = n`
### Axis Scaling (v3.7+)
2025-05-17 18:21:09 -06:00
Allows for a factor to be applied to X,Y,Z coordinates. Useful for some machines like the Roland MDX-40A that uses an unusual coordinate space. Default axis scale is `1`
2025-05-16 14:05:29 -06:00
_`;; SCALE { "X":100, "Y":100, "Z":100 }` _
2025-05-17 18:21:09 -06:00
## CAM & FDM Modes
### Simple Algebraic Expression Support
2025-05-16 14:05:29 -06:00
- Text inside `{}` is evaluated algebraically with access to JS classes and methods
- `{Math.min(layer/layers, 0.5) + 1}`
- `{token+n} {token-n}`
2025-05-17 18:21:09 -06:00
### Axis Remapping (v3.5+)
Allows for over-riding the default axis names. Useful for swapping axes and changing output for specific firmware targets. The format of the map is a JSON object._
`;; AXISMAP {"X":"Y", "Y":"Z", "E":"E1"}`