[Bf-blender-cvs] [8073c95fe9d] master: Cleanup: Deduplicate logic in `RNA_property_editable` & co.

Bastien Montagne noreply at git.blender.org
Tue Feb 22 16:38:17 CET 2022


Commit: 8073c95fe9d47bc42ba390f61da35062ffd6398a
Author: Bastien Montagne
Date:   Tue Feb 22 16:36:31 2022 +0100
Branches: master
https://developer.blender.org/rB8073c95fe9d47bc42ba390f61da35062ffd6398a

Cleanup: Deduplicate logic in `RNA_property_editable` & co.

Fairly straight-forward, now all the logic for all those 'is editable'
complex checks is gathered into a single util function.

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

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 eb25733a88a..bc4e7314512 100644
--- a/source/blender/makesrna/RNA_access.h
+++ b/source/blender/makesrna/RNA_access.h
@@ -997,7 +997,7 @@ bool RNA_property_editable_info(PointerRNA *ptr, PropertyRNA *prop, const char *
 /**
  * Same as RNA_property_editable(), except this checks individual items in an array.
  */
-bool RNA_property_editable_index(PointerRNA *ptr, PropertyRNA *prop, int index);
+bool RNA_property_editable_index(PointerRNA *ptr, PropertyRNA *prop, const int index);
 
 /**
  * Without lib check, only checks the flag.
diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c
index 59acbc81afd..c8f710d9931 100644
--- a/source/blender/makesrna/intern/rna_access.c
+++ b/source/blender/makesrna/intern/rna_access.c
@@ -1898,17 +1898,28 @@ int RNA_property_ui_icon(const PropertyRNA *prop)
   return rna_ensure_property((PropertyRNA *)prop)->icon;
 }
 
-bool RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop_orig)
+static bool rna_property_editable_do(PointerRNA *ptr,
+                                     PropertyRNA *prop_orig,
+                                     const int index,
+                                     const char **r_info)
 {
   ID *id = ptr->owner_id;
-  int flag;
-  const char *dummy_info;
 
   PropertyRNA *prop = rna_ensure_property(prop_orig);
-  flag = prop->editable ? prop->editable(ptr, &dummy_info) : prop->flag;
+
+  const char *info = "";
+  const int flag = (prop->itemeditable != NULL && index >= 0) ?
+                       prop->itemeditable(ptr, index) :
+                       (prop->editable != NULL ? prop->editable(ptr, &info) : prop->flag);
+  if (r_info != NULL) {
+    *r_info = info;
+  }
 
   /* Early return if the property itself is not editable. */
   if ((flag & PROP_EDITABLE) == 0 || (flag & PROP_REGISTER) != 0) {
+    if (r_info != NULL && (*r_info)[0] == '\0') {
+      *r_info = N_("This property is for internal use only and can't be edited");
+    }
     return false;
   }
 
@@ -1919,55 +1930,31 @@ bool RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop_orig)
 
   /* Handle linked or liboverride ID cases. */
   const bool is_linked_prop_exception = (prop->flag & PROP_LIB_EXCEPTION) != 0;
-  if (ID_IS_LINKED(id)) {
-    return is_linked_prop_exception;
+  if (ID_IS_LINKED(id) && !is_linked_prop_exception) {
+    if (r_info != NULL && (*r_info)[0] == '\0') {
+      *r_info = N_("Can't edit this property from a linked data-block");
+    }
+    return false;
   }
-  if (ID_IS_OVERRIDE_LIBRARY(id)) {
-    return RNA_property_overridable_get(ptr, prop_orig);
+  if (ID_IS_OVERRIDE_LIBRARY(id) && !RNA_property_overridable_get(ptr, prop_orig)) {
+    if (r_info != NULL && (*r_info)[0] == '\0') {
+      *r_info = N_("Can't edit this property from an override data-block");
+    }
+    return false;
   }
 
   /* At this point, property is owned by a local ID and therefore fully editable. */
   return true;
 }
 
-bool RNA_property_editable_info(PointerRNA *ptr, PropertyRNA *prop, const char **r_info)
+bool RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop)
 {
-  ID *id = ptr->owner_id;
-  int flag;
-
-  PropertyRNA *prop_type = rna_ensure_property(prop);
-  *r_info = "";
-
-  /* get flag */
-  if (prop_type->editable) {
-    flag = prop_type->editable(ptr, r_info);
-  }
-  else {
-    flag = prop_type->flag;
-    if ((flag & PROP_EDITABLE) == 0 || (flag & PROP_REGISTER)) {
-      *r_info = N_("This property is for internal use only and can't be edited");
-    }
-  }
-
-  /* property from linked data-block */
-  if (id) {
-    if (ID_IS_LINKED(id) && (prop_type->flag & PROP_LIB_EXCEPTION) == 0) {
-      if (!(*r_info)[0]) {
-        *r_info = N_("Can't edit this property from a linked data-block");
-      }
-      return false;
-    }
-    if (ID_IS_OVERRIDE_LIBRARY(id)) {
-      if (!RNA_property_overridable_get(ptr, prop)) {
-        if (!(*r_info)[0]) {
-          *r_info = N_("Can't edit this property from an override data-block");
-        }
-        return false;
-      }
-    }
-  }
+  return rna_property_editable_do(ptr, prop, -1, NULL);
+}
 
-  return ((flag & PROP_EDITABLE) && (flag & PROP_REGISTER) == 0);
+bool RNA_property_editable_info(PointerRNA *ptr, PropertyRNA *prop, const char **r_info)
+{
+  return rna_property_editable_do(ptr, prop, -1, r_info);
 }
 
 bool RNA_property_editable_flag(PointerRNA *ptr, PropertyRNA *prop)
@@ -1980,40 +1967,11 @@ bool RNA_property_editable_flag(PointerRNA *ptr, PropertyRNA *prop)
   return (flag & PROP_EDITABLE) != 0;
 }
 
-bool RNA_property_editable_index(PointerRNA *ptr, PropertyRNA *prop_orig, int index)
+bool RNA_property_editable_index(PointerRNA *ptr, PropertyRNA *prop, const int index)
 {
   BLI_assert(index >= 0);
 
-  ID *id = ptr->owner_id;
-  const char *dummy_info;
-
-  PropertyRNA *prop = rna_ensure_property(prop_orig);
-
-  const int flag = prop->itemeditable != NULL ?
-                       prop->itemeditable(ptr, index) :
-                       (prop->editable != NULL ? prop->editable(ptr, &dummy_info) : prop->flag);
-
-  /* Early return if the property itself is not editable. */
-  if ((flag & PROP_EDITABLE) == 0 || (flag & PROP_REGISTER) != 0) {
-    return false;
-  }
-
-  /* If there is no owning ID, the property is editable at this point. */
-  if (id == NULL) {
-    return true;
-  }
-
-  /* Handle linked or liboverride ID cases. */
-  const bool is_linked_prop_exception = (prop->flag & PROP_LIB_EXCEPTION) != 0;
-  if (ID_IS_LINKED(id)) {
-    return is_linked_prop_exception;
-  }
-  if (ID_IS_OVERRIDE_LIBRARY(id)) {
-    return RNA_property_overridable_get(ptr, prop_orig);
-  }
-
-  /* At this point, property is owned by a local ID and therefore fully editable. */
-  return true;
+  return rna_property_editable_do(ptr, prop, index, NULL);
 }
 
 bool RNA_property_animateable(PointerRNA *ptr, PropertyRNA *prop)



More information about the Bf-blender-cvs mailing list