0

As per the question, is it possible to avoid the effect of Undo / Redo on a StringProperty? I've run into a situation where I have to avoid this, but I'm not sure if it can be done, and how.

At this moment in particular I am focused on a StringPropery, but I think it may be useful to know also for other types of bpy.props

Noob Cat
  • 1,222
  • 3
  • 20
  • 61

1 Answers1

2

Setter and getter saving data outside blend file data.

Suggest this could be done with a setter / getter. For example could append string property to a text file, which would be outside the influence of the blend file data (undo stack.)

Here is a test example, using a hardcoded filepath to a text file. The property is prepended to the text editor footer to test.

enter image description here

import bpy
from bpy.props import StringProperty

from pathlib import Path filepath = "/tmp/test.txt"

def get_foo(self): f = Path(filepath) if f.exists(): return f.read_text() else: return "Default"

def set_foo(self, value): f = Path(filepath) f.write_text(value)

bpy.types.Scene.foo = StringProperty( get=get_foo, set=set_foo, )

def draw(self, context): scene = context.scene self.layout.prop(scene, "foo")

bpy.types.TEXT_HT_footer.prepend(draw)

Note: This is solely a proof of concept. Would require a blend file name based filepath, or in file data for different values on a per blend basis.

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • It could be a solution. I am surprised that there are no APIs in Blender to avoid this, such as operators that can avoid undo and redo. I wonder if I am making "absurd" claims, or is it simply an API missing in Blender. You who are an expert in Python / Blender, what do you think about this? – Noob Cat May 23 '21 at 13:56
  • No expert in the ins and outs of how the undo stack works. But imagine it would be difficult to maintain state. – batFINGER May 23 '21 at 15:08
  • I think maybe it could also be interesting to use bpy.app.handlers undo_pre, undo_post, redo_pre, redo_post. – Noob Cat May 29 '21 at 12:42