From 935b7a6f65d2c90e54b59a50fd1c488f02da0e8e Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Fri, 24 Jun 2022 17:08:54 +0300 Subject: 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 --- release/scripts/modules/bpy_types.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'release') 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) -- cgit v1.2.3