Using Undo in Scripts?

Hi All,

I am working on a render engine and I would like to offer more objects than the engine currently supports (which is basically mesh). For instance, under my script I could convert a font object to a mesh, save the BLEND file and launch the render engine for processing. That would allow me to extend the font object as a renderable feature in that target engine.

But the problem I face, is how do I restore the scene back to it’s original state? I have looked into saving the original BLEND file then reloading it but because my code is running as a Render AddOn there are certain locks in place on bpy.ops.wm operations (such as loading a new scene while rendering).

So I wondered, could I just do a bunch of undos after I convert say 100 objects to meshes what about 1000?
Can undo undo everything?

Does anyone have any experience using undo like this? Or any code that might show me how to start?


why don’t you use to_mesh() on the font object to create a new mesh object? you can delete it after export or whatever and the original object will stay unchanged.

That is kind of the route I am leaning towards. Convert all non-renderable object types to mesh, then turn on the hide_render flag for the original object. Save the blend file to disk then issue a render on that file. After the render completes delete the created mesh objects and datablocks and restore the hide_render state for the original objects.

One flaw in this plan is that the render file contains both the original and newly created objects. So double the objects will be delivered to the render engine with some of them being ignored for rendering.

The render engine is the blenderplayer. As a game engine it likes more lean files. But I will probably give this work flow a try and see how it performs/behave.

Another problem I face, a bit off topic, is how to handle the conversion of objects with constraints that reposition the source object. An object with a follow path always ends up a world origin in the game engine.

So I thought Undo might be a way to go back to an original state. Or deliver a leaner file to the BGE.

I have tried the concept of saving the file before altering then restoring after the render. This caused more event headaches, once again because I was still in a render even when I issue the reload. When the reload competes Blender scene update operates upon stale data.(bad=crash)

I just tried the saving the hide_render flag and setting it to false concept. It fails too, I can not set the hide_render flag to false in side a render event.

Writing to ID classes in this context is not allowed: Text, Object datablock, error setting Object.hide_render

I was able to move this code out of the render event into the pre-render event and the error went away. So I can alter objects in the pre-render event and restore_objects in the post_render event.