4c29490b0f08521a43c153210813bc0d6be7eec9
[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_multires.h"
57 #include "BKE_armature.h"
58
59 #include "RNA_define.h"
60 #include "RNA_access.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "ED_armature.h"
66 #include "ED_keyframing.h"
67 #include "ED_mesh.h"
68 #include "ED_screen.h"
69 #include "ED_view3d.h"
70
71 #include "object_intern.h"
72
73 /*************************** Clear Transformation ****************************/
74
75 /* clear location of object */
76 static void object_clear_loc(Object *ob)
77 {
78         /* clear location if not locked */
79         if ((ob->protectflag & OB_LOCK_LOCX)==0)
80                 ob->loc[0]= ob->dloc[0]= 0.0f;
81         if ((ob->protectflag & OB_LOCK_LOCY)==0)
82                 ob->loc[1]= ob->dloc[1]= 0.0f;
83         if ((ob->protectflag & OB_LOCK_LOCZ)==0)
84                 ob->loc[2]= ob->dloc[2]= 0.0f;
85 }
86
87 /* clear rotation of object */
88 static void object_clear_rot(Object *ob)
89 {
90         /* clear rotations that aren't locked */
91         if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
92                 if (ob->protectflag & OB_LOCK_ROT4D) {
93                         /* perform clamping on a component by component basis */
94                         if (ob->rotmode == ROT_MODE_AXISANGLE) {
95                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
96                                         ob->rotAngle= ob->drotAngle= 0.0f;
97                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
98                                         ob->rotAxis[0]= ob->drotAxis[0]= 0.0f;
99                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
100                                         ob->rotAxis[1]= ob->drotAxis[1]= 0.0f;
101                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
102                                         ob->rotAxis[2]= ob->drotAxis[2]= 0.0f;
103                                         
104                                 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
105                                 if (IS_EQF(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQF(ob->rotAxis[1], ob->rotAxis[2]))
106                                         ob->rotAxis[1] = 1.0f;
107                                 if (IS_EQF(ob->drotAxis[0], ob->drotAxis[1]) && IS_EQF(ob->drotAxis[1], ob->drotAxis[2]))
108                                         ob->drotAxis[1]= 1.0f;
109                         }
110                         else if (ob->rotmode == ROT_MODE_QUAT) {
111                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
112                                         ob->quat[0]= ob->dquat[0]= 1.0f;
113                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
114                                         ob->quat[1]= ob->dquat[1]= 0.0f;
115                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
116                                         ob->quat[2]= ob->dquat[2]= 0.0f;
117                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
118                                         ob->quat[3]= ob->dquat[3]= 0.0f;
119                                         
120                                 // TODO: does this quat need normalising now?
121                         }
122                         else {
123                                 /* the flag may have been set for the other modes, so just ignore the extra flag... */
124                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
125                                         ob->rot[0]= ob->drot[0]= 0.0f;
126                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
127                                         ob->rot[1]= ob->drot[1]= 0.0f;
128                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
129                                         ob->rot[2]= ob->drot[2]= 0.0f;
130                         }
131                 }
132                 else {
133                         /* perform clamping using euler form (3-components) */
134                         // FIXME: deltas are not handled for these cases yet...
135                         float eul[3], oldeul[3], quat1[4] = {0};
136                         
137                         if (ob->rotmode == ROT_MODE_QUAT) {
138                                 QUATCOPY(quat1, ob->quat);
139                                 quat_to_eul(oldeul, ob->quat);
140                         }
141                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
142                                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, ob->rotAxis, ob->rotAngle);
143                         }
144                         else {
145                                 copy_v3_v3(oldeul, ob->rot);
146                         }
147                         
148                         eul[0]= eul[1]= eul[2]= 0.0f;
149                         
150                         if (ob->protectflag & OB_LOCK_ROTX)
151                                 eul[0]= oldeul[0];
152                         if (ob->protectflag & OB_LOCK_ROTY)
153                                 eul[1]= oldeul[1];
154                         if (ob->protectflag & OB_LOCK_ROTZ)
155                                 eul[2]= oldeul[2];
156                         
157                         if (ob->rotmode == ROT_MODE_QUAT) {
158                                 eul_to_quat(ob->quat, eul);
159                                 /* quaternions flip w sign to accumulate rotations correctly */
160                                 if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
161                                         mul_qt_fl(ob->quat, -1.0f);
162                                 }
163                         }
164                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
165                                 eulO_to_axis_angle(ob->rotAxis, &ob->rotAngle,eul, EULER_ORDER_DEFAULT);
166                         }
167                         else {
168                                 copy_v3_v3(ob->rot, eul);
169                         }
170                 }
171         }                                                // Duplicated in source/blender/editors/armature/editarmature.c
172         else { 
173                 if (ob->rotmode == ROT_MODE_QUAT) {
174                         unit_qt(ob->quat);
175                         unit_qt(ob->dquat);
176                 }
177                 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
178                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
179                         unit_axis_angle(ob->drotAxis, &ob->drotAngle);
180                 }
181                 else {
182                         zero_v3(ob->rot);
183                         zero_v3(ob->drot);
184                 }
185         }
186 }
187
188 /* clear scale of object */
189 static void object_clear_scale(Object *ob)
190 {
191         /* clear scale factors which are not locked */
192         if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
193                 ob->dsize[0]= 0.0f;
194                 ob->size[0]= 1.0f;
195         }
196         if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
197                 ob->dsize[1]= 0.0f;
198                 ob->size[1]= 1.0f;
199         }
200         if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
201                 ob->dsize[2]= 0.0f;
202                 ob->size[2]= 1.0f;
203         }
204 }
205
206 /* --------------- */
207
208 /* generic exec for clear-transform operators */
209 static int object_clear_transform_generic_exec(bContext *C, wmOperator *op, 
210                 void (*clear_func)(Object*), const char default_ksName[])
211 {
212         Main *bmain = CTX_data_main(C);
213         Scene *scene= CTX_data_scene(C);
214         KeyingSet *ks;
215         
216         /* sanity checks */
217         if ELEM(NULL, clear_func, default_ksName) {
218                 BKE_report(op->reports, RPT_ERROR, "Programming error: missing clear transform func or Keying Set Name");
219                 return OPERATOR_CANCELLED;
220         }
221         
222         /* get KeyingSet to use */
223         ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
224         
225         /* operate on selected objects only if they aren't in weight-paint mode 
226          * (so that object-transform clearing won't be applied at same time as bone-clearing)
227          */
228         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
229         {
230                 if (!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
231                         /* run provided clearing function */
232                         clear_func(ob);
233                         
234                         /* auto keyframing */
235                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
236                                 ListBase dsources = {NULL, NULL};
237                                 
238                                 /* now insert the keyframe(s) using the Keying Set
239                                  *      1) add datasource override for the Object
240                                  *      2) insert keyframes
241                                  *      3) free the extra info 
242                                  */
243                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, NULL, NULL); 
244                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
245                                 BLI_freelistN(&dsources);
246                         }
247                         
248                         /* tag for updates */
249                         DAG_id_tag_update(&ob->id, OB_RECALC_OB);
250                 }
251         }
252         CTX_DATA_END;
253         
254         /* this is needed so children are also updated */
255         DAG_ids_flush_update(bmain, 0);
256
257         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
258
259         return OPERATOR_FINISHED;
260 }
261
262 /* --------------- */
263
264
265 static int object_location_clear_exec(bContext *C, wmOperator *op)
266 {
267         return object_clear_transform_generic_exec(C, op, object_clear_loc, "Location");
268 }
269
270 void OBJECT_OT_location_clear(wmOperatorType *ot)
271 {
272         /* identifiers */
273         ot->name= "Clear Location";
274         ot->description = "Clear the object's location";
275         ot->idname= "OBJECT_OT_location_clear";
276         
277         /* api callbacks */
278         ot->exec= object_location_clear_exec;
279         ot->poll= ED_operator_scene_editable;
280         
281         /* flags */
282         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
283 }
284
285 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
286 {
287         return object_clear_transform_generic_exec(C, op, object_clear_rot, "Rotation");
288 }
289
290 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
291 {
292         /* identifiers */
293         ot->name= "Clear Rotation";
294         ot->description = "Clear the object's rotation";
295         ot->idname= "OBJECT_OT_rotation_clear";
296         
297         /* api callbacks */
298         ot->exec= object_rotation_clear_exec;
299         ot->poll= ED_operator_scene_editable;
300         
301         /* flags */
302         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
303 }
304
305 static int object_scale_clear_exec(bContext *C, wmOperator *op)
306 {
307         return object_clear_transform_generic_exec(C, op, object_clear_scale, "Scaling");
308 }
309
310 void OBJECT_OT_scale_clear(wmOperatorType *ot)
311 {
312         /* identifiers */
313         ot->name= "Clear Scale";
314         ot->description = "Clear the object's scale";
315         ot->idname= "OBJECT_OT_scale_clear";
316         
317         /* api callbacks */
318         ot->exec= object_scale_clear_exec;
319         ot->poll= ED_operator_scene_editable;
320         
321         /* flags */
322         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
323 }
324
325 /* --------------- */
326
327 static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
328 {
329         Main *bmain= CTX_data_main(C);
330         float *v1, *v3;
331         float mat[3][3];
332
333         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
334         {
335                 if (ob->parent) {
336                         /* vectors pointed to by v1 and v3 will get modified */
337                         v1= ob->loc;
338                         v3= ob->parentinv[3];
339                         
340                         copy_m3_m4(mat, ob->parentinv);
341                         negate_v3_v3(v3, v1);
342                         mul_m3_v3(mat, v3);
343                 }
344
345                 DAG_id_tag_update(&ob->id, 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->mface, me->totface, NULL);
497                 }
498                 else if (ob->type==OB_ARMATURE) {
499                         ED_armature_apply_transform(ob, mat);
500                 }
501                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
502                         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                                         calchandlesNurb(nu);
520                                 }
521                                 else {
522                                         a= nu->pntsu*nu->pntsv;
523                                         for(bp= nu->bp; a--; bp++)
524                                                 mul_m4_v3(mat, bp->vec);
525                                 }
526                         }
527                 }
528                 else
529                         continue;
530
531                 if(apply_loc)
532                         zero_v3(ob->loc);
533                 if(apply_scale)
534                         ob->size[0]= ob->size[1]= ob->size[2]= 1.0f;
535                 if(apply_rot) {
536                         zero_v3(ob->rot);
537                         unit_qt(ob->quat);
538                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
539                 }
540
541                 where_is_object(scene, ob);
542                 if(ob->type==OB_ARMATURE) {
543                         where_is_pose(scene, ob); /* needed for bone parents */
544                 }
545
546                 ignore_parent_tx(bmain, scene, ob);
547
548                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
549
550                 change = 1;
551         }
552         CTX_DATA_END;
553
554         if(!change)
555                 return OPERATOR_CANCELLED;
556
557         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
558         return OPERATOR_FINISHED;
559 }
560
561 static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
562 {
563         Scene *scene= CTX_data_scene(C);
564         int change = 0;
565         
566         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
567                 where_is_object(scene, ob);
568                 object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
569                 where_is_object(scene, ob);
570
571                 /* update for any children that may get moved */
572                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
573         
574                 change = 1;
575         }
576         CTX_DATA_END;
577
578         if(!change)
579                 return OPERATOR_CANCELLED;
580
581         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
582         return OPERATOR_FINISHED;
583 }
584
585 void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
586 {
587         /* identifiers */
588         ot->name= "Apply Visual Transform";
589         ot->description = "Apply the object's visual transformation to its data";
590         ot->idname= "OBJECT_OT_visual_transform_apply";
591         
592         /* api callbacks */
593         ot->exec= visual_transform_apply_exec;
594         ot->poll= ED_operator_scene_editable;
595         
596         /* flags */
597         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
598 }
599
600 static int object_transform_apply_exec(bContext *C, wmOperator *op)
601 {
602         const int loc= RNA_boolean_get(op->ptr, "location");
603         const int rot= RNA_boolean_get(op->ptr, "rotation");
604         const int sca= RNA_boolean_get(op->ptr, "scale");
605
606         if(loc || rot || sca) {
607                 return apply_objects_internal(C, op->reports, loc, rot, sca);
608         }
609         else {
610                 return OPERATOR_CANCELLED;
611         }
612 }
613
614 void OBJECT_OT_transform_apply(wmOperatorType *ot)
615 {
616         /* identifiers */
617         ot->name= "Apply Object Transform";
618         ot->description = "Apply the object's transformation to its data";
619         ot->idname= "OBJECT_OT_transform_apply";
620
621         /* api callbacks */
622         ot->exec= object_transform_apply_exec;
623         ot->poll= ED_operator_objectmode;
624
625         /* flags */
626         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
627
628         RNA_def_boolean(ot->srna, "location", 0, "Location", "");
629         RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", "");
630         RNA_def_boolean(ot->srna, "scale", 0, "Scale", "");
631 }
632
633 /********************* Set Object Center ************************/
634
635 enum {
636         GEOMETRY_TO_ORIGIN=0,
637         ORIGIN_TO_GEOMETRY,
638         ORIGIN_TO_CURSOR
639 };
640
641 static int object_origin_set_exec(bContext *C, wmOperator *op)
642 {
643         Main *bmain= CTX_data_main(C);
644         Scene *scene= CTX_data_scene(C);
645         Object *obedit= CTX_data_edit_object(C);
646         Object *tob;
647         float cursor[3], cent[3], cent_neg[3], centn[3], min[3], max[3];
648         int centermode = RNA_enum_get(op->ptr, "type");
649         int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
650
651         /* keep track of what is changed */
652         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
653
654         if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
655                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
656                 return OPERATOR_CANCELLED;
657         }
658         else {
659                 /* get the view settings if 'around' isnt set and the view is available */
660                 View3D *v3d= CTX_wm_view3d(C);
661                 copy_v3_v3(cursor, give_cursor(scene, v3d));
662                 if(v3d && !RNA_property_is_set(op->ptr, "around"))
663                         around= v3d->around;
664         }
665
666         zero_v3(cent);
667
668         if(obedit) {
669                 INIT_MINMAX(min, max);
670
671                 if(obedit->type==OB_MESH) {
672                         Mesh *me= obedit->data;
673                         EditMesh *em = BKE_mesh_get_editmesh(me);
674                         EditVert *eve;
675
676                         if(around==V3D_CENTROID) {
677                                 int total= 0;
678                                 for(eve= em->verts.first; eve; eve= eve->next) {
679                                         total++;
680                                         add_v3_v3(cent, eve->co);
681                                 }
682                                 if(total) {
683                                         mul_v3_fl(cent, 1.0f/(float)total);
684                                 }
685                         }
686                         else {
687                                 for(eve= em->verts.first; eve; eve= eve->next) {
688                                         DO_MINMAX(eve->co, min, max);
689                                 }
690                                 mid_v3_v3v3(cent, min, max);
691                         }
692
693                         if(!is_zero_v3(cent)) {
694                                 for(eve= em->verts.first; eve; eve= eve->next) {
695                                         sub_v3_v3(eve->co, cent);
696                                 }
697
698                                 recalc_editnormals(em);
699                                 tot_change++;
700                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
701                         }
702                         BKE_mesh_end_editmesh(me, em);
703                 }
704         }
705
706         /* reset flags */
707         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
708                         ob->flag &= ~OB_DONE;
709         }
710         CTX_DATA_END;
711
712         for (tob= bmain->object.first; tob; tob= tob->id.next) {
713                 if(tob->data)
714                         ((ID *)tob->data)->flag &= ~LIB_DOIT;
715                 if(tob->dup_group)
716                         ((ID *)tob->dup_group)->flag &= ~LIB_DOIT;
717         }
718
719         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
720                 if((ob->flag & OB_DONE)==0) {
721                         int do_inverse_offset = FALSE;
722                         ob->flag |= OB_DONE;
723
724                         if(centermode == ORIGIN_TO_CURSOR) {
725                                 copy_v3_v3(cent, cursor);
726                                 invert_m4_m4(ob->imat, ob->obmat);
727                                 mul_m4_v3(ob->imat, cent);
728                         }
729                         
730                         if(ob->data == NULL) {
731                                 /* special support for dupligroups */
732                                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.flag & LIB_DOIT)==0) {
733                                         if(ob->dup_group->id.lib) {
734                                                 tot_lib_error++;
735                                         }
736                                         else {
737                                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
738                                                 else {
739                                                         /* only bounds support */
740                                                         INIT_MINMAX(min, max);
741                                                         minmax_object_duplis(scene, ob, min, max);
742                                                         mid_v3_v3v3(cent, min, max);
743                                                         invert_m4_m4(ob->imat, ob->obmat);
744                                                         mul_m4_v3(ob->imat, cent);
745                                                 }
746                                                 
747                                                 add_v3_v3(ob->dup_group->dupli_ofs, cent);
748
749                                                 tot_change++;
750                                                 ob->dup_group->id.flag |= LIB_DOIT;
751                                                 do_inverse_offset= TRUE;
752                                         }
753                                 }
754                         }
755                         else if (((ID *)ob->data)->lib) {
756                                 tot_lib_error++;
757                         }
758
759                         if(obedit==NULL && ob->type==OB_MESH) {
760                                 Mesh *me= ob->data;
761
762                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
763                                 else if(around==V3D_CENTROID) { mesh_center_median(me, cent); }
764                                 else { mesh_center_bounds(me, cent); }
765
766                                 negate_v3_v3(cent_neg, cent);
767                                 mesh_translate(me, cent_neg, 1);
768
769                                 tot_change++;
770                                 me->id.flag |= LIB_DOIT;
771                                 do_inverse_offset= TRUE;
772                         }
773                         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
774                                 Curve *cu= ob->data;
775
776                                 if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
777                                 else if(around==V3D_CENTROID) { curve_center_median(cu, cent); }
778                                 else { curve_center_bounds(cu, cent);   }
779
780                                 /* don't allow Z change if curve is 2D */
781                                 if((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
782                                         cent[2] = 0.0;
783
784                                 negate_v3_v3(cent_neg, cent);
785                                 curve_translate(cu, cent_neg, 1);
786
787                                 tot_change++;
788                                 cu->id.flag |= LIB_DOIT;
789                                 do_inverse_offset= TRUE;
790
791                                 if(obedit) {
792                                         if (centermode == GEOMETRY_TO_ORIGIN) {
793                                                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
794                                         }
795                                         break;
796                                 }
797                         }
798                         else if(ob->type==OB_FONT) {
799                                 /* get from bb */
800
801                                 Curve *cu= ob->data;
802
803                                 if(cu->bb==NULL && (centermode != ORIGIN_TO_CURSOR)) {
804                                         /* do nothing*/
805                                 }
806                                 else {
807                                         if(centermode == ORIGIN_TO_CURSOR) {
808                                                 /* done */
809                                         }
810                                         else {
811                                                 cent[0]= 0.5f * ( cu->bb->vec[4][0] + cu->bb->vec[0][0]);
812                                                 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 */
813                                         }
814
815                                         cent[2]= 0.0f;
816
817                                         cu->xof= cu->xof - (cent[0] / cu->fsize);
818                                         cu->yof= cu->yof - (cent[1] / cu->fsize);
819
820                                         tot_change++;
821                                         cu->id.flag |= LIB_DOIT;
822                                         do_inverse_offset= TRUE;
823                                 }
824                         }
825                         else if(ob->type==OB_ARMATURE) {
826                                 bArmature *arm = ob->data;
827
828                                 if(ID_REAL_USERS(arm) > 1) {
829                                         /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
830                                         return;*/
831                                         tot_multiuser_arm_error++;
832                                 }
833                                 else {
834                                         /* Function to recenter armatures in editarmature.c
835                                          * Bone + object locations are handled there.
836                                          */
837                                         docenter_armature(scene, ob, cursor, centermode, around);
838
839                                         tot_change++;
840                                         arm->id.flag |= LIB_DOIT;
841                                         /* do_inverse_offset= TRUE; */ /* docenter_armature() handles this */
842
843                                         where_is_object(scene, ob);
844                                         where_is_pose(scene, ob); /* needed for bone parents */
845
846                                         ignore_parent_tx(bmain, scene, ob);
847
848                                         if(obedit)
849                                                 break;
850                                 }
851                         }
852
853                         /* offset other selected objects */
854                         if(do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
855                                 /* was the object data modified
856                                  * note: the functions above must set 'cent' */
857                                 copy_v3_v3(centn, cent);
858                                 mul_mat3_m4_v3(ob->obmat, centn); /* ommit translation part */
859                                 add_v3_v3(ob->loc, centn);
860
861                                 where_is_object(scene, ob);
862                                 if(ob->type==OB_ARMATURE) {
863                                         where_is_pose(scene, ob); /* needed for bone parents */
864                                 }
865
866                                 ignore_parent_tx(bmain, scene, ob);
867                                 
868                                 /* other users? */
869                                 CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
870                                         if(             (ob_other->flag & OB_DONE)==0 &&
871                                                         (       (ob->data && (ob->data == ob_other->data)) ||
872                                                                 (ob->dup_group==ob_other->dup_group && (ob->transflag|ob_other->transflag) & OB_DUPLIGROUP) )
873                                         ) {
874                                                 ob_other->flag |= OB_DONE;
875                                                 DAG_id_tag_update(&ob_other->id, OB_RECALC_OB|OB_RECALC_DATA);
876
877                                                 copy_v3_v3(centn, cent);
878                                                 mul_mat3_m4_v3(ob_other->obmat, centn); /* ommit translation part */
879                                                 add_v3_v3(ob_other->loc, centn);
880
881                                                 where_is_object(scene, ob_other);
882                                                 if(ob_other->type==OB_ARMATURE) {
883                                                         where_is_pose(scene, ob_other); /* needed for bone parents */
884                                                 }
885                                                 ignore_parent_tx(bmain, scene, ob_other);
886                                         }
887                                 }
888                                 CTX_DATA_END;
889                         }
890                 }
891         }
892         CTX_DATA_END;
893
894         for (tob= bmain->object.first; tob; tob= tob->id.next)
895                 if(tob->data && (((ID *)tob->data)->flag & LIB_DOIT))
896                         DAG_id_tag_update(&tob->id, OB_RECALC_OB|OB_RECALC_DATA);
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