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