=bmesh= merge from trunk at r36529
[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 /** \file blender/editors/object/object_transform.c
29  *  \ingroup edobj
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "DNA_anim_types.h"
37 #include "DNA_armature_types.h"
38 #include "DNA_key_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_group_types.h"
43
44 #include "BLI_math.h"
45 #include "BLI_editVert.h"
46 #include "BLI_listbase.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_context.h"
50 #include "BKE_curve.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_main.h"
53 #include "BKE_mesh.h"
54 #include "BKE_object.h"
55 #include "BKE_report.h"
56 #include "BKE_tessmesh.h"
57 #include "BKE_multires.h"
58 #include "BKE_armature.h"
59
60 #include "RNA_define.h"
61 #include "RNA_access.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "ED_armature.h"
67 #include "ED_keyframing.h"
68 #include "ED_mesh.h"
69 #include "ED_screen.h"
70 #include "ED_view3d.h"
71
72 #include "object_intern.h"
73
74 /*************************** Clear Transformation ****************************/
75
76 /* clear location of object */
77 static void object_clear_loc(Object *ob)
78 {
79         /* clear location if not locked */
80         if ((ob->protectflag & OB_LOCK_LOCX)==0)
81                 ob->loc[0]= ob->dloc[0]= 0.0f;
82         if ((ob->protectflag & OB_LOCK_LOCY)==0)
83                 ob->loc[1]= ob->dloc[1]= 0.0f;
84         if ((ob->protectflag & OB_LOCK_LOCZ)==0)
85                 ob->loc[2]= ob->dloc[2]= 0.0f;
86 }
87
88 /* clear rotation of object */
89 static void object_clear_rot(Object *ob)
90 {
91         /* clear rotations that aren't locked */
92         if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
93                 if (ob->protectflag & OB_LOCK_ROT4D) {
94                         /* perform clamping on a component by component basis */
95                         if (ob->rotmode == ROT_MODE_AXISANGLE) {
96                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
97                                         ob->rotAngle= ob->drotAngle= 0.0f;
98                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
99                                         ob->rotAxis[0]= ob->drotAxis[0]= 0.0f;
100                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
101                                         ob->rotAxis[1]= ob->drotAxis[1]= 0.0f;
102                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
103                                         ob->rotAxis[2]= ob->drotAxis[2]= 0.0f;
104                                         
105                                 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
106                                 if (IS_EQF(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQF(ob->rotAxis[1], ob->rotAxis[2]))
107                                         ob->rotAxis[1] = 1.0f;
108                                 if (IS_EQF(ob->drotAxis[0], ob->drotAxis[1]) && IS_EQF(ob->drotAxis[1], ob->drotAxis[2]))
109                                         ob->drotAxis[1]= 1.0f;
110                         }
111                         else if (ob->rotmode == ROT_MODE_QUAT) {
112                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
113                                         ob->quat[0]= ob->dquat[0]= 1.0f;
114                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
115                                         ob->quat[1]= ob->dquat[1]= 0.0f;
116                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
117                                         ob->quat[2]= ob->dquat[2]= 0.0f;
118                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
119                                         ob->quat[3]= ob->dquat[3]= 0.0f;
120                                         
121                                 // TODO: does this quat need normalising now?
122                         }
123                         else {
124                                 /* the flag may have been set for the other modes, so just ignore the extra flag... */
125                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
126                                         ob->rot[0]= ob->drot[0]= 0.0f;
127                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
128                                         ob->rot[1]= ob->drot[1]= 0.0f;
129                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
130                                         ob->rot[2]= ob->drot[2]= 0.0f;
131                         }
132                 }
133                 else {
134                         /* perform clamping using euler form (3-components) */
135                         // FIXME: deltas are not handled for these cases yet...
136                         float eul[3], oldeul[3], quat1[4] = {0};
137                         
138                         if (ob->rotmode == ROT_MODE_QUAT) {
139                                 QUATCOPY(quat1, ob->quat);
140                                 quat_to_eul(oldeul, ob->quat);
141                         }
142                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
143                                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, ob->rotAxis, ob->rotAngle);
144                         }
145                         else {
146                                 copy_v3_v3(oldeul, ob->rot);
147                         }
148                         
149                         eul[0]= eul[1]= eul[2]= 0.0f;
150                         
151                         if (ob->protectflag & OB_LOCK_ROTX)
152                                 eul[0]= oldeul[0];
153                         if (ob->protectflag & OB_LOCK_ROTY)
154                                 eul[1]= oldeul[1];
155                         if (ob->protectflag & OB_LOCK_ROTZ)
156                                 eul[2]= oldeul[2];
157                         
158                         if (ob->rotmode == ROT_MODE_QUAT) {
159                                 eul_to_quat(ob->quat, eul);
160                                 /* quaternions flip w sign to accumulate rotations correctly */
161                                 if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
162                                         mul_qt_fl(ob->quat, -1.0f);
163                                 }
164                         }
165                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
166                                 eulO_to_axis_angle(ob->rotAxis, &ob->rotAngle,eul, EULER_ORDER_DEFAULT);
167                         }
168                         else {
169                                 copy_v3_v3(ob->rot, eul);
170                         }
171                 }
172         }                                                // Duplicated in source/blender/editors/armature/editarmature.c
173         else { 
174                 if (ob->rotmode == ROT_MODE_QUAT) {
175                         unit_qt(ob->quat);
176                         unit_qt(ob->dquat);
177                 }
178                 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
179                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
180                         unit_axis_angle(ob->drotAxis, &ob->drotAngle);
181                 }
182                 else {
183                         zero_v3(ob->rot);
184                         zero_v3(ob->drot);
185                 }
186         }
187 }
188
189 /* clear scale of object */
190 static void object_clear_scale(Object *ob)
191 {
192         /* clear scale factors which are not locked */
193         if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
194                 ob->dsize[0]= 0.0f;
195                 ob->size[0]= 1.0f;
196         }
197         if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
198                 ob->dsize[1]= 0.0f;
199                 ob->size[1]= 1.0f;
200         }
201         if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
202                 ob->dsize[2]= 0.0f;
203                 ob->size[2]= 1.0f;
204         }
205 }
206
207 /* --------------- */
208
209 /* generic exec for clear-transform operators */
210 static int object_clear_transform_generic_exec(bContext *C, wmOperator *op, 
211                 void (*clear_func)(Object*), const char default_ksName[])
212 {
213         Main *bmain = CTX_data_main(C);
214         Scene *scene= CTX_data_scene(C);
215         KeyingSet *ks;
216         
217         /* sanity checks */
218         if ELEM(NULL, clear_func, default_ksName) {
219                 BKE_report(op->reports, RPT_ERROR, "Programming error: missing clear transform func or Keying Set Name");
220                 return OPERATOR_CANCELLED;
221         }
222         
223         /* get KeyingSet to use */
224         ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
225         
226         /* operate on selected objects only if they aren't in weight-paint mode 
227          * (so that object-transform clearing won't be applied at same time as bone-clearing)
228          */
229         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
230         {
231                 if (!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
232                         /* run provided clearing function */
233                         clear_func(ob);
234                         
235                         /* auto keyframing */
236                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
237                                 ListBase dsources = {NULL, NULL};
238                                 
239                                 /* now insert the keyframe(s) using the Keying Set
240                                  *      1) add datasource override for the Object
241                                  *      2) insert keyframes
242                                  *      3) free the extra info 
243                                  */
244                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, NULL, NULL); 
245                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
246                                 BLI_freelistN(&dsources);
247                         }
248                         
249                         /* tag for updates */
250                         ob->recalc |= OB_RECALC_OB;
251                 }
252         }
253         CTX_DATA_END;
254         
255         /* this is needed so children are also updated */
256         DAG_ids_flush_update(bmain, 0);
257
258         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
259
260         return OPERATOR_FINISHED;
261 }
262
263 /* --------------- */
264
265
266 static int object_location_clear_exec(bContext *C, wmOperator *op)
267 {
268         return object_clear_transform_generic_exec(C, op, object_clear_loc, "Location");
269 }
270
271 void OBJECT_OT_location_clear(wmOperatorType *ot)
272 {
273         /* identifiers */
274         ot->name= "Clear Location";
275         ot->description = "Clear the object's location";
276         ot->idname= "OBJECT_OT_location_clear";
277         
278         /* api callbacks */
279         ot->exec= object_location_clear_exec;
280         ot->poll= ED_operator_scene_editable;
281         
282         /* flags */
283         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
284 }
285
286 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
287 {
288         return object_clear_transform_generic_exec(C, op, object_clear_rot, "Rotation");
289 }
290
291 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
292 {
293         /* identifiers */
294         ot->name= "Clear Rotation";
295         ot->description = "Clear the object's rotation";
296         ot->idname= "OBJECT_OT_rotation_clear";
297         
298         /* api callbacks */
299         ot->exec= object_rotation_clear_exec;
300         ot->poll= ED_operator_scene_editable;
301         
302         /* flags */
303         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
304 }
305
306 static int object_scale_clear_exec(bContext *C, wmOperator *op)
307 {
308         return object_clear_transform_generic_exec(C, op, object_clear_scale, "Scaling");
309 }
310
311 void OBJECT_OT_scale_clear(wmOperatorType *ot)
312 {
313         /* identifiers */
314         ot->name= "Clear Scale";
315         ot->description = "Clear the object's scale";
316         ot->idname= "OBJECT_OT_scale_clear";
317         
318         /* api callbacks */
319         ot->exec= object_scale_clear_exec;
320         ot->poll= ED_operator_scene_editable;
321         
322         /* flags */
323         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
324 }
325
326 /* --------------- */
327
328 static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
329 {
330         Main *bmain= CTX_data_main(C);
331         float *v1, *v3;
332         float mat[3][3];
333
334         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
335         {
336                 if (ob->parent) {
337                         /* vectors pointed to by v1 and v3 will get modified */
338                         v1= ob->loc;
339                         v3= ob->parentinv[3];
340                         
341                         copy_m3_m4(mat, ob->parentinv);
342                         negate_v3_v3(v3, v1);
343                         mul_m3_v3(mat, v3);
344                 }
345                 ob->recalc |= OB_RECALC_OB;
346         }
347         CTX_DATA_END;
348
349         DAG_ids_flush_update(bmain, 0);
350         
351         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
352         
353         return OPERATOR_FINISHED;
354 }
355
356 void OBJECT_OT_origin_clear(wmOperatorType *ot)
357 {
358         /* identifiers */
359         ot->name= "Clear Origin";
360         ot->description = "Clear the object's origin";
361         ot->idname= "OBJECT_OT_origin_clear";
362         
363         /* api callbacks */
364         ot->exec= object_origin_clear_exec;
365         ot->poll= ED_operator_scene_editable;
366         
367         /* flags */
368         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
369 }
370
371 /*************************** Apply Transformation ****************************/
372
373 /* use this when the loc/size/rot of the parent has changed but the children
374  * should stay in the same place, e.g. for apply-size-rot or object center */
375 static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob ) 
376 {
377         Object workob;
378         Object *ob_child;
379         
380         /* a change was made, adjust the children to compensate */
381         for(ob_child=bmain->object.first; ob_child; ob_child=ob_child->id.next) {
382                 if(ob_child->parent == ob) {
383                         object_apply_mat4(ob_child, ob_child->obmat, TRUE, FALSE);
384                         what_does_parent(scene, ob_child, &workob);
385                         invert_m4_m4(ob_child->parentinv, workob.obmat);
386                 }
387         }
388 }
389
390 static int apply_objects_internal(bContext *C, ReportList *reports, int apply_loc, int apply_rot, int apply_scale)
391 {
392         Main *bmain= CTX_data_main(C);
393         Scene *scene= CTX_data_scene(C);
394         float rsmat[3][3], tmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
395         int a, change = 0;
396         
397         /* first check if we can execute */
398         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
399
400                 if(ob->type==OB_MESH) {
401                         if(ID_REAL_USERS(ob->data) > 1) {
402                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user mesh, doing nothing.");
403                                 return OPERATOR_CANCELLED;
404                         }
405                 }
406                 else if(ob->type==OB_ARMATURE) {
407                         if(ID_REAL_USERS(ob->data) > 1) {
408                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user armature, doing nothing.");
409                                 return OPERATOR_CANCELLED;
410                         }
411                 }
412                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
413                         Curve *cu;
414
415                         if(ID_REAL_USERS(ob->data) > 1) {
416                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user curve, doing nothing.");
417                                 return OPERATOR_CANCELLED;
418                         }
419
420                         cu= ob->data;
421
422                         if(!(cu->flag & CU_3D) && (apply_rot || apply_loc)) {
423                                 BKE_report(reports, RPT_ERROR, "Neither rotation nor location could be applied to a 2d curve, doing nothing.");
424                                 return OPERATOR_CANCELLED;
425                         }
426                         if(cu->key) {
427                                 BKE_report(reports, RPT_ERROR, "Can't apply to a curve with vertex keys, doing nothing.");
428                                 return OPERATOR_CANCELLED;
429                         }
430                 }
431         }
432         CTX_DATA_END;
433         
434         /* now execute */
435         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
436
437                 /* calculate rotation/scale matrix */
438                 if(apply_scale && apply_rot)
439                         object_to_mat3(ob, rsmat);
440                 else if(apply_scale)
441                         object_scale_to_mat3(ob, rsmat);
442                 else if(apply_rot) {
443                         float tmat[3][3], timat[3][3];
444
445                         /* simple rotation matrix */
446                         object_rot_to_mat3(ob, rsmat);
447
448                         /* correct for scale, note mul_m3_m3m3 has swapped args! */
449                         object_scale_to_mat3(ob, tmat);
450                         invert_m3_m3(timat, tmat);
451                         mul_m3_m3m3(rsmat, timat, rsmat);
452                         mul_m3_m3m3(rsmat, rsmat, tmat);
453                 }
454                 else
455                         unit_m3(rsmat);
456
457                 copy_m4_m3(mat, rsmat);
458
459                 /* calculate translation */
460                 if(apply_loc) {
461                         copy_v3_v3(mat[3], ob->loc);
462
463                         if(!(apply_scale && apply_rot)) {
464                                 /* correct for scale and rotation that is still applied */
465                                 object_to_mat3(ob, obmat);
466                                 invert_m3_m3(iobmat, obmat);
467                                 mul_m3_m3m3(tmat, rsmat, iobmat);
468                                 mul_m3_v3(tmat, mat[3]);
469                         }
470                 }
471
472                 /* apply to object data */
473                 if(ob->type==OB_MESH) {
474                         Mesh *me= ob->data;
475                         MVert *mvert;
476
477                         multiresModifier_scale_disp(scene, ob);
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->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, 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                         Curve *cu= ob->data;
503
504                         Nurb *nu;
505                         BPoint *bp;
506                         BezTriple *bezt;
507
508                         scale = mat3_to_scale(rsmat);
509
510                         for(nu=cu->nurb.first; nu; nu=nu->next) {
511                                 if(nu->type == CU_BEZIER) {
512                                         a= nu->pntsu;
513                                         for(bezt= nu->bezt; a--; bezt++) {
514                                                 mul_m4_v3(mat, bezt->vec[0]);
515                                                 mul_m4_v3(mat, bezt->vec[1]);
516                                                 mul_m4_v3(mat, bezt->vec[2]);
517                                                 bezt->radius *= scale;
518                                         }
519                                 }
520                                 else {
521                                         a= nu->pntsu*nu->pntsv;
522                                         for(bp= nu->bp; a--; bp++)
523                                                 mul_m4_v3(mat, bp->vec);
524                                 }
525                         }
526                 }
527                 else
528                         continue;
529
530                 if(apply_loc)
531                         zero_v3(ob->loc);
532                 if(apply_scale)
533                         ob->size[0]= ob->size[1]= ob->size[2]= 1.0f;
534                 if(apply_rot) {
535                         zero_v3(ob->rot);
536                         unit_qt(ob->quat);
537                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
538                 }
539
540                 where_is_object(scene, ob);
541                 if(ob->type==OB_ARMATURE) {
542                         where_is_pose(scene, ob); /* needed for bone parents */
543                 }
544
545                 ignore_parent_tx(bmain, scene, ob);
546
547                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
548
549                 change = 1;
550         }
551         CTX_DATA_END;
552
553         if(!change)
554                 return OPERATOR_CANCELLED;
555
556         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
557         return OPERATOR_FINISHED;
558 }
559
560 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
561 {
562         Scene *scene= CTX_data_scene(C);
563         int change = 0;
564         
565         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
566                 where_is_object(scene, ob);
567                 object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
568                 where_is_object(scene, ob);
569
570                 /* update for any children that may get moved */
571                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
572         
573                 change = 1;
574         }
575         CTX_DATA_END;
576
577         if(!change)
578                 return OPERATOR_CANCELLED;
579
580         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
581         return OPERATOR_FINISHED;
582 }
583
584 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
585 {
586         /* identifiers */
587         ot->name= "Apply Visual Transform";
588         ot->description = "Apply the object's visual transformation to its data";
589         ot->idname= "OBJECT_OT_visual_transform_apply";
590         
591         /* api callbacks */
592         ot->exec= visual_transform_apply_exec;
593         ot->poll= ED_operator_scene_editable;
594         
595         /* flags */
596         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
597 }
598
599 static int object_transform_apply_exec(bContext *C, wmOperator *op)
600 {
601         const int loc= RNA_boolean_get(op->ptr, "location");
602         const int rot= RNA_boolean_get(op->ptr, "rotation");
603         const int sca= RNA_boolean_get(op->ptr, "scale");
604
605         if(loc || rot || sca) {
606                 return apply_objects_internal(C, op->reports, loc, rot, sca);
607         }
608         else {
609                 return OPERATOR_CANCELLED;
610         }
611 }
612
613 void OBJECT_OT_transform_apply(wmOperatorType *ot)
614 {
615         /* identifiers */
616         ot->name= "Apply Object Transform";
617         ot->description = "Apply the object's transformation to its data";
618         ot->idname= "OBJECT_OT_transform_apply";
619
620         /* api callbacks */
621         ot->exec= object_transform_apply_exec;
622         ot->poll= ED_operator_objectmode;
623
624         /* flags */
625         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
626
627         RNA_def_boolean(ot->srna, "location", 0, "Location", "");
628         RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
629         RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
630 }
631
632 /********************* Set Object Center ************************/
633
634 enum {
635         GEOMETRY_TO_ORIGIN=0,
636         ORIGIN_TO_GEOMETRY,
637         ORIGIN_TO_CURSOR
638 };
639
640 static int object_origin_set_exec(bContext *C, wmOperator *op)
641 {
642         Main *bmain= CTX_data_main(C);
643         Scene *scene= CTX_data_scene(C);
644         Object *obedit= CTX_data_edit_object(C);
645         Object *tob;
646         float cursor[3], cent[3], cent_neg[3], centn[3], min[3], max[3];
647         int centermode = RNA_enum_get(op->ptr, "type");
648         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
649
650         /* keep track of what is changed */
651         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
652
653         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
654                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
655                 return OPERATOR_CANCELLED;
656         }
657         else {
658                 /* get the view settings if 'around' isnt set and the view is available */
659                 View3D *v3d= CTX_wm_view3d(C);
660                 copy_v3_v3(cursor, give_cursor(scene, v3d));
661                 if(v3d && !RNA_property_is_set(op->ptr, "around"))
662                         around= v3d->around;
663         }
664         zero_v3(cent);
665
666         if(obedit) {
667             INIT_MINMAX(min, max);
668             
669             if(obedit->type==OB_MESH) {
670                         Mesh *me= obedit->data;
671                         BMEditMesh *em = me->edit_btmesh;
672                         BMVert *eve;
673                         BMIter iter;
674                         int total = 0;
675                         
676                         if(centermode == ORIGIN_TO_CURSOR) {
677                                 copy_v3_v3(cent, cursor);
678                                 invert_m4_m4(obedit->imat, obedit->obmat);
679                                 mul_m4_v3(obedit->imat, cent);
680                         } else {
681                                 BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
682                                         if(around==V3D_CENTROID) {
683                                                 total++;
684                                                 add_v3_v3(cent, eve->co);
685                                                 mul_v3_fl(cent, 1.0f/(float)total);
686                                         }
687                                         else {
688                                                 DO_MINMAX(eve->co, min, max);
689                                                 mid_v3_v3v3(cent, min, max);
690                                         }
691                                 }
692                         }
693                         
694                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
695                                 sub_v3_v3(eve->co, cent);
696                         }
697         
698                         EDBM_RecalcNormals(em);
699                         tot_change++;
700                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
701             }
702         }
703
704         /* reset flags */
705         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
706                         ob->flag &= ~OB_DONE;
707         }
708         CTX_DATA_END;
709
710         for (tob= bmain->object.first; tob; tob= tob->id.next) {
711                 if(tob->data)
712                         ((ID *)tob->data)->flag &= ~LIB_DOIT;
713                 if(tob->dup_group)
714                         ((ID *)tob->dup_group)->flag &= ~LIB_DOIT;
715         }
716
717         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
718                 if((ob->flag & OB_DONE)==0) {
719                         int do_inverse_offset = FALSE;
720                         ob->flag |= OB_DONE;
721
722                         if(centermode == ORIGIN_TO_CURSOR) {
723                                 copy_v3_v3(cent, cursor);
724                                 invert_m4_m4(ob->imat, ob->obmat);
725                                 mul_m4_v3(ob->imat, cent);
726                         }
727                         
728                         if(ob->data == NULL) {
729                                 /* special support for dupligroups */
730                                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.flag & LIB_DOIT)==0) {
731                                         if(ob->dup_group->id.lib) {
732                                                 tot_lib_error++;
733                                         }
734                                         else {
735                                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
736                                                 else {
737                                                         /* only bounds support */
738                                                         INIT_MINMAX(min, max);
739                                                         minmax_object_duplis(scene, ob, min, max);
740                                                         mid_v3_v3v3(cent, min, max);
741                                                         invert_m4_m4(ob->imat, ob->obmat);
742                                                         mul_m4_v3(ob->imat, cent);
743                                                 }
744                                                 
745                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
746
747                                                 tot_change++;
748                                                 ob->dup_group->id.flag |= LIB_DOIT;
749                                                 do_inverse_offset= TRUE;
750                                         }
751                                 }
752                         }
753                         else if (((ID *)ob->data)->lib) {
754                                 tot_lib_error++;
755                         }
756
757                         if(obedit==NULL && ob->type==OB_MESH) {
758                                 Mesh *me= ob->data;
759
760                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
761                                 else if(around==V3D_CENTROID) { mesh_center_median(me, cent); }
762                                 else { mesh_center_bounds(me, cent); }
763
764                                 negate_v3_v3(cent_neg, cent);
765                                 mesh_translate(me, cent_neg, 1);
766
767                                 tot_change++;
768                                 me->id.flag |= LIB_DOIT;
769                                 do_inverse_offset= TRUE;
770                         }
771                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
772                                 Curve *cu= ob->data;
773
774                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
775                                 else if(around==V3D_CENTROID) { curve_center_median(cu, cent); }
776                                 else { curve_center_bounds(cu, cent);   }
777
778                                 /* don't allow Z change if curve is 2D */
779                                 if((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
780                                         cent[2] = 0.0;
781
782                                 negate_v3_v3(cent_neg, cent);
783                                 curve_translate(cu, cent_neg, 1);
784
785                                 tot_change++;
786                                 cu->id.flag |= LIB_DOIT;
787                                 do_inverse_offset= TRUE;
788
789                                 if(obedit) {
790                                         if (centermode == GEOMETRY_TO_ORIGIN) {
791                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
792                                         }
793                                         break;
794                                 }
795                         }
796                         else if(ob->type==OB_FONT) {
797                                 /* get from bb */
798
799                                 Curve *cu= ob->data;
800
801                                 if(cu->bb==NULL && (centermode != ORIGIN_TO_CURSOR)) {
802                                         /* do nothing*/
803                                 }
804                                 else {
805                                         if(centermode == ORIGIN_TO_CURSOR) {
806                                                 /* done */
807                                         }
808                                         else {
809                                                 cent[0]= 0.5f * ( cu->bb->vec[4][0] + cu->bb->vec[0][0]);
810                                                 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 */
811                                         }
812
813                                         cent[2]= 0.0f;
814
815                                         cu->xof= cu->xof - (cent[0] / cu->fsize);
816                                         cu->yof= cu->yof - (cent[1] / cu->fsize);
817
818                                         tot_change++;
819                                         cu->id.flag |= LIB_DOIT;
820                                         do_inverse_offset= TRUE;
821                                 }
822                         }
823                         else if(ob->type==OB_ARMATURE) {
824                                 bArmature *arm = ob->data;
825
826                                 if(ID_REAL_USERS(arm) > 1) {
827                                         /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
828                                         return;*/
829                                         tot_multiuser_arm_error++;
830                                 }
831                                 else {
832                                         /* Function to recenter armatures in editarmature.c
833                                          * Bone + object locations are handled there.
834                                          */
835                                         docenter_armature(scene, ob, cursor, centermode, around);
836
837                                         tot_change++;
838                                         arm->id.flag |= LIB_DOIT;
839                                         /* do_inverse_offset= TRUE; */ /* docenter_armature() handles this */
840
841                                         where_is_object(scene, ob);
842                                         where_is_pose(scene, ob); /* needed for bone parents */
843
844                                         ignore_parent_tx(bmain, scene, ob);
845
846                                         if(obedit)
847                                                 break;
848                                 }
849                         }
850
851                         /* offset other selected objects */
852                         if(do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
853                                 /* was the object data modified
854                                  * note: the functions above must set 'cent' */
855                                 copy_v3_v3(centn, cent);
856                                 mul_mat3_m4_v3(ob->obmat, centn); /* ommit translation part */
857                                 add_v3_v3(ob->loc, centn);
858
859                                 where_is_object(scene, ob);
860                                 if(ob->type==OB_ARMATURE) {
861                                         where_is_pose(scene, ob); /* needed for bone parents */
862                                 }
863
864                                 ignore_parent_tx(bmain, scene, ob);
865                                 
866                                 /* other users? */
867                                 CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
868                                         if(             (ob_other->flag & OB_DONE)==0 &&
869                                                         (       (ob->data && (ob->data == ob_other->data)) ||
870                                                                 (ob->dup_group==ob_other->dup_group && (ob->transflag|ob_other->transflag) & OB_DUPLIGROUP) )
871                                         ) {
872                                                 ob_other->flag |= OB_DONE;
873                                                 ob_other->recalc= OB_RECALC_OB|OB_RECALC_DATA;
874
875                                                 copy_v3_v3(centn, cent);
876                                                 mul_mat3_m4_v3(ob_other->obmat, centn); /* ommit translation part */
877                                                 add_v3_v3(ob_other->loc, centn);
878
879                                                 where_is_object(scene, ob_other);
880                                                 if(ob_other->type==OB_ARMATURE) {
881                                                         where_is_pose(scene, ob_other); /* needed for bone parents */
882                                                 }
883                                                 ignore_parent_tx(bmain, scene, ob_other);
884                                         }
885                                 }
886                                 CTX_DATA_END;
887                         }
888                 }
889         }
890         CTX_DATA_END;
891
892         for (tob= bmain->object.first; tob; tob= tob->id.next) {
893                 if(tob->data && (((ID *)tob->data)->flag & LIB_DOIT)) {
894                         tob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
895                 }
896         }
897
898         if (tot_change) {
899                 DAG_ids_flush_update(bmain, 0);
900                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
901         }
902
903         /* Warn if any errors occurred */
904         if (tot_lib_error+tot_multiuser_arm_error) {
905                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);
906                 if (tot_lib_error)
907                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
908                 if (tot_multiuser_arm_error)
909                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
910         }
911
912         return OPERATOR_FINISHED;
913 }
914
915 void OBJECT_OT_origin_set(wmOperatorType *ot)
916 {
917         static EnumPropertyItem prop_set_center_types[] = {
918                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
919                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
920                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
921                 {0, NULL, 0, NULL, NULL}
922         };
923         
924         static EnumPropertyItem prop_set_bounds_types[] = {
925                 {V3D_CENTROID, "MEDIAN", 0, "Median Center", ""},
926                 {V3D_CENTER, "BOUNDS", 0, "Bounds Center", ""},
927                 {0, NULL, 0, NULL, NULL}
928         };
929         
930         /* identifiers */
931         ot->name= "Set Origin";
932         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
933         ot->idname= "OBJECT_OT_origin_set";
934         
935         /* api callbacks */
936         ot->invoke= WM_menu_invoke;
937         ot->exec= object_origin_set_exec;
938         
939         ot->poll= ED_operator_scene_editable;
940         
941         /* flags */
942         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
943         
944         ot->prop= RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
945         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_CENTROID, "Center", "");
946 }
947