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