Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@frankbags
Copy link
Author

frankbags commented Nov 21, 2021

I should probably break out of that first For Loop.

@dapostol73
Copy link

dapostol73 commented Dec 22, 2021

Super helpful, thanks for this....modified it so I append the info to my START_PRINT line data instead of replacing it, so if I loose the script it just doesn't add it.

https://github.com/dapostol73/Randoms/blob/main/Cura/Plugins/PostProcessingPlugin/MeshPrintSize.py

@PhilBaz
Copy link

PhilBaz commented Apr 25, 2022

Im having trouble getting Cura to recognize & use the script. I'm placing it in C:\Users\xxx\AppData\Roaming\cura\master\plugins, as instructed here : https://github.com/Frix-x/klipper-voron-V2/blob/main/macros/probing/bed_mesh.cfg .

Ive tried on 5.0 Beta, as well as 4.13 master fork.

Any help would be appreciated.

PS...does this need to be reworked for 5.0?

@dapostol73
Copy link

dapostol73 commented Apr 25, 2022

Hey @PhilBaz as this is a bit of a hack, you need to put it directly in C:\Program Files\Ultimaker Cura 4.13.1\plugins\PostProcessingPlugin\scripts.

I'm not sure how Cura mounts these script, you could try first putting it in and see if it pick it up.
C:\Users\xxx\AppData\Roaming\cura\4.13\plugins\PostProcessingPlugin\scripts

@frankbags
Copy link
Author

frankbags commented Apr 25, 2022

I'll see if I can't find some time to mess around with 5.0. They may have changed the API for the post processing plugin.

@PhilBaz
Copy link

PhilBaz commented Apr 25, 2022

It works !!!

[dapostol73]
(https://github.com/dapostol73) 2 hours ago

I have it working with Cura 5.0, for that version it lives here:

C:\Program Files\Ultimaker Cura 5.0.0\share\cura\plugins\PostProcessingPlugin\scripts

@Shpyda
Copy link

Shpyda commented Apr 26, 2022

It works !!!

[dapostol73] (https://github.com/dapostol73) 2 hours ago

I have it working with Cura 5.0, for that version it lives here:

C:\Program Files\Ultimaker Cura 5.0.0\share\cura\plugins\PostProcessingPlugin\scripts

What did you have to do additionally? I added the script and the start code and now Klipper is saying that I must home the axis first. What could I have wrong?

@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

frankbags commented May 1, 2022

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

LordFlashmeow commented Jun 22, 2022

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

damiandudycz commented Aug 4, 2022

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

@frankbags
Copy link
Author

frankbags commented Aug 4, 2022

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

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