1

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\

tintwotin
  • 2,296
  • 10
  • 24
  • 1
    Code (or example) of 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. – batFINGER Sep 06 '20 at 11:21
  • I've added it as a link above. Thank you. – tintwotin Sep 06 '20 at 12:30
  • Yep. The classes should be registered in the register method. As is will only do this in main thread. Will notice many addons call register from main thread. 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. – batFINGER Sep 06 '20 at 12:48
  • I've tried adding this instead: https://blender.stackexchange.com/a/135045/37272 for installing and enabling as add-ons, but it didn't work for me either. That would be the preferable way to do it. Registering all things from inside the add-ons will be a lot of work in most cases, so I would prefer not to do this. – tintwotin Sep 06 '20 at 13:00
  • @batFINGER I realized that the init file is only called when using Load Factory Settings 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. – tintwotin Sep 11 '20 at 05:09
  • Would consider making an addon that installs the template and associates itself to the workspace. A choice is to have one umbrella addon with all your tools (still favour this) or one for each ... or a bit of both. Also adding a folder named scripts with addons, startup etc subfolders to system path is another way to make a group of addons available without having to do much. Haven't done much on this front, but have an nvim based scripting workspace in mind, am looking for answers re templates with great interest. On question front have you tried importing and calling the – batFINGER Sep 11 '20 at 07:03
  • register method. – batFINGER Sep 11 '20 at 07:03
  • No need to register. Installing and enabling the add-ons as the script does in the updated first post works, but only when Load Factory Settings is run. It doesn't seem like the init file actually is run when selecting the template at start-up. Which is odd, or even a bug? – tintwotin Sep 11 '20 at 08:02
  • Not that it solves the problem, but add a little more info about templates: https://docs.blender.org/manual/en/latest/advanced/app_templates.html – tintwotin Sep 21 '20 at 07:45

1 Answers1

0

The init.py file inside the template folder will only be loaded when the user is selecting File - Defaults - Load Factory Settings, and not the the workspace template is selected at start up.

The process is shown in this video tutorial: https://www.youtube.com/watch?v=ufSyony6eRE

tintwotin
  • 2,296
  • 10
  • 24