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.
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