From 07f3ad06fc0900f719294c7b59598ac58e410dea Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 23 Jul 2019 16:23:56 +1000 Subject: Fix T63775: Toolbar icons ignore BLENDER_SYSTEM_DATAFILES 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 | 1 + .../startup/bl_ui/space_toolsystem_common.py | 7 +-- source/blender/python/intern/bpy.c | 52 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py index e6424de0742..b39099158c6 100644 --- a/release/scripts/modules/bpy/utils/__init__.py +++ b/release/scripts/modules/bpy/utils/__init__.py @@ -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 diff --git a/release/scripts/startup/bl_ui/space_toolsystem_common.py b/release/scripts/startup/bl_ui/space_toolsystem_common.py index e7e95c26b55..222185e18d5 100644 --- a/release/scripts/startup/bl_ui/space_toolsystem_common.py +++ b/release/scripts/startup/bl_ui/space_toolsystem_common.py @@ -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: diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c index 92ba7704b19..b8a83d0588d 100644 --- a/source/blender/python/intern/bpy.c +++ b/source/blender/python/intern/bpy.c @@ -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)); -- cgit v1.2.3