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