Print Start Macro

Good evening. Can someone help me to check 2 things before printing? Have nen start macro in which I would like to query whether a mesh has already been made and a car z calibrate has already been done. That it then no longer makes

gcode:
    {% set T_EX = params.EXTRUDER|default(250)|int %}    ; Get Parameter from Slicer (Start_G-Code Filament)
    {% set T_BD = params.BED|default(110)|int %}
    M117 Prepare Printer ...
    #SET_FAN_SPEED FAN=Base SPEED=0.5 ; Fan Speed Elektronik auf 50%
    SET_PRESSURE_ADVANCE ADVANCE=0.070 ; Pressure Advance setzen ABS+ 
    BED_MESH_CLEAR
    M117 Homing all Axes ...
    G28 ; Home all axes
    M117 Heat up the bed and nozzle ...
    M109 S{T_EX} ; Set Extruder Temp & Wait
    M190 S{T_BD} ; Set Hotend Temp & Wait
    M117 Quad Gantry Leveling ...
     {% if printer.quad_gantry_level.applied|lower == 'false' %}
    {action_respond_info("QGL CHECK: QGL ausfuehren")}
    QUAD_GANTRY_LEVEL
    {% else %}
    {action_respond_info("QGL CHECK: QGL bereits ausgefuehrt")}
    {% endif %}
    #QUAD_GANTRY_LEVEL
    CLEAR_PAUSE
    CLEAN_NOZZLE
    G28 ; Home all axes again after Cleaning
    M117 Auto Calibrating Z Offset ...
    CALIBRATE_Z
    M117 Bed Mesh ...  ; BED_MESH_PROFILE LOAD=Mesh_ABS+ ; BED_MESH Profil ABS+ laden
    #BED_MESH_CALIBRATE AREA_START={params.AREA_START|default("0,0")} AREA_END={params.AREA_END|default("0,0")}
    BED_MESH_CALIBRATE ; Or make clean Bed Mesh Calibrate
    BED_MESH_PROFILE LOAD=default
    M117 Start Printing ...```

This is my Start Macro

Hi Zimbo,

I am also working on a more advanced print_start macro.

You can access the current bedmesh state of the printer using the printer.bed_mesh value. It will return a dictionary of values, use that as you wish to confirm or deny the application of a bed_mesh to your current printer state.

Here is an example script:
{% set container_bedmeshProfileName = printer.bed_mesh.profile_name|string %}

{% if container_bedmeshProfileName == “” %}
RESPOND PREFIX="DEBUG: " MSG=“No profile loaded”
{% endif %}

Thank you. Okay, that means that I make a mesh once at the beginning and then it does not make a new mesh for further prints? Where exactly would I have to enter your lines? My mesh that it creates before printing is called default.
Do I have to change my start code further?

Yes, once you have a mesh, you don’t have to mesh again. Although it is recommended you mesh prior to every print, if you want to save time you can just load a previous mesh using the BED_MESH_PROFILE LOAD=default gcode command.

You can test whether a bed mesh is currently loaded using the printer.bed_mesh.profile_name parameter in the code.

So I would say have some logic after your CALIBRATE_Z command to heck if a bed mesh is loaded. Note that if you keep your BED_MESH_CLEAR command in the macro you will remove the loaded profile. As for how to check whether a profile exists, I am working on it and I will have an answer for you soon.

EDIT:
So after working on this today I used the following script to dump all runtime variables created by the klipper service: (common-macros/search_vars.cfg at main · simplisticton/common-macros · GitHub).

You can see from the readout that there is a parameter called printer.bed_mesh.profiles which should return a dictionary containing all possible profiles. This should allow you to confirm whether a profile exists and act accordingly using jinja logic in the gcode section of your print_start macro.

Okay, and how exactly would you integrate it if you were me? Do you have an example for me that I could try out? That would be great if it works just for a multicolor print with several elements when there is already a printed part in a different color on the bed.