COLLADA branch: merge from trunk -r 25745:26429.
[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(ID_REAL_USERS(me) > 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(ID_REAL_USERS(arm) > 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(ID_REAL_USERS(cu) > 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                                         }
510                                 }
511                                 else {
512                                         a= nu->pntsu*nu->pntsv;
513                                         for(bp= nu->bp; a--; bp++)
514                                                 mul_m4_v3(mat, bp->vec);
515                                 }
516                         }
517                 }
518                 else
519                         continue;
520
521                 if(apply_loc)
522                         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0f;
523                 if(apply_scale)
524                         ob->size[0]= ob->size[1]= ob->size[2]= 1.0f;
525                 if(apply_rot) {
526                         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f;
527                         ob->quat[1]= ob->quat[2]= ob->quat[3]= 0.0f;
528                         ob->rotAxis[0]= ob->rotAxis[2]= 0.0f;
529                         ob->rotAngle= 0.0f;
530                         
531                         ob->quat[0]= ob->rotAxis[1]= 1.0f;
532                 }
533
534                 where_is_object(scene, ob);
535                 ignore_parent_tx(bmain, scene, ob);
536
537                 DAG_id_flush_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
538
539                 change = 1;
540         }
541         CTX_DATA_END;
542
543         if(!change)
544                 return OPERATOR_CANCELLED;
545
546         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
547         return OPERATOR_FINISHED;
548 }
549
550 static int visual_transform_apply_exec(bContext *C, wmOperator *op)
551 {
552         Scene *scene= CTX_data_scene(C);
553         int change = 0;
554         
555         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
556                 where_is_object(scene, ob);
557                 object_apply_mat4(ob, ob->obmat);
558                 where_is_object(scene, ob);
559                 
560                 change = 1;
561         }
562         CTX_DATA_END;
563
564         if(!change)
565                 return OPERATOR_CANCELLED;
566
567         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
568         return OPERATOR_FINISHED;
569 }
570
571 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
572 {
573         /* identifiers */
574         ot->name= "Apply Visual Transform";
575         ot->description = "Apply the object's visual transformation to its data.";
576         ot->idname= "OBJECT_OT_visual_transform_apply";
577         
578         /* api callbacks */
579         ot->exec= visual_transform_apply_exec;
580         ot->poll= ED_operator_object_active_editable;
581         
582         /* flags */
583         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
584 }
585
586 static int location_apply_exec(bContext *C, wmOperator *op)
587 {
588         return apply_objects_internal(C, op->reports, 1, 0, 0);
589 }
590
591 void OBJECT_OT_location_apply(wmOperatorType *ot)
592 {
593         /* identifiers */
594         ot->name= "Apply Location";
595         ot->description = "Apply the object's location to its data.";
596         ot->idname= "OBJECT_OT_location_apply";
597         
598         /* api callbacks */
599         ot->exec= location_apply_exec;
600         ot->poll= ED_operator_object_active_editable;
601         
602         /* flags */
603         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
604 }
605
606 static int scale_apply_exec(bContext *C, wmOperator *op)
607 {
608         return apply_objects_internal(C, op->reports, 0, 1, 0);
609 }
610
611 void OBJECT_OT_scale_apply(wmOperatorType *ot)
612 {
613         /* identifiers */
614         ot->name= "Apply Scale";
615         ot->description = "Apply the object's scale to its data.";
616         ot->idname= "OBJECT_OT_scale_apply";
617         
618         /* api callbacks */
619         ot->exec= scale_apply_exec;
620         ot->poll= ED_operator_object_active_editable;
621         
622         /* flags */
623         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
624 }
625
626 static int rotation_apply_exec(bContext *C, wmOperator *op)
627 {
628         return apply_objects_internal(C, op->reports, 0, 0, 1);
629 }
630
631 void OBJECT_OT_rotation_apply(wmOperatorType *ot)
632 {
633         /* identifiers */
634         ot->name= "Apply Rotation";
635         ot->description = "Apply the object's rotation to its data.";
636         ot->idname= "OBJECT_OT_rotation_apply";
637         
638         /* api callbacks */
639         ot->exec= rotation_apply_exec;
640         ot->poll= ED_operator_object_active_editable;
641         
642         /* flags */
643         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
644 }
645
646 /************************ Texture Space Transform ****************************/
647
648 void texspace_edit(Scene *scene, View3D *v3d)
649 {
650         Base *base;
651         int nr=0;
652         
653         /* first test if from visible and selected objects
654          * texspacedraw is set:
655          */
656         
657         if(scene->obedit) return; // XXX get from context
658         
659         for(base= FIRSTBASE; base; base= base->next) {
660                 if(TESTBASELIB(v3d, base)) {
661                         break;
662                 }
663         }
664
665         if(base==0) {
666                 return;
667         }
668         
669         nr= 0; // XXX pupmenu("Texture Space %t|Grab/Move%x1|Size%x2");
670         if(nr<1) return;
671         
672         for(base= FIRSTBASE; base; base= base->next) {
673                 if(TESTBASELIB(v3d, base)) {
674                         base->object->dtx |= OB_TEXSPACE;
675                 }
676         }
677         
678
679         if(nr==1) {
680 // XXX          initTransform(TFM_TRANSLATION, CTX_TEXTURE);
681 // XXX          Transform();
682         }
683         else if(nr==2) {
684 // XXX          initTransform(TFM_RESIZE, CTX_TEXTURE);
685 // XXX          Transform();
686         }
687         else if(nr==3) {
688 // XXX          initTransform(TFM_ROTATION, CTX_TEXTURE);
689 // XXX          Transform();
690         }
691 }
692
693 /********************* Set Object Center ************************/
694
695 static EnumPropertyItem prop_set_center_types[] = {
696         {0, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
697         {1, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
698         {2, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
699         {0, NULL, 0, NULL, NULL}
700 };
701
702 /* 0 == do center, 1 == center new, 2 == center cursor */
703 static int object_origin_set_exec(bContext *C, wmOperator *op)
704 {
705         Main *bmain= CTX_data_main(C);
706         Scene *scene= CTX_data_scene(C);
707         ScrArea *sa= CTX_wm_area(C);
708         View3D *v3d= sa->spacedata.first;
709         Object *obedit= CTX_data_edit_object(C);
710         Mesh *me, *tme;
711         Curve *cu;
712 /*      BezTriple *bezt;
713         BPoint *bp; */
714         Nurb *nu, *nu1;
715         EditVert *eve;
716         float cent[3], centn[3], min[3], max[3], omat[3][3];
717         int a, total= 0;
718         int centermode = RNA_enum_get(op->ptr, "type");
719         
720         /* keep track of what is changed */
721         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
722         MVert *mvert;
723
724         if(scene->id.lib || v3d==NULL){
725                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed on Lib data");
726                  return OPERATOR_CANCELLED;
727         }
728         if (obedit && centermode > 0) {
729                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
730                 return OPERATOR_CANCELLED;
731         }       
732         cent[0]= cent[1]= cent[2]= 0.0; 
733         
734         if(obedit) {
735
736                 INIT_MINMAX(min, max);
737         
738                 if(obedit->type==OB_MESH) {
739                         Mesh *me= obedit->data;
740                         EditMesh *em = BKE_mesh_get_editmesh(me);
741
742                         for(eve= em->verts.first; eve; eve= eve->next) {
743                                 if(v3d->around==V3D_CENTROID) {
744                                         total++;
745                                         VECADD(cent, cent, eve->co);
746                                 }
747                                 else {
748                                         DO_MINMAX(eve->co, min, max);
749                                 }
750                         }
751                         
752                         if(v3d->around==V3D_CENTROID) {
753                                 mul_v3_fl(cent, 1.0f/(float)total);
754                         }
755                         else {
756                                 cent[0]= (min[0]+max[0])/2.0f;
757                                 cent[1]= (min[1]+max[1])/2.0f;
758                                 cent[2]= (min[2]+max[2])/2.0f;
759                         }
760                         
761                         for(eve= em->verts.first; eve; eve= eve->next) {
762                                 sub_v3_v3v3(eve->co, eve->co, cent);                    
763                         }
764                         
765                         recalc_editnormals(em);
766                         tot_change++;
767                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
768                         BKE_mesh_end_editmesh(me, em);
769                 }
770         }
771         
772         /* reset flags */
773         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
774                         ob->flag &= ~OB_DONE;
775         }
776         CTX_DATA_END;
777         
778         for (me= G.main->mesh.first; me; me= me->id.next) {
779                 me->flag &= ~ME_ISDONE;
780         }
781         
782         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
783                 if((ob->flag & OB_DONE)==0) {
784                         ob->flag |= OB_DONE;
785                                 
786                         if(obedit==NULL && (me=get_mesh(ob)) ) {
787                                 if (me->id.lib) {
788                                         tot_lib_error++;
789                                 } else {
790                                         if(centermode==2) {
791                                                 VECCOPY(cent, give_cursor(scene, v3d));
792                                                 invert_m4_m4(ob->imat, ob->obmat);
793                                                 mul_m4_v3(ob->imat, cent);
794                                         } else {
795                                                 INIT_MINMAX(min, max);
796                                                 mvert= me->mvert;
797                                                 for(a=0; a<me->totvert; a++, mvert++) {
798                                                         DO_MINMAX(mvert->co, min, max);
799                                                 }
800                                         
801                                                 cent[0]= (min[0]+max[0])/2.0f;
802                                                 cent[1]= (min[1]+max[1])/2.0f;
803                                                 cent[2]= (min[2]+max[2])/2.0f;
804                                         }
805
806                                         mvert= me->mvert;
807                                         for(a=0; a<me->totvert; a++, mvert++) {
808                                                 sub_v3_v3v3(mvert->co, mvert->co, cent);
809                                         }
810                                         
811                                         if (me->key) {
812                                                 KeyBlock *kb;
813                                                 for (kb=me->key->block.first; kb; kb=kb->next) {
814                                                         float *fp= kb->data;
815                                                         
816                                                         for (a=0; a<kb->totelem; a++, fp+=3) {
817                                                                 sub_v3_v3v3(fp, fp, cent);
818                                                         }
819                                                 }
820                                         }
821                                                 
822                                         me->flag |= ME_ISDONE;
823                                                 
824                                         if(centermode) {
825                                                 copy_m3_m4(omat, ob->obmat);
826                                                 
827                                                 VECCOPY(centn, cent);
828                                                 mul_m3_v3(omat, centn);
829                                                 ob->loc[0]+= centn[0];
830                                                 ob->loc[1]+= centn[1];
831                                                 ob->loc[2]+= centn[2];
832                                                 
833                                                 where_is_object(scene, ob);
834                                                 ignore_parent_tx(bmain, scene, ob);
835                                                 
836                                                 /* other users? */
837                                                 CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
838                                                         if((ob_other->flag & OB_DONE)==0) {
839                                                                 tme= get_mesh(ob_other);
840                                                                 
841                                                                 if(tme==me) {
842                                                                         
843                                                                         ob_other->flag |= OB_DONE;
844                                                                         ob_other->recalc= OB_RECALC_OB|OB_RECALC_DATA;
845
846                                                                         copy_m3_m4(omat, ob_other->obmat);
847                                                                         VECCOPY(centn, cent);
848                                                                         mul_m3_v3(omat, centn);
849                                                                         ob_other->loc[0]+= centn[0];
850                                                                         ob_other->loc[1]+= centn[1];
851                                                                         ob_other->loc[2]+= centn[2];
852                                                                         
853                                                                         where_is_object(scene, ob_other);
854                                                                         ignore_parent_tx(bmain, scene, ob_other);
855                                                                         
856                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
857                                                                                 mvert= tme->mvert;
858                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
859                                                                                         sub_v3_v3v3(mvert->co, mvert->co, cent);
860                                                                                 }
861                                                                                 
862                                                                                 if (tme->key) {
863                                                                                         KeyBlock *kb;
864                                                                                         for (kb=tme->key->block.first; kb; kb=kb->next) {
865                                                                                                 float *fp= kb->data;
866                                                                                                 
867                                                                                                 for (a=0; a<kb->totelem; a++, fp+=3) {
868                                                                                                         sub_v3_v3v3(fp, fp, cent);
869                                                                                                 }
870                                                                                         }
871                                                                                 }
872                                                                                 
873                                                                                 tme->flag |= ME_ISDONE;
874                                                                         }
875                                                                 }
876                                                         }
877                                                 }
878                                                 CTX_DATA_END;
879                                         }
880                                         tot_change++;
881                                 }
882                         }
883                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
884                                 
885                                 /* weak code here... (ton) */
886                                 if(obedit==ob) {
887                                         ListBase *editnurb= curve_get_editcurve(obedit);
888
889                                         nu1= editnurb->first;
890                                         cu= obedit->data;
891                                 }
892                                 else {
893                                         cu= ob->data;
894                                         nu1= cu->nurb.first;
895                                 }
896                                 
897                                 if (cu->id.lib) {
898                                         tot_lib_error++;
899                                 } else {
900                                         if(centermode==2) {
901                                                 VECCOPY(cent, give_cursor(scene, v3d));
902                                                 invert_m4_m4(ob->imat, ob->obmat);
903                                                 mul_m4_v3(ob->imat, cent);
904
905                                                 /* don't allow Z change if curve is 2D */
906                                                 if( !( cu->flag & CU_3D ) )
907                                                         cent[2] = 0.0;
908                                         } 
909                                         else {
910                                                 INIT_MINMAX(min, max);
911                                                 
912                                                 nu= nu1;
913                                                 while(nu) {
914                                                         minmaxNurb(nu, min, max);
915                                                         nu= nu->next;
916                                                 }
917                                                 
918                                                 cent[0]= (min[0]+max[0])/2.0f;
919                                                 cent[1]= (min[1]+max[1])/2.0f;
920                                                 cent[2]= (min[2]+max[2])/2.0f;
921                                         }
922                                         
923                                         nu= nu1;
924                                         while(nu) {
925                                                 if(nu->type == CU_BEZIER) {
926                                                         a= nu->pntsu;
927                                                         while (a--) {
928                                                                 sub_v3_v3v3(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
929                                                                 sub_v3_v3v3(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
930                                                                 sub_v3_v3v3(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
931                                                         }
932                                                 }
933                                                 else {
934                                                         a= nu->pntsu*nu->pntsv;
935                                                         while (a--)
936                                                                 sub_v3_v3v3(nu->bp[a].vec, nu->bp[a].vec, cent);
937                                                 }
938                                                 nu= nu->next;
939                                         }
940                         
941                                         if(centermode && obedit==NULL) {
942                                                 copy_m3_m4(omat, ob->obmat);
943                                                 
944                                                 mul_m3_v3(omat, cent);
945                                                 ob->loc[0]+= cent[0];
946                                                 ob->loc[1]+= cent[1];
947                                                 ob->loc[2]+= cent[2];
948                                                 
949                                                 where_is_object(scene, ob);
950                                                 ignore_parent_tx(bmain, scene, ob);
951                                         }
952                                         
953                                         tot_change++;
954                                         if(obedit) {
955                                                 if (centermode==0) {
956                                                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
957                                                 }
958                                                 break;
959                                         }
960                                 }
961                         }
962                         else if(ob->type==OB_FONT) {
963                                 /* get from bb */
964                                 
965                                 cu= ob->data;
966                                 
967                                 if(cu->bb==NULL) {
968                                         /* do nothing*/
969                                 } else if (cu->id.lib) {
970                                         tot_lib_error++;
971                                 } else {
972                                         cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
973                                         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 */
974                                         
975                                         /* not really ok, do this better once! */
976                                         cu->xof /= cu->fsize;
977                                         cu->yof /= cu->fsize;
978
979                                         tot_change++;
980                                 }
981                         }
982                         else if(ob->type==OB_ARMATURE) {
983                                 bArmature *arm = ob->data;
984                                 
985                                 if (arm->id.lib) {
986                                         tot_lib_error++;
987                                 } else if(ID_REAL_USERS(arm) > 1) {
988                                         /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
989                                         return;*/
990                                         tot_multiuser_arm_error++;
991                                 } else {
992                                         /* Function to recenter armatures in editarmature.c 
993                                          * Bone + object locations are handled there.
994                                          */
995                                         docenter_armature(scene, v3d, ob, centermode);
996                                         tot_change++;
997                                         
998                                         where_is_object(scene, ob);
999                                         ignore_parent_tx(bmain, scene, ob);
1000                                         
1001                                         if(obedit) 
1002                                                 break;
1003                                 }
1004                         }
1005                         ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
1006                 }
1007         }
1008         CTX_DATA_END;
1009         
1010         if (tot_change) {
1011                 DAG_ids_flush_update(0);
1012                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1013         }
1014         
1015         /* Warn if any errors occured */
1016         if (tot_lib_error+tot_multiuser_arm_error) {
1017                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);              
1018                 if (tot_lib_error)
1019                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
1020                 if (tot_multiuser_arm_error)
1021                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
1022         }
1023         
1024         return OPERATOR_FINISHED;
1025 }
1026
1027 void OBJECT_OT_origin_set(wmOperatorType *ot)
1028 {
1029         /* identifiers */
1030         ot->name= "Set Origin";
1031         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
1032         ot->idname= "OBJECT_OT_origin_set";
1033         
1034         /* api callbacks */
1035         ot->invoke= WM_menu_invoke;
1036         ot->exec= object_origin_set_exec;
1037         
1038         ot->poll= ED_operator_view3d_active;
1039         
1040         /* flags */
1041         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1042         
1043         ot->prop= RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
1044
1045 }
1046