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