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