Q: Custom Application Template & startup file?

Hi,

I’m trying to run a startup file when switching to a custom application template, but it isn’t properly working.
What I’m trying to do is e.g. set the UI size and some File Paths/Folder settings on startup of the custom template.
If I run multiple variants of this code in the script editor it works every time:

import bpy

def register():
bpy.context.preferences.view.ui_scale = 1.3
bpy.context.preferences.filepaths.script_directory = (“S:\BB_Addons”)
bpy.context.preferences.filepaths.temporary_directory = (“S:\Temp\Blender”)
bpy.context.preferences.filepaths.render_cache_directory = (“S:\Temp”)

def unregister():
bpy.context.preferences.view.ui_scale = 1.3
bpy.context.preferences.filepaths.script_directory = (“S:\BB_Addons”)
bpy.context.preferences.filepaths.temporary_directory = (“S:\Temp\Blender”)
bpy.context.preferences.filepaths.render_cache_directory = (“S:\Temp”)

if name == “main”:

register()

But if I add this as “init.py” to the template folder, and switch templates, it does not set the UI scale, and will not reset the temporary folder path.

Also, it does not switch back to the default preferences from the user templates (e.g. General), after opening it. It pick up the settings from the last opened custom template.

What am I missing here? I feel I’m almost there, and would be so useful to have working…
Turning off/on the autosave for Preferences doesn’t make any difference here.

rob

Bump…
Sorry about this, but I would really like to know if things like these are possible. Not just for templates.

If we cannot set file paths via scripting, it’s also limiting the abilities of a addon I’m working on atm.

Hi, welcome to devtalk. Your question is borderline user support, that may be why there hasn’t been much activity here.

From the documentation it says your file needs to be renamed “init.py”:
https://docs.blender.org/manual/en/latest/advanced/app_templates.html

Which I just realized you did, but the __ was interpreted as bold. Hm in this case I don’t know, the documentation is a bit vague on:

“Preferences: Only certain preferences from a template are used”

It doesn’t specify if this limitation is for the config.blend or for also the python script. I would recommend you first trying to change something else via your script (e.g., the theme) to see if things are working.

As a workaround if you do bpy.ops.wm.save_userpref() in your script it may work.

I don’t understand why this is borderline user support…
And in the future, it would be nice if someone just says so straight away, so I can try to find my answer some where else…

I’m trying to program certain functionality into Blender, which is not working properly, AND the docs aren’t really helping either.
Where else can I ask questions like this? RightClick? please… :wink:
Or Discord? Same thing.

Like I said, if i run things from the script editor inside Blender it all works as expected. The moment you try this on runtime, it all fails. For me this is extremely frustrating as there is almost no documentation.

I try to figure this stuff out mostly without trying to bother people here, but sometimes I have no other place to go to get direct answers…

But thanks for your reply, will have a look at this as well.

cheers,

rob

But thanks for your reply, will have a look at this as well.

I think you may need to run your code not in register(), but as a bpy.app.handlers (load_factory_startup_post?) with bpy.app.handlers.persistent.

If you want your app-template to have it’s own defaults that are generated dynamically and don’t need to be in the blend files (startup.blend and userdef.blend) it can be done with:

  • bpy.app.handlers.load_factory_preferences_post for preferences.
  • bpy.app.handlers.load_factory_startup_post for the startup scene and data.

This way, the user can change the settings you’ve defined if they wish, but loading factory settings can get them back.

1 Like

Updated docs with examples of how to use handlers to properly handle changing defaults: https://docs.blender.org/manual/en/dev/advanced/app_templates.html#template-scripts

5 Likes

cheer for that, much appreciated! :slight_smile: