1
$\begingroup$

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\

$\endgroup$
9
  • 1
    $\begingroup$ 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. $\endgroup$
    – batFINGER
    Sep 6, 2020 at 11:21
  • $\begingroup$ I've added it as a link above. Thank you. $\endgroup$
    – tintwotin
    Sep 6, 2020 at 12:30
  • $\begingroup$ 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. $\endgroup$
    – batFINGER
    Sep 6, 2020 at 12:48
  • $\begingroup$ I've tried adding this instead: 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. $\endgroup$
    – tintwotin
    Sep 6, 2020 at 13:00
  • $\begingroup$ @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. $\endgroup$
    – tintwotin
    Sep 11, 2020 at 5:09

1 Answer 1

0
$\begingroup$

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

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .