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>2021-03-30 16:44:29 +0300
committerCampbell Barton <ideasman42@gmail.com>2021-03-30 17:02:08 +0300
commit5a6d5d20de2cfa43d14d67970b2f2eb24048b230 (patch)
treea135cefc13d99b3094945684d4f529a785f0e58d /release/scripts/modules/bl_rna_utils
parent88d94d89fadd9d7933f26679aeb0d1c2fa86fe34 (diff)
UI: add description methods for `wm.context_*` operators
Generic context operators now look-up the RNA properties to extract their description (when it's available). Add `bl_rna_utils.data_path.property_definition_from_data_path()` to handle the details of accessing the RNA property definition.
Diffstat (limited to 'release/scripts/modules/bl_rna_utils')
-rw-r--r--release/scripts/modules/bl_rna_utils/__init__.py0
-rw-r--r--release/scripts/modules/bl_rna_utils/data_path.py80
2 files changed, 80 insertions, 0 deletions
diff --git a/release/scripts/modules/bl_rna_utils/__init__.py b/release/scripts/modules/bl_rna_utils/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/release/scripts/modules/bl_rna_utils/__init__.py
diff --git a/release/scripts/modules/bl_rna_utils/data_path.py b/release/scripts/modules/bl_rna_utils/data_path.py
new file mode 100644
index 00000000000..76306e379f0
--- /dev/null
+++ b/release/scripts/modules/bl_rna_utils/data_path.py
@@ -0,0 +1,80 @@
+# ##### 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 compliant>
+
+__all__ = (
+ "property_definition_from_data_path",
+)
+
+class _TokenizeDataPath:
+ """Class to split up tokens of a data-path."""
+ __slots__ = (
+ "data_path",
+ )
+
+ def __init__(self, attrs):
+ self.data_path = attrs
+
+ def __getattr__(self, attr):
+ return _TokenizeDataPath(self.data_path + ((".%s" % attr),))
+
+ def __getitem__(self, key):
+ return _TokenizeDataPath(self.data_path + (("[%r]" % (key,)),))
+
+ def __call__(self, *args, **kw):
+ value_str = ", ".join([
+ val for val in (
+ repr(args)[1:-1],
+ ", ".join(["%s=%r" % (key, value) for key, value in kw.items()])
+ ) if val])
+ return _TokenizeDataPath(self.data_path + ('(%s)' % value_str, ))
+
+ def __iter__(self):
+ return iter(self.data_path)
+
+
+def property_definition_from_data_path(base, data_path):
+ """
+ Return an RNA property definition from an object and a data path.
+
+ In Blender this is often used with ``context`` as the base and a
+ path that it references, for example ``.space_data.lock_camera``.
+ """
+ base_tokenize = _TokenizeDataPath(())
+ data = list(eval("base_tokenize" + data_path))
+ del base_tokenize
+ while data and (not data[-1].startswith(".")):
+ data.pop()
+
+ if (not data) or (not data[-1].startswith(".")) or (len(data) < 2):
+ return None
+
+ data_path_head = "".join(data[:-1])
+ data_path_tail = data[-1]
+
+ value_head = eval("base" + data_path_head)
+ value_head_rna = getattr(value_head, "bl_rna", None)
+ if value_head_rna is None:
+ return None
+
+ value_tail = value_head.bl_rna.properties.get(data_path_tail[1:])
+ if not value_tail:
+ return None
+
+ return value_tail