Style cleanup.
[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(
418         bContext *C, ReportList *reports,
419         bool apply_loc, bool apply_rot, bool apply_scale,
420         bool do_props)
421 {
422         Main *bmain = CTX_data_main(C);
423         Scene *scene = CTX_data_scene(C);
424         float rsmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
425         bool changed = true;
426         
427         /* first check if we can execute */
428         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
429         {
430                 if (ELEM(ob->type, OB_MESH, OB_ARMATURE, OB_LATTICE, OB_MBALL, OB_CURVE, OB_SURF, OB_FONT)) {
431                         ID *obdata = ob->data;
432                         if (ID_REAL_USERS(obdata) > 1) {
433                                 BKE_reportf(reports, RPT_ERROR,
434                                             "Cannot apply to a multi user: Object \"%s\", %s \"%s\", aborting",
435                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
436                                 changed = false;
437                         }
438
439                         if (ID_IS_LINKED(obdata)) {
440                                 BKE_reportf(reports, RPT_ERROR,
441                                             "Cannot apply to library data: Object \"%s\", %s \"%s\", aborting",
442                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
443                                 changed = false;
444                         }
445                 }
446
447                 if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
448                         ID *obdata = ob->data;
449                         Curve *cu;
450
451                         cu = ob->data;
452
453                         if (((ob->type == OB_CURVE) && !(cu->flag & CU_3D)) && (apply_rot || apply_loc)) {
454                                 BKE_reportf(reports, RPT_ERROR,
455                                             "Rotation/Location can't apply to a 2D curve: Object \"%s\", %s \"%s\", aborting",
456                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
457                                 changed = false;
458                         }
459                         if (cu->key) {
460                                 BKE_reportf(reports, RPT_ERROR,
461                                             "Can't apply to a curve with shape-keys: Object \"%s\", %s \"%s\", aborting",
462                                             ob->id.name + 2, BKE_idcode_to_name(GS(obdata->name)), obdata->name + 2);
463                                 changed = false;
464                         }
465                 }
466
467                 if (ob->type == OB_FONT) {
468                         if (apply_rot || apply_loc) {
469                                 BKE_reportf(reports, RPT_ERROR,
470                                             "Font's can only have scale applied: \"%s\"",
471                                             ob->id.name + 2);
472                                 changed = false;
473                         }
474                 }
475         }
476         CTX_DATA_END;
477         
478         if (!changed)
479                 return OPERATOR_CANCELLED;
480
481         changed = false;
482
483         /* now execute */
484         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
485         {
486
487                 /* calculate rotation/scale matrix */
488                 if (apply_scale && apply_rot)
489                         BKE_object_to_mat3(ob, rsmat);
490                 else if (apply_scale)
491                         BKE_object_scale_to_mat3(ob, rsmat);
492                 else if (apply_rot) {
493                         float tmat[3][3], timat[3][3];
494
495                         /* simple rotation matrix */
496                         BKE_object_rot_to_mat3(ob, rsmat, true);
497
498                         /* correct for scale, note mul_m3_m3m3 has swapped args! */
499                         BKE_object_scale_to_mat3(ob, tmat);
500                         invert_m3_m3(timat, tmat);
501                         mul_m3_m3m3(rsmat, timat, rsmat);
502                         mul_m3_m3m3(rsmat, rsmat, tmat);
503                 }
504                 else
505                         unit_m3(rsmat);
506
507                 copy_m4_m3(mat, rsmat);
508
509                 /* calculate translation */
510                 if (apply_loc) {
511                         copy_v3_v3(mat[3], ob->loc);
512
513                         if (!(apply_scale && apply_rot)) {
514                                 float tmat[3][3];
515                                 /* correct for scale and rotation that is still applied */
516                                 BKE_object_to_mat3(ob, obmat);
517                                 invert_m3_m3(iobmat, obmat);
518                                 mul_m3_m3m3(tmat, rsmat, iobmat);
519                                 mul_m3_v3(tmat, mat[3]);
520                         }
521                 }
522
523                 /* apply to object data */
524                 if (ob->type == OB_MESH) {
525                         Mesh *me = ob->data;
526
527                         if (apply_scale)
528                                 multiresModifier_scale_disp(scene, ob);
529                         
530                         /* adjust data */
531                         BKE_mesh_transform(me, mat, true);
532                         
533                         /* update normals */
534                         BKE_mesh_calc_normals(me);
535                 }
536                 else if (ob->type == OB_ARMATURE) {
537                         ED_armature_apply_transform(ob, mat, do_props);
538                 }
539                 else if (ob->type == OB_LATTICE) {
540                         Lattice *lt = ob->data;
541
542                         BKE_lattice_transform(lt, mat, true);
543                 }
544                 else if (ob->type == OB_MBALL) {
545                         MetaBall *mb = ob->data;
546                         BKE_mball_transform(mb, mat, do_props);
547                 }
548                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
549                         Curve *cu = ob->data;
550                         scale = mat3_to_scale(rsmat);
551                         BKE_curve_transform_ex(cu, mat, true, do_props, scale);
552                 }
553                 else if (ob->type == OB_FONT) {
554                         Curve *cu = ob->data;
555                         int i;
556
557                         scale = mat3_to_scale(rsmat);
558
559                         for (i = 0; i < cu->totbox; i++) {
560                                 TextBox *tb = &cu->tb[i];
561                                 tb->x *= scale;
562                                 tb->y *= scale;
563                                 tb->w *= scale;
564                                 tb->h *= scale;
565                         }
566
567                         if (do_props) {
568                                 cu->fsize *= scale;
569                         }
570                 }
571                 else if (ob->type == OB_CAMERA) {
572                         MovieClip *clip = BKE_object_movieclip_get(scene, ob, false);
573
574                         /* applying scale on camera actually scales clip's reconstruction.
575                          * of there's clip assigned to camera nothing to do actually.
576                          */
577                         if (!clip)
578                                 continue;
579
580                         if (apply_scale)
581                                 BKE_tracking_reconstruction_scale(&clip->tracking, ob->size);
582                 }
583                 else if (ob->type == OB_EMPTY) {
584                         /* It's possible for empties too, even though they don't 
585                          * really have obdata, since we can simply apply the maximum
586                          * scaling to the empty's drawsize.
587                          *
588                          * Core Assumptions:
589                          * 1) Most scaled empties have uniform scaling 
590                          *    (i.e. for visibility reasons), AND/OR
591                          * 2) Preserving non-uniform scaling is not that important,
592                          *    and is something that many users would be willing to
593                          *    sacrifice for having an easy way to do this.
594                          */
595
596                         if ((apply_loc == false) &&
597                             (apply_rot == false) &&
598                             (apply_scale == true))
599                         {
600                                 float max_scale = max_fff(fabsf(ob->size[0]), fabsf(ob->size[1]), fabsf(ob->size[2]));
601                                 ob->empty_drawsize *= max_scale;
602                         }
603                 }
604                 else {
605                         continue;
606                 }
607
608                 if (apply_loc)
609                         zero_v3(ob->loc);
610                 if (apply_scale)
611                         ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
612                 if (apply_rot) {
613                         zero_v3(ob->rot);
614                         unit_qt(ob->quat);
615                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
616                 }
617
618                 BKE_object_where_is_calc(scene, ob);
619                 if (ob->type == OB_ARMATURE) {
620                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
621                 }
622
623                 ignore_parent_tx(bmain, scene, ob);
624
625                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
626
627                 changed = true;
628         }
629         CTX_DATA_END;
630
631         if (!changed) {
632                 BKE_report(reports, RPT_WARNING, "Objects have no data to transform");
633                 return OPERATOR_CANCELLED;
634         }
635
636         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
637         return OPERATOR_FINISHED;
638 }
639
640 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
641 {
642         Scene *scene = CTX_data_scene(C);
643         bool changed = false;
644         
645         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
646         {
647                 BKE_object_where_is_calc(scene, ob);
648                 BKE_object_apply_mat4(ob, ob->obmat, true, true);
649                 BKE_object_where_is_calc(scene, ob);
650
651                 /* update for any children that may get moved */
652                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
653         
654                 changed = true;
655         }
656         CTX_DATA_END;
657
658         if (!changed)
659                 return OPERATOR_CANCELLED;
660
661         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
662         return OPERATOR_FINISHED;
663 }
664
665 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
666 {
667         /* identifiers */
668         ot->name = "Apply Visual Transform";
669         ot->description = "Apply the object's visual transformation to its data";
670         ot->idname = "OBJECT_OT_visual_transform_apply";
671         
672         /* api callbacks */
673         ot->exec = visual_transform_apply_exec;
674         ot->poll = ED_operator_scene_editable;
675         
676         /* flags */
677         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
678 }
679
680 static int object_transform_apply_exec(bContext *C, wmOperator *op)
681 {
682         const bool loc = RNA_boolean_get(op->ptr, "location");
683         const bool rot = RNA_boolean_get(op->ptr, "rotation");
684         const bool sca = RNA_boolean_get(op->ptr, "scale");
685         const bool do_props = RNA_boolean_get(op->ptr, "properties");
686
687         if (loc || rot || sca) {
688                 return apply_objects_internal(C, op->reports, loc, rot, sca, do_props);
689         }
690         else {
691                 /* allow for redo */
692                 return OPERATOR_FINISHED;
693         }
694 }
695
696 void OBJECT_OT_transform_apply(wmOperatorType *ot)
697 {
698         /* identifiers */
699         ot->name = "Apply Object Transform";
700         ot->description = "Apply the object's transformation to its data";
701         ot->idname = "OBJECT_OT_transform_apply";
702
703         /* api callbacks */
704         ot->exec = object_transform_apply_exec;
705         ot->poll = ED_operator_objectmode;
706
707         /* flags */
708         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
709
710         RNA_def_boolean(ot->srna, "location", 0, "Location", "");
711         RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
712         RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
713         RNA_def_boolean(ot->srna, "properties", true, "Apply Properties",
714                         "Modify properties such as curve vertex radius, font size and bone envelope");
715 }
716
717 /********************* Set Object Center ************************/
718
719 enum {
720         GEOMETRY_TO_ORIGIN = 0,
721         ORIGIN_TO_GEOMETRY,
722         ORIGIN_TO_CURSOR,
723         ORIGIN_TO_CENTER_OF_MASS_SURFACE,
724         ORIGIN_TO_CENTER_OF_MASS_VOLUME,
725 };
726
727 static int object_origin_set_exec(bContext *C, wmOperator *op)
728 {
729         Main *bmain = CTX_data_main(C);
730         Scene *scene = CTX_data_scene(C);
731         Object *obact = CTX_data_active_object(C);
732         Object *obedit = CTX_data_edit_object(C);
733         Object *tob;
734         float cursor[3], cent[3], cent_neg[3], centn[3];
735         int centermode = RNA_enum_get(op->ptr, "type");
736         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
737
738         ListBase ctx_data_list;
739         CollectionPointerLink *ctx_ob;
740         CollectionPointerLink *ctx_ob_act = NULL;
741
742         /* keep track of what is changed */
743         int tot_change = 0, tot_lib_error = 0, tot_multiuser_arm_error = 0;
744
745         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
746                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
747                 return OPERATOR_CANCELLED;
748         }
749         else {
750                 /* get the view settings if 'around' isn't set and the view is available */
751                 View3D *v3d = CTX_wm_view3d(C);
752                 copy_v3_v3(cursor, ED_view3d_cursor3d_get(scene, v3d));
753                 if (v3d && !RNA_struct_property_is_set(op->ptr, "center"))
754                         around = v3d->around;
755         }
756
757         zero_v3(cent);
758
759         if (obedit) {
760                 if (obedit->type == OB_MESH) {
761                         Mesh *me = obedit->data;
762                         BMEditMesh *em = me->edit_btmesh;
763                         BMVert *eve;
764                         BMIter iter;
765                         
766                         if (centermode == ORIGIN_TO_CURSOR) {
767                                 copy_v3_v3(cent, cursor);
768                                 invert_m4_m4(obedit->imat, obedit->obmat);
769                                 mul_m4_v3(obedit->imat, cent);
770                         }
771                         else {
772                                 if (around == V3D_AROUND_CENTER_MEAN) {
773                                         if (em->bm->totvert) {
774                                                 const float total_div = 1.0f / (float)em->bm->totvert;
775                                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
776                                                         madd_v3_v3fl(cent, eve->co, total_div);
777                                                 }
778                                         }
779                                 }
780                                 else {
781                                         float min[3], max[3];
782                                         INIT_MINMAX(min, max);
783                                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
784                                                 minmax_v3v3_v3(min, max, eve->co);
785                                         }
786                                         mid_v3_v3v3(cent, min, max);
787                                 }
788                         }
789                         
790                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
791                                 sub_v3_v3(eve->co, cent);
792                         }
793
794                         EDBM_mesh_normals_update(em);
795                         tot_change++;
796                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
797                 }
798         }
799
800         CTX_data_selected_editable_objects(C, &ctx_data_list);
801
802         /* reset flags */
803         for (ctx_ob = ctx_data_list.first;
804              ctx_ob;
805              ctx_ob = ctx_ob->next)
806         {
807                 Object *ob = ctx_ob->ptr.data;
808                 ob->flag &= ~OB_DONE;
809
810                 /* move active first */
811                 if (ob == obact) {
812                         ctx_ob_act = ctx_ob;
813                 }
814         }
815
816         if (ctx_ob_act) {
817                 BLI_listbase_rotate_first(&ctx_data_list, (LinkData *)ctx_ob_act);
818         }
819
820         for (tob = bmain->object.first; tob; tob = tob->id.next) {
821                 if (tob->data)
822                         ((ID *)tob->data)->tag &= ~LIB_TAG_DOIT;
823                 if (tob->dup_group)
824                         ((ID *)tob->dup_group)->tag &= ~LIB_TAG_DOIT;
825         }
826
827         for (ctx_ob = ctx_data_list.first;
828              ctx_ob;
829              ctx_ob = ctx_ob->next)
830         {
831                 Object *ob = ctx_ob->ptr.data;
832
833                 if ((ob->flag & OB_DONE) == 0) {
834                         bool do_inverse_offset = false;
835                         ob->flag |= OB_DONE;
836
837                         if (centermode == ORIGIN_TO_CURSOR) {
838                                 copy_v3_v3(cent, cursor);
839                                 invert_m4_m4(ob->imat, ob->obmat);
840                                 mul_m4_v3(ob->imat, cent);
841                         }
842                         
843                         if (ob->data == NULL) {
844                                 /* special support for dupligroups */
845                                 if ((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.tag & LIB_TAG_DOIT) == 0) {
846                                         if (ID_IS_LINKED(ob->dup_group)) {
847                                                 tot_lib_error++;
848                                         }
849                                         else {
850                                                 if (centermode == ORIGIN_TO_CURSOR) {
851                                                         /* done */
852                                                 }
853                                                 else {
854                                                         float min[3], max[3];
855                                                         /* only bounds support */
856                                                         INIT_MINMAX(min, max);
857                                                         BKE_object_minmax_dupli(scene, ob, min, max, true);
858                                                         mid_v3_v3v3(cent, min, max);
859                                                         invert_m4_m4(ob->imat, ob->obmat);
860                                                         mul_m4_v3(ob->imat, cent);
861                                                 }
862                                                 
863                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
864
865                                                 tot_change++;
866                                                 ob->dup_group->id.tag |= LIB_TAG_DOIT;
867                                                 do_inverse_offset = true;
868                                         }
869                                 }
870                         }
871                         else if (ID_IS_LINKED(ob->data)) {
872                                 tot_lib_error++;
873                         }
874
875                         if (obedit == NULL && ob->type == OB_MESH) {
876                                 Mesh *me = ob->data;
877
878                                 if (centermode == ORIGIN_TO_CURSOR) {
879                                         /* done */
880                                 }
881                                 else if (centermode == ORIGIN_TO_CENTER_OF_MASS_SURFACE) {
882                                         BKE_mesh_center_of_surface(me, cent);
883                                 }
884                                 else if (centermode == ORIGIN_TO_CENTER_OF_MASS_VOLUME) {
885                                         BKE_mesh_center_of_volume(me, cent);
886                                 }
887                                 else if (around == V3D_AROUND_CENTER_MEAN) {
888                                         BKE_mesh_center_median(me, cent);
889                                 }
890                                 else {
891                                         BKE_mesh_center_bounds(me, cent);
892                                 }
893
894                                 negate_v3_v3(cent_neg, cent);
895                                 BKE_mesh_translate(me, cent_neg, 1);
896
897                                 tot_change++;
898                                 me->id.tag |= LIB_TAG_DOIT;
899                                 do_inverse_offset = true;
900                         }
901                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
902                                 Curve *cu = ob->data;
903
904                                 if      (centermode == ORIGIN_TO_CURSOR)    { /* done */ }
905                                 else if (around == V3D_AROUND_CENTER_MEAN)  { BKE_curve_center_median(cu, cent); }
906                                 else                                        { BKE_curve_center_bounds(cu, cent); }
907
908                                 /* don't allow Z change if curve is 2D */
909                                 if ((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
910                                         cent[2] = 0.0;
911
912                                 negate_v3_v3(cent_neg, cent);
913                                 BKE_curve_translate(cu, cent_neg, 1);
914
915                                 tot_change++;
916                                 cu->id.tag |= LIB_TAG_DOIT;
917                                 do_inverse_offset = true;
918
919                                 if (obedit) {
920                                         if (centermode == GEOMETRY_TO_ORIGIN) {
921                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
922                                         }
923                                         break;
924                                 }
925                         }
926                         else if (ob->type == OB_FONT) {
927                                 /* get from bb */
928
929                                 Curve *cu = ob->data;
930
931                                 if (ob->bb == NULL && (centermode != ORIGIN_TO_CURSOR)) {
932                                         /* do nothing*/
933                                 }
934                                 else {
935                                         if (centermode == ORIGIN_TO_CURSOR) {
936                                                 /* done */
937                                         }
938                                         else {
939                                                 /* extra 0.5 is the height o above line */
940                                                 cent[0] = 0.5f * (ob->bb->vec[4][0] + ob->bb->vec[0][0]);
941                                                 cent[1] = 0.5f * (ob->bb->vec[0][1] + ob->bb->vec[2][1]);
942                                         }
943
944                                         cent[2] = 0.0f;
945
946                                         cu->xof = cu->xof - cent[0];
947                                         cu->yof = cu->yof - cent[1];
948
949                                         tot_change++;
950                                         cu->id.tag |= LIB_TAG_DOIT;
951                                         do_inverse_offset = true;
952                                 }
953                         }
954                         else if (ob->type == OB_ARMATURE) {
955                                 bArmature *arm = ob->data;
956
957                                 if (ID_REAL_USERS(arm) > 1) {
958 #if 0
959                                         BKE_report(op->reports, RPT_ERROR, "Cannot apply to a multi user armature");
960                                         return;
961 #endif
962                                         tot_multiuser_arm_error++;
963                                 }
964                                 else {
965                                         /* Function to recenter armatures in editarmature.c
966                                          * Bone + object locations are handled there.
967                                          */
968                                         ED_armature_origin_set(scene, ob, cursor, centermode, around);
969
970                                         tot_change++;
971                                         arm->id.tag |= LIB_TAG_DOIT;
972                                         /* do_inverse_offset = true; */ /* docenter_armature() handles this */
973
974                                         BKE_object_where_is_calc(scene, ob);
975                                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
976
977                                         ignore_parent_tx(bmain, scene, ob);
978
979                                         if (obedit)
980                                                 break;
981                                 }
982                         }
983                         else if (ob->type == OB_MBALL) {
984                                 MetaBall *mb = ob->data;
985
986                                 if      (centermode == ORIGIN_TO_CURSOR)    { /* done */ }
987                                 else if (around == V3D_AROUND_CENTER_MEAN)  { BKE_mball_center_median(mb, cent); }
988                                 else                                        { BKE_mball_center_bounds(mb, cent); }
989
990                                 negate_v3_v3(cent_neg, cent);
991                                 BKE_mball_translate(mb, cent_neg);
992
993                                 tot_change++;
994                                 mb->id.tag |= LIB_TAG_DOIT;
995                                 do_inverse_offset = true;
996
997                                 if (obedit) {
998                                         if (centermode == GEOMETRY_TO_ORIGIN) {
999                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
1000                                         }
1001                                         break;
1002                                 }
1003                         }
1004                         else if (ob->type == OB_LATTICE) {
1005                                 Lattice *lt = ob->data;
1006
1007                                 if      (centermode == ORIGIN_TO_CURSOR)    { /* done */ }
1008                                 else if (around == V3D_AROUND_CENTER_MEAN)  { BKE_lattice_center_median(lt, cent); }
1009                                 else                                        { BKE_lattice_center_bounds(lt, cent); }
1010
1011                                 negate_v3_v3(cent_neg, cent);
1012                                 BKE_lattice_translate(lt, cent_neg, 1);
1013
1014                                 tot_change++;
1015                                 lt->id.tag |= LIB_TAG_DOIT;
1016                                 do_inverse_offset = true;
1017                         }
1018
1019                         /* offset other selected objects */
1020                         if (do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
1021                                 CollectionPointerLink *ctx_link_other;
1022                                 float obmat[4][4];
1023
1024                                 /* was the object data modified
1025                                  * note: the functions above must set 'cent' */
1026
1027                                 /* convert the offset to parent space */
1028                                 BKE_object_to_mat4(ob, obmat);
1029                                 mul_v3_mat3_m4v3(centn, obmat, cent); /* omit translation part */
1030
1031                                 add_v3_v3(ob->loc, centn);
1032
1033                                 BKE_object_where_is_calc(scene, ob);
1034                                 if (ob->type == OB_ARMATURE) {
1035                                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
1036                                 }
1037
1038                                 ignore_parent_tx(bmain, scene, ob);
1039                                 
1040                                 /* other users? */
1041                                 //CTX_DATA_BEGIN (C, Object *, ob_other, selected_editable_objects)
1042                                 //{
1043
1044                                 /* use existing context looper */
1045                                 for (ctx_link_other = ctx_data_list.first;
1046                                      ctx_link_other;
1047                                      ctx_link_other = ctx_link_other->next)
1048                                 {
1049                                         Object *ob_other = ctx_link_other->ptr.data;
1050
1051                                         if ((ob_other->flag & OB_DONE) == 0 &&
1052                                             ((ob->data && (ob->data == ob_other->data)) ||
1053                                              (ob->dup_group == ob_other->dup_group &&
1054                                               (ob->transflag | ob_other->transflag) & OB_DUPLIGROUP)))
1055                                         {
1056                                                 ob_other->flag |= OB_DONE;
1057                                                 DAG_id_tag_update(&ob_other->id, OB_RECALC_OB | OB_RECALC_DATA);
1058
1059                                                 mul_v3_mat3_m4v3(centn, ob_other->obmat, cent); /* omit translation part */
1060                                                 add_v3_v3(ob_other->loc, centn);
1061
1062                                                 BKE_object_where_is_calc(scene, ob_other);
1063                                                 if (ob_other->type == OB_ARMATURE) {
1064                                                         BKE_pose_where_is(scene, ob_other); /* needed for bone parents */
1065                                                 }
1066                                                 ignore_parent_tx(bmain, scene, ob_other);
1067                                         }
1068                                 }
1069                                 //CTX_DATA_END;
1070                         }
1071                 }
1072         }
1073         BLI_freelistN(&ctx_data_list);
1074
1075         for (tob = bmain->object.first; tob; tob = tob->id.next)
1076                 if (tob->data && (((ID *)tob->data)->tag & LIB_TAG_DOIT))
1077                         DAG_id_tag_update(&tob->id, OB_RECALC_OB | OB_RECALC_DATA);
1078
1079         if (tot_change) {
1080                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1081         }
1082
1083         /* Warn if any errors occurred */
1084         if (tot_lib_error + tot_multiuser_arm_error) {
1085                 BKE_reportf(op->reports, RPT_WARNING, "%i object(s) not centered, %i changed:", tot_lib_error + tot_multiuser_arm_error, tot_change);
1086                 if (tot_lib_error)
1087                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library object(s)", tot_lib_error);
1088                 if (tot_multiuser_arm_error)
1089                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)", tot_multiuser_arm_error);
1090         }
1091
1092         return OPERATOR_FINISHED;
1093 }
1094
1095 void OBJECT_OT_origin_set(wmOperatorType *ot)
1096 {
1097         static const EnumPropertyItem prop_set_center_types[] = {
1098                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
1099                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry",
1100                  "Calculate the center of geometry based on the current pivot point (median, otherwise bounding-box)"},
1101                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor",
1102                  "Move object origin to position of the 3D cursor"},
1103                 /* Intentional naming mismatch since some scripts refer to this. */
1104                 {ORIGIN_TO_CENTER_OF_MASS_SURFACE, "ORIGIN_CENTER_OF_MASS", 0, "Origin to Center of Mass (Surface)",
1105                  "Calculate the center of mass from the surface area"},
1106                 {ORIGIN_TO_CENTER_OF_MASS_VOLUME, "ORIGIN_CENTER_OF_VOLUME", 0, "Origin to Center of Mass (Volume)",
1107                  "Calculate the center of mass from the volume (must be manifold geometry with consistent normals)"},
1108                 {0, NULL, 0, NULL, NULL}
1109         };
1110         
1111         static const EnumPropertyItem prop_set_bounds_types[] = {
1112                 {V3D_AROUND_CENTER_MEAN, "MEDIAN", 0, "Median Center", ""},
1113                 {V3D_AROUND_CENTER_BOUNDS, "BOUNDS", 0, "Bounds Center", ""},
1114                 {0, NULL, 0, NULL, NULL}
1115         };
1116         
1117         /* identifiers */
1118         ot->name = "Set Origin";
1119         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3D cursor";
1120         ot->idname = "OBJECT_OT_origin_set";
1121         
1122         /* api callbacks */
1123         ot->invoke = WM_menu_invoke;
1124         ot->exec = object_origin_set_exec;
1125         
1126         ot->poll = ED_operator_scene_editable;
1127         
1128         /* flags */
1129         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1130         
1131         ot->prop = RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
1132         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_AROUND_CENTER_MEAN, "Center", "");
1133 }