svn merge -r39800:39900 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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 /* returns TRUE if it thinks you need to reset the weights due to normalizing while multi-painting */
1450 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) {
1451         MDeformVert *dvert = me->dvert+index;
1452         MDeformVert *dv = MEM_mallocN(sizeof (*(me->dvert+index)), "oldMDeformVert");
1453
1454         dv->dw= MEM_dupallocN(dvert->dw);
1455         dv->flag = dvert->flag;
1456         dv->totweight = dvert->totweight;
1457         // do not multi-paint if a locked group is selected or the active group is locked
1458         // !flags[dw->def_nr] helps if nothing is selected, but active group is locked
1459         if(!flags || flags && !has_locked_group_selected(defcnt, selection, flags) && !flags[dw->def_nr]) {
1460                 if(multipaint && selected > 1) {
1461                         if(change && change!=1) {
1462                                 multipaint_selection(dvert, change, selection, defcnt);
1463                         }
1464                 } else {// this lets users paint normally, but don't let them paint locked groups
1465                         dw->weight = neww;
1466                 }
1467         }
1468         clamp_weights(dvert);
1469
1470         enforce_locks(dv, dvert, defcnt, flags, bone_groups, validmap);
1471
1472         do_weight_paint_auto_normalize_all_groups(dvert, validmap);
1473
1474         if(oldChange && multipaint && selected > 1) {
1475                 if(tdw->weight != oldw) {
1476                         if( neww > oldw ) {
1477                                 if(tdw->weight <= oldw) {
1478                                         MEM_freeN(dv->dw);
1479                                         MEM_freeN(dv);
1480                                         return TRUE;
1481                                 }
1482                         } else {
1483                                 if(tdw->weight >= oldw) {
1484                                         MEM_freeN(dv->dw);
1485                                         MEM_freeN(dv);
1486                                         return TRUE;
1487                                 }
1488                         }
1489                 }
1490         }
1491         MEM_freeN(dv->dw);
1492         MEM_freeN(dv);
1493         return FALSE;
1494 }
1495
1496 /* Jason was here duplicate function I used in DerivedMesh.c*/
1497 static char* get_selected_defgroups(Object *ob, int defcnt) {
1498         bPoseChannel *chan;
1499         bPose *pose;
1500         bDeformGroup *defgroup;
1501         //Bone *bone;
1502         char *dg_flags = MEM_callocN(defcnt*sizeof(char), "dg_selected_flags");
1503         int i;
1504         Object *armob = ED_object_pose_armature(ob);
1505
1506         if(armob) {
1507                 pose = armob->pose;
1508                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1509                         for (i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
1510                                 if(!strcmp(defgroup->name, chan->bone->name)) {
1511                                         dg_flags[i] = (chan->bone->flag & BONE_SELECTED);
1512                                 }
1513                         }
1514                 }
1515         }
1516         // the array has whether or not the corresponding group is selected
1517         return dg_flags;
1518 }
1519 /* TODO move duplicates to header */
1520 /* Jason was here duplicate function */
1521 static int count_true(char *list, int len)
1522 {
1523         int i;
1524         int cnt = 0;
1525         for(i = 0; i < len; i++) {
1526                 if (list[i]) {
1527                         cnt++;
1528                 }
1529         }
1530         return cnt;
1531 }
1532 // within the current dvert index, get the dw that is selected and has a weight above 0
1533 // this helps multi-paint
1534 static int get_first_selected_nonzero_weight(MDeformVert *dvert, char *selection) {
1535         int i;
1536         MDeformWeight *dw;
1537         for(i=0; i< dvert->totweight; i++) {
1538                 dw = dvert->dw+i;
1539                 if(selection[dw->def_nr] && dw->weight > 0) {
1540                         return i;
1541                 }
1542         }
1543         return -1;
1544 }
1545 // Jason
1546 static char *wpaint_make_validmap(Object *ob);
1547
1548 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
1549                                    float alpha, float paintweight, int flip, 
1550                                    int vgroup_mirror, char *validmap, int multipaint)
1551 {
1552         Mesh *me= ob->data;
1553         //                                              Jason: tdw, tuw
1554         MDeformWeight *dw, *uw, *tdw = NULL, *tuw;
1555         int vgroup= ob->actdef-1;
1556
1557         /* Jason was here */
1558         char *flags;
1559         char *bone_groups;
1560         char *selection;
1561         int selected;
1562         float oldw;
1563         float neww;
1564         float testw=0;
1565         int defcnt;
1566         float change = 0;
1567         float oldChange = 0;
1568         int i;
1569         MDeformVert *dv = NULL;
1570
1571         // Need to know which groups are bone groups
1572         if(validmap) {
1573                 bone_groups = validmap;
1574         }else {
1575                 bone_groups = wpaint_make_validmap(ob);
1576         }
1577
1578         if(wp->flag & VP_ONLYVGROUP) {
1579                 dw= defvert_find_index(me->dvert+index, vgroup);
1580                 uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
1581         }
1582         else {
1583                 dw= defvert_verify_index(me->dvert+index, vgroup);
1584                 uw= defvert_verify_index(wp->wpaint_prev+index, vgroup);
1585         }
1586         if(dw==NULL || uw==NULL)
1587                 return;
1588         /* Jason was here */
1589         flags = gen_lck_flags(ob, defcnt = BLI_countlist(&ob->defbase), bone_groups);
1590         selection = get_selected_defgroups(ob, defcnt);
1591         selected = count_true(selection, defcnt);
1592         if(!selected && ob->actdef) {
1593                 selected = 1;
1594         }
1595         
1596         oldw = dw->weight;
1597         wpaint_blend(wp, dw, uw, alpha, paintweight, flip, multipaint && selected >1);
1598         neww = dw->weight;
1599         dw->weight = oldw;
1600         
1601         // setup multi-paint
1602         if(selected > 1 && multipaint) {
1603                 dv = MEM_mallocN(sizeof (*(me->dvert+index)), "prevMDeformVert");
1604
1605                 dv->dw= MEM_dupallocN((me->dvert+index)->dw);
1606                 dv->flag = (me->dvert+index)->flag;
1607                 dv->totweight = (me->dvert+index)->totweight;
1608                 tdw = dw;
1609                 tuw = uw;
1610                 change = get_mp_change(wp->wpaint_prev+index, selection, neww-oldw);
1611                 if(change) {
1612                         if(!tdw->weight) {
1613                                 i = get_first_selected_nonzero_weight(me->dvert+index, selection);
1614                                 if(i>=0) {
1615                                         tdw = ((me->dvert+index)->dw+i);
1616                                         tuw = defvert_verify_index(wp->wpaint_prev+index, tdw->def_nr);
1617                                 } else {
1618                                         change = 0;
1619                                 }
1620                         }
1621                         if(change && tuw->weight && tuw->weight * change) {
1622                                 if(tdw->weight != tuw->weight) {
1623                                         oldChange = tdw->weight/tuw->weight;
1624                                         testw = tuw->weight*change;
1625                                         if( testw > tuw->weight ) {
1626                                                 if(change > oldChange) {
1627                                                         // reset the weights and use the new change
1628                                                         reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
1629                                                 } else {
1630                                                         // the old change was more significant,
1631                                                         // so set the change to 0 so that it will not do another multi-paint
1632                                                         change = 0;
1633                                                 }
1634                                         } else {
1635                                                 if(change < oldChange) {
1636                                                         reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
1637                                                 } else {
1638                                                         change = 0;
1639                                                 }
1640                                         }
1641                                 }
1642                         } else {
1643                                 change = 0;
1644                         }
1645                 }
1646         }
1647         /* Jason was here */
1648         if(apply_mp_lcks_normalize(ob, me, index, dw, tdw, defcnt, change, oldChange, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint)) {
1649                 reset_to_prev(dv, me->dvert+index);
1650                 change = 0;
1651                 oldChange = 0;
1652         }
1653         if(dv) {
1654                 MEM_freeN(dv->dw);
1655                 MEM_freeN(dv);
1656         }
1657         // dvert may have been altered greatly
1658         dw = defvert_find_index(me->dvert+index, vgroup);
1659
1660         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
1661                 int j= mesh_get_x_mirror_vert(ob, index);
1662                 if(j>=0) {
1663                         /* copy, not paint again */
1664                         if(vgroup_mirror != -1)
1665                                 uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
1666                         else
1667                                 uw= defvert_verify_index(me->dvert+j, vgroup);
1668                         /* Jason */
1669                         //uw->weight= dw->weight;
1670                         /* Jason */
1671                         apply_mp_lcks_normalize(ob, me, j, uw, tdw, defcnt, change, oldChange, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
1672                 }
1673         }
1674         /* Jason */
1675         if(flags) {
1676                 MEM_freeN(flags);
1677         }
1678         MEM_freeN(selection);
1679         if(!validmap) {
1680                 MEM_freeN(bone_groups);
1681         }
1682 }
1683
1684 /* *************** set wpaint operator ****************** */
1685
1686 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))              /* toggle */
1687 {               
1688         Object *ob= CTX_data_active_object(C);
1689         Scene *scene= CTX_data_scene(C);
1690         VPaint *wp= scene->toolsettings->wpaint;
1691         Mesh *me;
1692         
1693         me= get_mesh(ob);
1694         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1695         
1696         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1697         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1698         
1699         
1700         /* Weightpaint works by overriding colors in mesh,
1701                 * so need to make sure we recalc on enter and
1702                 * exit (exit needs doing regardless because we
1703                                 * should redeform).
1704                 */
1705         DAG_id_tag_update(&me->id, 0);
1706         
1707         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1708                 Object *par;
1709                 
1710                 if(wp==NULL)
1711                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1712
1713                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1714                 paint_cursor_start(C, weight_paint_poll);
1715                 
1716                 mesh_octree_table(ob, NULL, NULL, 's');
1717                 
1718                 /* verify if active weight group is also active bone */
1719                 par= modifiers_isDeformedByArmature(ob);
1720                 if(par && (par->mode & OB_MODE_POSE)) {
1721                         bArmature *arm= par->data;
1722
1723                         if(arm->act_bone)
1724                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1725                 }
1726         }
1727         else {
1728                 mesh_octree_table(NULL, NULL, NULL, 'e');
1729                 mesh_mirrtopo_table(NULL, 'e');
1730         }
1731         
1732         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1733         
1734         return OPERATOR_FINISHED;
1735 }
1736
1737 /* for switching to/from mode */
1738 static int paint_poll_test(bContext *C)
1739 {
1740         if(CTX_data_edit_object(C))
1741                 return 0;
1742         if(CTX_data_active_object(C)==NULL)
1743                 return 0;
1744         return 1;
1745 }
1746
1747 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1748 {
1749         
1750         /* identifiers */
1751         ot->name= "Weight Paint Mode";
1752         ot->idname= "PAINT_OT_weight_paint_toggle";
1753         
1754         /* api callbacks */
1755         ot->exec= set_wpaint;
1756         ot->poll= paint_poll_test;
1757         
1758         /* flags */
1759         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1760         
1761 }
1762
1763 /* ************ weight paint operator ********** */
1764
1765 struct WPaintData {
1766         ViewContext vc;
1767         int *indexar;
1768         int vgroup_mirror;
1769         float *vertexcosnos;
1770         float wpimat[3][3];
1771         
1772         /*variables for auto normalize*/
1773         int auto_normalize;
1774         char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1775 };
1776
1777 static char *wpaint_make_validmap(Object *ob)
1778 {
1779         bDeformGroup *dg;
1780         ModifierData *md;
1781         char *validmap;
1782         bPose *pose;
1783         bPoseChannel *chan;
1784         ArmatureModifierData *amd;
1785         GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1786         int i = 0, step1=1;
1787
1788         /*add all names to a hash table*/
1789         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1790                 BLI_ghash_insert(gh, dg->name, NULL);
1791         }
1792
1793         if (!i)
1794                 return NULL;
1795
1796         validmap = MEM_callocN(i, "wpaint valid map");
1797
1798         /*now loop through the armature modifiers and identify deform bones*/
1799         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1800                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1801                         continue;
1802
1803                 if (md->type == eModifierType_Armature) 
1804                 {
1805                         amd = (ArmatureModifierData*) md;
1806
1807                         if(amd->object && amd->object->pose) {
1808                                 pose = amd->object->pose;
1809                                 
1810                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1811                                         if (chan->bone->flag & BONE_NO_DEFORM)
1812                                                 continue;
1813
1814                                         if (BLI_ghash_haskey(gh, chan->name)) {
1815                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1816                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1817                                         }
1818                                 }
1819                         }
1820                 }
1821         }
1822         
1823         /*add all names to a hash table*/
1824         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1825                 if (BLI_ghash_lookup(gh, dg->name) != NULL) {
1826                         validmap[i] = 1;
1827                 }
1828         }
1829
1830         BLI_ghash_free(gh, NULL, NULL);
1831
1832         return validmap;
1833 }
1834
1835 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1836 {
1837         Scene *scene= CTX_data_scene(C);
1838         struct PaintStroke *stroke = op->customdata;
1839         ToolSettings *ts= CTX_data_tool_settings(C);
1840         VPaint *wp= ts->wpaint;
1841         Object *ob= CTX_data_active_object(C);
1842         struct WPaintData *wpd;
1843         Mesh *me;
1844         float mat[4][4], imat[4][4];
1845
1846         if(scene->obedit) return OPERATOR_CANCELLED;
1847         
1848         me= get_mesh(ob);
1849         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1850         
1851         /* if nothing was added yet, we make dverts and a vertex deform group */
1852         if (!me->dvert) {
1853                 ED_vgroup_data_create(&me->id);
1854                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1855         }
1856
1857
1858         /* make mode data storage */
1859         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1860         paint_stroke_set_mode_data(stroke, wpd);
1861         view3d_set_viewcontext(C, &wpd->vc);
1862         wpd->vgroup_mirror= -1;
1863
1864         /*set up auto-normalize, and generate map for detecting which
1865           vgroups affect deform bones*/
1866         wpd->auto_normalize = ts->auto_normalize;
1867         if (wpd->auto_normalize)
1868                 wpd->vgroup_validmap = wpaint_make_validmap(ob);
1869         
1870         //      if(qual & LR_CTRLKEY) {
1871         //              sample_wpaint(scene, ar, v3d, 0);
1872         //              return;
1873         //      }
1874         //      if(qual & LR_SHIFTKEY) {
1875         //              sample_wpaint(scene, ar, v3d, 1);
1876         //              return;
1877         //      }
1878         
1879         /* ALLOCATIONS! no return after this line */
1880         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1881         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1882         wpd->indexar= get_indexarray(me);
1883         copy_wpaint_prev(wp, me->dvert, me->totvert);
1884         
1885         /* this happens on a Bone select, when no vgroup existed yet */
1886         if(ob->actdef<=0) {
1887                 Object *modob;
1888                 if((modob = modifiers_isDeformedByArmature(ob))) {
1889                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
1890                         if(actbone) {
1891                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
1892
1893                                 if(pchan) {
1894                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
1895                                         if(dg==NULL)
1896                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
1897                                         else
1898                                                 ob->actdef= 1 + defgroup_find_index(ob, dg);
1899                                 }
1900                         }
1901                 }
1902         }
1903         if(ob->defbase.first==NULL) {
1904                 ED_vgroup_add(ob);
1905         }
1906         
1907         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1908         
1909         /* imat for normals */
1910         mul_m4_m4m4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1911         invert_m4_m4(imat, mat);
1912         copy_m3_m4(wpd->wpimat, imat);
1913         
1914         /* if mirror painting, find the other group */
1915         if(me->editflag & ME_EDIT_MIRROR_X) {
1916                 wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
1917         }
1918
1919         return 1;
1920 }
1921
1922 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1923 {
1924         ToolSettings *ts= CTX_data_tool_settings(C);
1925         VPaint *wp= ts->wpaint;
1926         Brush *brush = paint_brush(&wp->paint);
1927         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1928         ViewContext *vc;
1929         Object *ob;
1930         Mesh *me;
1931         float mat[4][4];
1932         float paintweight;
1933         int *indexar;
1934         int totindex, index, totw, flip;
1935         float alpha;
1936         float mval[2], pressure;
1937         
1938         // Jason
1939         int selectedVerts;
1940
1941         /* cannot paint if there is no stroke data */
1942         if (wpd == NULL) {
1943                 // XXX: force a redraw here, since even though we can't paint, 
1944                 // at least view won't freeze until stroke ends
1945                 ED_region_tag_redraw(CTX_wm_region(C));
1946                 return;
1947         }
1948                 
1949         vc= &wpd->vc;
1950         ob= vc->obact;
1951         me= ob->data;
1952         indexar= wpd->indexar;
1953         
1954         view3d_operator_needs_opengl(C);
1955                 
1956         /* load projection matrix */
1957         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1958
1959         flip = RNA_boolean_get(itemptr, "pen_flip");
1960         pressure = RNA_float_get(itemptr, "pressure");
1961         RNA_float_get_array(itemptr, "mouse", mval);
1962         mval[0]-= vc->ar->winrct.xmin;
1963         mval[1]-= vc->ar->winrct.ymin;
1964                         
1965         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1966                         
1967         /* which faces are involved */
1968         if(wp->flag & VP_AREA) {
1969                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1970         }
1971         else {
1972                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1973                 if(indexar[0]) totindex= 1;
1974                 else totindex= 0;
1975         }
1976                         
1977         if(wp->flag & VP_COLINDEX) {
1978                 for(index=0; index<totindex; index++) {
1979                         if(indexar[index] && indexar[index]<=me->totface) {
1980                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1981                                                 
1982                                 if(mface->mat_nr!=ob->actcol-1) {
1983                                         indexar[index]= 0;
1984                                 }
1985                         }
1986                 }
1987         }
1988         // Jason
1989         selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
1990                         
1991         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1992                 for(index=0; index<totindex; index++) {
1993                         if(indexar[index] && indexar[index]<=me->totface) {
1994                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1995                                                 
1996                                 if((mface->flag & ME_FACE_SEL)==0) {
1997                                         indexar[index]= 0;
1998                                 }
1999                         }                                       
2000                 }
2001         }
2002                         
2003         /* make sure each vertex gets treated only once */
2004         /* and calculate filter weight */
2005         totw= 0;
2006         if(brush->vertexpaint_tool==VP_BLUR) 
2007                 paintweight= 0.0f;
2008         else
2009                 paintweight= ts->vgroup_weight;
2010                         
2011         for(index=0; index<totindex; index++) {
2012                 if(indexar[index] && indexar[index]<=me->totface) {
2013                         MFace *mface= me->mface + (indexar[index]-1);
2014                                         
2015                         (me->dvert+mface->v1)->flag= selectedVerts ? ((me->mvert+mface->v1)->flag & SELECT): 1;
2016                         (me->dvert+mface->v2)->flag= selectedVerts ? ((me->mvert+mface->v2)->flag & SELECT): 1;
2017                         (me->dvert+mface->v3)->flag= selectedVerts ? ((me->mvert+mface->v3)->flag & SELECT): 1;
2018                         if(mface->v4) (me->dvert+mface->v4)->flag= selectedVerts ? ((me->mvert+mface->v4)->flag & SELECT): 1;
2019                                         
2020                         if(brush->vertexpaint_tool==VP_BLUR) {
2021                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
2022                                                 
2023                                 if(wp->flag & VP_ONLYVGROUP)
2024                                         dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
2025                                 else
2026                                         dw_func= defvert_verify_index;
2027                                                 
2028                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
2029                                 if(dw) {paintweight+= dw->weight; totw++;}
2030                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
2031                                 if(dw) {paintweight+= dw->weight; totw++;}
2032                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
2033                                 if(dw) {paintweight+= dw->weight; totw++;}
2034                                 if(mface->v4) {
2035                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
2036                                         if(dw) {paintweight+= dw->weight; totw++;}
2037                                 }
2038                         }
2039                 }
2040         }
2041                         
2042         if(brush->vertexpaint_tool==VP_BLUR) 
2043                 paintweight/= (float)totw;
2044                         
2045         for(index=0; index<totindex; index++) {
2046                                 
2047                 if(indexar[index] && indexar[index]<=me->totface) {
2048                         MFace *mface= me->mface + (indexar[index]-1);
2049                                         
2050                         if((me->dvert+mface->v1)->flag) {
2051                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
2052                                 if(alpha) {
2053                                         do_weight_paint_vertex(wp, ob, mface->v1, 
2054                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2055                                                 wpd->vgroup_validmap, ts->multipaint);
2056                                 }
2057                                 (me->dvert+mface->v1)->flag= 0;
2058                         }
2059                                         
2060                         if((me->dvert+mface->v2)->flag) {
2061                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
2062                                 if(alpha) {
2063                                         do_weight_paint_vertex(wp, ob, mface->v2, 
2064                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2065                                                 wpd->vgroup_validmap, ts->multipaint);
2066                                 }
2067                                 (me->dvert+mface->v2)->flag= 0;
2068                         }
2069                                         
2070                         if((me->dvert+mface->v3)->flag) {
2071                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
2072                                 if(alpha) {
2073                                         do_weight_paint_vertex(wp, ob, mface->v3, 
2074                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2075                                                 wpd->vgroup_validmap, ts->multipaint);
2076                                 }
2077                                 (me->dvert+mface->v3)->flag= 0;
2078                         }
2079                                         
2080                         if((me->dvert+mface->v4)->flag) {
2081                                 if(mface->v4) {
2082                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
2083                                         if(alpha) {
2084                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
2085                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
2086                                                         wpd->vgroup_validmap, ts->multipaint);
2087                                         }
2088                                         (me->dvert+mface->v4)->flag= 0;
2089                                 }
2090                         }
2091                 }
2092         }
2093                         
2094         swap_m4m4(vc->rv3d->persmat, mat);
2095                         
2096         DAG_id_tag_update(ob->data, 0);
2097         ED_region_tag_redraw(vc->ar);
2098 }
2099
2100 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2101 {
2102         ToolSettings *ts= CTX_data_tool_settings(C);
2103         Object *ob= CTX_data_active_object(C);
2104         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2105         
2106         if(wpd) {
2107                 if(wpd->vertexcosnos)
2108                         MEM_freeN(wpd->vertexcosnos);
2109                 MEM_freeN(wpd->indexar);
2110                 
2111                 if (wpd->vgroup_validmap)
2112                         MEM_freeN(wpd->vgroup_validmap);
2113                 
2114                 MEM_freeN(wpd);
2115         }
2116         
2117         /* frees prev buffer */
2118         copy_wpaint_prev(ts->wpaint, NULL, 0);
2119         
2120         /* and particles too */
2121         if(ob->particlesystem.first) {
2122                 ParticleSystem *psys;
2123                 int i;
2124                 
2125                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
2126                         for(i=0; i<PSYS_TOT_VG; i++) {
2127                                 if(psys->vgroup[i]==ob->actdef) {
2128                                         psys->recalc |= PSYS_RECALC_RESET;
2129                                         break;
2130                                 }
2131                         }
2132                 }
2133         }
2134         
2135         DAG_id_tag_update(ob->data, 0);
2136 }
2137
2138
2139 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2140 {
2141         
2142         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
2143                                           wpaint_stroke_update_step,
2144                                           wpaint_stroke_done, event->type);
2145         
2146         /* add modal handler */
2147         WM_event_add_modal_handler(C, op);
2148
2149         op->type->modal(C, op, event);
2150         
2151         return OPERATOR_RUNNING_MODAL;
2152 }
2153
2154 static int wpaint_cancel(bContext *C, wmOperator *op)
2155 {
2156         paint_stroke_cancel(C, op);
2157
2158         return OPERATOR_CANCELLED;
2159 }
2160
2161 void PAINT_OT_weight_paint(wmOperatorType *ot)
2162 {
2163         
2164         /* identifiers */
2165         ot->name= "Weight Paint";
2166         ot->idname= "PAINT_OT_weight_paint";
2167         
2168         /* api callbacks */
2169         ot->invoke= wpaint_invoke;
2170         ot->modal= paint_stroke_modal;
2171         /* ot->exec= vpaint_exec; <-- needs stroke property */
2172         ot->poll= weight_paint_poll;
2173         ot->cancel= wpaint_cancel;
2174         
2175         /* flags */
2176         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2177
2178         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2179 }
2180
2181 static int weight_paint_set_exec(bContext *C, wmOperator *UNUSED(op))
2182 {
2183         struct Scene *scene= CTX_data_scene(C);
2184         Object *obact = CTX_data_active_object(C);
2185
2186         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
2187         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
2188         return OPERATOR_FINISHED;
2189 }
2190
2191 void PAINT_OT_weight_set(wmOperatorType *ot)
2192 {
2193         /* identifiers */
2194         ot->name= "Set Weight";
2195         ot->idname= "PAINT_OT_weight_set";
2196
2197         /* api callbacks */
2198         ot->exec= weight_paint_set_exec;
2199         ot->poll= mask_paint_poll; // Jason, it was facemask_paint_poll
2200
2201         /* flags */
2202         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2203 }
2204
2205 /* ************ set / clear vertex paint mode ********** */
2206
2207
2208 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
2209 {       
2210         Object *ob= CTX_data_active_object(C);
2211         Scene *scene= CTX_data_scene(C);
2212         VPaint *vp= scene->toolsettings->vpaint;
2213         Mesh *me;
2214         
2215         me= get_mesh(ob);
2216         
2217         if(me==NULL || object_data_is_libdata(ob)) {
2218                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2219                 return OPERATOR_PASS_THROUGH;
2220         }
2221         
2222         if(me && me->mcol==NULL) make_vertexcol(ob);
2223         
2224         /* toggle: end vpaint */
2225         if(ob->mode & OB_MODE_VERTEX_PAINT) {
2226                 
2227                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2228         }
2229         else {
2230                 ob->mode |= OB_MODE_VERTEX_PAINT;
2231                 /* Turn off weight painting */
2232                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
2233                         set_wpaint(C, op);
2234                 
2235                 if(vp==NULL)
2236                         vp= scene->toolsettings->vpaint= new_vpaint(0);
2237                 
2238                 paint_cursor_start(C, vertex_paint_poll);
2239
2240                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
2241         }
2242         
2243         if (me)
2244                 /* update modifier stack for mapping requirements */
2245                 DAG_id_tag_update(&me->id, 0);
2246         
2247         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
2248         
2249         return OPERATOR_FINISHED;
2250 }
2251
2252 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
2253 {
2254         
2255         /* identifiers */
2256         ot->name= "Vertex Paint Mode";
2257         ot->idname= "PAINT_OT_vertex_paint_toggle";
2258         
2259         /* api callbacks */
2260         ot->exec= set_vpaint;
2261         ot->poll= paint_poll_test;
2262         
2263         /* flags */
2264         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2265 }
2266
2267
2268
2269 /* ********************** vertex paint operator ******************* */
2270
2271 /* Implementation notes:
2272
2273 Operator->invoke()
2274   - validate context (add mcol)
2275   - create customdata storage
2276   - call paint once (mouse click)
2277   - add modal handler 
2278
2279 Operator->modal()
2280   - for every mousemove, apply vertex paint
2281   - exit on mouse release, free customdata
2282         (return OPERATOR_FINISHED also removes handler and operator)
2283
2284 For future:
2285   - implement a stroke event (or mousemove with past positons)
2286   - revise whether op->customdata should be added in object, in set_vpaint
2287
2288 */
2289
2290 typedef struct VPaintData {
2291         ViewContext vc;
2292         unsigned int paintcol;
2293         int *indexar;
2294         float *vertexcosnos;
2295         float vpimat[3][3];
2296 } VPaintData;
2297
2298 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
2299 {
2300         ToolSettings *ts= CTX_data_tool_settings(C);
2301         struct PaintStroke *stroke = op->customdata;
2302         VPaint *vp= ts->vpaint;
2303         struct VPaintData *vpd;
2304         Object *ob= CTX_data_active_object(C);
2305         Mesh *me;
2306         float mat[4][4], imat[4][4];
2307
2308         /* context checks could be a poll() */
2309         me= get_mesh(ob);
2310         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2311         
2312         if(me->mcol==NULL) make_vertexcol(ob);
2313         if(me->mcol==NULL) return OPERATOR_CANCELLED;
2314         
2315         /* make mode data storage */
2316         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
2317         paint_stroke_set_mode_data(stroke, vpd);
2318         view3d_set_viewcontext(C, &vpd->vc);
2319         
2320         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
2321         vpd->indexar= get_indexarray(me);
2322         vpd->paintcol= vpaint_get_current_col(vp);
2323         
2324         /* for filtering */
2325         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
2326         
2327         /* some old cruft to sort out later */
2328         mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
2329         invert_m4_m4(imat, mat);
2330         copy_m3_m4(vpd->vpimat, imat);
2331
2332         return 1;
2333 }
2334
2335 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, const float mval[2], float pressure, int UNUSED(flip))
2336 {
2337         ViewContext *vc = &vpd->vc;
2338         Brush *brush = paint_brush(&vp->paint);
2339         Mesh *me = get_mesh(ob);
2340         MFace *mface= ((MFace*)me->mface) + index;
2341
2342         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
2343         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
2344         float alpha;
2345         int i;
2346
2347         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
2348            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
2349                    return;
2350
2351         if(brush->vertexpaint_tool==VP_BLUR) {
2352                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
2353                 if(mface->v4) {
2354                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
2355                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
2356                 }
2357                 else {
2358                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
2359                 }
2360                 
2361         }
2362
2363         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
2364                 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i], mval, pressure);
2365                 if(alpha)
2366                         vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0f));
2367         }
2368 }
2369
2370 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2371 {
2372         ToolSettings *ts= CTX_data_tool_settings(C);
2373         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
2374         VPaint *vp= ts->vpaint;
2375         Brush *brush = paint_brush(&vp->paint);
2376         ViewContext *vc= &vpd->vc;
2377         Object *ob= vc->obact;
2378         Mesh *me= ob->data;
2379         float mat[4][4];
2380         int *indexar= vpd->indexar;
2381         int totindex, index, flip;
2382         float pressure, mval[2];
2383
2384         RNA_float_get_array(itemptr, "mouse", mval);
2385         flip = RNA_boolean_get(itemptr, "pen_flip");
2386         pressure = RNA_float_get(itemptr, "pressure");
2387                         
2388         view3d_operator_needs_opengl(C);
2389                         
2390         /* load projection matrix */
2391         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
2392
2393         mval[0]-= vc->ar->winrct.xmin;
2394         mval[1]-= vc->ar->winrct.ymin;
2395
2396                         
2397         /* which faces are involved */
2398         if(vp->flag & VP_AREA) {
2399                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
2400         }
2401         else {
2402                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
2403                 if(indexar[0]) totindex= 1;
2404                 else totindex= 0;
2405         }
2406                         
2407         swap_m4m4(vc->rv3d->persmat, mat);
2408                         
2409         for(index=0; index<totindex; index++) {                         
2410                 if(indexar[index] && indexar[index]<=me->totface)
2411                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, pressure, flip);
2412         }
2413                                                 
2414         swap_m4m4(vc->rv3d->persmat, mat);
2415
2416         /* was disabled because it is slow, but necessary for blur */
2417         if(brush->vertexpaint_tool == VP_BLUR)
2418                 do_shared_vertexcol(me);
2419                         
2420         ED_region_tag_redraw(vc->ar);
2421                         
2422         DAG_id_tag_update(ob->data, 0);
2423 }
2424
2425 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2426 {
2427         ToolSettings *ts= CTX_data_tool_settings(C);
2428         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
2429         
2430         if(vpd->vertexcosnos)
2431                 MEM_freeN(vpd->vertexcosnos);
2432         MEM_freeN(vpd->indexar);
2433         
2434         /* frees prev buffer */
2435         copy_vpaint_prev(ts->vpaint, NULL, 0);
2436         
2437         MEM_freeN(vpd);
2438 }
2439
2440 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2441 {
2442         
2443         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
2444                                           vpaint_stroke_update_step,
2445                                           vpaint_stroke_done, event->type);
2446         
2447         /* add modal handler */
2448         WM_event_add_modal_handler(C, op);
2449
2450         op->type->modal(C, op, event);
2451         
2452         return OPERATOR_RUNNING_MODAL;
2453 }
2454
2455 static int vpaint_cancel(bContext *C, wmOperator *op)
2456 {
2457         paint_stroke_cancel(C, op);
2458
2459         return OPERATOR_CANCELLED;
2460 }
2461
2462 void PAINT_OT_vertex_paint(wmOperatorType *ot)
2463 {
2464         /* identifiers */
2465         ot->name= "Vertex Paint";
2466         ot->idname= "PAINT_OT_vertex_paint";
2467         
2468         /* api callbacks */
2469         ot->invoke= vpaint_invoke;
2470         ot->modal= paint_stroke_modal;
2471         /* ot->exec= vpaint_exec; <-- needs stroke property */
2472         ot->poll= vertex_paint_poll;
2473         ot->cancel= vpaint_cancel;
2474         
2475         /* flags */
2476         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2477
2478         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2479 }
2480
2481 /* ********************** weight from bones operator ******************* */
2482
2483 static int weight_from_bones_poll(bContext *C)
2484 {
2485         Object *ob= CTX_data_active_object(C);
2486
2487         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
2488 }
2489
2490 static int weight_from_bones_exec(bContext *C, wmOperator *op)
2491 {
2492         Scene *scene= CTX_data_scene(C);
2493         Object *ob= CTX_data_active_object(C);
2494         Object *armob= modifiers_isDeformedByArmature(ob);
2495         Mesh *me= ob->data;
2496         int type= RNA_enum_get(op->ptr, "type");
2497
2498         create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
2499
2500         DAG_id_tag_update(&me->id, 0);
2501         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
2502
2503         return OPERATOR_FINISHED;
2504 }
2505
2506 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
2507 {
2508         static EnumPropertyItem type_items[]= {
2509                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
2510                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
2511                 {0, NULL, 0, NULL, NULL}};
2512
2513         /* identifiers */
2514         ot->name= "Weight from Bones";
2515         ot->idname= "PAINT_OT_weight_from_bones";
2516         
2517         /* api callbacks */
2518         ot->exec= weight_from_bones_exec;
2519         ot->invoke= WM_menu_invoke;
2520         ot->poll= weight_from_bones_poll;
2521         
2522         /* flags */
2523         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2524
2525         /* properties */
2526         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights.");
2527 }
2528