fix [#32616] Changing parent does not keep transformation
[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], tmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
385         int a, 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                                 /* correct for scale and rotation that is still applied */
468                                 BKE_object_to_mat3(ob, obmat);
469                                 invert_m3_m3(iobmat, obmat);
470                                 mul_m3_m3m3(tmat, rsmat, iobmat);
471                                 mul_m3_v3(tmat, mat[3]);
472                         }
473                 }
474
475                 /* apply to object data */
476                 if (ob->type == OB_MESH) {
477                         Mesh *me = ob->data;
478                         MVert *mvert;
479
480                         if (apply_scale)
481                                 multiresModifier_scale_disp(scene, ob);
482                         
483                         /* adjust data */
484                         mvert = me->mvert;
485                         for (a = 0; a < me->totvert; a++, mvert++)
486                                 mul_m4_v3(mat, mvert->co);
487                         
488                         if (me->key) {
489                                 KeyBlock *kb;
490                                 
491                                 for (kb = me->key->block.first; kb; kb = kb->next) {
492                                         float *fp = kb->data;
493                                         
494                                         for (a = 0; a < kb->totelem; a++, fp += 3)
495                                                 mul_m4_v3(mat, fp);
496                                 }
497                         }
498                         
499                         /* update normals */
500                         BKE_mesh_calc_normals_mapping(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
501                 }
502                 else if (ob->type == OB_ARMATURE) {
503                         ED_armature_apply_transform(ob, mat);
504                 }
505                 else if (ob->type == OB_LATTICE) {
506                         Lattice *lt = ob->data;
507                         BPoint *bp = lt->def;
508                         int a = lt->pntsu * lt->pntsv * lt->pntsw;
509                         
510                         while (a--) {
511                                 mul_m4_v3(mat, bp->vec);
512                                 bp++;
513                         }
514                 }
515                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
516                         Curve *cu = ob->data;
517
518                         Nurb *nu;
519                         BPoint *bp;
520                         BezTriple *bezt;
521
522                         scale = mat3_to_scale(rsmat);
523
524                         for (nu = cu->nurb.first; nu; nu = nu->next) {
525                                 if (nu->type == CU_BEZIER) {
526                                         a = nu->pntsu;
527                                         for (bezt = nu->bezt; a--; bezt++) {
528                                                 mul_m4_v3(mat, bezt->vec[0]);
529                                                 mul_m4_v3(mat, bezt->vec[1]);
530                                                 mul_m4_v3(mat, bezt->vec[2]);
531                                                 bezt->radius *= scale;
532                                         }
533                                         BKE_nurb_handles_calc(nu);
534                                 }
535                                 else {
536                                         a = nu->pntsu * nu->pntsv;
537                                         for (bp = nu->bp; a--; bp++)
538                                                 mul_m4_v3(mat, bp->vec);
539                                 }
540                         }
541                 }
542                 else
543                         continue;
544
545                 if (apply_loc)
546                         zero_v3(ob->loc);
547                 if (apply_scale)
548                         ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
549                 if (apply_rot) {
550                         zero_v3(ob->rot);
551                         unit_qt(ob->quat);
552                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
553                 }
554
555                 BKE_object_where_is_calc(scene, ob);
556                 if (ob->type == OB_ARMATURE) {
557                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
558                 }
559
560                 ignore_parent_tx(bmain, scene, ob);
561
562                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
563
564                 change = 1;
565         }
566         CTX_DATA_END;
567
568         if (!change)
569                 return OPERATOR_CANCELLED;
570
571         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
572         return OPERATOR_FINISHED;
573 }
574
575 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
576 {
577         Scene *scene = CTX_data_scene(C);
578         int change = 0;
579         
580         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
581         {
582                 BKE_object_where_is_calc(scene, ob);
583                 BKE_object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
584                 BKE_object_where_is_calc(scene, ob);
585
586                 /* update for any children that may get moved */
587                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
588         
589                 change = 1;
590         }
591         CTX_DATA_END;
592
593         if (!change)
594                 return OPERATOR_CANCELLED;
595
596         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
597         return OPERATOR_FINISHED;
598 }
599
600 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
601 {
602         /* identifiers */
603         ot->name = "Apply Visual Transform";
604         ot->description = "Apply the object's visual transformation to its data";
605         ot->idname = "OBJECT_OT_visual_transform_apply";
606         
607         /* api callbacks */
608         ot->exec = visual_transform_apply_exec;
609         ot->poll = ED_operator_scene_editable;
610         
611         /* flags */
612         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
613 }
614
615 static int object_transform_apply_exec(bContext *C, wmOperator *op)
616 {
617         const int loc = RNA_boolean_get(op->ptr, "location");
618         const int rot = RNA_boolean_get(op->ptr, "rotation");
619         const int sca = RNA_boolean_get(op->ptr, "scale");
620
621         if (loc || rot || sca) {
622                 return apply_objects_internal(C, op->reports, loc, rot, sca);
623         }
624         else {
625                 return OPERATOR_CANCELLED;
626         }
627 }
628
629 void OBJECT_OT_transform_apply(wmOperatorType *ot)
630 {
631         /* identifiers */
632         ot->name = "Apply Object Transform";
633         ot->description = "Apply the object's transformation to its data";
634         ot->idname = "OBJECT_OT_transform_apply";
635
636         /* api callbacks */
637         ot->exec = object_transform_apply_exec;
638         ot->poll = ED_operator_objectmode;
639
640         /* flags */
641         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
642
643         RNA_def_boolean(ot->srna, "location", 0, "Location", "");
644         RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
645         RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
646 }
647
648 /********************* Set Object Center ************************/
649
650 enum {
651         GEOMETRY_TO_ORIGIN = 0,
652         ORIGIN_TO_GEOMETRY,
653         ORIGIN_TO_CURSOR
654 };
655
656 static int object_origin_set_exec(bContext *C, wmOperator *op)
657 {
658         Main *bmain = CTX_data_main(C);
659         Scene *scene = CTX_data_scene(C);
660         Object *obedit = CTX_data_edit_object(C);
661         Object *tob;
662         float cursor[3], cent[3], cent_neg[3], centn[3];
663         int centermode = RNA_enum_get(op->ptr, "type");
664         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
665
666         /* keep track of what is changed */
667         int tot_change = 0, tot_lib_error = 0, tot_multiuser_arm_error = 0;
668
669         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
670                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
671                 return OPERATOR_CANCELLED;
672         }
673         else {
674                 /* get the view settings if 'around' isn't set and the view is available */
675                 View3D *v3d = CTX_wm_view3d(C);
676                 copy_v3_v3(cursor, give_cursor(scene, v3d));
677                 if (v3d && !RNA_struct_property_is_set(op->ptr, "center"))
678                         around = v3d->around;
679         }
680
681         zero_v3(cent);
682
683         if (obedit) {
684                 if (obedit->type == OB_MESH) {
685                         Mesh *me = obedit->data;
686                         BMEditMesh *em = me->edit_btmesh;
687                         BMVert *eve;
688                         BMIter iter;
689                         
690                         if (centermode == ORIGIN_TO_CURSOR) {
691                                 copy_v3_v3(cent, cursor);
692                                 invert_m4_m4(obedit->imat, obedit->obmat);
693                                 mul_m4_v3(obedit->imat, cent);
694                         }
695                         else {
696                                 if (around == V3D_CENTROID) {
697                                         const float total_div = 1.0f / (float)em->bm->totvert;
698                                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
699                                                 madd_v3_v3fl(cent, eve->co, total_div);
700                                         }
701                                 }
702                                 else {
703                                         float min[3], max[3];
704                                         INIT_MINMAX(min, max);
705                                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
706                                                 minmax_v3v3_v3(min, max, eve->co);
707                                         }
708                                         mid_v3_v3v3(cent, min, max);
709                                 }
710                         }
711                         
712                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
713                                 sub_v3_v3(eve->co, cent);
714                         }
715
716                         EDBM_mesh_normals_update(em);
717                         tot_change++;
718                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
719                 }
720         }
721
722         /* reset flags */
723         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
724         {
725                 ob->flag &= ~OB_DONE;
726         }
727         CTX_DATA_END;
728
729         for (tob = bmain->object.first; tob; tob = tob->id.next) {
730                 if (tob->data)
731                         ((ID *)tob->data)->flag &= ~LIB_DOIT;
732                 if (tob->dup_group)
733                         ((ID *)tob->dup_group)->flag &= ~LIB_DOIT;
734         }
735
736         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
737         {
738                 if ((ob->flag & OB_DONE) == 0) {
739                         int do_inverse_offset = FALSE;
740                         ob->flag |= OB_DONE;
741
742                         if (centermode == ORIGIN_TO_CURSOR) {
743                                 copy_v3_v3(cent, cursor);
744                                 invert_m4_m4(ob->imat, ob->obmat);
745                                 mul_m4_v3(ob->imat, cent);
746                         }
747                         
748                         if (ob->data == NULL) {
749                                 /* special support for dupligroups */
750                                 if ((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.flag & LIB_DOIT) == 0) {
751                                         if (ob->dup_group->id.lib) {
752                                                 tot_lib_error++;
753                                         }
754                                         else {
755                                                 if (centermode == ORIGIN_TO_CURSOR) {
756                                                         /* done */
757                                                 }
758                                                 else {
759                                                         float min[3], max[3];
760                                                         /* only bounds support */
761                                                         INIT_MINMAX(min, max);
762                                                         BKE_object_minmax_dupli(scene, ob, min, max, TRUE);
763                                                         mid_v3_v3v3(cent, min, max);
764                                                         invert_m4_m4(ob->imat, ob->obmat);
765                                                         mul_m4_v3(ob->imat, cent);
766                                                 }
767                                                 
768                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
769
770                                                 tot_change++;
771                                                 ob->dup_group->id.flag |= LIB_DOIT;
772                                                 do_inverse_offset = TRUE;
773                                         }
774                                 }
775                         }
776                         else if (((ID *)ob->data)->lib) {
777                                 tot_lib_error++;
778                         }
779
780                         if (obedit == NULL && ob->type == OB_MESH) {
781                                 Mesh *me = ob->data;
782
783                                 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
784                                 else if (around == V3D_CENTROID) { BKE_mesh_center_median(me, cent); }
785                                 else { BKE_mesh_center_bounds(me, cent); }
786
787                                 negate_v3_v3(cent_neg, cent);
788                                 BKE_mesh_translate(me, cent_neg, 1);
789
790                                 tot_change++;
791                                 me->id.flag |= LIB_DOIT;
792                                 do_inverse_offset = TRUE;
793                         }
794                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
795                                 Curve *cu = ob->data;
796
797                                 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
798                                 else if (around == V3D_CENTROID) { BKE_curve_center_median(cu, cent); }
799                                 else { BKE_curve_center_bounds(cu, cent);   }
800
801                                 /* don't allow Z change if curve is 2D */
802                                 if ((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
803                                         cent[2] = 0.0;
804
805                                 negate_v3_v3(cent_neg, cent);
806                                 BKE_curve_translate(cu, cent_neg, 1);
807
808                                 tot_change++;
809                                 cu->id.flag |= LIB_DOIT;
810                                 do_inverse_offset = TRUE;
811
812                                 if (obedit) {
813                                         if (centermode == GEOMETRY_TO_ORIGIN) {
814                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
815                                         }
816                                         break;
817                                 }
818                         }
819                         else if (ob->type == OB_FONT) {
820                                 /* get from bb */
821
822                                 Curve *cu = ob->data;
823
824                                 if (cu->bb == NULL && (centermode != ORIGIN_TO_CURSOR)) {
825                                         /* do nothing*/
826                                 }
827                                 else {
828                                         if (centermode == ORIGIN_TO_CURSOR) {
829                                                 /* done */
830                                         }
831                                         else {
832                                                 cent[0] = 0.5f * (cu->bb->vec[4][0] + cu->bb->vec[0][0]);
833                                                 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 */
834                                         }
835
836                                         cent[2] = 0.0f;
837
838                                         cu->xof = cu->xof - (cent[0] / cu->fsize);
839                                         cu->yof = cu->yof - (cent[1] / cu->fsize);
840
841                                         tot_change++;
842                                         cu->id.flag |= LIB_DOIT;
843                                         do_inverse_offset = TRUE;
844                                 }
845                         }
846                         else if (ob->type == OB_ARMATURE) {
847                                 bArmature *arm = ob->data;
848
849                                 if (ID_REAL_USERS(arm) > 1) {
850 #if 0
851                                         BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
852                                         return;
853 #endif
854                                         tot_multiuser_arm_error++;
855                                 }
856                                 else {
857                                         /* Function to recenter armatures in editarmature.c
858                                          * Bone + object locations are handled there.
859                                          */
860                                         docenter_armature(scene, ob, cursor, centermode, around);
861
862                                         tot_change++;
863                                         arm->id.flag |= LIB_DOIT;
864                                         /* do_inverse_offset= TRUE; */ /* docenter_armature() handles this */
865
866                                         BKE_object_where_is_calc(scene, ob);
867                                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
868
869                                         ignore_parent_tx(bmain, scene, ob);
870
871                                         if (obedit)
872                                                 break;
873                                 }
874                         }
875                         else if (ob->type == OB_MBALL) {
876                                 MetaBall *mb = ob->data;
877
878                                 if (centermode == ORIGIN_TO_CURSOR) { /* done */ }
879                                 else if (around == V3D_CENTROID) { BKE_mball_center_median(mb, cent); }
880                                 else { BKE_mball_center_bounds(mb, cent);    }
881
882                                 negate_v3_v3(cent_neg, cent);
883                                 BKE_mball_translate(mb, cent_neg);
884
885                                 tot_change++;
886                                 mb->id.flag |= LIB_DOIT;
887                                 do_inverse_offset = TRUE;
888
889                                 if (obedit) {
890                                         if (centermode == GEOMETRY_TO_ORIGIN) {
891                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
892                                         }
893                                         break;
894                                 }
895                         }
896
897                         /* offset other selected objects */
898                         if (do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
899                                 /* was the object data modified
900                                  * note: the functions above must set 'cent' */
901                                 copy_v3_v3(centn, cent);
902                                 mul_mat3_m4_v3(ob->obmat, centn); /* ommit translation part */
903                                 add_v3_v3(ob->loc, centn);
904
905                                 BKE_object_where_is_calc(scene, ob);
906                                 if (ob->type == OB_ARMATURE) {
907                                         BKE_pose_where_is(scene, ob); /* needed for bone parents */
908                                 }
909
910                                 ignore_parent_tx(bmain, scene, ob);
911                                 
912                                 /* other users? */
913                                 CTX_DATA_BEGIN (C, Object *, ob_other, selected_editable_objects)
914                                 {
915                                         if ((ob_other->flag & OB_DONE) == 0 &&
916                                             ((ob->data && (ob->data == ob_other->data)) ||
917                                              (ob->dup_group == ob_other->dup_group &&
918                                               (ob->transflag | ob_other->transflag) & OB_DUPLIGROUP)))
919                                         {
920                                                 ob_other->flag |= OB_DONE;
921                                                 DAG_id_tag_update(&ob_other->id, OB_RECALC_OB | OB_RECALC_DATA);
922
923                                                 copy_v3_v3(centn, cent);
924                                                 mul_mat3_m4_v3(ob_other->obmat, centn); /* ommit translation part */
925                                                 add_v3_v3(ob_other->loc, centn);
926
927                                                 BKE_object_where_is_calc(scene, ob_other);
928                                                 if (ob_other->type == OB_ARMATURE) {
929                                                         BKE_pose_where_is(scene, ob_other); /* needed for bone parents */
930                                                 }
931                                                 ignore_parent_tx(bmain, scene, ob_other);
932                                         }
933                                 }
934                                 CTX_DATA_END;
935                         }
936                 }
937         }
938         CTX_DATA_END;
939
940         for (tob = bmain->object.first; tob; tob = tob->id.next)
941                 if (tob->data && (((ID *)tob->data)->flag & LIB_DOIT))
942                         DAG_id_tag_update(&tob->id, OB_RECALC_OB | OB_RECALC_DATA);
943
944         if (tot_change) {
945                 DAG_ids_flush_update(bmain, 0);
946                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
947         }
948
949         /* Warn if any errors occurred */
950         if (tot_lib_error + tot_multiuser_arm_error) {
951                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:", tot_lib_error + tot_multiuser_arm_error, tot_change);
952                 if (tot_lib_error)
953                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects", tot_lib_error);
954                 if (tot_multiuser_arm_error)
955                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)", tot_multiuser_arm_error);
956         }
957
958         return OPERATOR_FINISHED;
959 }
960
961 void OBJECT_OT_origin_set(wmOperatorType *ot)
962 {
963         static EnumPropertyItem prop_set_center_types[] = {
964                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
965                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
966                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
967                 {0, NULL, 0, NULL, NULL}
968         };
969         
970         static EnumPropertyItem prop_set_bounds_types[] = {
971                 {V3D_CENTROID, "MEDIAN", 0, "Median Center", ""},
972                 {V3D_CENTER, "BOUNDS", 0, "Bounds Center", ""},
973                 {0, NULL, 0, NULL, NULL}
974         };
975         
976         /* identifiers */
977         ot->name = "Set Origin";
978         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
979         ot->idname = "OBJECT_OT_origin_set";
980         
981         /* api callbacks */
982         ot->invoke = WM_menu_invoke;
983         ot->exec = object_origin_set_exec;
984         
985         ot->poll = ED_operator_scene_editable;
986         
987         /* flags */
988         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
989         
990         ot->prop = RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
991         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_CENTROID, "Center", "");
992 }
993