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