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