An existing operator can be overridden by assigning bl_idname
of an existing operator to a new one. This can be used to extend and even limit the functionality of original operators.
As an example, the following operator is using the bl_idname
of the original save operator (wm.save_mainfile
), allowing the self.filepath
to be manipulated before saving:
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty
from bpy.types import Operator
class ExportSomeData(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "wm.save_mainfile" #
bl_label = "Save..."
# ExportHelper mixin class uses this
filename_ext = ".blend"
filter_glob: StringProperty(
default="*.blend",
options={'HIDDEN'},
maxlen=255,
)
def execute(self, context):
print(f"Call save as, rejig {self.filepath}")
#bpy.ops.wm.save_as_mainfile(filepath=self.filepath)
return {'FINISHED'}
def register():
bpy.utils.register_class(ExportSomeData)
def unregister():
bpy.utils.unregister_class(ExportSomeData)
if __name__ == "__main__":
register()
However, when converting this into an add-on we would have to re-register the old operator to get the original operator functionality back in order to prevent the user having to restart blender or reload all the scripts each time.
Q: What's a reliable way of re-registering the old operator when the add-on gets disabled?