[Bf-blender-cvs] [ebb519652ce] master: Object: add functionality to access the object as an index for operators

Campbell Barton noreply at git.blender.org
Fri Jan 20 04:04:56 CET 2023


Commit: ebb519652cecad93867f13c4c21bd8ef58f15909
Author: Campbell Barton
Date:   Fri Jan 20 12:46:06 2023 +1100
Branches: master
https://developer.blender.org/rBebb519652cecad93867f13c4c21bd8ef58f15909

Object: add functionality to access the object as an index for operators

Add:
- ED_object_in_mode_to_index
- ED_object_in_mode_from_index

Useful for operators that act on a non-active object in multi-edit mode,
so the index can be stored for the operators exec function to read back
the value when redoing an action. Needed to resolve T102680.

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

M	source/blender/editors/include/ED_object.h
M	source/blender/editors/object/object_edit.c

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

diff --git a/source/blender/editors/include/ED_object.h b/source/blender/editors/include/ED_object.h
index 798175b7cb8..82f8505509a 100644
--- a/source/blender/editors/include/ED_object.h
+++ b/source/blender/editors/include/ED_object.h
@@ -507,6 +507,31 @@ void ED_object_posemode_set_for_weight_paint(struct bContext *C,
                                              struct Object *ob,
                                              bool is_mode_set);
 
+/**
+ * Return the index of an object in a mode (typically edit/pose mode).
+ *
+ * Useful for operators with multi-mode editing to be able to redo an action on an object
+ * by it's index which (unlike pointers) the operator can store for redo.
+ *
+ * The indices aren't intended to be useful from Python scripts,
+ * although they are not prevented from passing them in, this is mainly to enable redo.
+ * For scripts it's more convenient to set the object active before operating on it.
+ *
+ * \note The active object is always index 0.
+ */
+int ED_object_in_mode_to_index(const struct Scene *scene,
+                               struct ViewLayer *view_layer,
+                               eObjectMode mode,
+                               const struct Object *ob);
+
+/**
+ * Access the object from the index returned by #ED_object_in_mode_to_index.
+ */
+Object *ED_object_in_mode_from_index(const struct Scene *scene,
+                                     struct ViewLayer *view_layer,
+                                     eObjectMode mode,
+                                     int index);
+
 /* object_modifier.c */
 
 enum {
diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c
index b1cebc32513..2939c31bc5c 100644
--- a/source/blender/editors/object/object_edit.c
+++ b/source/blender/editors/object/object_edit.c
@@ -217,6 +217,52 @@ Object **ED_object_array_in_mode_or_selected(bContext *C,
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Object Index Lookup/Creation
+ * \{ */
+
+int ED_object_in_mode_to_index(const Scene *scene,
+                               ViewLayer *view_layer,
+                               const eObjectMode mode,
+                               const Object *ob)
+{
+  BLI_assert(ob != NULL);
+  /* NOTE: the `v3d` is always NULL because the purpose of this function is to return
+   * a reusable index, using the `v3d` only increases the chance the index may become invalid. */
+  int index = -1;
+  int i = 0;
+  FOREACH_BASE_IN_MODE_BEGIN (scene, view_layer, NULL, -1, mode, base_iter) {
+    if (base_iter->object == ob) {
+      index = i;
+      break;
+    }
+    i++;
+  }
+  FOREACH_BASE_IN_MODE_END;
+  return index;
+}
+
+Object *ED_object_in_mode_from_index(const Scene *scene,
+                                     ViewLayer *view_layer,
+                                     const eObjectMode mode,
+                                     int index)
+{
+  BLI_assert(index >= 0);
+  Object *ob = NULL;
+  int i = 0;
+  FOREACH_BASE_IN_MODE_BEGIN (scene, view_layer, NULL, -1, mode, base_iter) {
+    if (index == i) {
+      ob = base_iter->object;
+      break;
+    }
+    i++;
+  }
+  FOREACH_BASE_IN_MODE_END;
+  return ob;
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Hide Operator
  * \{ */



More information about the Bf-blender-cvs mailing list