Files
test/tests/python/bl_rna_paths.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
5.9 KiB
Python
Raw Permalink Normal View History

# SPDX-FileCopyrightText: 2025 Blender Authors
#
# SPDX-License-Identifier: Apache-2.0
import bpy
import unittest
def process_rna_struct(self, struct, rna_path):
# These paths are currently known failures of `path_from_id`.
KNOWN_FAILURES = {
"render.views[\"left\"]",
"render.views[\"right\"]",
"uv_layers[\"UVMap\"]",
"uv_layers[\"UVMap\"]",
}
SKIP_KNOWN_FAILURES = True
def filter_prop_iter(struct):
for p in struct.bl_rna.properties:
# Internal rna meta-data, not expected to support RNA paths generation.
if p.identifier in {"bl_rna", "rna_type"}:
continue
# Only these types can point to sub-structs.
if p.type not in {'POINTER', 'COLLECTION'}:
continue
# TODO: Dynamic typed pointer/collection properties are ignored for now.
if not p.fixed_type:
continue
if bpy.types.ID.bl_rna in {p.fixed_type, p.fixed_type.base}:
continue
yield p
def validate_rna_path(self, struct, rna_path_root, p, p_data, p_keys=[]):
if not p_data:
return None
rna_path_references = [rna_path_root + "." + p.identifier if rna_path_root else p.identifier]
if p_keys:
rna_path_reference = rna_path_references[0]
rna_path_references = [rna_path_reference + '[' + p_key + ']' for p_key in p_keys]
try:
rna_path_generated = p_data.path_from_id()
except ValueError:
return None
if SKIP_KNOWN_FAILURES and rna_path_generated in KNOWN_FAILURES:
return None
self.assertTrue((rna_path_generated in rna_path_references) or ("..." in rna_path_generated),
msg=f"\"{rna_path_generated}\" (from {struct}) failed to match expected paths {rna_path_references}")
return rna_path_generated
for p in filter_prop_iter(struct):
if p.type == 'COLLECTION':
for p_idx, (p_key, p_data) in enumerate(getattr(struct, p.identifier).items()):
p_keys = ['"' + p_key + '"', str(p_idx)] if isinstance(p_key, str) else [str(p_key), str(p_idx)]
rna_path_sub = validate_rna_path(self, struct, rna_path, p, p_data, p_keys)
if rna_path_sub is not None:
process_rna_struct(self, p_data, rna_path_sub)
else:
assert (p.type == 'POINTER')
p_data = getattr(struct, p.identifier)
rna_path_sub = validate_rna_path(self, struct, rna_path, p, p_data)
if rna_path_sub is not None:
process_rna_struct(self, p_data, rna_path_sub)
# Walk over all exposed RNA properties in factory startup file, and compare generated RNA paths to 'actual' paths.
class TestRnaPaths(unittest.TestCase):
def test_paths_generation(self):
bpy.ops.wm.read_factory_settings()
for data_block in bpy.data.user_map().keys():
process_rna_struct(self, data_block, "")
Core: Add info about chain of ancestors (owner data) of a PointerRNA. The general idea is to store an array of (type, data) pointers of all PointerRNA ancestors of the current one. This will help solving cases in our code where the owner (or sometimes even the owner of the owner) of a random PointerRNA needs to be accessed. Current solution mainly relies on linear search from the owner ID, which is sub-optimal at best, and may not even be possible in case a same data is shared between different owners. This lead to refactoring quite a bit of existing PointerRNA creation code. At a high level (i.e. expected usages outside of RNA internals): * Add `RNA_pointer_create_with_parent` and `RNA_pointer_create_id_subdata` to create RNA pointers with ancestors info. * `RNA_id_pointer_create` and `RNA_main_pointer_create` remain unchanged, as they should never have ancestors currently. * Add `RNA_pointer_create_from_ancestor` to re-create a RNA pointer from the nth ancestor of another PointerRNA. * Add basic python API to access this new ancestors data. * Update internal RNA/bpy code to handle ancestors generation in most common generic cases. - The most verbose change here is for collection code, as the owner of the collection property is now passed around, to allow collection items to get a valid ancestors chain. Internally: * `PointerRNA` now has an array of `AncestorPointerRNA` data to store the ancestors. * `PointerRNA` now has constructors that take care of setting its data for most usual cases, including handling of the ancestor array data. * Pointer type refining has been fully factorized into a small utils, `rna_pointer_refine`, that is now used from all code doing that operation. * `rna_pointer_inherit_refine` has been replaced by `rna_pointer_create_with_ancestors` as the core function taking care of creating pointers with valid ancestors info. - Its usage outside of `rna_access` has been essentially reduced to custom collection lookup callbacks. Implements #122431. -------------- Some notes: * The goal of this commit is _not_ to fully cover all cases creating PointerRNA that should also store the ancestors' chain info. It only tackles the most generic code paths (in bpyrna and RNA itself mainly). The remaining 'missing cases' can be tackle later, as needs be. * Performances seem to be only marginally affected currently. * Currently `AncestorPointerRNA` only stores PointerRNA-like data. This will help `StructPathFunc` callbacks to more efficiently generate an RNA paths when calling e.g. `RNA_path_from_ID_to_property`, but will not be enough info to build these paths without these callbacks. And some cases may still remain fuzzy. We'd have to add thinks like a `PropertyRNA` pointer, and for RNA collection ones, an index and string identifier, to store a complete unambiguous 'RNA path' info. This is probably not needed, nor worth the extra processing and memory footprint, for now. Pull Request: https://projects.blender.org/blender/blender/pulls/122427
2025-02-05 15:45:04 +01:00
# Walk over all exposed RNA Collection and Pointer properties in factory startup file, and compare generated RNA
# ancestors to 'actual' ones.
class TestRnaAncestors(unittest.TestCase):
def process_rna_struct(self, struct, ancestors):
def filter_prop_iter(struct):
# These paths are problematic to process, skip for the time being.
SKIP_PROPERTIES = {
bpy.types.Depsgraph.bl_rna.properties["object_instances"],
# XXX To be removed once #133551 is fixed.
bpy.types.ToolSettings.bl_rna.properties["uv_sculpt"],
}
for p in struct.bl_rna.properties:
# Internal rna meta-data, not expected to support RNA paths generation.
if p.identifier in {"bl_rna", "rna_type"}:
continue
if p in SKIP_PROPERTIES:
continue
# Only these types can point to sub-structs.
if p.type not in {'POINTER', 'COLLECTION'}:
continue
# TODO: Dynamic typed pointer/collection properties are ignored for now.
if not p.fixed_type:
continue
if bpy.types.ID.bl_rna in {p.fixed_type, p.fixed_type.base}:
continue
yield p
def process_pointer_property(self, p_data, ancestors_sub):
if not p_data:
return
rna_ancestors = p_data.rna_ancestors()
if not rna_ancestors:
# Do not error for now. Only ensure that if there is a rna_ancestors array, it is valid.
return
if repr(rna_ancestors[0]) != ancestors_sub[0]:
# Do not error for now. There are valid cases where the data is 'rebased' on a new 'root' ID.
Core: Add info about chain of ancestors (owner data) of a PointerRNA. The general idea is to store an array of (type, data) pointers of all PointerRNA ancestors of the current one. This will help solving cases in our code where the owner (or sometimes even the owner of the owner) of a random PointerRNA needs to be accessed. Current solution mainly relies on linear search from the owner ID, which is sub-optimal at best, and may not even be possible in case a same data is shared between different owners. This lead to refactoring quite a bit of existing PointerRNA creation code. At a high level (i.e. expected usages outside of RNA internals): * Add `RNA_pointer_create_with_parent` and `RNA_pointer_create_id_subdata` to create RNA pointers with ancestors info. * `RNA_id_pointer_create` and `RNA_main_pointer_create` remain unchanged, as they should never have ancestors currently. * Add `RNA_pointer_create_from_ancestor` to re-create a RNA pointer from the nth ancestor of another PointerRNA. * Add basic python API to access this new ancestors data. * Update internal RNA/bpy code to handle ancestors generation in most common generic cases. - The most verbose change here is for collection code, as the owner of the collection property is now passed around, to allow collection items to get a valid ancestors chain. Internally: * `PointerRNA` now has an array of `AncestorPointerRNA` data to store the ancestors. * `PointerRNA` now has constructors that take care of setting its data for most usual cases, including handling of the ancestor array data. * Pointer type refining has been fully factorized into a small utils, `rna_pointer_refine`, that is now used from all code doing that operation. * `rna_pointer_inherit_refine` has been replaced by `rna_pointer_create_with_ancestors` as the core function taking care of creating pointers with valid ancestors info. - Its usage outside of `rna_access` has been essentially reduced to custom collection lookup callbacks. Implements #122431. -------------- Some notes: * The goal of this commit is _not_ to fully cover all cases creating PointerRNA that should also store the ancestors' chain info. It only tackles the most generic code paths (in bpyrna and RNA itself mainly). The remaining 'missing cases' can be tackle later, as needs be. * Performances seem to be only marginally affected currently. * Currently `AncestorPointerRNA` only stores PointerRNA-like data. This will help `StructPathFunc` callbacks to more efficiently generate an RNA paths when calling e.g. `RNA_path_from_ID_to_property`, but will not be enough info to build these paths without these callbacks. And some cases may still remain fuzzy. We'd have to add thinks like a `PropertyRNA` pointer, and for RNA collection ones, an index and string identifier, to store a complete unambiguous 'RNA path' info. This is probably not needed, nor worth the extra processing and memory footprint, for now. Pull Request: https://projects.blender.org/blender/blender/pulls/122427
2025-02-05 15:45:04 +01:00
return
if repr(p_data) in ancestors_sub:
# Loop back onto itself, skip.
# E.g. `Scene.view_layer.depsgraph.view_layer`.
return
self.process_rna_struct(p_data, ancestors_sub)
print(struct, "from", ancestors)
self.assertEqual([repr(a) for a in struct.rna_ancestors()], ancestors)
ancestors_sub = ancestors + [repr(struct)]
for p in filter_prop_iter(struct):
if p.type == 'COLLECTION':
for p_key, p_data in getattr(struct, p.identifier).items():
process_pointer_property(self, p_data, ancestors_sub)
else:
assert (p.type == 'POINTER')
p_data = getattr(struct, p.identifier)
process_pointer_property(self, p_data, ancestors_sub)
def test_ancestors(self):
bpy.ops.wm.read_factory_settings()
for data_block in bpy.data.user_map().keys():
self.process_rna_struct(data_block, [])
if __name__ == '__main__':
import sys
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
unittest.main()