How to set Z-Offset based on used build plate and filament with simple klipper macro

Wanted to share again some of ‘tools’ I use when printing with K2 (this macro with other values I use also on other printers).

I had to change my nozzle because after 500+ hours of printing with it it was starting to clog every 2nd print, even with some PLA and always with PETG.
After replacing the nozzle and running all printer calibration I realized that I need to increase my Z-Offset by +0.02 mm now because even PLA was pushed too much into print plate.
On top of that I bought a 3rd party print plate (on AliExpress) which has one one side a golden textured PEI and on the other side a black flat carbon laser texture. For the flat side I user the ‘high temp plate’ settings in slice but it needs also some increased Z-offset of additional +0.06mm.
Most of my PETG needs also +0.025 Z-Offset so therefore to automatically set the right Z-Offset I use the the below macro:

#[gcode_macro MY_PRINT_START] sets Z-Offset based on print bed and filament type 

[respond]
# the [respond] is needed to make m118 working and echo output! You need it only once in you macro file....

[gcode_macro MY_PRINT_START]
description: Add this to the "Machine start G-Code" section after the line "START_PRINT ..." in your special printer config file.
  Usage: MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" N_ZO=<your default offset>
gcode:
    {% set CURR_BED_TYPE = params.CURR_BED_TYPE|default('none')|string %}
    {% set FILAMENT_TYPE = params.FILAMENT_TYPE|default('none')|string %}
    {% set N_ZO = params.N_ZO|default(0.0)|float %}
    M118 My_PRINT_START with {FILAMENT_TYPE} and {CURR_BED_TYPE} @{N_ZO}

    {% if FILAMENT_TYPE == 'PETG' %}
      M118 PETG filament detected, increasing Z-Offset by 0.025!
      {% set N_ZO = N_ZO + 0.025 %}
    {% elif FILAMENT_TYPE == 'TPU' %}
      {% set N_ZO = N_ZO + 0.025 %}
#    {% else %}
#      {% set N_ZO = N_ZO + 0.0 %}
    {% endif %}

    {% if CURR_BED_TYPE == 'High Temp Plate' %}
      M118 High Temp Plate (flat)  detected, increasing Z-Offset by 0.09!
      {% set N_ZO = N_ZO + 0.06 %}
#    {% elif CURR_BED_TYPE == ['Textured PEI Plate'] %}
#        {% set N_ZO = N_ZO + 0.0 %}
#    {% else %}
#        {% set N_ZO = N_ZO + 0.09 %}
    {% endif %}
    M118 The new ZO is: {N_ZO}
    SET_GCODE_OFFSET Z={N_ZO} MOVE=1

You can put the macro directly in the printer.cfg with fluidd but I have it in my ‘my_macros.cfg’ file which I include in ‘printer.cfg’ with

[include my_macros.cfg]

In this way I need to upload after firmware updates only my_macros.cfg and edit into printer.cfg only above line if firmware update changes printer.cfg.

Anyhow, to make the above macro working you need to run the macro also in your printer start code in slicer.

You need to enter in the ‘Machine start G-Code’ section after the ‘START_PRINT’ line

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" N_ZO=0.0

Where N_ZO will be the Z-Offset you would need with PLA on the textured print plate, before I changed my nozzle it was 0.0 but now it is 0.02!

In ‘Machine end G-Code’ you need to enter before the ‘END_PRINT’ line

SET_GCODE_OFFSET Z=0.0 MOVE=1

In this way the macro will be called just before loading filament and printing the purge line (which is important that purge line is also affected). At print end the Z-Offset is reset to 0 (or whatever you want or need).

The MY_PRINT_START macro itself is (in the listed version) only checking if filament = ‘PETG’ and if yes adding 0.025mm (you may change this for your filament needs) and then it’s checking if print plate = ‘High temp Plate’ and if yess adding my 0.06mm for this plate.

So in my case when I print PLA on textured plate the Z-Offset will be 0.02, for PETG it would be 0.045 and on High temp plate it would be 0.08 for PLA and 0.105 for PETG.

You may extend the macro for other filament or bed types as well, I use for TPU for example also the PETG settings.

All the best for your print optimization!

6 Likes

Hello frankjoke,

Many thanks for this.

You are a genius as usual :+1:

Cheers.

1 Like

I changed the macro a bit to allow also to change TPU or PETG adjustments in slicers printer profile.

#[gcode_macro MY_PRINT_START] sets Z-Offset based on print bed and filament type 

[respond]
# the [respond] is needed to make m118 working and echo output! You need it only once in you macro file....

[gcode_macro MY_PRINT_START]
description: Add this to the "Machine start G-Code" section after the line "START_PRINT ..." in your special printer config file.
  Usage: MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=<your default offset> ZG=<diff for PETG> ZT=<diff for TPU> ZB=<diff for flat bed>
gcode:
    {% set CURR_BED_TYPE = params.CURR_BED_TYPE|default('none')|string %}
    {% set FILAMENT_TYPE = params.FILAMENT_TYPE|default('none')|string %}
    {% set ZO = params.ZO|default(0.0)|float %}
    {% set ZG = params.ZG|default(0.025)|float %}
    {% set ZT = params.ZT|default(0.025)|float %}
    {% set ZB = params.ZB|default(0.06)|float %}
    M118 My_PRINT_START with {FILAMENT_TYPE} and {CURR_BED_TYPE} @{ZO} ZB={ZB} ZG={ZG} ZT={ZT}

    {% if FILAMENT_TYPE == 'PETG' %}
      M118 PETG filament detected, increasing Z-Offset by ZG={ZG}!
      {% set ZO = ZO + ZG %}
    {% elif FILAMENT_TYPE == 'TPU' %}
      M118 TPU filament detected, increasing Z-Offset by ZT={ZT}!
      {% set ZO = ZO + ZT %}
#    {% else %}
#      {% set ZO = ZO + 0.0 %}
    {% endif %}

    {% if CURR_BED_TYPE == 'High Temp Plate' %}
      M118 High Temp Plate (flat)  detected, increasing Z-Offset by ZB={ZB}!
      {% set ZO = ZO + ZB %}
#    {% elif CURR_BED_TYPE == ['Textured PEI Plate'] %}
#        {% set ZO = ZO + 0.0 %}
#    {% else %}
#        {% set ZO = ZO + 0.09 %}
    {% endif %}
    M118 The new ZO is: {ZO}
    SET_GCODE_OFFSET Z={ZO} MOVE=1

What I changed are the parameters for the macro call in the printer profile:

  • ZO replaces N_ZO and is the basic z-offset for all prints (default 0.0mm)
  • ZG is the z-offset to add for PETG (default 0.025mm)
  • ZT is the z-offset to add for TPU (default 0.025mm)
  • ZB is the z-offset to be added for a flat bed (default 0.06mm)

If you omit any of parameters the defaults will be used but if you have different PETG which need different Z-offset you may vary now.

The line to write into printer profile machine start code changes now to

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=0.02

if you have 0.02mm basic z-offset for PLA and for flat bed 0.08 (and not the default 0.06) as an example

4 Likes

Dear all!
As I copied over the macro from a non-CFS/AMS printer I had to change the call in the printer settings to make it work also on CFS when first filament used is not in first slot!

I reflected the changes needed in above postings but you need to change only the printer config settings line to:

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=0.01

where ZO=0.01 is you Z-offset when using PLA, most probably can be removed or set to 0.0 for you (I need it for new nozzle).

Here is again the picture where you need to enter the line:

2 Likes

I don’t think this macro has gotten the respect it deserves! This is awesome!

Absolutely brilliant. The macro I’ve been using is one-size-fits-all… one Z-offset for every material. :joy: This is next-level!

Oh, and flat bed? :laughing: I managed to get mine leveled down to a 0.0600mm deviation after a full heat soak at 50°C for my PLA prints. Took some serious time and patience! Most of the deviation happens toward the outer edges due to expansion at the center, but I’m totally fine with that.

I actually designed a handy dial indicator jig in Fusion360 that attaches to the print head for more precise adjustments. Made bed leveling way smoother! Suppose I should throw that into Creality Cloud for others to utilize.

Thanks @frankjoke !

1 Like

Thanks @frankjoke for sharing this macro and your knowledge.

I have been meaning to add this to my K2 Plus for a while now and finally did.

Somewhere I have an issue as I don’t see a Z offset change when printing nor any of the M118 messages in the console.

Here’s my printer machine G-code change with the added lines in the start and end sections:

My printer.cfg code with the [include my_macros.cfg] line:

And here is the my_macros.cfg file that is in the configuration section of fluidd:

I did a save and reset after all of the changes.

I have re-read the post several times and must be missing something.

Any help would be appreciated.

Thanks a lot!

@NumerousPayloads , It looks good.
Normally you just need to slice it with this profile and then the lines sould be visible in the gcode file.
You can check if the macro is running when you watch your fluidd console just when the purge line is printed, you should see similar lines (yellow painted):

If you do not see these echo lines the macro is either not loaded or not run correctly. You may watch fluidd console after a fluidd restart if you get any error messages when starting. The problem may be also in another file!

the lines above the macro is running normally,

p.s.: I see in your printer.cfg that you also include the set-skew macros.
I did than on my Qidi as well, but realized then that I can include it just in print profile as well like below:

1 Like

Thanks @frankjoke!

I wasn’t reslicing my print file after the machine G-code changes. Works well now!

Had to put [skew_correction] in printer.cfg to get the SET_SKEW in the machine G-code to run.

Happy with how the K2 Plus is running.

Thanks again!

It has from me. I’m giving it exactly the same respect I’d give a wandering bear, and for the same reason; it looks fantastic, but I know if I approach it I’m likely to get something shredded.

Seriously, I’m well aware I need some “per filament” adjustments, but whilst I once used to be a confident user of macros, I’m now old and dumb, and have a hard time following their logic. I just know I’d change something that looks quite simple, and end up opening a portal to hell or something.

That said, I recognise @frankjoke’s genius, and would just ask personally; what do you know about support structures?

I have some very complex robots (Mechwarrior style) I need to print. They’re very complex and really intended more for resin printers which don’t have. Scale isn’t the issue as I want to print them more like a foot tall rather than the traditional inch or so “gaming scale”.

The issue is surface detail, particularly on the undersides. I’m fighting a losing battle trying to get as good a finish on the bottom of the model (or model part, as they have about thirty parts each). I’m currently evaluating dedicated support filament from Bambu, but not sure if it’s selling point is simply lower adhesion for easier removal, or if it’s possible to use that lower adhesion to increase the density and contact level (ie reduce distance) to improve quality of finish.

BTW, this isn’t a hijack - Z differences in printing may be part of the “secret” to good support…

Any thoughts?

1 Like

Dear all, Creality updated the default profiles for CP6 for most of the printers.


They mention changes in the support parameters but they changed also the start g-code in printer profile and if you created your own printer profile you should incorporate the changes in there as well!

1 Like

Hey frank :slight_smile: I just wanted to jump in and share the output I’m getting (it’s not setting the offset) and see if maybe you might have some guidance as usual:

01:36:04 echo: My_PRINT_START with and @0
01:36:04 echo: The new ZO is: 0.0

Thanks as always :slight_smile:

It seems that filament type and other info is not handed over to the macro.

are you sure that you have in your user machine settings - Start G-code the right line (here yellow)?:

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=0.005 ZG=0.025

This is my version for 0.05mm basic and additional 0.025mm for PETG.
You may leave out the ZO as here:

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZG=0.025

If the syntax is not right - like missing space or bad characters inside - it won’t work.
The text need to be exactly the same with the exclamation marks and all the special characters.

Yep, and as far as I can tell it’s in there unless I did something stupid:

Ok, did you print PETG?
Only if you print PETG or on flat print bed you will see a difference.

Here is my output when printing PETG:

23:52:24 
echo: The new ZO is: 0.030000000000000002
23:52:24 
echo: PETG filament detected, increasing Z-Offset by ZG=0.025!
23:52:24 
echo: My_PRINT_START with PETG and Textured PEI Plate @0.005 ZB=0.06 ZG=0.025 ZT=0.02

I am missing also some status print in first echo line therefore it seems that you did not use my updated macro from February 26th:

#[gcode_macro MY_PRINT_START] sets Z-Offset based on print bed and filament type 

[respond]
# the [respond] is needed to make m118 working and echo output! You need it only once in you macro file....

[gcode_macro MY_PRINT_START]
description: Add this to the "Machine start G-Code" section after the line "START_PRINT ..." in your special printer config file.
  Usage: MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=<your default offset> ZG=<diff for PETG> ZT=<diff for TPU> ZB=<diff for flat bed>
gcode:
    {% set CURR_BED_TYPE = params.CURR_BED_TYPE|default('none')|string %}
    {% set FILAMENT_TYPE = params.FILAMENT_TYPE|default('none')|string %}
    {% set ZO = params.ZO|default(0.0)|float %}
    {% set ZG = params.ZG|default(0.025)|float %}
    {% set ZT = params.ZT|default(0.025)|float %}
    {% set ZB = params.ZB|default(0.06)|float %}
    M118 My_PRINT_START with {FILAMENT_TYPE} and {CURR_BED_TYPE} @{ZO} ZB={ZB} ZG={ZG} ZT={ZT}

    {% if FILAMENT_TYPE == 'PETG' %}
      M118 PETG filament detected, increasing Z-Offset by ZG={ZG}!
      {% set ZO = ZO + ZG %}
    {% elif FILAMENT_TYPE == 'TPU' %}
      M118 TPU filament detected, increasing Z-Offset by ZT={ZT}!
      {% set ZO = ZO + ZT %}
#    {% else %}
#      {% set ZO = ZO + 0.0 %}
    {% endif %}

    {% if CURR_BED_TYPE == 'High Temp Plate' %}
      M118 High Temp Plate (flat)  detected, increasing Z-Offset by ZB={ZB}!
      {% set ZO = ZO + ZB %}
#    {% elif CURR_BED_TYPE == ['Textured PEI Plate'] %}
#        {% set ZO = ZO + 0.0 %}
#    {% else %}
#        {% set ZO = ZO + 0.09 %}
    {% endif %}
    M118 The new ZO is: {ZO}
    SET_GCODE_OFFSET Z={ZO} MOVE=1
1 Like

Thnak you very much for this macro, Works like a charm!

I also used that macro system, but there’s actually a much simpler way… directly in the printer profile within Creality Print. You can set the Z-Offset directly and create as many profiles as you want. And when there’s a firmware update, it prevents the macros from being overwritten.

1 Like