应用模板

用法

应用模板功能让用户能够定义可重复利用的配置,选中之后即可替代默认配置,无需另外安装Blender或者覆盖个人设置。

Application templates can be selected from the splash screen or File ‣ New submenu. When there are no templates found the menu will not be displayed on the splash screen.

可以在 程序菜单 安装新的应用模板。如果要在重新启动Blender时保留当前的应用模板,请保存偏好设置。

动机

有些时候,写一个简单的脚本或者插件可能还不足以解决问题,一些用户可能希望有人可以为其替换用户设置和启动文件、安装脚本并修改键位映射。

应用模板的存在意在帮助用户快速切换到自定义配置,而无需破坏已有的设置和安装环境。这意味着人们可以在Blender基础上自行构建易于分发的 应用

细节

应用模板需要定义其自身以下内容:

启动文件

加载模板后的默认文件。

用户设置

应用模板中只有某些特定用户设置才会被用到:

  • 主题。

  • 插件。

  • 键位映射。

  • 视窗照明.

启动画面

模板可以使用自定义启动画面图像。

Python脚本

模板可以与其他脚本一样访问功能,典型的操作包括:

  • 修改和替换部分用户界面。

  • 定义新的菜单、键位映射和工具。

  • 自定义模板特有插件路径。

模板也有自己的用户配置,所以使用模板时保存启动文件不会覆盖默认的启动文件。

目录布局

模板文件可以放在 scripts 目录下两个位置之一。

模板位置:
{BLENDER_USER_SCRIPTS}/startup/bl_app_templates_user
{BLENDER_SYSTEM_SCRIPTS}/startup/bl_app_templates_system

用户配置保存在其子目录:

没有模板:
./config/startup.blend
./config/userpref.blend
有模板:
./config/{APP_TEMPLATE_ID}/startup.blend
./config/{APP_TEMPLATE_ID}/userpref.blend

更多关于脚本和配置位置的细节见 Blender目录布局

Hint

Troubleshooting Paths

When creating a application template, you may run into issues where paths are not being found. To investigate this you can log output of all of Blender's path look-ups.

Example command line arguments that load Blender with a custom application template (replace my_app_template with the name of your own template):

blender --log "bke.appdir.*" --log-level -1 --app-template my_app_template

You can then check the paths where attemps to access my_app_template are made.

命令行访问

使用 命令行参数 可以设置Blender启动器以指定的应用模板启动:

blender --app-template my_template

模板内容

应用模板可以配置以下文件,不过这是可选的。

startup.blend

改模板的初始文件.

userpref.blend

用于该模板的初始设置文件。当被忽略的设置是与默认Blender配置共用的。

(如前文所述,这仅仅是用户设置的子集).

splash.png

Splash screen to override Blender's default artwork (not including header text). Note, this image must be a 1000x500 image.

__init__.py

Python脚本必须包含 registerunregister 方法。

Note

自带的blend文件 startup.blenduserpref.blend 视作 初始设置 ,且不会被覆盖。

The user may save their own startup/preferences while using this template which will be stored in their user configuration, but only when the template includes its own userpref.blend file.

恢复初始设置 一样,用户可以从文件菜单使用 加载模板初始设置 加载模板初始设置。

模板脚本

While app templates can use Python scripts, they simply have access to the same APIs available for add-ons and any other scripts.

正如上文所言,可以选择在应用模板中使用 __init__.py。这样做有以下优势:

  • 无需分发blend文件即可更改启动或首选项。

  • 可以动态更改。

    比如,可以配置模板为先检查处理器、操作系统和内存,然后基于此设置参数值。

  • 可以启用与模板关联的插件。

激活时,将调用 register 函数,当选择另一个模板时,将调用 unregister

由于这些只运行一次,因此对默认值进行任何更改都必须通过处理程序。下面两个是你可能会用到的两个处理程序:

  • bpy.app.handlers.load_factory_preferences_post

  • bpy.app.handlers.load_factory_startup_post

以上两者用于自定义用户可以更改的"初始设置",就像 Blender 在首次启动时的默认值一样。

下面是一个 __init__.py 文件范例,定义了应用模板要使用的默认值。

import bpy
from bpy.app.handlers import persistent

@persistent
def load_handler_for_preferences(_):
    print("Changing Preference Defaults!")
    from bpy import context

    prefs = context.preferences
    prefs.use_preferences_save = False

    kc = context.window_manager.keyconfigs["blender"]
    kc_prefs = kc.preferences
    if kc_prefs is not None:
        kc_prefs.select_mouse = 'RIGHT'
        kc_prefs.spacebar_action = 'SEARCH'
        kc_prefs.use_pie_click_drag = True

    view = prefs.view
    view.header_align = 'BOTTOM'


@persistent
def load_handler_for_startup(_):
    print("Changing Startup Defaults!")

    # Use smooth faces.
    for mesh in bpy.data.meshes:
        for poly in mesh.polygons:
            poly.use_smooth = True

    # Use material preview shading.
    for screen in bpy.data.screens:
        for area in screen.areas:
            for space in area.spaces:
                if space.type == 'VIEW_3D':
                    space.shading.type = 'MATERIAL'
                    space.shading.use_scene_lights = True


def register():
    print("Registering to Change Defaults")
    bpy.app.handlers.load_factory_preferences_post.append(load_handler_for_preferences)
    bpy.app.handlers.load_factory_startup_post.append(load_handler_for_startup)

def unregister():
    print("Unregistering to Change Defaults")
    bpy.app.handlers.load_factory_preferences_post.remove(load_handler_for_preferences)
    bpy.app.handlers.load_factory_startup_post.remove(load_handler_for_startup)