I am writing an add-on that creates a mesh with several randomly generated aspects. The mesh is generated from default property values when the operator is initially called. These properties are added to the tools panel for the user to modify.
In some cases I want to modify the already generated mesh both to avoid wasted recalculation and to retain the majority of randomly generated features. Ie - it might look the way I want it except for some minor aspect I can change without effecting the overall appearance.
To do this, my property update functions set a flag to indicate whether the property change necessitates a complete regeneration of the mesh or if the existing mesh can be tweaked instead.
I am seeing that there is no object to tweak when the operator's execute function is called after a property change. It appears that an undo is performed before the operator is called again.
Is there a way to avoid the undo operation? Or a way to store the mesh and access it when the operator is called again?
As an example:
# ------------------------------------------------------------
# Operator for adding a Thing
# ------------------------------------------------------------
class ThingAddOp(bpy.types.Operator):
...
# set a flag indicating a complete regenerate is required
def update_regenerate(self, context):
self.do_regenerate = True
# set a flag to indicating a simpler update can be done
def update_modify(self, context):
self.do_modify = True
# flags used to determine if Thing should be regenerated or modified
do_regenerate = BoolProperty(name='Must regenerate object', default=False, options={'HIDDEN'})
do_modify = BoolProperty(name='Can modify object', default=False, options={'HIDDEN'})
...
def execute(self, context):
thing_add(self)
self.do_regenerate = False
self.do_modify = False
return {'FINISHED'}
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def invoke(self, context, event):
return self.execute(context)
def draw(self, context):
...
# ------------------------------------------------------------
# Creates a random Thing
# ------------------------------------------------------------
def thing_add(props):
if props.do_modify:
# At this point there is no object to modify!!
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
# only resize because the user liked the randomly
# generated object but wants to resize the base
# without changing the other apsects of it
size_base(props)
else:
create_base(props) # creates a base with a given size
create_spokes(props) # creates randomly oriented spokes
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)