c0822ca134acefc61144ed560d4bce524e133936
[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, selectedVerts;//Jason
365         // Jason
366         MVert *mv;
367         
368         me= get_mesh(ob);
369         if(me==NULL || me->totface==0) return;
370
371         if(!me->mcol)
372                 make_vertexcol(ob);
373
374         selected= (me->editflag & ME_EDIT_PAINT_MASK);
375         // Jason
376         selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
377         mv = me->mvert;
378
379         mf = me->mface;
380         mcol = (unsigned int*)me->mcol;
381         for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
382                 if (!selected || mf->flag & ME_FACE_SEL || selectedVerts) {
383                         if(selectedVerts) {
384                                 if(((mv+mf->v1)->flag & SELECT))
385                                         mcol[0] = paintcol;
386                                 if(((mv+mf->v2)->flag & SELECT))
387                                         mcol[1] = paintcol;
388                                 if(((mv+mf->v3)->flag & SELECT))
389                                         mcol[2] = paintcol;
390                                 if(mf->v4 && ((mv+mf->v4)->flag & SELECT))
391                                         mcol[3] = paintcol;
392                         } else {
393                                 mcol[0] = paintcol;
394                                 mcol[1] = paintcol;
395                                 mcol[2] = paintcol;
396                                 mcol[3] = paintcol;
397                         }
398                 }
399         }
400         
401         DAG_id_tag_update(&me->id, 0);
402 }
403
404
405 /* fills in the selected faces with the current weight and vertex group */
406 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
407 {
408         Mesh *me;
409         MFace *mface;
410         MDeformWeight *dw, *uw;
411         int *indexar;
412         int index, vgroup;
413         unsigned int faceverts[5]={0,0,0,0,0};
414         unsigned char i;
415         int vgroup_mirror= -1;
416         int selected;
417         // Jason
418         int selectedVerts;
419
420         me= ob->data;
421         if(me==NULL || me->totface==0 || me->dvert==NULL || !me->mface) return;
422         
423         selected= (me->editflag & ME_EDIT_PAINT_MASK);
424         // Jason
425         selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
426
427         indexar= get_indexarray(me);
428
429         if(selected) {
430                 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
431                         if((mface->flag & ME_FACE_SEL)==0)
432                                 indexar[index]= 0;
433                         else
434                                 indexar[index]= index+1;
435                 }
436         }
437         else {
438                 for(index=0; index<me->totface; index++)
439                         indexar[index]= index+1;
440         }
441         
442         vgroup= ob->actdef-1;
443
444         /* if mirror painting, find the other group */          
445         if(me->editflag & ME_EDIT_MIRROR_X) {
446                 wpaint_mirror_vgroup_ensure(ob, &vgroup_mirror);
447         }
448         
449         copy_wpaint_prev(wp, me->dvert, me->totvert);
450         
451         for(index=0; index<me->totface; index++) {
452                 if(indexar[index] && indexar[index]<=me->totface) {
453                         mface= me->mface + (indexar[index]-1);
454                         /* just so we can loop through the verts */
455                         faceverts[0]= mface->v1;
456                         faceverts[1]= mface->v2;
457                         faceverts[2]= mface->v3;
458                         faceverts[3]= mface->v4;
459                         for (i=0; i<3 || faceverts[i]; i++) {
460                                 if(!((me->dvert+faceverts[i])->flag)) {
461                                         // Jason
462                                         if(selectedVerts && !((me->mvert+faceverts[i])->flag & SELECT)) {
463                                                 continue;
464                                         }
465
466                                         dw= defvert_verify_index(me->dvert+faceverts[i], vgroup);
467                                         if(dw) {
468                                                 uw= defvert_verify_index(wp->wpaint_prev+faceverts[i], vgroup);
469                                                 uw->weight= dw->weight; /* set the undo weight */
470                                                 dw->weight= paintweight;
471                                                 
472                                                 if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
473                                                         int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
474                                                         if(j>=0) {
475                                                                 /* copy, not paint again */
476                                                                 if(vgroup_mirror != -1) {
477                                                                         dw= defvert_verify_index(me->dvert+j, vgroup_mirror);
478                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup_mirror);
479                                                                 } else {
480                                                                         dw= defvert_verify_index(me->dvert+j, vgroup);
481                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup);
482                                                                 }
483                                                                 uw->weight= dw->weight; /* set the undo weight */
484                                                                 dw->weight= paintweight;
485                                                         }
486                                                 }
487                                         }
488                                         (me->dvert+faceverts[i])->flag= 1;
489                                 }
490                         }
491                 }
492         }
493         
494         index=0;
495         while (index<me->totvert) {
496                 (me->dvert+index)->flag= 0;
497                 index++;
498         }
499         
500         MEM_freeN(indexar);
501         copy_wpaint_prev(wp, NULL, 0);
502
503         DAG_id_tag_update(&me->id, 0);
504 }
505
506 /* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator
507  
508 void vpaint_dogamma(Scene *scene)
509 {
510         VPaint *vp= scene->toolsettings->vpaint;
511         Mesh *me;
512         Object *ob;
513         float igam, fac;
514         int a, temp;
515         unsigned char *cp, gamtab[256];
516
517         ob= OBACT;
518         me= get_mesh(ob);
519
520         if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
521         if(me==0 || me->mcol==0 || me->totface==0) return;
522
523         igam= 1.0/vp->gamma;
524         for(a=0; a<256; a++) {
525                 
526                 fac= ((float)a)/255.0;
527                 fac= vp->mul*pow( fac, igam);
528                 
529                 temp= 255.9*fac;
530                 
531                 if(temp<=0) gamtab[a]= 0;
532                 else if(temp>=255) gamtab[a]= 255;
533                 else gamtab[a]= temp;
534         }
535
536         a= 4*me->totface;
537         cp= (unsigned char *)me->mcol;
538         while(a--) {
539                 
540                 cp[1]= gamtab[ cp[1] ];
541                 cp[2]= gamtab[ cp[2] ];
542                 cp[3]= gamtab[ cp[3] ];
543                 
544                 cp+= 4;
545         }
546 }
547  */
548
549 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
550 {
551         char *cp1, *cp2, *cp;
552         int mfac;
553         unsigned int col=0;
554         
555         if(fac==0) return col1;
556         if(fac>=255) return col2;
557
558         mfac= 255-fac;
559         
560         cp1= (char *)&col1;
561         cp2= (char *)&col2;
562         cp=  (char *)&col;
563         
564         cp[0]= 255;
565         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
566         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
567         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
568         
569         return col;
570 }
571
572 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
573 {
574         char *cp1, *cp2, *cp;
575         int temp;
576         unsigned int col=0;
577         
578         if(fac==0) return col1;
579         
580         cp1= (char *)&col1;
581         cp2= (char *)&col2;
582         cp=  (char *)&col;
583         
584         cp[0]= 255;
585         temp= cp1[1] + ((fac*cp2[1])/255);
586         if(temp>254) cp[1]= 255; else cp[1]= temp;
587         temp= cp1[2] + ((fac*cp2[2])/255);
588         if(temp>254) cp[2]= 255; else cp[2]= temp;
589         temp= cp1[3] + ((fac*cp2[3])/255);
590         if(temp>254) cp[3]= 255; else cp[3]= temp;
591         
592         return col;
593 }
594
595 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
596 {
597         char *cp1, *cp2, *cp;
598         int temp;
599         unsigned int col=0;
600         
601         if(fac==0) return col1;
602         
603         cp1= (char *)&col1;
604         cp2= (char *)&col2;
605         cp=  (char *)&col;
606         
607         cp[0]= 255;
608         temp= cp1[1] - ((fac*cp2[1])/255);
609         if(temp<0) cp[1]= 0; else cp[1]= temp;
610         temp= cp1[2] - ((fac*cp2[2])/255);
611         if(temp<0) cp[2]= 0; else cp[2]= temp;
612         temp= cp1[3] - ((fac*cp2[3])/255);
613         if(temp<0) cp[3]= 0; else cp[3]= temp;
614         
615         return col;
616 }
617
618 static unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
619 {
620         char *cp1, *cp2, *cp;
621         int mfac;
622         unsigned int col=0;
623         
624         if(fac==0) return col1;
625
626         mfac= 255-fac;
627         
628         cp1= (char *)&col1;
629         cp2= (char *)&col2;
630         cp=  (char *)&col;
631         
632         /* first mul, then blend the fac */
633         cp[0]= 255;
634         cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255)  )/255;
635         cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255)  )/255;
636         cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255)  )/255;
637
638         
639         return col;
640 }
641
642 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
643 {
644         char *cp1, *cp2, *cp;
645         int mfac;
646         unsigned int col=0;
647         
648         if(fac==0) return col1;
649         if(fac>=255) return col2;
650
651         mfac= 255-fac;
652         
653         cp1= (char *)&col1;
654         cp2= (char *)&col2;
655         cp=  (char *)&col;
656         
657         /* See if are lighter, if so mix, else dont do anything.
658         if the paint col is darker then the original, then ignore */
659         if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
660                 return col1;
661         
662         cp[0]= 255;
663         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
664         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
665         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
666         
667         return col;
668 }
669
670 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
671 {
672         char *cp1, *cp2, *cp;
673         int mfac;
674         unsigned int col=0;
675         
676         if(fac==0) return col1;
677         if(fac>=255) return col2;
678
679         mfac= 255-fac;
680         
681         cp1= (char *)&col1;
682         cp2= (char *)&col2;
683         cp=  (char *)&col;
684         
685         /* See if were darker, if so mix, else dont do anything.
686         if the paint col is brighter then the original, then ignore */
687         if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
688                 return col1;
689         
690         cp[0]= 255;
691         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
692         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
693         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
694         return col;
695 }
696
697 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
698 {
699         Brush *brush = paint_brush(&vp->paint);
700
701         if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) *col= mcol_blend( *col, paintcol, alpha);
702         else if(brush->vertexpaint_tool==VP_ADD) *col= mcol_add( *col, paintcol, alpha);
703         else if(brush->vertexpaint_tool==VP_SUB) *col= mcol_sub( *col, paintcol, alpha);
704         else if(brush->vertexpaint_tool==VP_MUL) *col= mcol_mul( *col, paintcol, alpha);
705         else if(brush->vertexpaint_tool==VP_LIGHTEN) *col= mcol_lighten( *col, paintcol, alpha);
706         else if(brush->vertexpaint_tool==VP_DARKEN) *col= mcol_darken( *col, paintcol, alpha);
707         
708         /* if no spray, clip color adding with colorig & orig alpha */
709         if((vp->flag & VP_SPRAY)==0) {
710                 unsigned int testcol=0, a;
711                 char *cp, *ct, *co;
712                 
713                 alpha= (int)(255.0f*brush_alpha(brush));
714                 
715                 if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
716                 else if(brush->vertexpaint_tool==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
717                 else if(brush->vertexpaint_tool==VP_SUB) testcol= mcol_sub( *colorig, paintcol, alpha);
718                 else if(brush->vertexpaint_tool==VP_MUL) testcol= mcol_mul( *colorig, paintcol, alpha);
719                 else if(brush->vertexpaint_tool==VP_LIGHTEN)  testcol= mcol_lighten( *colorig, paintcol, alpha);
720                 else if(brush->vertexpaint_tool==VP_DARKEN)   testcol= mcol_darken( *colorig, paintcol, alpha);
721                 
722                 cp= (char *)col;
723                 ct= (char *)&testcol;
724                 co= (char *)colorig;
725                 
726                 for(a=0; a<4; a++) {
727                         if( ct[a]<co[a] ) {
728                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
729                                 else if( cp[a]>co[a] ) cp[a]= co[a];
730                         }
731                         else {
732                                 if( cp[a]<co[a] ) cp[a]= co[a];
733                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
734                         }
735                 }
736         }
737 }
738
739
740 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
741 {
742         struct ImBuf *ibuf;
743         int a, tot=0, index;
744         
745         /* brecht: disabled this because it obviously failes for
746            brushes with size > 64, why is this here? */
747         /*if(size>64.0) size= 64.0;*/
748         
749         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
750         if(ibuf) {
751                 unsigned int *rt= ibuf->rect;
752
753                 memset(indexar, 0, sizeof(int)*(totface+1));
754                 
755                 size= ibuf->x*ibuf->y;
756                 while(size--) {
757                                 
758                         if(*rt) {
759                                 index= WM_framebuffer_to_index(*rt);
760                                 if(index>0 && index<=totface)
761                                         indexar[index] = 1;
762                         }
763                 
764                         rt++;
765                 }
766                 
767                 for(a=1; a<=totface; a++) {
768                         if(indexar[a]) indexar[tot++]= a;
769                 }
770
771                 IMB_freeImBuf(ibuf);
772         }
773         
774         return tot;
775 }
776
777 static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, const float mval[2], float pressure)
778 {
779         Brush *brush = paint_brush(&vp->paint);
780         float fac, fac_2, size, dx, dy;
781         float alpha;
782         int vertco[2];
783         const int radius= brush_size(brush);
784
785         project_int_noclip(vc->ar, vert_nor, vertco);
786         dx= mval[0]-vertco[0];
787         dy= mval[1]-vertco[1];
788         
789         if (brush_use_size_pressure(brush))
790                 size = pressure * radius;
791         else
792                 size = radius;
793         
794         fac_2= dx*dx + dy*dy;
795         if(fac_2 > size*size) return 0.f;
796         fac = sqrtf(fac_2);
797         
798         alpha= brush_alpha(brush) * brush_curve_strength_clamp(brush, fac, size);
799         
800         if (brush_use_alpha_pressure(brush))
801                 alpha *= pressure;
802                 
803         if(vp->flag & VP_NORMALS) {
804                 float *no= vert_nor+3;
805                 
806                 /* transpose ! */
807                 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
808                 if(fac > 0.0f) {
809                         dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
810                         dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
811                         
812                         alpha*= fac/sqrtf(dx*dx + dy*dy + fac*fac);
813                 }
814                 else return 0.f;
815         }
816         
817         return alpha;
818 }
819
820 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip, int multipaint)
821 {
822         Brush *brush = paint_brush(&wp->paint);
823         int tool = brush->vertexpaint_tool;
824         
825         if(dw==NULL || uw==NULL) return;
826         
827         if (flip) {
828                 switch(tool) {
829                         case VP_MIX:
830                                 paintval = 1.f - paintval; break;
831                         case VP_ADD:
832                                 tool= VP_SUB; break;
833                         case VP_SUB:
834                                 tool= VP_ADD; break;
835                         case VP_LIGHTEN:
836                                 tool= VP_DARKEN; break;
837                         case VP_DARKEN:
838                                 tool= VP_LIGHTEN; break;
839                 }
840         }
841         
842         if(tool==VP_MIX || tool==VP_BLUR)
843                 dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
844         else if(tool==VP_ADD)
845                 dw->weight += paintval*alpha;
846         else if(tool==VP_SUB) 
847                 dw->weight -= paintval*alpha;
848         else if(tool==VP_MUL) 
849                 /* first mul, then blend the fac */
850                 dw->weight = ((1.0f-alpha) + alpha*paintval)*dw->weight;
851         else if(tool==VP_LIGHTEN) {
852                 if (dw->weight < paintval)
853                         dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
854         } else if(tool==VP_DARKEN) {
855                 if (dw->weight > paintval)
856                         dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
857         }
858         // Jason delay clamping until the end so multi-paint can function when the active group is at the limits
859         //CLAMP(dw->weight, 0.0f, 1.0f);
860         
861         /* if no spray, clip result with orig weight & orig alpha */
862         if((wp->flag & VP_SPRAY)==0) {
863                 float testw=0.0f;
864                 
865                 alpha= brush_alpha(brush);
866                 if(tool==VP_MIX || tool==VP_BLUR)
867                         testw = paintval*alpha + uw->weight*(1.0f-alpha);
868                 else if(tool==VP_ADD)
869                         testw = uw->weight + paintval*alpha;
870                 else if(tool==VP_SUB) 
871                         testw = uw->weight - paintval*alpha;
872                 else if(tool==VP_MUL) 
873                         /* first mul, then blend the fac */
874                         testw = ((1.0f-alpha) + alpha*paintval)*uw->weight;
875                 else if(tool==VP_LIGHTEN) {
876                         if (uw->weight < paintval)
877                                 testw = paintval*alpha + uw->weight*(1.0f-alpha);
878                         else
879                                 testw = uw->weight;
880                 } else if(tool==VP_DARKEN) {
881                         if (uw->weight > paintval)
882                                 testw = paintval*alpha + uw->weight*(1.0f-alpha);
883                         else
884                                 testw = uw->weight;
885                 }
886                 //CLAMP(testw, 0.0f, 1.0f);
887                 if(!multipaint) {
888                         if( testw<uw->weight ) {
889                                 if(dw->weight < testw) dw->weight= testw;
890                                 else if(dw->weight > uw->weight) dw->weight= uw->weight;
891                         }
892                         else {
893                                 if(dw->weight > testw) dw->weight= testw;
894                                 else if(dw->weight < uw->weight) dw->weight= uw->weight;
895                         }
896                 }
897         }
898         
899 }
900
901 /* ----------------------------------------------------- */
902
903
904 /* sets wp->weight to the closest weight value to vertex */
905 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
906 static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
907 {
908         ViewContext vc;
909         Mesh *me;
910         short change= FALSE;
911
912         view3d_set_viewcontext(C, &vc);
913         me= get_mesh(vc.obact);
914
915         if (me && me->dvert && vc.v3d && vc.rv3d) {
916                 int index;
917
918                 view3d_operator_needs_opengl(C);
919
920                 index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
921
922                 if(index && index<=me->totface) {
923                         DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
924
925                         if(dm->getVertCo==NULL) {
926                                 BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
927                         }
928                         else {
929                                 MFace *mf= ((MFace *)me->mface) + index-1;
930                                 const int vgroup= vc.obact->actdef - 1;
931                                 ToolSettings *ts= vc.scene->toolsettings;
932                                 float mval_f[2];
933                                 int v_idx_best= -1;
934                                 int fidx;
935                                 float len_best= FLT_MAX;
936
937                                 mval_f[0]= (float)event->mval[0];
938                                 mval_f[1]= (float)event->mval[1];
939
940                                 fidx= mf->v4 ? 3:2;
941                                 do {
942                                         float co[3], sco[3], len;
943                                         const int v_idx= (*(&mf->v1 + fidx));
944                                         dm->getVertCo(dm, v_idx, co);
945                                         project_float_noclip(vc.ar, co, sco);
946                                         len= len_squared_v2v2(mval_f, sco);
947                                         if(len < len_best) {
948                                                 len_best= len;
949                                                 v_idx_best= v_idx;
950                                         }
951                                 } while (fidx--);
952
953                                 if(v_idx_best != -1) { /* should always be valid */
954                                         ts->vgroup_weight= defvert_find_weight(&me->dvert[v_idx_best], vgroup);
955                                         change= TRUE;
956                                 }
957                         }
958                         dm->release(dm);
959                 }
960         }
961
962         if(change) {
963                 /* not really correct since the brush didnt change, but redraws the toolbar */
964                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, NULL); /* ts->wpaint->paint.brush */
965
966                 return OPERATOR_FINISHED;
967         }
968         else {
969                 return OPERATOR_CANCELLED;
970         }
971 }
972
973 void PAINT_OT_weight_sample(wmOperatorType *ot)
974 {
975         /* identifiers */
976         ot->name= "Weight Paint Sample Weight";
977         ot->idname= "PAINT_OT_weight_sample";
978
979         /* api callbacks */
980         ot->invoke= weight_sample_invoke;
981         ot->poll= weight_paint_mode_poll;
982
983         /* flags */
984         ot->flag= OPTYPE_UNDO;
985 }
986
987 /* samples cursor location, and gives menu with vertex groups to activate */
988 static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
989 {
990         if (C) {
991                 wmWindow *win= CTX_wm_window(C);
992                 if(win && win->eventstate) {
993                         ViewContext vc;
994                         Mesh *me;
995
996                         view3d_set_viewcontext(C, &vc);
997                         me= get_mesh(vc.obact);
998
999                         if (me && me->dvert && vc.v3d && vc.rv3d) {
1000                                 int index;
1001
1002                                 view3d_operator_needs_opengl(C);
1003
1004                                 index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
1005
1006                                 if(index && index<=me->totface) {
1007                                         const int totgroup= BLI_countlist(&vc.obact->defbase);
1008                                         if(totgroup) {
1009                                                 MFace *mf= ((MFace *)me->mface) + index-1;
1010                                                 int fidx= mf->v4 ? 3:2;
1011                                                 int *groups= MEM_callocN(totgroup*sizeof(int), "groups");
1012                                                 int found= FALSE;
1013
1014                                                 do {
1015                                                         MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
1016                                                         int i= dvert->totweight;
1017                                                         MDeformWeight *dw;
1018                                                         for(dw= dvert->dw; i > 0; dw++, i--) {
1019                                                                 groups[dw->def_nr]= TRUE;
1020                                                                 found= TRUE;
1021                                                         }
1022                                                 } while (fidx--);
1023
1024                                                 if(found==FALSE) {
1025                                                         MEM_freeN(groups);
1026                                                 }
1027                                                 else {
1028                                                         EnumPropertyItem *item= NULL, item_tmp= {0};
1029                                                         int totitem= 0;
1030                                                         int i= 0;
1031                                                         bDeformGroup *dg;
1032                                                         for(dg= vc.obact->defbase.first; dg && i<totgroup; i++, dg= dg->next) {
1033                                                                 if(groups[i]) {
1034                                                                         item_tmp.identifier= item_tmp.name= dg->name;
1035                                                                         item_tmp.value= i;
1036                                                                         RNA_enum_item_add(&item, &totitem, &item_tmp);
1037                                                                 }
1038                                                         }
1039
1040                                                         RNA_enum_item_end(&item, &totitem);
1041                                                         *free= 1;
1042
1043                                                         MEM_freeN(groups);
1044                                                         return item;
1045                                                 }
1046                                         }
1047                                 }
1048                         }
1049                 }
1050         }
1051
1052         return DummyRNA_NULL_items;
1053 }
1054
1055 static int weight_sample_group_exec(bContext *C, wmOperator *op)
1056 {
1057         int type= RNA_enum_get(op->ptr, "group");
1058         ViewContext vc;
1059         view3d_set_viewcontext(C, &vc);
1060
1061         vc.obact->actdef= type + 1;
1062
1063         DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
1064         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, vc.obact);
1065         return OPERATOR_FINISHED;
1066 }
1067
1068 /* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
1069 void PAINT_OT_weight_sample_group(wmOperatorType *ot)
1070 {
1071         PropertyRNA *prop= NULL;
1072
1073         /* identifiers */
1074         ot->name= "Weight Paint Sample Group";
1075         ot->idname= "PAINT_OT_weight_sample_group";
1076
1077         /* api callbacks */
1078         ot->exec= weight_sample_group_exec;
1079         ot->invoke= WM_menu_invoke;
1080         ot->poll= weight_paint_mode_poll;
1081
1082         /* flags */
1083         ot->flag= OPTYPE_UNDO;
1084
1085         /* keyingset to use (dynamic enum) */
1086         prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
1087         RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
1088         ot->prop= prop;
1089 }
1090
1091
1092 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
1093                                            int paint_nr, char *map)
1094 {
1095 //      MDeformWeight *dw = dvert->dw;
1096         float sum=0.0f, fac=0.0f, paintw=0.0f;
1097         int i, tot=0;
1098
1099         if (!map)
1100                 return;
1101
1102         for (i=0; i<dvert->totweight; i++) {
1103                 if (dvert->dw[i].def_nr == paint_nr)
1104                         paintw = dvert->dw[i].weight;
1105
1106                 if (map[dvert->dw[i].def_nr]) {
1107                         tot += 1;
1108                         if (dvert->dw[i].def_nr != paint_nr)
1109                                 sum += dvert->dw[i].weight;
1110                 }
1111         }
1112         
1113         if (!tot || sum <= (1.0f - paintw))
1114                 return;
1115
1116         fac = sum / (1.0f - paintw);
1117         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1118
1119         for (i=0; i<dvert->totweight; i++) {
1120                 if (map[dvert->dw[i].def_nr]) {
1121                         if (dvert->dw[i].def_nr != paint_nr)
1122                                 dvert->dw[i].weight *= fac;
1123                 }
1124         }
1125 }
1126 // Jason was here: the active group should be involved in auto normalize
1127 static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *map)
1128 {
1129 //      MDeformWeight *dw = dvert->dw;
1130         float sum=0.0f, fac=0.0f;
1131         int i, tot=0;
1132
1133         if (!map)
1134                 return;
1135
1136         for (i=0; i<dvert->totweight; i++) {
1137                 if (map[dvert->dw[i].def_nr]) {
1138                         tot += 1;
1139                         sum += dvert->dw[i].weight;
1140                 }
1141         }
1142         
1143         if (!tot || sum == 1.0f)
1144                 return;
1145
1146         fac = sum;
1147         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1148
1149         for (i=0; i<dvert->totweight; i++) {
1150                 if (map[dvert->dw[i].def_nr]) {
1151                         dvert->dw[i].weight *= fac;
1152                 }
1153         }
1154 }
1155 /* Jason was here */
1156 /*
1157 See if the current deform vertex has a locked group
1158 */
1159 static char has_locked_group(MDeformVert *dvert, char *flags)
1160 {
1161         int i;
1162         for(i = 0; i < dvert->totweight; i++) {
1163                 if(flags[(dvert->dw+i)->def_nr] && (dvert->dw+i)->weight > 0.0f) {
1164                         return TRUE;
1165                 }
1166         }
1167         return FALSE;
1168 }
1169 /*Jason was here
1170 gen_lck_flags gets the status of "flag" for each bDeformGroup
1171 in ob->defbase and returns an array containing them
1172 */
1173 static char* gen_lck_flags(Object* ob, int defcnt, char *map)
1174 {
1175         char is_locked = FALSE;
1176         int i;
1177         //int defcnt = BLI_countlist(&ob->defbase);
1178         char *flags = MEM_mallocN(defcnt*sizeof(char), "defflags");
1179         bDeformGroup *defgroup;
1180         int selected = 0;
1181
1182         for(i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
1183                 flags[i] = defgroup->flag;
1184                 if(flags[i]) {
1185                         is_locked = TRUE;
1186                 }
1187         }
1188         if(is_locked){
1189                 return flags;
1190         }
1191         // don't forget to free it if it is unneeded
1192         MEM_freeN(flags);
1193         return NULL;
1194 }
1195 /* Jason was here */
1196 static int has_locked_group_selected(int defcnt, char *selection, char *flags) {
1197         int i;
1198         for(i = 0; i < defcnt; i++) {
1199                 if(selection[i] && flags[i]) {
1200                         return TRUE;
1201                 }
1202         }
1203         return FALSE;
1204 }
1205
1206 /* Jason was here */
1207 static int has_unselected_unlocked_bone_group(int defcnt, char *selection, int selected, char *flags, char *bone_groups) {
1208         int i;
1209         if(defcnt == selected) {
1210                 return FALSE;
1211         }
1212         for(i = 0; i < defcnt; i++) {
1213                 if(bone_groups[i] && !selection[i] && !flags[i]) {
1214                         return TRUE;
1215                 }
1216         }
1217         return FALSE;
1218 }
1219
1220 /*Jason*/
1221 static void multipaint_selection(MDeformVert *dvert, float change, char *selection, int defcnt) {
1222         int i;
1223         MDeformWeight *dw;
1224         float val;
1225         // make sure they are all at most 1 after the change
1226         for(i = 0; i < defcnt; i++) {
1227                 if(selection[i]) {
1228                         dw = defvert_find_index(dvert, i);
1229                         if(dw && dw->weight) {
1230                                 val = dw->weight * change;
1231                                 if(val > 1) {
1232                                         // Jason TODO: when the change is reduced, you need to recheck the earlier values to make sure they are not 0 (precision error)
1233                                         change = 1.0f/dw->weight;
1234                                 }
1235                                 // the value should never reach zero while multi-painting if it was nonzero beforehand
1236                                 if(val <= 0) {
1237                                         return;
1238                                 }
1239                         }
1240                 }
1241         }
1242         // apply the valid change
1243         for(i = 0; i < defcnt; i++) {
1244                 if(selection[i]) {
1245                         dw = defvert_find_index(dvert, i);
1246                         if(dw && dw->weight) {
1247                                 dw->weight = dw->weight * change;
1248                         }
1249                 }
1250         }
1251 }
1252 /*Jason*/
1253 // move all change onto valid, unchanged groups.  If there is change left over, then return it.
1254 // assumes there are valid groups to shift weight onto
1255 static float redistribute_change(MDeformVert *ndv, char *change_status, int changeme, int changeto, char *validmap, float totchange, float total_valid) {
1256         float was_change;
1257         float change;
1258         float oldval;
1259         MDeformWeight *ndw;
1260         int i;
1261         do {
1262                 // assume there is no change until you see one
1263                 was_change = FALSE;
1264                 // change each group by the same amount each time
1265                 change = totchange/total_valid;
1266                 for(i = 0; i < ndv->totweight && total_valid && totchange; i++) {
1267                         ndw = (ndv->dw+i);
1268                         // change only the groups with a valid status
1269                         if(change_status[ndw->def_nr] == changeme) {
1270                                 oldval = ndw->weight;
1271                                 // if auto normalize is active, don't worry about upper bounds
1272                                 if(!validmap && ndw->weight + change > 1) {
1273                                         totchange -= 1-ndw->weight;
1274                                         ndw->weight = 1;
1275                                         // stop the changes to this group
1276                                         change_status[ndw->def_nr] = changeto;
1277                                         total_valid--;
1278                                 } else if(ndw->weight + change < 0) { // check the lower bound
1279                                         totchange -= ndw->weight;
1280                                         ndw->weight = 0;
1281                                         change_status[ndw->def_nr] = changeto;
1282                                         total_valid--;
1283                                 } else {// a perfectly valid change occurred to ndw->weight
1284                                         totchange -= change;
1285                                         ndw->weight += change;
1286                                 }
1287                                 // see if there was a change
1288                                 if(oldval != ndw->weight) {
1289                                         was_change = TRUE;
1290                                 }
1291                         }
1292                 }
1293         // don't go again if there was no change, if there is no valid group, or there is no change left
1294         }while(was_change && total_valid && totchange);
1295         // left overs
1296         return totchange;
1297 }
1298 /*Jason*/
1299 // observe the changes made to the weights of groups.
1300 // make sure all locked groups on the vertex have the same deformation
1301 // by moving the changes made to groups onto other unlocked groups
1302 static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *flags, char *bone_groups, char *validmap) {
1303         float totchange = 0.0f;
1304         float totchange_allowed = 0.0f;
1305         float left_over;
1306
1307         int total_valid = 0;
1308         int total_changed = 0;
1309         int i;
1310         MDeformWeight *ndw;
1311         MDeformWeight *odw;
1312         MDeformWeight *ndw2;
1313         MDeformWeight *odw2;
1314         int designatedw = -1;
1315         int designatedw_changed = FALSE;
1316         float storedw;
1317         char *change_status;
1318         char new_weight_has_zero = FALSE;
1319
1320         if(!flags || !has_locked_group(ndv, flags)) {
1321                 return;
1322         }
1323         // record if a group was changed, unlocked and not changed, or locked
1324         change_status = MEM_callocN(sizeof(char)*defcnt, "unlocked_unchanged");
1325
1326         for(i = 0; i < defcnt; i++) {
1327                 ndw = defvert_find_index(ndv, i);
1328                 odw = defvert_find_index(odv, i);
1329                 // the weights are zero, so we can assume a lot
1330                 if(!ndw || !odw) {
1331                         if (!flags[i] && bone_groups[i]){
1332                                 defvert_verify_index(odv, i);
1333                                 defvert_verify_index(ndv, i);
1334                                 total_valid++;
1335                                 change_status[i] = 1; // can be altered while redistributing
1336                         }
1337                         continue;
1338                 }
1339                 // locked groups should not be changed
1340                 if(flags[i]) {
1341                         ndw->weight = odw->weight;
1342                 } else if(ndw->weight != odw->weight) { // changed groups are handled here
1343                         totchange += ndw->weight-odw->weight;
1344                         change_status[i] = 2; // was altered already
1345                         total_changed++;
1346                         if(ndw->weight == 0) {
1347                                 new_weight_has_zero = TRUE;
1348                         } else if(designatedw == -1){
1349                                 designatedw = i;
1350                         }
1351                 } // unchanged, unlocked bone groups are handled here
1352                 else if (bone_groups[i]){
1353                         totchange_allowed += ndw->weight;
1354                         total_valid++;
1355                         change_status[i] = 1; // can be altered while redistributing
1356                 }
1357         }
1358         // if there was any change, redistribute it
1359         if(total_changed) {
1360                 // auto normalize will allow weights to temporarily go above 1 in redistribution
1361                 if(validmap && total_changed < 0 && total_valid) {
1362                         totchange_allowed = total_valid;
1363                 }
1364                 // there needs to be change allowed, or you should not bother
1365                 if(totchange_allowed) {
1366                         // the way you modify the unlocked+unchanged groups is different depending
1367                         // on whether or not you are painting the weight(s) up or down
1368                         if(totchange < 0) {
1369                                 totchange_allowed = total_valid - totchange_allowed;
1370                         } else {
1371                                 totchange_allowed *= -1;
1372                         }
1373                         left_over = 0;
1374                         if(fabs(totchange_allowed) < fabs(totchange)) {
1375                                 // this amount goes back onto the changed, unlocked weights
1376                                 left_over = fabs(fabs(totchange)-fabs(totchange_allowed));
1377                                 if(totchange > 0) {
1378                                         left_over *= -1;
1379                                 }
1380                         }else {
1381                                 // all of the change will be permitted
1382                                 totchange_allowed = -totchange;
1383                         }
1384                         // move the weight evenly between the allowed groups, move excess back onto the used groups based on the change
1385                         totchange_allowed = redistribute_change(ndv, change_status, 1, -1, validmap, totchange_allowed, total_valid);
1386                         left_over += totchange_allowed;
1387                         if(left_over) {
1388                                 // more than one nonzero weights were changed with the same ratio, so keep them changed that way!
1389                                 if(total_changed > 1 && !new_weight_has_zero && designatedw >= 0) {
1390                                         // this dw is special, it is used as a base to determine how to change the others
1391                                         ndw = defvert_find_index(ndv, designatedw);
1392                                         odw = defvert_find_index(odv, designatedw);
1393                                         storedw = ndw->weight;
1394                                         for(i = 0; i < ndv->totweight; i++) {
1395                                                 if(change_status[ndw->def_nr] == 2) {
1396                                                         odw2 = (odv->dw+i);
1397                                                         ndw2 = (ndv->dw+i);
1398                                                         if(!designatedw_changed) {
1399                                                                 ndw->weight = (totchange_allowed + odw->weight + odw2->weight)/(1 + ndw2->weight/ndw->weight);
1400                                                                 designatedw_changed = TRUE;
1401                                                         }
1402                                                         ndw2->weight = ndw->weight*ndw2->weight/storedw;
1403                                                 }
1404                                         }
1405                                 }
1406                                 // a weight was changed to zero, only one weight was changed, or designatedw is still -1
1407                                 // put weight back as evenly as possible
1408                                 else {
1409                                         redistribute_change(ndv, change_status, 2, -2, validmap, left_over, total_changed);
1410                                 }
1411                         }
1412                 } else {
1413                         // reset the weights
1414                         for(i = 0; i < ndv->totweight; i++) {
1415                                 (ndv->dw+i)->weight = (odv->dw+i)->weight;
1416                         }
1417                 }
1418         }
1419
1420         MEM_freeN(change_status);
1421 }
1422 /*Jason*/
1423 // multi-paint's initial, potential change is computed here based on the user's stroke
1424 static float get_mp_change(MDeformVert *odv, char *selection, float brush_change) {
1425         float selwsum = 0.0f;
1426         int i;
1427         MDeformWeight *dw;
1428         for(i=0; i < odv->totweight; i++) {
1429                 if(selection[(dw = (odv->dw+i))->def_nr]) {
1430                         selwsum += dw->weight;
1431                 }
1432         }
1433         if(selwsum && selwsum+brush_change > 0) {
1434                 return (selwsum+brush_change)/selwsum;
1435         }
1436         return 0.0f;
1437 }
1438 /*Jason*/
1439 // change the weights back to the wv's weights
1440 // it assumes you already have the correct pointer index
1441 static void reset_to_prev(MDeformVert *wv, MDeformVert *dv) {
1442         int i;
1443         MDeformWeight *d;
1444         MDeformWeight *w;
1445         for(i = 0; i < dv->totweight; i++) {
1446                 d = dv->dw+i;
1447                 w = defvert_find_index(wv, d->def_nr);
1448                 // if there was no w when there is a d, then the old weight was 0
1449                 if(w) {
1450                         d->weight = w->weight;
1451                 } else {
1452                         d->weight = 0;
1453                 }
1454         }
1455 }
1456 /* Jason */
1457 static void clamp_weights(MDeformVert *dvert) {
1458         int i;
1459         for (i = 0; i < dvert->totweight; i++) {
1460                 CLAMP((dvert->dw+i)->weight, 0.0f, 1.0f);
1461         }
1462 }
1463 /*Jason*/
1464 /* fresh start to make multi-paint and locking modular */
1465 static void apply_mp_lcks_normalize(Object *ob, Mesh *me, int index, MDeformWeight *dw, int defcnt, float change, float oldw, float neww, char *selection, int selected, char *bone_groups, char *validmap, char *flags, int multipaint) {
1466         MDeformVert *dvert = me->dvert+index;
1467         MDeformVert *dv = MEM_mallocN(sizeof (*(me->dvert+index)), "oldMDeformVert");
1468
1469         dv->dw= MEM_dupallocN(dvert->dw);
1470         dv->flag = dvert->flag;
1471         dv->totweight = dvert->totweight;
1472         // do not multi-paint if a locked group is selected or the active group is locked
1473         // !flags[dw->def_nr] helps if nothing is selected, but active group is locked
1474         if(!flags || flags && !has_locked_group_selected(defcnt, selection, flags) && !flags[dw->def_nr]) {
1475                 if(multipaint && selected > 1) {
1476                         if(change && change!=1) {
1477                                 multipaint_selection(dvert, change, selection, defcnt);
1478                         }
1479                 } else {// this lets users paint normally, but don't let them paint locked groups
1480                         dw->weight = neww;
1481                 }
1482         }
1483         clamp_weights(me->dvert+index);
1484
1485         enforce_locks(dv, dvert, defcnt, flags, bone_groups, validmap);
1486
1487         do_weight_paint_auto_normalize_all_groups(dvert, validmap);
1488         
1489         MEM_freeN(dv->dw);
1490         MEM_freeN(dv);
1491 }
1492
1493 /* Jason was here duplicate function I used in DerivedMesh.c*/
1494 static char* get_selected_defgroups(Object *ob, int defcnt) {
1495         bPoseChannel *chan;
1496         bPose *pose;
1497         bDeformGroup *defgroup;
1498         //Bone *bone;
1499         char *dg_flags = MEM_callocN(defcnt*sizeof(char), "dg_selected_flags");
1500         int i;
1501         Object *armob = ED_object_pose_armature(ob);
1502
1503         if(armob) {
1504                 pose = armob->pose;
1505                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1506                         for (i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
1507                                 if(!strcmp(defgroup->name, chan->bone->name)) {
1508                                         dg_flags[i] = (chan->bone->flag & BONE_SELECTED);
1509                                 }
1510                         }
1511                 }
1512         }
1513         // the array has whether or not the corresponding group is selected
1514         return dg_flags;
1515 }
1516 /* TODO move duplicates to header */
1517 /* Jason was here duplicate function */
1518 static int count_true(char *list, int len)
1519 {
1520         int i;
1521         int cnt = 0;
1522         for(i = 0; i < len; i++) {
1523                 if (list[i]) {
1524                         cnt++;
1525                 }
1526         }
1527         return cnt;
1528 }
1529 // within the current dvert index, get the dw that is selected and has a weight above 0
1530 // this helps multi-paint
1531 static int get_first_selected_nonzero_weight(MDeformVert *dvert, char *selection) {
1532         int i;
1533         MDeformWeight *dw;
1534         for(i=0; i< dvert->totweight; i++) {
1535                 dw = dvert->dw+i;
1536                 if(selection[dw->def_nr] && dw->weight > 0) {
1537                         return i;
1538                 }
1539         }
1540         return -1;
1541 }
1542 // Jason
1543 static char *wpaint_make_validmap(Object *ob);
1544
1545 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
1546                                    float alpha, float paintweight, int flip, 
1547                                    int vgroup_mirror, char *validmap, int multipaint)
1548 {
1549         Mesh *me= ob->data;
1550         MDeformWeight *dw, *uw, *tdw, *tuw;
1551         int vgroup= ob->actdef-1;
1552
1553         /* Jason was here */
1554         char *flags;
1555         char *bone_groups;
1556         char *selection;
1557         int selected;
1558         float oldw;
1559         float neww;
1560         float testw;
1561         int defcnt;
1562         float change = 0;
1563         int i;
1564         // Need to know which groups are bone groups
1565         if(validmap) {
1566                 bone_groups = validmap;
1567         }else {
1568                 bone_groups = wpaint_make_validmap(ob);
1569         }
1570
1571         if(wp->flag & VP_ONLYVGROUP) {
1572                 dw= defvert_find_index(me->dvert+index, vgroup);
1573                 uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
1574         }
1575         else {
1576                 dw= defvert_verify_index(me->dvert+index, vgroup);
1577                 uw= defvert_verify_index(wp->wpaint_prev+index, vgroup);
1578         }
1579         if(dw==NULL || uw==NULL)
1580                 return;
1581         /* Jason was here */
1582         flags = gen_lck_flags(ob, defcnt = BLI_countlist(&ob->defbase), bone_groups);
1583         selection = get_selected_defgroups(ob, defcnt);
1584         selected = count_true(selection, defcnt);
1585         if(!selected && ob->actdef) {
1586                 selected = 1;
1587         }
1588         
1589         oldw = dw->weight;
1590         wpaint_blend(wp, dw, uw, alpha, paintweight, flip, multipaint && selected >1);
1591         neww = dw->weight;
1592         dw->weight = oldw;
1593         // setup multi-paint
1594         if(selected > 1 && multipaint) {
1595                 tdw = dw;
1596                 tuw = uw;
1597                 change = get_mp_change(wp->wpaint_prev+index, selection, neww-oldw);
1598                 if(change) {
1599                         if(!tdw->weight) {
1600                                 i = get_first_selected_nonzero_weight(me->dvert+index, selection);
1601                                 if(i>=0) {
1602                                         tdw = ((me->dvert+index)->dw+i);
1603                                         tuw = defvert_verify_index(wp->wpaint_prev+index, tdw->def_nr);
1604                                 } else {
1605                                         change = 0;
1606                                 }
1607                         }
1608                         if(change && tdw->weight * change && tuw->weight) {
1609                                 if(tdw->weight != tuw->weight) {
1610                                         testw = tuw->weight*change;
1611                                         if( testw > tuw->weight ) {
1612                                                 if(change > tdw->weight/tuw->weight) {
1613                                                         reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
1614                                                 } else {
1615                                                         change = 0;
1616                                                 }
1617                                         } else {
1618                                                 if(change < tdw->weight/tuw->weight) {
1619                                                         reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
1620                                                 } else {
1621                                                         change = 0;
1622                                                 }
1623                                         }
1624                                 }
1625                         } else {
1626                                 change = 0;
1627                         }
1628                 }
1629         }
1630         /* Jason was here */
1631         apply_mp_lcks_normalize(ob, me, index, dw, defcnt, change, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
1632
1633         // dvert may have been altered greatly
1634         dw = defvert_find_index(me->dvert+index, vgroup);
1635
1636         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
1637                 int j= mesh_get_x_mirror_vert(ob, index);
1638                 if(j>=0) {
1639                         /* copy, not paint again */
1640                         if(vgroup_mirror != -1)
1641                                 uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
1642                         else
1643                                 uw= defvert_verify_index(me->dvert+j, vgroup);
1644                         /* Jason */
1645                         //uw->weight= dw->weight;
1646                         /* Jason */
1647                         apply_mp_lcks_normalize(ob, me, j, uw, defcnt, change, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
1648                 }
1649         }
1650         /* Jason */
1651         if(flags) {
1652                 MEM_freeN(flags);
1653         }
1654         MEM_freeN(selection);
1655         if(!validmap) {
1656                 MEM_freeN(bone_groups);
1657         }
1658 }
1659
1660 /* *************** set wpaint operator ****************** */
1661
1662 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))              /* toggle */
1663 {               
1664         Object *ob= CTX_data_active_object(C);
1665         Scene *scene= CTX_data_scene(C);
1666         VPaint *wp= scene->toolsettings->wpaint;
1667         Mesh *me;
1668         
1669         me= get_mesh(ob);
1670         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1671         
1672         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1673         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1674         
1675         
1676         /* Weightpaint works by overriding colors in mesh,
1677                 * so need to make sure we recalc on enter and
1678                 * exit (exit needs doing regardless because we
1679                                 * should redeform).
1680                 */
1681         DAG_id_tag_update(&me->id, 0);
1682         
1683         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1684                 Object *par;
1685                 
1686                 if(wp==NULL)
1687                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1688
1689                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1690                 paint_cursor_start(C, weight_paint_poll);
1691                 
1692                 mesh_octree_table(ob, NULL, NULL, 's');
1693                 
1694                 /* verify if active weight group is also active bone */
1695                 par= modifiers_isDeformedByArmature(ob);
1696                 if(par && (par->mode & OB_MODE_POSE)) {
1697                         bArmature *arm= par->data;
1698
1699                         if(arm->act_bone)
1700                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1701                 }
1702         }
1703         else {
1704                 mesh_octree_table(NULL, NULL, NULL, 'e');
1705                 mesh_mirrtopo_table(NULL, 'e');
1706         }
1707         
1708         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1709         
1710         return OPERATOR_FINISHED;
1711 }
1712
1713 /* for switching to/from mode */
1714 static int paint_poll_test(bContext *C)
1715 {
1716         if(CTX_data_edit_object(C))
1717                 return 0;
1718         if(CTX_data_active_object(C)==NULL)
1719                 return 0;
1720         return 1;
1721 }
1722
1723 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1724 {
1725         
1726         /* identifiers */
1727         ot->name= "Weight Paint Mode";
1728         ot->idname= "PAINT_OT_weight_paint_toggle";
1729         
1730         /* api callbacks */
1731         ot->exec= set_wpaint;
1732         ot->poll= paint_poll_test;
1733         
1734         /* flags */
1735         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1736         
1737 }
1738
1739 /* ************ weight paint operator ********** */
1740
1741 struct WPaintData {
1742         ViewContext vc;
1743         int *indexar;
1744         int vgroup_mirror;
1745         float *vertexcosnos;
1746         float wpimat[3][3];
1747         
1748         /*variables for auto normalize*/
1749         int auto_normalize;
1750         char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1751 };
1752
1753 static char *wpaint_make_validmap(Object *ob)
1754 {
1755         bDeformGroup *dg;
1756         ModifierData *md;
1757         char *validmap;
1758         bPose *pose;
1759         bPoseChannel *chan;
1760         ArmatureModifierData *amd;
1761         GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1762         int i = 0, step1=1;
1763
1764         /*add all names to a hash table*/
1765         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1766                 BLI_ghash_insert(gh, dg->name, NULL);
1767         }
1768
1769         if (!i)
1770                 return NULL;
1771
1772         validmap = MEM_callocN(i, "wpaint valid map");
1773
1774         /*now loop through the armature modifiers and identify deform bones*/
1775         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1776                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1777                         continue;
1778
1779                 if (md->type == eModifierType_Armature) 
1780                 {
1781                         amd = (ArmatureModifierData*) md;
1782
1783                         if(amd->object && amd->object->pose) {
1784                                 pose = amd->object->pose;
1785                                 
1786                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1787                                         if (chan->bone->flag & BONE_NO_DEFORM)
1788                                                 continue;
1789
1790                                         if (BLI_ghash_haskey(gh, chan->name)) {
1791                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1792                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1793                                         }
1794                                 }
1795                         }
1796                 }
1797         }
1798         
1799         /*add all names to a hash table*/
1800         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1801                 if (BLI_ghash_lookup(gh, dg->name) != NULL) {
1802                         validmap[i] = 1;
1803                 }
1804         }
1805
1806         BLI_ghash_free(gh, NULL, NULL);
1807
1808         return validmap;
1809 }
1810
1811 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1812 {
1813         Scene *scene= CTX_data_scene(C);
1814         struct PaintStroke *stroke = op->customdata;
1815         ToolSettings *ts= CTX_data_tool_settings(C);
1816         VPaint *wp= ts->wpaint;
1817         Object *ob= CTX_data_active_object(C);
1818         struct WPaintData *wpd;
1819         Mesh *me;
1820         float mat[4][4], imat[4][4];
1821
1822         if(scene->obedit) return OPERATOR_CANCELLED;
1823         
1824         me= get_mesh(ob);
1825         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1826         
1827         /* if nothing was added yet, we make dverts and a vertex deform group */
1828         if (!me->dvert) {
1829                 ED_vgroup_data_create(&me->id);
1830                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1831         }
1832
1833
1834         /* make mode data storage */
1835         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1836         paint_stroke_set_mode_data(stroke, wpd);
1837         view3d_set_viewcontext(C, &wpd->vc);
1838         wpd->vgroup_mirror= -1;
1839
1840         /*set up auto-normalize, and generate map for detecting which
1841           vgroups affect deform bones*/
1842         wpd->auto_normalize = ts->auto_normalize;
1843         if (wpd->auto_normalize)
1844                 wpd->vgroup_validmap = wpaint_make_validmap(ob);
1845         
1846         //      if(qual & LR_CTRLKEY) {
1847         //              sample_wpaint(scene, ar, v3d, 0);
1848         //              return;
1849         //      }
1850         //      if(qual & LR_SHIFTKEY) {
1851         //              sample_wpaint(scene, ar, v3d, 1);
1852         //              return;
1853         //      }
1854         
1855         /* ALLOCATIONS! no return after this line */
1856         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1857         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1858         wpd->indexar= get_indexarray(me);
1859         copy_wpaint_prev(wp, me->dvert, me->totvert);
1860         
1861         /* this happens on a Bone select, when no vgroup existed yet */
1862         if(ob->actdef<=0) {
1863                 Object *modob;
1864                 if((modob = modifiers_isDeformedByArmature(ob))) {
1865                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
1866                         if(actbone) {
1867                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
1868
1869                                 if(pchan) {
1870                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
1871                                         if(dg==NULL)
1872                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
1873                                         else
1874                                                 ob->actdef= 1 + defgroup_find_index(ob, dg);
1875                                 }
1876                         }
1877                 }
1878         }
1879         if(ob->defbase.first==NULL) {
1880                 ED_vgroup_add(ob);
1881         }
1882         
1883         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1884         
1885         /* imat for normals */
1886         mul_m4_m4m4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1887         invert_m4_m4(imat, mat);
1888         copy_m3_m4(wpd->wpimat, imat);
1889         
1890         /* if mirror painting, find the other group */
1891         if(me->editflag & ME_EDIT_MIRROR_X) {
1892                 wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
1893         }
1894
1895         return 1;
1896 }
1897
1898 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1899 {
1900         ToolSettings *ts= CTX_data_tool_settings(C);
1901         VPaint *wp= ts->wpaint;
1902         Brush *brush = paint_brush(&wp->paint);
1903         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1904         ViewContext *vc;
1905         Object *ob;
1906         Mesh *me;
1907         float mat[4][4];
1908         float paintweight;
1909         int *indexar;
1910         int totindex, index, totw, flip;
1911         float alpha;
1912         float mval[2], pressure;
1913         
1914         // Jason
1915         int selectedVerts;
1916
1917         /* cannot paint if there is no stroke data */
1918         if (wpd == NULL) {
1919                 // XXX: force a redraw here, since even though we can't paint, 
1920                 // at least view won't freeze until stroke ends
1921                 ED_region_tag_redraw(CTX_wm_region(C));
1922                 return;
1923         }
1924                 
1925         vc= &wpd->vc;
1926         ob= vc->obact;
1927         me= ob->data;
1928         indexar= wpd->indexar;
1929         
1930         view3d_operator_needs_opengl(C);
1931                 
1932         /* load projection matrix */
1933         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1934
1935         flip = RNA_boolean_get(itemptr, "pen_flip");
1936         pressure = RNA_float_get(itemptr, "pressure");
1937         RNA_float_get_array(itemptr, "mouse", mval);
1938         mval[0]-= vc->ar->winrct.xmin;
1939         mval[1]-= vc->ar->winrct.ymin;
1940                         
1941         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1942                         
1943         /* which faces are involved */
1944         if(wp->flag & VP_AREA) {
1945                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1946         }
1947         else {
1948                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1949                 if(indexar[0]) totindex= 1;
1950                 else totindex= 0;
1951         }
1952                         
1953         if(wp->flag & VP_COLINDEX) {
1954                 for(index=0; index<totindex; index++) {
1955                         if(indexar[index] && indexar[index]<=me->totface) {
1956                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1957                                                 
1958                                 if(mface->mat_nr!=ob->actcol-1) {
1959                                         indexar[index]= 0;
1960                                 }
1961                         }
1962                 }
1963         }
1964         // Jason
1965         selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
1966                         
1967         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1968                 for(index=0; index<totindex; index++) {
1969                         if(indexar[index] && indexar[index]<=me->totface) {
1970                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1971                                                 
1972                                 if((mface->flag & ME_FACE_SEL)==0) {
1973                                         indexar[index]= 0;
1974                                 }
1975                         }                                       
1976                 }
1977         }
1978                         
1979         /* make sure each vertex gets treated only once */
1980         /* and calculate filter weight */
1981         totw= 0;
1982         if(brush->vertexpaint_tool==VP_BLUR) 
1983                 paintweight= 0.0f;
1984         else
1985                 paintweight= ts->vgroup_weight;
1986                         
1987         for(index=0; index<totindex; index++) {
1988                 if(indexar[index] && indexar[index]<=me->totface) {
1989                         MFace *mface= me->mface + (indexar[index]-1);
1990                                         
1991                         (me->dvert+mface->v1)->flag= selectedVerts ? ((me->mvert+mface->v1)->flag & SELECT): 1;
1992                         (me->dvert+mface->v2)->flag= selectedVerts ? ((me->mvert+mface->v2)->flag & SELECT): 1;
1993                         (me->dvert+mface->v3)->flag= selectedVerts ? ((me->mvert+mface->v3)->flag & SELECT): 1;
1994                         if(mface->v4) (me->dvert+mface->v4)->flag= selectedVerts ? ((me->mvert+mface->v4)->flag & SELECT): 1;
1995                                         
1996                         if(brush->vertexpaint_tool==VP_BLUR) {
1997                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
1998                                                 
1999                                 if(wp->flag & VP_ONLYVGROUP)
2000                                         dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
2001                                 else
2002                                         dw_func= defvert_verify_index;
2003                                                 
2004                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
2005                                 if(dw) {paintweight+= dw->weight; totw++;}
2006                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
2007                                 if(dw) {paintweight+= dw->weight; totw++;}
2008                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
2009                                 if(dw) {paintweight+= dw->weight; totw++;}
2010                                 if(mface->v4) {
2011                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
2012                                         if(dw) {paintweight+= dw->weight; totw++;}
2013                                 }
2014                         }
2015                 }
2016         }
2017                         
2018         if(brush->vertexpaint_tool==VP_BLUR) 
2019                 paintweight/= (float)totw;
2020                         
2021         for(index=0; index<totindex; index++) {
2022                                 
2023                 if(indexar[index] && indexar[index]<=me->totface) {
2024                         MFace *mface= me->mface + (indexar[index]-1);
2025                                         
2026                         if((me->dvert+mface->v1)->flag) {
2027                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
2028                                 if(alpha) {
2029                                         do_weight_paint_vertex(wp, ob, mface->v1, 
2030                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2031                                                 wpd->vgroup_validmap, ts->multipaint);
2032                                 }
2033                                 (me->dvert+mface->v1)->flag= 0;
2034                         }
2035                                         
2036                         if((me->dvert+mface->v2)->flag) {
2037                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
2038                                 if(alpha) {
2039                                         do_weight_paint_vertex(wp, ob, mface->v2, 
2040                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2041                                                 wpd->vgroup_validmap, ts->multipaint);
2042                                 }
2043                                 (me->dvert+mface->v2)->flag= 0;
2044                         }
2045                                         
2046                         if((me->dvert+mface->v3)->flag) {
2047                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
2048                                 if(alpha) {
2049                                         do_weight_paint_vertex(wp, ob, mface->v3, 
2050                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
2051                                                 wpd->vgroup_validmap, ts->multipaint);
2052                                 }
2053                                 (me->dvert+mface->v3)->flag= 0;
2054                         }
2055                                         
2056                         if((me->dvert+mface->v4)->flag) {
2057                                 if(mface->v4) {
2058                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
2059                                         if(alpha) {
2060                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
2061                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
2062                                                         wpd->vgroup_validmap, ts->multipaint);
2063                                         }
2064                                         (me->dvert+mface->v4)->flag= 0;
2065                                 }
2066                         }
2067                 }
2068         }
2069                         
2070         swap_m4m4(vc->rv3d->persmat, mat);
2071                         
2072         DAG_id_tag_update(ob->data, 0);
2073         ED_region_tag_redraw(vc->ar);
2074 }
2075
2076 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2077 {
2078         ToolSettings *ts= CTX_data_tool_settings(C);
2079         Object *ob= CTX_data_active_object(C);
2080         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2081         
2082         if(wpd) {
2083                 if(wpd->vertexcosnos)
2084                         MEM_freeN(wpd->vertexcosnos);
2085                 MEM_freeN(wpd->indexar);
2086                 
2087                 if (wpd->vgroup_validmap)
2088                         MEM_freeN(wpd->vgroup_validmap);
2089                 
2090                 MEM_freeN(wpd);
2091         }
2092         
2093         /* frees prev buffer */
2094         copy_wpaint_prev(ts->wpaint, NULL, 0);
2095         
2096         /* and particles too */
2097         if(ob->particlesystem.first) {
2098                 ParticleSystem *psys;
2099                 int i;
2100                 
2101                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
2102                         for(i=0; i<PSYS_TOT_VG; i++) {
2103                                 if(psys->vgroup[i]==ob->actdef) {
2104                                         psys->recalc |= PSYS_RECALC_RESET;
2105                                         break;
2106                                 }
2107                         }
2108                 }
2109         }
2110         
2111         DAG_id_tag_update(ob->data, 0);
2112 }
2113
2114
2115 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2116 {
2117         
2118         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
2119                                           wpaint_stroke_update_step,
2120                                           wpaint_stroke_done, event->type);
2121         
2122         /* add modal handler */
2123         WM_event_add_modal_handler(C, op);
2124
2125         op->type->modal(C, op, event);
2126         
2127         return OPERATOR_RUNNING_MODAL;
2128 }
2129
2130 static int wpaint_cancel(bContext *C, wmOperator *op)
2131 {
2132         paint_stroke_cancel(C, op);
2133
2134         return OPERATOR_CANCELLED;
2135 }
2136
2137 void PAINT_OT_weight_paint(wmOperatorType *ot)
2138 {
2139         
2140         /* identifiers */
2141         ot->name= "Weight Paint";
2142         ot->idname= "PAINT_OT_weight_paint";
2143         
2144         /* api callbacks */
2145         ot->invoke= wpaint_invoke;
2146         ot->modal= paint_stroke_modal;
2147         /* ot->exec= vpaint_exec; <-- needs stroke property */
2148         ot->poll= weight_paint_poll;
2149         ot->cancel= wpaint_cancel;
2150         
2151         /* flags */
2152         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2153
2154         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2155 }
2156
2157 static int weight_paint_set_exec(bContext *C, wmOperator *UNUSED(op))
2158 {
2159         struct Scene *scene= CTX_data_scene(C);
2160         Object *obact = CTX_data_active_object(C);
2161
2162         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
2163         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
2164         return OPERATOR_FINISHED;
2165 }
2166
2167 void PAINT_OT_weight_set(wmOperatorType *ot)
2168 {
2169         /* identifiers */
2170         ot->name= "Set Weight";
2171         ot->idname= "PAINT_OT_weight_set";
2172
2173         /* api callbacks */
2174         ot->exec= weight_paint_set_exec;
2175         ot->poll= mask_paint_poll; // Jason, it was facemask_paint_poll
2176
2177         /* flags */
2178         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2179 }
2180
2181 /* ************ set / clear vertex paint mode ********** */
2182
2183
2184 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
2185 {       
2186         Object *ob= CTX_data_active_object(C);
2187         Scene *scene= CTX_data_scene(C);
2188         VPaint *vp= scene->toolsettings->vpaint;
2189         Mesh *me;
2190         
2191         me= get_mesh(ob);
2192         
2193         if(me==NULL || object_data_is_libdata(ob)) {
2194                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2195                 return OPERATOR_PASS_THROUGH;
2196         }
2197         
2198         if(me && me->mcol==NULL) make_vertexcol(ob);
2199         
2200         /* toggle: end vpaint */
2201         if(ob->mode & OB_MODE_VERTEX_PAINT) {
2202                 
2203                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2204         }
2205         else {
2206                 ob->mode |= OB_MODE_VERTEX_PAINT;
2207                 /* Turn off weight painting */
2208                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
2209                         set_wpaint(C, op);
2210                 
2211                 if(vp==NULL)
2212                         vp= scene->toolsettings->vpaint= new_vpaint(0);
2213                 
2214                 paint_cursor_start(C, vertex_paint_poll);
2215
2216                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
2217         }
2218         
2219         if (me)
2220                 /* update modifier stack for mapping requirements */
2221                 DAG_id_tag_update(&me->id, 0);
2222         
2223         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
2224         
2225         return OPERATOR_FINISHED;
2226 }
2227
2228 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
2229 {
2230         
2231         /* identifiers */
2232         ot->name= "Vertex Paint Mode";
2233         ot->idname= "PAINT_OT_vertex_paint_toggle";
2234         
2235         /* api callbacks */
2236         ot->exec= set_vpaint;
2237         ot->poll= paint_poll_test;
2238         
2239         /* flags */
2240         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2241 }
2242
2243
2244
2245 /* ********************** vertex paint operator ******************* */
2246
2247 /* Implementation notes:
2248
2249 Operator->invoke()
2250   - validate context (add mcol)
2251   - create customdata storage
2252   - call paint once (mouse click)
2253   - add modal handler 
2254
2255 Operator->modal()
2256   - for every mousemove, apply vertex paint
2257   - exit on mouse release, free customdata
2258         (return OPERATOR_FINISHED also removes handler and operator)
2259
2260 For future:
2261   - implement a stroke event (or mousemove with past positons)
2262   - revise whether op->customdata should be added in object, in set_vpaint
2263
2264 */
2265
2266 typedef struct VPaintData {
2267         ViewContext vc;
2268         unsigned int paintcol;
2269         int *indexar;
2270         float *vertexcosnos;
2271         float vpimat[3][3];
2272 } VPaintData;
2273
2274 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
2275 {
2276         ToolSettings *ts= CTX_data_tool_settings(C);
2277         struct PaintStroke *stroke = op->customdata;
2278         VPaint *vp= ts->vpaint;
2279         struct VPaintData *vpd;
2280         Object *ob= CTX_data_active_object(C);
2281         Mesh *me;
2282         float mat[4][4], imat[4][4];
2283
2284         /* context checks could be a poll() */
2285         me= get_mesh(ob);
2286         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2287         
2288         if(me->mcol==NULL) make_vertexcol(ob);
2289         if(me->mcol==NULL) return OPERATOR_CANCELLED;
2290         
2291         /* make mode data storage */
2292         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
2293         paint_stroke_set_mode_data(stroke, vpd);
2294         view3d_set_viewcontext(C, &vpd->vc);
2295         
2296         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
2297         vpd->indexar= get_indexarray(me);
2298         vpd->paintcol= vpaint_get_current_col(vp);
2299         
2300         /* for filtering */
2301         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
2302         
2303         /* some old cruft to sort out later */
2304         mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
2305         invert_m4_m4(imat, mat);
2306         copy_m3_m4(vpd->vpimat, imat);
2307
2308         return 1;
2309 }
2310
2311 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, const float mval[2], float pressure, int UNUSED(flip))
2312 {
2313         ViewContext *vc = &vpd->vc;
2314         Brush *brush = paint_brush(&vp->paint);
2315         Mesh *me = get_mesh(ob);
2316         MFace *mface= ((MFace*)me->mface) + index;
2317         // Jason
2318         MVert *mv = me->mvert;
2319         int selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
2320         char *m;
2321
2322         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
2323         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
2324         float alpha;
2325         int i;
2326         
2327         // Jason
2328         if(selectedVerts) {
2329                 m = MEM_mallocN(sizeof(char)*4, "selectedVerts");
2330                 m[0] = ((mv+mface->v1)->flag & SELECT);
2331                 m[1] = ((mv+mface->v2)->flag & SELECT);
2332                 m[2] = ((mv+mface->v3)->flag & SELECT);
2333                 m[3] = (mface->v4 && ((mv+mface->v4)->flag & SELECT));
2334         }
2335
2336         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
2337            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL))
2338            || (selectedVerts && !(m[0] || m[1] || m[2] || m[3]))) {// Jason
2339                 if(selectedVerts) {
2340                         MEM_freeN(m);
2341                 }
2342                    return;
2343         }
2344
2345         if(brush->vertexpaint_tool==VP_BLUR) {
2346                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
2347                 if(mface->v4) {
2348                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
2349                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
2350                 }
2351                 else {
2352                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
2353                 }
2354                 
2355         }
2356
2357         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
2358                 if(!selectedVerts || m[i]) {
2359                         alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i], mval, pressure);
2360                         if(alpha)
2361                                 vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0f));
2362                 }
2363         }
2364         // Jason
2365         if(selectedVerts) {
2366                 MEM_freeN(m);
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