PyAPI: support returning non ID types when accessing Context.property

Previously accessing properties without an associated ID would return
None, even when the non-ID data was available.

Ref !116981
This commit is contained in:
Campbell Barton
2024-01-10 22:31:10 +11:00
parent 20ccaa9d67
commit 59dc67974a
3 changed files with 35 additions and 13 deletions

View File

@@ -1,6 +1,11 @@
"""
Get the property associated with a hovered button.
Returns a tuple of the datablock, data path to the property, and array index.
Returns a tuple of the data-block, data path to the property, and array index.
.. note::
When the property doesn't have an associated :class:`bpy.types.ID` non-ID data may be returned.
This may occur when accessing windowing data, for example, operator properties.
"""
# Example inserting keyframe for the hovered property.

View File

@@ -1217,7 +1217,7 @@ context_type_map = {
"particle_settings": ("ParticleSettings", False),
"particle_system": ("ParticleSystem", False),
"particle_system_editable": ("ParticleSystem", False),
"property": ("(:class:`bpy.types.ID`, :class:`string`, :class:`int`)", False),
"property": ("(:class:`bpy.types.AnyType`, :class:`string`, :class:`int`)", False),
"pointcloud": ("PointCloud", False),
"pose_bone": ("PoseBone", False),
"pose_object": ("Object", False),

View File

@@ -4422,19 +4422,36 @@ static PyObject *pyrna_struct_getattro(BPy_StructRNA *self, PyObject *pyname)
break;
}
case CTX_DATA_TYPE_PROPERTY: {
char *path_str = nullptr;
if ((newprop != nullptr) && (newptr.owner_id != nullptr) &&
(path_str = RNA_path_from_ID_to_property(&newptr, newprop)))
{
if (newprop != nullptr) {
/* Create pointer to parent ID, and path from ID to property. */
PointerRNA idptr = RNA_id_pointer_create(newptr.owner_id);
ret = PyTuple_New(3);
PyTuple_SET_ITEMS(ret,
pyrna_struct_CreatePyObject(&idptr),
PyUnicode_FromString(path_str),
PyLong_FromLong(newindex));
PointerRNA idptr;
MEM_freeN(path_str);
PointerRNA *base_ptr;
char *path_str;
if (newptr.owner_id) {
idptr = RNA_id_pointer_create(newptr.owner_id);
path_str = RNA_path_from_ID_to_property(&idptr, newprop);
base_ptr = &idptr;
}
else {
path_str = RNA_path_from_ptr_to_property_index(&newptr, newprop, 0, -1);
base_ptr = &newptr;
}
if (path_str) {
ret = PyTuple_New(3);
PyTuple_SET_ITEMS(ret,
pyrna_struct_CreatePyObject(base_ptr),
PyUnicode_FromString(path_str),
PyLong_FromLong(newindex));
MEM_freeN(path_str);
}
else {
ret = Py_None;
Py_INCREF(ret);
}
}
else {
ret = Py_None;