Sometime when writing a python script I will run into an error such as syntax or some Exceptions. The script will partially execute, but not everything. This is frustrating because I have to manually undo what the script executed before I can run the script again. Is there a way that I can have the script undo what it has done if it fails. I want the script to only make permanent changes if it ran without error.
You could enable Undo support for your operator. Below
But I wouldn't make the operator undo itself on errors, rather let the user do it if necessarily. However, your script shouldn't raise exceptions in the first place, but check in the operator's
Wrap your script in an operator. You will only need one undo step then. Just replace the main function of this (shortened) template from Text Editor -> Python -> Operator Simple
(See CoDeman's post). Using blender's undo system with