fix for non Euler-XYZ rotations...
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <stdlib.h>
29 #include <string.h>
30
31 #include "DNA_anim_types.h"
32 #include "DNA_action_types.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_key_types.h"
36 #include "DNA_mesh_types.h"
37 #include "DNA_meshdata_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_view3d_types.h"
42
43 #include "BLI_math.h"
44 #include "BLI_editVert.h"
45 #include "BLI_listbase.h"
46
47 #include "BKE_context.h"
48 #include "BKE_curve.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_mesh.h"
53 #include "BKE_object.h"
54 #include "BKE_report.h"
55 #include "BKE_utildefines.h"
56
57 #include "RNA_define.h"
58 #include "RNA_access.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "ED_anim_api.h"
64 #include "ED_armature.h"
65 #include "ED_curve.h"
66 #include "ED_keyframing.h"
67 #include "ED_mesh.h"
68 #include "ED_object.h"
69 #include "ED_screen.h"
70 #include "ED_view3d.h"
71
72 #include "object_intern.h"
73
74 /*************************** Clear Transformation ****************************/
75
76 static int object_location_clear_exec(bContext *C, wmOperator *op)
77 {
78         Scene *scene= CTX_data_scene(C);
79         
80         KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Location");
81         bCommonKeySrc cks;
82         ListBase dsources = {&cks, &cks};
83         
84         /* init common-key-source for use by KeyingSets */
85         memset(&cks, 0, sizeof(bCommonKeySrc));
86         
87         /* clear location of selected objects if not in weight-paint mode */
88         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
89                 if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
90                         if((ob->protectflag & OB_LOCK_LOCX)==0)
91                                 ob->loc[0]= ob->dloc[0]= 0.0f;
92                         if((ob->protectflag & OB_LOCK_LOCY)==0)
93                                 ob->loc[1]= ob->dloc[1]= 0.0f;
94                         if((ob->protectflag & OB_LOCK_LOCZ)==0)
95                                 ob->loc[2]= ob->dloc[2]= 0.0f;
96                                 
97                         /* do auto-keyframing as appropriate */
98                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
99                                 /* init cks for this object, then use the relative KeyingSets to keyframe it */
100                                 cks.id= &ob->id;
101                                 modify_keyframes(scene, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
102                         }
103                 }
104                 ob->recalc |= OB_RECALC_OB;
105         }
106         CTX_DATA_END;
107         
108         /* this is needed so children are also updated */
109         DAG_ids_flush_update(0);
110
111         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
112
113         return OPERATOR_FINISHED;
114 }
115
116 void OBJECT_OT_location_clear(wmOperatorType *ot)
117 {
118         /* identifiers */
119         ot->name= "Clear Location";
120         ot->description = "Clear the object's location.";
121         ot->idname= "OBJECT_OT_location_clear";
122         
123         /* api callbacks */
124         ot->exec= object_location_clear_exec;
125         ot->poll= ED_operator_object_active_editable;
126         
127         /* flags */
128         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
129 }
130
131 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
132 {
133         Scene *scene= CTX_data_scene(C);
134         
135         KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
136         bCommonKeySrc cks;
137         ListBase dsources = {&cks, &cks};
138         
139         /* init common-key-source for use by KeyingSets */
140         memset(&cks, 0, sizeof(bCommonKeySrc));
141         
142         /* clear rotation of selected objects if not in weight-paint mode */
143         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
144                 if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
145                         if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
146                                 if (ob->protectflag & OB_LOCK_ROT4D) {
147                                         /* perform clamping on a component by component basis */
148                                         if (ob->rotmode == ROT_MODE_AXISANGLE) {
149                                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
150                                                         ob->rotAngle= 0.0f;
151                                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
152                                                         ob->rotAxis[0]= 0.0f;
153                                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
154                                                         ob->rotAxis[1]= 0.0f;
155                                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
156                                                         ob->rotAxis[2]= 0.0f;
157                                                         
158                                                 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
159                                                 if (IS_EQ(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQ(ob->rotAxis[1], ob->rotAxis[2]))
160                                                         ob->rotAxis[1] = 1.0f;
161                                         }
162                                         else if (ob->rotmode == ROT_MODE_QUAT) {
163                                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
164                                                         ob->quat[0]= 1.0f;
165                                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
166                                                         ob->quat[1]= 0.0f;
167                                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
168                                                         ob->quat[2]= 0.0f;
169                                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
170                                                         ob->quat[3]= 0.0f;
171                                         }
172                                         else {
173                                                 /* the flag may have been set for the other modes, so just ignore the extra flag... */
174                                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
175                                                         ob->rot[0]= 0.0f;
176                                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
177                                                         ob->rot[1]= 0.0f;
178                                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
179                                                         ob->rot[2]= 0.0f;
180                                         }
181                                 }
182                                 else {
183                                         /* perform clamping using euler form (3-components) */
184                                         float eul[3], oldeul[3], quat1[4];
185                                         
186                                         if (ob->rotmode == ROT_MODE_QUAT) {
187                                                 QUATCOPY(quat1, ob->quat);
188                                                 quat_to_eul( oldeul,ob->quat);
189                                         }
190                                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
191                                                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,ob->rotAxis, ob->rotAngle);
192                                         }
193                                         else {
194                                                 VECCOPY(oldeul, ob->rot);
195                                         }
196                                         
197                                         eul[0]= eul[1]= eul[2]= 0.0f;
198                                         
199                                         if (ob->protectflag & OB_LOCK_ROTX)
200                                                 eul[0]= oldeul[0];
201                                         if (ob->protectflag & OB_LOCK_ROTY)
202                                                 eul[1]= oldeul[1];
203                                         if (ob->protectflag & OB_LOCK_ROTZ)
204                                                 eul[2]= oldeul[2];
205                                         
206                                         if (ob->rotmode == ROT_MODE_QUAT) {
207                                                 eul_to_quat( ob->quat,eul);
208                                                 /* quaternions flip w sign to accumulate rotations correctly */
209                                                 if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
210                                                         mul_qt_fl(ob->quat, -1.0f);
211                                                 }
212                                         }
213                                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
214                                                 eulO_to_axis_angle( ob->rotAxis, &ob->rotAngle,eul, EULER_ORDER_DEFAULT);
215                                         }
216                                         else {
217                                                 VECCOPY(ob->rot, eul);
218                                         }
219                                 }
220                         }                                               
221                         else { 
222                                 if (ob->rotmode == ROT_MODE_QUAT) {
223                                         ob->quat[1]=ob->quat[2]=ob->quat[3]= 0.0f; 
224                                         ob->quat[0]= 1.0f;
225                                 }
226                                 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
227                                         /* by default, make rotation of 0 radians around y-axis (roll) */
228                                         ob->rotAxis[0]=ob->rotAxis[2]=ob->rotAngle= 0.0f;
229                                         ob->rotAxis[1]= 1.0f;
230                                 }
231                                 else {
232                                         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f;
233                                 }
234                         }
235                         
236                         /* do auto-keyframing as appropriate */
237                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
238                                 /* init cks for this object, then use the relative KeyingSets to keyframe it */
239                                 cks.id= &ob->id;
240                                 modify_keyframes(scene, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
241                         }
242                 }
243                 ob->recalc |= OB_RECALC_OB;
244         }
245         CTX_DATA_END;
246         
247         /* this is needed so children are also updated */
248         DAG_ids_flush_update(0);
249
250         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
251         
252         return OPERATOR_FINISHED;
253 }
254
255 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
256 {
257         /* identifiers */
258         ot->name= "Clear Rotation";
259         ot->description = "Clear the object's rotation.";
260         ot->idname= "OBJECT_OT_rotation_clear";
261         
262         /* api callbacks */
263         ot->exec= object_rotation_clear_exec;
264         ot->poll= ED_operator_object_active_editable;
265         
266         /* flags */
267         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
268 }
269
270 static int object_scale_clear_exec(bContext *C, wmOperator *op)
271 {
272         Scene *scene= CTX_data_scene(C);
273         
274         KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Scaling");
275         bCommonKeySrc cks;
276         ListBase dsources = {&cks, &cks};
277         
278         /* init common-key-source for use by KeyingSets */
279         memset(&cks, 0, sizeof(bCommonKeySrc));
280         
281         /* clear scales of selected objects if not in weight-paint mode */
282         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
283                 if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
284                         if((ob->protectflag & OB_LOCK_SCALEX)==0) {
285                                 ob->dsize[0]= 0.0f;
286                                 ob->size[0]= 1.0f;
287                         }
288                         if((ob->protectflag & OB_LOCK_SCALEY)==0) {
289                                 ob->dsize[1]= 0.0f;
290                                 ob->size[1]= 1.0f;
291                         }
292                         if((ob->protectflag & OB_LOCK_SCALEZ)==0) {
293                                 ob->dsize[2]= 0.0f;
294                                 ob->size[2]= 1.0f;
295                         }
296                         
297                         /* do auto-keyframing as appropriate */
298                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
299                                 /* init cks for this object, then use the relative KeyingSets to keyframe it */
300                                 cks.id= &ob->id;
301                                 modify_keyframes(scene, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
302                         }
303                 }
304                 ob->recalc |= OB_RECALC_OB;
305         }
306         CTX_DATA_END;
307         
308         /* this is needed so children are also updated */
309         DAG_ids_flush_update(0);
310
311         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
312         
313         return OPERATOR_FINISHED;
314 }
315
316 void OBJECT_OT_scale_clear(wmOperatorType *ot)
317 {
318         /* identifiers */
319         ot->name= "Clear Scale";
320         ot->description = "Clear the object's scale.";
321         ot->idname= "OBJECT_OT_scale_clear";
322         
323         /* api callbacks */
324         ot->exec= object_scale_clear_exec;
325         ot->poll= ED_operator_object_active_editable;
326         
327         /* flags */
328         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
329 }
330
331 static int object_origin_clear_exec(bContext *C, wmOperator *op)
332 {
333         float *v1, *v3, mat[3][3];
334         int armature_clear= 0;
335
336         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
337                 if(ob->parent) {
338                         v1= ob->loc;
339                         v3= ob->parentinv[3];
340                         
341                         copy_m3_m4(mat, ob->parentinv);
342                         VECCOPY(v3, v1);
343                         v3[0]= -v3[0];
344                         v3[1]= -v3[1];
345                         v3[2]= -v3[2];
346                         mul_m3_v3(mat, v3);
347                 }
348                 ob->recalc |= OB_RECALC_OB;
349         }
350         CTX_DATA_END;
351
352         if(armature_clear==0) /* in this case flush was done */
353                 DAG_ids_flush_update(0);
354         
355         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
356         
357         return OPERATOR_FINISHED;
358 }
359
360 void OBJECT_OT_origin_clear(wmOperatorType *ot)
361 {
362         /* identifiers */
363         ot->name= "Clear Origin";
364         ot->description = "Clear the object's origin.";
365         ot->idname= "OBJECT_OT_origin_clear";
366         
367         /* api callbacks */
368         ot->exec= object_origin_clear_exec;
369         ot->poll= ED_operator_object_active_editable;
370         
371         /* flags */
372         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
373 }
374
375 /*************************** Apply Transformation ****************************/
376
377 /* use this when the loc/size/rot of the parent has changed but the children
378  * should stay in the same place, e.g. for apply-size-rot or object center */
379 static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob ) 
380 {
381         Object workob;
382         Object *ob_child;
383         
384         /* a change was made, adjust the children to compensate */
385         for(ob_child=bmain->object.first; ob_child; ob_child=ob_child->id.next) {
386                 if(ob_child->parent == ob) {
387                         ED_object_apply_obmat(ob_child);
388                         what_does_parent(scene, ob_child, &workob);
389                         invert_m4_m4(ob_child->parentinv, workob.obmat);
390                 }
391         }
392 }
393
394 static int apply_objects_internal(bContext *C, ReportList *reports, int apply_loc, int apply_scale, int apply_rot)
395 {
396         Main *bmain= CTX_data_main(C);
397         Scene *scene= CTX_data_scene(C);
398         bArmature *arm;
399         Mesh *me;
400         Curve *cu;
401         Nurb *nu;
402         BPoint *bp;
403         BezTriple *bezt;
404         MVert *mvert;
405         float rsmat[3][3], tmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
406         int a, change = 0;
407         
408         /* first check if we can execute */
409         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
410
411                 if(ob->type==OB_MESH) {
412                         me= ob->data;
413                         
414                         if(me->id.us>1) {
415                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user mesh, doing nothing.");
416                                 return OPERATOR_CANCELLED;
417                         }
418                 }
419                 else if(ob->type==OB_ARMATURE) {
420                         arm= ob->data;
421                         
422                         if(arm->id.us>1) {
423                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user armature, doing nothing.");
424                                 return OPERATOR_CANCELLED;
425                         }
426                 }
427                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
428                         cu= ob->data;
429                         
430                         if(cu->id.us>1) {
431                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user curve, doing nothing.");
432                                 return OPERATOR_CANCELLED;
433                         }
434                         if(cu->key) {
435                                 BKE_report(reports, RPT_ERROR, "Can't apply to a curve with vertex keys, doing nothing.");
436                                 return OPERATOR_CANCELLED;
437                         }
438                 }
439         }
440         CTX_DATA_END;
441         
442         /* now execute */
443         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
444
445                 /* calculate rotation/scale matrix */
446                 if(apply_scale && apply_rot)
447                         object_to_mat3(ob, rsmat);
448                 else if(apply_scale)
449                         object_scale_to_mat3(ob, rsmat);
450                 else if(apply_rot)
451                         object_rot_to_mat3(ob, rsmat);
452                 else
453                         unit_m3(rsmat);
454
455                 copy_m4_m3(mat, rsmat);
456
457                 /* calculate translation */
458                 if(apply_loc) {
459                         copy_v3_v3(mat[3], ob->loc);
460
461                         if(!(apply_scale && apply_rot)) {
462                                 /* correct for scale and rotation that is still applied */
463                                 object_to_mat3(ob, obmat);
464                                 invert_m3_m3(iobmat, obmat);
465                                 mul_m3_m3m3(tmat, rsmat, iobmat);
466                                 mul_m3_v3(tmat, mat[3]);
467                         }
468                 }
469
470                 /* apply to object data */
471                 if(ob->type==OB_MESH) {
472                         me= ob->data;
473                         
474                         /* adjust data */
475                         mvert= me->mvert;
476                         for(a=0; a<me->totvert; a++, mvert++)
477                                 mul_m4_v3(mat, mvert->co);
478                         
479                         if(me->key) {
480                                 KeyBlock *kb;
481                                 
482                                 for(kb=me->key->block.first; kb; kb=kb->next) {
483                                         float *fp= kb->data;
484                                         
485                                         for(a=0; a<kb->totelem; a++, fp+=3)
486                                                 mul_m4_v3(mat, fp);
487                                 }
488                         }
489                         
490                         /* update normals */
491                         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
492                 }
493                 else if (ob->type==OB_ARMATURE) {
494                         ED_armature_apply_transform(ob, mat);
495                 }
496                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
497                         cu= ob->data;
498
499                         scale = mat3_to_scale(rsmat);
500                         
501                         for(nu=cu->nurb.first; nu; nu=nu->next) {
502                                 if(nu->type == CU_BEZIER) {
503                                         a= nu->pntsu;
504                                         for(bezt= nu->bezt; a--; bezt++) {
505                                                 mul_m4_v3(mat, bezt->vec[0]);
506                                                 mul_m4_v3(mat, bezt->vec[1]);
507                                                 mul_m4_v3(mat, bezt->vec[2]);
508                                                 bezt->radius *= scale;
509                                                 bezt++;
510                                         }
511                                 }
512                                 else {
513                                         a= nu->pntsu*nu->pntsv;
514                                         for(bp= nu->bp; a--; bp++)
515                                                 mul_m4_v3(mat, bp->vec);
516                                 }
517                         }
518                 }
519                 else
520                         continue;
521
522                 if(apply_loc)
523                         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0f;
524                 if(apply_scale)
525                         ob->size[0]= ob->size[1]= ob->size[2]= 1.0f;
526                 if(apply_rot) {
527                         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f;
528                         ob->quat[1]= ob->quat[2]= ob->quat[3]= 0.0f;
529                         ob->rotAxis[0]= ob->rotAxis[2]= 0.0f;
530                         ob->rotAngle= 0.0f;
531                         
532                         ob->quat[0]= ob->rotAxis[1]= 1.0f;
533                 }
534
535                 where_is_object(scene, ob);
536                 ignore_parent_tx(bmain, scene, ob);
537
538                 DAG_id_flush_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
539
540                 change = 1;
541         }
542         CTX_DATA_END;
543
544         if(!change)
545                 return OPERATOR_CANCELLED;
546
547         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
548         return OPERATOR_FINISHED;
549 }
550
551 static int visual_transform_apply_exec(bContext *C, wmOperator *op)
552 {
553         Scene *scene= CTX_data_scene(C);
554         int change = 0;
555         
556         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
557                 where_is_object(scene, ob);
558                 object_apply_mat4(ob, ob->obmat);
559                 where_is_object(scene, ob);
560                 
561                 change = 1;
562         }
563         CTX_DATA_END;
564
565         if(!change)
566                 return OPERATOR_CANCELLED;
567
568         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
569         return OPERATOR_FINISHED;
570 }
571
572 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
573 {
574         /* identifiers */
575         ot->name= "Apply Visual Transform";
576         ot->description = "Apply the object's visual transformation to its data.";
577         ot->idname= "OBJECT_OT_visual_transform_apply";
578         
579         /* api callbacks */
580         ot->exec= visual_transform_apply_exec;
581         ot->poll= ED_operator_object_active_editable;
582         
583         /* flags */
584         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
585 }
586
587 static int location_apply_exec(bContext *C, wmOperator *op)
588 {
589         return apply_objects_internal(C, op->reports, 1, 0, 0);
590 }
591
592 void OBJECT_OT_location_apply(wmOperatorType *ot)
593 {
594         /* identifiers */
595         ot->name= "Apply Location";
596         ot->description = "Apply the object's location to its data.";
597         ot->idname= "OBJECT_OT_location_apply";
598         
599         /* api callbacks */
600         ot->exec= location_apply_exec;
601         ot->poll= ED_operator_object_active_editable;
602         
603         /* flags */
604         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
605 }
606
607 static int scale_apply_exec(bContext *C, wmOperator *op)
608 {
609         return apply_objects_internal(C, op->reports, 0, 1, 0);
610 }
611
612 void OBJECT_OT_scale_apply(wmOperatorType *ot)
613 {
614         /* identifiers */
615         ot->name= "Apply Scale";
616         ot->description = "Apply the object's scale to its data.";
617         ot->idname= "OBJECT_OT_scale_apply";
618         
619         /* api callbacks */
620         ot->exec= scale_apply_exec;
621         ot->poll= ED_operator_object_active_editable;
622         
623         /* flags */
624         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
625 }
626
627 static int rotation_apply_exec(bContext *C, wmOperator *op)
628 {
629         return apply_objects_internal(C, op->reports, 0, 0, 1);
630 }
631
632 void OBJECT_OT_rotation_apply(wmOperatorType *ot)
633 {
634         /* identifiers */
635         ot->name= "Apply Rotation";
636         ot->description = "Apply the object's rotation to its data.";
637         ot->idname= "OBJECT_OT_rotation_apply";
638         
639         /* api callbacks */
640         ot->exec= rotation_apply_exec;
641         ot->poll= ED_operator_object_active_editable;
642         
643         /* flags */
644         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
645 }
646
647 /************************ Texture Space Transform ****************************/
648
649 void texspace_edit(Scene *scene, View3D *v3d)
650 {
651         Base *base;
652         int nr=0;
653         
654         /* first test if from visible and selected objects
655          * texspacedraw is set:
656          */
657         
658         if(scene->obedit) return; // XXX get from context
659         
660         for(base= FIRSTBASE; base; base= base->next) {
661                 if(TESTBASELIB(v3d, base)) {
662                         break;
663                 }
664         }
665
666         if(base==0) {
667                 return;
668         }
669         
670         nr= 0; // XXX pupmenu("Texture Space %t|Grab/Move%x1|Size%x2");
671         if(nr<1) return;
672         
673         for(base= FIRSTBASE; base; base= base->next) {
674                 if(TESTBASELIB(v3d, base)) {
675                         base->object->dtx |= OB_TEXSPACE;
676                 }
677         }
678         
679
680         if(nr==1) {
681 // XXX          initTransform(TFM_TRANSLATION, CTX_TEXTURE);
682 // XXX          Transform();
683         }
684         else if(nr==2) {
685 // XXX          initTransform(TFM_RESIZE, CTX_TEXTURE);
686 // XXX          Transform();
687         }
688         else if(nr==3) {
689 // XXX          initTransform(TFM_ROTATION, CTX_TEXTURE);
690 // XXX          Transform();
691         }
692 }
693
694 /********************* Set Object Center ************************/
695
696 static EnumPropertyItem prop_set_center_types[] = {
697         {0, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
698         {1, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
699         {2, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
700         {0, NULL, 0, NULL, NULL}
701 };
702
703 /* 0 == do center, 1 == center new, 2 == center cursor */
704 static int object_origin_set_exec(bContext *C, wmOperator *op)
705 {
706         Main *bmain= CTX_data_main(C);
707         Scene *scene= CTX_data_scene(C);
708         ScrArea *sa= CTX_wm_area(C);
709         View3D *v3d= sa->spacedata.first;
710         Object *obedit= CTX_data_edit_object(C);
711         Mesh *me, *tme;
712         Curve *cu;
713 /*      BezTriple *bezt;
714         BPoint *bp; */
715         Nurb *nu, *nu1;
716         EditVert *eve;
717         float cent[3], centn[3], min[3], max[3], omat[3][3];
718         int a, total= 0;
719         int centermode = RNA_enum_get(op->ptr, "type");
720         
721         /* keep track of what is changed */
722         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
723         MVert *mvert;
724
725         if(scene->id.lib || v3d==NULL){
726                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed on Lib data");
727                  return OPERATOR_CANCELLED;
728         }
729         if (obedit && centermode > 0) {
730                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
731                 return OPERATOR_CANCELLED;
732         }       
733         cent[0]= cent[1]= cent[2]= 0.0; 
734         
735         if(obedit) {
736
737                 INIT_MINMAX(min, max);
738         
739                 if(obedit->type==OB_MESH) {
740                         Mesh *me= obedit->data;
741                         EditMesh *em = BKE_mesh_get_editmesh(me);
742
743                         for(eve= em->verts.first; eve; eve= eve->next) {
744                                 if(v3d->around==V3D_CENTROID) {
745                                         total++;
746                                         VECADD(cent, cent, eve->co);
747                                 }
748                                 else {
749                                         DO_MINMAX(eve->co, min, max);
750                                 }
751                         }
752                         
753                         if(v3d->around==V3D_CENTROID) {
754                                 mul_v3_fl(cent, 1.0f/(float)total);
755                         }
756                         else {
757                                 cent[0]= (min[0]+max[0])/2.0f;
758                                 cent[1]= (min[1]+max[1])/2.0f;
759                                 cent[2]= (min[2]+max[2])/2.0f;
760                         }
761                         
762                         for(eve= em->verts.first; eve; eve= eve->next) {
763                                 sub_v3_v3v3(eve->co, eve->co, cent);                    
764                         }
765                         
766                         recalc_editnormals(em);
767                         tot_change++;
768                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
769                         BKE_mesh_end_editmesh(me, em);
770                 }
771         }
772         
773         /* reset flags */
774         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
775                         ob->flag &= ~OB_DONE;
776         }
777         CTX_DATA_END;
778         
779         for (me= G.main->mesh.first; me; me= me->id.next) {
780                 me->flag &= ~ME_ISDONE;
781         }
782         
783         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
784                 if((ob->flag & OB_DONE)==0) {
785                         ob->flag |= OB_DONE;
786                                 
787                         if(obedit==NULL && (me=get_mesh(ob)) ) {
788                                 if (me->id.lib) {
789                                         tot_lib_error++;
790                                 } else {
791                                         if(centermode==2) {
792                                                 VECCOPY(cent, give_cursor(scene, v3d));
793                                                 invert_m4_m4(ob->imat, ob->obmat);
794                                                 mul_m4_v3(ob->imat, cent);
795                                         } else {
796                                                 INIT_MINMAX(min, max);
797                                                 mvert= me->mvert;
798                                                 for(a=0; a<me->totvert; a++, mvert++) {
799                                                         DO_MINMAX(mvert->co, min, max);
800                                                 }
801                                         
802                                                 cent[0]= (min[0]+max[0])/2.0f;
803                                                 cent[1]= (min[1]+max[1])/2.0f;
804                                                 cent[2]= (min[2]+max[2])/2.0f;
805                                         }
806
807                                         mvert= me->mvert;
808                                         for(a=0; a<me->totvert; a++, mvert++) {
809                                                 sub_v3_v3v3(mvert->co, mvert->co, cent);
810                                         }
811                                         
812                                         if (me->key) {
813                                                 KeyBlock *kb;
814                                                 for (kb=me->key->block.first; kb; kb=kb->next) {
815                                                         float *fp= kb->data;
816                                                         
817                                                         for (a=0; a<kb->totelem; a++, fp+=3) {
818                                                                 sub_v3_v3v3(fp, fp, cent);
819                                                         }
820                                                 }
821                                         }
822                                                 
823                                         me->flag |= ME_ISDONE;
824                                                 
825                                         if(centermode) {
826                                                 copy_m3_m4(omat, ob->obmat);
827                                                 
828                                                 VECCOPY(centn, cent);
829                                                 mul_m3_v3(omat, centn);
830                                                 ob->loc[0]+= centn[0];
831                                                 ob->loc[1]+= centn[1];
832                                                 ob->loc[2]+= centn[2];
833                                                 
834                                                 where_is_object(scene, ob);
835                                                 ignore_parent_tx(bmain, scene, ob);
836                                                 
837                                                 /* other users? */
838                                                 CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
839                                                         if((ob_other->flag & OB_DONE)==0) {
840                                                                 tme= get_mesh(ob_other);
841                                                                 
842                                                                 if(tme==me) {
843                                                                         
844                                                                         ob_other->flag |= OB_DONE;
845                                                                         ob_other->recalc= OB_RECALC_OB|OB_RECALC_DATA;
846
847                                                                         copy_m3_m4(omat, ob_other->obmat);
848                                                                         VECCOPY(centn, cent);
849                                                                         mul_m3_v3(omat, centn);
850                                                                         ob_other->loc[0]+= centn[0];
851                                                                         ob_other->loc[1]+= centn[1];
852                                                                         ob_other->loc[2]+= centn[2];
853                                                                         
854                                                                         where_is_object(scene, ob_other);
855                                                                         ignore_parent_tx(bmain, scene, ob_other);
856                                                                         
857                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
858                                                                                 mvert= tme->mvert;
859                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
860                                                                                         sub_v3_v3v3(mvert->co, mvert->co, cent);
861                                                                                 }
862                                                                                 
863                                                                                 if (tme->key) {
864                                                                                         KeyBlock *kb;
865                                                                                         for (kb=tme->key->block.first; kb; kb=kb->next) {
866                                                                                                 float *fp= kb->data;
867                                                                                                 
868                                                                                                 for (a=0; a<kb->totelem; a++, fp+=3) {
869                                                                                                         sub_v3_v3v3(fp, fp, cent);
870                                                                                                 }
871                                                                                         }
872                                                                                 }
873                                                                                 
874                                                                                 tme->flag |= ME_ISDONE;
875                                                                         }
876                                                                 }
877                                                         }
878                                                 }
879                                                 CTX_DATA_END;
880                                         }
881                                         tot_change++;
882                                 }
883                         }
884                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
885                                 
886                                 /* weak code here... (ton) */
887                                 if(obedit==ob) {
888                                         ListBase *editnurb= curve_get_editcurve(obedit);
889
890                                         nu1= editnurb->first;
891                                         cu= obedit->data;
892                                 }
893                                 else {
894                                         cu= ob->data;
895                                         nu1= cu->nurb.first;
896                                 }
897                                 
898                                 if (cu->id.lib) {
899                                         tot_lib_error++;
900                                 } else {
901                                         if(centermode==2) {
902                                                 VECCOPY(cent, give_cursor(scene, v3d));
903                                                 invert_m4_m4(ob->imat, ob->obmat);
904                                                 mul_m4_v3(ob->imat, cent);
905
906                                                 /* don't allow Z change if curve is 2D */
907                                                 if( !( cu->flag & CU_3D ) )
908                                                         cent[2] = 0.0;
909                                         } 
910                                         else {
911                                                 INIT_MINMAX(min, max);
912                                                 
913                                                 nu= nu1;
914                                                 while(nu) {
915                                                         minmaxNurb(nu, min, max);
916                                                         nu= nu->next;
917                                                 }
918                                                 
919                                                 cent[0]= (min[0]+max[0])/2.0f;
920                                                 cent[1]= (min[1]+max[1])/2.0f;
921                                                 cent[2]= (min[2]+max[2])/2.0f;
922                                         }
923                                         
924                                         nu= nu1;
925                                         while(nu) {
926                                                 if(nu->type == CU_BEZIER) {
927                                                         a= nu->pntsu;
928                                                         while (a--) {
929                                                                 sub_v3_v3v3(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
930                                                                 sub_v3_v3v3(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
931                                                                 sub_v3_v3v3(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
932                                                         }
933                                                 }
934                                                 else {
935                                                         a= nu->pntsu*nu->pntsv;
936                                                         while (a--)
937                                                                 sub_v3_v3v3(nu->bp[a].vec, nu->bp[a].vec, cent);
938                                                 }
939                                                 nu= nu->next;
940                                         }
941                         
942                                         if(centermode && obedit==NULL) {
943                                                 copy_m3_m4(omat, ob->obmat);
944                                                 
945                                                 mul_m3_v3(omat, cent);
946                                                 ob->loc[0]+= cent[0];
947                                                 ob->loc[1]+= cent[1];
948                                                 ob->loc[2]+= cent[2];
949                                                 
950                                                 where_is_object(scene, ob);
951                                                 ignore_parent_tx(bmain, scene, ob);
952                                         }
953                                         
954                                         tot_change++;
955                                         if(obedit) {
956                                                 if (centermode==0) {
957                                                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
958                                                 }
959                                                 break;
960                                         }
961                                 }
962                         }
963                         else if(ob->type==OB_FONT) {
964                                 /* get from bb */
965                                 
966                                 cu= ob->data;
967                                 
968                                 if(cu->bb==NULL) {
969                                         /* do nothing*/
970                                 } else if (cu->id.lib) {
971                                         tot_lib_error++;
972                                 } else {
973                                         cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
974                                         cu->yof= -0.5f -0.5f*( cu->bb->vec[0][1] - cu->bb->vec[2][1]);  /* extra 0.5 is the height o above line */
975                                         
976                                         /* not really ok, do this better once! */
977                                         cu->xof /= cu->fsize;
978                                         cu->yof /= cu->fsize;
979
980                                         tot_change++;
981                                 }
982                         }
983                         else if(ob->type==OB_ARMATURE) {
984                                 bArmature *arm = ob->data;
985                                 
986                                 if (arm->id.lib) {
987                                         tot_lib_error++;
988                                 } else if(arm->id.us>1) {
989                                         /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
990                                         return;*/
991                                         tot_multiuser_arm_error++;
992                                 } else {
993                                         /* Function to recenter armatures in editarmature.c 
994                                          * Bone + object locations are handled there.
995                                          */
996                                         docenter_armature(scene, v3d, ob, centermode);
997                                         tot_change++;
998                                         
999                                         where_is_object(scene, ob);
1000                                         ignore_parent_tx(bmain, scene, ob);
1001                                         
1002                                         if(obedit) 
1003                                                 break;
1004                                 }
1005                         }
1006                         ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
1007                 }
1008         }
1009         CTX_DATA_END;
1010         
1011         if (tot_change) {
1012                 DAG_ids_flush_update(0);
1013                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1014         }
1015         
1016         /* Warn if any errors occured */
1017         if (tot_lib_error+tot_multiuser_arm_error) {
1018                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);              
1019                 if (tot_lib_error)
1020                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
1021                 if (tot_multiuser_arm_error)
1022                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
1023         }
1024         
1025         return OPERATOR_FINISHED;
1026 }
1027
1028 void OBJECT_OT_origin_set(wmOperatorType *ot)
1029 {
1030         /* identifiers */
1031         ot->name= "Set Origin";
1032         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
1033         ot->idname= "OBJECT_OT_origin_set";
1034         
1035         /* api callbacks */
1036         ot->invoke= WM_menu_invoke;
1037         ot->exec= object_origin_set_exec;
1038         
1039         ot->poll= ED_operator_view3d_active;
1040         
1041         /* flags */
1042         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1043         
1044         RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
1045 }
1046