[Intro]
I'm looking for the Python function (if it exists at all) that saves the .blend file in order to modify it. If it doesn't exist - Please continue reading. I'm opened to any suggestions!
[Reason]
I want to expand it so that every time a .blend file is being saved, a Statistics.txt file with project info is saved along with it.
[Workaround]
I think the whole thing is written in C instead of Python so I can't really modify it. The workaround I came up with is every time the wm.save_mainfile() or the wm.save_as_mainfile() is being called, a custom function gets called instead. That custom function simply runs the wm.save_mainfile() and then executes the code for the Stats.txt file.
[N/B]
The reason I want to avoid the workaround I came up with is that this method requires modifying a number of (5-10) .py files that actually run the wm.save_mainfile() function
[P.S]
So, thanks to @brockmann I got my hands on this example file (written by brockmann):
import bpy
import os
from bpy.app.handlers import persistent
bl_info = {
"name": "Stats Add-on",
"author": "brockmann",
"version": (0, 1),
"blender": (2, 82, 0),
"category": "System"
}
class STATS_AP_preferences(bpy.types.AddonPreferences):
# this must match the add-on name, use '__package__'
# when defining this in a submodule of a python package.
bl_idname = __name__
state: bpy.props.EnumProperty(
name="switch",
default = 'ON',
items=(
("ON", "on", "", 1),
("OFF", "off", "", 2)))
def draw(self, context):
layout = self.layout
layout.prop(self, "state", expand=True)
class STATS_PT_panel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "Layout Demo"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
prefs = context.preferences.addons[__name__].preferences
layout.prop(prefs, "state", expand=True)
@persistent
def my_save_handler(dummy):
if bpy.context.preferences.addons[__name__].preferences.state == 'ON':
stats = bpy.context.scene.statistics(bpy.context.view_layer).split("|")
text_file = os.path.join(bpy.path.abspath("//"), "stats.txt")
with open(text_file, mode='wt', encoding='utf-8') as myfile:
myfile.write('\n'.join(stats))
classes = (
STATS_AP_preferences,
STATS_PT_panel
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.app.handlers.save_post.append(my_save_handler)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
bpy.app.handlers.save_post.remove(my_save_handler)
if __name__ == "__main__":
register()
[The new problem]
Everything works great when I load the script and then save the project file after that. The problem is that after closing Blender and opening it some time later - the script no longer works for some reason, even though it's "ON". Any ideas why this is happening?
[FINAL]
Everything works great! I updated the code snipet above so that anyone could use this tamplate. Thanks again to @brockmann !