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