Fix T63775: Toolbar icons ignore BLENDER_SYSTEM_DATAFILES
authorCampbell Barton <ideasman42@gmail.com>
Tue, 23 Jul 2019 06:23:56 +0000 (16:23 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 23 Jul 2019 06:30:26 +0000 (16:30 +1000)
The environment variable to locate system data-files was
ignored by toolbar icons.

Add bpy.utils.system_resource
to match Blender's internal data-file access.

release/scripts/modules/bpy/utils/__init__.py
release/scripts/startup/bl_ui/space_toolsystem_common.py
source/blender/python/intern/bpy.c

index e6424de0742d52dcb87655b7e908aa65ae93bdc4..b39099158c6a577e67d935dae322b7dc31958303 100644 (file)
@@ -65,6 +65,7 @@ from _bpy import (
     script_paths as _bpy_script_paths,
     unregister_class,
     user_resource as _user_resource,
+    system_resource,
 )
 
 import bpy as _bpy
index e7e95c26b55dc066b28cbc55112a995bb8ca2caa..222185e18d57ece872d4255dd663f79478101d71 100644 (file)
@@ -195,11 +195,8 @@ class ToolSelectPanelHelper:
             assert(type(icon_name) is str)
             icon_value = _icon_cache.get(icon_name)
             if icon_value is None:
-                dirname = bpy.utils.resource_path('LOCAL')
-                if not os.path.exists(dirname):
-                    # TODO(campbell): use a better way of finding datafiles.
-                    dirname = bpy.utils.resource_path('SYSTEM')
-                filename = os.path.join(dirname, "datafiles", "icons", icon_name + ".dat")
+                dirname = bpy.utils.system_resource('DATAFILES', "icons")
+                filename = os.path.join(dirname, icon_name + ".dat")
                 try:
                     icon_value = bpy.app.icons.new_triangles_from_file(filename)
                 except Exception as ex:
index 92ba7704b19e8eeaa06d58dd4f5db64b459009ae..b8a83d0588d88dfaa317a5ee16d10d672f6f24c1 100644 (file)
@@ -184,6 +184,49 @@ static PyObject *bpy_user_resource(PyObject *UNUSED(self), PyObject *args, PyObj
   return PyC_UnicodeFromByte(path ? path : "");
 }
 
+PyDoc_STRVAR(bpy_system_resource_doc,
+             ".. function:: system_resource(type, path=\"\")\n"
+             "\n"
+             "   Return a system resource path.\n"
+             "\n"
+             "   :arg type: string in ['DATAFILES', 'SCRIPTS', 'PYTHON'].\n"
+             "   :type type: string\n"
+             "   :arg path: Optional subdirectory.\n"
+             "   :type path: string\n");
+static PyObject *bpy_system_resource(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
+{
+  const char *type;
+  const char *subdir = NULL;
+  int folder_id;
+
+  const char *path;
+
+  static const char *_keywords[] = {"type", "path", NULL};
+  static _PyArg_Parser _parser = {"s|s:system_resource", _keywords, 0};
+  if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &type, &subdir)) {
+    return NULL;
+  }
+
+  /* stupid string compare */
+  if (STREQ(type, "DATAFILES")) {
+    folder_id = BLENDER_SYSTEM_DATAFILES;
+  }
+  else if (STREQ(type, "SCRIPTS")) {
+    folder_id = BLENDER_SYSTEM_SCRIPTS;
+  }
+  else if (STREQ(type, "PYTHON")) {
+    folder_id = BLENDER_SYSTEM_PYTHON;
+  }
+  else {
+    PyErr_SetString(PyExc_ValueError, "invalid resource argument");
+    return NULL;
+  }
+
+  path = BKE_appdir_folder_id(folder_id, subdir);
+
+  return PyC_UnicodeFromByte(path ? path : "");
+}
+
 PyDoc_STRVAR(
     bpy_resource_path_doc,
     ".. function:: resource_path(type, major=bpy.app.version[0], minor=bpy.app.version[1])\n"
@@ -292,6 +335,12 @@ static PyMethodDef meth_bpy_user_resource = {
     METH_VARARGS | METH_KEYWORDS,
     NULL,
 };
+static PyMethodDef meth_bpy_system_resource = {
+    "system_resource",
+    (PyCFunction)bpy_system_resource,
+    METH_VARARGS | METH_KEYWORDS,
+    bpy_system_resource_doc,
+};
 static PyMethodDef meth_bpy_resource_path = {
     "resource_path",
     (PyCFunction)bpy_resource_path,
@@ -397,6 +446,9 @@ void BPy_init_modules(void)
   PyModule_AddObject(mod,
                      meth_bpy_user_resource.ml_name,
                      (PyObject *)PyCFunction_New(&meth_bpy_user_resource, NULL));
+  PyModule_AddObject(mod,
+                     meth_bpy_system_resource.ml_name,
+                     (PyObject *)PyCFunction_New(&meth_bpy_system_resource, NULL));
   PyModule_AddObject(mod,
                      meth_bpy_resource_path.ml_name,
                      (PyObject *)PyCFunction_New(&meth_bpy_resource_path, NULL));