3

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?

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 1
    AFAICT Reload scripts doesn't revive it either. – batFINGER Jun 26 '20 at 15:22
  • 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.) – Ray Mairlot Jun 26 '20 at 15:29
  • @brockmann This question sounds like an antipattern. – Leander Jun 26 '20 at 15:29
  • 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. – Leander Jun 26 '20 at 15:43
  • Not sure what you mean by "Did not say anything like that"? I'm not saying anything is wrong with the question at all. But you linked to another question, so I'm saying to include any information that is relevant in the question body itself and to give context to what 'override' means here. – Ray Mairlot Jun 26 '20 at 15:55
  • I hadn't heard of 'overriding an operator' in this context before so I've made a few edits to clarify what it means and what that code does. Feel free to rollback if you disagree with them. Cheers. – Ray Mairlot Jun 26 '20 at 16:59
  • 1
    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 – batFINGER Jun 26 '20 at 17:37
  • Many thanks @RayMairlot. Only minor corrections (technical details)... – brockmann Jun 26 '20 at 17:42

0 Answers0