svn merge ^/trunk/blender -r41200:41226 .
[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                         ED_autokeyframe_object(C, scene, ob, ks);
236
237                         /* tag for updates */
238                         DAG_id_tag_update(&ob->id, OB_RECALC_OB);
239                 }
240         }
241         CTX_DATA_END;
242         
243         /* this is needed so children are also updated */
244         DAG_ids_flush_update(bmain, 0);
245
246         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
247
248         return OPERATOR_FINISHED;
249 }
250
251 /* --------------- */
252
253
254 static int object_location_clear_exec(bContext *C, wmOperator *op)
255 {
256         return object_clear_transform_generic_exec(C, op, object_clear_loc, ANIM_KS_LOCATION_ID);
257 }
258
259 void OBJECT_OT_location_clear(wmOperatorType *ot)
260 {
261         /* identifiers */
262         ot->name= "Clear Location";
263         ot->description = "Clear the object's location";
264         ot->idname= "OBJECT_OT_location_clear";
265         
266         /* api callbacks */
267         ot->exec= object_location_clear_exec;
268         ot->poll= ED_operator_scene_editable;
269         
270         /* flags */
271         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
272 }
273
274 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
275 {
276         return object_clear_transform_generic_exec(C, op, object_clear_rot, ANIM_KS_ROTATION_ID);
277 }
278
279 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
280 {
281         /* identifiers */
282         ot->name= "Clear Rotation";
283         ot->description = "Clear the object's rotation";
284         ot->idname= "OBJECT_OT_rotation_clear";
285         
286         /* api callbacks */
287         ot->exec= object_rotation_clear_exec;
288         ot->poll= ED_operator_scene_editable;
289         
290         /* flags */
291         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
292 }
293
294 static int object_scale_clear_exec(bContext *C, wmOperator *op)
295 {
296         return object_clear_transform_generic_exec(C, op, object_clear_scale, ANIM_KS_SCALING_ID);
297 }
298
299 void OBJECT_OT_scale_clear(wmOperatorType *ot)
300 {
301         /* identifiers */
302         ot->name= "Clear Scale";
303         ot->description = "Clear the object's scale";
304         ot->idname= "OBJECT_OT_scale_clear";
305         
306         /* api callbacks */
307         ot->exec= object_scale_clear_exec;
308         ot->poll= ED_operator_scene_editable;
309         
310         /* flags */
311         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
312 }
313
314 /* --------------- */
315
316 static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
317 {
318         Main *bmain= CTX_data_main(C);
319         float *v1, *v3;
320         float mat[3][3];
321
322         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
323         {
324                 if (ob->parent) {
325                         /* vectors pointed to by v1 and v3 will get modified */
326                         v1= ob->loc;
327                         v3= ob->parentinv[3];
328                         
329                         copy_m3_m4(mat, ob->parentinv);
330                         negate_v3_v3(v3, v1);
331                         mul_m3_v3(mat, v3);
332                 }
333
334                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
335         }
336         CTX_DATA_END;
337
338         DAG_ids_flush_update(bmain, 0);
339         
340         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
341         
342         return OPERATOR_FINISHED;
343 }
344
345 void OBJECT_OT_origin_clear(wmOperatorType *ot)
346 {
347         /* identifiers */
348         ot->name= "Clear Origin";
349         ot->description = "Clear the object's origin";
350         ot->idname= "OBJECT_OT_origin_clear";
351         
352         /* api callbacks */
353         ot->exec= object_origin_clear_exec;
354         ot->poll= ED_operator_scene_editable;
355         
356         /* flags */
357         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
358 }
359
360 /*************************** Apply Transformation ****************************/
361
362 /* use this when the loc/size/rot of the parent has changed but the children
363  * should stay in the same place, e.g. for apply-size-rot or object center */
364 static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob ) 
365 {
366         Object workob;
367         Object *ob_child;
368         
369         /* a change was made, adjust the children to compensate */
370         for(ob_child=bmain->object.first; ob_child; ob_child=ob_child->id.next) {
371                 if(ob_child->parent == ob) {
372                         object_apply_mat4(ob_child, ob_child->obmat, TRUE, FALSE);
373                         what_does_parent(scene, ob_child, &workob);
374                         invert_m4_m4(ob_child->parentinv, workob.obmat);
375                 }
376         }
377 }
378
379 static int apply_objects_internal(bContext *C, ReportList *reports, int apply_loc, int apply_rot, int apply_scale)
380 {
381         Main *bmain= CTX_data_main(C);
382         Scene *scene= CTX_data_scene(C);
383         float rsmat[3][3], tmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
384         int a, change = 0;
385         
386         /* first check if we can execute */
387         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
388
389                 if(ob->type==OB_MESH) {
390                         if(ID_REAL_USERS(ob->data) > 1) {
391                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user mesh, doing nothing");
392                                 return OPERATOR_CANCELLED;
393                         }
394                 }
395                 else if(ob->type==OB_ARMATURE) {
396                         if(ID_REAL_USERS(ob->data) > 1) {
397                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user armature, doing nothing");
398                                 return OPERATOR_CANCELLED;
399                         }
400                 }
401                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
402                         Curve *cu;
403
404                         if(ID_REAL_USERS(ob->data) > 1) {
405                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user curve, doing nothing");
406                                 return OPERATOR_CANCELLED;
407                         }
408
409                         cu= ob->data;
410
411                         if(!(cu->flag & CU_3D) && (apply_rot || apply_loc)) {
412                                 BKE_report(reports, RPT_ERROR, "Neither rotation nor location could be applied to a 2d curve, doing nothing");
413                                 return OPERATOR_CANCELLED;
414                         }
415                         if(cu->key) {
416                                 BKE_report(reports, RPT_ERROR, "Can't apply to a curve with vertex keys, doing nothing");
417                                 return OPERATOR_CANCELLED;
418                         }
419                 }
420         }
421         CTX_DATA_END;
422         
423         /* now execute */
424         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
425
426                 /* calculate rotation/scale matrix */
427                 if(apply_scale && apply_rot)
428                         object_to_mat3(ob, rsmat);
429                 else if(apply_scale)
430                         object_scale_to_mat3(ob, rsmat);
431                 else if(apply_rot) {
432                         float tmat[3][3], timat[3][3];
433
434                         /* simple rotation matrix */
435                         object_rot_to_mat3(ob, rsmat);
436
437                         /* correct for scale, note mul_m3_m3m3 has swapped args! */
438                         object_scale_to_mat3(ob, tmat);
439                         invert_m3_m3(timat, tmat);
440                         mul_m3_m3m3(rsmat, timat, rsmat);
441                         mul_m3_m3m3(rsmat, rsmat, tmat);
442                 }
443                 else
444                         unit_m3(rsmat);
445
446                 copy_m4_m3(mat, rsmat);
447
448                 /* calculate translation */
449                 if(apply_loc) {
450                         copy_v3_v3(mat[3], ob->loc);
451
452                         if(!(apply_scale && apply_rot)) {
453                                 /* correct for scale and rotation that is still applied */
454                                 object_to_mat3(ob, obmat);
455                                 invert_m3_m3(iobmat, obmat);
456                                 mul_m3_m3m3(tmat, rsmat, iobmat);
457                                 mul_m3_v3(tmat, mat[3]);
458                         }
459                 }
460
461                 /* apply to object data */
462                 if(ob->type==OB_MESH) {
463                         Mesh *me= ob->data;
464                         MVert *mvert;
465
466                         multiresModifier_scale_disp(scene, ob);
467                         
468                         /* adjust data */
469                         mvert= me->mvert;
470                         for(a=0; a<me->totvert; a++, mvert++)
471                                 mul_m4_v3(mat, mvert->co);
472                         
473                         if(me->key) {
474                                 KeyBlock *kb;
475                                 
476                                 for(kb=me->key->block.first; kb; kb=kb->next) {
477                                         float *fp= kb->data;
478                                         
479                                         for(a=0; a<kb->totelem; a++, fp+=3)
480                                                 mul_m4_v3(mat, fp);
481                                 }
482                         }
483                         
484                         /* update normals */
485                         mesh_calc_normals(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
486                 }
487                 else if (ob->type==OB_ARMATURE) {
488                         ED_armature_apply_transform(ob, mat);
489                 }
490                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
491                         Curve *cu= ob->data;
492
493                         Nurb *nu;
494                         BPoint *bp;
495                         BezTriple *bezt;
496
497                         scale = mat3_to_scale(rsmat);
498
499                         for(nu=cu->nurb.first; nu; nu=nu->next) {
500                                 if(nu->type == CU_BEZIER) {
501                                         a= nu->pntsu;
502                                         for(bezt= nu->bezt; a--; bezt++) {
503                                                 mul_m4_v3(mat, bezt->vec[0]);
504                                                 mul_m4_v3(mat, bezt->vec[1]);
505                                                 mul_m4_v3(mat, bezt->vec[2]);
506                                                 bezt->radius *= scale;
507                                         }
508                                         calchandlesNurb(nu);
509                                 }
510                                 else {
511                                         a= nu->pntsu*nu->pntsv;
512                                         for(bp= nu->bp; a--; bp++)
513                                                 mul_m4_v3(mat, bp->vec);
514                                 }
515                         }
516                 }
517                 else
518                         continue;
519
520                 if(apply_loc)
521                         zero_v3(ob->loc);
522                 if(apply_scale)
523                         ob->size[0]= ob->size[1]= ob->size[2]= 1.0f;
524                 if(apply_rot) {
525                         zero_v3(ob->rot);
526                         unit_qt(ob->quat);
527                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
528                 }
529
530                 where_is_object(scene, ob);
531                 if(ob->type==OB_ARMATURE) {
532                         where_is_pose(scene, ob); /* needed for bone parents */
533                 }
534
535                 ignore_parent_tx(bmain, scene, ob);
536
537                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
538
539                 change = 1;
540         }
541         CTX_DATA_END;
542
543         if(!change)
544                 return OPERATOR_CANCELLED;
545
546         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
547         return OPERATOR_FINISHED;
548 }
549
550 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
551 {
552         Scene *scene= CTX_data_scene(C);
553         int change = 0;
554         
555         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
556                 where_is_object(scene, ob);
557                 object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
558                 where_is_object(scene, ob);
559
560                 /* update for any children that may get moved */
561                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
562         
563                 change = 1;
564         }
565         CTX_DATA_END;
566
567         if(!change)
568                 return OPERATOR_CANCELLED;
569
570         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
571         return OPERATOR_FINISHED;
572 }
573
574 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
575 {
576         /* identifiers */
577         ot->name= "Apply Visual Transform";
578         ot->description = "Apply the object's visual transformation to its data";
579         ot->idname= "OBJECT_OT_visual_transform_apply";
580         
581         /* api callbacks */
582         ot->exec= visual_transform_apply_exec;
583         ot->poll= ED_operator_scene_editable;
584         
585         /* flags */
586         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
587 }
588
589 static int object_transform_apply_exec(bContext *C, wmOperator *op)
590 {
591         const int loc= RNA_boolean_get(op->ptr, "location");
592         const int rot= RNA_boolean_get(op->ptr, "rotation");
593         const int sca= RNA_boolean_get(op->ptr, "scale");
594
595         if(loc || rot || sca) {
596                 return apply_objects_internal(C, op->reports, loc, rot, sca);
597         }
598         else {
599                 return OPERATOR_CANCELLED;
600         }
601 }
602
603 void OBJECT_OT_transform_apply(wmOperatorType *ot)
604 {
605         /* identifiers */
606         ot->name= "Apply Object Transform";
607         ot->description = "Apply the object's transformation to its data";
608         ot->idname= "OBJECT_OT_transform_apply";
609
610         /* api callbacks */
611         ot->exec= object_transform_apply_exec;
612         ot->poll= ED_operator_objectmode;
613
614         /* flags */
615         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
616
617         RNA_def_boolean(ot->srna, "location", 0, "Location", "");
618         RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
619         RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
620 }
621
622 /********************* Set Object Center ************************/
623
624 enum {
625         GEOMETRY_TO_ORIGIN=0,
626         ORIGIN_TO_GEOMETRY,
627         ORIGIN_TO_CURSOR
628 };
629
630 static int object_origin_set_exec(bContext *C, wmOperator *op)
631 {
632         Main *bmain= CTX_data_main(C);
633         Scene *scene= CTX_data_scene(C);
634         Object *obedit= CTX_data_edit_object(C);
635         Object *tob;
636         float cursor[3], cent[3], cent_neg[3], centn[3], min[3], max[3];
637         int centermode = RNA_enum_get(op->ptr, "type");
638         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
639
640         /* keep track of what is changed */
641         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
642
643         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
644                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
645                 return OPERATOR_CANCELLED;
646         }
647         else {
648                 /* get the view settings if 'around' isnt set and the view is available */
649                 View3D *v3d= CTX_wm_view3d(C);
650                 copy_v3_v3(cursor, give_cursor(scene, v3d));
651                 if(v3d && !RNA_property_is_set(op->ptr, "around"))
652                         around= v3d->around;
653         }
654
655         zero_v3(cent);
656
657         if(obedit) {
658                 INIT_MINMAX(min, max);
659
660                 if(obedit->type==OB_MESH) {
661                         Mesh *me= obedit->data;
662                         BMEditMesh *em = me->edit_btmesh;
663                         BMVert *eve;
664                         BMIter iter;
665                         int total = 0;
666                         
667                         if(centermode == ORIGIN_TO_CURSOR) {
668                                 copy_v3_v3(cent, cursor);
669                                 invert_m4_m4(obedit->imat, obedit->obmat);
670                                 mul_m4_v3(obedit->imat, cent);
671                         } else {
672                                 BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
673                                         if(around==V3D_CENTROID) {
674                                                 total++;
675                                                 add_v3_v3(cent, eve->co);
676                                                 mul_v3_fl(cent, 1.0f/(float)total);
677                                         }
678                                         else {
679                                                 DO_MINMAX(eve->co, min, max);
680                                                 mid_v3_v3v3(cent, min, max);
681                                         }
682                                 }
683                         }
684                         
685                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
686                                 sub_v3_v3(eve->co, cent);
687                         }
688
689                         EDBM_RecalcNormals(em);
690                         tot_change++;
691                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
692             }
693         }
694
695         /* reset flags */
696         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
697                         ob->flag &= ~OB_DONE;
698         }
699         CTX_DATA_END;
700
701         for (tob= bmain->object.first; tob; tob= tob->id.next) {
702                 if(tob->data)
703                         ((ID *)tob->data)->flag &= ~LIB_DOIT;
704                 if(tob->dup_group)
705                         ((ID *)tob->dup_group)->flag &= ~LIB_DOIT;
706         }
707
708         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
709                 if((ob->flag & OB_DONE)==0) {
710                         int do_inverse_offset = FALSE;
711                         ob->flag |= OB_DONE;
712
713                         if(centermode == ORIGIN_TO_CURSOR) {
714                                 copy_v3_v3(cent, cursor);
715                                 invert_m4_m4(ob->imat, ob->obmat);
716                                 mul_m4_v3(ob->imat, cent);
717                         }
718                         
719                         if(ob->data == NULL) {
720                                 /* special support for dupligroups */
721                                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.flag & LIB_DOIT)==0) {
722                                         if(ob->dup_group->id.lib) {
723                                                 tot_lib_error++;
724                                         }
725                                         else {
726                                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
727                                                 else {
728                                                         /* only bounds support */
729                                                         INIT_MINMAX(min, max);
730                                                         minmax_object_duplis(scene, ob, min, max);
731                                                         mid_v3_v3v3(cent, min, max);
732                                                         invert_m4_m4(ob->imat, ob->obmat);
733                                                         mul_m4_v3(ob->imat, cent);
734                                                 }
735                                                 
736                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
737
738                                                 tot_change++;
739                                                 ob->dup_group->id.flag |= LIB_DOIT;
740                                                 do_inverse_offset= TRUE;
741                                         }
742                                 }
743                         }
744                         else if (((ID *)ob->data)->lib) {
745                                 tot_lib_error++;
746                         }
747
748                         if(obedit==NULL && ob->type==OB_MESH) {
749                                 Mesh *me= ob->data;
750
751                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
752                                 else if(around==V3D_CENTROID) { mesh_center_median(me, cent); }
753                                 else { mesh_center_bounds(me, cent); }
754
755                                 negate_v3_v3(cent_neg, cent);
756                                 mesh_translate(me, cent_neg, 1);
757
758                                 tot_change++;
759                                 me->id.flag |= LIB_DOIT;
760                                 do_inverse_offset= TRUE;
761                         }
762                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
763                                 Curve *cu= ob->data;
764
765                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
766                                 else if(around==V3D_CENTROID) { curve_center_median(cu, cent); }
767                                 else { curve_center_bounds(cu, cent);   }
768
769                                 /* don't allow Z change if curve is 2D */
770                                 if((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
771                                         cent[2] = 0.0;
772
773                                 negate_v3_v3(cent_neg, cent);
774                                 curve_translate(cu, cent_neg, 1);
775
776                                 tot_change++;
777                                 cu->id.flag |= LIB_DOIT;
778                                 do_inverse_offset= TRUE;
779
780                                 if(obedit) {
781                                         if (centermode == GEOMETRY_TO_ORIGIN) {
782                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
783                                         }
784                                         break;
785                                 }
786                         }
787                         else if(ob->type==OB_FONT) {
788                                 /* get from bb */
789
790                                 Curve *cu= ob->data;
791
792                                 if(cu->bb==NULL && (centermode != ORIGIN_TO_CURSOR)) {
793                                         /* do nothing*/
794                                 }
795                                 else {
796                                         if(centermode == ORIGIN_TO_CURSOR) {
797                                                 /* done */
798                                         }
799                                         else {
800                                                 cent[0]= 0.5f * ( cu->bb->vec[4][0] + cu->bb->vec[0][0]);
801                                                 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 */
802                                         }
803
804                                         cent[2]= 0.0f;
805
806                                         cu->xof= cu->xof - (cent[0] / cu->fsize);
807                                         cu->yof= cu->yof - (cent[1] / cu->fsize);
808
809                                         tot_change++;
810                                         cu->id.flag |= LIB_DOIT;
811                                         do_inverse_offset= TRUE;
812                                 }
813                         }
814                         else if(ob->type==OB_ARMATURE) {
815                                 bArmature *arm = ob->data;
816
817                                 if(ID_REAL_USERS(arm) > 1) {
818                                         /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
819                                         return;*/
820                                         tot_multiuser_arm_error++;
821                                 }
822                                 else {
823                                         /* Function to recenter armatures in editarmature.c
824                                          * Bone + object locations are handled there.
825                                          */
826                                         docenter_armature(scene, ob, cursor, centermode, around);
827
828                                         tot_change++;
829                                         arm->id.flag |= LIB_DOIT;
830                                         /* do_inverse_offset= TRUE; */ /* docenter_armature() handles this */
831
832                                         where_is_object(scene, ob);
833                                         where_is_pose(scene, ob); /* needed for bone parents */
834
835                                         ignore_parent_tx(bmain, scene, ob);
836
837                                         if(obedit)
838                                                 break;
839                                 }
840                         }
841
842                         /* offset other selected objects */
843                         if(do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
844                                 /* was the object data modified
845                                  * note: the functions above must set 'cent' */
846                                 copy_v3_v3(centn, cent);
847                                 mul_mat3_m4_v3(ob->obmat, centn); /* ommit translation part */
848                                 add_v3_v3(ob->loc, centn);
849
850                                 where_is_object(scene, ob);
851                                 if(ob->type==OB_ARMATURE) {
852                                         where_is_pose(scene, ob); /* needed for bone parents */
853                                 }
854
855                                 ignore_parent_tx(bmain, scene, ob);
856                                 
857                                 /* other users? */
858                                 CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
859                                         if(             (ob_other->flag & OB_DONE)==0 &&
860                                                         (       (ob->data && (ob->data == ob_other->data)) ||
861                                                                 (ob->dup_group==ob_other->dup_group && (ob->transflag|ob_other->transflag) & OB_DUPLIGROUP) )
862                                         ) {
863                                                 ob_other->flag |= OB_DONE;
864                                                 DAG_id_tag_update(&ob_other->id, OB_RECALC_OB|OB_RECALC_DATA);
865
866                                                 copy_v3_v3(centn, cent);
867                                                 mul_mat3_m4_v3(ob_other->obmat, centn); /* ommit translation part */
868                                                 add_v3_v3(ob_other->loc, centn);
869
870                                                 where_is_object(scene, ob_other);
871                                                 if(ob_other->type==OB_ARMATURE) {
872                                                         where_is_pose(scene, ob_other); /* needed for bone parents */
873                                                 }
874                                                 ignore_parent_tx(bmain, scene, ob_other);
875                                         }
876                                 }
877                                 CTX_DATA_END;
878                         }
879                 }
880         }
881         CTX_DATA_END;
882
883         for (tob= bmain->object.first; tob; tob= tob->id.next)
884                 if(tob->data && (((ID *)tob->data)->flag & LIB_DOIT))
885                         DAG_id_tag_update(&tob->id, OB_RECALC_OB|OB_RECALC_DATA);
886
887         if (tot_change) {
888                 DAG_ids_flush_update(bmain, 0);
889                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
890         }
891
892         /* Warn if any errors occurred */
893         if (tot_lib_error+tot_multiuser_arm_error) {
894                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);
895                 if (tot_lib_error)
896                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
897                 if (tot_multiuser_arm_error)
898                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
899         }
900
901         return OPERATOR_FINISHED;
902 }
903
904 void OBJECT_OT_origin_set(wmOperatorType *ot)
905 {
906         static EnumPropertyItem prop_set_center_types[] = {
907                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
908                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
909                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
910                 {0, NULL, 0, NULL, NULL}
911         };
912         
913         static EnumPropertyItem prop_set_bounds_types[] = {
914                 {V3D_CENTROID, "MEDIAN", 0, "Median Center", ""},
915                 {V3D_CENTER, "BOUNDS", 0, "Bounds Center", ""},
916                 {0, NULL, 0, NULL, NULL}
917         };
918         
919         /* identifiers */
920         ot->name= "Set Origin";
921         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
922         ot->idname= "OBJECT_OT_origin_set";
923         
924         /* api callbacks */
925         ot->invoke= WM_menu_invoke;
926         ot->exec= object_origin_set_exec;
927         
928         ot->poll= ED_operator_scene_editable;
929         
930         /* flags */
931         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
932         
933         ot->prop= RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
934         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_CENTROID, "Center", "");
935 }
936