[Bf-blender-cvs] [5e509a3aa9d] master: RNA access: Add utils to search a collection item from its name, and get both item pointer and its index in the collection.

Bastien Montagne noreply at git.blender.org
Sun Sep 20 17:00:31 CEST 2020


Commit: 5e509a3aa9d1f14d3293da3b0cc72fb633d7773a
Author: Bastien Montagne
Date:   Fri Sep 18 15:16:30 2020 +0200
Branches: master
https://developer.blender.org/rB5e509a3aa9d1f14d3293da3b0cc72fb633d7773a

RNA access: Add utils to search a collection item from its name, and get both item pointer and its index in the collection.

===================================================================

M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_access.c

===================================================================

diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h
index 7a72ba2dc14..38438f66a0f 100644
--- a/source/blender/makesrna/RNA_access.h
+++ b/source/blender/makesrna/RNA_access.h
@@ -1029,6 +1029,8 @@ int RNA_property_collection_lookup_string(PointerRNA *ptr,
                                           PropertyRNA *prop,
                                           const char *key,
                                           PointerRNA *r_ptr);
+int RNA_property_collection_lookup_string_index(
+    PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr, int *r_index);
 int RNA_property_collection_assign_int(PointerRNA *ptr,
                                        PropertyRNA *prop,
                                        const int key,
diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c
index 793552c5c34..45508aec67a 100644
--- a/source/blender/makesrna/intern/rna_access.c
+++ b/source/blender/makesrna/intern/rna_access.c
@@ -4216,10 +4216,8 @@ int RNA_property_collection_lookup_int(PointerRNA *ptr,
   }
 }
 
-int RNA_property_collection_lookup_string(PointerRNA *ptr,
-                                          PropertyRNA *prop,
-                                          const char *key,
-                                          PointerRNA *r_ptr)
+int RNA_property_collection_lookup_string_index(
+    PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr, int *r_index)
 {
   CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)rna_ensure_property(prop);
 
@@ -4237,9 +4235,10 @@ int RNA_property_collection_lookup_string(PointerRNA *ptr,
     int found = 0;
     int keylen = strlen(key);
     int namelen;
+    int index = 0;
 
     RNA_property_collection_begin(ptr, prop, &iter);
-    for (; iter.valid; RNA_property_collection_next(&iter)) {
+    for (; iter.valid; RNA_property_collection_next(&iter), index++) {
       if (iter.ptr.data && iter.ptr.type->nameproperty) {
         nameprop = iter.ptr.type->nameproperty;
 
@@ -4263,12 +4262,25 @@ int RNA_property_collection_lookup_string(PointerRNA *ptr,
 
     if (!iter.valid) {
       memset(r_ptr, 0, sizeof(*r_ptr));
+      *r_index = -1;
+    }
+    else {
+      *r_index = index;
     }
 
     return iter.valid;
   }
 }
 
+int RNA_property_collection_lookup_string(PointerRNA *ptr,
+                                          PropertyRNA *prop,
+                                          const char *key,
+                                          PointerRNA *r_ptr)
+{
+  int index;
+  return RNA_property_collection_lookup_string_index(ptr, prop, key, r_ptr, &index);
+}
+
 /* zero return is an assignment error */
 int RNA_property_collection_assign_int(PointerRNA *ptr,
                                        PropertyRNA *prop,



More information about the Bf-blender-cvs mailing list