committing working copy
[blender.git] / source / blender / editors / space_view3d / view3d_buttons.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) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_armature_types.h"
35 #include "DNA_curve_types.h"
36 #include "DNA_lattice_types.h"
37 #include "DNA_meta_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_math.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_action.h"
50 #include "BKE_brush.h"
51 #include "BKE_context.h"
52 #include "BKE_curve.h"
53 #include "BKE_customdata.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_idprop.h"
56 #include "BKE_mesh.h"
57 #include "BKE_object.h"
58 #include "BKE_global.h"
59 #include "BKE_scene.h"
60 #include "BKE_screen.h"
61 #include "BKE_utildefines.h"
62 #include "BKE_tessmesh.h"
63 #include "BKE_deform.h"
64
65 #include "BIF_gl.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "RNA_access.h"
71
72 #include "ED_armature.h"
73 #include "ED_gpencil.h"
74 #include "ED_mesh.h"
75 #include "ED_screen.h"
76 #include "ED_transform.h"
77
78 #include "UI_interface.h"
79 #include "UI_resources.h"
80
81 #include "view3d_intern.h"      // own include
82
83
84 /* ******************* view3d space & buttons ************** */
85 #define B_NOP           1
86 #define B_REDR          2
87 #define B_OBJECTPANELROT        1007
88 #define B_OBJECTPANELMEDIAN 1008
89 #define B_ARMATUREPANEL1        1009
90 #define B_ARMATUREPANEL2        1010
91 #define B_OBJECTPANELPARENT 1011
92 #define B_OBJECTPANEL           1012
93 #define B_ARMATUREPANEL3        1013
94 #define B_OBJECTPANELSCALE      1014
95 #define B_OBJECTPANELDIMS       1015
96 #define B_TRANSFORMSPACEADD     1016
97 #define B_TRANSFORMSPACECLEAR   1017
98 #define B_SETPT_AUTO    2125
99 #define B_SETPT_VECTOR  2126
100 #define B_SETPT_ALIGN   2127
101 #define B_SETPT_FREE    2128
102 #define B_RECALCMBALL   2501
103
104 #define B_WEIGHT0_0             2840
105 #define B_WEIGHT1_4             2841
106 #define B_WEIGHT1_2             2842
107 #define B_WEIGHT3_4             2843
108 #define B_WEIGHT1_0             2844
109
110 #define B_OPA1_8                2845
111 #define B_OPA1_4                2846
112 #define B_OPA1_2                2847
113 #define B_OPA3_4                2848
114 #define B_OPA1_0                2849
115
116 #define B_CLR_WPAINT    2850
117
118 #define B_RV3D_LOCKED   2900
119 #define B_RV3D_BOXVIEW  2901
120 #define B_RV3D_BOXCLIP  2902
121
122 #define B_IDNAME                3000
123
124 /* temporary struct for storing transform properties */
125 typedef struct {
126         float ob_eul[4];        // used for quat too....
127         float ob_scale[3]; // need temp space due to linked values
128         float ob_dims[3];
129         short link_scale;
130         float ve_median[6];
131         int curdef;
132         float *defweightp;
133 } TransformProperties;
134
135
136 /* is used for both read and write... */
137 static void v3d_editvertex_buts(const bContext *C, uiLayout *layout, View3D *v3d, Object *ob, float lim)
138 {
139         uiBlock *block= (layout)? uiLayoutAbsoluteBlock(layout): NULL;
140         MDeformVert *dvert=NULL;
141         TransformProperties *tfp= v3d->properties_storage;
142         float median[6], ve_median[6];
143         int tot, totw, totweight, totedge, totradius;
144         char defstr[320];
145         
146         median[0]= median[1]= median[2]= median[3]= median[4]= median[5]= 0.0;
147         tot= totw= totweight= totedge= totradius= 0;
148         defstr[0]= 0;
149
150         if(ob->type==OB_MESH) {
151                 Mesh *me= ob->data;
152                 BMEditMesh *em = me->edit_btmesh;
153                 BMesh *bm = em->bm;
154                 BMVert *eve, *evedef=NULL;
155                 BMEdge *eed;
156                 BMIter iter;
157                 
158                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
159                         if(BM_TestHFlag(eve, BM_SELECT)) {
160                                 evedef= eve;
161                                 tot++;
162                                 add_v3_v3(median, eve->co);
163                         }
164                 }
165
166                 BM_ITER(eed, &iter, bm, BM_EDGES_OF_MESH, NULL) {
167                         if(BM_TestHFlag(eed, BM_SELECT)) {
168                                 float *f = bm_get_cd_float(&bm->edata, eed->head.data, CD_CREASE);
169
170                                 totedge++;
171                                 median[3]+= f ? *f : 0.0f;
172                         }
173                 }
174
175                 /* check for defgroups */
176                 if(evedef)
177                         dvert= CustomData_bmesh_get(&em->bm->vdata, evedef->head.data, CD_MDEFORMVERT);
178                 if(tot==1 && dvert && dvert->totweight) {
179                         bDeformGroup *dg;
180                         int i, max=1, init=1;
181                         char str[320];
182                         
183                         for (i=0; i<dvert->totweight; i++){
184                                 dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
185                                 if(dg) {
186                                         max+= BLI_snprintf(str, sizeof(str), "%s %%x%d|", dg->name, dvert->dw[i].def_nr); 
187                                         if(max<320) strcat(defstr, str);
188                                 }
189
190                                 if(tfp->curdef==dvert->dw[i].def_nr) {
191                                         init= 0;
192                                         tfp->defweightp= &dvert->dw[i].weight;
193                                 }
194                         }
195                         
196                         if(init) {      // needs new initialized 
197                                 tfp->curdef= dvert->dw[0].def_nr;
198                                 tfp->defweightp= &dvert->dw[0].weight;
199                         }
200                 }
201         }
202         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
203                 Curve *cu= ob->data;
204                 Nurb *nu;
205                 BPoint *bp;
206                 BezTriple *bezt;
207                 int a;
208                 
209                 nu= cu->editnurb->first;
210                 while(nu) {
211                         if(nu->type == CU_BEZIER) {
212                                 bezt= nu->bezt;
213                                 a= nu->pntsu;
214                                 while(a--) {
215                                         if(bezt->f2 & SELECT) {
216                                                 add_v3_v3(median, bezt->vec[1]);
217                                                 tot++;
218                                                 median[4]+= bezt->weight;
219                                                 totweight++;
220                                                 median[5]+= bezt->radius;
221                                                 totradius++;
222                                         }
223                                         else {
224                                                 if(bezt->f1 & SELECT) {
225                                                         add_v3_v3(median, bezt->vec[0]);
226                                                         tot++;
227                                                 }
228                                                 if(bezt->f3 & SELECT) {
229                                                         add_v3_v3(median, bezt->vec[2]);
230                                                         tot++;
231                                                 }
232                                         }
233                                         bezt++;
234                                 }
235                         }
236                         else {
237                                 bp= nu->bp;
238                                 a= nu->pntsu*nu->pntsv;
239                                 while(a--) {
240                                         if(bp->f1 & SELECT) {
241                                                 add_v3_v3(median, bp->vec);
242                                                 median[3]+= bp->vec[3];
243                                                 totw++;
244                                                 tot++;
245                                                 median[4]+= bp->weight;
246                                                 totweight++;
247                                                 median[5]+= bp->radius;
248                                                 totradius++;
249                                         }
250                                         bp++;
251                                 }
252                         }
253                         nu= nu->next;
254                 }
255         }
256         else if(ob->type==OB_LATTICE) {
257                 Lattice *lt= ob->data;
258                 BPoint *bp;
259                 int a;
260                 
261                 a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
262                 bp= lt->editlatt->def;
263                 while(a--) {
264                         if(bp->f1 & SELECT) {
265                                 add_v3_v3(median, bp->vec);
266                                 tot++;
267                                 median[4]+= bp->weight;
268                                 totweight++;
269                         }
270                         bp++;
271                 }
272         }
273         
274         if(tot==0) return;
275
276         median[0] /= (float)tot;
277         median[1] /= (float)tot;
278         median[2] /= (float)tot;
279         if(totedge) median[3] /= (float)totedge;
280         else if(totw) median[3] /= (float)totw;
281         if(totweight) median[4] /= (float)totweight;
282         if(totradius) median[5] /= (float)totradius;
283         
284         if(v3d->flag & V3D_GLOBAL_STATS)
285                 mul_m4_v3(ob->obmat, median);
286         
287         if(block) {     // buttons
288                 int but_y;
289                 if((ob->parent) && (ob->partype == PARBONE))    but_y = 135;
290                 else                                                                                    but_y = 150;
291                 
292                 
293                 
294                 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
295                 
296                 uiBlockBeginAlign(block);
297                 if(tot==1) {
298                         uiDefBut(block, LABEL, 0, "Vertex:",                                    0, 130, 200, 20, 0, 0, 0, 0, 0, "");
299                         uiBlockBeginAlign(block);
300                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",                0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
301                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",                0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
302                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",                0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
303                         
304                         if(totw==1) {
305                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
306                                 uiBlockBeginAlign(block);
307                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
308                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
309                                 uiBlockEndAlign(block);
310                                 if(totweight)
311                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
312                                 if(totradius)
313                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
314                                 }
315                         else {
316                                 uiBlockBeginAlign(block);
317                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
318                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
319                                 uiBlockEndAlign(block);
320                                 if(totweight)
321                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
322                                 if(totradius)
323                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 20, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
324                         }
325                 }
326                 else {
327                         uiDefBut(block, LABEL, 0, "Median:",                                    0, 130, 200, 20, 0, 0, 0, 0, 0, "");
328                         uiBlockBeginAlign(block);
329                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",                0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
330                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",                0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
331                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",                0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
332                         if(totw==tot) {
333                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
334                                 uiBlockEndAlign(block);
335                                 uiBlockBeginAlign(block);
336                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
337                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
338                                 uiBlockEndAlign(block);
339                                 if(totweight)
340                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
341                                 if(totradius)
342                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
343                                 uiBlockEndAlign(block);
344                         }
345                         else {
346                                 uiBlockBeginAlign(block);
347                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
348                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
349                                 uiBlockEndAlign(block);
350                                 if(totweight)
351                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
352                                 if(totradius)
353                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
354                                 uiBlockEndAlign(block);
355                         }
356                 }
357                                 
358                 if(totedge==1)
359                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease:",   0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
360                 else if(totedge>1)
361                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease:",    0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
362                 
363         }
364         else {  // apply
365                 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
366                 
367                 if(v3d->flag & V3D_GLOBAL_STATS) {
368                         invert_m4_m4(ob->imat, ob->obmat);
369                         mul_m4_v3(ob->imat, median);
370                         mul_m4_v3(ob->imat, ve_median);
371                 }
372                 sub_v3_v3v3(median, ve_median, median);
373                 median[3]= ve_median[3]-median[3];
374                 median[4]= ve_median[4]-median[4];
375                 median[5]= ve_median[5]-median[5];
376                 
377                 if(ob->type==OB_MESH) {
378                         Mesh *me= ob->data;
379                         BMEditMesh *em = me->edit_btmesh;
380                         BMVert *eve;
381                         BMEdge *eed;
382                         BMIter iter;
383
384                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
385                                 if(BM_TestHFlag(eve, BM_SELECT)) {
386                                         add_v3_v3(eve->co, median);
387                                 }
388                         }
389                         
390                         BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
391                                 if(BM_TestHFlag(eed, BM_SELECT)) {
392                                         float *crease = CustomData_bmesh_get(&em->bm->edata, eed->head.data, CD_CREASE);
393                                         if (!crease) continue;
394                                         
395                                         /* ensure the median can be set to zero or one */
396                                         if(ve_median[3]==0.0f) *crease= 0.0f;
397                                         else if(ve_median[3]==1.0f) *crease= 1.0f;
398                                         else {
399                                                 *crease+= median[3];
400                                                 CLAMP(*crease, 0.0, 1.0);
401                                         }
402                                 }
403                         }
404                         
405                         EDBM_RecalcNormals(em);
406                 }
407                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
408                         Curve *cu= ob->data;
409                         Nurb *nu;
410                         BPoint *bp;
411                         BezTriple *bezt;
412                         int a;
413                         
414                         nu= cu->editnurb->first;
415                         while(nu) {
416                                 if(nu->type == CU_BEZIER) {
417                                         bezt= nu->bezt;
418                                         a= nu->pntsu;
419                                         while(a--) {
420                                                 if(bezt->f2 & SELECT) {
421                                                         add_v3_v3(bezt->vec[0], median);
422                                                         add_v3_v3(bezt->vec[1], median);
423                                                         add_v3_v3(bezt->vec[2], median);
424                                                         bezt->weight+= median[4];
425                                                         bezt->radius+= median[5];
426                                                 }
427                                                 else {
428                                                         if(bezt->f1 & SELECT) {
429                                                                 add_v3_v3(bezt->vec[0], median);
430                                                         }
431                                                         if(bezt->f3 & SELECT) {
432                                                                 add_v3_v3(bezt->vec[2], median);
433                                                         }
434                                                 }
435                                                 bezt++;
436                                         }
437                                 }
438                                 else {
439                                         bp= nu->bp;
440                                         a= nu->pntsu*nu->pntsv;
441                                         while(a--) {
442                                                 if(bp->f1 & SELECT) {
443                                                         add_v3_v3(bp->vec, median);
444                                                         bp->vec[3]+= median[3];
445                                                         bp->weight+= median[4];
446                                                         bp->radius+= median[5];
447                                                 }
448                                                 bp++;
449                                         }
450                                 }
451                                 test2DNurb(nu);
452                                 testhandlesNurb(nu); /* test for bezier too */
453
454                                 nu= nu->next;
455                         }
456                 }
457                 else if(ob->type==OB_LATTICE) {
458                         Lattice *lt= ob->data;
459                         BPoint *bp;
460                         int a;
461                         
462                         a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
463                         bp= lt->editlatt->def;
464                         while(a--) {
465                                 if(bp->f1 & SELECT) {
466                                         add_v3_v3(bp->vec, median);
467                                         bp->weight+= median[4];
468                                 }
469                                 bp++;
470                         }
471                 }
472                 
473 //              ED_undo_push(C, "Transform properties");
474         }
475 }
476 #define B_VGRP_PNL_COPY 1
477 #define B_VGRP_PNL_NORMALIZE 2
478 #define B_VGRP_PNL_EDIT_SINGLE 8 /* or greater */
479 #define B_VGRP_PNL_COPY_SINGLE 16384 /* or greater */
480
481 static void act_vert_def(Object *ob, BMVert **eve, MDeformVert **dvert)
482 {
483         if(ob && ob->mode & OB_MODE_EDIT && ob->type==OB_MESH && ob->defbase.first) {
484                 Mesh *me= ob->data;
485                 BMEditMesh *em = me->edit_btmesh;
486                 BMEditSelection *ese = ((BMEditSelection*)em->bm->selected.last);
487
488                 if(ese && ese->type == BM_VERT) {
489                         *eve= (BMVert*)ese->data;
490                         *dvert= CustomData_bmesh_get(&em->bm->vdata, (*eve)->head.data, CD_MDEFORMVERT);
491                         return;
492                 }
493         }
494
495         *eve= NULL;
496         *dvert= NULL;
497 }
498
499 static void editvert_mirror_update(Object *ob, BMVert *eve, int def_nr, int index)
500 {
501         Mesh *me= ob->data;
502 #if 0 //BMESH_TODO use editbmesh_bvh.h code for this! see EDBM_FindNearestVertTopo.
503         EditMesh *em = BKE_mesh_get_editmesh(me);
504         EditVert *eve_mirr;
505
506         eve_mirr= editmesh_get_x_mirror_vert(ob, em, eve, eve->co, index);
507
508         if(eve_mirr && eve_mirr != eve) {
509                 MDeformVert *dvert_src= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
510                 MDeformVert *dvert_dst= CustomData_em_get(&em->vdata, eve_mirr->data, CD_MDEFORMVERT);
511                 if(dvert_dst) {
512                         if(def_nr == -1) {
513                                 /* all vgroups, add groups where neded  */
514
515                                 int *flip_map= defgroup_flip_map(ob, 1);
516                                 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, 1);
517                                 MEM_freeN(flip_map);
518                         }
519                         else {
520                                 /* single vgroup */
521                                 MDeformWeight *dw= defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
522                                 if(dw) {
523                                         dw->weight= defvert_find_weight(dvert_src, def_nr);
524                                 }
525                         }
526                 }
527         }
528 #endif
529 }
530
531 static void vgroup_adjust_active(Object *ob, int def_nr)
532 {
533         BMVert *eve_act;
534         MDeformVert *dvert_act;
535
536         act_vert_def(ob, &eve_act, &dvert_act);
537
538         if(dvert_act) {
539                 if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
540                         editvert_mirror_update(ob, eve_act, def_nr, -1);
541         }
542 }
543
544 static void vgroup_copy_active_to_sel(Object *ob)
545 {
546         BMVert *eve_act;
547         MDeformVert *dvert_act;
548
549         act_vert_def(ob, &eve_act, &dvert_act);
550
551         if(dvert_act==NULL) {
552                 return;
553         }
554         else {
555                 Mesh *me= ob->data;
556                 BMEditMesh *em = me->edit_btmesh;
557                 BMIter iter;
558                 BMVert *eve;
559                 MDeformVert *dvert;
560                 int index= 0;
561
562                 BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
563                         if(BM_TestHFlag(eve, BM_SELECT) && eve != eve_act) {
564                                 dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
565                                 if(dvert) {
566                                         defvert_copy(dvert, dvert_act);
567
568                                         if(me->editflag & ME_EDIT_MIRROR_X)
569                                                 editvert_mirror_update(ob, eve, -1, index);
570
571                                 }
572                         }
573
574                         index++;
575                 }
576         }
577 }
578
579 static void vgroup_copy_active_to_sel_single(Object *ob, int def_nr)
580 {
581         BMVert *eve_act;
582         MDeformVert *dvert_act;
583
584         act_vert_def(ob, &eve_act, &dvert_act);
585
586         if(dvert_act==NULL) {
587                 return;
588         }
589         else {
590                 Mesh *me= ob->data;
591                 BMEditMesh *em = me->edit_btmesh;
592                 BMIter iter;
593                 BMVert *eve;
594                 MDeformVert *dvert;
595                 MDeformWeight *dw;
596                 float act_weight = -1.0f;
597                 int i;
598                 int index= 0;
599
600                 for(i=0, dw=dvert_act->dw; i < dvert_act->totweight; i++, dw++) {
601                         if(def_nr == dw->def_nr) {
602                                 act_weight= dw->weight;
603                                 break;
604                         }
605                 }
606
607                 if(act_weight < -0.5f)
608                         return;
609                 
610                 eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
611                 for (index=0; eve; eve=BMIter_Step(&iter), index++) {
612                         if(BM_TestHFlag(eve, BM_SELECT) && eve != eve_act) {
613                                 dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
614                                 if(dvert) {
615                                         for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
616                                                 if(def_nr == dw->def_nr) {
617                                                         dw->weight= act_weight;
618
619                                                         if(me->editflag & ME_EDIT_MIRROR_X)
620                                                                 editvert_mirror_update(ob, eve, -1, index);
621
622                                                         break;
623                                                 }
624                                         }
625                                 }
626                         }
627                 }
628
629                 if(me->editflag & ME_EDIT_MIRROR_X)
630                         editvert_mirror_update(ob, eve_act, -1, -1);
631
632         }
633 }
634
635 static void vgroup_normalize_active(Object *ob)
636 {
637         BMVert *eve_act;
638         MDeformVert *dvert_act;
639
640         act_vert_def(ob, &eve_act, &dvert_act);
641
642         if(dvert_act==NULL)
643                 return;
644
645         defvert_normalize(dvert_act);
646
647         if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
648                 editvert_mirror_update(ob, eve_act, -1, -1);
649
650
651
652 }
653
654 static void do_view3d_vgroup_buttons(bContext *C, void *arg, int event)
655 {
656         Scene *scene= CTX_data_scene(C);
657         Object *ob= OBACT;
658
659         if(event==B_VGRP_PNL_NORMALIZE) {
660                 vgroup_normalize_active(ob);
661         }
662         else if(event == B_VGRP_PNL_COPY) {
663                 vgroup_copy_active_to_sel(ob);
664         }
665         else if(event >= B_VGRP_PNL_COPY_SINGLE) {
666                 vgroup_copy_active_to_sel_single(ob, event - B_VGRP_PNL_COPY_SINGLE);
667         }
668         else if(event >= B_VGRP_PNL_EDIT_SINGLE) {
669                 vgroup_adjust_active(ob, event - B_VGRP_PNL_EDIT_SINGLE);
670         }
671
672 //  todo
673 //      if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
674 //              ED_vgroup_mirror(ob, 1, 1, 0);
675
676         /* default for now */
677         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
678         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
679 }
680
681 int view3d_panel_vgroup_poll(const bContext *C, PanelType *pt)
682 {
683         Scene *scene= CTX_data_scene(C);
684         Object *ob= OBACT;
685         BMVert *eve_act;
686         MDeformVert *dvert_act;
687
688         act_vert_def(ob, &eve_act, &dvert_act);
689
690         return dvert_act ? dvert_act->totweight : 0;
691 }
692
693
694 static void view3d_panel_vgroup(const bContext *C, Panel *pa)
695 {
696         uiBlock *block= uiLayoutAbsoluteBlock(pa->layout);
697         Scene *scene= CTX_data_scene(C);
698         Object *ob= OBACT;
699
700         BMVert *eve;
701         MDeformVert *dvert;
702
703         act_vert_def(ob, &eve, &dvert);
704
705         if(dvert && dvert->totweight) {
706                 uiLayout *col;
707                 bDeformGroup *dg;
708                 int i;
709                 int yco = 0;
710
711                 uiBlockSetHandleFunc(block, do_view3d_vgroup_buttons, NULL);
712
713                 col= uiLayoutColumn(pa->layout, 0);
714                 block= uiLayoutAbsoluteBlock(col);
715
716                 uiBlockBeginAlign(block);
717
718                 for (i=0; i<dvert->totweight; i++){
719                         dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
720                         if(dg) {
721                                 uiDefButF(block, NUM, B_VGRP_PNL_EDIT_SINGLE + dvert->dw[i].def_nr, dg->name,   0, yco, 180, 20, &dvert->dw[i].weight, 0.0, 1.0, 1, 3, "");
722                                 uiDefBut(block, BUT, B_VGRP_PNL_COPY_SINGLE + dvert->dw[i].def_nr, "C", 180,yco,20,20, 0, 0, 0, 0, 0, "Copy this groups weight to other selected verts");
723                                 yco -= 20;
724                         }
725                 }
726                 yco-=2;
727
728                 uiBlockEndAlign(block);
729                 uiBlockBeginAlign(block);
730                 uiDefBut(block, BUT, B_VGRP_PNL_NORMALIZE, "Normalize", 0, yco,100,20, 0, 0, 0, 0, 0, "Normalize active vertex weights");
731                 uiDefBut(block, BUT, B_VGRP_PNL_COPY, "Copy", 100,yco,100,20, 0, 0, 0, 0, 0, "Copy active vertex to other seleted verts");
732                 uiBlockEndAlign(block);
733         }
734 }
735
736 static void v3d_transform_butsR(uiLayout *layout, PointerRNA *ptr)
737 {
738         uiLayout *split, *colsub;
739         
740         split = uiLayoutSplit(layout, 0.8, 0);
741         
742         if (ptr->type == &RNA_PoseBone) {
743                 PointerRNA boneptr;
744                 Bone *bone;
745                 
746                 boneptr = RNA_pointer_get(ptr, "bone");
747                 bone = boneptr.data;
748                 uiLayoutSetActive(split, !(bone->parent && bone->flag & BONE_CONNECTED));
749         }
750         colsub = uiLayoutColumn(split, 1);
751         uiItemR(colsub, ptr, "location", 0, "Location", 0);
752         colsub = uiLayoutColumn(split, 1);
753         uiItemL(colsub, "", 0);
754         uiItemR(colsub, ptr, "lock_location", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
755         
756         split = uiLayoutSplit(layout, 0.8, 0);
757         
758         switch(RNA_enum_get(ptr, "rotation_mode")) {
759                 case ROT_MODE_QUAT: /* quaternion */
760                         colsub = uiLayoutColumn(split, 1);
761                         uiItemR(colsub, ptr, "rotation_quaternion", 0, "Rotation", 0);
762                         colsub = uiLayoutColumn(split, 1);
763                         uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, "4L", 0);
764                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
765                                 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
766                         else
767                                 uiItemL(colsub, "", 0);
768                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
769                         break;
770                 case ROT_MODE_AXISANGLE: /* axis angle */
771                         colsub = uiLayoutColumn(split, 1);
772                         uiItemR(colsub, ptr, "rotation_axis_angle", 0, "Rotation", 0);
773                         colsub = uiLayoutColumn(split, 1);
774                         uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, "4L", 0);
775                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
776                                 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
777                         else
778                                 uiItemL(colsub, "", 0);
779                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
780                         break;
781                 default: /* euler rotations */
782                         colsub = uiLayoutColumn(split, 1);
783                         uiItemR(colsub, ptr, "rotation_euler", 0, "Rotation", 0);
784                         colsub = uiLayoutColumn(split, 1);
785                         uiItemL(colsub, "", 0);
786                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
787                         break;
788         }
789         uiItemR(layout, ptr, "rotation_mode", 0, "", 0);
790         
791         split = uiLayoutSplit(layout, 0.8, 0);
792         colsub = uiLayoutColumn(split, 1);
793         uiItemR(colsub, ptr, "scale", 0, "Scale", 0);
794         colsub = uiLayoutColumn(split, 1);
795         uiItemL(colsub, "", 0);
796         uiItemR(colsub, ptr, "lock_scale", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", 0);
797         
798         if (ptr->type == &RNA_Object) {
799                 Object *ob = ptr->data;
800                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
801                         uiItemR(layout, ptr, "dimensions", 0, "Dimensions", 0);
802         }
803 }
804
805 static void v3d_posearmature_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
806 {
807 //      uiBlock *block= uiLayoutGetBlock(layout);
808 //      bArmature *arm;
809         bPoseChannel *pchan;
810 //      TransformProperties *tfp= v3d->properties_storage;
811         PointerRNA pchanptr;
812         uiLayout *col;
813 //      uiLayout *row;
814
815         pchan= get_active_posechannel(ob);
816
817 //      row= uiLayoutRow(layout, 0);
818         
819         if (!pchan)     {
820                 uiItemL(layout, "No Bone Active", 0);
821                 return; 
822         }
823
824         RNA_pointer_create(&ob->id, &RNA_PoseBone, pchan, &pchanptr);
825
826         col= uiLayoutColumn(layout, 0);
827         
828         /* XXX: RNA buts show data in native types (i.e. quats, 4-component axis/angle, etc.)
829          * but oldskool UI shows in eulers always. Do we want to be able to still display in Eulers?
830          * Maybe needs RNA/ui options to display rotations as different types... */
831         v3d_transform_butsR(col, &pchanptr);
832
833 #if 0
834         uiLayoutAbsoluteBlock(layout);
835
836         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
837                 float quat[4];
838                 /* convert to euler, passing through quats... */
839                 axis_angle_to_quat(quat, pchan->rotAxis, pchan->rotAngle);
840                 quat_to_eul( tfp->ob_eul,quat);
841         }
842         else if (pchan->rotmode == ROT_MODE_QUAT)
843                 quat_to_eul( tfp->ob_eul,pchan->quat);
844         else
845                 copy_v3_v3(tfp->ob_eul, pchan->eul);
846         tfp->ob_eul[0]*= 180.0/M_PI;
847         tfp->ob_eul[1]*= 180.0/M_PI;
848         tfp->ob_eul[2]*= 180.0/M_PI;
849         
850         uiDefBut(block, LABEL, 0, "Location:",                  0, 240, 100, 20, 0, 0, 0, 0, 0, "");
851         uiBlockBeginAlign(block);
852         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 220, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
853         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 200, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
854         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 180, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
855         uiBlockEndAlign(block);
856         
857         uiBlockBeginAlign(block);
858         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, B_REDR, ICON_UNLOCKED,   125, 220, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Location value from being Transformed");
859         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, B_REDR, ICON_UNLOCKED,   125, 200, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Location value from being Transformed");
860         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, B_REDR, ICON_UNLOCKED,   125, 180, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Z Location value from being Transformed");
861         uiBlockEndAlign(block);
862         
863         uiDefBut(block, LABEL, 0, "Rotation:",                  0, 160, 100, 20, 0, 0, 0, 0, 0, "");
864         uiBlockBeginAlign(block);
865         uiDefButF(block, NUM, B_ARMATUREPANEL3, "X:",   0, 140, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
866         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Y:",   0, 120, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
867         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Z:",   0, 100, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
868         uiBlockEndAlign(block);
869         
870         uiBlockBeginAlign(block);
871         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, B_REDR, ICON_UNLOCKED,   125, 140, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Rotation value from being Transformed");
872         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, B_REDR, ICON_UNLOCKED,   125, 120, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Rotation value from being Transformed");
873         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, B_REDR, ICON_UNLOCKED,   125, 100, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Z Rotation value from being Transformed");
874         uiBlockEndAlign(block);
875         
876         uiDefBut(block, LABEL, 0, "Scale:",                             0, 80, 100, 20, 0, 0, 0, 0, 0, "");
877         uiBlockBeginAlign(block);
878         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 60, 120, 19, pchan->size, -lim, lim, 10, 3, "");
879         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 40, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
880         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 20, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
881         uiBlockEndAlign(block);
882         
883         uiBlockBeginAlign(block);
884         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, B_REDR, ICON_UNLOCKED, 125, 60, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Scale value from being Transformed");
885         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, B_REDR, ICON_UNLOCKED, 125, 40, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Scale value from being Transformed");
886         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, B_REDR, ICON_UNLOCKED, 125, 20, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects z Scale value from being Transformed");
887         uiBlockEndAlign(block);
888 #endif
889 }
890
891 /* assumes armature editmode */
892 void validate_editbonebutton_cb(bContext *C, void *bonev, void *namev)
893 {
894         EditBone *eBone= bonev;
895         char oldname[32], newname[32];
896         
897         /* need to be on the stack */
898         BLI_strncpy(newname, eBone->name, 32);
899         BLI_strncpy(oldname, (char *)namev, 32);
900         /* restore */
901         BLI_strncpy(eBone->name, oldname, 32);
902         
903         ED_armature_bone_rename(CTX_data_edit_object(C)->data, oldname, newname); // editarmature.c
904         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, CTX_data_edit_object(C)); // XXX fix
905 }
906
907 static void v3d_editarmature_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
908 {
909 //      uiBlock *block= uiLayoutGetBlock(layout);
910         bArmature *arm= ob->data;
911         EditBone *ebone;
912 //      TransformProperties *tfp= v3d->properties_storage;
913 //      uiLayout *row;
914         uiLayout *col;
915         PointerRNA eboneptr;
916         
917         ebone= arm->act_edbone;
918
919         if (!ebone || (ebone->layer & arm->layer)==0)
920                 return;
921         
922 //      row= uiLayoutRow(layout, 0);
923         RNA_pointer_create(&arm->id, &RNA_EditBone, ebone, &eboneptr);
924
925
926         col= uiLayoutColumn(layout, 0);
927         uiItemR(col, &eboneptr, "head", 0, "Head", 0);
928         if (ebone->parent && ebone->flag & BONE_CONNECTED ) {
929                 PointerRNA parptr = RNA_pointer_get(&eboneptr, "parent");
930                 uiItemR(col, &parptr, "tail_radius", 0, "Radius", 0);
931         } else {
932                 uiItemR(col, &eboneptr, "head_radius", 0, "Radius", 0);
933         }
934         
935         uiItemR(col, &eboneptr, "tail", 0, "Tail", 0);
936         uiItemR(col, &eboneptr, "tail_radius", 0, "Radius", 0);
937         
938         uiItemR(col, &eboneptr, "roll", 0, "Roll", 0);
939 }
940
941 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob, float lim)
942 {
943         PointerRNA mbptr, ptr;
944         MetaBall *mball= ob->data;
945 //      uiLayout *row;
946         uiLayout *col;
947         
948         if (!mball || !(mball->lastelem)) return;
949         
950         RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
951         
952 //      row= uiLayoutRow(layout, 0);
953
954         RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
955                 
956         col= uiLayoutColumn(layout, 0);
957         uiItemR(col, &ptr, "location", 0, "Location", 0);
958                 
959         uiItemR(col, &ptr, "radius", 0, "Radius", 0);
960         uiItemR(col, &ptr, "stiffness", 0, "Stiffness", 0);
961         
962         uiItemR(col, &ptr, "type", 0, "Type", 0);
963         
964         col= uiLayoutColumn(layout, 1);
965         switch (RNA_enum_get(&ptr, "type")) {
966                 case MB_BALL:
967                         break;
968                 case MB_CUBE:
969                         uiItemL(col, "Size:", 0);
970                         uiItemR(col, &ptr, "size_x", 0, "X", 0);
971                         uiItemR(col, &ptr, "size_y", 0, "Y", 0);
972                         uiItemR(col, &ptr, "size_z", 0, "Z", 0);
973                         break;
974                 case MB_TUBE:
975                         uiItemL(col, "Size:", 0);
976                         uiItemR(col, &ptr, "size_x", 0, "X", 0);
977                         break;
978                 case MB_PLANE:
979                         uiItemL(col, "Size:", 0);
980                         uiItemR(col, &ptr, "size_x", 0, "X", 0);
981                         uiItemR(col, &ptr, "size_y", 0, "Y", 0);
982                         break;
983                 case MB_ELIPSOID:
984                         uiItemL(col, "Size:", 0);
985                         uiItemR(col, &ptr, "size_x", 0, "X", 0);
986                         uiItemR(col, &ptr, "size_y", 0, "Y", 0);
987                         uiItemR(col, &ptr, "size_z", 0, "Z", 0);
988                         break;             
989         }       
990 }
991
992 /* test if 'ob' is a parent somewhere in par's parents */
993 static int test_parent_loop(Object *par, Object *ob)
994 {
995         if(par == NULL) return 0;
996         if(ob == par) return 1;
997         return test_parent_loop(par->parent, ob);
998 }
999
1000 static void do_view3d_region_buttons(bContext *C, void *arg, int event)
1001 {
1002         Scene *scene= CTX_data_scene(C);
1003 //      Object *obedit= CTX_data_edit_object(C);
1004         View3D *v3d= CTX_wm_view3d(C);
1005 //      BoundBox *bb;
1006         Object *ob= OBACT;
1007         TransformProperties *tfp= v3d->properties_storage;
1008         
1009         switch(event) {
1010         
1011         case B_REDR:
1012                 ED_area_tag_redraw(CTX_wm_area(C));
1013                 return; /* no notifier! */
1014                 
1015         case B_OBJECTPANEL:
1016                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
1017                 break;
1018
1019                                 
1020         case B_OBJECTPANELMEDIAN:
1021                 if(ob) {
1022                         v3d_editvertex_buts(C, NULL, v3d, ob, 1.0);
1023                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1024                 }
1025                 break;
1026                 
1027                 /* note; this case also used for parbone */
1028         case B_OBJECTPANELPARENT:
1029                 if(ob) {
1030                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
1031                                 ob->parent= NULL;
1032                         else {
1033                                 DAG_scene_sort(scene);
1034                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
1035                         }
1036                 }
1037                 break;
1038                 
1039
1040         case B_ARMATUREPANEL3:  // rotate button on channel
1041                 {
1042                         bPoseChannel *pchan;
1043                         float eul[3];
1044                         
1045                         pchan= get_active_posechannel(ob);
1046                         if (!pchan) return;
1047                         
1048                         /* make a copy to eul[3], to allow TAB on buttons to work */
1049                         eul[0]= M_PI*tfp->ob_eul[0]/180.0;
1050                         eul[1]= M_PI*tfp->ob_eul[1]/180.0;
1051                         eul[2]= M_PI*tfp->ob_eul[2]/180.0;
1052                         
1053                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1054                                 float quat[4];
1055                                 /* convert to axis-angle, passing through quats  */
1056                                 eul_to_quat( quat,eul);
1057                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,quat);
1058                         }
1059                         else if (pchan->rotmode == ROT_MODE_QUAT)
1060                                 eul_to_quat( pchan->quat,eul);
1061                         else
1062                                 copy_v3_v3(pchan->eul, eul);
1063                 }
1064                 /* no break, pass on */
1065         case B_ARMATUREPANEL2:
1066                 {
1067                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1068                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1069                 }
1070                 break;
1071         case B_TRANSFORMSPACEADD:
1072                 BIF_createTransformOrientation(C, NULL, "", 1, 0);
1073                 break;
1074         case B_TRANSFORMSPACECLEAR:
1075                 BIF_clearTransformOrientation(C);
1076                 break;
1077                 
1078 #if 0 // XXX
1079         case B_WEIGHT0_0:
1080                 wpaint->weight = 0.0f;
1081                 break;
1082                 
1083         case B_WEIGHT1_4:
1084                 wpaint->weight = 0.25f;
1085                 break;
1086         case B_WEIGHT1_2:
1087                 wpaint->weight = 0.5f;
1088                 break;
1089         case B_WEIGHT3_4:
1090                 wpaint->weight = 0.75f;
1091                 break;
1092         case B_WEIGHT1_0:
1093                 wpaint->weight = 1.0f;
1094                 break;
1095                 
1096         case B_OPA1_8:
1097                 wpaint->a = 0.125f;
1098                 break;
1099         case B_OPA1_4:
1100                 wpaint->a = 0.25f;
1101                 break;
1102         case B_OPA1_2:
1103                 wpaint->a = 0.5f;
1104                 break;
1105         case B_OPA3_4:
1106                 wpaint->a = 0.75f;
1107                 break;
1108         case B_OPA1_0:
1109                 wpaint->a = 1.0f;
1110                 break;
1111 #endif
1112         case B_CLR_WPAINT:
1113 //              if(!multires_level1_test()) {
1114                 {
1115                         bDeformGroup *defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
1116                         if(defGroup) {
1117                                 Mesh *me= ob->data;
1118                                 int a;
1119                                 for(a=0; a<me->totvert; a++)
1120                                         ED_vgroup_vert_remove (ob, defGroup, a);
1121                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1122                         }
1123                 }
1124                 break;
1125         case B_RV3D_LOCKED:
1126         case B_RV3D_BOXVIEW:
1127         case B_RV3D_BOXCLIP:
1128                 {
1129                         ScrArea *sa= CTX_wm_area(C);
1130                         ARegion *ar= sa->regionbase.last;
1131                         RegionView3D *rv3d;
1132                         short viewlock;
1133                         
1134                         ar= ar->prev;
1135                         rv3d= ar->regiondata;
1136                         viewlock= rv3d->viewlock;
1137                         
1138                         if((viewlock & RV3D_LOCKED)==0)
1139                                 viewlock= 0;
1140                         else if((viewlock & RV3D_BOXVIEW)==0)
1141                                 viewlock &= ~RV3D_BOXCLIP;
1142                         
1143                         for(; ar; ar= ar->prev) {
1144                                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
1145                                         rv3d= ar->regiondata;
1146                                         rv3d->viewlock= viewlock;
1147                                 }
1148                         }
1149                         
1150                         if(rv3d->viewlock & RV3D_BOXVIEW)
1151                                 view3d_boxview_copy(sa, sa->regionbase.last);
1152                         
1153                         ED_area_tag_redraw(sa);
1154                 }
1155                 break;
1156         }
1157
1158         /* default for now */
1159         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, ob);
1160 }
1161
1162 void removeTransformOrientation_func(bContext *C, void *target, void *unused)
1163 {
1164         BIF_removeTransformOrientation(C, (TransformOrientation *) target);
1165 }
1166
1167 void selectTransformOrientation_func(bContext *C, void *target, void *unused)
1168 {
1169         BIF_selectTransformOrientation(C, (TransformOrientation *) target);
1170 }
1171
1172 static void view3d_panel_object(const bContext *C, Panel *pa)
1173 {
1174         uiBlock *block;
1175         Scene *scene= CTX_data_scene(C);
1176         Object *obedit= CTX_data_edit_object(C);
1177         View3D *v3d= CTX_wm_view3d(C);
1178         //uiBut *bt;
1179         Object *ob= OBACT;
1180         TransformProperties *tfp;
1181         PointerRNA obptr;
1182         uiLayout *col, *row;
1183         float lim;
1184         
1185         if(ob==NULL) return;
1186
1187         /* make sure we got storage */
1188         if(v3d->properties_storage==NULL)
1189                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
1190         tfp= v3d->properties_storage;
1191         
1192 // XXX  uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
1193         /*
1194         if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
1195         }
1196         else {
1197                 if((ob->mode & OB_MODE_PARTICLE_EDIT)==0) {
1198                         uiBlockEndAlign(block);
1199                 }
1200         }
1201         */
1202
1203         lim= 10000.0f*MAX2(1.0, v3d->grid);
1204
1205         block= uiLayoutGetBlock(pa->layout);
1206         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1207
1208         col= uiLayoutColumn(pa->layout, 0);
1209         row= uiLayoutRow(col, 0);
1210         RNA_id_pointer_create(&ob->id, &obptr);
1211
1212         if(ob==obedit) {
1213                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(col, v3d, ob, lim);
1214                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(col, ob, lim);
1215                 else v3d_editvertex_buts(C, col, v3d, ob, lim);
1216         }
1217         else if(ob->mode & OB_MODE_POSE) {
1218                 v3d_posearmature_buts(col, v3d, ob, lim);
1219         }
1220         else {
1221
1222                 v3d_transform_butsR(col, &obptr);
1223                 }
1224 }
1225
1226 #if 0
1227 static void view3d_panel_preview(bContext *C, ARegion *ar, short cntrl) // VIEW3D_HANDLER_PREVIEW
1228 {
1229         uiBlock *block;
1230         View3D *v3d= sa->spacedata.first;
1231         int ofsx, ofsy;
1232         
1233         block= uiBeginBlock(C, ar, "view3d_panel_preview", UI_EMBOSS);
1234         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1235         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
1236         
1237         ofsx= -150+(sa->winx/2)/v3d->blockscale;
1238         ofsy= -100+(sa->winy/2)/v3d->blockscale;
1239         if(uiNewPanel(C, ar, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
1240
1241         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
1242         
1243         if(scene->recalc & SCE_PRV_CHANGED) {
1244                 scene->recalc &= ~SCE_PRV_CHANGED;
1245                 //printf("found recalc\n");
1246                 BIF_view3d_previewrender_free(sa->spacedata.first);
1247                 BIF_preview_changed(0);
1248         }
1249 }
1250 #endif
1251
1252 #if 0 // XXX not used
1253 static void delete_sketch_armature(bContext *C, void *arg1, void *arg2)
1254 {
1255         BIF_deleteSketch(C);
1256 }
1257
1258 static void convert_sketch_armature(bContext *C, void *arg1, void *arg2)
1259 {
1260         BIF_convertSketch(C);
1261 }
1262
1263 static void assign_template_sketch_armature(bContext *C, void *arg1, void *arg2)
1264 {
1265         int index = *(int*)arg1;
1266         BIF_setTemplate(C, index);
1267 }
1268
1269
1270 static int view3d_panel_bonesketch_spaces_poll(const bContext *C, PanelType *pt)
1271 {
1272         Object *obedit = CTX_data_edit_object(C);
1273
1274         /* replace with check call to sketching lib */
1275         return (obedit && obedit->type == OB_ARMATURE);
1276 }
1277 static void view3d_panel_bonesketch_spaces(const bContext *C, Panel *pa)
1278 {
1279         Scene *scene = CTX_data_scene(C);
1280         static int template_index;
1281         static char joint_label[128];
1282         uiBlock *block;
1283         uiBut *but;
1284         char *bone_name;
1285         int yco = 130;
1286         int nb_joints;
1287         static char subdiv_tooltip[4][64] = {
1288                 "Subdivide arcs based on a fixed number of bones",
1289                 "Subdivide arcs in bones of equal length",
1290                 "Subdivide arcs based on correlation",
1291                 "Retarget template to stroke"
1292                 };
1293
1294         
1295         block= uiLayoutAbsoluteBlock(pa->layout);
1296         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1297
1298         uiBlockBeginAlign(block);
1299         
1300         /* use real flag instead of 1 */
1301         uiDefButBitC(block, TOG, BONE_SKETCHING, B_REDR, "Use Bone Sketching", 10, yco, 160, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Use sketching to create and edit bones, (Ctrl snaps to mesh volume)");
1302         uiDefButBitC(block, TOG, BONE_SKETCHING_ADJUST, B_REDR, "A", 170, yco, 20, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Adjust strokes by drawing near them");
1303         uiDefButBitC(block, TOG, BONE_SKETCHING_QUICK, B_REDR, "Q", 190, yco, 20, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Automatically convert and delete on stroke end");
1304         yco -= 20;
1305         
1306         but = uiDefBut(block, BUT, B_REDR, "Convert", 10,yco,100,20, 0, 0, 0, 0, 0, "Convert sketch to armature");
1307         uiButSetFunc(but, convert_sketch_armature, NULL, NULL);
1308
1309         but = uiDefBut(block, BUT, B_REDR, "Delete", 110,yco,100,20, 0, 0, 0, 0, 0, "Delete sketch");
1310         uiButSetFunc(but, delete_sketch_armature, NULL, NULL);
1311         yco -= 20;
1312         
1313         uiBlockEndAlign(block);
1314
1315         uiBlockBeginAlign(block);
1316         
1317         uiDefButC(block, MENU, B_REDR, "Subdivision Method%t|Length%x1|Adaptative%x2|Fixed%x0|Template%x3", 10,yco,60,19, &scene->toolsettings->bone_sketching_convert, 0, 0, 0, 0, subdiv_tooltip[(unsigned char)scene->toolsettings->bone_sketching_convert]);
1318
1319         switch(scene->toolsettings->bone_sketching_convert)
1320         {
1321         case SK_CONVERT_CUT_LENGTH:
1322                 uiDefButF(block, NUM, B_REDR,                                   "Lim:",         70, yco, 140, 19, &scene->toolsettings->skgen_length_limit,0.1,50.0, 10, 0,             "Maximum length of the subdivided bones");
1323                 yco -= 20;
1324                 break;
1325         case SK_CONVERT_CUT_ADAPTATIVE:
1326                 uiDefButF(block, NUM, B_REDR,                                   "Thres:",                       70, yco, 140, 19, &scene->toolsettings->skgen_correlation_limit,0.0, 1.0, 0.01, 0,      "Correlation threshold for subdivision");
1327                 yco -= 20;
1328                 break;
1329         default:
1330         case SK_CONVERT_CUT_FIXED:
1331                 uiDefButC(block, NUM, B_REDR,                                   "Num:",         70, yco, 140, 19, &scene->toolsettings->skgen_subdivision_number,1, 100, 1, 5,  "Number of subdivided bones");
1332                 yco -= 20;
1333                 break;
1334         case SK_CONVERT_RETARGET:
1335                 uiDefButC(block, ROW, B_NOP, "No",                      70,  yco, 40,19, &scene->toolsettings->skgen_retarget_roll, 0, 0, 0, 0,                                                                 "No special roll treatment");
1336                 uiDefButC(block, ROW, B_NOP, "View",            110,  yco, 50,19, &scene->toolsettings->skgen_retarget_roll, 0, SK_RETARGET_ROLL_VIEW, 0, 0,                            "Roll bones perpendicular to view");
1337                 uiDefButC(block, ROW, B_NOP, "Joint",           160, yco, 50,19, &scene->toolsettings->skgen_retarget_roll, 0, SK_RETARGET_ROLL_JOINT, 0, 0,                            "Roll bones relative to joint bend");
1338                 yco -= 30;
1339
1340                 uiBlockEndAlign(block);
1341
1342                 uiBlockBeginAlign(block);
1343                 /* button here to select what to do (copy or not), template, ...*/
1344
1345                 BIF_makeListTemplates(C);
1346                 template_index = BIF_currentTemplate(C);
1347                 
1348                 but = uiDefButI(block, MENU, B_REDR, BIF_listTemplates(C), 10,yco,200,19, &template_index, 0, 0, 0, 0, "Template");
1349                 uiButSetFunc(but, assign_template_sketch_armature, &template_index, NULL);
1350                 
1351                 yco -= 20;
1352                 
1353                 uiDefButF(block, NUM, B_NOP,                                                    "A:",                   10, yco, 66,19, &scene->toolsettings->skgen_retarget_angle_weight, 0, 10, 1, 0,         "Angle Weight");
1354                 uiDefButF(block, NUM, B_NOP,                                                    "L:",                   76, yco, 67,19, &scene->toolsettings->skgen_retarget_length_weight, 0, 10, 1, 0,                "Length Weight");
1355                 uiDefButF(block, NUM, B_NOP,                                                    "D:",           143,yco, 67,19, &scene->toolsettings->skgen_retarget_distance_weight, 0, 10, 1, 0,              "Distance Weight");
1356                 yco -= 20;
1357                 
1358                 uiDefBut(block, TEX,B_REDR,"S:",                                                        10,  yco, 90, 20, scene->toolsettings->skgen_side_string, 0.0, 8.0, 0, 0, "Text to replace &S with");
1359                 uiDefBut(block, TEX,B_REDR,"N:",                                                        100, yco, 90, 20, scene->toolsettings->skgen_num_string, 0.0, 8.0, 0, 0, "Text to replace &N with");
1360                 uiDefIconButBitC(block, TOG, SK_RETARGET_AUTONAME, B_NOP, ICON_AUTO,190,yco,20,20, &scene->toolsettings->skgen_retarget_options, 0, 0, 0, 0, "Use Auto Naming");        
1361                 yco -= 20;
1362
1363                 /* auto renaming magic */
1364                 uiBlockEndAlign(block);
1365                 
1366                 nb_joints = BIF_nbJointsTemplate(C);
1367
1368                 if (nb_joints == -1)
1369                 {
1370                         //XXX
1371                         //nb_joints = G.totvertsel;
1372                 }
1373                 
1374                 bone_name = BIF_nameBoneTemplate(C);
1375                 
1376                 BLI_snprintf(joint_label, 32, "%i joints: %s", nb_joints, bone_name);
1377                 
1378                 uiDefBut(block, LABEL, 1, joint_label,                                  10, yco, 200, 20, NULL, 0.0, 0.0, 0, 0, "");
1379                 yco -= 20;
1380                 break;
1381         }
1382
1383         uiBlockEndAlign(block);
1384 }
1385
1386 /* op->invoke */
1387 static void redo_cb(bContext *C, void *arg_op, void *arg2)
1388 {
1389         wmOperator *lastop= arg_op;
1390         
1391         if(lastop) {
1392                 int retval;
1393                 
1394                 if (G.f & G_DEBUG)
1395                         printf("operator redo %s\n", lastop->type->name);
1396                 ED_undo_pop(C);
1397                 retval= WM_operator_repeat(C, lastop);
1398                 if((retval & OPERATOR_FINISHED)==0) {
1399                         if (G.f & G_DEBUG)
1400                                 printf("operator redo failed %s\n", lastop->type->name);
1401                         ED_undo_redo(C);
1402                 }
1403         }
1404 }
1405
1406 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
1407 {
1408         wmWindowManager *wm= CTX_wm_manager(C);
1409         wmOperator *op;
1410         PointerRNA ptr;
1411         uiBlock *block;
1412         
1413         block= uiLayoutGetBlock(pa->layout);
1414
1415         /* only for operators that are registered and did an undo push */
1416         for(op= wm->operators.last; op; op= op->prev)
1417                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
1418                         break;
1419         
1420         if(op==NULL)
1421                 return;
1422         
1423         uiBlockSetFunc(block, redo_cb, op, NULL);
1424         
1425         if(!op->properties) {
1426                 IDPropertyTemplate val = {0};
1427                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
1428         }
1429         
1430         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1431         uiDefAutoButsRNA(C, pa->layout, &ptr, 2);
1432 }
1433 #endif // XXX not used
1434
1435 void view3d_buttons_register(ARegionType *art)
1436 {
1437         PanelType *pt;
1438
1439         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1440         strcpy(pt->idname, "VIEW3D_PT_object");
1441         strcpy(pt->label, "Transform");
1442         pt->draw= view3d_panel_object;
1443         BLI_addtail(&art->paneltypes, pt);
1444         
1445         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel gpencil");
1446         strcpy(pt->idname, "VIEW3D_PT_gpencil");
1447         strcpy(pt->label, "Grease Pencil");
1448         pt->draw= gpencil_panel_standard;
1449         BLI_addtail(&art->paneltypes, pt);
1450
1451         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel vgroup");
1452         strcpy(pt->idname, "VIEW3D_PT_vgroup");
1453         strcpy(pt->label, "Vertex Groups");
1454         pt->draw= view3d_panel_vgroup;
1455         pt->poll= view3d_panel_vgroup_poll;
1456         BLI_addtail(&art->paneltypes, pt);
1457
1458         // XXX view3d_panel_preview(C, ar, 0);
1459 }
1460
1461 static int view3d_properties(bContext *C, wmOperator *op)
1462 {
1463         ScrArea *sa= CTX_wm_area(C);
1464         ARegion *ar= view3d_has_buttons_region(sa);
1465         
1466         if(ar)
1467                 ED_region_toggle_hidden(C, ar);
1468
1469         return OPERATOR_FINISHED;
1470 }
1471
1472 void VIEW3D_OT_properties(wmOperatorType *ot)
1473 {
1474         ot->name= "Properties";
1475         ot->description= "Toggles the properties panel display";
1476         ot->idname= "VIEW3D_OT_properties";
1477         
1478         ot->exec= view3d_properties;
1479         ot->poll= ED_operator_view3d_active;
1480         
1481         /* flags */
1482         ot->flag= 0;
1483 }