I don’t have a concrete answer, but I had the same problem and fixed it once a long time ago. I found I had to do some adjustments to the orientation of the _mth nodes, so they lined up to the components they are supposed to compare to. (But I forget all the specific details, sorry.)
As far as I ever guessed, _mth stood for match? And I noticed some of them seemed to have a rotation similar to the offset when switching.
Oh, I just found a script I wrote for the arms.
I haven’t touched this code since 2019. It might be better to find an actual node it is supposed to match and move it to match. Instead, I hard-coded some values. I hope this gives you some clues for the legs.
import pymel.core as pm
for side in 'LR':
if side == 'L':
pm.setAttr("arm_L0_ik_mth.rotateX", 90)
pm.setAttr("arm_L0_ik_mth.rotateY", 0)
pm.setAttr("arm_L0_ik_mth.rotateZ", 0)
pm.setAttr("arm_L0_ikCtl_ref.rotateX", 0)
pm.setAttr("arm_L0_ikCtl_ref.rotateY", 0)
pm.setAttr("arm_L0_ikCtl_ref.rotateZ", 0)
pm.setAttr("arm_L0_fk2_mth.rx", -90)
pm.setAttr("arm_L0_fk2_mth.ry", 0)
pm.setAttr("arm_L0_fk2_mth.rz", 0)
if side == 'R':
pm.setAttr("arm_R0_ik_mth.rotateX", -90)
pm.setAttr("arm_R0_ik_mth.rotateY", 0)
pm.setAttr("arm_R0_ik_mth.rotateZ", 180)
pm.setAttr("arm_R0_ikCtl_ref.rotateX", 180)
pm.setAttr("arm_R0_ikCtl_ref.rotateY", 0)
pm.setAttr("arm_R0_ikCtl_ref.rotateZ", 180)
pm.setAttr("arm_R0_fk2_mth.rotateX", 90)
pm.setAttr("arm_R0_fk2_mth.rotateY", 0)
pm.setAttr("arm_R0_fk2_mth.rotateZ", 180)