diff options
author | Campbell Barton <ideasman42@gmail.com> | 2018-12-13 02:37:01 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2018-12-13 02:41:03 +0300 |
commit | 0835c224ba3b245523816700d42d7aad77e8b9fa (patch) | |
tree | 5e2dc9ef1dbb50179c438a2edbbafeb32920b4ad /source/blender/editors/object | |
parent | 8a804d5da71bff7b581a8f4e973ddfe0e2db0d7c (diff) |
Cleanup: de-duplicate active item center access
Diffstat (limited to 'source/blender/editors/object')
-rw-r--r-- | source/blender/editors/object/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/editors/object/object_edit.c | 62 | ||||
-rw-r--r-- | source/blender/editors/object/object_utils.c | 153 |
3 files changed, 154 insertions, 62 deletions
diff --git a/source/blender/editors/object/CMakeLists.txt b/source/blender/editors/object/CMakeLists.txt index 17359934c6e..db8997c5e0a 100644 --- a/source/blender/editors/object/CMakeLists.txt +++ b/source/blender/editors/object/CMakeLists.txt @@ -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 diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c index efbea1ee30e..c745a13087d 100644 --- a/source/blender/editors/object/object_edit.c +++ b/source/blender/editors/object/object_edit.c @@ -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 index 00000000000..780942f04d2 --- /dev/null +++ b/source/blender/editors/object/object_utils.c @@ -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; + } +} + +/** \} */ |