Merged changes in the trunk up to revision 34193.
[blender.git] / source / blender / editors / transform / transform_orientations.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  * Contributor(s): Martin Poirier
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <string.h>
26 #include <ctype.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_armature_types.h"
31 #include "DNA_curve_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_view3d_types.h"
36
37
38 #include "BKE_armature.h"
39 #include "BKE_context.h"
40 #include "BKE_report.h"
41
42 #include "BLI_math.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_editVert.h"
45 #include "BLI_utildefines.h"
46
47 //#include "BIF_editmesh.h"
48 //#include "BIF_interface.h"
49 //#include "BIF_space.h"
50 //#include "BIF_toolbox.h"
51
52 #include "ED_armature.h"
53 #include "ED_mesh.h"
54 #include "ED_curve.h" /* for ED_curve_editnurbs */
55
56
57 #include "RNA_define.h"
58
59 #include "transform.h"
60
61 /* *********************** TransSpace ************************** */
62
63 void BIF_clearTransformOrientation(bContext *C)
64 {
65         View3D *v3d = CTX_wm_view3d(C);
66
67         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
68         BLI_freelistN(transform_spaces);
69         
70         // Need to loop over all view3d
71         if(v3d && v3d->twmode >= V3D_MANIP_CUSTOM) {
72                 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
73         }
74 }
75
76 static TransformOrientation* findOrientationName(ListBase *lb, const char *name)
77 {
78         TransformOrientation *ts= NULL;
79
80         for (ts= lb->first; ts; ts = ts->next) {
81                 if (strncmp(ts->name, name, sizeof(ts->name)-1) == 0) {
82                         return ts;
83                 }
84         }
85         
86         return NULL;
87 }
88
89 static int uniqueOrientationNameCheck(void *arg, const char *name)
90 {
91         return findOrientationName((ListBase *)arg, name) != NULL;
92 }
93
94 static void uniqueOrientationName(ListBase *lb, char *name)
95 {
96         BLI_uniquename_cb(uniqueOrientationNameCheck, lb, "Space", '.', name, sizeof(((TransformOrientation *)NULL)->name));
97 }
98
99 void BIF_createTransformOrientation(bContext *C, ReportList *reports, char *name, int use, int overwrite)
100 {
101         Object *obedit = CTX_data_edit_object(C);
102         Object *ob = CTX_data_active_object(C);
103         TransformOrientation *ts = NULL;
104         
105         if (obedit) {
106                 if (obedit->type == OB_MESH)
107                         ts = createMeshSpace(C, reports, name, overwrite);
108                 else if (obedit->type == OB_ARMATURE)
109                         ts = createBoneSpace(C, reports, name, overwrite);
110         }
111         else if (ob && (ob->mode & OB_MODE_POSE)) {
112                         ts = createBoneSpace(C, reports, name, overwrite);
113         }
114         else {
115                 ts = createObjectSpace(C, reports, name, overwrite);
116         }
117         
118         if (use && ts != NULL)
119         {
120                 BIF_selectTransformOrientation(C, ts);
121         }
122 }
123
124 TransformOrientation *createObjectSpace(bContext *C, ReportList *UNUSED(reports), char *name, int overwrite) {
125         Base *base = CTX_data_active_base(C);
126         Object *ob;
127         float mat[3][3];
128
129         if (base == NULL)
130                 return NULL;
131
132
133         ob = base->object;
134         
135         copy_m3_m4(mat, ob->obmat);
136         normalize_m3(mat);
137
138         /* use object name if no name is given */
139         if (name[0] == 0)
140         {
141                 strncpy(name, ob->id.name+2, 35);
142         }
143
144         return addMatrixSpace(C, mat, name, overwrite); 
145 }
146
147 TransformOrientation *createBoneSpace(bContext *C, ReportList *reports, char *name, int overwrite) {
148         float mat[3][3];
149         float normal[3], plane[3];
150
151         getTransformOrientation(C, normal, plane, 0);
152
153         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
154                 BKE_reports_prepend(reports, "Cannot use zero-length bone");
155                 return NULL;
156         }
157
158         if (name[0] == 0)
159         {
160                 strcpy(name, "Bone");
161         }
162
163         return addMatrixSpace(C, mat, name, overwrite);
164 }
165
166 TransformOrientation *createMeshSpace(bContext *C, ReportList *reports, char *name, int overwrite) {
167         float mat[3][3];
168         float normal[3], plane[3];
169         int type;
170
171         type = getTransformOrientation(C, normal, plane, 0);
172         
173         switch (type)
174         {
175                 case ORIENTATION_VERT:
176                         if (createSpaceNormal(mat, normal) == 0) {
177                                 BKE_reports_prepend(reports, "Cannot use vertex with zero-length normal");
178                                 return NULL;
179                         }
180         
181                         if (name[0] == 0)
182                         {
183                                 strcpy(name, "Vertex");
184                         }
185                         break;
186                 case ORIENTATION_EDGE:
187                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
188                                 BKE_reports_prepend(reports, "Cannot use zero-length edge");
189                                 return NULL;
190                         }
191         
192                         if (name[0] == 0)
193                         {
194                                 strcpy(name, "Edge");
195                         }
196                         break;
197                 case ORIENTATION_FACE:
198                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
199                                 BKE_reports_prepend(reports, "Cannot use zero-area face");
200                                 return NULL;
201                         }
202         
203                         if (name[0] == 0)
204                         {
205                                 strcpy(name, "Face");
206                         }
207                         break;
208                 default:
209                         return NULL;
210                         break;
211         }
212
213         return addMatrixSpace(C, mat, name, overwrite);
214 }
215
216 int createSpaceNormal(float mat[3][3], float normal[3])
217 {
218         float tangent[3] = {0.0f, 0.0f, 1.0f};
219         
220         VECCOPY(mat[2], normal);
221         if (normalize_v3(mat[2]) == 0.0f) {
222                 return 0; /* error return */
223         }
224
225         cross_v3_v3v3(mat[0], mat[2], tangent);
226         if (dot_v3v3(mat[0], mat[0]) == 0.0f) {
227                 tangent[0] = 1.0f;
228                 tangent[1] = tangent[2] = 0.0f;
229                 cross_v3_v3v3(mat[0], tangent, mat[2]);
230         }
231
232         cross_v3_v3v3(mat[1], mat[2], mat[0]);
233
234         normalize_m3(mat);
235         
236         return 1;
237 }
238
239 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3])
240 {
241         VECCOPY(mat[2], normal);
242         if (normalize_v3(mat[2]) == 0.0f) {
243                 return 0; /* error return */
244         }
245         
246         /* preempt zero length tangent from causing trouble */
247         if (tangent[0] == 0 && tangent[1] == 0 && tangent[2] == 0)
248         {
249                 tangent[2] = 1;
250         }
251
252         cross_v3_v3v3(mat[0], mat[2], tangent);
253         if (normalize_v3(mat[0]) == 0.0f) {
254                 return 0; /* error return */
255         }
256         
257         cross_v3_v3v3(mat[1], mat[2], mat[0]);
258
259         normalize_m3(mat);
260         
261         return 1;
262 }
263
264 TransformOrientation* addMatrixSpace(bContext *C, float mat[3][3], char name[], int overwrite) {
265         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
266         TransformOrientation *ts = NULL;
267
268         if (overwrite)
269         {
270                 ts = findOrientationName(transform_spaces, name);
271         }
272         else
273         {
274                 uniqueOrientationName(transform_spaces, name);
275         }
276
277         /* if not, create a new one */
278         if (ts == NULL)
279         {
280                 ts = MEM_callocN(sizeof(TransformOrientation), "UserTransSpace from matrix");
281                 BLI_addtail(transform_spaces, ts);
282                 strncpy(ts->name, name, 35);
283         }
284
285         /* copy matrix into transform space */
286         copy_m3_m3(ts->mat, mat);
287
288         return ts;
289 }
290
291 void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target) {
292         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
293         TransformOrientation *ts = transform_spaces->first;
294         int i;
295         
296         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
297                 if (ts == target) {
298                         View3D *v3d = CTX_wm_view3d(C);
299                         if(v3d) {
300                                 int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
301                                 
302                                 // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
303                                 if (selected_index == i) {
304                                         v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
305                                 }
306                                 else if (selected_index > i) {
307                                         v3d->twmode--;
308                                 }
309                                 
310                         }
311
312                         BLI_freelinkN(transform_spaces, ts);
313                         break;
314                 }
315         }
316 }
317
318 void BIF_removeTransformOrientationIndex(bContext *C, int index) {
319         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
320         TransformOrientation *ts= BLI_findlink(transform_spaces, index);
321
322         if (ts) {
323                 View3D *v3d = CTX_wm_view3d(C);
324                 if(v3d) {
325                         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
326                         
327                         // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
328                         if (selected_index == index) {
329                                 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
330                         }
331                         else if (selected_index > index) {
332                                 v3d->twmode--;
333                         }
334                         
335                 }
336
337                 BLI_freelinkN(transform_spaces, ts);
338         }
339 }
340
341 void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target) {
342         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
343         View3D *v3d = CTX_wm_view3d(C);
344         TransformOrientation *ts = transform_spaces->first;
345         int i;
346         
347         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
348                 if (ts == target) {
349                         v3d->twmode = V3D_MANIP_CUSTOM + i;
350                         break;
351                 }
352         }
353 }
354
355 void BIF_selectTransformOrientationValue(bContext *C, int orientation) {
356         View3D *v3d = CTX_wm_view3d(C);
357         if(v3d) /* currently using generic poll */
358                 v3d->twmode = orientation;
359 }
360
361 EnumPropertyItem *BIF_enumTransformOrientation(bContext *C)
362 {
363         Scene *scene;
364         ListBase *transform_spaces;
365         TransformOrientation *ts= NULL;
366
367         EnumPropertyItem global = {V3D_MANIP_GLOBAL, "GLOBAL", 0, "Global", ""};
368         EnumPropertyItem normal = {V3D_MANIP_NORMAL, "NORMAL", 0, "Normal", ""};
369         EnumPropertyItem local = {V3D_MANIP_LOCAL, "LOCAL", 0, "Local", ""};
370         EnumPropertyItem view = {V3D_MANIP_VIEW, "VIEW", 0, "View", ""};
371         EnumPropertyItem tmp = {0, "", 0, "", ""};
372         EnumPropertyItem *item= NULL;
373         int i = V3D_MANIP_CUSTOM, totitem= 0;
374
375         RNA_enum_item_add(&item, &totitem, &global);
376         RNA_enum_item_add(&item, &totitem, &normal);
377         RNA_enum_item_add(&item, &totitem, &local);
378         RNA_enum_item_add(&item, &totitem, &view);
379
380         if(C) {
381                 scene= CTX_data_scene(C);
382
383                 if(scene) {
384                         transform_spaces = &scene->transform_spaces;
385                         ts = transform_spaces->first;
386                 }
387         }
388                 
389         if(ts)
390                 RNA_enum_item_add_separator(&item, &totitem);
391
392         for(; ts; ts = ts->next) {
393                 tmp.identifier = "CUSTOM";
394                 tmp.name= ts->name;
395                 tmp.value = i++;
396                 RNA_enum_item_add(&item, &totitem, &tmp);
397         }
398
399         RNA_enum_item_end(&item, &totitem);
400
401         return item;
402 }
403
404 const char * BIF_menustringTransformOrientation(const bContext *C, const char *title) {
405         char menu[] = "%t|Global%x0|Local%x1|Gimbal%x4|Normal%x2|View%x3";
406         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
407         TransformOrientation *ts;
408         int i = V3D_MANIP_CUSTOM;
409         char *str_menu, *p;
410         
411         
412         str_menu = MEM_callocN(strlen(menu) + strlen(title) + 1 + 40 * BIF_countTransformOrientation(C), "UserTransSpace from matrix");
413         p = str_menu;
414         
415         p += sprintf(str_menu, "%s", title);
416         p += sprintf(p, "%s", menu);
417         
418         for (ts = transform_spaces->first; ts; ts = ts->next) {
419                 p += sprintf(p, "|%s%%x%d", ts->name, i++);
420         }
421         
422         return str_menu;
423 }
424
425 int BIF_countTransformOrientation(const bContext *C) {
426         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
427         TransformOrientation *ts;
428         int count = 0;
429
430         for (ts = transform_spaces->first; ts; ts = ts->next) {
431                 count++;
432         }
433         
434         return count;
435 }
436
437 void applyTransformOrientation(const bContext *C, float mat[3][3], char *name) {
438         TransformOrientation *ts;
439         View3D *v3d = CTX_wm_view3d(C);
440         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
441         int i;
442         
443         if (selected_index >= 0) {
444                 for (i = 0, ts = CTX_data_scene(C)->transform_spaces.first; ts; ts = ts->next, i++) {
445                         if (selected_index == i) {
446                                 
447                                 if (name)
448                                         strcpy(name, ts->name);
449                                 
450                                 copy_m3_m3(mat, ts->mat);
451                                 break;
452                         }
453                 }
454           }
455 }
456
457 static int count_bone_select(bArmature *arm, ListBase *lb, int do_it) 
458 {
459         Bone *bone;
460         int do_next;
461         int total = 0;
462         
463         for(bone= lb->first; bone; bone= bone->next) {
464                 bone->flag &= ~BONE_TRANSFORM;
465                 do_next = do_it;
466                 if(do_it) {
467                         if(bone->layer & arm->layer) {
468                                 if (bone->flag & BONE_SELECTED) {
469                                         bone->flag |= BONE_TRANSFORM;
470                                         total++;
471                                         do_next= 0;     // no transform on children if one parent bone is selected
472                                 }
473                         }
474                 }
475                 total += count_bone_select(arm, &bone->childbase, do_next);
476         }
477         
478         return total;
479 }
480
481 void initTransformOrientation(bContext *C, TransInfo *t)
482 {
483         View3D *v3d = CTX_wm_view3d(C);
484         Object *ob = CTX_data_active_object(C);
485         Object *obedit = CTX_data_active_object(C);
486
487         switch(t->current_orientation) {
488         case V3D_MANIP_GLOBAL:
489                 strcpy(t->spacename, "global");
490                 break;
491
492         case V3D_MANIP_GIMBAL:
493                 unit_m3(t->spacemtx);
494                 if (gimbal_axis(ob, t->spacemtx)) {
495                         strcpy(t->spacename, "gimbal");
496                         break;
497                 }
498                 /* no gimbal fallthrough to normal */
499         case V3D_MANIP_NORMAL:
500                 if(obedit || (ob && ob->mode & OB_MODE_POSE)) {
501                         strcpy(t->spacename, "normal");
502                         ED_getTransformOrientationMatrix(C, t->spacemtx, (v3d->around == V3D_ACTIVE));
503                         break;
504                 }
505                 /* no break we define 'normal' as 'local' in Object mode */
506         case V3D_MANIP_LOCAL:
507                 strcpy(t->spacename, "local");
508                 
509                 if(ob) {
510                         copy_m3_m4(t->spacemtx, ob->obmat);
511                         normalize_m3(t->spacemtx);
512                 } else {
513                         unit_m3(t->spacemtx);
514                 }
515                 
516                 break;
517                 
518         case V3D_MANIP_VIEW:
519                 if (t->ar->regiontype == RGN_TYPE_WINDOW)
520                 {
521                         RegionView3D *rv3d = t->ar->regiondata;
522                         float mat[3][3];
523
524                         strcpy(t->spacename, "view");
525                         copy_m3_m4(mat, rv3d->viewinv);
526                         normalize_m3(mat);
527                         copy_m3_m3(t->spacemtx, mat);
528                 }
529                 else
530                 {
531                         unit_m3(t->spacemtx);
532                 }
533                 break;
534         default: /* V3D_MANIP_CUSTOM */
535                 applyTransformOrientation(C, t->spacemtx, t->spacename);
536                 break;
537         }
538 }
539
540 int getTransformOrientation(const bContext *C, float normal[3], float plane[3], int activeOnly)
541 {
542         Scene *scene = CTX_data_scene(C);
543         View3D *v3d = CTX_wm_view3d(C);
544         Object *obedit= CTX_data_edit_object(C);
545         Base *base;
546         Object *ob = OBACT;
547         int result = ORIENTATION_NONE;
548
549         normal[0] = normal[1] = normal[2] = 0;
550         plane[0] = plane[1] = plane[2] = 0;
551
552         if(obedit)
553         {
554                 float imat[3][3], mat[3][3];
555                 
556                 /* we need the transpose of the inverse for a normal... */
557                 copy_m3_m4(imat, ob->obmat);
558                 
559                 invert_m3_m3(mat, imat);
560                 transpose_m3(mat);
561
562                 ob= obedit;
563
564                 if(ob->type==OB_MESH)
565                 {
566                         Mesh *me= ob->data;
567                         EditMesh *em = me->edit_mesh;
568                         EditVert *eve;
569                         EditSelection ese;
570                         float vec[3]= {0,0,0};
571                         
572                         /* USE LAST SELECTED WITH ACTIVE */
573                         if (activeOnly && EM_get_actSelection(em, &ese))
574                         {
575                                 EM_editselection_normal(normal, &ese);
576                                 EM_editselection_plane(plane, &ese);
577                                 
578                                 switch (ese.type)
579                                 {
580                                         case EDITVERT:
581                                                 result = ORIENTATION_VERT;
582                                                 break;
583                                         case EDITEDGE:
584                                                 result = ORIENTATION_EDGE;
585                                                 break;
586                                         case EDITFACE:
587                                                 result = ORIENTATION_FACE;
588                                                 break;
589                                 }
590                         }
591                         else
592                         {
593                                 if (em->totfacesel >= 1)
594                                 {
595                                         EditFace *efa;
596                                         
597                                         for(efa= em->faces.first; efa; efa= efa->next)
598                                         {
599                                                 if(efa->f & SELECT)
600                                                 {
601                                                         VECADD(normal, normal, efa->n);
602                                                         sub_v3_v3v3(vec, efa->v2->co, efa->v1->co);
603                                                         VECADD(plane, plane, vec);
604                                                 }
605                                         }
606                                         
607                                         result = ORIENTATION_FACE;
608                                 }
609                                 else if (em->totvertsel == 3)
610                                 {
611                                         EditVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
612                                         float cotangent[3];
613                                         
614                                         for (eve = em->verts.first; eve; eve = eve->next)
615                                         {
616                                                 if ( eve->f & SELECT ) {
617                                                         if (v1 == NULL) {
618                                                                 v1 = eve; 
619                                                         }
620                                                         else if (v2 == NULL) {
621                                                                 v2 = eve;
622                                                         }
623                                                         else {
624                                                                 v3 = eve;
625
626                                                                 sub_v3_v3v3(plane, v2->co, v1->co);
627                                                                 sub_v3_v3v3(cotangent, v3->co, v2->co);
628                                                                 cross_v3_v3v3(normal, cotangent, plane);
629                                                                 break;
630                                                         }
631                                                 }
632                                         }
633
634                                         /* if there's an edge available, use that for the tangent */
635                                         if (em->totedgesel >= 1)
636                                         {
637                                                 EditEdge *eed = NULL;
638         
639                                                 for(eed= em->edges.first; eed; eed= eed->next) {
640                                                         if(eed->f & SELECT) {
641                                                                 sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
642                                                                 break;
643                                                         }
644                                                 }
645                                         }
646
647                                         result = ORIENTATION_FACE;
648                                 }
649                                 else if (em->totedgesel == 1)
650                                 {
651                                         EditEdge *eed;
652
653                                         for(eed= em->edges.first; eed; eed= eed->next) {
654                                                 if(eed->f & SELECT) {
655                                                         /* use average vert normals as plane and edge vector as normal */
656                                                         VECCOPY(plane, eed->v1->no);
657                                                         VECADD(plane, plane, eed->v2->no);
658                                                         sub_v3_v3v3(normal, eed->v2->co, eed->v1->co);
659                                                         break;
660                                                 }
661                                         }
662                                         result = ORIENTATION_EDGE;
663                                 }
664                                 else if (em->totvertsel == 2)
665                                 {
666                                         EditVert *v1 = NULL, *v2 = NULL;
667                 
668                                         for (eve = em->verts.first; eve; eve = eve->next)
669                                         {
670                                                 if ( eve->f & SELECT ) {
671                                                         if (v1 == NULL) {
672                                                                 v1 = eve; 
673                                                         }
674                                                         else {
675                                                                 v2 = eve;
676                                                                 
677                                                                 VECCOPY(plane, v1->no);
678                                                                 VECADD(plane, plane, v2->no);
679                                                                 sub_v3_v3v3(normal, v2->co, v1->co);
680                                                                 break; 
681                                                         }
682                                                 }
683                                         }
684                                         result = ORIENTATION_EDGE;
685                                 }
686                                 else if (em->totvertsel == 1)
687                                 {
688                                         for (eve = em->verts.first; eve; eve = eve->next)
689                                         {
690                                                 if ( eve->f & SELECT ) {
691                                                         VECCOPY(normal, eve->no);
692                                                         break;
693                                                 }
694                                         }
695                                         result = ORIENTATION_VERT;
696                                 }
697                                 else if (em->totvertsel > 3)
698                                 {
699                                         normal[0] = normal[1] = normal[2] = 0;
700                                         
701                                         for (eve = em->verts.first; eve; eve = eve->next)
702                                         {
703                                                 if ( eve->f & SELECT ) {
704                                                         add_v3_v3(normal, eve->no);
705                                                 }
706                                         }
707                                         normalize_v3(normal);
708                                         result = ORIENTATION_VERT;
709                                 }
710                         }
711                 } /* end editmesh */
712                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
713                 {
714                         Curve *cu= obedit->data;
715                         Nurb *nu;
716                         BezTriple *bezt;
717                         int a;
718                         ListBase *nurbs= ED_curve_editnurbs(cu);
719
720                         for (nu = nurbs->first; nu; nu = nu->next)
721                         {
722                                 /* only bezier has a normal */
723                                 if(nu->type == CU_BEZIER)
724                                 {
725                                         bezt= nu->bezt;
726                                         a= nu->pntsu;
727                                         while(a--)
728                                         {
729                                                 /* exception */
730                                                 if ( (bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT )
731                                                 {
732                                                         sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
733                                                 }
734                                                 else
735                                                 {
736                                                         if(bezt->f1)
737                                                         {
738                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[1]);
739                                                         }
740                                                         if(bezt->f2)
741                                                         {
742                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
743                                                         }
744                                                         if(bezt->f3)
745                                                         {
746                                                                 sub_v3_v3v3(normal, bezt->vec[1], bezt->vec[2]);
747                                                         }
748                                                 }
749                                                 bezt++;
750                                         }
751                                 }
752                         }
753                         
754                         if (normal[0] != 0 || normal[1] != 0 || normal[2] != 0)
755                         {
756                                 result = ORIENTATION_NORMAL;
757                         }
758                 }
759                 else if(obedit->type==OB_MBALL)
760                 {
761 #if 0 // XXX
762                         /* editmball.c */
763                         extern ListBase editelems;  /* go away ! */
764                         MetaElem *ml, *ml_sel = NULL;
765         
766                         /* loop and check that only one element is selected */  
767                         for (ml = editelems.first; ml; ml = ml->next)
768                         {
769                                 if (ml->flag & SELECT) {
770                                         if (ml_sel == NULL)
771                                         {
772                                                 ml_sel = ml;
773                                         }
774                                         else
775                                         {
776                                                 ml_sel = NULL;
777                                                 break;
778                                         }
779                                 }
780                         }
781                         
782                         if (ml_sel)
783                         {       
784                                 float mat[4][4];
785
786                                 /* Rotation of MetaElem is stored in quat */
787                                  quat_to_mat4( mat,ml_sel->quat);
788
789                                 VECCOPY(normal, mat[2]);
790
791                                 negate_v3_v3(plane, mat[1]);
792                                 
793                                 result = ORIENTATION_NORMAL;
794                         }
795 #endif
796                         
797                 }
798                 else if (obedit->type == OB_ARMATURE)
799                 {
800                         bArmature *arm = obedit->data;
801                         EditBone *ebone;
802                         
803                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
804                                 if (arm->layer & ebone->layer)
805                                 {
806                                         if (ebone->flag & BONE_SELECTED)
807                                         {
808                                                 float tmat[3][3];
809                                                 float vec[3];
810                                                 sub_v3_v3v3(vec, ebone->tail, ebone->head);
811                                                 normalize_v3(vec);
812                                                 add_v3_v3(normal, vec);
813                                                 
814                                                 vec_roll_to_mat3(vec, ebone->roll, tmat);
815                                                 add_v3_v3(plane, tmat[2]);
816                                         }
817                                 }
818                         }
819                         
820                         normalize_v3(normal);
821                         normalize_v3(plane);
822
823                         if (plane[0] != 0 || plane[1] != 0 || plane[2] != 0)
824                         {
825                                 result = ORIENTATION_EDGE;
826                         }
827
828                 }
829
830                 /* Vectors from edges don't need the special transpose inverse multiplication */
831                 if (result == ORIENTATION_EDGE)
832                 {
833                         mul_mat3_m4_v3(ob->obmat, normal);
834                         mul_mat3_m4_v3(ob->obmat, plane);
835                 }
836                 else
837                 {
838                         mul_m3_v3(mat, normal);
839                         mul_m3_v3(mat, plane);
840                 }
841         }
842         else if(ob && (ob->mode & OB_MODE_POSE))
843         {
844                 bArmature *arm= ob->data;
845                 bPoseChannel *pchan;
846                 int totsel;
847                 
848                 totsel = count_bone_select(arm, &arm->bonebase, 1);
849                 if(totsel) {
850                         float imat[3][3], mat[3][3];
851
852                         /* use channels to get stats */
853                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
854                                 if (pchan->bone && pchan->bone->flag & BONE_TRANSFORM) {
855                                         add_v3_v3(normal, pchan->pose_mat[2]);
856                                         add_v3_v3(plane, pchan->pose_mat[1]);
857                                 }
858                         }
859                         negate_v3(plane);
860                         
861                         /* we need the transpose of the inverse for a normal... */
862                         copy_m3_m4(imat, ob->obmat);
863                         
864                         invert_m3_m3(mat, imat);
865                         transpose_m3(mat);
866                         mul_m3_v3(mat, normal);
867                         mul_m3_v3(mat, plane);
868                         
869                         result = ORIENTATION_EDGE;
870                 }
871         }
872         else if(ob && (ob->mode & (OB_MODE_ALL_PAINT|OB_MODE_PARTICLE_EDIT)))
873         {
874         }
875         else {
876                 /* we need the one selected object, if its not active */
877                 ob = OBACT;
878                 if(ob && !(ob->flag & SELECT)) ob = NULL;
879                 
880                 for(base= scene->base.first; base; base= base->next) {
881                         if TESTBASELIB(v3d, base) {
882                                 if(ob == NULL) { 
883                                         ob= base->object;
884                                         break;
885                                 }
886                         }
887                 }
888                 
889                 if (ob) {
890                         VECCOPY(normal, ob->obmat[2]);
891                         VECCOPY(plane, ob->obmat[1]);
892                 }
893                 result = ORIENTATION_NORMAL;
894         }
895         
896         return result;
897 }
898
899 void ED_getTransformOrientationMatrix(const bContext *C, float orientation_mat[][3], int activeOnly)
900 {
901         float normal[3]={0.0, 0.0, 0.0};
902         float plane[3]={0.0, 0.0, 0.0};
903
904         int type;
905
906         type = getTransformOrientation(C, normal, plane, activeOnly);
907
908         switch (type)
909         {
910                 case ORIENTATION_NORMAL:
911                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
912                         {
913                                 type = ORIENTATION_NONE;
914                         }
915                         break;
916                 case ORIENTATION_VERT:
917                         if (createSpaceNormal(orientation_mat, normal) == 0)
918                         {
919                                 type = ORIENTATION_NONE;
920                         }
921                         break;
922                 case ORIENTATION_EDGE:
923                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
924                         {
925                                 type = ORIENTATION_NONE;
926                         }
927                         break;
928                 case ORIENTATION_FACE:
929                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
930                         {
931                                 type = ORIENTATION_NONE;
932                         }
933                         break;
934         }
935
936         if (type == ORIENTATION_NONE)
937         {
938                 unit_m3(orientation_mat);
939         }
940 }