0

How can I add additional code for already existing operator? For example, I want to add into bpy.ops.wm.save_mainfile this string:

print("My code")

I know that I can override this operator by creating a custom one with similar bl_idname, but previous features (saving functions in this example) will be destroyed too.

Hope for your help!

user51106
  • 1
  • 2

2 Answers2

0

Okay, after many time of research I found a quite enough answer on my question. I rephrased what I want and made the following things.

Custom operator

First I created a custom operator. It looks like this:

class SaveFile(bpy.types.Operator):
    bl_idname = "my_wm.save"
    bl_label = "Save My File"

    def __init__(self):
        print("It is working now")

    def execute(self, context):
        try:
            bpy.ops.wm.save_mainfile()
            print("My code!")
        except RuntimeError:
            print("ERROR: Can't save file!")

        return {'FINISHED'}

    def invoke(self, context, event):
        return self.execute(context)

After that I registered it:

bpy.utils.register_class(SaveFile)

Invocation

In the places, where I want to execute my custom code, I use this:

bpy.ops.my_wm.save()

Hope that it will be helpful for somebody else.

user51106
  • 1
  • 2
  • You may find this answer of interest. I would recommend naming it wm.my_save. Also store the return from the original save and respond to it, it may return {'CANCELLED'} and then you may not want to do your steps. An exception is thrown if there is an error, a user clicking cancel isn't an error. – sambler Jan 26 '18 at 07:06
-3

try to Going back.to ,>mainfile>findfilefolder> /export// mainfile..< /< ~resavefolder:> (i hope this helps you.)

  • 1
    Unfortunately, I don't understand what do you mean. I'm talking about the thing similar to function overloading in C++ but in python script. – user51106 Jan 18 '18 at 08:11