Removed my earlier vertex paint vertex select--someone is changing it drastically...
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/sculpt_paint/paint_vertex.c
31  *  \ingroup edsculpt
32  */
33
34
35 #include <math.h>
36 #include <string.h>
37
38 #ifdef WIN32
39 #include <io.h>
40 #else
41 #include <unistd.h>
42 #endif   
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_math.h"
48 #include "BLI_utildefines.h"
49 #include "BLI_ghash.h"
50
51 #include "IMB_imbuf.h"
52 #include "IMB_imbuf_types.h"
53
54 #include "DNA_armature_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_particle_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_brush_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_meshdata_types.h"
61
62 #include "RNA_access.h"
63 #include "RNA_define.h"
64 #include "RNA_enum_types.h"
65
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_action.h"
68 #include "BKE_brush.h"
69 #include "BKE_context.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_deform.h"
72 #include "BKE_mesh.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_paint.h"
76 #include "BKE_report.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81
82 #include "ED_armature.h"
83 #include "ED_mesh.h"
84 #include "ED_screen.h"
85 #include "ED_view3d.h"
86
87 #include "paint_intern.h"
88
89 /* brush->vertexpaint_tool */
90 #define VP_MIX  0
91 #define VP_ADD  1
92 #define VP_SUB  2
93 #define VP_MUL  3
94 #define VP_BLUR 4
95 #define VP_LIGHTEN      5
96 #define VP_DARKEN       6
97
98 /* polling - retrieve whether cursor should be set or operator should be done */
99
100
101 /* Returns true if vertex paint mode is active */
102 int vertex_paint_mode_poll(bContext *C)
103 {
104         Object *ob = CTX_data_active_object(C);
105
106         return ob && ob->mode == OB_MODE_VERTEX_PAINT && ((Mesh *)ob->data)->totface;
107 }
108
109 int vertex_paint_poll(bContext *C)
110 {
111         if(vertex_paint_mode_poll(C) && 
112            paint_brush(&CTX_data_tool_settings(C)->vpaint->paint)) {
113                 ScrArea *sa= CTX_wm_area(C);
114                 if(sa->spacetype==SPACE_VIEW3D) {
115                         ARegion *ar= CTX_wm_region(C);
116                         if(ar->regiontype==RGN_TYPE_WINDOW)
117                                 return 1;
118                         }
119                 }
120         return 0;
121 }
122
123 int weight_paint_mode_poll(bContext *C)
124 {
125         Object *ob = CTX_data_active_object(C);
126
127         return ob && ob->mode == OB_MODE_WEIGHT_PAINT && ((Mesh *)ob->data)->totface;
128 }
129
130 int weight_paint_poll(bContext *C)
131 {
132         Object *ob= CTX_data_active_object(C);
133         ScrArea *sa;
134
135         if(     (ob != NULL) &&
136                 (ob->mode & OB_MODE_WEIGHT_PAINT) &&
137                 (paint_brush(&CTX_data_tool_settings(C)->wpaint->paint) != NULL) &&
138                 (sa= CTX_wm_area(C)) &&
139                 (sa->spacetype == SPACE_VIEW3D)
140         ) {
141                 ARegion *ar= CTX_wm_region(C);
142                 if(ar->regiontype==RGN_TYPE_WINDOW) {
143                         return 1;
144                 }
145         }
146         return 0;
147 }
148
149 static VPaint *new_vpaint(int wpaint)
150 {
151         VPaint *vp= MEM_callocN(sizeof(VPaint), "VPaint");
152         
153         vp->flag= VP_AREA+VP_SPRAY;
154         
155         if(wpaint)
156                 vp->flag= VP_AREA;
157
158         return vp;
159 }
160
161 static int *get_indexarray(Mesh *me)
162 {
163         return MEM_mallocN(sizeof(int)*(me->totface+1), "vertexpaint");
164 }
165
166
167 /* in contradiction to cpack drawing colors, the MCOL colors (vpaint colors) are per byte! 
168    so not endian sensitive. Mcol = ABGR!!! so be cautious with cpack calls */
169
170 static unsigned int rgba_to_mcol(float r, float g, float b, float a)
171 {
172         int ir, ig, ib, ia;
173         unsigned int col;
174         char *cp;
175         
176         ir= floor(255.0f * r);
177         if(ir<0) ir= 0; else if(ir>255) ir= 255;
178         ig= floor(255.0f * g);
179         if(ig<0) ig= 0; else if(ig>255) ig= 255;
180         ib= floor(255.0f * b);
181         if(ib<0) ib= 0; else if(ib>255) ib= 255;
182         ia= floor(255.0f * a);
183         if(ia<0) ia= 0; else if(ia>255) ia= 255;
184         
185         cp= (char *)&col;
186         cp[0]= ia;
187         cp[1]= ib;
188         cp[2]= ig;
189         cp[3]= ir;
190         
191         return col;
192         
193 }
194
195 unsigned int vpaint_get_current_col(VPaint *vp)
196 {
197         Brush *brush = paint_brush(&vp->paint);
198         return rgba_to_mcol(brush->rgb[0], brush->rgb[1], brush->rgb[2], 1.0f);
199 }
200
201 static void do_shared_vertexcol(Mesh *me)
202 {
203         /* if no mcol: do not do */
204         /* if tface: only the involved faces, otherwise all */
205         MFace *mface;
206         MTFace *tface;
207         int a;
208         short *scolmain, *scol;
209         char *mcol;
210         
211         if(me->mcol==NULL || me->totvert==0 || me->totface==0) return;
212         
213         scolmain= MEM_callocN(4*sizeof(short)*me->totvert, "colmain");
214         
215         tface= me->mtface;
216         mface= me->mface;
217         mcol= (char *)me->mcol;
218         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
219                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
220                         scol= scolmain+4*mface->v1;
221                         scol[0]++; scol[1]+= mcol[1]; scol[2]+= mcol[2]; scol[3]+= mcol[3];
222                         scol= scolmain+4*mface->v2;
223                         scol[0]++; scol[1]+= mcol[5]; scol[2]+= mcol[6]; scol[3]+= mcol[7];
224                         scol= scolmain+4*mface->v3;
225                         scol[0]++; scol[1]+= mcol[9]; scol[2]+= mcol[10]; scol[3]+= mcol[11];
226                         if(mface->v4) {
227                                 scol= scolmain+4*mface->v4;
228                                 scol[0]++; scol[1]+= mcol[13]; scol[2]+= mcol[14]; scol[3]+= mcol[15];
229                         }
230                 }
231                 if(tface) tface++;
232         }
233         
234         a= me->totvert;
235         scol= scolmain;
236         while(a--) {
237                 if(scol[0]>1) {
238                         scol[1]/= scol[0];
239                         scol[2]/= scol[0];
240                         scol[3]/= scol[0];
241                 }
242                 scol+= 4;
243         }
244         
245         tface= me->mtface;
246         mface= me->mface;
247         mcol= (char *)me->mcol;
248         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
249                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
250                         scol= scolmain+4*mface->v1;
251                         mcol[1]= scol[1]; mcol[2]= scol[2]; mcol[3]= scol[3];
252                         scol= scolmain+4*mface->v2;
253                         mcol[5]= scol[1]; mcol[6]= scol[2]; mcol[7]= scol[3];
254                         scol= scolmain+4*mface->v3;
255                         mcol[9]= scol[1]; mcol[10]= scol[2]; mcol[11]= scol[3];
256                         if(mface->v4) {
257                                 scol= scolmain+4*mface->v4;
258                                 mcol[13]= scol[1]; mcol[14]= scol[2]; mcol[15]= scol[3];
259                         }
260                 }
261                 if(tface) tface++;
262         }
263
264         MEM_freeN(scolmain);
265 }
266
267 static void make_vertexcol(Object *ob)  /* single ob */
268 {
269         Mesh *me;
270         if(!ob || ob->id.lib) return;
271         me= get_mesh(ob);
272         if(me==NULL) return;
273         if(me->edit_mesh) return;
274
275         /* copies from shadedisplist to mcol */
276         if(!me->mcol) {
277                 CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
278                 mesh_update_customdata_pointers(me);
279         }
280
281         //if(shade)
282         //      shadeMeshMCol(scene, ob, me);
283         //else
284
285         memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
286         
287         DAG_id_tag_update(&me->id, 0);
288         
289 }
290
291 /* mirror_vgroup is set to -1 when invalid */
292 static void wpaint_mirror_vgroup_ensure(Object *ob, int *vgroup_mirror)
293 {
294         bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef - 1);
295
296         if(defgroup) {
297                 bDeformGroup *curdef;
298                 int mirrdef;
299                 char name[MAXBONENAME];
300
301                 flip_side_name(name, defgroup->name, FALSE);
302
303                 if(strcmp(name, defgroup->name) != 0) {
304                         for (curdef= ob->defbase.first, mirrdef= 0; curdef; curdef=curdef->next, mirrdef++) {
305                                 if (!strcmp(curdef->name, name)) {
306                                         break;
307                                 }
308                         }
309
310                         if(curdef==NULL) {
311                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
312                                 curdef= ED_vgroup_add_name(ob, name);
313                                 ob->actdef= olddef;
314                         }
315
316                         /* curdef should never be NULL unless this is
317                          * a  lamp and ED_vgroup_add_name fails */
318                         if(curdef) {
319                                 *vgroup_mirror= mirrdef;
320                                 return;
321                         }
322                 }
323         }
324
325         *vgroup_mirror= -1;
326 }
327
328 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
329 {
330         if(vp->vpaint_prev) {
331                 MEM_freeN(vp->vpaint_prev);
332                 vp->vpaint_prev= NULL;
333         }
334         vp->tot= tot;   
335         
336         if(mcol==NULL || tot==0) return;
337         
338         vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
339         memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
340         
341 }
342
343 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
344 {
345         if (wp->wpaint_prev) {
346                 free_dverts(wp->wpaint_prev, wp->tot);
347                 wp->wpaint_prev= NULL;
348         }
349         
350         if(dverts && dcount) {
351                 
352                 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
353                 wp->tot = dcount;
354                 copy_dverts (wp->wpaint_prev, dverts, dcount);
355         }
356 }
357
358
359 void vpaint_fill(Object *ob, unsigned int paintcol)
360 {
361         Mesh *me;
362         MFace *mf;
363         unsigned int *mcol;
364         int i, selected;
365         
366         me= get_mesh(ob);
367         if(me==NULL || me->totface==0) return;
368
369         if(!me->mcol)
370                 make_vertexcol(ob);
371
372         selected= (me->editflag & ME_EDIT_PAINT_MASK);
373
374         mf = me->mface;
375         mcol = (unsigned int*)me->mcol;
376         for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
377                 if (!selected || mf->flag & ME_FACE_SEL) {
378                         mcol[0] = paintcol;
379                         mcol[1] = paintcol;
380                         mcol[2] = paintcol;
381                         mcol[3] = paintcol;
382                 }
383         }
384         
385         DAG_id_tag_update(&me->id, 0);
386 }
387
388
389 /* fills in the selected faces with the current weight and vertex group */
390 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
391 {
392         Mesh *me;
393         MFace *mface;
394         MDeformWeight *dw, *uw;
395         int *indexar;
396         int index, vgroup;
397         unsigned int faceverts[5]={0,0,0,0,0};
398         unsigned char i;
399         int vgroup_mirror= -1;
400         int selected;
401         // Jason
402         int selectedVerts;
403
404         me= ob->data;
405         if(me==NULL || me->totface==0 || me->dvert==NULL || !me->mface) return;
406         
407         selected= (me->editflag & ME_EDIT_PAINT_MASK);
408         // Jason
409         selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
410
411         indexar= get_indexarray(me);
412
413         if(selected) {
414                 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
415                         if((mface->flag & ME_FACE_SEL)==0)
416                                 indexar[index]= 0;
417                         else
418                                 indexar[index]= index+1;
419                 }
420         }
421         else {
422                 for(index=0; index<me->totface; index++)
423                         indexar[index]= index+1;
424         }
425         
426         vgroup= ob->actdef-1;
427
428         /* if mirror painting, find the other group */          
429         if(me->editflag & ME_EDIT_MIRROR_X) {
430                 wpaint_mirror_vgroup_ensure(ob, &vgroup_mirror);
431         }
432         
433         copy_wpaint_prev(wp, me->dvert, me->totvert);
434         
435         for(index=0; index<me->totface; index++) {
436                 if(indexar[index] && indexar[index]<=me->totface) {
437                         mface= me->mface + (indexar[index]-1);
438                         /* just so we can loop through the verts */
439                         faceverts[0]= mface->v1;
440                         faceverts[1]= mface->v2;
441                         faceverts[2]= mface->v3;
442                         faceverts[3]= mface->v4;
443                         for (i=0; i<3 || faceverts[i]; i++) {
444                                 if(!((me->dvert+faceverts[i])->flag)) {
445                                         // Jason
446                                         if(selectedVerts && !((me->mvert+faceverts[i])->flag & SELECT)) {
447                                                 continue;
448                                         }
449
450                                         dw= defvert_verify_index(me->dvert+faceverts[i], vgroup);
451                                         if(dw) {
452                                                 uw= defvert_verify_index(wp->wpaint_prev+faceverts[i], vgroup);
453                                                 uw->weight= dw->weight; /* set the undo weight */
454                                                 dw->weight= paintweight;
455                                                 
456                                                 if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
457                                                         int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
458                                                         if(j>=0) {
459                                                                 /* copy, not paint again */
460                                                                 if(vgroup_mirror != -1) {
461                                                                         dw= defvert_verify_index(me->dvert+j, vgroup_mirror);
462                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup_mirror);
463                                                                 } else {
464                                                                         dw= defvert_verify_index(me->dvert+j, vgroup);
465                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup);
466                                                                 }
467                                                                 uw->weight= dw->weight; /* set the undo weight */
468                                                                 dw->weight= paintweight;
469                                                         }
470                                                 }
471                                         }
472                                         (me->dvert+faceverts[i])->flag= 1;
473                                 }
474                         }
475                 }
476         }
477         
478         index=0;
479         while (index<me->totvert) {
480                 (me->dvert+index)->flag= 0;
481                 index++;
482         }
483         
484         MEM_freeN(indexar);
485         copy_wpaint_prev(wp, NULL, 0);
486
487         DAG_id_tag_update(&me->id, 0);
488 }
489
490 /* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator
491  
492 void vpaint_dogamma(Scene *scene)
493 {
494         VPaint *vp= scene->toolsettings->vpaint;
495         Mesh *me;
496         Object *ob;
497         float igam, fac;
498         int a, temp;
499         unsigned char *cp, gamtab[256];
500
501         ob= OBACT;
502         me= get_mesh(ob);
503
504         if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
505         if(me==0 || me->mcol==0 || me->totface==0) return;
506
507         igam= 1.0/vp->gamma;
508         for(a=0; a<256; a++) {
509                 
510                 fac= ((float)a)/255.0;
511                 fac= vp->mul*pow( fac, igam);
512                 
513                 temp= 255.9*fac;
514                 
515                 if(temp<=0) gamtab[a]= 0;
516                 else if(temp>=255) gamtab[a]= 255;
517                 else gamtab[a]= temp;
518         }
519
520         a= 4*me->totface;
521         cp= (unsigned char *)me->mcol;
522         while(a--) {
523                 
524                 cp[1]= gamtab[ cp[1] ];
525                 cp[2]= gamtab[ cp[2] ];
526                 cp[3]= gamtab[ cp[3] ];
527                 
528                 cp+= 4;
529         }
530 }
531  */
532
533 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
534 {
535         char *cp1, *cp2, *cp;
536         int mfac;
537         unsigned int col=0;
538         
539         if(fac==0) return col1;
540         if(fac>=255) return col2;
541
542         mfac= 255-fac;
543         
544         cp1= (char *)&col1;
545         cp2= (char *)&col2;
546         cp=  (char *)&col;
547         
548         cp[0]= 255;
549         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
550         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
551         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
552         
553         return col;
554 }
555
556 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
557 {
558         char *cp1, *cp2, *cp;
559         int temp;
560         unsigned int col=0;
561         
562         if(fac==0) return col1;
563         
564         cp1= (char *)&col1;
565         cp2= (char *)&col2;
566         cp=  (char *)&col;
567         
568         cp[0]= 255;
569         temp= cp1[1] + ((fac*cp2[1])/255);
570         if(temp>254) cp[1]= 255; else cp[1]= temp;
571         temp= cp1[2] + ((fac*cp2[2])/255);
572         if(temp>254) cp[2]= 255; else cp[2]= temp;
573         temp= cp1[3] + ((fac*cp2[3])/255);
574         if(temp>254) cp[3]= 255; else cp[3]= temp;
575         
576         return col;
577 }
578
579 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
580 {
581         char *cp1, *cp2, *cp;
582         int temp;
583         unsigned int col=0;
584         
585         if(fac==0) return col1;
586         
587         cp1= (char *)&col1;
588         cp2= (char *)&col2;
589         cp=  (char *)&col;
590         
591         cp[0]= 255;
592         temp= cp1[1] - ((fac*cp2[1])/255);
593         if(temp<0) cp[1]= 0; else cp[1]= temp;
594         temp= cp1[2] - ((fac*cp2[2])/255);
595         if(temp<0) cp[2]= 0; else cp[2]= temp;
596         temp= cp1[3] - ((fac*cp2[3])/255);
597         if(temp<0) cp[3]= 0; else cp[3]= temp;
598         
599         return col;
600 }
601
602 static unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
603 {
604         char *cp1, *cp2, *cp;
605         int mfac;
606         unsigned int col=0;
607         
608         if(fac==0) return col1;
609
610         mfac= 255-fac;
611         
612         cp1= (char *)&col1;
613         cp2= (char *)&col2;
614         cp=  (char *)&col;
615         
616         /* first mul, then blend the fac */
617         cp[0]= 255;
618         cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255)  )/255;
619         cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255)  )/255;
620         cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255)  )/255;
621
622         
623         return col;
624 }
625
626 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
627 {
628         char *cp1, *cp2, *cp;
629         int mfac;
630         unsigned int col=0;
631         
632         if(fac==0) return col1;
633         if(fac>=255) return col2;
634
635         mfac= 255-fac;
636         
637         cp1= (char *)&col1;
638         cp2= (char *)&col2;
639         cp=  (char *)&col;
640         
641         /* See if are lighter, if so mix, else dont do anything.
642         if the paint col is darker then the original, then ignore */
643         if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
644                 return col1;
645         
646         cp[0]= 255;
647         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
648         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
649         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
650         
651         return col;
652 }
653
654 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
655 {
656         char *cp1, *cp2, *cp;
657         int mfac;
658         unsigned int col=0;
659         
660         if(fac==0) return col1;
661         if(fac>=255) return col2;
662
663         mfac= 255-fac;
664         
665         cp1= (char *)&col1;
666         cp2= (char *)&col2;
667         cp=  (char *)&col;
668         
669         /* See if were darker, if so mix, else dont do anything.
670         if the paint col is brighter then the original, then ignore */
671         if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
672                 return col1;
673         
674         cp[0]= 255;
675         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
676         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
677         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
678         return col;
679 }
680
681 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
682 {
683         Brush *brush = paint_brush(&vp->paint);
684
685         if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) *col= mcol_blend( *col, paintcol, alpha);
686         else if(brush->vertexpaint_tool==VP_ADD) *col= mcol_add( *col, paintcol, alpha);
687         else if(brush->vertexpaint_tool==VP_SUB) *col= mcol_sub( *col, paintcol, alpha);
688         else if(brush->vertexpaint_tool==VP_MUL) *col= mcol_mul( *col, paintcol, alpha);
689         else if(brush->vertexpaint_tool==VP_LIGHTEN) *col= mcol_lighten( *col, paintcol, alpha);
690         else if(brush->vertexpaint_tool==VP_DARKEN) *col= mcol_darken( *col, paintcol, alpha);
691         
692         /* if no spray, clip color adding with colorig & orig alpha */
693         if((vp->flag & VP_SPRAY)==0) {
694                 unsigned int testcol=0, a;
695                 char *cp, *ct, *co;
696                 
697                 alpha= (int)(255.0f*brush_alpha(brush));
698                 
699                 if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
700                 else if(brush->vertexpaint_tool==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
701                 else if(brush->vertexpaint_tool==VP_SUB) testcol= mcol_sub( *colorig, paintcol, alpha);
702                 else if(brush->vertexpaint_tool==VP_MUL) testcol= mcol_mul( *colorig, paintcol, alpha);
703                 else if(brush->vertexpaint_tool==VP_LIGHTEN)  testcol= mcol_lighten( *colorig, paintcol, alpha);
704                 else if(brush->vertexpaint_tool==VP_DARKEN)   testcol= mcol_darken( *colorig, paintcol, alpha);
705                 
706                 cp= (char *)col;
707                 ct= (char *)&testcol;
708                 co= (char *)colorig;
709                 
710                 for(a=0; a<4; a++) {
711                         if( ct[a]<co[a] ) {
712                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
713                                 else if( cp[a]>co[a] ) cp[a]= co[a];
714                         }
715                         else {
716                                 if( cp[a]<co[a] ) cp[a]= co[a];
717                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
718                         }
719                 }
720         }
721 }
722
723
724 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
725 {
726         struct ImBuf *ibuf;
727         int a, tot=0, index;
728         
729         /* brecht: disabled this because it obviously failes for
730            brushes with size > 64, why is this here? */
731         /*if(size>64.0) size= 64.0;*/
732         
733         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
734         if(ibuf) {
735                 unsigned int *rt= ibuf->rect;
736
737                 memset(indexar, 0, sizeof(int)*(totface+1));
738                 
739                 size= ibuf->x*ibuf->y;
740                 while(size--) {
741                                 
742                         if(*rt) {
743                                 index= WM_framebuffer_to_index(*rt);
744                                 if(index>0 && index<=totface)
745                                         indexar[index] = 1;
746                         }
747                 
748                         rt++;
749                 }
750                 
751                 for(a=1; a<=totface; a++) {
752                         if(indexar[a]) indexar[tot++]= a;
753                 }
754
755                 IMB_freeImBuf(ibuf);
756         }
757         
758         return tot;
759 }
760
761 static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, const float mval[2], float pressure)
762 {
763         Brush *brush = paint_brush(&vp->paint);
764         float fac, fac_2, size, dx, dy;
765         float alpha;
766         int vertco[2];
767         const int radius= brush_size(brush);
768
769         project_int_noclip(vc->ar, vert_nor, vertco);
770         dx= mval[0]-vertco[0];
771         dy= mval[1]-vertco[1];
772         
773         if (brush_use_size_pressure(brush))
774                 size = pressure * radius;
775         else
776                 size = radius;
777         
778         fac_2= dx*dx + dy*dy;
779         if(fac_2 > size*size) return 0.f;
780         fac = sqrtf(fac_2);
781         
782         alpha= brush_alpha(brush) * brush_curve_strength_clamp(brush, fac, size);
783         
784         if (brush_use_alpha_pressure(brush))
785                 alpha *= pressure;
786                 
787         if(vp->flag & VP_NORMALS) {
788                 float *no= vert_nor+3;
789                 
790                 /* transpose ! */
791                 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
792                 if(fac > 0.0f) {
793                         dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
794                         dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
795                         
796                         alpha*= fac/sqrtf(dx*dx + dy*dy + fac*fac);
797                 }
798                 else return 0.f;
799         }
800         
801         return alpha;
802 }
803
804 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip, int multipaint)
805 {
806         Brush *brush = paint_brush(&wp->paint);
807         int tool = brush->vertexpaint_tool;
808         
809         if(dw==NULL || uw==NULL) return;
810         
811         if (flip) {
812                 switch(tool) {
813                         case VP_MIX:
814                                 paintval = 1.f - paintval; break;
815                         case VP_ADD:
816                                 tool= VP_SUB; break;
817                         case VP_SUB:
818                                 tool= VP_ADD; break;
819                         case VP_LIGHTEN:
820                                 tool= VP_DARKEN; break;
821                         case VP_DARKEN:
822                                 tool= VP_LIGHTEN; break;
823                 }
824         }
825         
826         if(tool==VP_MIX || tool==VP_BLUR)
827                 dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
828         else if(tool==VP_ADD)
829                 dw->weight += paintval*alpha;
830         else if(tool==VP_SUB) 
831                 dw->weight -= paintval*alpha;
832         else if(tool==VP_MUL) 
833                 /* first mul, then blend the fac */
834                 dw->weight = ((1.0f-alpha) + alpha*paintval)*dw->weight;
835         else if(tool==VP_LIGHTEN) {
836                 if (dw->weight < paintval)
837                         dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
838         } else if(tool==VP_DARKEN) {
839                 if (dw->weight > paintval)
840                         dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
841         }
842         // Jason delay clamping until the end so multi-paint can function when the active group is at the limits
843         //CLAMP(dw->weight, 0.0f, 1.0f);
844         
845         /* if no spray, clip result with orig weight & orig alpha */
846         if((wp->flag & VP_SPRAY)==0) {
847                 float testw=0.0f;
848                 
849                 alpha= brush_alpha(brush);
850                 if(tool==VP_MIX || tool==VP_BLUR)
851                         testw = paintval*alpha + uw->weight*(1.0f-alpha);
852                 else if(tool==VP_ADD)
853                         testw = uw->weight + paintval*alpha;
854                 else if(tool==VP_SUB) 
855                         testw = uw->weight - paintval*alpha;
856                 else if(tool==VP_MUL) 
857                         /* first mul, then blend the fac */
858                         testw = ((1.0f-alpha) + alpha*paintval)*uw->weight;
859                 else if(tool==VP_LIGHTEN) {
860                         if (uw->weight < paintval)
861                                 testw = paintval*alpha + uw->weight*(1.0f-alpha);
862                         else
863                                 testw = uw->weight;
864                 } else if(tool==VP_DARKEN) {
865                         if (uw->weight > paintval)
866                                 testw = paintval*alpha + uw->weight*(1.0f-alpha);
867                         else
868                                 testw = uw->weight;
869                 }
870                 //CLAMP(testw, 0.0f, 1.0f);
871                 if(!multipaint) {
872                         if( testw<uw->weight ) {
873                                 if(dw->weight < testw) dw->weight= testw;
874                                 else if(dw->weight > uw->weight) dw->weight= uw->weight;
875                         }
876                         else {
877                                 if(dw->weight > testw) dw->weight= testw;
878                                 else if(dw->weight < uw->weight) dw->weight= uw->weight;
879                         }
880                 }
881         }
882         
883 }
884
885 /* ----------------------------------------------------- */
886
887
888 /* sets wp->weight to the closest weight value to vertex */
889 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
890 static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
891 {
892         ViewContext vc;
893         Mesh *me;
894         short change= FALSE;
895
896         view3d_set_viewcontext(C, &vc);
897         me= get_mesh(vc.obact);
898
899         if (me && me->dvert && vc.v3d && vc.rv3d) {
900                 int index;
901
902                 view3d_operator_needs_opengl(C);
903
904                 index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
905
906                 if(index && index<=me->totface) {
907                         DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
908
909                         if(dm->getVertCo==NULL) {
910                                 BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
911                         }
912                         else {
913                                 MFace *mf= ((MFace *)me->mface) + index-1;
914                                 const int vgroup= vc.obact->actdef - 1;
915                                 ToolSettings *ts= vc.scene->toolsettings;
916                                 float mval_f[2];
917                                 int v_idx_best= -1;
918                                 int fidx;
919                                 float len_best= FLT_MAX;
920
921                                 mval_f[0]= (float)event->mval[0];
922                                 mval_f[1]= (float)event->mval[1];
923
924                                 fidx= mf->v4 ? 3:2;
925                                 do {
926                                         float co[3], sco[3], len;
927                                         const int v_idx= (*(&mf->v1 + fidx));
928                                         dm->getVertCo(dm, v_idx, co);
929                                         project_float_noclip(vc.ar, co, sco);
930                                         len= len_squared_v2v2(mval_f, sco);
931                                         if(len < len_best) {
932                                                 len_best= len;
933                                                 v_idx_best= v_idx;
934                                         }
935                                 } while (fidx--);
936
937                                 if(v_idx_best != -1) { /* should always be valid */
938                                         ts->vgroup_weight= defvert_find_weight(&me->dvert[v_idx_best], vgroup);
939                                         change= TRUE;
940                                 }
941                         }
942                         dm->release(dm);
943                 }
944         }
945
946         if(change) {
947                 /* not really correct since the brush didnt change, but redraws the toolbar */
948                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, NULL); /* ts->wpaint->paint.brush */
949
950                 return OPERATOR_FINISHED;
951         }
952         else {
953                 return OPERATOR_CANCELLED;
954         }
955 }
956
957 void PAINT_OT_weight_sample(wmOperatorType *ot)
958 {
959         /* identifiers */
960         ot->name= "Weight Paint Sample Weight";
961         ot->idname= "PAINT_OT_weight_sample";
962
963         /* api callbacks */
964         ot->invoke= weight_sample_invoke;
965         ot->poll= weight_paint_mode_poll;
966
967         /* flags */
968         ot->flag= OPTYPE_UNDO;
969 }
970
971 /* samples cursor location, and gives menu with vertex groups to activate */
972 static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
973 {
974         if (C) {
975                 wmWindow *win= CTX_wm_window(C);
976                 if(win && win->eventstate) {
977                         ViewContext vc;
978                         Mesh *me;
979
980                         view3d_set_viewcontext(C, &vc);
981                         me= get_mesh(vc.obact);
982
983                         if (me && me->dvert && vc.v3d && vc.rv3d) {
984                                 int index;
985
986                                 view3d_operator_needs_opengl(C);
987
988                                 index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
989
990                                 if(index && index<=me->totface) {
991                                         const int totgroup= BLI_countlist(&vc.obact->defbase);
992                                         if(totgroup) {
993                                                 MFace *mf= ((MFace *)me->mface) + index-1;
994                                                 int fidx= mf->v4 ? 3:2;
995                                                 int *groups= MEM_callocN(totgroup*sizeof(int), "groups");
996                                                 int found= FALSE;
997
998                                                 do {
999                                                         MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
1000                                                         int i= dvert->totweight;
1001                                                         MDeformWeight *dw;
1002                                                         for(dw= dvert->dw; i > 0; dw++, i--) {
1003                                                                 groups[dw->def_nr]= TRUE;
1004                                                                 found= TRUE;
1005                                                         }
1006                                                 } while (fidx--);
1007
1008                                                 if(found==FALSE) {
1009                                                         MEM_freeN(groups);
1010                                                 }
1011                                                 else {
1012                                                         EnumPropertyItem *item= NULL, item_tmp= {0};
1013                                                         int totitem= 0;
1014                                                         int i= 0;
1015                                                         bDeformGroup *dg;
1016                                                         for(dg= vc.obact->defbase.first; dg && i<totgroup; i++, dg= dg->next) {
1017                                                                 if(groups[i]) {
1018                                                                         item_tmp.identifier= item_tmp.name= dg->name;
1019                                                                         item_tmp.value= i;
1020                                                                         RNA_enum_item_add(&item, &totitem, &item_tmp);
1021                                                                 }
1022                                                         }
1023
1024                                                         RNA_enum_item_end(&item, &totitem);
1025                                                         *free= 1;
1026
1027                                                         MEM_freeN(groups);
1028                                                         return item;
1029                                                 }
1030                                         }
1031                                 }
1032                         }
1033                 }
1034         }
1035
1036         return DummyRNA_NULL_items;
1037 }
1038
1039 static int weight_sample_group_exec(bContext *C, wmOperator *op)
1040 {
1041         int type= RNA_enum_get(op->ptr, "group");
1042         ViewContext vc;
1043         view3d_set_viewcontext(C, &vc);
1044
1045         vc.obact->actdef= type + 1;
1046
1047         DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
1048         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, vc.obact);
1049         return OPERATOR_FINISHED;
1050 }
1051
1052 /* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
1053 void PAINT_OT_weight_sample_group(wmOperatorType *ot)
1054 {
1055         PropertyRNA *prop= NULL;
1056
1057         /* identifiers */
1058         ot->name= "Weight Paint Sample Group";
1059         ot->idname= "PAINT_OT_weight_sample_group";
1060
1061         /* api callbacks */
1062         ot->exec= weight_sample_group_exec;
1063         ot->invoke= WM_menu_invoke;
1064         ot->poll= weight_paint_mode_poll;
1065
1066         /* flags */
1067         ot->flag= OPTYPE_UNDO;
1068
1069         /* keyingset to use (dynamic enum) */
1070         prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
1071         RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
1072         ot->prop= prop;
1073 }
1074
1075
1076 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
1077                                            int paint_nr, char *map)
1078 {
1079 //      MDeformWeight *dw = dvert->dw;
1080         float sum=0.0f, fac=0.0f, paintw=0.0f;
1081         int i, tot=0;
1082
1083         if (!map)
1084                 return;
1085
1086         for (i=0; i<dvert->totweight; i++) {
1087                 if (dvert->dw[i].def_nr == paint_nr)
1088                         paintw = dvert->dw[i].weight;
1089
1090                 if (map[dvert->dw[i].def_nr]) {
1091                         tot += 1;
1092                         if (dvert->dw[i].def_nr != paint_nr)
1093                                 sum += dvert->dw[i].weight;
1094                 }
1095         }
1096         
1097         if (!tot || sum <= (1.0f - paintw))
1098                 return;
1099
1100         fac = sum / (1.0f - paintw);
1101         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1102
1103         for (i=0; i<dvert->totweight; i++) {
1104                 if (map[dvert->dw[i].def_nr]) {
1105                         if (dvert->dw[i].def_nr != paint_nr)
1106                                 dvert->dw[i].weight *= fac;
1107                 }
1108         }
1109 }
1110 // Jason was here: the active group should be involved in auto normalize
1111 static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *map)
1112 {
1113 //      MDeformWeight *dw = dvert->dw;
1114         float sum=0.0f, fac=0.0f;
1115         int i, tot=0;
1116
1117         if (!map)
1118                 return;
1119
1120         for (i=0; i<dvert->totweight; i++) {
1121                 if (map[dvert->dw[i].def_nr]) {
1122                         tot += 1;
1123                         sum += dvert->dw[i].weight;
1124                 }
1125         }
1126         
1127         if (!tot || sum == 1.0f)
1128                 return;
1129
1130         fac = sum;
1131         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1132
1133         for (i=0; i<dvert->totweight; i++) {
1134                 if (map[dvert->dw[i].def_nr]) {
1135                         dvert->dw[i].weight *= fac;
1136                 }
1137         }
1138 }
1139 /* Jason was here */
1140 /*
1141 See if the current deform vertex has a locked group
1142 */
1143 static char has_locked_group(MDeformVert *dvert, char *flags)
1144 {
1145         int i;
1146         for(i = 0; i < dvert->totweight; i++) {
1147                 if(flags[(dvert->dw+i)->def_nr] && (dvert->dw+i)->weight > 0.0f) {
1148                         return TRUE;
1149                 }
1150         }
1151         return FALSE;
1152 }
1153 /*Jason was here
1154 gen_lck_flags gets the status of "flag" for each bDeformGroup
1155 in ob->defbase and returns an array containing them
1156 */
1157 static char* gen_lck_flags(Object* ob, int defcnt, char *map)
1158 {
1159         char is_locked = FALSE;
1160         int i;
1161         //int defcnt = BLI_countlist(&ob->defbase);
1162         char *flags = MEM_mallocN(defcnt*sizeof(char), "defflags");
1163         bDeformGroup *defgroup;
1164         int selected = 0;
1165
1166         for(i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
1167                 flags[i] = defgroup->flag;
1168                 if(flags[i]) {
1169                         is_locked = TRUE;
1170                 }
1171         }
1172         if(is_locked){
1173                 return flags;
1174         }
1175         // don't forget to free it if it is unneeded
1176         MEM_freeN(flags);
1177         return NULL;
1178 }
1179 /* Jason was here */
1180 static int has_locked_group_selected(int defcnt, char *selection, char *flags) {
1181         int i;
1182         for(i = 0; i < defcnt; i++) {
1183                 if(selection[i] && flags[i]) {
1184                         return TRUE;
1185                 }
1186         }
1187         return FALSE;
1188 }
1189
1190 /* Jason was here */
1191 static int has_unselected_unlocked_bone_group(int defcnt, char *selection, int selected, char *flags, char *bone_groups) {
1192         int i;
1193         if(defcnt == selected) {
1194                 return FALSE;
1195         }
1196         for(i = 0; i < defcnt; i++) {
1197                 if(bone_groups[i] && !selection[i] && !flags[i]) {
1198                         return TRUE;
1199                 }
1200         }
1201         return FALSE;
1202 }
1203
1204 /*Jason*/
1205 static void multipaint_selection(MDeformVert *dvert, float change, char *selection, int defcnt) {
1206         int i;
1207         MDeformWeight *dw;
1208         float val;
1209         // make sure they are all at most 1 after the change
1210         for(i = 0; i < defcnt; i++) {
1211                 if(selection[i]) {
1212                         dw = defvert_find_index(dvert, i);
1213                         if(dw && dw->weight) {
1214                                 val = dw->weight * change;
1215                                 if(val > 1) {
1216                                         // Jason TODO: when the change is reduced, you need to recheck the earlier values to make sure they are not 0 (precision error)
1217                                         change = 1.0f/dw->weight;
1218                                 }
1219                                 // the value should never reach zero while multi-painting if it was nonzero beforehand
1220                                 if(val <= 0) {
1221                                         return;
1222                                 }
1223                         }
1224                 }
1225         }
1226         // apply the valid change
1227         for(i = 0; i < defcnt; i++) {
1228                 if(selection[i]) {
1229                         dw = defvert_find_index(dvert, i);
1230                         if(dw && dw->weight) {
1231                                 dw->weight = dw->weight * change;
1232                         }
1233                 }
1234         }
1235 }
1236 /*Jason*/
1237 // move all change onto valid, unchanged groups.  If there is change left over, then return it.
1238 // assumes there are valid groups to shift weight onto
1239 static float redistribute_change(MDeformVert *ndv, char *change_status, int changeme, int changeto, char *validmap, float totchange, float total_valid) {
1240         float was_change;
1241         float change;
1242         float oldval;
1243         MDeformWeight *ndw;
1244         int i;
1245         do {
1246                 // assume there is no change until you see one
1247                 was_change = FALSE;
1248                 // change each group by the same amount each time
1249                 change = totchange/total_valid;
1250                 for(i = 0; i < ndv->totweight && total_valid && totchange; i++) {
1251                         ndw = (ndv->dw+i);
1252                         // change only the groups with a valid status
1253                         if(change_status[ndw->def_nr] == changeme) {
1254                                 oldval = ndw->weight;
1255                                 // if auto normalize is active, don't worry about upper bounds
1256                                 if(!validmap && ndw->weight + change > 1) {
1257                                         totchange -= 1-ndw->weight;
1258                                         ndw->weight = 1;
1259                                         // stop the changes to this group
1260                                         change_status[ndw->def_nr] = changeto;
1261                                         total_valid--;
1262                                 } else if(ndw->weight + change < 0) { // check the lower bound
1263                                         totchange -= ndw->weight;
1264                                         ndw->weight = 0;
1265                                         change_status[ndw->def_nr] = changeto;
1266                                         total_valid--;
1267                                 } else {// a perfectly valid change occurred to ndw->weight
1268                                         totchange -= change;
1269                                         ndw->weight += change;
1270                                 }
1271                                 // see if there was a change
1272                                 if(oldval != ndw->weight) {
1273                                         was_change = TRUE;
1274                                 }
1275                         }
1276                 }
1277         // don't go again if there was no change, if there is no valid group, or there is no change left
1278         }while(was_change && total_valid && totchange);
1279         // left overs
1280         return totchange;
1281 }
1282 /*Jason*/
1283 // observe the changes made to the weights of groups.
1284 // make sure all locked groups on the vertex have the same deformation
1285 // by moving the changes made to groups onto other unlocked groups
1286 static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *flags, char *bone_groups, char *validmap) {
1287         float totchange = 0.0f;
1288         float totchange_allowed = 0.0f;
1289         float left_over;
1290
1291         int total_valid = 0;
1292         int total_changed = 0;
1293         int i;
1294         MDeformWeight *ndw;
1295         MDeformWeight *odw;
1296         MDeformWeight *ndw2;
1297         MDeformWeight *odw2;
1298         int designatedw = -1;
1299         int designatedw_changed = FALSE;
1300         float storedw;
1301         char *change_status;
1302         char new_weight_has_zero = FALSE;
1303
1304         if(!flags || !has_locked_group(ndv, flags)) {
1305                 return;
1306         }
1307         // record if a group was changed, unlocked and not changed, or locked
1308         change_status = MEM_callocN(sizeof(char)*defcnt, "unlocked_unchanged");
1309
1310         for(i = 0; i < defcnt; i++) {
1311                 ndw = defvert_find_index(ndv, i);
1312                 odw = defvert_find_index(odv, i);
1313                 // the weights are zero, so we can assume a lot
1314                 if(!ndw || !odw) {
1315                         if (!flags[i] && bone_groups[i]){
1316                                 defvert_verify_index(odv, i);
1317                                 defvert_verify_index(ndv, i);
1318                                 total_valid++;
1319                                 change_status[i] = 1; // can be altered while redistributing
1320                         }
1321                         continue;
1322                 }
1323                 // locked groups should not be changed
1324                 if(flags[i]) {
1325                         ndw->weight = odw->weight;
1326                 } else if(ndw->weight != odw->weight) { // changed groups are handled here
1327                         totchange += ndw->weight-odw->weight;
1328                         change_status[i] = 2; // was altered already
1329                         total_changed++;
1330                         if(ndw->weight == 0) {
1331                                 new_weight_has_zero = TRUE;
1332                         } else if(designatedw == -1){
1333                                 designatedw = i;
1334                         }
1335                 } // unchanged, unlocked bone groups are handled here
1336                 else if (bone_groups[i]){
1337                         totchange_allowed += ndw->weight;
1338                         total_valid++;
1339                         change_status[i] = 1; // can be altered while redistributing
1340                 }
1341         }
1342         // if there was any change, redistribute it
1343         if(total_changed) {
1344                 // auto normalize will allow weights to temporarily go above 1 in redistribution
1345                 if(validmap && total_changed < 0 && total_valid) {
1346                         totchange_allowed = total_valid;
1347                 }
1348                 // there needs to be change allowed, or you should not bother
1349                 if(totchange_allowed) {
1350                         // the way you modify the unlocked+unchanged groups is different depending
1351                         // on whether or not you are painting the weight(s) up or down
1352                         if(totchange < 0) {
1353                                 totchange_allowed = total_valid - totchange_allowed;
1354                         } else {
1355                                 totchange_allowed *= -1;
1356                         }
1357                         left_over = 0;
1358                         if(fabs(totchange_allowed) < fabs(totchange)) {
1359                                 // this amount goes back onto the changed, unlocked weights
1360                                 left_over = fabs(fabs(totchange)-fabs(totchange_allowed));
1361                                 if(totchange > 0) {
1362                                         left_over *= -1;
1363                                 }
1364                         }else {
1365                                 // all of the change will be permitted
1366                                 totchange_allowed = -totchange;
1367                         }
1368                         // move the weight evenly between the allowed groups, move excess back onto the used groups based on the change
1369                         totchange_allowed = redistribute_change(ndv, change_status, 1, -1, validmap, totchange_allowed, total_valid);
1370                         left_over += totchange_allowed;
1371                         if(left_over) {
1372                                 // more than one nonzero weights were changed with the same ratio, so keep them changed that way!
1373                                 if(total_changed > 1 && !new_weight_has_zero && designatedw >= 0) {
1374                                         // this dw is special, it is used as a base to determine how to change the others
1375                                         ndw = defvert_find_index(ndv, designatedw);
1376                                         odw = defvert_find_index(odv, designatedw);
1377                                         storedw = ndw->weight;
1378                                         for(i = 0; i < ndv->totweight; i++) {
1379                                                 if(change_status[ndw->def_nr] == 2) {
1380                                                         odw2 = (odv->dw+i);
1381                                                         ndw2 = (ndv->dw+i);
1382                                                         if(!designatedw_changed) {
1383                                                                 ndw->weight = (totchange_allowed + odw->weight + odw2->weight)/(1 + ndw2->weight/ndw->weight);
1384                                                                 designatedw_changed = TRUE;
1385                                                         }
1386                                                         ndw2->weight = ndw->weight*ndw2->weight/storedw;
1387                                                 }
1388                                         }
1389                                 }
1390                                 // a weight was changed to zero, only one weight was changed, or designatedw is still -1
1391                                 // put weight back as evenly as possible
1392                                 else {
1393                                         redistribute_change(ndv, change_status, 2, -2, validmap, left_over, total_changed);
1394                                 }
1395                         }
1396                 } else {
1397                         // reset the weights
1398                         for(i = 0; i < ndv->totweight; i++) {
1399                                 (ndv->dw+i)->weight = (odv->dw+i)->weight;
1400                         }
1401                 }
1402         }
1403
1404         MEM_freeN(change_status);
1405 }
1406 /*Jason*/
1407 // multi-paint's initial, potential change is computed here based on the user's stroke
1408 static float get_mp_change(MDeformVert *odv, char *selection, float brush_change) {
1409         float selwsum = 0.0f;
1410         int i;
1411         MDeformWeight *dw;
1412         for(i=0; i < odv->totweight; i++) {
1413                 if(selection[(dw = (odv->dw+i))->def_nr]) {
1414                         selwsum += dw->weight;
1415                 }
1416         }
1417         if(selwsum && selwsum+brush_change > 0) {
1418                 return (selwsum+brush_change)/selwsum;
1419         }
1420         return 0.0f;
1421 }
1422 /*Jason*/
1423 // change the weights back to the wv's weights
1424 // it assumes you already have the correct pointer index
1425 static void reset_to_prev(MDeformVert *wv, MDeformVert *dv) {
1426         int i;
1427         MDeformWeight *d;
1428         MDeformWeight *w;
1429         for(i = 0; i < dv->totweight; i++) {
1430                 d = dv->dw+i;
1431                 w = defvert_find_index(wv, d->def_nr);
1432                 // if there was no w when there is a d, then the old weight was 0
1433                 if(w) {
1434                         d->weight = w->weight;
1435                 } else {
1436                         d->weight = 0;
1437                 }
1438         }
1439 }
1440 /* Jason */
1441 static void clamp_weights(MDeformVert *dvert) {
1442         int i;
1443         for (i = 0; i < dvert->totweight; i++) {
1444                 CLAMP((dvert->dw+i)->weight, 0.0f, 1.0f);
1445         }
1446 }
1447 /*Jason*/
1448 /* fresh start to make multi-paint and locking modular */
1449 static int apply_mp_lcks_normalize(Object *ob, Mesh *me, int index, MDeformWeight *dw, MDeformWeight *tdw, int defcnt, float change, float oldChange, float oldw, float neww, char *selection, int selected, char *bone_groups, char *validmap, char *flags, int multipaint) {
1450         MDeformVert *dvert = me->dvert+index;
1451         MDeformVert *dv = MEM_mallocN(sizeof (*(me->dvert+index)), "oldMDeformVert");
1452
1453         dv->dw= MEM_dupallocN(dvert->dw);
1454         dv->flag = dvert->flag;
1455         dv->totweight = dvert->totweight;
1456         // do not multi-paint if a locked group is selected or the active group is locked
1457         // !flags[dw->def_nr] helps if nothing is selected, but active group is locked
1458         if(!flags || flags && !has_locked_group_selected(defcnt, selection, flags) && !flags[dw->def_nr]) {
1459                 if(multipaint && selected > 1) {
1460                         if(change && change!=1) {
1461                                 multipaint_selection(dvert, change, selection, defcnt);
1462                         }
1463                 } else {// this lets users paint normally, but don't let them paint locked groups
1464                         dw->weight = neww;
1465                 }
1466         }
1467         clamp_weights(dvert);
1468
1469         enforce_locks(dv, dvert, defcnt, flags, bone_groups, validmap);
1470
1471         do_weight_paint_auto_normalize_all_groups(dvert, validmap);
1472
1473         if(oldChange && multipaint && selected > 1) {
1474                 if(tdw->weight != oldw) {
1475                         if( neww > oldw ) {
1476                                 if(tdw->weight > oldw) {
1477                                         //printf("should have changed\n");
1478                                 } else {
1479                                         //printf("should not have changed\n");
1480                                         //reset_to_prev(dv, dvert);
1481                                         MEM_freeN(dv->dw);
1482                                         MEM_freeN(dv);
1483                                         return TRUE;
1484                                 }
1485                         } else {
1486                                 if(tdw->weight < oldw) {
1487                                         //printf("should have changed\n");
1488                                 } else {
1489                                         //printf("should not have changed\n");
1490                                         //reset_to_prev(dv, dvert);
1491                                         MEM_freeN(dv->dw);
1492                                         MEM_freeN(dv);
1493                                         return TRUE;
1494                                 }
1495                         }
1496                 }
1497         }
1498         MEM_freeN(dv->dw);
1499         MEM_freeN(dv);
1500         return FALSE;
1501 }
1502
1503 /* Jason was here duplicate function I used in DerivedMesh.c*/
1504 static char* get_selected_defgroups(Object *ob, int defcnt) {
1505         bPoseChannel *chan;
1506         bPose *pose;
1507         bDeformGroup *defgroup;
1508         //Bone *bone;
1509         char *dg_flags = MEM_callocN(defcnt*sizeof(char), "dg_selected_flags");
1510         int i;
1511         Object *armob = ED_object_pose_armature(ob);
1512
1513         if(armob) {
1514                 pose = armob->pose;
1515                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1516                         for (i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
1517                                 if(!strcmp(defgroup->name, chan->bone->name)) {
1518                                         dg_flags[i] = (chan->bone->flag & BONE_SELECTED);
1519                                 }
1520                         }
1521                 }
1522         }
1523         // the array has whether or not the corresponding group is selected
1524         return dg_flags;
1525 }
1526 /* TODO move duplicates to header */
1527 /* Jason was here duplicate function */
1528 static int count_true(char *list, int len)
1529 {
1530         int i;
1531         int cnt = 0;
1532         for(i = 0; i < len; i++) {
1533                 if (list[i]) {
1534                         cnt++;
1535                 }
1536         }
1537         return cnt;
1538 }
1539 // within the current dvert index, get the dw that is selected and has a weight above 0
1540 // this helps multi-paint
1541 static int get_first_selected_nonzero_weight(MDeformVert *dvert, char *selection) {
1542         int i;
1543         MDeformWeight *dw;
1544         for(i=0; i< dvert->totweight; i++) {
1545                 dw = dvert->dw+i;
1546                 if(selection[dw->def_nr] && dw->weight > 0) {
1547                         return i;
1548                 }
1549         }
1550         return -1;
1551 }
1552 // Jason
1553 static char *wpaint_make_validmap(Object *ob);
1554
1555 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
1556                                    float alpha, float paintweight, int flip, 
1557                                    int vgroup_mirror, char *validmap, int multipaint)
1558 {
1559         Mesh *me= ob->data;
1560         //                                              Jason: tdw, tuw
1561         MDeformWeight *dw, *uw, *tdw = NULL, *tuw;
1562         int vgroup= ob->actdef-1;
1563
1564         /* Jason was here */
1565         char *flags;
1566         char *bone_groups;
1567         char *selection;
1568         int selected;
1569         float oldw;
1570         float neww;
1571         float testw=0;
1572         int defcnt;
1573         float change = 0;
1574         float oldChange = 0;
1575         int i;
1576         MDeformVert *dv = NULL;
1577
1578         // Need to know which groups are bone groups
1579         if(validmap) {
1580                 bone_groups = validmap;
1581         }else {
1582                 bone_groups = wpaint_make_validmap(ob);
1583         }
1584
1585         if(wp->flag & VP_ONLYVGROUP) {
1586                 dw= defvert_find_index(me->dvert+index, vgroup);
1587                 uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
1588         }
1589         else {
1590                 dw= defvert_verify_index(me->dvert+index, vgroup);
1591                 uw= defvert_verify_index(wp->wpaint_prev+index, vgroup);
1592         }
1593         if(dw==NULL || uw==NULL)
1594                 return;
1595         /* Jason was here */
1596         flags = gen_lck_flags(ob, defcnt = BLI_countlist(&ob->defbase), bone_groups);
1597         selection = get_selected_defgroups(ob, defcnt);
1598         selected = count_true(selection, defcnt);
1599         if(!selected && ob->actdef) {
1600                 selected = 1;
1601         }
1602         
1603         oldw = dw->weight;
1604         wpaint_blend(wp, dw, uw, alpha, paintweight, flip, multipaint && selected >1);
1605         neww = dw->weight;
1606         dw->weight = oldw;
1607         
1608         // setup multi-paint
1609         if(selected > 1 && multipaint) {
1610                 dv = MEM_mallocN(sizeof (*(me->dvert+index)), "prevMDeformVert");
1611
1612                 dv->dw= MEM_dupallocN((me->dvert+index)->dw);
1613                 dv->flag = (me->dvert+index)->flag;
1614                 dv->totweight = (me->dvert+index)->totweight;
1615                 tdw = dw;
1616                 tuw = uw;
1617                 change = get_mp_change(wp->wpaint_prev+index, selection, neww-oldw);
1618                 if(change) {
1619                         if(!tdw->weight) {
1620                                 i = get_first_selected_nonzero_weight(me->dvert+index, selection);
1621                                 if(i>=0) {
1622                                         tdw = ((me->dvert+index)->dw+i);
1623                                         tuw = defvert_verify_index(wp->wpaint_prev+index, tdw->def_nr);
1624                                 } else {
1625                                         change = 0;
1626                                 }
1627                         }
1628                         if(change && tuw->weight && tuw->weight * change) {
1629                                 if(tdw->weight != tuw->weight) {
1630                                         oldChange = tdw->weight/tuw->weight;
1631                                         testw = tuw->weight*change;
1632                                         if( testw > tuw->weight ) {
1633                                                 if(change > oldChange) {
1634                                                         reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
1635                                                 } else {
1636                                                         change = 0;
1637                                                 }
1638                                         } else {
1639                                                 if(change < oldChange) {
1640                                                         reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
1641                                                 } else {
1642                                                         change = 0;
1643                                                 }
1644                                         }
1645                                 }
1646                         } else {
1647                                 change = 0;
1648                         }
1649                 }
1650         }
1651         /* Jason was here */
1652         if(apply_mp_lcks_normalize(ob, me, index, dw, tdw, defcnt, change, oldChange, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint)) {
1653                 reset_to_prev(dv, me->dvert+index);
1654                 change = 0;
1655                 oldChange = 0;
1656         }
1657         if(dv) {
1658                 MEM_freeN(dv->dw);
1659                 MEM_freeN(dv);
1660         }
1661         // dvert may have been altered greatly
1662         dw = defvert_find_index(me->dvert+index, vgroup);
1663
1664         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
1665                 int j= mesh_get_x_mirror_vert(ob, index);
1666                 if(j>=0) {
1667                         /* copy, not paint again */
1668                         if(vgroup_mirror != -1)
1669                                 uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
1670                         else
1671                                 uw= defvert_verify_index(me->dvert+j, vgroup);
1672                         /* Jason */
1673                         //uw->weight= dw->weight;
1674                         /* Jason */
1675                         apply_mp_lcks_normalize(ob, me, j, uw, tdw, defcnt, change, oldChange, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
1676                 }
1677         }
1678         /* Jason */
1679         if(flags) {
1680                 MEM_freeN(flags);
1681         }
1682         MEM_freeN(selection);
1683         if(!validmap) {
1684                 MEM_freeN(bone_groups);
1685         }
1686 }
1687
1688 /* *************** set wpaint operator ****************** */
1689
1690 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))              /* toggle */
1691 {               
1692         Object *ob= CTX_data_active_object(C);
1693         Scene *scene= CTX_data_scene(C);
1694         VPaint *wp= scene->toolsettings->wpaint;
1695         Mesh *me;
1696         
1697         me= get_mesh(ob);
1698         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1699         
1700         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1701         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1702         
1703         
1704         /* Weightpaint works by overriding colors in mesh,
1705                 * so need to make sure we recalc on enter and
1706                 * exit (exit needs doing regardless because we
1707                                 * should redeform).
1708                 */
1709         DAG_id_tag_update(&me->id, 0);
1710         
1711         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1712                 Object *par;
1713                 
1714                 if(wp==NULL)
1715                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1716
1717                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1718                 paint_cursor_start(C, weight_paint_poll);
1719                 
1720                 mesh_octree_table(ob, NULL, NULL, 's');
1721                 
1722                 /* verify if active weight group is also active bone */
1723                 par= modifiers_isDeformedByArmature(ob);
1724                 if(par && (par->mode & OB_MODE_POSE)) {
1725                         bArmature *arm= par->data;
1726
1727                         if(arm->act_bone)
1728                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1729                 }
1730         }
1731         else {
1732                 mesh_octree_table(NULL, NULL, NULL, 'e');
1733                 mesh_mirrtopo_table(NULL, 'e');
1734         }
1735         
1736         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1737         
1738         return OPERATOR_FINISHED;
1739 }
1740
1741 /* for switching to/from mode */
1742 static int paint_poll_test(bContext *C)
1743 {
1744         if(CTX_data_edit_object(C))
1745                 return 0;
1746         if(CTX_data_active_object(C)==NULL)
1747                 return 0;
1748         return 1;
1749 }
1750
1751 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1752 {
1753         
1754         /* identifiers */
1755         ot->name= "Weight Paint Mode";
1756         ot->idname= "PAINT_OT_weight_paint_toggle";
1757         
1758         /* api callbacks */
1759         ot->exec= set_wpaint;
1760         ot->poll= paint_poll_test;
1761         
1762         /* flags */
1763         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1764         
1765 }
1766
1767 /* ************ weight paint operator ********** */
1768
1769 struct WPaintData {
1770         ViewContext vc;
1771         int *indexar;
1772         int vgroup_mirror;
1773         float *vertexcosnos;
1774         float wpimat[3][3];
1775         
1776         /*variables for auto normalize*/
1777         int auto_normalize;
1778         char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1779 };
1780
1781 static char *wpaint_make_validmap(Object *ob)
1782 {
1783         bDeformGroup *dg;
1784         ModifierData *md;
1785         char *validmap;
1786         bPose *pose;
1787         bPoseChannel *chan;
1788         ArmatureModifierData *amd;
1789         GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1790         int i = 0, step1=1;
1791
1792         /*add all names to a hash table*/
1793         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1794                 BLI_ghash_insert(gh, dg->name, NULL);
1795         }
1796
1797         if (!i)
1798                 return NULL;
1799
1800         validmap = MEM_callocN(i, "wpaint valid map");
1801
1802         /*now loop through the armature modifiers and identify deform bones*/
1803         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1804                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1805                         continue;
1806
1807                 if (md->type == eModifierType_Armature) 
1808                 {
1809                         amd = (ArmatureModifierData*) md;
1810
1811                         if(amd->object && amd->object->pose) {
1812                                 pose = amd->object->pose;
1813                                 
1814                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1815                                         if (chan->bone->flag & BONE_NO_DEFORM)
1816                                                 continue;
1817
1818                                         if (BLI_ghash_haskey(gh, chan->name)) {
1819                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1820                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1821                                         }
1822                                 }
1823                         }
1824                 }
1825         }
1826         
1827         /*add all names to a hash table*/
1828         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1829                 if (BLI_ghash_lookup(gh, dg->name) != NULL) {
1830                         validmap[i] = 1;
1831                 }
1832         }
1833
1834         BLI_ghash_free(gh, NULL, NULL);
1835
1836         return validmap;
1837 }
1838
1839 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1840 {
1841         Scene *scene= CTX_data_scene(C);
1842         struct PaintStroke *stroke = op->customdata;
1843         ToolSettings *ts= CTX_data_tool_settings(C);
1844         VPaint *wp= ts->wpaint;
1845         Object *ob= CTX_data_active_object(C);
1846         struct WPaintData *wpd;
1847         Mesh *me;
1848         float mat[4][4], imat[4][4];
1849
1850         if(scene->obedit) return OPERATOR_CANCELLED;
1851         
1852         me= get_mesh(ob);
1853         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1854         
1855         /* if nothing was added yet, we make dverts and a vertex deform group */
1856         if (!me->dvert) {
1857                 ED_vgroup_data_create(&me->id);
1858                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1859         }
1860
1861
1862         /* make mode data storage */
1863         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1864         paint_stroke_set_mode_data(stroke, wpd);
1865         view3d_set_viewcontext(C, &wpd->vc);
1866         wpd->vgroup_mirror= -1;
1867
1868         /*set up auto-normalize, and generate map for detecting which
1869           vgroups affect deform bones*/
1870         wpd->auto_normalize = ts->auto_normalize;
1871         if (wpd->auto_normalize)
1872                 wpd->vgroup_validmap = wpaint_make_validmap(ob);
1873         
1874         //      if(qual & LR_CTRLKEY) {
1875         //              sample_wpaint(scene, ar, v3d, 0);
1876         //              return;
1877         //      }
1878         //      if(qual & LR_SHIFTKEY) {
1879         //              sample_wpaint(scene, ar, v3d, 1);
1880         //              return;
1881         //      }
1882         
1883         /* ALLOCATIONS! no return after this line */
1884         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1885         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1886         wpd->indexar= get_indexarray(me);
1887         copy_wpaint_prev(wp, me->dvert, me->totvert);
1888         
1889         /* this happens on a Bone select, when no vgroup existed yet */
1890         if(ob->actdef<=0) {
1891                 Object *modob;
1892                 if((modob = modifiers_isDeformedByArmature(ob))) {
1893                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
1894                         if(actbone) {
1895                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
1896
1897                                 if(pchan) {
1898                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
1899                                         if(dg==NULL)
1900                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
1901                                         else
1902                                                 ob->actdef= 1 + defgroup_find_index(ob, dg);
1903                                 }
1904                         }
1905                 }
1906         }
1907         if(ob->defbase.first==NULL) {
1908                 ED_vgroup_add(ob);
1909         }
1910         
1911         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1912         
1913         /* imat for normals */
1914         mul_m4_m4m4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1915         invert_m4_m4(imat, mat);
1916         copy_m3_m4(wpd->wpimat, imat);
1917         
1918         /* if mirror painting, find the other group */
1919         if(me->editflag & ME_EDIT_MIRROR_X) {
1920                 wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
1921         }
1922
1923         return 1;
1924 }
1925
1926 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1927 {
1928         ToolSettings *ts= CTX_data_tool_settings(C);
1929         VPaint *wp= ts->wpaint;
1930         Brush *brush = paint_brush(&wp->paint);
1931         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1932         ViewContext *vc;
1933         Object *ob;
1934         Mesh *me;
1935         float mat[4][4];
1936         float paintweight;
1937         int *indexar;
1938         int totindex, index, totw, flip;
1939         float alpha;
1940         float mval[2], pressure;
1941         
1942         // Jason
1943         int selectedVerts;
1944
1945         /* cannot paint if there is no stroke data */
1946         if (wpd == NULL) {
1947                 // XXX: force a redraw here, since even though we can't paint, 
1948                 // at least view won't freeze until stroke ends
1949                 ED_region_tag_redraw(CTX_wm_region(C));
1950                 return;
1951         }
1952                 
1953         vc= &wpd->vc;
1954         ob= vc->obact;
1955         me= ob->data;
1956         indexar= wpd->indexar;
1957         
1958         view3d_operator_needs_opengl(C);
1959                 
1960         /* load projection matrix */
1961         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1962
1963         flip = RNA_boolean_get(itemptr, "pen_flip");
1964         pressure = RNA_float_get(itemptr, "pressure");
1965         RNA_float_get_array(itemptr, "mouse", mval);
1966         mval[0]-= vc->ar->winrct.xmin;
1967         mval[1]-= vc->ar->winrct.ymin;
1968                         
1969         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1970                         
1971         /* which faces are involved */
1972         if(wp->flag & VP_AREA) {
1973                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1974         }
1975         else {
1976                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1977                 if(indexar[0]) totindex= 1;
1978                 else totindex= 0;
1979         }
1980                         
1981         if(wp->flag & VP_COLINDEX) {
1982                 for(index=0; index<totindex; index++) {
1983                         if(indexar[index] && indexar[index]<=me->totface) {
1984                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1985                                                 
1986                                 if(mface->mat_nr!=ob->actcol-1) {
1987                                         indexar[index]= 0;
1988                                 }
1989                         }
1990                 }
1991         }
1992         // Jason
1993         selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
1994                         
1995         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1996                 for(index=0; index<totindex; index++) {
1997                         if(indexar[index] && indexar[index]<=me->totface) {
1998                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1999                                                 
2000                                 if((mface->flag & ME_FACE_SEL)==0) {
2001                                         indexar[index]= 0;
2002                                 }
2003                         }                                       
2004                 }
2005         }
2006                         
2007         /* make sure each vertex gets treated only once */
2008         /* and calculate filter weight */
2009         totw= 0;
2010         if(brush->vertexpaint_tool==VP_BLUR) 
2011                 paintweight= 0.0f;
2012         else
2013                 paintweight= ts->vgroup_weight;
2014                         
2015         for(index=0; index<totindex; index++) {
2016                 if(indexar[index] && indexar[index]<=me->totface) {
2017                         MFace *mface= me->mface + (indexar[index]-1);
2018                                         
2019                         (me->dvert+mface->v1)->flag= selectedVerts ? ((me->mvert+mface->v1)->flag & SELECT): 1;
2020                         (me->dvert+mface->v2)->flag= selectedVerts ? ((me->mvert+mface->v2)->flag & SELECT): 1;
2021                         (me->dvert+mface->v3)->flag= selectedVerts ? ((me->mvert+mface->v3)->flag & SELECT): 1;
2022                         if(mface->v4) (me->dvert+mface->v4)->flag= selectedVerts ? ((me->mvert+mface->v4)->flag & SELECT): 1;
2023                                         
2024                         if(brush->vertexpaint_tool==VP_BLUR) {
2025                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
2026                                                 
2027                                 if(wp->flag & VP_ONLYVGROUP)
2028                                         dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
2029                                 else
2030                                         dw_func= defvert_verify_index;
2031                                                 
2032                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
2033                                 if(dw) {paintweight+= dw->weight; totw++;}
2034                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
2035                                 if(dw) {paintweight+= dw->weight; totw++;}
2036                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
2037                                 if(dw) {paintweight+= dw->weight; totw++;}
2038                                 if(mface->v4) {
2039                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
2040                                         if(dw) {paintweight+= dw->weight; totw++;}
2041                                 }
2042                         }
2043                 }
2044         }
2045                         
2046         if(brush->vertexpaint_tool==VP_BLUR) 
2047                 paintweight/= (float)totw;
2048                         
2049         for(index=0; index<totindex; index++) {
2050                                 
2051                 if(indexar[index] && indexar[index]<=me->totface) {
2052                         MFace *mface= me->mface + (indexar[index]-1);
2053                                         
2054                         if((me->dvert+mface->v1)->flag) {
2055                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
2056                                 if(alpha) {
2057                                         do_weight_paint_vertex(wp, ob, mface->v1, 
2058                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2059                                                 wpd->vgroup_validmap, ts->multipaint);
2060                                 }
2061                                 (me->dvert+mface->v1)->flag= 0;
2062                         }
2063                                         
2064                         if((me->dvert+mface->v2)->flag) {
2065                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
2066                                 if(alpha) {
2067                                         do_weight_paint_vertex(wp, ob, mface->v2, 
2068                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2069                                                 wpd->vgroup_validmap, ts->multipaint);
2070                                 }
2071                                 (me->dvert+mface->v2)->flag= 0;
2072                         }
2073                                         
2074                         if((me->dvert+mface->v3)->flag) {
2075                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
2076                                 if(alpha) {
2077                                         do_weight_paint_vertex(wp, ob, mface->v3, 
2078                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2079                                                 wpd->vgroup_validmap, ts->multipaint);
2080                                 }
2081                                 (me->dvert+mface->v3)->flag= 0;
2082                         }
2083                                         
2084                         if((me->dvert+mface->v4)->flag) {
2085                                 if(mface->v4) {
2086                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
2087                                         if(alpha) {
2088                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
2089                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
2090                                                         wpd->vgroup_validmap, ts->multipaint);
2091                                         }
2092                                         (me->dvert+mface->v4)->flag= 0;
2093                                 }
2094                         }
2095                 }
2096         }
2097                         
2098         swap_m4m4(vc->rv3d->persmat, mat);
2099                         
2100         DAG_id_tag_update(ob->data, 0);
2101         ED_region_tag_redraw(vc->ar);
2102 }
2103
2104 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2105 {
2106         ToolSettings *ts= CTX_data_tool_settings(C);
2107         Object *ob= CTX_data_active_object(C);
2108         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2109         
2110         if(wpd) {
2111                 if(wpd->vertexcosnos)
2112                         MEM_freeN(wpd->vertexcosnos);
2113                 MEM_freeN(wpd->indexar);
2114                 
2115                 if (wpd->vgroup_validmap)
2116                         MEM_freeN(wpd->vgroup_validmap);
2117                 
2118                 MEM_freeN(wpd);
2119         }
2120         
2121         /* frees prev buffer */
2122         copy_wpaint_prev(ts->wpaint, NULL, 0);
2123         
2124         /* and particles too */
2125         if(ob->particlesystem.first) {
2126                 ParticleSystem *psys;
2127                 int i;
2128                 
2129                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
2130                         for(i=0; i<PSYS_TOT_VG; i++) {
2131                                 if(psys->vgroup[i]==ob->actdef) {
2132                                         psys->recalc |= PSYS_RECALC_RESET;
2133                                         break;
2134                                 }
2135                         }
2136                 }
2137         }
2138         
2139         DAG_id_tag_update(ob->data, 0);
2140 }
2141
2142
2143 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2144 {
2145         
2146         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
2147                                           wpaint_stroke_update_step,
2148                                           wpaint_stroke_done, event->type);
2149         
2150         /* add modal handler */
2151         WM_event_add_modal_handler(C, op);
2152
2153         op->type->modal(C, op, event);
2154         
2155         return OPERATOR_RUNNING_MODAL;
2156 }
2157
2158 static int wpaint_cancel(bContext *C, wmOperator *op)
2159 {
2160         paint_stroke_cancel(C, op);
2161
2162         return OPERATOR_CANCELLED;
2163 }
2164
2165 void PAINT_OT_weight_paint(wmOperatorType *ot)
2166 {
2167         
2168         /* identifiers */
2169         ot->name= "Weight Paint";
2170         ot->idname= "PAINT_OT_weight_paint";
2171         
2172         /* api callbacks */
2173         ot->invoke= wpaint_invoke;
2174         ot->modal= paint_stroke_modal;
2175         /* ot->exec= vpaint_exec; <-- needs stroke property */
2176         ot->poll= weight_paint_poll;
2177         ot->cancel= wpaint_cancel;
2178         
2179         /* flags */
2180         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2181
2182         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2183 }
2184
2185 static int weight_paint_set_exec(bContext *C, wmOperator *UNUSED(op))
2186 {
2187         struct Scene *scene= CTX_data_scene(C);
2188         Object *obact = CTX_data_active_object(C);
2189
2190         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
2191         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
2192         return OPERATOR_FINISHED;
2193 }
2194
2195 void PAINT_OT_weight_set(wmOperatorType *ot)
2196 {
2197         /* identifiers */
2198         ot->name= "Set Weight";
2199         ot->idname= "PAINT_OT_weight_set";
2200
2201         /* api callbacks */
2202         ot->exec= weight_paint_set_exec;
2203         ot->poll= mask_paint_poll; // Jason, it was facemask_paint_poll
2204
2205         /* flags */
2206         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2207 }
2208
2209 /* ************ set / clear vertex paint mode ********** */
2210
2211
2212 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
2213 {       
2214         Object *ob= CTX_data_active_object(C);
2215         Scene *scene= CTX_data_scene(C);
2216         VPaint *vp= scene->toolsettings->vpaint;
2217         Mesh *me;
2218         
2219         me= get_mesh(ob);
2220         
2221         if(me==NULL || object_data_is_libdata(ob)) {
2222                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2223                 return OPERATOR_PASS_THROUGH;
2224         }
2225         
2226         if(me && me->mcol==NULL) make_vertexcol(ob);
2227         
2228         /* toggle: end vpaint */
2229         if(ob->mode & OB_MODE_VERTEX_PAINT) {
2230                 
2231                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2232         }
2233         else {
2234                 ob->mode |= OB_MODE_VERTEX_PAINT;
2235                 /* Turn off weight painting */
2236                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
2237                         set_wpaint(C, op);
2238                 
2239                 if(vp==NULL)
2240                         vp= scene->toolsettings->vpaint= new_vpaint(0);
2241                 
2242                 paint_cursor_start(C, vertex_paint_poll);
2243
2244                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
2245         }
2246         
2247         if (me)
2248                 /* update modifier stack for mapping requirements */
2249                 DAG_id_tag_update(&me->id, 0);
2250         
2251         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
2252         
2253         return OPERATOR_FINISHED;
2254 }
2255
2256 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
2257 {
2258         
2259         /* identifiers */
2260         ot->name= "Vertex Paint Mode";
2261         ot->idname= "PAINT_OT_vertex_paint_toggle";
2262         
2263         /* api callbacks */
2264         ot->exec= set_vpaint;
2265         ot->poll= paint_poll_test;
2266         
2267         /* flags */
2268         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2269 }
2270
2271
2272
2273 /* ********************** vertex paint operator ******************* */
2274
2275 /* Implementation notes:
2276
2277 Operator->invoke()
2278   - validate context (add mcol)
2279   - create customdata storage
2280   - call paint once (mouse click)
2281   - add modal handler 
2282
2283 Operator->modal()
2284   - for every mousemove, apply vertex paint
2285   - exit on mouse release, free customdata
2286         (return OPERATOR_FINISHED also removes handler and operator)
2287
2288 For future:
2289   - implement a stroke event (or mousemove with past positons)
2290   - revise whether op->customdata should be added in object, in set_vpaint
2291
2292 */
2293
2294 typedef struct VPaintData {
2295         ViewContext vc;
2296         unsigned int paintcol;
2297         int *indexar;
2298         float *vertexcosnos;
2299         float vpimat[3][3];
2300 } VPaintData;
2301
2302 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
2303 {
2304         ToolSettings *ts= CTX_data_tool_settings(C);
2305         struct PaintStroke *stroke = op->customdata;
2306         VPaint *vp= ts->vpaint;
2307         struct VPaintData *vpd;
2308         Object *ob= CTX_data_active_object(C);
2309         Mesh *me;
2310         float mat[4][4], imat[4][4];
2311
2312         /* context checks could be a poll() */
2313         me= get_mesh(ob);
2314         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2315         
2316         if(me->mcol==NULL) make_vertexcol(ob);
2317         if(me->mcol==NULL) return OPERATOR_CANCELLED;
2318         
2319         /* make mode data storage */
2320         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
2321         paint_stroke_set_mode_data(stroke, vpd);
2322         view3d_set_viewcontext(C, &vpd->vc);
2323         
2324         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
2325         vpd->indexar= get_indexarray(me);
2326         vpd->paintcol= vpaint_get_current_col(vp);
2327         
2328         /* for filtering */
2329         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
2330         
2331         /* some old cruft to sort out later */
2332         mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
2333         invert_m4_m4(imat, mat);
2334         copy_m3_m4(vpd->vpimat, imat);
2335
2336         return 1;
2337 }
2338
2339 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, const float mval[2], float pressure, int UNUSED(flip))
2340 {
2341         ViewContext *vc = &vpd->vc;
2342         Brush *brush = paint_brush(&vp->paint);
2343         Mesh *me = get_mesh(ob);
2344         MFace *mface= ((MFace*)me->mface) + index;
2345
2346         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
2347         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
2348         float alpha;
2349         int i;
2350
2351         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
2352            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
2353                    return;
2354
2355         if(brush->vertexpaint_tool==VP_BLUR) {
2356                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
2357                 if(mface->v4) {
2358                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
2359                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
2360                 }
2361                 else {
2362                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
2363                 }
2364                 
2365         }
2366
2367         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
2368                 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i], mval, pressure);
2369                 if(alpha)
2370                         vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0f));
2371         }
2372 }
2373
2374 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2375 {
2376         ToolSettings *ts= CTX_data_tool_settings(C);
2377         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
2378         VPaint *vp= ts->vpaint;
2379         Brush *brush = paint_brush(&vp->paint);
2380         ViewContext *vc= &vpd->vc;
2381         Object *ob= vc->obact;
2382         Mesh *me= ob->data;
2383         float mat[4][4];
2384         int *indexar= vpd->indexar;
2385         int totindex, index, flip;
2386         float pressure, mval[2];
2387
2388         RNA_float_get_array(itemptr, "mouse", mval);
2389         flip = RNA_boolean_get(itemptr, "pen_flip");
2390         pressure = RNA_float_get(itemptr, "pressure");
2391                         
2392         view3d_operator_needs_opengl(C);
2393                         
2394         /* load projection matrix */
2395         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
2396
2397         mval[0]-= vc->ar->winrct.xmin;
2398         mval[1]-= vc->ar->winrct.ymin;
2399
2400                         
2401         /* which faces are involved */
2402         if(vp->flag & VP_AREA) {
2403                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
2404         }
2405         else {
2406                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
2407                 if(indexar[0]) totindex= 1;
2408                 else totindex= 0;
2409         }
2410                         
2411         swap_m4m4(vc->rv3d->persmat, mat);
2412                         
2413         for(index=0; index<totindex; index++) {                         
2414                 if(indexar[index] && indexar[index]<=me->totface)
2415                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, pressure, flip);
2416         }
2417                                                 
2418         swap_m4m4(vc->rv3d->persmat, mat);
2419
2420         /* was disabled because it is slow, but necessary for blur */
2421         if(brush->vertexpaint_tool == VP_BLUR)
2422                 do_shared_vertexcol(me);
2423                         
2424         ED_region_tag_redraw(vc->ar);
2425                         
2426         DAG_id_tag_update(ob->data, 0);
2427 }
2428
2429 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2430 {
2431         ToolSettings *ts= CTX_data_tool_settings(C);
2432         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
2433         
2434         if(vpd->vertexcosnos)
2435                 MEM_freeN(vpd->vertexcosnos);
2436         MEM_freeN(vpd->indexar);
2437         
2438         /* frees prev buffer */
2439         copy_vpaint_prev(ts->vpaint, NULL, 0);
2440         
2441         MEM_freeN(vpd);
2442 }
2443
2444 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2445 {
2446         
2447         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
2448                                           vpaint_stroke_update_step,
2449                                           vpaint_stroke_done, event->type);
2450         
2451         /* add modal handler */
2452         WM_event_add_modal_handler(C, op);
2453
2454         op->type->modal(C, op, event);
2455         
2456         return OPERATOR_RUNNING_MODAL;
2457 }
2458
2459 static int vpaint_cancel(bContext *C, wmOperator *op)
2460 {
2461         paint_stroke_cancel(C, op);
2462
2463         return OPERATOR_CANCELLED;
2464 }
2465
2466 void PAINT_OT_vertex_paint(wmOperatorType *ot)
2467 {
2468         /* identifiers */
2469         ot->name= "Vertex Paint";
2470         ot->idname= "PAINT_OT_vertex_paint";
2471         
2472         /* api callbacks */
2473         ot->invoke= vpaint_invoke;
2474         ot->modal= paint_stroke_modal;
2475         /* ot->exec= vpaint_exec; <-- needs stroke property */
2476         ot->poll= vertex_paint_poll;
2477         ot->cancel= vpaint_cancel;
2478         
2479         /* flags */
2480         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2481
2482         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2483 }
2484
2485 /* ********************** weight from bones operator ******************* */
2486
2487 static int weight_from_bones_poll(bContext *C)
2488 {
2489         Object *ob= CTX_data_active_object(C);
2490
2491         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
2492 }
2493
2494 static int weight_from_bones_exec(bContext *C, wmOperator *op)
2495 {
2496         Scene *scene= CTX_data_scene(C);
2497         Object *ob= CTX_data_active_object(C);
2498         Object *armob= modifiers_isDeformedByArmature(ob);
2499         Mesh *me= ob->data;
2500         int type= RNA_enum_get(op->ptr, "type");
2501
2502         create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
2503
2504         DAG_id_tag_update(&me->id, 0);
2505         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
2506
2507         return OPERATOR_FINISHED;
2508 }
2509
2510 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
2511 {
2512         static EnumPropertyItem type_items[]= {
2513                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
2514                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
2515                 {0, NULL, 0, NULL, NULL}};
2516
2517         /* identifiers */
2518         ot->name= "Weight from Bones";
2519         ot->idname= "PAINT_OT_weight_from_bones";
2520         
2521         /* api callbacks */
2522         ot->exec= weight_from_bones_exec;
2523         ot->invoke= WM_menu_invoke;
2524         ot->poll= weight_from_bones_poll;
2525         
2526         /* flags */
2527         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2528
2529         /* properties */
2530         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights.");
2531 }
2532