Cleanup: de-duplicate active item center access
authorCampbell Barton <ideasman42@gmail.com>
Wed, 12 Dec 2018 23:37:01 +0000 (10:37 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 12 Dec 2018 23:41:03 +0000 (10:41 +1100)
source/blender/editors/include/ED_object.h
source/blender/editors/mesh/editmesh_tools.c
source/blender/editors/object/CMakeLists.txt
source/blender/editors/object/object_edit.c
source/blender/editors/object/object_utils.c [new file with mode: 0644]
source/blender/editors/space_view3d/view3d_snap.c
source/blender/editors/transform/transform_generics.c

index 58a7e5a1203e5c499756c1f695740ece4a170f00..0a666b02c754840de4453a4b57dfcd5a743f7a5a 100644 (file)
@@ -71,6 +71,14 @@ struct Object *ED_object_context(struct bContext *C);               /* context.o
 struct Object *ED_object_active_context(struct bContext *C); /* context.object or context.active_object */
 void ED_hide_collections_menu_draw(const struct bContext *C, struct uiLayout *layout);
 
+/* object_utils.c */
+bool ED_object_calc_active_center_for_editmode(
+        struct Object *obedit, const bool select_only, float r_center[3]);
+bool ED_object_calc_active_center_for_posemode(
+        struct Object *ob, const bool select_only, float r_center[3]);
+bool ED_object_calc_active_center(
+        struct Object *ob, const bool select_only, float r_center[3]);
+
 /* object_ops.c */
 void ED_operatortypes_object(void);
 void ED_operatormacros_object(void);
@@ -135,9 +143,6 @@ bool ED_object_editmode_enter_ex(struct Main *bmain, struct Scene *scene, struct
 bool ED_object_editmode_enter(struct bContext *C, int flag);
 bool ED_object_editmode_load(struct Main *bmain, struct Object *obedit);
 
-bool ED_object_editmode_calc_active_center(struct Object *obedit, const bool select_only, float r_center[3]);
-
-
 void ED_object_vpaintmode_enter_ex(
         struct Main *bmain, struct Depsgraph *depsgraph, struct wmWindowManager *wm,
         struct Scene *scene, struct Object *ob);
index 9775125af072102ad054218a948074984d9bfba7..1e0ae61ab0113f4ca19d3e99a81ffaae8d182d10 100644 (file)
@@ -7367,7 +7367,7 @@ static int edbm_point_normals_modal(bContext *C, wmOperator *op, const wmEvent *
                                new_mode = EDBM_CLNOR_POINTTO_MODE_COORDINATES;
                                view3d_operator_needs_opengl(C);
                                if (EDBM_select_pick(C, event->mval, false, false, false)) {
-                                       ED_object_editmode_calc_active_center(obedit, false, target);  /* Point to newly selected active. */
+                                       ED_object_calc_active_center_for_editmode(obedit, false, target);  /* Point to newly selected active. */
                                        add_v3_v3(target, obedit->loc);
                                        ret = OPERATOR_RUNNING_MODAL;
                                }
@@ -7412,7 +7412,7 @@ static int edbm_point_normals_modal(bContext *C, wmOperator *op, const wmEvent *
                                                break;
 
                                        case V3D_AROUND_ACTIVE:
-                                               if (!ED_object_editmode_calc_active_center(obedit, false, target)) {
+                                               if (!ED_object_calc_active_center_for_editmode(obedit, false, target)) {
                                                        zero_v3(target);
                                                }
                                                add_v3_v3(target, obedit->loc);
index 17359934c6ea8f912ca8070bf3491e98eec495ab..db8997c5e0a6dcb79e691256bc89b84d3092fa3e 100644 (file)
@@ -64,6 +64,7 @@ set(SRC
        object_shapekey.c
        object_data_transfer.c
        object_transform.c
+       object_utils.c
        object_warp.c
        object_vgroup.c
 
index efbea1ee30ede6c9035ac2ae7b3c34379c242228..c745a13087df61620ca8124417c266a6be252c15 100644 (file)
@@ -1766,68 +1766,6 @@ void OBJECT_OT_mode_set_or_submode(wmOperatorType *ot)
        RNA_def_property_flag(prop, PROP_SKIP_SAVE);
 }
 
-bool ED_object_editmode_calc_active_center(Object *obedit, const bool select_only, float r_center[3])
-{
-       switch (obedit->type) {
-               case OB_MESH:
-               {
-                       BMEditMesh *em = BKE_editmesh_from_object(obedit);
-                       BMEditSelection ese;
-
-                       if (BM_select_history_active_get(em->bm, &ese)) {
-                               BM_editselection_center(&ese, r_center);
-                               return true;
-                       }
-                       break;
-               }
-               case OB_ARMATURE:
-               {
-                       bArmature *arm = obedit->data;
-                       EditBone *ebo = arm->act_edbone;
-
-                       if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
-                               copy_v3_v3(r_center, ebo->head);
-                               return true;
-                       }
-
-                       break;
-               }
-               case OB_CURVE:
-               case OB_SURF:
-               {
-                       Curve *cu = obedit->data;
-
-                       if (ED_curve_active_center(cu, r_center)) {
-                               return true;
-                       }
-                       break;
-               }
-               case OB_MBALL:
-               {
-                       MetaBall *mb = obedit->data;
-                       MetaElem *ml_act = mb->lastelem;
-
-                       if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
-                               copy_v3_v3(r_center, &ml_act->x);
-                               return true;
-                       }
-                       break;
-               }
-               case OB_LATTICE:
-               {
-                       BPoint *actbp = BKE_lattice_active_point_get(obedit->data);
-
-                       if (actbp) {
-                               copy_v3_v3(r_center, actbp->vec);
-                               return true;
-                       }
-                       break;
-               }
-       }
-
-       return false;
-}
-
 static bool move_to_collection_poll(bContext *C)
 {
        if (CTX_wm_space_outliner(C) != NULL) {
diff --git a/source/blender/editors/object/object_utils.c b/source/blender/editors/object/object_utils.c
new file mode 100644 (file)
index 0000000..780942f
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/editors/object/object_utils.c
+ *  \ingroup edobj
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "DNA_anim_types.h"
+#include "DNA_armature_types.h"
+#include "DNA_meta_types.h"
+#include "DNA_object_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_collection_types.h"
+
+#include "BLI_math.h"
+#include "BLI_utildefines.h"
+
+#include "BKE_action.h"
+#include "BKE_editmesh.h"
+#include "BKE_lattice.h"
+
+#include "WM_types.h"
+
+#include "ED_armature.h"
+#include "ED_curve.h"
+#include "ED_object.h"  /* own include */
+
+
+/* -------------------------------------------------------------------- */
+/** \name Active Element Center
+ * \{ */
+
+bool ED_object_calc_active_center_for_editmode(
+        Object *obedit, const bool select_only, float r_center[3])
+{
+       switch (obedit->type) {
+               case OB_MESH:
+               {
+                       BMEditMesh *em = BKE_editmesh_from_object(obedit);
+                       BMEditSelection ese;
+
+                       if (BM_select_history_active_get(em->bm, &ese)) {
+                               BM_editselection_center(&ese, r_center);
+                               return true;
+                       }
+                       break;
+               }
+               case OB_ARMATURE:
+               {
+                       bArmature *arm = obedit->data;
+                       EditBone *ebo = arm->act_edbone;
+
+                       if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
+                               copy_v3_v3(r_center, ebo->head);
+                               return true;
+                       }
+
+                       break;
+               }
+               case OB_CURVE:
+               case OB_SURF:
+               {
+                       Curve *cu = obedit->data;
+
+                       if (ED_curve_active_center(cu, r_center)) {
+                               return true;
+                       }
+                       break;
+               }
+               case OB_MBALL:
+               {
+                       MetaBall *mb = obedit->data;
+                       MetaElem *ml_act = mb->lastelem;
+
+                       if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
+                               copy_v3_v3(r_center, &ml_act->x);
+                               return true;
+                       }
+                       break;
+               }
+               case OB_LATTICE:
+               {
+                       BPoint *actbp = BKE_lattice_active_point_get(obedit->data);
+
+                       if (actbp) {
+                               copy_v3_v3(r_center, actbp->vec);
+                               return true;
+                       }
+                       break;
+               }
+       }
+
+       return false;
+}
+
+bool ED_object_calc_active_center_for_posemode(
+        Object *ob, const bool select_only, float r_center[3])
+{
+       bPoseChannel *pchan = BKE_pose_channel_active(ob);
+       if (pchan && (!select_only || (pchan->bone->flag & BONE_SELECTED))) {
+               copy_v3_v3(r_center, pchan->pose_head);
+               return true;
+       }
+       return false;
+}
+
+bool ED_object_calc_active_center(
+        Object *ob, const bool select_only, float r_center[3])
+{
+       if (ob->mode & OB_MODE_EDIT) {
+               if (ED_object_calc_active_center_for_editmode(ob, select_only, r_center)) {
+                       mul_m4_v3(ob->obmat, r_center);
+                       return true;
+               }
+               return false;
+       }
+       else if (ob->mode & OB_MODE_POSE) {
+               if (ED_object_calc_active_center_for_posemode(ob, select_only, r_center)) {
+                       mul_m4_v3(ob->obmat, r_center);
+                       return true;
+               }
+               return false;
+       }
+       else {
+               if (!select_only || (ob->flag & SELECT)) {
+                       copy_v3_v3(r_center, ob->obmat[3]);
+                       return true;
+               }
+               return false;
+       }
+}
+
+/** \} */
index 1bff52e7950a1de4755364696dd7fc0876eae560..0271bedd3d2f0e5fd7046032ff81269f2bed32f5 100644 (file)
@@ -755,44 +755,11 @@ void VIEW3D_OT_snap_cursor_to_selected(wmOperatorType *ot)
  *
  * Note: this could be exported to be a generic function.
  * see: calculateCenterActive
-**/
+ */
 static bool snap_calc_active_center(bContext *C, const bool select_only, float r_center[3])
 {
-       const Depsgraph *depsgraph = CTX_data_depsgraph(C);
-       Object *obedit = CTX_data_edit_object(C);
-
-       if (obedit) {
-               if (ED_object_editmode_calc_active_center(obedit, select_only, r_center)) {
-                       mul_m4_v3(obedit->obmat, r_center);
-                       return true;
-               }
-       }
-       else {
-               Object *ob = CTX_data_active_object(C);
-               if (ob) {
-                       Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
-
-                       if (ob->mode & OB_MODE_POSE) {
-                               bPoseChannel *pchan = BKE_pose_channel_active(ob_eval);
-                               if (pchan) {
-                                       if (!select_only || (pchan->bone->flag & BONE_SELECTED)) {
-                                               copy_v3_v3(r_center, pchan->pose_head);
-                                               mul_m4_v3(ob_eval->obmat, r_center);
-                                               return true;
-                                       }
-                               }
-                       }
-                       else {
-
-                               if (!select_only || (ob_eval->flag & SELECT)) {
-                                       copy_v3_v3(r_center, ob_eval->obmat[3]);
-                                       return true;
-                               }
-                       }
-               }
-       }
-
-       return false;
+       Object *ob = CTX_data_active_object(C);
+       return ED_object_calc_active_center(ob, select_only, r_center);
 }
 
 static int snap_curs_to_active_exec(bContext *C, wmOperator *UNUSED(op))
index 0e06162e335c988c3451fc49494108550fc79a2f..5b4485d2f66131293d6dd368a3394295e1650381 100644 (file)
@@ -2005,21 +2005,17 @@ bool calculateCenterActive(TransInfo *t, bool select_only, float r_center[3])
        bool ok = false;
 
        if (tc->obedit) {
-               if (ED_object_editmode_calc_active_center(tc->obedit, select_only, r_center)) {
+               if (ED_object_calc_active_center_for_editmode(tc->obedit, select_only, r_center)) {
                        mul_m4_v3(tc->obedit->obmat, r_center);
                        ok = true;
                }
        }
        else if (t->flag & T_POSE) {
                ViewLayer *view_layer = t->view_layer;
-               Object *ob = OBACT(view_layer);
-               if (ob) {
-                       bPoseChannel *pchan = BKE_pose_channel_active(ob);
-                       if (pchan && (!select_only || (pchan->bone->flag & BONE_SELECTED))) {
-                               copy_v3_v3(r_center, pchan->pose_head);
-                               mul_m4_v3(ob->obmat, r_center);
-                               ok = true;
-                       }
+               Object *ob = OBACT(view_layer) ;
+               if (ED_object_calc_active_center_for_posemode(ob, select_only, r_center)) {
+                       mul_m4_v3(ob->obmat, r_center);
+                       ok = true;
                }
        }
        else if (t->options & CTX_PAINT_CURVE) {