[Bf-blender-cvs] [ad37fc994b] app-templates: Addon support enable/disable now works
Campbell Barton
noreply at git.blender.org
Mon Mar 20 20:17:37 CET 2017
Commit: ad37fc994b3a6185bcde901e872aefa238cd017f
Author: Campbell Barton
Date: Tue Mar 21 06:22:26 2017 +1100
Branches: app-templates
https://developer.blender.org/rBad37fc994b3a6185bcde901e872aefa238cd017f
Addon support enable/disable now works
Add simple addon as a demo
===================================================================
A release/datafiles/app_templates/101/addons/template_101_library/__init__.py
A release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend
A release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend
M release/datafiles/app_templates/101/template/__init__.py
M release/scripts/modules/bl_app_template_utils.py
M source/blender/windowmanager/intern/wm_files.c
===================================================================
diff --git a/release/datafiles/app_templates/101/addons/template_101_library/__init__.py b/release/datafiles/app_templates/101/addons/template_101_library/__init__.py
new file mode 100644
index 0000000000..05d7fea25c
--- /dev/null
+++ b/release/datafiles/app_templates/101/addons/template_101_library/__init__.py
@@ -0,0 +1,118 @@
+# ##### 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 #####
+
+# <pep8-80 compliant>
+
+bl_info = {
+ "name": "Blender 101 Library",
+ "author": "None",
+ "version": (1, 0),
+ "blender": (2, 75, 0),
+ "location": "View3D > Add > Mesh > New Object",
+ "description": "Simple test case library",
+ "warning": "",
+ "wiki_url": "",
+ "category": "Add Mesh",
+}
+
+import os
+
+BASE_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__)))
+DATA_DIR = os.path.join(BASE_DIR, "data")
+
+import bpy
+from bpy.types import Operator
+from bpy.props import (
+ FloatVectorProperty,
+ StringProperty,
+)
+from bpy_extras.object_utils import AddObjectHelper, object_data_add
+from mathutils import Vector
+
+
+class OBJECT_OT_add_object_from_blend(Operator, AddObjectHelper):
+ """Create a new Mesh Object"""
+ bl_idname = "mesh.add_object"
+ bl_label = "Add Mesh Object"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ scale = FloatVectorProperty(
+ name="scale",
+ default=(1.0, 1.0, 1.0),
+ subtype='TRANSLATION',
+ description="scaling",
+ )
+
+ filepath = StringProperty(
+ name="Filepath",
+ maxlen=1024,
+ options={'HIDDEN'},
+ )
+ type_attr = StringProperty(
+ name="Type",
+ maxlen=64,
+ options={'HIDDEN'},
+ )
+
+ def execute(self, context):
+
+ name = self.name
+ filepath = self.filepath
+ type_attr = self.type_attr
+
+ with bpy.data.libraries.load(filepath) as (data_from, data_to):
+ setattr(data_to, type_attr, getattr(data_from, type_attr))
+
+ for data in getattr(data_to, type_attr):
+ object_data_add(context, data, operator=self)
+
+ return {'FINISHED'}
+
+
+def paths_from_id(dir_id):
+ """(display_name, full_path)"""
+ d = os.path.join(DATA_DIR, dir_id)
+ for f in os.listdir(d):
+ if f.endswith(".blend"):
+ yield (bpy.path.display_name(f), os.path.join(d, f))
+
+
+def add_object_menu(self, context):
+ layout = self.layout
+ for name, f in paths_from_id("object"):
+ props = layout.operator(
+ OBJECT_OT_add_object_from_blend.bl_idname,
+ text=name,
+ icon='PLUGIN',
+ )
+ props.filepath = f
+ props.type_attr = "meshes"
+
+
+def register():
+ bpy.utils.register_class(OBJECT_OT_add_object_from_blend)
+ bpy.types.INFO_MT_mesh_add.append(add_object_menu)
+
+
+def unregister():
+ bpy.utils.unregister_class(OBJECT_OT_add_object_from_blend)
+ bpy.types.INFO_MT_mesh_add.remove(add_object_menu)
+
+
+if __name__ == "__main__":
+ register()
diff --git a/release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend b/release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend
new file mode 100644
index 0000000000..aee28d5d85
Binary files /dev/null and b/release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend differ
diff --git a/release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend b/release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend
new file mode 100644
index 0000000000..500c36be05
Binary files /dev/null and b/release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend differ
diff --git a/release/datafiles/app_templates/101/template/__init__.py b/release/datafiles/app_templates/101/template/__init__.py
index 1a697d840f..7c1a0f5365 100644
--- a/release/datafiles/app_templates/101/template/__init__.py
+++ b/release/datafiles/app_templates/101/template/__init__.py
@@ -30,6 +30,7 @@ class AppStateStore:
)
_template_addons = (
+ "template_101_library",
)
def __init__(self):
diff --git a/release/scripts/modules/bl_app_template_utils.py b/release/scripts/modules/bl_app_template_utils.py
index 358b8620ea..cf98da52b2 100644
--- a/release/scripts/modules/bl_app_template_utils.py
+++ b/release/scripts/modules/bl_app_template_utils.py
@@ -167,7 +167,7 @@ def _disable(template_id, *, handle_error=None):
getattr(mod, "__file__", template_id))
handle_error(ex)
else:
- print("addon_utils.disable: %s not %s." %
+ print("\tapp_template_utils.disable: %s not %s." %
(template_id, "disabled" if mod is None else "loaded"))
if _bpy.app.debug_python:
@@ -199,20 +199,26 @@ def import_from_id(template_id, ignore_not_found=False):
def activate(template_id=None):
+ template_id_prev = _app_template["id"]
+
+ # not needed but may as well avoid activating same template
+ # ... in fact keep this, it will show errors early on!
+ """
+ if template_id_prev == template_id:
+ return
+ """
# Disable all addons, afterwards caller must reset.
import addon_utils
addon_utils.disable_all()
- template_id_prev = _app_template["id"]
if template_id_prev:
_disable(template_id_prev)
# ignore_not_found so modules that don't contain scripts don't raise errors
mod = _enable(template_id, ignore_not_found=True) if template_id else None
- if mod is not None:
- _app_template["id"] = template_id
+ _app_template["id"] = template_id
def reset(*, reload_scripts=False):
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index 1fb3da34bc..f5ed8a8b03 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -1483,15 +1483,17 @@ static int wm_homefile_read_exec(bContext *C, wmOperator *op)
G.fileflags &= ~G_FILE_NO_UI;
}
- char app_template[sizeof(U.app_template)] = "";
+ char app_template_buf[sizeof(U.app_template)] = "";
+ const char *app_template = NULL;
const bool is_app_template = !from_memory && RNA_boolean_get(op->ptr, "use_template");
const bool use_splash = !from_memory && RNA_boolean_get(op->ptr, "use_splash");
- if (filepath != NULL) {
- wm_file_template_from_path(app_template, filepath);
+ if (is_app_template && filepath != NULL) {
+ wm_file_template_from_path(app_template_buf, filepath);
+ app_template = app_template_buf;
}
- if (wm_homefile_read(C, op->reports, from_memory, filepath, is_app_template ? app_template : NULL)) {
+ if (wm_homefile_read(C, op->reports, from_memory, filepath, app_template)) {
if (use_splash) {
WM_init_splash(C);
}
More information about the Bf-blender-cvs
mailing list