Pass EvaluationContext argument everywhere
[blender.git] / source / blender / editors / object / object_transform.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_transform.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
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"
42
43 #include "BLI_math.h"
44 #include "BLI_listbase.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_context.h"
48 #include "BKE_curve.h"
49 #include "BKE_main.h"
50 #include "BKE_idcode.h"
51 #include "BKE_mball.h"
52 #include "BKE_mesh.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"
60
61 #include "DEG_depsgraph.h"
62
63 #include "RNA_define.h"
64 #include "RNA_access.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "ED_armature.h"
70 #include "ED_keyframing.h"
71 #include "ED_mesh.h"
72 #include "ED_screen.h"
73 #include "ED_view3d.h"
74
75 #include "object_intern.h"
76
77 /*************************** Clear Transformation ****************************/
78
79 /* clear location of object */
80 static void object_clear_loc(Object *ob, const bool clear_delta)
81 {
82         /* clear location if not locked */
83         if ((ob->protectflag & OB_LOCK_LOCX) == 0) {
84                 ob->loc[0] = 0.0f;
85                 if (clear_delta) ob->dloc[0] = 0.0f;
86         }
87         if ((ob->protectflag & OB_LOCK_LOCY) == 0) {
88                 ob->loc[1] = 0.0f;
89                 if (clear_delta) ob->dloc[1] = 0.0f;
90         }
91         if ((ob->protectflag & OB_LOCK_LOCZ) == 0) {
92                 ob->loc[2] = 0.0f;
93                 if (clear_delta) ob->dloc[2] = 0.0f;
94         }
95 }
96
97 /* clear rotation of object */
98 static void object_clear_rot(Object *ob, const bool clear_delta)
99 {
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) {
106                                         ob->rotAngle = 0.0f;
107                                         if (clear_delta) ob->drotAngle = 0.0f;
108                                 }
109                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0) {
110                                         ob->rotAxis[0] = 0.0f;
111                                         if (clear_delta) ob->drotAxis[0] = 0.0f;
112                                 }
113                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0) {
114                                         ob->rotAxis[1] = 0.0f;
115                                         if (clear_delta) ob->drotAxis[1] = 0.0f;
116                                 }
117                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0) {
118                                         ob->rotAxis[2] = 0.0f;
119                                         if (clear_delta) ob->drotAxis[2] = 0.0f;
120                                 }
121                                         
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;
127                         }
128                         else if (ob->rotmode == ROT_MODE_QUAT) {
129                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0) {
130                                         ob->quat[0] = 1.0f;
131                                         if (clear_delta) ob->dquat[0] = 1.0f;
132                                 }
133                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0) {
134                                         ob->quat[1] = 0.0f;
135                                         if (clear_delta) ob->dquat[1] = 0.0f;
136                                 }
137                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0) {
138                                         ob->quat[2] = 0.0f;
139                                         if (clear_delta) ob->dquat[2] = 0.0f;
140                                 }
141                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0) {
142                                         ob->quat[3] = 0.0f;
143                                         if (clear_delta) ob->dquat[3] = 0.0f;
144                                 }
145                                 /* TODO: does this quat need normalizing now? */
146                         }
147                         else {
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) {
150                                         ob->rot[0] = 0.0f;
151                                         if (clear_delta) ob->drot[0] = 0.0f;
152                                 }
153                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0) {
154                                         ob->rot[1] = 0.0f;
155                                         if (clear_delta) ob->drot[1] = 0.0f;
156                                 }
157                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0) {
158                                         ob->rot[2] = 0.0f;
159                                         if (clear_delta) ob->drot[2] = 0.0f;
160                                 }
161                         }
162                 }
163                 else {
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};
167                         
168                         if (ob->rotmode == ROT_MODE_QUAT) {
169                                 copy_qt_qt(quat1, ob->quat);
170                                 quat_to_eul(oldeul, ob->quat);
171                         }
172                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
173                                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, ob->rotAxis, ob->rotAngle);
174                         }
175                         else {
176                                 copy_v3_v3(oldeul, ob->rot);
177                         }
178                         
179                         eul[0] = eul[1] = eul[2] = 0.0f;
180                         
181                         if (ob->protectflag & OB_LOCK_ROTX)
182                                 eul[0] = oldeul[0];
183                         if (ob->protectflag & OB_LOCK_ROTY)
184                                 eul[1] = oldeul[1];
185                         if (ob->protectflag & OB_LOCK_ROTZ)
186                                 eul[2] = oldeul[2];
187                         
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);
193                                 }
194                         }
195                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
196                                 eulO_to_axis_angle(ob->rotAxis, &ob->rotAngle, eul, EULER_ORDER_DEFAULT);
197                         }
198                         else {
199                                 copy_v3_v3(ob->rot, eul);
200                         }
201                 }
202         }                        // Duplicated in source/blender/editors/armature/editarmature.c
203         else {
204                 if (ob->rotmode == ROT_MODE_QUAT) {
205                         unit_qt(ob->quat);
206                         if (clear_delta) unit_qt(ob->dquat);
207                 }
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);
211                 }
212                 else {
213                         zero_v3(ob->rot);
214                         if (clear_delta) zero_v3(ob->drot);
215                 }
216         }
217 }
218
219 /* clear scale of object */
220 static void object_clear_scale(Object *ob, const bool clear_delta)
221 {
222         /* clear scale factors which are not locked */
223         if ((ob->protectflag & OB_LOCK_SCALEX) == 0) {
224                 ob->size[0] = 1.0f;
225                 if (clear_delta) ob->dscale[0] = 1.0f;
226         }
227         if ((ob->protectflag & OB_LOCK_SCALEY) == 0) {
228                 ob->size[1] = 1.0f;
229                 if (clear_delta) ob->dscale[1] = 1.0f;
230         }
231         if ((ob->protectflag & OB_LOCK_SCALEZ) == 0) {
232                 ob->size[2] = 1.0f;
233                 if (clear_delta) ob->dscale[2] = 1.0f;
234         }
235 }
236
237 /* --------------- */
238
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[])
243 {
244         Scene *scene = CTX_data_scene(C);
245         KeyingSet *ks;
246         const bool clear_delta = RNA_boolean_get(op->ptr, "clear_delta");
247         
248         /* sanity checks */
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;
252         }
253         
254         /* get KeyingSet to use */
255         ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
256         
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)
259          */
260         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
261         {
262                 if (!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
263                         /* run provided clearing function */
264                         clear_func(ob, clear_delta);
265                         
266                         ED_autokeyframe_object(C, scene, ob, ks);
267                         
268                         /* tag for updates */
269                         DEG_id_tag_update(&ob->id, OB_RECALC_OB);
270                 }
271         }
272         CTX_DATA_END;
273         
274         /* this is needed so children are also updated */
275         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
276
277         return OPERATOR_FINISHED;
278 }
279
280 /* --------------- */
281
282
283 static int object_location_clear_exec(bContext *C, wmOperator *op)
284 {
285         return object_clear_transform_generic_exec(C, op, object_clear_loc, ANIM_KS_LOCATION_ID);
286 }
287
288 void OBJECT_OT_location_clear(wmOperatorType *ot)
289 {
290         /* identifiers */
291         ot->name = "Clear Location";
292         ot->description = "Clear the object's location";
293         ot->idname = "OBJECT_OT_location_clear";
294         
295         /* api callbacks */
296         ot->exec = object_location_clear_exec;
297         ot->poll = ED_operator_scene_editable;
298         
299         /* flags */
300         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
301         
302         
303         /* properties */
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");
306 }
307
308 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
309 {
310         return object_clear_transform_generic_exec(C, op, object_clear_rot, ANIM_KS_ROTATION_ID);
311 }
312
313 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
314 {
315         /* identifiers */
316         ot->name = "Clear Rotation";
317         ot->description = "Clear the object's rotation";
318         ot->idname = "OBJECT_OT_rotation_clear";
319         
320         /* api callbacks */
321         ot->exec = object_rotation_clear_exec;
322         ot->poll = ED_operator_scene_editable;
323         
324         /* flags */
325         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
326         
327         /* properties */
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");
330 }
331
332 static int object_scale_clear_exec(bContext *C, wmOperator *op)
333 {
334         return object_clear_transform_generic_exec(C, op, object_clear_scale, ANIM_KS_SCALING_ID);
335 }
336
337 void OBJECT_OT_scale_clear(wmOperatorType *ot)
338 {
339         /* identifiers */
340         ot->name = "Clear Scale";
341         ot->description = "Clear the object's scale";
342         ot->idname = "OBJECT_OT_scale_clear";
343         
344         /* api callbacks */
345         ot->exec = object_scale_clear_exec;
346         ot->poll = ED_operator_scene_editable;
347         
348         /* flags */
349         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
350         
351         /* properties */
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");
354 }
355
356 /* --------------- */
357
358 static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
359 {
360         float *v1, *v3;
361         float mat[3][3];
362
363         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
364         {
365                 if (ob->parent) {
366                         /* vectors pointed to by v1 and v3 will get modified */
367                         v1 = ob->loc;
368                         v3 = ob->parentinv[3];
369                         
370                         copy_m3_m4(mat, ob->parentinv);
371                         negate_v3_v3(v3, v1);
372                         mul_m3_v3(mat, v3);
373                 }
374
375                 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
376         }
377         CTX_DATA_END;
378
379         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
380         
381         return OPERATOR_FINISHED;
382 }
383
384 void OBJECT_OT_origin_clear(wmOperatorType *ot)
385 {
386         /* identifiers */
387         ot->name = "Clear Origin";
388         ot->description = "Clear the object's origin";
389         ot->idname = "OBJECT_OT_origin_clear";
390         
391         /* api callbacks */
392         ot->exec = object_origin_clear_exec;
393         ot->poll = ED_operator_scene_editable;
394         
395         /* flags */
396         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
397 }
398
399 /*************************** Apply Transformation ****************************/
400
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(const bContext *C, Main *bmain, Scene *scene, Object *ob)
404 {
405         Object workob;
406         Object *ob_child;
407         EvaluationContext eval_ctx;
408
409         CTX_data_eval_ctx(C, &eval_ctx);
410         
411         /* a change was made, adjust the children to compensate */
412         for (ob_child = bmain->object.first; ob_child; ob_child = ob_child->id.next) {
413                 if (ob_child->parent == ob) {
414                         BKE_object_apply_mat4(ob_child, ob_child->obmat, true, false);
415                         BKE_object_workob_calc_parent(&eval_ctx, scene, ob_child, &workob);
416                         invert_m4_m4(ob_child->parentinv, workob.obmat);
417                 }
418         }
419 }
420
421 static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_loc, bool apply_rot, bool apply_scale)
422 {
423         Main *bmain = CTX_data_main(C);
424         Scene *scene = CTX_data_scene(C);
425         EvaluationContext eval_ctx;
426         float rsmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
427         bool changed = true;
428
429         CTX_data_eval_ctx(C, &eval_ctx);
430         
431         /* first check if we can execute */
432         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
433         {
434                 if (ELEM(ob->type, OB_MESH, OB_ARMATURE, OB_LATTICE, OB_MBALL, OB_CURVE, OB_SURF, OB_FONT)) {
435                         ID *obdata = ob->data;
436                         if (ID_REAL_USERS(obdata) > 1) {
437                                 BKE_reportf(reports, RPT_ERROR,
438                                             "Cannot apply to a multi user: Object \"%s\", %s \"%s\", aborting",
439                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
440                                 changed = false;
441                         }
442
443                         if (ID_IS_LINKED_DATABLOCK(obdata)) {
444                                 BKE_reportf(reports, RPT_ERROR,
445                                             "Cannot apply to library data: Object \"%s\", %s \"%s\", aborting",
446                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
447                                 changed = false;
448                         }
449                 }
450
451                 if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
452                         ID *obdata = ob->data;
453                         Curve *cu;
454
455                         cu = ob->data;
456
457                         if (((ob->type == OB_CURVE) && !(cu->flag & CU_3D)) && (apply_rot || apply_loc)) {
458                                 BKE_reportf(reports, RPT_ERROR,
459                                             "Rotation/Location can't apply to a 2D curve: Object \"%s\", %s \"%s\", aborting",
460                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
461                                 changed = false;
462                         }
463                         if (cu->key) {
464                                 BKE_reportf(reports, RPT_ERROR,
465                                             "Can't apply to a curve with shape-keys: Object \"%s\", %s \"%s\", aborting",
466                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
467                                 changed = false;
468                         }
469                 }
470
471                 if (ob->type == OB_FONT) {
472                         if (apply_rot || apply_loc) {
473                                 BKE_reportf(reports, RPT_ERROR,
474                                             "Font's can only have scale applied: \"%s\"",
475                                             ob->id.name + 2);
476                                 changed = false;
477                         }
478                 }
479         }
480         CTX_DATA_END;
481         
482         if (!changed)
483                 return OPERATOR_CANCELLED;
484
485         changed = false;
486
487         /* now execute */
488         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
489         {
490
491                 /* calculate rotation/scale matrix */
492                 if (apply_scale && apply_rot)
493                         BKE_object_to_mat3(ob, rsmat);
494                 else if (apply_scale)
495                         BKE_object_scale_to_mat3(ob, rsmat);
496                 else if (apply_rot) {
497                         float tmat[3][3], timat[3][3];
498
499                         /* simple rotation matrix */
500                         BKE_object_rot_to_mat3(ob, rsmat, true);
501
502                         /* correct for scale, note mul_m3_m3m3 has swapped args! */
503                         BKE_object_scale_to_mat3(ob, tmat);
504                         invert_m3_m3(timat, tmat);
505                         mul_m3_m3m3(rsmat, timat, rsmat);
506                         mul_m3_m3m3(rsmat, rsmat, tmat);
507                 }
508                 else
509                         unit_m3(rsmat);
510
511                 copy_m4_m3(mat, rsmat);
512
513                 /* calculate translation */
514                 if (apply_loc) {
515                         copy_v3_v3(mat[3], ob->loc);
516
517                         if (!(apply_scale && apply_rot)) {
518                                 float tmat[3][3];
519                                 /* correct for scale and rotation that is still applied */
520                                 BKE_object_to_mat3(ob, obmat);
521                                 invert_m3_m3(iobmat, obmat);
522                                 mul_m3_m3m3(tmat, rsmat, iobmat);
523                                 mul_m3_v3(tmat, mat[3]);
524                         }
525                 }
526
527                 /* apply to object data */
528                 if (ob->type == OB_MESH) {
529                         Mesh *me = ob->data;
530
531                         if (apply_scale)
532                                 multiresModifier_scale_disp(&eval_ctx, scene, ob);
533                         
534                         /* adjust data */
535                         BKE_mesh_transform(me, mat, true);
536                         
537                         /* update normals */
538                         BKE_mesh_calc_normals(me);
539                 }
540                 else if (ob->type == OB_ARMATURE) {
541                         ED_armature_apply_transform(ob, mat);
542                 }
543                 else if (ob->type == OB_LATTICE) {
544                         Lattice *lt = ob->data;
545
546                         BKE_lattice_transform(lt, mat, true);
547                 }
548                 else if (ob->type == OB_MBALL) {
549                         MetaBall *mb = ob->data;
550                         BKE_mball_transform(mb, mat);
551                 }
552                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
553                         Curve *cu = ob->data;
554                         scale = mat3_to_scale(rsmat);
555                         BKE_curve_transform_ex(cu, mat, true, scale);
556                 }
557                 else if (ob->type == OB_FONT) {
558                         Curve *cu = ob->data;
559                         int i;
560
561                         scale = mat3_to_scale(rsmat);
562
563                         for (i = 0; i < cu->totbox; i++) {
564                                 TextBox *tb = &cu->tb[i];
565                                 tb->x *= scale;
566                                 tb->y *= scale;
567                                 tb->w *= scale;
568                                 tb->h *= scale;
569                         }
570
571                         cu->fsize *= scale;
572                 }
573                 else if (ob->type == OB_CAMERA) {
574                         MovieClip *clip = BKE_object_movieclip_get(scene, ob, false);
575
576                         /* applying scale on camera actually scales clip's reconstruction.
577                          * of there's clip assigned to camera nothing to do actually.
578                          */
579                         if (!clip)
580                                 continue;
581
582                         if (apply_scale)
583                                 BKE_tracking_reconstruction_scale(&clip->tracking, ob->size);
584                 }
585                 else if (ob->type == OB_EMPTY) {
586                         /* It's possible for empties too, even though they don't 
587                          * really have obdata, since we can simply apply the maximum
588                          * scaling to the empty's drawsize.
589                          *
590                          * Core Assumptions:
591                          * 1) Most scaled empties have uniform scaling 
592                          *    (i.e. for visibility reasons), AND/OR
593                          * 2) Preserving non-uniform scaling is not that important,
594                          *    and is something that many users would be willing to
595                          *    sacrifice for having an easy way to do this.
596                          */
597
598                         if ((apply_loc == false) &&
599                             (apply_rot == false) &&
600                             (apply_scale == true))
601                         {
602                                 float max_scale = max_fff(fabsf(ob->size[0]), fabsf(ob->size[1]), fabsf(ob->size[2]));
603                                 ob->empty_drawsize *= max_scale;
604                         }
605                 }
606                 else {
607                         continue;
608                 }
609
610                 if (apply_loc)
611                         zero_v3(ob->loc);
612                 if (apply_scale)
613                         ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
614                 if (apply_rot) {
615                         zero_v3(ob->rot);
616                         unit_qt(ob->quat);
617                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
618                 }
619
620                 BKE_object_where_is_calc(&eval_ctx, scene, ob);
621                 if (ob->type == OB_ARMATURE) {
622                         BKE_pose_where_is(&eval_ctx, scene, ob); /* needed for bone parents */
623                 }
624
625                 ignore_parent_tx(C, bmain, scene, ob);
626
627                 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
628
629                 changed = true;
630         }
631         CTX_DATA_END;
632
633         if (!changed) {
634                 BKE_report(reports, RPT_WARNING, "Objects have no data to transform");
635                 return OPERATOR_CANCELLED;
636         }
637
638         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
639         return OPERATOR_FINISHED;
640 }
641
642 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
643 {
644         Scene *scene = CTX_data_scene(C);
645         EvaluationContext eval_ctx;
646         bool changed = false;
647         
648         CTX_data_eval_ctx(C, &eval_ctx);
649
650         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
651         {
652                 BKE_object_where_is_calc(&eval_ctx, scene, ob);
653                 BKE_object_apply_mat4(ob, ob->obmat, true, true);
654                 BKE_object_where_is_calc(&eval_ctx, scene, ob);
655
656                 /* update for any children that may get moved */
657                 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
658         
659                 changed = true;
660         }
661         CTX_DATA_END;
662
663         if (!changed)
664                 return OPERATOR_CANCELLED;
665
666         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
667         return OPERATOR_FINISHED;
668 }
669
670 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
671 {
672         /* identifiers */
673         ot->name = "Apply Visual Transform";
674         ot->description = "Apply the object's visual transformation to its data";
675         ot->idname = "OBJECT_OT_visual_transform_apply";
676         
677         /* api callbacks */
678         ot->exec = visual_transform_apply_exec;
679         ot->poll = ED_operator_scene_editable;
680         
681         /* flags */
682         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
683 }
684
685 static int object_transform_apply_exec(bContext *C, wmOperator *op)
686 {
687         const bool loc = RNA_boolean_get(op->ptr, "location");
688         const bool rot = RNA_boolean_get(op->ptr, "rotation");
689         const bool sca = RNA_boolean_get(op->ptr, "scale");
690
691         if (loc || rot || sca) {
692                 return apply_objects_internal(C, op->reports, loc, rot, sca);
693         }
694         else {
695                 /* allow for redo */
696                 return OPERATOR_FINISHED;
697         }
698 }
699
700 void OBJECT_OT_transform_apply(wmOperatorType *ot)
701 {
702         /* identifiers */
703         ot->name = "Apply Object Transform";
704         ot->description = "Apply the object's transformation to its data";
705         ot->idname = "OBJECT_OT_transform_apply";
706
707         /* api callbacks */
708         ot->exec = object_transform_apply_exec;
709         ot->poll = ED_operator_objectmode;
710
711         /* flags */
712         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
713
714         RNA_def_boolean(ot->srna, "location", 0, "Location", "");
715         RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
716         RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
717 }
718
719 /********************* Set Object Center ************************/
720
721 enum {
722         GEOMETRY_TO_ORIGIN = 0,
723         ORIGIN_TO_GEOMETRY,
724         ORIGIN_TO_CURSOR,
725         ORIGIN_TO_CENTER_OF_MASS
726 };
727
728 static int object_origin_set_exec(bContext *C, wmOperator *op)
729 {
730         Main *bmain = CTX_data_main(C);
731         Scene *scene = CTX_data_scene(C);
732         Object *obact = CTX_data_active_object(C);
733         Object *obedit = CTX_data_edit_object(C);
734         EvaluationContext eval_ctx;
735         Object *tob;
736         float cursor[3], cent[3], cent_neg[3], centn[3];
737         int centermode = RNA_enum_get(op->ptr, "type");
738         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
739
740         ListBase ctx_data_list;
741         CollectionPointerLink *ctx_ob;
742         CollectionPointerLink *ctx_ob_act = NULL;
743
744         CTX_data_eval_ctx(C, &eval_ctx);
745
746         /* keep track of what is changed */
747         int tot_change = 0, tot_lib_error = 0, tot_multiuser_arm_error = 0;
748
749         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
750                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
751                 return OPERATOR_CANCELLED;
752         }
753         else {
754                 /* get the view settings if 'around' isn't set and the view is available */
755                 View3D *v3d = CTX_wm_view3d(C);
756                 copy_v3_v3(cursor, ED_view3d_cursor3d_get(scene, v3d));
757                 if (v3d && !RNA_struct_property_is_set(op->ptr, "center"))
758                         around = v3d->around;
759         }
760
761         zero_v3(cent);
762
763         if (obedit) {
764                 if (obedit->type == OB_MESH) {
765                         Mesh *me = obedit->data;
766                         BMEditMesh *em = me->edit_btmesh;
767                         BMVert *eve;
768                         BMIter iter;
769                         
770                         if (centermode == ORIGIN_TO_CURSOR) {
771                                 copy_v3_v3(cent, cursor);
772                                 invert_m4_m4(obedit->imat, obedit->obmat);
773                                 mul_m4_v3(obedit->imat, cent);
774                         }
775                         else {
776                                 if (around == V3D_AROUND_CENTER_MEAN) {
777                                         if (em->bm->totvert) {
778                                                 const float total_div = 1.0f / (float)em->bm->totvert;
779                                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
780                                                         madd_v3_v3fl(cent, eve->co, total_div);
781                                                 }
782                                         }
783                                 }
784                                 else {
785                                         float min[3], max[3];
786                                         INIT_MINMAX(min, max);
787                                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
788                                                 minmax_v3v3_v3(min, max, eve->co);
789                                         }
790                                         mid_v3_v3v3(cent, min, max);
791                                 }
792                         }
793                         
794                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
795                                 sub_v3_v3(eve->co, cent);
796                         }
797
798                         EDBM_mesh_normals_update(em);
799                         tot_change++;
800                         DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
801                 }
802         }
803
804         CTX_data_selected_editable_objects(C, &ctx_data_list);
805
806         /* reset flags */
807         for (ctx_ob = ctx_data_list.first;
808              ctx_ob;
809              ctx_ob = ctx_ob->next)
810         {
811                 Object *ob = ctx_ob->ptr.data;
812                 ob->flag &= ~OB_DONE;
813
814                 /* move active first */
815                 if (ob == obact) {
816                         ctx_ob_act = ctx_ob;
817                 }
818         }
819
820         if (ctx_ob_act) {
821                 BLI_listbase_rotate_first(&ctx_data_list, (LinkData *)ctx_ob_act);
822         }
823
824         for (tob = bmain->object.first; tob; tob = tob->id.next) {
825                 if (tob->data)
826                         ((ID *)tob->data)->tag &= ~LIB_TAG_DOIT;
827                 if (tob->dup_group)
828                         ((ID *)tob->dup_group)->tag &= ~LIB_TAG_DOIT;
829         }
830
831         for (ctx_ob = ctx_data_list.first;
832              ctx_ob;
833              ctx_ob = ctx_ob->next)
834         {
835                 Object *ob = ctx_ob->ptr.data;
836
837                 if ((ob->flag & OB_DONE) == 0) {
838                         bool do_inverse_offset = false;
839                         ob->flag |= OB_DONE;
840
841                         if (centermode == ORIGIN_TO_CURSOR) {
842                                 copy_v3_v3(cent, cursor);
843                                 invert_m4_m4(ob->imat, ob->obmat);
844                                 mul_m4_v3(ob->imat, cent);
845                         }
846                         
847                         if (ob->data == NULL) {
848                                 /* special support for dupligroups */
849                                 if ((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.tag & LIB_TAG_DOIT) == 0) {
850                                         if (ID_IS_LINKED_DATABLOCK(ob->dup_group)) {
851                                                 tot_lib_error++;
852                                         }
853                                         else {
854                                                 if (centermode == ORIGIN_TO_CURSOR) {
855                                                         /* done */
856                                                 }
857                                                 else {
858                                                         float min[3], max[3];
859                                                         /* only bounds support */
860                                                         INIT_MINMAX(min, max);
861                                                         BKE_object_minmax_dupli(scene, ob, min, max, true);
862                                                         mid_v3_v3v3(cent, min, max);
863                                                         invert_m4_m4(ob->imat, ob->obmat);
864                                                         mul_m4_v3(ob->imat, cent);
865                                                 }
866                                                 
867                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
868
869                                                 tot_change++;
870                                                 ob->dup_group->id.tag |= LIB_TAG_DOIT;
871                                                 do_inverse_offset = true;
872                                         }
873                                 }
874                         }
875                         else if (ID_IS_LINKED_DATABLOCK(ob->data)) {
876                                 tot_lib_error++;
877                         }
878
879                         if (obedit == NULL && ob->type == OB_MESH) {
880                                 Mesh *me = ob->data;
881
882                                 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
883                                 else if (centermode == ORIGIN_TO_CENTER_OF_MASS)    { BKE_mesh_center_centroid(me, cent); }
884                                 else if (around == V3D_AROUND_CENTER_MEAN)          { BKE_mesh_center_median(me, cent); }
885                                 else                                                { BKE_mesh_center_bounds(me, cent); }
886
887                                 negate_v3_v3(cent_neg, cent);
888                                 BKE_mesh_translate(me, cent_neg, 1);
889
890                                 tot_change++;
891                                 me->id.tag |= LIB_TAG_DOIT;
892                                 do_inverse_offset = true;
893                         }
894                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
895                                 Curve *cu = ob->data;
896
897                                 if      (centermode == ORIGIN_TO_CURSOR)    { /* done */ }
898                                 else if (around == V3D_AROUND_CENTER_MEAN)  { BKE_curve_center_median(cu, cent); }
899                                 else                                        { BKE_curve_center_bounds(cu, cent); }
900
901                                 /* don't allow Z change if curve is 2D */
902                                 if ((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
903                                         cent[2] = 0.0;
904
905                                 negate_v3_v3(cent_neg, cent);
906                                 BKE_curve_translate(cu, cent_neg, 1);
907
908                                 tot_change++;
909                                 cu->id.tag |= LIB_TAG_DOIT;
910                                 do_inverse_offset = true;
911
912                                 if (obedit) {
913                                         if (centermode == GEOMETRY_TO_ORIGIN) {
914                                                 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
915                                         }
916                                         break;
917                                 }
918                         }
919                         else if (ob->type == OB_FONT) {
920                                 /* get from bb */
921
922                                 Curve *cu = ob->data;
923
924                                 if (ob->bb == NULL && (centermode != ORIGIN_TO_CURSOR)) {
925                                         /* do nothing*/
926                                 }
927                                 else {
928                                         if (centermode == ORIGIN_TO_CURSOR) {
929                                                 /* done */
930                                         }
931                                         else {
932                                                 /* extra 0.5 is the height o above line */
933                                                 cent[0] = 0.5f * (ob->bb->vec[4][0] + ob->bb->vec[0][0]);
934                                                 cent[1] = 0.5f * (ob->bb->vec[0][1] + ob->bb->vec[2][1]);
935                                         }
936
937                                         cent[2] = 0.0f;
938
939                                         cu->xof = cu->xof - cent[0];
940                                         cu->yof = cu->yof - cent[1];
941
942                                         tot_change++;
943                                         cu->id.tag |= LIB_TAG_DOIT;
944                                         do_inverse_offset = true;
945                                 }
946                         }
947                         else if (ob->type == OB_ARMATURE) {
948                                 bArmature *arm = ob->data;
949
950                                 if (ID_REAL_USERS(arm) > 1) {
951 #if 0
952                                         BKE_report(op->reports, RPT_ERROR, "Cannot apply to a multi user armature");
953                                         return;
954 #endif
955                                         tot_multiuser_arm_error++;
956                                 }
957                                 else {
958                                         /* Function to recenter armatures in editarmature.c
959                                          * Bone + object locations are handled there.
960                                          */
961                                         ED_armature_origin_set(scene, ob, cursor, centermode, around);
962
963                                         tot_change++;
964                                         arm->id.tag |= LIB_TAG_DOIT;
965                                         /* do_inverse_offset = true; */ /* docenter_armature() handles this */
966
967                                         BKE_object_where_is_calc(&eval_ctx, scene, ob);
968                                         BKE_pose_where_is(&eval_ctx, scene, ob); /* needed for bone parents */
969
970                                         ignore_parent_tx(C, bmain, scene, ob);
971
972                                         if (obedit)
973                                                 break;
974                                 }
975                         }
976                         else if (ob->type == OB_MBALL) {
977                                 MetaBall *mb = ob->data;
978
979                                 if      (centermode == ORIGIN_TO_CURSOR)    { /* done */ }
980                                 else if (around == V3D_AROUND_CENTER_MEAN)  { BKE_mball_center_median(mb, cent); }
981                                 else                                        { BKE_mball_center_bounds(mb, cent); }
982
983                                 negate_v3_v3(cent_neg, cent);
984                                 BKE_mball_translate(mb, cent_neg);
985
986                                 tot_change++;
987                                 mb->id.tag |= LIB_TAG_DOIT;
988                                 do_inverse_offset = true;
989
990                                 if (obedit) {
991                                         if (centermode == GEOMETRY_TO_ORIGIN) {
992                                                 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
993                                         }
994                                         break;
995                                 }
996                         }
997                         else if (ob->type == OB_LATTICE) {
998                                 Lattice *lt = ob->data;
999
1000                                 if      (centermode == ORIGIN_TO_CURSOR)    { /* done */ }
1001                                 else if (around == V3D_AROUND_CENTER_MEAN)  { BKE_lattice_center_median(lt, cent); }
1002                                 else                                        { BKE_lattice_center_bounds(lt, cent); }
1003
1004                                 negate_v3_v3(cent_neg, cent);
1005                                 BKE_lattice_translate(lt, cent_neg, 1);
1006
1007                                 tot_change++;
1008                                 lt->id.tag |= LIB_TAG_DOIT;
1009                                 do_inverse_offset = true;
1010                         }
1011
1012                         /* offset other selected objects */
1013                         if (do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
1014                                 CollectionPointerLink *ctx_link_other;
1015                                 float obmat[4][4];
1016
1017                                 /* was the object data modified
1018                                  * note: the functions above must set 'cent' */
1019
1020                                 /* convert the offset to parent space */
1021                                 BKE_object_to_mat4(ob, obmat);
1022                                 mul_v3_mat3_m4v3(centn, obmat, cent); /* omit translation part */
1023
1024                                 add_v3_v3(ob->loc, centn);
1025
1026                                 BKE_object_where_is_calc(&eval_ctx, scene, ob);
1027                                 if (ob->type == OB_ARMATURE) {
1028                                         BKE_pose_where_is(&eval_ctx, scene, ob); /* needed for bone parents */
1029                                 }
1030
1031                                 ignore_parent_tx(C, bmain, scene, ob);
1032                                 
1033                                 /* other users? */
1034                                 //CTX_DATA_BEGIN (C, Object *, ob_other, selected_editable_objects)
1035                                 //{
1036
1037                                 /* use existing context looper */
1038                                 for (ctx_link_other = ctx_data_list.first;
1039                                      ctx_link_other;
1040                                      ctx_link_other = ctx_link_other->next)
1041                                 {
1042                                         Object *ob_other = ctx_link_other->ptr.data;
1043
1044                                         if ((ob_other->flag & OB_DONE) == 0 &&
1045                                             ((ob->data && (ob->data == ob_other->data)) ||
1046                                              (ob->dup_group == ob_other->dup_group &&
1047                                               (ob->transflag | ob_other->transflag) & OB_DUPLIGROUP)))
1048                                         {
1049                                                 ob_other->flag |= OB_DONE;
1050                                                 DEG_id_tag_update(&ob_other->id, OB_RECALC_OB | OB_RECALC_DATA);
1051
1052                                                 mul_v3_mat3_m4v3(centn, ob_other->obmat, cent); /* omit translation part */
1053                                                 add_v3_v3(ob_other->loc, centn);
1054
1055                                                 BKE_object_where_is_calc(&eval_ctx, scene, ob_other);
1056                                                 if (ob_other->type == OB_ARMATURE) {
1057                                                         BKE_pose_where_is(&eval_ctx, scene, ob_other); /* needed for bone parents */
1058                                                 }
1059                                                 ignore_parent_tx(C, bmain, scene, ob_other);
1060                                         }
1061                                 }
1062                                 //CTX_DATA_END;
1063                         }
1064                 }
1065         }
1066         BLI_freelistN(&ctx_data_list);
1067
1068         for (tob = bmain->object.first; tob; tob = tob->id.next) {
1069                 if (tob->data && (((ID *)tob->data)->tag & LIB_TAG_DOIT)) {
1070                         BKE_mesh_batch_cache_dirty(tob->data, BKE_MESH_BATCH_DIRTY_NOCHECK);
1071                         DEG_id_tag_update(&tob->id, OB_RECALC_OB | OB_RECALC_DATA);
1072                 }
1073         }
1074
1075         if (tot_change) {
1076                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1077         }
1078
1079         /* Warn if any errors occurred */
1080         if (tot_lib_error + tot_multiuser_arm_error) {
1081                 BKE_reportf(op->reports, RPT_WARNING, "%i object(s) not centered, %i changed:", tot_lib_error + tot_multiuser_arm_error, tot_change);
1082                 if (tot_lib_error)
1083                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library object(s)", tot_lib_error);
1084                 if (tot_multiuser_arm_error)
1085                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)", tot_multiuser_arm_error);
1086         }
1087
1088         return OPERATOR_FINISHED;
1089 }
1090
1091 void OBJECT_OT_origin_set(wmOperatorType *ot)
1092 {
1093         static EnumPropertyItem prop_set_center_types[] = {
1094                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
1095                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry",
1096                                      "Move object origin to center of object geometry"},
1097                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor",
1098                                    "Move object origin to position of the 3D cursor"},
1099                 {ORIGIN_TO_CENTER_OF_MASS, "ORIGIN_CENTER_OF_MASS", 0, "Origin to Center of Mass",
1100                                            "Move object origin to the object center of mass (assuming uniform density)"},
1101                 {0, NULL, 0, NULL, NULL}
1102         };
1103         
1104         static EnumPropertyItem prop_set_bounds_types[] = {
1105                 {V3D_AROUND_CENTER_MEAN, "MEDIAN", 0, "Median Center", ""},
1106                 {V3D_AROUND_CENTER_BOUNDS, "BOUNDS", 0, "Bounds Center", ""},
1107                 {0, NULL, 0, NULL, NULL}
1108         };
1109         
1110         /* identifiers */
1111         ot->name = "Set Origin";
1112         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3D cursor";
1113         ot->idname = "OBJECT_OT_origin_set";
1114         
1115         /* api callbacks */
1116         ot->invoke = WM_menu_invoke;
1117         ot->exec = object_origin_set_exec;
1118         
1119         ot->poll = ED_operator_scene_editable;
1120         
1121         /* flags */
1122         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1123         
1124         ot->prop = RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
1125         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_AROUND_CENTER_MEAN, "Center", "");
1126 }