RegExp based G-Code Substitution like in PrusaSlicer
Is there an existing issue for this feature request?
- [X] I have searched the existing issues
Is your feature request related to a problem?
It's related to some extents to: #6863 - Auxiliary Fan pin cannot be set #6864 - Pre-heat doesn't work on RepRapFirmware #6773 - Add Z coordinate when come back to purge tower
There are also many other small customizations that can be improved and bugs that can be worked-around by substituting G-Code after generation.
Which printers will be beneficial to this feature?
All
Describe the solution you'd like
Porting the existing G-Code Substitution feature from PrusaSlicer to OrcaSlicer.
Describe alternatives you've considered
We considered using the Script Post-Processing feature, but it's not a portable solution. It requires additional software to execute external scripts, which need to be put in a specific folder. This makes harder to share a complete printer profile.
Additional context
We are aware of two related issues (#2235 and #903), but they were either closed after addressing only the primary issue or completely ignored. Therefore, we are opening a new issue. This feature would be highly useful in ensuring OrcaSlicer works as expected, even if there is a low-priority bug or missing feature. It would also be much easier than relying on Script Post-Processing and especially helpful if you need to share a printer profile on multiple computers.
As I mentioned in #2235 Yes this would be very useful, now I had to take harder way and use Post processing script, which makes sharing profiles difficult. (setting up scripts to work is not easy for everyone)
This would be great to have so I can adjust extrusion ratio for individual feature types.
Orca bot: this issue is stale because it has been open for 90 days with no activity.
This should not be closed, feature would be very useful as I stated earlier.
I am developing a printer that needs some custom g-code to achieve certain functionality. https://www.youtube.com/watch?v=SmmD324tIl0&t=0s
Also jumping in here to say I'd like to see this feature ported over from Prusa. It's helped me a ton in the past with some development work.
Yes this would be a game changer. Even replace the brute force block removal in this PR. https://github.com/OrcaSlicer/OrcaSlicer/pull/12455
Compatibility issues that are per printer could be saved in the printer config file and distributed as default configurations removing many on the unique or niche PRs that will never get merged.
Additionally, this can replace simple scripts that just use python as a wrapper for regex. Not only is this a speed up due to using C over python it is safer as it does not execute arbitrary code. Likely this is the reason post processing fixes are not bundled with the default configuration files as fixes.
Experimental idea of a post processing regex feature here. Will likely create a draft PR. Based on v2.4.0 updated to v2.4.1. https://github.com/zekrom-vale/OrcaSlicer/tree/feature/gcode-substitutions
FORMAT
Each line: s/find/replace/flags (regex) or l/find/replace/flags (literal) Delimiter can be any character after s or l. Choose a delimiter that does not appear in the pattern or replacement string. There is no escape mechanism for the delimiter. e.g.: s@G1@G0@i or s#OLD#NEW# or s|find|replace|i Comments: anything after the 4th delimiter is ignored (useful for inline documentation). e.g.: s/G1/G0/i/ ; replace G1 with G0
SYNTAX FLAGS
i = case-insensitive n = no-sub-match (disables capture group tracking, improves performance when you don't need ${1} etc. in the replacement) c = collate (uses the current locale for character comparison) m = multiline (^ and $ match at line boundaries within the chunk, not just at the start/end of the entire chunk)
INLINE MODIFIER
s = match-newline (adds (?s) to regex, making . match newline characters)
FORMAT FLAGS
f = format-first-only (replace only the first match, then stop) 'f' replaces only the first match within each chunk (layer or color block), which means the same substitution can fire once per chunk — effectively duplicating the action across chunks.
BLOCK FLAGS
L = layer block — substitution is applied per layer chunk. The G-code is split at ; CHANGE_LAYER / ;LAYER_CHANGE markers. Each layer's content is processed independently. Preamble (before first layer) and suffix (after last layer) are also treated as layer chunks. C = color/toolhead block — substitution is applied per color chunk within each layer. Color chunks are delimited by ; CP TOOLCHANGE START. Color rules only apply within layers (not to preamble or suffix). If both L and C are set on the same rule, L takes precedence. Color chunks are split at layer boundaries, so a color rule cannot match content that crosses a layer boundary. Rules without L or C flags are treated as layer-level rules (applied per layer chunk).
ESCAPE SEQUENCES
Processed in literal find strings and ALL replace strings: \ = newline, \r = carriage return, \t = tab, \\ = backslash \\ = double quote, \' = single quote \{ = literal open brace, \} = literal close brace ⚠ Regex find strings do NOT process escape sequences — the regex engine handles \d, \s, , etc. directly. Use \{ and \} for literal braces in regex patterns (these are preserved and passed to the regex engine).
PROCESSING
All rules are processed in a chunked full-file pass after the G-code file is written. The G-code is split at layer boundaries (; CHANGE_LAYER / ;LAYER_CHANGE) and color boundaries (; CP TOOLCHANGE START). Each chunk is processed independently, allowing multiline matches and newline insertion.
CAPTURE GROUPS
Use ${1}, ${2}, ${3}, etc. in the replacement string to reference capture groups from the regex find pattern. Use the 'n' flag if you don't need capture groups (improves performance).
ERRORS AND WARNINGS
- Empty find patterns are skipped with a warning logged.
- Invalid regex patterns throw an error and abort slicing.
- Lines not starting with 's' or 'l' are silently ignored.
Examples
#https://github.com/MisterCrabExE/GCODEAdapterForKobraOS/
s/SET_VELOCITY_LIMIT[^\n]*ACCEL=([-\d.]+)[^\n]*SQUARE_CORNER_VELOCITY=(\d+)[^\n]*/M204 S\1 ;Set velocity value for KobraOS\nM205 X\2 Y\2 ;Set SCV value for KobraOS/
s/SET_VELOCITY_LIMIT[^\n]*ACCEL=([-\d.]+)[^\n]*/M204 S\1 ;Set velocity value for KobraOS/
s/SET_VELOCITY_LIMIT[^\n]*SQUARE_CORNER_VELOCITY=(\d+)[^\n]*/M205 X\1 Y\1 ;Set SCV value for KobraOS/
s/SET_PRESSURE_ADVANCE ADVANCE=([0-9.]+)[^\n]*/M900 K\1 ;Override pressure advance value for KobraOS/
# Fix filament_colour_type = 1;1;1;1... to 1,1,1,1 ...
s/(?:(^;\s*filament_colour_type\s*=\s*)|\G\D)(\d+)/${1}${2},//Replace anything to ,
s/(^;\s*filament_colour_type\s*=\s*(?:,?\d+)+)[^\n\d]/${1}//Remove trailing
Adding some macro support https://github.com/zekrom-vale/OrcaSlicer/tree/feature/gcode-substitutions-macro
Man that was not easy. But I got it to dump 600+ macro vars and resolve s/^/\n; {filament_colour_type[current_extruder]}\n/C. Working on other issues, features, and security holes. While I understand this is a powerful tool, it is running arbitrary code, all be it regex and locked to a file. It cannot be loaded from a 3mf file. While custom gcode is more obvious, this regex is a lot less. However, the black box system still exists for post processing scripts, there is no validation done. It just sends the gcode. Though, perhaps it could be different with diffs. Show the user what is different before sending it.
This issue has had no activity for 90 days, so it will be closed in 14 days.
If it's a bug that still happens on the latest OrcaSlicer release, please leave a comment with the version you tested. If it's a feature request you still want, please leave a comment saying so. Either one keeps it open.