Gcode commands to save babystep for both extruder

Hey guys! I’m trying to fugure out how does work gcode_move.py . I didn’t get how do homing_position and base_position work and connect with each other. I have two extruders and I need to save the z offset for each extruder, and when switching through t1 and t0, set the z offset accordingly depending on the extruder. Also, if offset was not set on the second extruder, then we set the value from the first extruder. I tested the code and the offset changes and persists, but when printing with two extruders, at some point, when switching the extruder, it starts printing at the height of the previous layer. I would be grateful for any advice .:handshake: ` cmd_SET_BABYSTEP_EXTRUDER_DEFAULT_help = “Set a default value to EXTRUDER Z offset”

def cmd_SET_BABYSTEP_EXTRUDER_DEFAULT(self, gcmd):
    toolhead = self.printer.lookup_object('toolhead')
    extruder = toolhead.get_extruder()
    active_extruder = extruder.get_name()
    logging.info(f"Executing SET_BABYSTEP_EXTRUDER_DEFAULT. Active extruder: {active_extruder}")
    gcmd.respond_info(f"active extruder: {active_extruder}")

    if active_extruder == "extruder":
        self.offset_extruder = self.homing_position[2]
        self.offset_extruder_assigned = True
        logging.info(f"set offset_extruder0 for homing_position[2]")
        gcmd.respond_info(f"set offset_extruder0 for homing_position[2]")
    elif active_extruder == "extruder1":
        logging.info(f"set offset_extruder1 for homing_position[2]")
        self.offset_extruder1 = self.homing_position[2]
        gcmd.respond_info(f"set offset_extruder1 for homing_position[2]")
    else:
        raise gcmd.error(f'Unsupported extruder: {active_extruder}')
    # logging.info(f"Resetting homing_position[2] to 0.0. Previous value: {self.homing_position[2]}")
    # self.homing_position[2] = 0.0

    move_delta = [0., 0., 0., 0.]
    move_delta[2] = 0.0 - self.last_position[2]

    if gcmd.get_int('MOVE', 0):
        speed = gcmd.get_float('MOVE_SPEED', self.speed, above=0.)
        for pos, delta in enumerate(move_delta):
            self.last_position[pos] += delta
        logging.info(f"Moving to {self.last_position}")
        self.move_with_transform_local(self.last_position, speed)

cmd_SET_BABYSTEP_EXTRUDER_help = "Save babystep for extruder"

def cmd_SET_BABYSTEP_EXTRUDER(self, gcmd):
    logging.info("Executing SET_BABYSTEP_EXTRUDER")
    if not self.offset_extruder_assigned:
        logging.info("offset_extruder not assigned -> offset_extruder = offset_extruder1")
        self.offset_extruder = self.offset_extruder1
    self._set_babystep_extruder(gcmd, self.offset_extruder)
    self.offset_extruder_assigned = True
    logging.info(f"set babystep for extruder = {self.offset_extruder}")

cmd_SET_BABYSTEP_EXTRUDER1_help = "Save babystep for extruder1"

def cmd_SET_BABYSTEP_EXTRUDER1(self, gcmd):
    logging.info("Executing SET_BABYSTEP_EXTRUDER1")
    if not self.offset_extruder1_assigned:
        logging.info("offset_extruder1 not assigned -> offset_extruder1 = offset_extruder")
        self.offset_extruder1 = self.offset_extruder
    self._set_babystep_extruder(gcmd, self.offset_extruder1)
    self.offset_extruder1_assigned = True
    logging.info(f"set babystep for extruder1 = {self.offset_extruder1}")

def _set_babystep_extruder(self, gcmd, offset_extruder):
    if offset_extruder is None:
        raise gcmd.error(f"Offset for extruder is not initialized.")

    self.homing_position[2] = offset_extruder
    # self.base_position[2] += offset_extruder
    logging.info(f"set {offset_extruder} for homing_position[2]")
    gcmd.respond_info(f"set {offset_extruder} for homing_position[2]")

    move_delta = [0., 0., 0., 0.]
    move_delta[2] = offset_extruder - self.last_position[2]

    toolhead = self.printer.lookup_object('toolhead')
    extruder = toolhead.get_extruder()
    active_extruder = extruder.get_name()
    gcmd.respond_info(f"active extruder: {active_extruder}")
    logging.info(f"active extruder: {active_extruder}")
    logging.info(f"z offset for {active_extruder} {offset_extruder}")
    gcmd.respond_info(f"homing position {self.homing_position}")
    gcmd.respond_info(f"base position {self.base_position}")
    logging.info(f"toolhead offsets: {self.toolhead_offsets}")
    logging.info(f"cartridge offset: {self.cartridge_offset}")
    logging.info(f"gcode: {self.last_position}")

    if gcmd.get_int('MOVE', 0) and offset_extruder != 0.0:
        speed = gcmd.get_float('MOVE_SPEED', self.speed, above=0.)
        for pos, delta in enumerate(move_delta):
            self.last_position[pos] += delta
        logging.info(f"Moving to {self.last_position}")
        self.move_with_transform_local(self.last_position, speed)

`