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