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