svn merge -r36583:36603 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / object / object_transform.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_transform.c
29  *  \ingroup edobj
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "DNA_anim_types.h"
37 #include "DNA_armature_types.h"
38 #include "DNA_key_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_group_types.h"
43
44 #include "BLI_math.h"
45 #include "BLI_editVert.h"
46 #include "BLI_listbase.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_context.h"
50 #include "BKE_curve.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_main.h"
53 #include "BKE_mesh.h"
54 #include "BKE_object.h"
55 #include "BKE_report.h"
56 #include "BKE_tessmesh.h"
57 #include "BKE_multires.h"
58 #include "BKE_armature.h"
59
60 #include "RNA_define.h"
61 #include "RNA_access.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "ED_armature.h"
67 #include "ED_keyframing.h"
68 #include "ED_mesh.h"
69 #include "ED_screen.h"
70 #include "ED_view3d.h"
71
72 #include "object_intern.h"
73
74 /*************************** Clear Transformation ****************************/
75
76 /* clear location of object */
77 static void object_clear_loc(Object *ob)
78 {
79         /* clear location if not locked */
80         if ((ob->protectflag & OB_LOCK_LOCX)==0)
81                 ob->loc[0]= ob->dloc[0]= 0.0f;
82         if ((ob->protectflag & OB_LOCK_LOCY)==0)
83                 ob->loc[1]= ob->dloc[1]= 0.0f;
84         if ((ob->protectflag & OB_LOCK_LOCZ)==0)
85                 ob->loc[2]= ob->dloc[2]= 0.0f;
86 }
87
88 /* clear rotation of object */
89 static void object_clear_rot(Object *ob)
90 {
91         /* clear rotations that aren't locked */
92         if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
93                 if (ob->protectflag & OB_LOCK_ROT4D) {
94                         /* perform clamping on a component by component basis */
95                         if (ob->rotmode == ROT_MODE_AXISANGLE) {
96                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
97                                         ob->rotAngle= ob->drotAngle= 0.0f;
98                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
99                                         ob->rotAxis[0]= ob->drotAxis[0]= 0.0f;
100                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
101                                         ob->rotAxis[1]= ob->drotAxis[1]= 0.0f;
102                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
103                                         ob->rotAxis[2]= ob->drotAxis[2]= 0.0f;
104                                         
105                                 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
106                                 if (IS_EQF(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQF(ob->rotAxis[1], ob->rotAxis[2]))
107                                         ob->rotAxis[1] = 1.0f;
108                                 if (IS_EQF(ob->drotAxis[0], ob->drotAxis[1]) && IS_EQF(ob->drotAxis[1], ob->drotAxis[2]))
109                                         ob->drotAxis[1]= 1.0f;
110                         }
111                         else if (ob->rotmode == ROT_MODE_QUAT) {
112                                 if ((ob->protectflag & OB_LOCK_ROTW) == 0)
113                                         ob->quat[0]= ob->dquat[0]= 1.0f;
114                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
115                                         ob->quat[1]= ob->dquat[1]= 0.0f;
116                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
117                                         ob->quat[2]= ob->dquat[2]= 0.0f;
118                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
119                                         ob->quat[3]= ob->dquat[3]= 0.0f;
120                                         
121                                 // TODO: does this quat need normalising now?
122                         }
123                         else {
124                                 /* the flag may have been set for the other modes, so just ignore the extra flag... */
125                                 if ((ob->protectflag & OB_LOCK_ROTX) == 0)
126                                         ob->rot[0]= ob->drot[0]= 0.0f;
127                                 if ((ob->protectflag & OB_LOCK_ROTY) == 0)
128                                         ob->rot[1]= ob->drot[1]= 0.0f;
129                                 if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
130                                         ob->rot[2]= ob->drot[2]= 0.0f;
131                         }
132                 }
133                 else {
134                         /* perform clamping using euler form (3-components) */
135                         // FIXME: deltas are not handled for these cases yet...
136                         float eul[3], oldeul[3], quat1[4] = {0};
137                         
138                         if (ob->rotmode == ROT_MODE_QUAT) {
139                                 QUATCOPY(quat1, ob->quat);
140                                 quat_to_eul(oldeul, ob->quat);
141                         }
142                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
143                                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, ob->rotAxis, ob->rotAngle);
144                         }
145                         else {
146                                 copy_v3_v3(oldeul, ob->rot);
147                         }
148                         
149                         eul[0]= eul[1]= eul[2]= 0.0f;
150                         
151                         if (ob->protectflag & OB_LOCK_ROTX)
152                                 eul[0]= oldeul[0];
153                         if (ob->protectflag & OB_LOCK_ROTY)
154                                 eul[1]= oldeul[1];
155                         if (ob->protectflag & OB_LOCK_ROTZ)
156                                 eul[2]= oldeul[2];
157                         
158                         if (ob->rotmode == ROT_MODE_QUAT) {
159                                 eul_to_quat(ob->quat, eul);
160                                 /* quaternions flip w sign to accumulate rotations correctly */
161                                 if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
162                                         mul_qt_fl(ob->quat, -1.0f);
163                                 }
164                         }
165                         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
166                                 eulO_to_axis_angle(ob->rotAxis, &ob->rotAngle,eul, EULER_ORDER_DEFAULT);
167                         }
168                         else {
169                                 copy_v3_v3(ob->rot, eul);
170                         }
171                 }
172         }                                                // Duplicated in source/blender/editors/armature/editarmature.c
173         else { 
174                 if (ob->rotmode == ROT_MODE_QUAT) {
175                         unit_qt(ob->quat);
176                         unit_qt(ob->dquat);
177                 }
178                 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
179                         unit_axis_angle(ob->rotAxis, &ob->rotAngle);
180                         unit_axis_angle(ob->drotAxis, &ob->drotAngle);
181                 }
182                 else {
183                         zero_v3(ob->rot);
184                         zero_v3(ob->drot);
185                 }
186         }
187 }
188
189 /* clear scale of object */
190 static void object_clear_scale(Object *ob)
191 {
192         /* clear scale factors which are not locked */
193         if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
194                 ob->dsize[0]= 0.0f;
195                 ob->size[0]= 1.0f;
196         }
197         if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
198                 ob->dsize[1]= 0.0f;
199                 ob->size[1]= 1.0f;
200         }
201         if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
202                 ob->dsize[2]= 0.0f;
203                 ob->size[2]= 1.0f;
204         }
205 }
206
207 /* --------------- */
208
209 /* generic exec for clear-transform operators */
210 static int object_clear_transform_generic_exec(bContext *C, wmOperator *op, 
211                 void (*clear_func)(Object*), const char default_ksName[])
212 {
213         Main *bmain = CTX_data_main(C);
214         Scene *scene= CTX_data_scene(C);
215         KeyingSet *ks;
216         
217         /* sanity checks */
218         if ELEM(NULL, clear_func, default_ksName) {
219                 BKE_report(op->reports, RPT_ERROR, "Programming error: missing clear transform func or Keying Set Name");
220                 return OPERATOR_CANCELLED;
221         }
222         
223         /* get KeyingSet to use */
224         ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
225         
226         /* operate on selected objects only if they aren't in weight-paint mode 
227          * (so that object-transform clearing won't be applied at same time as bone-clearing)
228          */
229         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
230         {
231                 if (!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
232                         /* run provided clearing function */
233                         clear_func(ob);
234                         
235                         /* auto keyframing */
236                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
237                                 ListBase dsources = {NULL, NULL};
238                                 
239                                 /* now insert the keyframe(s) using the Keying Set
240                                  *      1) add datasource override for the Object
241                                  *      2) insert keyframes
242                                  *      3) free the extra info 
243                                  */
244                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, NULL, NULL); 
245                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
246                                 BLI_freelistN(&dsources);
247                         }
248                         
249                         /* tag for updates */
250                         ob->recalc |= OB_RECALC_OB;
251                 }
252         }
253         CTX_DATA_END;
254         
255         /* this is needed so children are also updated */
256         DAG_ids_flush_update(bmain, 0);
257
258         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
259
260         return OPERATOR_FINISHED;
261 }
262
263 /* --------------- */
264
265
266 static int object_location_clear_exec(bContext *C, wmOperator *op)
267 {
268         return object_clear_transform_generic_exec(C, op, object_clear_loc, "Location");
269 }
270
271 void OBJECT_OT_location_clear(wmOperatorType *ot)
272 {
273         /* identifiers */
274         ot->name= "Clear Location";
275         ot->description = "Clear the object's location";
276         ot->idname= "OBJECT_OT_location_clear";
277         
278         /* api callbacks */
279         ot->exec= object_location_clear_exec;
280         ot->poll= ED_operator_scene_editable;
281         
282         /* flags */
283         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
284 }
285
286 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
287 {
288         return object_clear_transform_generic_exec(C, op, object_clear_rot, "Rotation");
289 }
290
291 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
292 {
293         /* identifiers */
294         ot->name= "Clear Rotation";
295         ot->description = "Clear the object's rotation";
296         ot->idname= "OBJECT_OT_rotation_clear";
297         
298         /* api callbacks */
299         ot->exec= object_rotation_clear_exec;
300         ot->poll= ED_operator_scene_editable;
301         
302         /* flags */
303         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
304 }
305
306 static int object_scale_clear_exec(bContext *C, wmOperator *op)
307 {
308         return object_clear_transform_generic_exec(C, op, object_clear_scale, "Scaling");
309 }
310
311 void OBJECT_OT_scale_clear(wmOperatorType *ot)
312 {
313         /* identifiers */
314         ot->name= "Clear Scale";
315         ot->description = "Clear the object's scale";
316         ot->idname= "OBJECT_OT_scale_clear";
317         
318         /* api callbacks */
319         ot->exec= object_scale_clear_exec;
320         ot->poll= ED_operator_scene_editable;
321         
322         /* flags */
323         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
324 }
325
326 /* --------------- */
327
328 static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
329 {
330         Main *bmain= CTX_data_main(C);
331         float *v1, *v3;
332         float mat[3][3];
333
334         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
335         {
336                 if (ob->parent) {
337                         /* vectors pointed to by v1 and v3 will get modified */
338                         v1= ob->loc;
339                         v3= ob->parentinv[3];
340                         
341                         copy_m3_m4(mat, ob->parentinv);
342                         negate_v3_v3(v3, v1);
343                         mul_m3_v3(mat, v3);
344                 }
345                 ob->recalc |= OB_RECALC_OB;
346         }
347         CTX_DATA_END;
348
349         DAG_ids_flush_update(bmain, 0);
350         
351         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
352         
353         return OPERATOR_FINISHED;
354 }
355
356 void OBJECT_OT_origin_clear(wmOperatorType *ot)
357 {
358         /* identifiers */
359         ot->name= "Clear Origin";
360         ot->description = "Clear the object's origin";
361         ot->idname= "OBJECT_OT_origin_clear";
362         
363         /* api callbacks */
364         ot->exec= object_origin_clear_exec;
365         ot->poll= ED_operator_scene_editable;
366         
367         /* flags */
368         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
369 }
370
371 /*************************** Apply Transformation ****************************/
372
373 /* use this when the loc/size/rot of the parent has changed but the children
374  * should stay in the same place, e.g. for apply-size-rot or object center */
375 static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob ) 
376 {
377         Object workob;
378         Object *ob_child;
379         
380         /* a change was made, adjust the children to compensate */
381         for(ob_child=bmain->object.first; ob_child; ob_child=ob_child->id.next) {
382                 if(ob_child->parent == ob) {
383                         object_apply_mat4(ob_child, ob_child->obmat, TRUE, FALSE);
384                         what_does_parent(scene, ob_child, &workob);
385                         invert_m4_m4(ob_child->parentinv, workob.obmat);
386                 }
387         }
388 }
389
390 static int apply_objects_internal(bContext *C, ReportList *reports, int apply_loc, int apply_rot, int apply_scale)
391 {
392         Main *bmain= CTX_data_main(C);
393         Scene *scene= CTX_data_scene(C);
394         float rsmat[3][3], tmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale;
395         int a, change = 0;
396         
397         /* first check if we can execute */
398         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
399
400                 if(ob->type==OB_MESH) {
401                         if(ID_REAL_USERS(ob->data) > 1) {
402                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user mesh, doing nothing.");
403                                 return OPERATOR_CANCELLED;
404                         }
405                 }
406                 else if(ob->type==OB_ARMATURE) {
407                         if(ID_REAL_USERS(ob->data) > 1) {
408                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user armature, doing nothing.");
409                                 return OPERATOR_CANCELLED;
410                         }
411                 }
412                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
413                         Curve *cu;
414
415                         if(ID_REAL_USERS(ob->data) > 1) {
416                                 BKE_report(reports, RPT_ERROR, "Can't apply to a multi user curve, doing nothing.");
417                                 return OPERATOR_CANCELLED;
418                         }
419
420                         cu= ob->data;
421
422                         if(!(cu->flag & CU_3D) && (apply_rot || apply_loc)) {
423                                 BKE_report(reports, RPT_ERROR, "Neither rotation nor location could be applied to a 2d curve, doing nothing.");
424                                 return OPERATOR_CANCELLED;
425                         }
426                         if(cu->key) {
427                                 BKE_report(reports, RPT_ERROR, "Can't apply to a curve with vertex keys, doing nothing.");
428                                 return OPERATOR_CANCELLED;
429                         }
430                 }
431         }
432         CTX_DATA_END;
433         
434         /* now execute */
435         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
436
437                 /* calculate rotation/scale matrix */
438                 if(apply_scale && apply_rot)
439                         object_to_mat3(ob, rsmat);
440                 else if(apply_scale)
441                         object_scale_to_mat3(ob, rsmat);
442                 else if(apply_rot) {
443                         float tmat[3][3], timat[3][3];
444
445                         /* simple rotation matrix */
446                         object_rot_to_mat3(ob, rsmat);
447
448                         /* correct for scale, note mul_m3_m3m3 has swapped args! */
449                         object_scale_to_mat3(ob, tmat);
450                         invert_m3_m3(timat, tmat);
451                         mul_m3_m3m3(rsmat, timat, rsmat);
452                         mul_m3_m3m3(rsmat, rsmat, tmat);
453                 }
454                 else
455                         unit_m3(rsmat);
456
457                 copy_m4_m3(mat, rsmat);
458
459                 /* calculate translation */
460                 if(apply_loc) {
461                         copy_v3_v3(mat[3], ob->loc);
462
463                         if(!(apply_scale && apply_rot)) {
464                                 /* correct for scale and rotation that is still applied */
465                                 object_to_mat3(ob, obmat);
466                                 invert_m3_m3(iobmat, obmat);
467                                 mul_m3_m3m3(tmat, rsmat, iobmat);
468                                 mul_m3_v3(tmat, mat[3]);
469                         }
470                 }
471
472                 /* apply to object data */
473                 if(ob->type==OB_MESH) {
474                         Mesh *me= ob->data;
475                         MVert *mvert;
476
477                         multiresModifier_scale_disp(scene, ob);
478                         
479                         /* adjust data */
480                         mvert= me->mvert;
481                         for(a=0; a<me->totvert; a++, mvert++)
482                                 mul_m4_v3(mat, mvert->co);
483                         
484                         if(me->key) {
485                                 KeyBlock *kb;
486                                 
487                                 for(kb=me->key->block.first; kb; kb=kb->next) {
488                                         float *fp= kb->data;
489                                         
490                                         for(a=0; a<kb->totelem; a++, fp+=3)
491                                                 mul_m4_v3(mat, fp);
492                                 }
493                         }
494                         
495                         /* update normals */
496                         mesh_calc_normals(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
497                 }
498                 else if (ob->type==OB_ARMATURE) {
499                         ED_armature_apply_transform(ob, mat);
500                 }
501                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
502                         Curve *cu= ob->data;
503
504                         Nurb *nu;
505                         BPoint *bp;
506                         BezTriple *bezt;
507
508                         scale = mat3_to_scale(rsmat);
509
510                         for(nu=cu->nurb.first; nu; nu=nu->next) {
511                                 if(nu->type == CU_BEZIER) {
512                                         a= nu->pntsu;
513                                         for(bezt= nu->bezt; a--; bezt++) {
514                                                 mul_m4_v3(mat, bezt->vec[0]);
515                                                 mul_m4_v3(mat, bezt->vec[1]);
516                                                 mul_m4_v3(mat, bezt->vec[2]);
517                                                 bezt->radius *= scale;
518                                         }
519                                         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                         BMEditMesh *em = me->edit_btmesh;
674                         BMVert *eve;
675                         BMIter iter;
676                         int total = 0;
677                         
678                         if(centermode == ORIGIN_TO_CURSOR) {
679                                 copy_v3_v3(cent, cursor);
680                                 invert_m4_m4(obedit->imat, obedit->obmat);
681                                 mul_m4_v3(obedit->imat, cent);
682                         } else {
683                                 BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
684                                         if(around==V3D_CENTROID) {
685                                                 total++;
686                                                 add_v3_v3(cent, eve->co);
687                                                 mul_v3_fl(cent, 1.0f/(float)total);
688                                         }
689                                         else {
690                                                 DO_MINMAX(eve->co, min, max);
691                                                 mid_v3_v3v3(cent, min, max);
692                                         }
693                                 }
694                         }
695                         
696                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
697                                 sub_v3_v3(eve->co, cent);
698                         }
699
700                         EDBM_RecalcNormals(em);
701                         tot_change++;
702                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
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                                                 ob_other->recalc= 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                         tob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
897                 }
898         }
899
900         if (tot_change) {
901                 DAG_ids_flush_update(bmain, 0);
902                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
903         }
904
905         /* Warn if any errors occurred */
906         if (tot_lib_error+tot_multiuser_arm_error) {
907                 BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);
908                 if (tot_lib_error)
909                         BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
910                 if (tot_multiuser_arm_error)
911                         BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
912         }
913
914         return OPERATOR_FINISHED;
915 }
916
917 void OBJECT_OT_origin_set(wmOperatorType *ot)
918 {
919         static EnumPropertyItem prop_set_center_types[] = {
920                 {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
921                 {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
922                 {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
923                 {0, NULL, 0, NULL, NULL}
924         };
925         
926         static EnumPropertyItem prop_set_bounds_types[] = {
927                 {V3D_CENTROID, "MEDIAN", 0, "Median Center", ""},
928                 {V3D_CENTER, "BOUNDS", 0, "Bounds Center", ""},
929                 {0, NULL, 0, NULL, NULL}
930         };
931         
932         /* identifiers */
933         ot->name= "Set Origin";
934         ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
935         ot->idname= "OBJECT_OT_origin_set";
936         
937         /* api callbacks */
938         ot->invoke= WM_menu_invoke;
939         ot->exec= object_origin_set_exec;
940         
941         ot->poll= ED_operator_scene_editable;
942         
943         /* flags */
944         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
945         
946         ot->prop= RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
947         RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_CENTROID, "Center", "");
948 }
949