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