2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2002-2008 full recode
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/object/object_transform.c
34 #include "DNA_anim_types.h"
35 #include "DNA_armature_types.h"
36 #include "DNA_mesh_types.h"
37 #include "DNA_meta_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_lattice_types.h"
44 #include "BLI_listbase.h"
45 #include "BLI_utildefines.h"
47 #include "BKE_context.h"
48 #include "BKE_curve.h"
50 #include "BKE_idcode.h"
51 #include "BKE_mball.h"
53 #include "BKE_object.h"
54 #include "BKE_report.h"
55 #include "BKE_editmesh.h"
56 #include "BKE_multires.h"
57 #include "BKE_armature.h"
58 #include "BKE_lattice.h"
59 #include "BKE_tracking.h"
61 #include "DEG_depsgraph.h"
63 #include "RNA_define.h"
64 #include "RNA_access.h"
69 #include "ED_armature.h"
70 #include "ED_keyframing.h"
72 #include "ED_screen.h"
73 #include "ED_view3d.h"
75 #include "object_intern.h"
77 /*************************** Clear Transformation ****************************/
79 /* clear location of object */
80 static void object_clear_loc(Object *ob, const bool clear_delta)
82 /* clear location if not locked */
83 if ((ob->protectflag & OB_LOCK_LOCX) == 0) {
85 if (clear_delta) ob->dloc[0] = 0.0f;
87 if ((ob->protectflag & OB_LOCK_LOCY) == 0) {
89 if (clear_delta) ob->dloc[1] = 0.0f;
91 if ((ob->protectflag & OB_LOCK_LOCZ) == 0) {
93 if (clear_delta) ob->dloc[2] = 0.0f;
97 /* clear rotation of object */
98 static void object_clear_rot(Object *ob, const bool clear_delta)
100 /* clear rotations that aren't locked */
101 if (ob->protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) {
102 if (ob->protectflag & OB_LOCK_ROT4D) {
103 /* perform clamping on a component by component basis */
104 if (ob->rotmode == ROT_MODE_AXISANGLE) {
105 if ((ob->protectflag & OB_LOCK_ROTW) == 0) {
107 if (clear_delta) ob->drotAngle = 0.0f;
109 if ((ob->protectflag & OB_LOCK_ROTX) == 0) {
110 ob->rotAxis[0] = 0.0f;
111 if (clear_delta) ob->drotAxis[0] = 0.0f;
113 if ((ob->protectflag & OB_LOCK_ROTY) == 0) {
114 ob->rotAxis[1] = 0.0f;
115 if (clear_delta) ob->drotAxis[1] = 0.0f;
117 if ((ob->protectflag & OB_LOCK_ROTZ) == 0) {
118 ob->rotAxis[2] = 0.0f;
119 if (clear_delta) ob->drotAxis[2] = 0.0f;
122 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
123 if (IS_EQF(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQF(ob->rotAxis[1], ob->rotAxis[2]))
124 ob->rotAxis[1] = 1.0f;
125 if (IS_EQF(ob->drotAxis[0], ob->drotAxis[1]) && IS_EQF(ob->drotAxis[1], ob->drotAxis[2]) && clear_delta)
126 ob->drotAxis[1] = 1.0f;
128 else if (ob->rotmode == ROT_MODE_QUAT) {
129 if ((ob->protectflag & OB_LOCK_ROTW) == 0) {
131 if (clear_delta) ob->dquat[0] = 1.0f;
133 if ((ob->protectflag & OB_LOCK_ROTX) == 0) {
135 if (clear_delta) ob->dquat[1] = 0.0f;
137 if ((ob->protectflag & OB_LOCK_ROTY) == 0) {
139 if (clear_delta) ob->dquat[2] = 0.0f;
141 if ((ob->protectflag & OB_LOCK_ROTZ) == 0) {
143 if (clear_delta) ob->dquat[3] = 0.0f;
145 /* TODO: does this quat need normalizing now? */
148 /* the flag may have been set for the other modes, so just ignore the extra flag... */
149 if ((ob->protectflag & OB_LOCK_ROTX) == 0) {
151 if (clear_delta) ob->drot[0] = 0.0f;
153 if ((ob->protectflag & OB_LOCK_ROTY) == 0) {
155 if (clear_delta) ob->drot[1] = 0.0f;
157 if ((ob->protectflag & OB_LOCK_ROTZ) == 0) {
159 if (clear_delta) ob->drot[2] = 0.0f;
164 /* perform clamping using euler form (3-components) */
165 /* FIXME: deltas are not handled for these cases yet... */
166 float eul[3], oldeul[3], quat1[4] = {0};
168 if (ob->rotmode == ROT_MODE_QUAT) {
169 copy_qt_qt(quat1, ob->quat);
170 quat_to_eul(oldeul, ob->quat);
172 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
173 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, ob->rotAxis, ob->rotAngle);
176 copy_v3_v3(oldeul, ob->rot);
179 eul[0] = eul[1] = eul[2] = 0.0f;
181 if (ob->protectflag & OB_LOCK_ROTX)
183 if (ob->protectflag & OB_LOCK_ROTY)
185 if (ob->protectflag & OB_LOCK_ROTZ)
188 if (ob->rotmode == ROT_MODE_QUAT) {
189 eul_to_quat(ob->quat, eul);
190 /* quaternions flip w sign to accumulate rotations correctly */
191 if ((quat1[0] < 0.0f && ob->quat[0] > 0.0f) || (quat1[0] > 0.0f && ob->quat[0] < 0.0f)) {
192 mul_qt_fl(ob->quat, -1.0f);
195 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
196 eulO_to_axis_angle(ob->rotAxis, &ob->rotAngle, eul, EULER_ORDER_DEFAULT);
199 copy_v3_v3(ob->rot, eul);
202 } // Duplicated in source/blender/editors/armature/editarmature.c
204 if (ob->rotmode == ROT_MODE_QUAT) {
206 if (clear_delta) unit_qt(ob->dquat);
208 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
209 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
210 if (clear_delta) unit_axis_angle(ob->drotAxis, &ob->drotAngle);
214 if (clear_delta) zero_v3(ob->drot);
219 /* clear scale of object */
220 static void object_clear_scale(Object *ob, const bool clear_delta)
222 /* clear scale factors which are not locked */
223 if ((ob->protectflag & OB_LOCK_SCALEX) == 0) {
225 if (clear_delta) ob->dscale[0] = 1.0f;
227 if ((ob->protectflag & OB_LOCK_SCALEY) == 0) {
229 if (clear_delta) ob->dscale[1] = 1.0f;
231 if ((ob->protectflag & OB_LOCK_SCALEZ) == 0) {
233 if (clear_delta) ob->dscale[2] = 1.0f;
237 /* --------------- */
239 /* generic exec for clear-transform operators */
240 static int object_clear_transform_generic_exec(bContext *C, wmOperator *op,
241 void (*clear_func)(Object *, const bool),
242 const char default_ksName[])
244 Scene *scene = CTX_data_scene(C);
246 const bool clear_delta = RNA_boolean_get(op->ptr, "clear_delta");
249 if (ELEM(NULL, clear_func, default_ksName)) {
250 BKE_report(op->reports, RPT_ERROR, "Programming error: missing clear transform function or keying set name");
251 return OPERATOR_CANCELLED;
254 /* get KeyingSet to use */
255 ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
257 /* operate on selected objects only if they aren't in weight-paint mode
258 * (so that object-transform clearing won't be applied at same time as bone-clearing)
260 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
262 if (!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
263 /* run provided clearing function */
264 clear_func(ob, clear_delta);
266 ED_autokeyframe_object(C, scene, ob, ks);
268 /* tag for updates */
269 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
274 /* this is needed so children are also updated */
275 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
277 return OPERATOR_FINISHED;
280 /* --------------- */
283 static int object_location_clear_exec(bContext *C, wmOperator *op)
285 return object_clear_transform_generic_exec(C, op, object_clear_loc, ANIM_KS_LOCATION_ID);
288 void OBJECT_OT_location_clear(wmOperatorType *ot)
291 ot->name = "Clear Location";
292 ot->description = "Clear the object's location";
293 ot->idname = "OBJECT_OT_location_clear";
296 ot->exec = object_location_clear_exec;
297 ot->poll = ED_operator_scene_editable;
300 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
304 ot->prop = RNA_def_boolean(ot->srna, "clear_delta", false, "Clear Delta",
305 "Clear delta location in addition to clearing the normal location transform");
308 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
310 return object_clear_transform_generic_exec(C, op, object_clear_rot, ANIM_KS_ROTATION_ID);
313 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
316 ot->name = "Clear Rotation";
317 ot->description = "Clear the object's rotation";
318 ot->idname = "OBJECT_OT_rotation_clear";
321 ot->exec = object_rotation_clear_exec;
322 ot->poll = ED_operator_scene_editable;
325 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
328 ot->prop = RNA_def_boolean(ot->srna, "clear_delta", false, "Clear Delta",
329 "Clear delta rotation in addition to clearing the normal rotation transform");
332 static int object_scale_clear_exec(bContext *C, wmOperator *op)
334 return object_clear_transform_generic_exec(C, op, object_clear_scale, ANIM_KS_SCALING_ID);
337 void OBJECT_OT_scale_clear(wmOperatorType *ot)
340 ot->name = "Clear Scale";
341 ot->description = "Clear the object's scale";
342 ot->idname = "OBJECT_OT_scale_clear";
345 ot->exec = object_scale_clear_exec;
346 ot->poll = ED_operator_scene_editable;
349 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
352 ot->prop = RNA_def_boolean(ot->srna, "clear_delta", false, "Clear Delta",
353 "Clear delta scale in addition to clearing the normal scale transform");
356 /* --------------- */
358 static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
363 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
366 /* vectors pointed to by v1 and v3 will get modified */
368 v3 = ob->parentinv[3];
370 copy_m3_m4(mat, ob->parentinv);
371 negate_v3_v3(v3, v1);
375 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
379 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
381 return OPERATOR_FINISHED;
384 void OBJECT_OT_origin_clear(wmOperatorType *ot)
387 ot->name = "Clear Origin";
388 ot->description = "Clear the object's origin";
389 ot->idname = "OBJECT_OT_origin_clear";
392 ot->exec = object_origin_clear_exec;
393 ot->poll = ED_operator_scene_editable;
396 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
399 /*************************** Apply Transformation ****************************/
401 /* use this when the loc/size/rot of the parent has changed but the children
402 * should stay in the same place, e.g. for apply-size-rot or object center */
403 static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob)
408 /* a change was made, adjust the children to compensate */
409 for (ob_child = bmain->object.first; ob_child; ob_child = ob_child->id.next) {
410 if (ob_child->parent == ob) {
411 BKE_object_apply_mat4(ob_child, ob_child->obmat, true, false);
412 BKE_object_workob_calc_parent(scene, ob_child, &workob);
413 invert_m4_m4(ob_child->parentinv, workob.obmat);
418 static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_loc, bool apply_rot, bool apply_scale)
420 Main *bmain = CTX_data_main(C);
421 Scene *scene = CTX_data_scene(C);
422 float rsmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
425 /* first check if we can execute */
426 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
428 if (ELEM(ob->type, OB_MESH, OB_ARMATURE, OB_LATTICE, OB_MBALL, OB_CURVE, OB_SURF, OB_FONT)) {
429 ID *obdata = ob->data;
430 if (ID_REAL_USERS(obdata) > 1) {
431 BKE_reportf(reports, RPT_ERROR,
432 "Cannot apply to a multi user: Object \"%s\", %s \"%s\", aborting",
433 ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
437 if (ID_IS_LINKED_DATABLOCK(obdata)) {
438 BKE_reportf(reports, RPT_ERROR,
439 "Cannot apply to library data: Object \"%s\", %s \"%s\", aborting",
440 ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
445 if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
446 ID *obdata = ob->data;
451 if (((ob->type == OB_CURVE) && !(cu->flag & CU_3D)) && (apply_rot || apply_loc)) {
452 BKE_reportf(reports, RPT_ERROR,
453 "Rotation/Location can't apply to a 2D curve: Object \"%s\", %s \"%s\", aborting",
454 ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
458 BKE_reportf(reports, RPT_ERROR,
459 "Can't apply to a curve with shape-keys: Object \"%s\", %s \"%s\", aborting",
460 ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
465 if (ob->type == OB_FONT) {
466 if (apply_rot || apply_loc) {
467 BKE_reportf(reports, RPT_ERROR,
468 "Font's can only have scale applied: \"%s\"",
477 return OPERATOR_CANCELLED;
482 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
485 /* calculate rotation/scale matrix */
486 if (apply_scale && apply_rot)
487 BKE_object_to_mat3(ob, rsmat);
488 else if (apply_scale)
489 BKE_object_scale_to_mat3(ob, rsmat);
490 else if (apply_rot) {
491 float tmat[3][3], timat[3][3];
493 /* simple rotation matrix */
494 BKE_object_rot_to_mat3(ob, rsmat, true);
496 /* correct for scale, note mul_m3_m3m3 has swapped args! */
497 BKE_object_scale_to_mat3(ob, tmat);
498 invert_m3_m3(timat, tmat);
499 mul_m3_m3m3(rsmat, timat, rsmat);
500 mul_m3_m3m3(rsmat, rsmat, tmat);
505 copy_m4_m3(mat, rsmat);
507 /* calculate translation */
509 copy_v3_v3(mat[3], ob->loc);
511 if (!(apply_scale && apply_rot)) {
513 /* correct for scale and rotation that is still applied */
514 BKE_object_to_mat3(ob, obmat);
515 invert_m3_m3(iobmat, obmat);
516 mul_m3_m3m3(tmat, rsmat, iobmat);
517 mul_m3_v3(tmat, mat[3]);
521 /* apply to object data */
522 if (ob->type == OB_MESH) {
526 multiresModifier_scale_disp(scene, ob);
529 BKE_mesh_transform(me, mat, true);
532 BKE_mesh_calc_normals(me);
534 else if (ob->type == OB_ARMATURE) {
535 ED_armature_apply_transform(ob, mat);
537 else if (ob->type == OB_LATTICE) {
538 Lattice *lt = ob->data;
540 BKE_lattice_transform(lt, mat, true);
542 else if (ob->type == OB_MBALL) {
543 MetaBall *mb = ob->data;
544 BKE_mball_transform(mb, mat);
546 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
547 Curve *cu = ob->data;
548 scale = mat3_to_scale(rsmat);
549 BKE_curve_transform_ex(cu, mat, true, scale);
551 else if (ob->type == OB_FONT) {
552 Curve *cu = ob->data;
555 scale = mat3_to_scale(rsmat);
557 for (i = 0; i < cu->totbox; i++) {
558 TextBox *tb = &cu->tb[i];
567 else if (ob->type == OB_CAMERA) {
568 MovieClip *clip = BKE_object_movieclip_get(scene, ob, false);
570 /* applying scale on camera actually scales clip's reconstruction.
571 * of there's clip assigned to camera nothing to do actually.
577 BKE_tracking_reconstruction_scale(&clip->tracking, ob->size);
579 else if (ob->type == OB_EMPTY) {
580 /* It's possible for empties too, even though they don't
581 * really have obdata, since we can simply apply the maximum
582 * scaling to the empty's drawsize.
585 * 1) Most scaled empties have uniform scaling
586 * (i.e. for visibility reasons), AND/OR
587 * 2) Preserving non-uniform scaling is not that important,
588 * and is something that many users would be willing to
589 * sacrifice for having an easy way to do this.
592 if ((apply_loc == false) &&
593 (apply_rot == false) &&
594 (apply_scale == true))
596 float max_scale = max_fff(fabsf(ob->size[0]), fabsf(ob->size[1]), fabsf(ob->size[2]));
597 ob->empty_drawsize *= max_scale;
607 ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
611 unit_axis_angle(ob->rotAxis, &ob->rotAngle);
614 BKE_object_where_is_calc(scene, ob);
615 if (ob->type == OB_ARMATURE) {
616 BKE_pose_where_is(scene, ob); /* needed for bone parents */
619 ignore_parent_tx(bmain, scene, ob);
621 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
628 BKE_report(reports, RPT_WARNING, "Objects have no data to transform");
629 return OPERATOR_CANCELLED;
632 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
633 return OPERATOR_FINISHED;
636 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
638 Scene *scene = CTX_data_scene(C);
639 bool changed = false;
641 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
643 BKE_object_where_is_calc(scene, ob);
644 BKE_object_apply_mat4(ob, ob->obmat, true, true);
645 BKE_object_where_is_calc(scene, ob);
647 /* update for any children that may get moved */
648 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
655 return OPERATOR_CANCELLED;
657 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
658 return OPERATOR_FINISHED;
661 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
664 ot->name = "Apply Visual Transform";
665 ot->description = "Apply the object's visual transformation to its data";
666 ot->idname = "OBJECT_OT_visual_transform_apply";
669 ot->exec = visual_transform_apply_exec;
670 ot->poll = ED_operator_scene_editable;
673 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
676 static int object_transform_apply_exec(bContext *C, wmOperator *op)
678 const bool loc = RNA_boolean_get(op->ptr, "location");
679 const bool rot = RNA_boolean_get(op->ptr, "rotation");
680 const bool sca = RNA_boolean_get(op->ptr, "scale");
682 if (loc || rot || sca) {
683 return apply_objects_internal(C, op->reports, loc, rot, sca);
687 return OPERATOR_FINISHED;
691 void OBJECT_OT_transform_apply(wmOperatorType *ot)
694 ot->name = "Apply Object Transform";
695 ot->description = "Apply the object's transformation to its data";
696 ot->idname = "OBJECT_OT_transform_apply";
699 ot->exec = object_transform_apply_exec;
700 ot->poll = ED_operator_objectmode;
703 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
705 RNA_def_boolean(ot->srna, "location", 0, "Location", "");
706 RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
707 RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
710 /********************* Set Object Center ************************/
713 GEOMETRY_TO_ORIGIN = 0,
716 ORIGIN_TO_CENTER_OF_MASS
719 static int object_origin_set_exec(bContext *C, wmOperator *op)
721 Main *bmain = CTX_data_main(C);
722 Scene *scene = CTX_data_scene(C);
723 Object *obact = CTX_data_active_object(C);
724 Object *obedit = CTX_data_edit_object(C);
726 float cursor[3], cent[3], cent_neg[3], centn[3];
727 int centermode = RNA_enum_get(op->ptr, "type");
728 int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
730 ListBase ctx_data_list;
731 CollectionPointerLink *ctx_ob;
732 CollectionPointerLink *ctx_ob_act = NULL;
734 /* keep track of what is changed */
735 int tot_change = 0, tot_lib_error = 0, tot_multiuser_arm_error = 0;
737 if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
738 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
739 return OPERATOR_CANCELLED;
742 /* get the view settings if 'around' isn't set and the view is available */
743 View3D *v3d = CTX_wm_view3d(C);
744 copy_v3_v3(cursor, ED_view3d_cursor3d_get(scene, v3d));
745 if (v3d && !RNA_struct_property_is_set(op->ptr, "center"))
746 around = v3d->around;
752 if (obedit->type == OB_MESH) {
753 Mesh *me = obedit->data;
754 BMEditMesh *em = me->edit_btmesh;
758 if (centermode == ORIGIN_TO_CURSOR) {
759 copy_v3_v3(cent, cursor);
760 invert_m4_m4(obedit->imat, obedit->obmat);
761 mul_m4_v3(obedit->imat, cent);
764 if (around == V3D_AROUND_CENTER_MEAN) {
765 if (em->bm->totvert) {
766 const float total_div = 1.0f / (float)em->bm->totvert;
767 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
768 madd_v3_v3fl(cent, eve->co, total_div);
773 float min[3], max[3];
774 INIT_MINMAX(min, max);
775 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
776 minmax_v3v3_v3(min, max, eve->co);
778 mid_v3_v3v3(cent, min, max);
782 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
783 sub_v3_v3(eve->co, cent);
786 EDBM_mesh_normals_update(em);
788 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
792 CTX_data_selected_editable_objects(C, &ctx_data_list);
795 for (ctx_ob = ctx_data_list.first;
797 ctx_ob = ctx_ob->next)
799 Object *ob = ctx_ob->ptr.data;
800 ob->flag &= ~OB_DONE;
802 /* move active first */
809 BLI_listbase_rotate_first(&ctx_data_list, (LinkData *)ctx_ob_act);
812 for (tob = bmain->object.first; tob; tob = tob->id.next) {
814 ((ID *)tob->data)->tag &= ~LIB_TAG_DOIT;
816 ((ID *)tob->dup_group)->tag &= ~LIB_TAG_DOIT;
819 for (ctx_ob = ctx_data_list.first;
821 ctx_ob = ctx_ob->next)
823 Object *ob = ctx_ob->ptr.data;
825 if ((ob->flag & OB_DONE) == 0) {
826 bool do_inverse_offset = false;
829 if (centermode == ORIGIN_TO_CURSOR) {
830 copy_v3_v3(cent, cursor);
831 invert_m4_m4(ob->imat, ob->obmat);
832 mul_m4_v3(ob->imat, cent);
835 if (ob->data == NULL) {
836 /* special support for dupligroups */
837 if ((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.tag & LIB_TAG_DOIT) == 0) {
838 if (ID_IS_LINKED_DATABLOCK(ob->dup_group)) {
842 if (centermode == ORIGIN_TO_CURSOR) {
846 float min[3], max[3];
847 /* only bounds support */
848 INIT_MINMAX(min, max);
849 BKE_object_minmax_dupli(scene, ob, min, max, true);
850 mid_v3_v3v3(cent, min, max);
851 invert_m4_m4(ob->imat, ob->obmat);
852 mul_m4_v3(ob->imat, cent);
855 add_v3_v3(ob->dup_group->dupli_ofs, cent);
858 ob->dup_group->id.tag |= LIB_TAG_DOIT;
859 do_inverse_offset = true;
863 else if (ID_IS_LINKED_DATABLOCK(ob->data)) {
867 if (obedit == NULL && ob->type == OB_MESH) {
870 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
871 else if (centermode == ORIGIN_TO_CENTER_OF_MASS) { BKE_mesh_center_centroid(me, cent); }
872 else if (around == V3D_AROUND_CENTER_MEAN) { BKE_mesh_center_median(me, cent); }
873 else { BKE_mesh_center_bounds(me, cent); }
875 negate_v3_v3(cent_neg, cent);
876 BKE_mesh_translate(me, cent_neg, 1);
879 me->id.tag |= LIB_TAG_DOIT;
880 do_inverse_offset = true;
882 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
883 Curve *cu = ob->data;
885 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
886 else if (around == V3D_AROUND_CENTER_MEAN) { BKE_curve_center_median(cu, cent); }
887 else { BKE_curve_center_bounds(cu, cent); }
889 /* don't allow Z change if curve is 2D */
890 if ((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
893 negate_v3_v3(cent_neg, cent);
894 BKE_curve_translate(cu, cent_neg, 1);
897 cu->id.tag |= LIB_TAG_DOIT;
898 do_inverse_offset = true;
901 if (centermode == GEOMETRY_TO_ORIGIN) {
902 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
907 else if (ob->type == OB_FONT) {
910 Curve *cu = ob->data;
912 if (ob->bb == NULL && (centermode != ORIGIN_TO_CURSOR)) {
916 if (centermode == ORIGIN_TO_CURSOR) {
920 /* extra 0.5 is the height o above line */
921 cent[0] = 0.5f * (ob->bb->vec[4][0] + ob->bb->vec[0][0]);
922 cent[1] = 0.5f * (ob->bb->vec[0][1] + ob->bb->vec[2][1]);
927 cu->xof = cu->xof - (cent[0] / cu->fsize);
928 cu->yof = cu->yof - (cent[1] / cu->fsize);
931 cu->id.tag |= LIB_TAG_DOIT;
932 do_inverse_offset = true;
935 else if (ob->type == OB_ARMATURE) {
936 bArmature *arm = ob->data;
938 if (ID_REAL_USERS(arm) > 1) {
940 BKE_report(op->reports, RPT_ERROR, "Cannot apply to a multi user armature");
943 tot_multiuser_arm_error++;
946 /* Function to recenter armatures in editarmature.c
947 * Bone + object locations are handled there.
949 ED_armature_origin_set(scene, ob, cursor, centermode, around);
952 arm->id.tag |= LIB_TAG_DOIT;
953 /* do_inverse_offset = true; */ /* docenter_armature() handles this */
955 BKE_object_where_is_calc(scene, ob);
956 BKE_pose_where_is(scene, ob); /* needed for bone parents */
958 ignore_parent_tx(bmain, scene, ob);
964 else if (ob->type == OB_MBALL) {
965 MetaBall *mb = ob->data;
967 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
968 else if (around == V3D_AROUND_CENTER_MEAN) { BKE_mball_center_median(mb, cent); }
969 else { BKE_mball_center_bounds(mb, cent); }
971 negate_v3_v3(cent_neg, cent);
972 BKE_mball_translate(mb, cent_neg);
975 mb->id.tag |= LIB_TAG_DOIT;
976 do_inverse_offset = true;
979 if (centermode == GEOMETRY_TO_ORIGIN) {
980 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
985 else if (ob->type == OB_LATTICE) {
986 Lattice *lt = ob->data;
988 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
989 else if (around == V3D_AROUND_CENTER_MEAN) { BKE_lattice_center_median(lt, cent); }
990 else { BKE_lattice_center_bounds(lt, cent); }
992 negate_v3_v3(cent_neg, cent);
993 BKE_lattice_translate(lt, cent_neg, 1);
996 lt->id.tag |= LIB_TAG_DOIT;
997 do_inverse_offset = true;
1000 /* offset other selected objects */
1001 if (do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
1002 CollectionPointerLink *ctx_link_other;
1005 /* was the object data modified
1006 * note: the functions above must set 'cent' */
1008 /* convert the offset to parent space */
1009 BKE_object_to_mat4(ob, obmat);
1010 mul_v3_mat3_m4v3(centn, obmat, cent); /* omit translation part */
1012 add_v3_v3(ob->loc, centn);
1014 BKE_object_where_is_calc(scene, ob);
1015 if (ob->type == OB_ARMATURE) {
1016 BKE_pose_where_is(scene, ob); /* needed for bone parents */
1019 ignore_parent_tx(bmain, scene, ob);
1022 //CTX_DATA_BEGIN (C, Object *, ob_other, selected_editable_objects)
1025 /* use existing context looper */
1026 for (ctx_link_other = ctx_data_list.first;
1028 ctx_link_other = ctx_link_other->next)
1030 Object *ob_other = ctx_link_other->ptr.data;
1032 if ((ob_other->flag & OB_DONE) == 0 &&
1033 ((ob->data && (ob->data == ob_other->data)) ||
1034 (ob->dup_group == ob_other->dup_group &&
1035 (ob->transflag | ob_other->transflag) & OB_DUPLIGROUP)))
1037 ob_other->flag |= OB_DONE;
1038 DEG_id_tag_update(&ob_other->id, OB_RECALC_OB | OB_RECALC_DATA);
1040 mul_v3_mat3_m4v3(centn, ob_other->obmat, cent); /* omit translation part */
1041 add_v3_v3(ob_other->loc, centn);
1043 BKE_object_where_is_calc(scene, ob_other);
1044 if (ob_other->type == OB_ARMATURE) {
1045 BKE_pose_where_is(scene, ob_other); /* needed for bone parents */
1047 ignore_parent_tx(bmain, scene, ob_other);
1054 BLI_freelistN(&ctx_data_list);
1056 for (tob = bmain->object.first; tob; tob = tob->id.next) {
1057 if (tob->data && (((ID *)tob->data)->tag & LIB_TAG_DOIT)) {
1058 BKE_mesh_batch_cache_dirty(tob->data, BKE_MESH_BATCH_DIRTY_NOCHECK);
1059 DEG_id_tag_update(&tob->id, OB_RECALC_OB | OB_RECALC_DATA);
1064 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1067 /* Warn if any errors occurred */
1068 if (tot_lib_error + tot_multiuser_arm_error) {
1069 BKE_reportf(op->reports, RPT_WARNING, "%i object(s) not centered, %i changed:", tot_lib_error + tot_multiuser_arm_error, tot_change);
1071 BKE_reportf(op->reports, RPT_WARNING, "|%i linked library object(s)", tot_lib_error);
1072 if (tot_multiuser_arm_error)
1073 BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)", tot_multiuser_arm_error);
1076 return OPERATOR_FINISHED;
1079 void OBJECT_OT_origin_set(wmOperatorType *ot)
1081 static EnumPropertyItem prop_set_center_types[] = {
1082 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
1083 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry",
1084 "Move object origin to center of object geometry"},
1085 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor",
1086 "Move object origin to position of the 3D cursor"},
1087 {ORIGIN_TO_CENTER_OF_MASS, "ORIGIN_CENTER_OF_MASS", 0, "Origin to Center of Mass",
1088 "Move object origin to the object center of mass (assuming uniform density)"},
1089 {0, NULL, 0, NULL, NULL}
1092 static EnumPropertyItem prop_set_bounds_types[] = {
1093 {V3D_AROUND_CENTER_MEAN, "MEDIAN", 0, "Median Center", ""},
1094 {V3D_AROUND_CENTER_BOUNDS, "BOUNDS", 0, "Bounds Center", ""},
1095 {0, NULL, 0, NULL, NULL}
1099 ot->name = "Set Origin";
1100 ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3D cursor";
1101 ot->idname = "OBJECT_OT_origin_set";
1104 ot->invoke = WM_menu_invoke;
1105 ot->exec = object_origin_set_exec;
1107 ot->poll = ED_operator_scene_editable;
1110 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1112 ot->prop = RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
1113 RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_AROUND_CENTER_MEAN, "Center", "");