merge with trunk at r31523
[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 #include "DNA_group_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_editVert.h"
41 #include "BLI_listbase.h"
42
43 #include "BKE_context.h"
44 #include "BKE_curve.h"
45 #include "BKE_depsgraph.h"
46 #include "BKE_main.h"
47 #include "BKE_mesh.h"
48 #include "BKE_object.h"
49 #include "BKE_report.h"
50 #include "BKE_tessmesh.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_keyframing.h"
60 #include "ED_mesh.h"
61 #include "ED_screen.h"
62 #include "ED_view3d.h"
63
64 #include "object_intern.h"
65
66 /*************************** Clear Transformation ****************************/
67
68 static int object_location_clear_exec(bContext *C, wmOperator *op)
69 {
70         Main *bmain = CTX_data_main(C);
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(bmain, 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         Main *bmain= CTX_data_main(C);
130         Scene *scene= CTX_data_scene(C);
131         KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
132         
133         /* clear rotation of selected objects if not in weight-paint mode */
134         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
135                 if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
136                         /* clear rotations that aren't locked */
137                         if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
138                                 if (ob->protectflag & OB_LOCK_ROT4D) {
139                                         /* perform clamping on a component by component basis */
140                                         if (ob->rotmode == ROT_MODE_AXISANGLE) {
141                                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
142                                                         ob->rotAngle= 0.0f;
143                                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
144                                                         ob->rotAxis[0]= 0.0f;
145                                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
146                                                         ob->rotAxis[1]= 0.0f;
147                                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
148                                                         ob->rotAxis[2]= 0.0f;
149                                                         
150                                                 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
151                                                 if (IS_EQ(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQ(ob->rotAxis[1], ob->rotAxis[2]))
152                                                         ob->rotAxis[1] = 1.0f;
153                                         }
154                                         else if (ob->rotmode == ROT_MODE_QUAT) {
155                                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
156                                                         ob->quat[0]= 1.0f;
157                                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
158                                                         ob->quat[1]= 0.0f;
159                                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
160                                                         ob->quat[2]= 0.0f;
161                                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
162                                                         ob->quat[3]= 0.0f;
163                                         }
164                                         else {
165                                                 /* the flag may have been set for the other modes, so just ignore the extra flag... */
166                                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
167                                                         ob->rot[0]= 0.0f;
168                                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
169                                                         ob->rot[1]= 0.0f;
170                                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
171                                                         ob->rot[2]= 0.0f;
172                                         }
173                                 }
174                                 else {
175                                         /* perform clamping using euler form (3-components) */
176                                         float eul[3], oldeul[3], quat1[4] = {0};
177                                         
178                                         if (ob->rotmode == ROT_MODE_QUAT) {
179                                                 QUATCOPY(quat1, ob->quat);
180                                                 quat_to_eul( oldeul,ob->quat);
181                                         }
182                                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
183                                                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,ob->rotAxis, ob->rotAngle);
184                                         }
185                                         else {
186                                                 copy_v3_v3(oldeul, ob->rot);
187                                         }
188                                         
189                                         eul[0]= eul[1]= eul[2]= 0.0f;
190                                         
191                                         if (ob->protectflag & OB_LOCK_ROTX)
192                                                 eul[0]= oldeul[0];
193                                         if (ob->protectflag & OB_LOCK_ROTY)
194                                                 eul[1]= oldeul[1];
195                                         if (ob->protectflag & OB_LOCK_ROTZ)
196                                                 eul[2]= oldeul[2];
197                                         
198                                         if (ob->rotmode == ROT_MODE_QUAT) {
199                                                 eul_to_quat( ob->quat,eul);
200                                                 /* quaternions flip w sign to accumulate rotations correctly */
201                                                 if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
202                                                         mul_qt_fl(ob->quat, -1.0f);
203                                                 }
204                                         }
205                                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
206                                                 eulO_to_axis_angle( ob->rotAxis, &ob->rotAngle,eul, EULER_ORDER_DEFAULT);
207                                         }
208                                         else {
209                                                 copy_v3_v3(ob->rot, eul);
210                                         }
211                                 }
212                         }                                                // Duplicated in source/blender/editors/armature/editarmature.c
213                         else { 
214                                 if (ob->rotmode == ROT_MODE_QUAT) {
215                                         ob->quat[1]=ob->quat[2]=ob->quat[3]= 0.0f; 
216                                         ob->quat[0]= 1.0f;
217                                 }
218                                 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
219                                         /* by default, make rotation of 0 radians around y-axis (roll) */
220                                         ob->rotAxis[0]=ob->rotAxis[2]=ob->rotAngle= 0.0f;
221                                         ob->rotAxis[1]= 1.0f;
222                                 }
223                                 else {
224                                         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f;
225                                 }
226                         }
227                         
228                         /* auto keyframing */
229                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
230                                 ListBase dsources = {NULL, NULL};
231                                 
232                                 /* now insert the keyframe(s) using the Keying Set
233                                  *      1) add datasource override for the PoseChannel
234                                  *      2) insert keyframes
235                                  *      3) free the extra info 
236                                  */
237                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, NULL, NULL); 
238                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
239                                 BLI_freelistN(&dsources);
240                         }
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(bmain, 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         Main *bmain= CTX_data_main(C);
273         Scene *scene= CTX_data_scene(C);
274         KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Scaling");
275         
276         /* clear scales of selected objects if not in weight-paint mode */
277         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
278                 if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
279                         /* clear scale factors which are not locked */
280                         if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
281                                 ob->dsize[0]= 0.0f;
282                                 ob->size[0]= 1.0f;
283                         }
284                         if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
285                                 ob->dsize[1]= 0.0f;
286                                 ob->size[1]= 1.0f;
287                         }
288                         if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
289                                 ob->dsize[2]= 0.0f;
290                                 ob->size[2]= 1.0f;
291                         }
292                         
293                         /* auto keyframing */
294                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
295                                 ListBase dsources = {NULL, NULL};
296                                 
297                                 /* now insert the keyframe(s) using the Keying Set
298                                  *      1) add datasource override for the PoseChannel
299                                  *      2) insert keyframes
300                                  *      3) free the extra info 
301                                  */
302                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, NULL, NULL); 
303                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
304                                 BLI_freelistN(&dsources);
305                         }
306                 }
307                 ob->recalc |= OB_RECALC_OB;
308         }
309         CTX_DATA_END;
310         
311         /* this is needed so children are also updated */
312         DAG_ids_flush_update(bmain, 0);
313
314         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
315         
316         return OPERATOR_FINISHED;
317 }
318
319 void OBJECT_OT_scale_clear(wmOperatorType *ot)
320 {
321         /* identifiers */
322         ot->name= "Clear Scale";
323         ot->description = "Clear the object's scale";
324         ot->idname= "OBJECT_OT_scale_clear";
325         
326         /* api callbacks */
327         ot->exec= object_scale_clear_exec;
328         ot->poll= ED_operator_object_active_editable;
329         
330         /* flags */
331         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
332 }
333
334 static int object_origin_clear_exec(bContext *C, wmOperator *op)
335 {
336         Main *bmain= CTX_data_main(C);
337         float *v1, *v3, mat[3][3];
338         int armature_clear= 0;
339
340         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
341                 if(ob->parent) {
342                         v1= ob->loc;
343                         v3= ob->parentinv[3];
344                         
345                         copy_m3_m4(mat, ob->parentinv);
346                         negate_v3_v3(v3, v1);
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(bmain, 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 enum {
701         GEOMETRY_TO_ORIGIN=0,
702         ORIGIN_TO_GEOMETRY,
703         ORIGIN_TO_CURSOR
704 };
705
706 static int object_origin_set_exec(bContext *C, wmOperator *op)
707 {
708         Main *bmain= CTX_data_main(C);
709         Scene *scene= CTX_data_scene(C);
710         Object *obedit= CTX_data_edit_object(C);
711         Object *tob;
712         float cursor[3], cent[3], cent_neg[3], centn[3], min[3], max[3];
713         int centermode = RNA_enum_get(op->ptr, "type");
714         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
715
716         /* keep track of what is changed */
717         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
718
719         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
720                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
721                 return OPERATOR_CANCELLED;
722         }
723         else {
724                 /* get the view settings if 'around' isnt set and the view is available */
725                 View3D *v3d= CTX_wm_view3d(C);
726                 copy_v3_v3(cursor, give_cursor(scene, v3d));
727                 if(v3d && !RNA_property_is_set(op->ptr, "around"))
728                         around= v3d->around;
729         }
730         zero_v3(cent);
731
732         if(obedit) {
733             INIT_MINMAX(min, max);
734             
735             if(obedit->type==OB_MESH) {
736                         Mesh *me= obedit->data;
737                         BMEditMesh *em = me->edit_btmesh;
738                         BMVert *eve;
739                         BMIter iter;
740                         int total = 0;
741                         
742                         if(centermode == ORIGIN_TO_CURSOR) {
743                                 copy_v3_v3(cent, cursor);
744                                 invert_m4_m4(obedit->imat, obedit->obmat);
745                                 mul_m4_v3(obedit->imat, cent);
746                         } else {
747                                 BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
748                                         if(around==V3D_CENTROID) {
749                                                 total++;
750                                                 add_v3_v3(cent, eve->co);
751                                                 mul_v3_fl(cent, 1.0f/(float)total);
752                                         }
753                                         else {
754                                                 DO_MINMAX(eve->co, min, max);
755                                                 mid_v3_v3v3(cent, min, max);
756                                         }
757                                 }
758                         }
759                         
760                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
761                                 sub_v3_v3(eve->co, cent);
762                         }
763         
764                         EDBM_RecalcNormals(em);
765                         tot_change++;
766                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
767             }
768         }
769
770         /* reset flags */
771         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
772                         ob->flag &= ~OB_DONE;
773         }
774         CTX_DATA_END;
775
776         for (tob= bmain->object.first; tob; tob= tob->id.next) {
777                 if(tob->data)
778                         ((ID *)tob->data)->flag &= ~LIB_DOIT;
779                 if(tob->dup_group)
780                         ((ID *)tob->dup_group)->flag &= ~LIB_DOIT;
781         }
782
783         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
784                 if((ob->flag & OB_DONE)==0) {
785                         int do_inverse_offset = FALSE;
786                         ob->flag |= OB_DONE;
787
788                         if(centermode == ORIGIN_TO_CURSOR) {
789                                 copy_v3_v3(cent, cursor);
790                                 invert_m4_m4(ob->imat, ob->obmat);
791                                 mul_m4_v3(ob->imat, cent);
792                         }
793                         
794                         if(ob->data == NULL) {
795                                 /* special support for dupligroups */
796                                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.flag & LIB_DOIT)==0) {
797                                         if(ob->dup_group->id.lib) {
798                                                 tot_lib_error++;
799                                         }
800                                         else {
801                                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
802                                                 else {
803                                                         /* only bounds support */
804                                                         INIT_MINMAX(min, max);
805                                                         minmax_object_duplis(scene, ob, min, max);
806                                                         mid_v3_v3v3(cent, min, max);
807                                                         invert_m4_m4(ob->imat, ob->obmat);
808                                                         mul_m4_v3(ob->imat, cent);
809                                                 }
810                                                 
811                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
812
813                                                 tot_change++;
814                                                 ob->dup_group->id.flag |= LIB_DOIT;
815                                                 do_inverse_offset= TRUE;
816                                         }
817                                 }
818                         }
819                         else if (((ID *)ob->data)->lib) {
820                                 tot_lib_error++;
821                         }
822
823                         if(obedit==NULL && ob->type==OB_MESH) {
824                                 Mesh *me= ob->data;
825
826                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
827                                 else if(around==V3D_CENTROID) { mesh_center_median(me, cent); }
828                                 else { mesh_center_bounds(me, cent); }
829
830                                 negate_v3_v3(cent_neg, cent);
831                                 mesh_translate(me, cent_neg, 1);
832
833                                 tot_change++;
834                                 me->id.flag |= LIB_DOIT;
835                                 do_inverse_offset= TRUE;
836                         }
837                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
838                                 Curve *cu= ob->data;
839
840                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
841                                 else if(around==V3D_CENTROID) { curve_center_median(cu, cent); }
842                                 else { curve_center_bounds(cu, cent);   }
843
844                                 /* don't allow Z change if curve is 2D */
845                                 if((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
846                                         cent[2] = 0.0;
847
848                                 negate_v3_v3(cent_neg, cent);
849                                 curve_translate(cu, cent_neg, 1);
850
851                                 tot_change++;
852                                 cu->id.flag |= LIB_DOIT;
853                                 do_inverse_offset= TRUE;
854
855                                 if(obedit) {
856                                         if (centermode == GEOMETRY_TO_ORIGIN) {
857                                                 DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
858                                         }
859                                         break;
860                                 }
861                         }
862                         else if(ob->type==OB_FONT) {
863                                 /* get from bb */
864
865                                 Curve *cu= ob->data;
866
867                                 if(cu->bb==NULL && (centermode != ORIGIN_TO_CURSOR)) {
868                                         /* do nothing*/
869                                 }
870                                 else {
871                                         if(centermode == ORIGIN_TO_CURSOR) {
872                                                 /* done */
873                                         }
874                                         else {
875                                                 cent[0]= 0.5f * ( cu->bb->vec[4][0] + cu->bb->vec[0][0]);
876                                                 cent[1]= 0.5f * ( cu->bb->vec[0][1] + cu->bb->vec[2][1]) - 0.5f;        /* extra 0.5 is the height o above line */
877                                         }
878
879                                         cent[2]= 0.0f;
880
881                                         cu->xof= cu->xof - (cent[0] / cu->fsize);
882                                         cu->yof= cu->yof - (cent[1] / cu->fsize);
883
884                                         tot_change++;
885                                         cu->id.flag |= LIB_DOIT;
886                                         do_inverse_offset= TRUE;
887                                 }
888                         }
889                         else if(ob->type==OB_ARMATURE) {
890                                 bArmature *arm = ob->data;
891
892                                 if(ID_REAL_USERS(arm) > 1) {
893                                         /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
894                                         return;*/
895                                         tot_multiuser_arm_error++;
896                                 }
897                                 else {
898                                         /* Function to recenter armatures in editarmature.c
899                                          * Bone + object locations are handled there.
900                                          */
901                                         docenter_armature(scene, ob, cursor, centermode, around);
902
903                                         tot_change++;
904                                         arm->id.flag |= LIB_DOIT;
905                                         /* do_inverse_offset= TRUE; */ /* docenter_armature() handles this */
906
907                                         where_is_object(scene, ob);
908                                         ignore_parent_tx(bmain, scene, ob);
909
910                                         if(obedit)
911                                                 break;
912                                 }
913                         }
914
915                         /* offset other selected objects */
916                         if(do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
917                                 /* was the object data modified
918                                  * note: the functions above must set 'cent' */
919                                 copy_v3_v3(centn, cent);
920                                 mul_mat3_m4_v3(ob->obmat, centn); /* ommit translation part */
921                                 add_v3_v3(ob->loc, centn);
922
923                                 where_is_object(scene, ob);
924                                 ignore_parent_tx(bmain, scene, ob);
925                                 
926                                 /* other users? */
927                                 CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
928                                         if(             (ob_other->flag & OB_DONE)==0 &&
929                                                         (       (ob->data && (ob->data == ob_other->data)) ||
930                                                                 (ob->dup_group==ob_other->dup_group && (ob->transflag|ob_other->transflag) & OB_DUPLIGROUP) )
931                                         ) {
932                                                 ob_other->flag |= OB_DONE;
933                                                 ob_other->recalc= OB_RECALC_OB|OB_RECALC_DATA;
934
935                                                 copy_v3_v3(centn, cent);
936                                                 mul_mat3_m4_v3(ob_other->obmat, centn); /* ommit translation part */
937                                                 add_v3_v3(ob_other->loc, centn);
938
939                                                 where_is_object(scene, ob_other);
940                                                 ignore_parent_tx(bmain, scene, ob_other);
941                                         }
942                                 }
943                                 CTX_DATA_END;
944                         }
945                 }
946         }
947         CTX_DATA_END;
948
949         for (tob= bmain->object.first; tob; tob= tob->id.next) {
950                 if(tob->data && (((ID *)tob->data)->flag & LIB_DOIT)) {
951                         tob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
952                 }
953         }
954
955         if (tot_change) {
956                 DAG_ids_flush_update(bmain, 0);
957                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
958         }
959
960         /* Warn if any errors occurred */
961         if (tot_lib_error+tot_multiuser_arm_error) {
962                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);
963                 if (tot_lib_error)
964                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
965                 if (tot_multiuser_arm_error)
966                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
967         }
968
969         return OPERATOR_FINISHED;
970 }
971
972 void OBJECT_OT_origin_set(wmOperatorType *ot)
973 {
974         static EnumPropertyItem prop_set_center_types[] = {
975                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
976                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
977                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
978                 {0, NULL, 0, NULL, NULL}
979         };
980         
981         static EnumPropertyItem prop_set_bounds_types[] = {
982                 {V3D_CENTROID, "MEDIAN", 0, "Median Center", ""},
983                 {V3D_CENTER, "BOUNDS", 0, "Bounds Center", ""},
984                 {0, NULL, 0, NULL, NULL}
985         };
986         
987         /* identifiers */
988         ot->name= "Set Origin";
989         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
990         ot->idname= "OBJECT_OT_origin_set";
991         
992         /* api callbacks */
993         ot->invoke= WM_menu_invoke;
994         ot->exec= object_origin_set_exec;
995         
996         ot->poll= ED_operator_scene_editable;
997         
998         /* flags */
999         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1000         
1001         ot->prop= RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
1002         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_CENTROID, "Center", "");
1003 }
1004