Goal: The snapping functionality should be enabled and disabled.
I use the function setSnapSettingsForLayer. Convenience function to set snap settings per layer. Short Example:
QgsProject.instance().setSnapSettingsForLayer(layer.id(), True, 2, 1, 10, True)
This approach has already been explained in another post: How to enable the snapping for a layer with the tolerance value with python programming
Up to this point everything works. Now we come to the problem. How can I disable the snapping functionality?
If I set the second parameter in the function setSnapSettingsForLayer to False (to enable the layer snapping), then nothing happens. The Snapping remains activated. Even if I change an another parameter e.g. tolerance, also does not change.
So, how can the Snapping be enabled and disabled while editing?
UPDATE:
Despite my answer, the problem is not yet solved. By choosing Settings -> Snapping options you will find the following window
Only when I confirm this window with the 'OK' button, the code in my answer works. Only now the Snapping can be turned off and on during processing. Has anyone any idea?