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