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