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
This commit is contained in:
Alexander Gavrilov 2022-06-24 17:08:54 +03:00
parent 1f8567ac68
commit 935b7a6f65
1 changed files with 15 additions and 0 deletions

View File

@ -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)