0

I'm looking for the source code of the operator bpy.ops.wm.save_mainfile.

I searched on their github repo, but I couldn't find the class linked to this operator. As I want to rewrite this operator, I would like to see how it is implemented to adapt it.

  • 1
    the source code is written in C++, it's in /source/blender/windowmanager/intern/wm_files.cc but you can overwrite the Python operator or use an app handler as shown here: https://blender.stackexchange.com/q/164370/107598 – Blunder Jan 21 '24 at 22:27
  • @Blunder that appears to be a full correct answer. Please post it as such. – TheLabCat Jan 22 '24 at 01:12
  • @Blunder I followed every links starting from your link to the app handler. I tried what I found here: link, but it didn't work. However, the code identified that save_mainfile is a function in which I can get idname through python code. So, even if it calls a C++ function, it should be written somewhere in python code isn't it? – Squelletton Jan 22 '24 at 19:37
  • I'm not a Python expert but yes, on the Python side, you have a wrapper operator and the operation itself is a C++ function (wm_files.cc:3538). If you enter type(bpy.ops.wm.save_mainfile) in the Python console it will tell you that it's a <class 'bpy.ops._BPyOpsSubModOp'>, a wrapper defined in blender/scripts/modules/bpy/ops.py:22. So, save_mainfile() is the constructor? and save_mainfile.id_name() is one of its methods that gives you the C++ id='WM_OT_save_mainfile'. There is also a py id='wm.save_mainfile'. – Blunder Jan 23 '24 at 20:52
  • in ops.py:107 you can see that the operator is called by its py id ret = _op_call(self.idname_py(), .... This _op_call seems to do the magic and calls the C++ code. It's imported from _bpy. I have no idea where this _bpy module comes from. But you can call the operator when you do a from _bpy import ops as ops_module and then ops_module.call('wm.save_mainfile', { 'filepath': 'c:/tmp/test.blend'}) for example. Related question: How to call original operator from operator's override? – Blunder Jan 23 '24 at 21:11
  • @Blunder, thank you for your answer, with further research it seems that _bpy is a C module, that why we cannot find it. If you post it as an offical answer, I will accept it, unless I might post answer to my own question. – Squelletton Jan 26 '24 at 20:37

0 Answers0