Merge branch 'master' into blender2.8
[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(Main *bmain, Scene *scene, Object *ob)
404 {
405         Object workob;
406         Object *ob_child;
407         
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);
414                 }
415         }
416 }
417
418 static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_loc, bool apply_rot, bool apply_scale)
419 {
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;
423         bool changed = true;
424         
425         /* first check if we can execute */
426         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
427         {
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);
434                                 changed = false;
435                         }
436
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);
441                                 changed = false;
442                         }
443                 }
444
445                 if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
446                         ID *obdata = ob->data;
447                         Curve *cu;
448
449                         cu = ob->data;
450
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);
455                                 changed = false;
456                         }
457                         if (cu->key) {
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);
461                                 changed = false;
462                         }
463                 }
464
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\"",
469                                             ob->id.name + 2);
470                                 changed = false;
471                         }
472                 }
473         }
474         CTX_DATA_END;
475         
476         if (!changed)
477                 return OPERATOR_CANCELLED;
478
479         changed = false;
480
481         /* now execute */
482         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
483         {
484
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];
492
493                         /* simple rotation matrix */
494                         BKE_object_rot_to_mat3(ob, rsmat, true);
495
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);
501                 }
502                 else
503                         unit_m3(rsmat);
504
505                 copy_m4_m3(mat, rsmat);
506
507                 /* calculate translation */
508                 if (apply_loc) {
509                         copy_v3_v3(mat[3], ob->loc);
510
511                         if (!(apply_scale && apply_rot)) {
512                                 float tmat[3][3];
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]);
518                         }
519                 }
520
521                 /* apply to object data */
522                 if (ob->type == OB_MESH) {
523                         Mesh *me = ob->data;
524
525                         if (apply_scale)
526                                 multiresModifier_scale_disp(scene, ob);
527                         
528                         /* adjust data */
529                         BKE_mesh_transform(me, mat, true);
530                         
531                         /* update normals */
532                         BKE_mesh_calc_normals(me);
533                 }
534                 else if (ob->type == OB_ARMATURE) {
535                         ED_armature_apply_transform(ob, mat);
536                 }
537                 else if (ob->type == OB_LATTICE) {
538                         Lattice *lt = ob->data;
539
540                         BKE_lattice_transform(lt, mat, true);
541                 }
542                 else if (ob->type == OB_MBALL) {
543                         MetaBall *mb = ob->data;
544                         BKE_mball_transform(mb, mat);
545                 }
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);
550                 }
551                 else if (ob->type == OB_FONT) {
552                         Curve *cu = ob->data;
553                         int i;
554
555                         scale = mat3_to_scale(rsmat);
556
557                         for (i = 0; i < cu->totbox; i++) {
558                                 TextBox *tb = &cu->tb[i];
559                                 tb->x *= scale;
560                                 tb->y *= scale;
561                                 tb->w *= scale;
562                                 tb->h *= scale;
563                         }
564
565                         cu->fsize *= scale;
566                 }
567                 else if (ob->type == OB_CAMERA) {
568                         MovieClip *clip = BKE_object_movieclip_get(scene, ob, false);
569
570                         /* applying scale on camera actually scales clip's reconstruction.
571                          * of there's clip assigned to camera nothing to do actually.
572                          */
573                         if (!clip)
574                                 continue;
575
576                         if (apply_scale)
577                                 BKE_tracking_reconstruction_scale(&clip->tracking, ob->size);
578                 }
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.
583                          *
584                          * Core Assumptions:
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.
590                          */
591
592                         if ((apply_loc == false) &&
593                             (apply_rot == false) &&
594                             (apply_scale == true))
595                         {
596                                 float max_scale = max_fff(fabsf(ob->size[0]), fabsf(ob->size[1]), fabsf(ob->size[2]));
597                                 ob->empty_drawsize *= max_scale;
598                         }
599                 }
600                 else {
601                         continue;
602                 }
603
604                 if (apply_loc)
605                         zero_v3(ob->loc);
606                 if (apply_scale)
607                         ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
608                 if (apply_rot) {
609                         zero_v3(ob->rot);
610                         unit_qt(ob->quat);
611                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
612                 }
613
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 */
617                 }
618
619                 ignore_parent_tx(bmain, scene, ob);
620
621                 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
622
623                 changed = true;
624         }
625         CTX_DATA_END;
626
627         if (!changed) {
628                 BKE_report(reports, RPT_WARNING, "Objects have no data to transform");
629                 return OPERATOR_CANCELLED;
630         }
631
632         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
633         return OPERATOR_FINISHED;
634 }
635
636 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
637 {
638         Scene *scene = CTX_data_scene(C);
639         bool changed = false;
640         
641         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
642         {
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);
646
647                 /* update for any children that may get moved */
648                 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
649         
650                 changed = true;
651         }
652         CTX_DATA_END;
653
654         if (!changed)
655                 return OPERATOR_CANCELLED;
656
657         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
658         return OPERATOR_FINISHED;
659 }
660
661 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
662 {
663         /* identifiers */
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";
667         
668         /* api callbacks */
669         ot->exec = visual_transform_apply_exec;
670         ot->poll = ED_operator_scene_editable;
671         
672         /* flags */
673         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
674 }
675
676 static int object_transform_apply_exec(bContext *C, wmOperator *op)
677 {
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");
681
682         if (loc || rot || sca) {
683                 return apply_objects_internal(C, op->reports, loc, rot, sca);
684         }
685         else {
686                 /* allow for redo */
687                 return OPERATOR_FINISHED;
688         }
689 }
690
691 void OBJECT_OT_transform_apply(wmOperatorType *ot)
692 {
693         /* identifiers */
694         ot->name = "Apply Object Transform";
695         ot->description = "Apply the object's transformation to its data";
696         ot->idname = "OBJECT_OT_transform_apply";
697
698         /* api callbacks */
699         ot->exec = object_transform_apply_exec;
700         ot->poll = ED_operator_objectmode;
701
702         /* flags */
703         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
704
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", "");
708 }
709
710 /********************* Set Object Center ************************/
711
712 enum {
713         GEOMETRY_TO_ORIGIN = 0,
714         ORIGIN_TO_GEOMETRY,
715         ORIGIN_TO_CURSOR,
716         ORIGIN_TO_CENTER_OF_MASS
717 };
718
719 static int object_origin_set_exec(bContext *C, wmOperator *op)
720 {
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);
725         Object *tob;
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 */
729
730         ListBase ctx_data_list;
731         CollectionPointerLink *ctx_ob;
732         CollectionPointerLink *ctx_ob_act = NULL;
733
734         /* keep track of what is changed */
735         int tot_change = 0, tot_lib_error = 0, tot_multiuser_arm_error = 0;
736
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;
740         }
741         else {
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;
747         }
748
749         zero_v3(cent);
750
751         if (obedit) {
752                 if (obedit->type == OB_MESH) {
753                         Mesh *me = obedit->data;
754                         BMEditMesh *em = me->edit_btmesh;
755                         BMVert *eve;
756                         BMIter iter;
757                         
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);
762                         }
763                         else {
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);
769                                                 }
770                                         }
771                                 }
772                                 else {
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);
777                                         }
778                                         mid_v3_v3v3(cent, min, max);
779                                 }
780                         }
781                         
782                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
783                                 sub_v3_v3(eve->co, cent);
784                         }
785
786                         EDBM_mesh_normals_update(em);
787                         tot_change++;
788                         DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
789                 }
790         }
791
792         CTX_data_selected_editable_objects(C, &ctx_data_list);
793
794         /* reset flags */
795         for (ctx_ob = ctx_data_list.first;
796              ctx_ob;
797              ctx_ob = ctx_ob->next)
798         {
799                 Object *ob = ctx_ob->ptr.data;
800                 ob->flag &= ~OB_DONE;
801
802                 /* move active first */
803                 if (ob == obact) {
804                         ctx_ob_act = ctx_ob;
805                 }
806         }
807
808         if (ctx_ob_act) {
809                 BLI_listbase_rotate_first(&ctx_data_list, (LinkData *)ctx_ob_act);
810         }
811
812         for (tob = bmain->object.first; tob; tob = tob->id.next) {
813                 if (tob->data)
814                         ((ID *)tob->data)->tag &= ~LIB_TAG_DOIT;
815                 if (tob->dup_group)
816                         ((ID *)tob->dup_group)->tag &= ~LIB_TAG_DOIT;
817         }
818
819         for (ctx_ob = ctx_data_list.first;
820              ctx_ob;
821              ctx_ob = ctx_ob->next)
822         {
823                 Object *ob = ctx_ob->ptr.data;
824
825                 if ((ob->flag & OB_DONE) == 0) {
826                         bool do_inverse_offset = false;
827                         ob->flag |= OB_DONE;
828
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);
833                         }
834                         
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)) {
839                                                 tot_lib_error++;
840                                         }
841                                         else {
842                                                 if (centermode == ORIGIN_TO_CURSOR) {
843                                                         /* done */
844                                                 }
845                                                 else {
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);
853                                                 }
854                                                 
855                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
856
857                                                 tot_change++;
858                                                 ob->dup_group->id.tag |= LIB_TAG_DOIT;
859                                                 do_inverse_offset = true;
860                                         }
861                                 }
862                         }
863                         else if (ID_IS_LINKED_DATABLOCK(ob->data)) {
864                                 tot_lib_error++;
865                         }
866
867                         if (obedit == NULL && ob->type == OB_MESH) {
868                                 Mesh *me = ob->data;
869
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); }
874
875                                 negate_v3_v3(cent_neg, cent);
876                                 BKE_mesh_translate(me, cent_neg, 1);
877
878                                 tot_change++;
879                                 me->id.tag |= LIB_TAG_DOIT;
880                                 do_inverse_offset = true;
881                         }
882                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
883                                 Curve *cu = ob->data;
884
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); }
888
889                                 /* don't allow Z change if curve is 2D */
890                                 if ((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
891                                         cent[2] = 0.0;
892
893                                 negate_v3_v3(cent_neg, cent);
894                                 BKE_curve_translate(cu, cent_neg, 1);
895
896                                 tot_change++;
897                                 cu->id.tag |= LIB_TAG_DOIT;
898                                 do_inverse_offset = true;
899
900                                 if (obedit) {
901                                         if (centermode == GEOMETRY_TO_ORIGIN) {
902                                                 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
903                                         }
904                                         break;
905                                 }
906                         }
907                         else if (ob->type == OB_FONT) {
908                                 /* get from bb */
909
910                                 Curve *cu = ob->data;
911
912                                 if (ob->bb == NULL && (centermode != ORIGIN_TO_CURSOR)) {
913                                         /* do nothing*/
914                                 }
915                                 else {
916                                         if (centermode == ORIGIN_TO_CURSOR) {
917                                                 /* done */
918                                         }
919                                         else {
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]);
923                                         }
924
925                                         cent[2] = 0.0f;
926
927                                         cu->xof = cu->xof - cent[0];
928                                         cu->yof = cu->yof - cent[1];
929
930                                         tot_change++;
931                                         cu->id.tag |= LIB_TAG_DOIT;
932                                         do_inverse_offset = true;
933                                 }
934                         }
935                         else if (ob->type == OB_ARMATURE) {
936                                 bArmature *arm = ob->data;
937
938                                 if (ID_REAL_USERS(arm) > 1) {
939 #if 0
940                                         BKE_report(op->reports, RPT_ERROR, "Cannot apply to a multi user armature");
941                                         return;
942 #endif
943                                         tot_multiuser_arm_error++;
944                                 }
945                                 else {
946                                         /* Function to recenter armatures in editarmature.c
947                                          * Bone + object locations are handled there.
948                                          */
949                                         ED_armature_origin_set(scene, ob, cursor, centermode, around);
950
951                                         tot_change++;
952                                         arm->id.tag |= LIB_TAG_DOIT;
953                                         /* do_inverse_offset = true; */ /* docenter_armature() handles this */
954
955                                         BKE_object_where_is_calc(scene, ob);
956                                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
957
958                                         ignore_parent_tx(bmain, scene, ob);
959
960                                         if (obedit)
961                                                 break;
962                                 }
963                         }
964                         else if (ob->type == OB_MBALL) {
965                                 MetaBall *mb = ob->data;
966
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); }
970
971                                 negate_v3_v3(cent_neg, cent);
972                                 BKE_mball_translate(mb, cent_neg);
973
974                                 tot_change++;
975                                 mb->id.tag |= LIB_TAG_DOIT;
976                                 do_inverse_offset = true;
977
978                                 if (obedit) {
979                                         if (centermode == GEOMETRY_TO_ORIGIN) {
980                                                 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
981                                         }
982                                         break;
983                                 }
984                         }
985                         else if (ob->type == OB_LATTICE) {
986                                 Lattice *lt = ob->data;
987
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); }
991
992                                 negate_v3_v3(cent_neg, cent);
993                                 BKE_lattice_translate(lt, cent_neg, 1);
994
995                                 tot_change++;
996                                 lt->id.tag |= LIB_TAG_DOIT;
997                                 do_inverse_offset = true;
998                         }
999
1000                         /* offset other selected objects */
1001                         if (do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
1002                                 CollectionPointerLink *ctx_link_other;
1003                                 float obmat[4][4];
1004
1005                                 /* was the object data modified
1006                                  * note: the functions above must set 'cent' */
1007
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 */
1011
1012                                 add_v3_v3(ob->loc, centn);
1013
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 */
1017                                 }
1018
1019                                 ignore_parent_tx(bmain, scene, ob);
1020                                 
1021                                 /* other users? */
1022                                 //CTX_DATA_BEGIN (C, Object *, ob_other, selected_editable_objects)
1023                                 //{
1024
1025                                 /* use existing context looper */
1026                                 for (ctx_link_other = ctx_data_list.first;
1027                                      ctx_link_other;
1028                                      ctx_link_other = ctx_link_other->next)
1029                                 {
1030                                         Object *ob_other = ctx_link_other->ptr.data;
1031
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)))
1036                                         {
1037                                                 ob_other->flag |= OB_DONE;
1038                                                 DEG_id_tag_update(&ob_other->id, OB_RECALC_OB | OB_RECALC_DATA);
1039
1040                                                 mul_v3_mat3_m4v3(centn, ob_other->obmat, cent); /* omit translation part */
1041                                                 add_v3_v3(ob_other->loc, centn);
1042
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 */
1046                                                 }
1047                                                 ignore_parent_tx(bmain, scene, ob_other);
1048                                         }
1049                                 }
1050                                 //CTX_DATA_END;
1051                         }
1052                 }
1053         }
1054         BLI_freelistN(&ctx_data_list);
1055
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);
1060                 }
1061         }
1062
1063         if (tot_change) {
1064                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1065         }
1066
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);
1070                 if (tot_lib_error)
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);
1074         }
1075
1076         return OPERATOR_FINISHED;
1077 }
1078
1079 void OBJECT_OT_origin_set(wmOperatorType *ot)
1080 {
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}
1090         };
1091         
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}
1096         };
1097         
1098         /* identifiers */
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";
1102         
1103         /* api callbacks */
1104         ot->invoke = WM_menu_invoke;
1105         ot->exec = object_origin_set_exec;
1106         
1107         ot->poll = ED_operator_scene_editable;
1108         
1109         /* flags */
1110         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1111         
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", "");
1114 }