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