How do I make a workspace-template-file execute a script?
In this case I would like to make the Video_Editing template run a script, which will add some buttons to the header.
That is in playback_functions.py which is located in the same folder as init.py
bin\Release\2.91\scripts\startup\bl_app_templates_system\Video_Editing\__init__.py
This is how to init file is looking with the two lines added:
import bpy
from bpy.app.handlers import persistent
@persistent
def load_handler(dummy):
import os
from bpy import context
screen = context.screen
for area in screen.areas:
if area.type == 'FILE_BROWSER':
space = area.spaces.active
params = space.params
params.use_filter_folder = True
filename = os.path.join(os.path.dirname(bpy.data.filepath), "playback_functions.py")
exec(compile(open(filename).read(), filename, 'exec'))
def register():
bpy.app.handlers.load_factory_startup_post.append(load_handler)
def unregister():
bpy.app.handlers.load_factory_startup_post.remove(load_handler)
playback_functions.py: https://pasteall.org/Of5O
EDIT: This works for getting the template installing add-ons, but only when running the script in the Text Editor, and not run as template, so do I do this?
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy.app.handlers import persistent
import os
@persistent
def load_handler(dummy):
#import os
from bpy import context
screen = context.screen
for area in screen.areas:
if area.type == 'FILE_BROWSER':
space = area.spaces.active
params = space.params
params.use_filter_folder = True
path_to_script_dir = bpy.app.binary_path.replace('blender.exe','')+"/2.91/scripts/startup/bl_app_templates_system/Script_Editing"
file_list = sorted(os.listdir(path_to_script_dir))
script_list =[]
for item in file_list:
if item.endswith('.zip'):
script_list.append(item)
for file in file_list:
path_to_file = os.path.join(path_to_script_dir, item)
bpy.ops.preferences.addon_install(overwrite=True, target='DEFAULT', filepath=path_to_file, filter_folder=True, filter_python=False, filter_glob="*.py;*.zip")
enableTheseAddons = ['textension', 'code_editor']
for string in enableTheseAddons:
name = enableTheseAddons
bpy.ops.preferences.addon_enable(module = string)
def register():
bpy.app.handlers.load_factory_startup_post.append(load_handler)
def unregister():
bpy.app.handlers.load_factory_startup_post.remove(load_handler)
if __name__ == "__main__":
register()
Updated template folder: Script_Editing.zip (212.1 KB) This folder should be unzipped and placed in bin\Release\2.91\scripts\startup\bl_app_templates_system\
playback_functions.py
would add clarity. eg If it was an addon or in startup or modules folder or is a registered script in blend file would be automatically imported as a module and have its register method called. $\endgroup$if __name__ == "__main__": register()
This is to emulate what happens when addon is registered, ie it is imported and the register method called. That is what would do here. Import and call register, or install as addon and enable for that workspace. $\endgroup$bpy.ops.wm.read_factory_settings(app_template="Script_Editing")
, but if you do from the init file, you'll end up in a loop. A catch 22 situation. $\endgroup$