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