3
$\begingroup$

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?

$\endgroup$
8
  • 1
    $\begingroup$ AFAICT Reload scripts doesn't revive it either. $\endgroup$
    – batFINGER
    Jun 26, 2020 at 15:22
  • $\begingroup$ Hi. Seeing as overriding an operator isn't common, I think it would be good to give some context in the question (aside from the code) as to what is meant by 'overriding' in this case. (Also, might be good to quote anything relevant from that other question, just in case it disappears at some point.) $\endgroup$ Jun 26, 2020 at 15:29
  • $\begingroup$ @brockmann This question sounds like an antipattern. $\endgroup$
    – Leander
    Jun 26, 2020 at 15:29
  • $\begingroup$ How about just adding the operator as a separate one, but instead changing the keyboards shortcuts and menu items to your addon. Them you can restore. $\endgroup$
    – Leander
    Jun 26, 2020 at 15:43
  • 1
    $\begingroup$ As mentioned if the operator has a python class then it is easy to import and reregister. IMO The implied question here is how to reregister a builtin operator.. or even for that matter can they be. @Leander re shortcuts and menus... how to replace the Save menu item becomes an issue. Overriding is a "one fell swoop" approach $\endgroup$
    – batFINGER
    Jun 26, 2020 at 17:37

0

You must log in to answer this question.

Browse other questions tagged .