diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2022-06-24 17:08:54 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2022-07-18 17:38:00 +0300 |
commit | 935b7a6f65d2c90e54b59a50fd1c488f02da0e8e (patch) | |
tree | c59f941c51f8fe3c885744dd400fe1cc4a66fe91 /release/scripts/modules | |
parent | 1f8567ac68b667ba9b001fd2f33c4c1f24ccafa0 (diff) |
Context: implement indexing for list properties in path_resolve.
The real RNA path_resolve method supports indexing lists,
but the python version on the Context object does not. This
patch adds the missing feature for completeness.
Differential Revision: https://developer.blender.org/D15413
Diffstat (limited to 'release/scripts/modules')
-rw-r--r-- | release/scripts/modules/bpy_types.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py index aaa2ae59a0d..df0631ec26d 100644 --- a/release/scripts/modules/bpy_types.py +++ b/release/scripts/modules/bpy_types.py @@ -56,6 +56,21 @@ class Context(StructRNA): if value is None: return value + # If the attribute is a list property, apply subscripting. + if isinstance(value, list) and path_rest.startswith("["): + index_str, div, index_tail = path_rest[1:].partition("]") + if not div: + raise ValueError("Path index is not terminated: %s%s" % (attr, path_rest)) + try: + index = int(index_str) + except ValueError: + raise ValueError("Path index is invalid: %s[%s]" % (attr, index_str)) + if 0 <= index < len(value): + path_rest = index_tail + value = value[index] + else: + raise IndexError("Path index out of range: %s[%s]" % (attr, index_str)) + # Resolve the rest of the path if necessary. if path_rest: path_resolve_fn = getattr(value, "path_resolve", None) |