I am wondering whether it is possible to run a Python script in Blender 2.82 from the text editor, such that each time the script is run, it reverses everything that was done in the previous run. In other words, if I first run the script from the default home file, then each time I run the code, the result will be the exactly same as if I had manually created a new Blender document and run the code there.
A minimum working example is as follows:
import bpy #Some code here to delete everything bpy.ops.mesh.primitive_cylinder_add(vertices = 360, radius = 1, depth = 1) bpy.ops.curve.primitive_bezier_circle_add(radius = 2)
I have tried putting the following codes in the commented line, but none of these solutions are satisfactory:
- Nothing — A new mesh and Bézier curve is generated each time the code is run.
- bpy.ops.wm.read_homefile(use_empty=True) — This closes the Blender window without running the code.
- The following code:
for o in bpy.data.objects: bpy.ops.object.delete()
— A new mesh for the cylinder and a curve for the Bézier circle are still generated each time the code is run.
- Same as above, with the following additional lines:
for o in bpy.data.curves: bpy.ops.curve.delete()
— This leads to an runtime error: ‘Operator bpy.ops.curve.delete.poll() failed, context is incorrect’. In any event, if possible, it would be preferable to have a solution that is not specific to a particular kind of object (eg curves), but works regardless of what might happen to be in the scene.
bpy.ops.scene.new()— This runs the script in a blank scene, which is the desired effect. However, I have been struggling to find a method to delete all other scenes with their content.
Any help would be much appreciated.