Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2020-12-10 06:09:29 +0300
committerCampbell Barton <ideasman42@gmail.com>2020-12-10 06:40:01 +0300
commitb5bc9d80a11dc99a296d0621bf3fdd156b70b754 (patch)
tree8315b5563a5401f365f02956800cad99a6dec625
parent2f86518ac085dde77977912b3cfcadd2bbd41080 (diff)
PyAPI: add bpy.utils.unescape_identifier
Utility to perform the reverse of `bpy.utils.escape_identifier`
-rw-r--r--release/scripts/modules/bpy/utils/__init__.py2
-rw-r--r--source/blender/python/intern/bpy.c53
2 files changed, 55 insertions, 0 deletions
diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py
index 3ff259e0e3e..897010e80cf 100644
--- a/release/scripts/modules/bpy/utils/__init__.py
+++ b/release/scripts/modules/bpy/utils/__init__.py
@@ -26,6 +26,7 @@ not associated with blenders internal data.
__all__ = (
"blend_paths",
"escape_identifier",
+ "unescape_identifier",
"keyconfig_init",
"keyconfig_set",
"load_scripts",
@@ -60,6 +61,7 @@ from _bpy import (
_utils_units as units,
blend_paths,
escape_identifier,
+ unescape_identifier,
register_class,
resource_path,
script_paths as _bpy_script_paths,
diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index 2e085f08946..bd2f40569f6 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -292,6 +292,50 @@ static PyObject *bpy_escape_identifier(PyObject *UNUSED(self), PyObject *value)
return value_escape;
}
+PyDoc_STRVAR(bpy_unescape_identifier_doc,
+ ".. function:: unescape_identifier(string)\n"
+ "\n"
+ " Simple string un-escape function used for animation paths.\n"
+ " This performs the reverse of `escape_identifier`.\n"
+ "\n"
+ " :arg string: text\n"
+ " :type string: string\n"
+ " :return: The un-escaped string.\n"
+ " :rtype: string\n");
+static PyObject *bpy_unescape_identifier(PyObject *UNUSED(self), PyObject *value)
+{
+ const char *value_str;
+ Py_ssize_t value_str_len;
+
+ char *value_unescape_str;
+ Py_ssize_t value_unescape_str_len;
+ PyObject *value_unescape;
+ size_t size;
+
+ value_str = _PyUnicode_AsStringAndSize(value, &value_str_len);
+
+ if (value_str == NULL) {
+ PyErr_SetString(PyExc_TypeError, "expected a string");
+ return NULL;
+ }
+
+ size = value_str_len + 1;
+ value_unescape_str = PyMem_MALLOC(size);
+ value_unescape_str_len = BLI_str_unescape(value_unescape_str, value_str, size);
+
+ if (value_unescape_str_len == value_str_len) {
+ Py_INCREF(value);
+ value_unescape = value;
+ }
+ else {
+ value_unescape = PyUnicode_FromStringAndSize(value_unescape_str, value_unescape_str_len);
+ }
+
+ PyMem_FREE(value_unescape_str);
+
+ return value_unescape;
+}
+
static PyMethodDef meth_bpy_script_paths = {
"script_paths",
(PyCFunction)bpy_script_paths,
@@ -328,6 +372,12 @@ static PyMethodDef meth_bpy_escape_identifier = {
METH_O,
bpy_escape_identifier_doc,
};
+static PyMethodDef meth_bpy_unescape_identifier = {
+ "unescape_identifier",
+ (PyCFunction)bpy_unescape_identifier,
+ METH_O,
+ bpy_unescape_identifier_doc,
+};
static PyObject *bpy_import_test(const char *modname)
{
@@ -429,6 +479,9 @@ void BPy_init_modules(struct bContext *C)
PyModule_AddObject(mod,
meth_bpy_escape_identifier.ml_name,
(PyObject *)PyCFunction_New(&meth_bpy_escape_identifier, NULL));
+ PyModule_AddObject(mod,
+ meth_bpy_unescape_identifier.ml_name,
+ (PyObject *)PyCFunction_New(&meth_bpy_unescape_identifier, NULL));
/* register funcs (bpy_rna.c) */
PyModule_AddObject(mod,