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