Skip to content

Instantly share code, notes, and snippets.

@frankbags
Last active November 14, 2023 02:19
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save frankbags/c85d37d9faff7bce67b6d18ec4e716ff to your computer and use it in GitHub Desktop.
Save frankbags/c85d37d9faff7bce67b6d18ec4e716ff to your computer and use it in GitHub Desktop.
Cura post processing script to resize bed mesh to print size

BEWARE

I have not tested this outside of comparing the gcode between SuperSlicer and Cura. Values were within 1mm of of each corresponding value.

I don't know python and have no interest in learning it, hopefully someone can clean this up.

CURA START G-CODE

START_PRINT BED_TEMP={material_bed_temperature_layer_0} EXTRUDER_TEMP={material_print_temperature_layer_0} AREA_START_X=%MINX% AREA_START_Y=%MINY% AREA_END_X=%MAXX% AREA_END_Y=%MAXY%

import re #To perform the search and replace.
from ..Script import Script
class MeshPrintSize(Script):
def getSettingDataString(self):
return """{
"name": "Mesh Print Size",
"key": "MeshPrintSize",
"metadata": {},
"version": 2,
"settings":{}
}"""
def execute(self, data):
minMaxXY = {'MINX':0,'MINY':0,'MAXX':0,'MAXY':0}
lineData = ''
for layer_number, layer in enumerate(data):
for k,v in minMaxXY.items():
result = re.search(str(k)+":(\d*\.?\d*)",layer)
if result is not None:
minMaxXY[k] = result.group(1)
areaStartGcode = re.search(".*%(MINX|MAXX|MINY|MAXY)%.*",layer)
if areaStartGcode is not None:
if not lineData:
lineData = layer
for k, v in minMaxXY.items():
pattern3 = re.compile('%' + k + '%')
lineData = re.sub(pattern3, v, lineData)
data[layer_number] = lineData
return data
@dapostol73
Copy link

dapostol73 commented Apr 26, 2022

Without seeing your start code, it is hard to say. Make sure you call your bed leveling macro after you have homed all axis. Here is a snippet from my start codr

​    G28 X0 Y0 ​;​move X/Y to min endstops
​    G28 Z0 ​;​move Z to min endstops
​    BED_MESH_CALIBRATE ​AREA_START​={MINX},{MINY} ​AREA_END​={MAXX},{MAXY} ​;​do bed leveling

You can find my config file here for reference

https://github.com/dapostol73/Randoms/blob/main/Klipper/configs/printer-eryone-thinker-series-current.cfg

@ORGATHM
Copy link

ORGATHM commented May 1, 2022

t_hanks for this nice plugin. maybe you can extend it, with a checkbox for following option:

please translate the START_PRINT BED_TEMP={material_bed_temperature_layer_0} EXTRUDER_TEMP={material_print_temperature_layer_0}
to the real temperature. i dont know why, but {material_bed_temperature_layer_0} and {material_print_temperature_layer_0}
are not translated in the output g-code. that suxx.
my klipper does not work propperly, without a number, like BED_TEMP=100.
in klipper i import the varables, with attched sequence.
everything works fine when is set BED_TEMP=100, but this {material_bed_temperature_layer_0} does not work.

after fiddling hours and hours, with manual setted BED_TEMP=100 & EXTRUDER_TEMP=250
I inspecting the generated g-code and found 4 lines automaticly inserted by cura. the bed temp und extruder temp.
and it is the bad M190 and M109. thats prevented mesh leveling while heating. and that costs much extra time.
after a research, i found that cura will insert 4 lines, if in the g-code start textfield {material_print_temperature_layer_0} & {material_bed_temperature_layer_0} is missing.

(https://community.ultimaker.com/topic/18559-changing-cura-generated-start-code/?do=findComment&comment=301886)

i tricked cura a bit, to prevent this 4 lines. unfortunely i have to set the temp manualy in cura g-code start and cant read the variable.
that is the point where you are coming into the game.

actually i use this quick and dirty trick.
START_PRINT EXTRUDER_TEMP=250 BED_TEMP=100 FAKEA={material_print_temperature_layer_0} FAKEB={material_bed_temperature_layer_0} AREA_START=%MINX%,%MINY% AREA_END=%MAXX%,%MAXY%

i set the temps manually, and i present cura the needed {material_print_temperature_layer_0}{material_bed_temperature_layer_0} and send it to fake variables. this supresses the 4 rows... and the m190&109

but it would be nice, when the temperatures are gotten from my print profil, instead of hardcoding it into the start script.
maybe with a placeholder like:

START_PRINT EXTRUDER_TEMP=%ext_temp% BED_TEMP=%bed_temp% FAKEA={material_print_temperature_layer_0} FAKEB={material_bed_temperature_layer_0} AREA_START=%MINX%,%MINY% AREA_END=%MAXX%,%MAXY%

python is not mine. but i think its easy for you ;-)

printer.cfg snippet

............snip..................working...with...cura..&..Superslicer......
[gcode_macro START_PRINT]
gcode:
{% set BED_TEMP = params.BED_TEMP|default(100)|float %}
{% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(240)|float %}
{% set MINX = params.MINX|default(0)|float %}
{% set MAXX = params.MAXX|default(0)|float %}
{% set MINY = params.MINY|default(0)|float %}
{% set MAXY = params.MAXY|default(0)|float %}
# preheat, homing, etc
M117 Heating... ; send message to display
M140 S{BED_TEMP} ; set bed temp in background, do not wait
M105 ; report temperature
M104 S{EXTRUDER_TEMP} ; set extruder temp
..........snip............................................_

@PhilBaz
Copy link

PhilBaz commented May 1, 2022

thanks for this nice plugin. maybe you can extend it, with a checkbox for following option:

please translate the START_PRINT BED_TEMP={material_bed_temperature_layer_0} EXTRUDER_TEMP={material_print_temperature_layer_0} to the real temperature. i dont know why, but {material_bed_temperature_layer_0} and {material_print_temperature_layer_0} are not translated in the output g-code. that suxx. my klipper does not work propperly, without a number, like BED_TEMP=100. in klipper i import the varables, with attched sequence. everything works fine when is set BED_TEMP=100, but this {material_bed_temperature_layer_0} does not work.

It works fine for me....I think you need the printer settings plug-in.
Screenshot 2022-05-01 073922

@frankbags
Copy link
Author

Those are placeholder values from Cura, they will be replaced by the first layer temp for both the hotend and the bed. If this doesn't work then something is wrong with your Cura. I don't know if Philbaz's screenshot will help you because I don't believe I have the plugin for printer settings and I never ran into issues.

thanks for this nice plugin. maybe you can extend it, with a checkbox for following option:

please translate the START_PRINT BED_TEMP={material_bed_temperature_layer_0} EXTRUDER_TEMP={material_print_temperature_layer_0} to the real temperature. i dont know why, but {material_bed_temperature_layer_0} and {material_print_temperature_layer_0} are not translated in the output g-code. that suxx. my klipper does not work propperly, without a number, like BED_TEMP=100. in klipper i import the varables, with attched sequence. everything works fine when is set BED_TEMP=100, but this {material_bed_temperature_layer_0} does not work.

@ORGATHM
Copy link

ORGATHM commented May 3, 2022

okay, that sounds promising. normaly i thought that {material_bed_temperature_layer_0} contains data. but in my spezial case, it would not be translated into the gcode. i think its a good possibility for me, to install cura 5.0 and check if this work better.

@ORGATHM
Copy link

ORGATHM commented May 3, 2022

@frankbags you are right. its unbeleavable. it was cura`s fault. with fresh installed cura (5.0.0 + 4.13.1) and deleted profile folder, it works with the variables. i think its better when i delete some desinformation in my post. now it works as it should. thank you for your help, and sorry for bothering you.

@LordFlashmeow
Copy link

On MacOS with Cura 5.0, the path to the scripts folder is /Applications/Ultimaker-Cura.app/Contents/MacOS/share/cura/plugins/PostProcessingPlugin/scripts/

(You may need to right-click on the Cura app and click "Show Package Contents" if you're navigating with Finder)

@damiandudycz
Copy link

Doesn't work for my using Cure 5.1. Anyone else tested with this version?

@frankbags
Copy link
Author

I just tested it with 5.1 after using it with 5.0 since its release and there are no issues on my end.

@esc247
Copy link

esc247 commented Aug 5, 2022

Hi, I just used it with Cura 5.1 on a mac as well. No issues. Thanks for this!

@esc247
Copy link

esc247 commented Sep 7, 2022

can this statement be added to the top of the page please?

*(Cura slicer plugin) To make the macro to work in Cura slicer, you need to install the post process plugin by frankbags - In cura menu Help -> Show configuration folder. - Copy the python script from the above link in to scripts folder. - Restart Cura - In cura menu Extensions -> Post processing -> Modify G-Code and select Mesh Print Size

@rstudner
Copy link

rstudner commented Jan 5, 2023

On my mac, Cura 5.2.1:
/Applications/Ultimaker-Cura.app/Contents/MacOS/share/cura/plugins/PostProcessingPlugin/scripts/

I don't have a plugins folder under "share"

Which is odd since I have, well, plugins and do post processing scripts.

@Turbine1991
Copy link

can this statement be added to the top of the page please?

*(Cura slicer plugin) To make the macro to work in Cura slicer, you need to install the post process plugin by frankbags - In cura menu Help -> Show configuration folder. - Copy the python script from the above link in to scripts folder. - Restart Cura - In cura menu Extensions -> Post processing -> Modify G-Code and select Mesh Print Size

Enabled by default these days.

@Turbine1991
Copy link

On my mac, Cura 5.2.1: /Applications/Ultimaker-Cura.app/Contents/MacOS/share/cura/plugins/PostProcessingPlugin/scripts/

I don't have a plugins folder under "share"

Which is odd since I have, well, plugins and do post processing scripts.

On Linux it comes in a similar app package bundle. I had to extract it, and then insert the script in there. For some reason my scripts are entirely sourced from here.

Though I'm not crazy enough to own a Mac, so I can't test it out.

@dev-zetta
Copy link

It fails the calibration:

21:40
Internal error on command:"BED_MESH_CALIBRATE"
21:40
Internal error on command:"BED_MESH_CALIBRATE_BASE"
21:40
Klipper state: Shutdown
21:40
bed_mesh: faulty region points
21:40
15 | (69.7, 130.0) | (92.7, 135.0)
21:40
14 | (91.0, 130.0) | (114.0, 135.0)
21:40
13 | (112.4, 130.0) | (135.4, 135.0)
21:40
12 | (133.7, 130.0) | (156.7, 135.0)
21:40
11 | (133.7, 110.3) | (156.7, 115.3)
21:40
10 | (112.4, 110.3) | (135.4, 115.3)
21:40
9 | (91.0, 110.3) | (114.0, 115.3)
21:40
8 | (69.7, 110.3) | (92.7, 115.3)
21:40
7 | (69.7, 90.6) | (92.7, 95.6)
21:40
6 | (91.0, 90.6) | (114.0, 95.6)
21:40
5 | (112.4, 90.6) | (135.4, 95.6)
21:40
4 | (133.7, 90.6) | (156.7, 95.6)
21:40
3 | (133.7, 70.8) | (156.7, 75.8)
21:40
2 | (112.4, 70.8) | (135.4, 75.8)
21:40
1 | (91.0, 70.8) | (114.0, 75.8)
21:40
0 | (69.7, 70.8) | (92.7, 75.8)
21:40
bed_mesh: generated points
Index | Tool Adjusted | Probe
21:40
Generating new points...
21:35
File selected

@esc247
Copy link

esc247 commented Apr 2, 2023

I’ve set up KAMP now and that works well without having to use a pluglin 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment