fixed "rather then" -> "rather than" typos all over the place
[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; curdef; curdef=curdef->next, mirrdef++) {
305                                 if (!strcmp(curdef->name, name)) {
306                                         break;
307                                 }
308                         }
309
310                         if(curdef==NULL) {
311                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
312                                 curdef= ED_vgroup_add_name(ob, name);
313                                 ob->actdef= olddef;
314                         }
315
316                         /* curdef should never be NULL unless this is
317                          * a  lamp and ED_vgroup_add_name fails */
318                         if(curdef) {
319                                 *vgroup_mirror= mirrdef;
320                                 return;
321                         }
322                 }
323         }
324
325         *vgroup_mirror= -1;
326 }
327
328 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
329 {
330         if(vp->vpaint_prev) {
331                 MEM_freeN(vp->vpaint_prev);
332                 vp->vpaint_prev= NULL;
333         }
334         vp->tot= tot;   
335         
336         if(mcol==NULL || tot==0) return;
337         
338         vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
339         memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
340         
341 }
342
343 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
344 {
345         if (wp->wpaint_prev) {
346                 free_dverts(wp->wpaint_prev, wp->tot);
347                 wp->wpaint_prev= NULL;
348         }
349         
350         if(dverts && dcount) {
351                 
352                 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
353                 wp->tot = dcount;
354                 copy_dverts (wp->wpaint_prev, dverts, dcount);
355         }
356 }
357
358
359 void vpaint_fill(Object *ob, unsigned int paintcol)
360 {
361         Mesh *me;
362         MFace *mf;
363         unsigned int *mcol;
364         int i, selected;
365
366         me= get_mesh(ob);
367         if(me==NULL || me->totface==0) return;
368
369         if(!me->mcol)
370                 make_vertexcol(ob);
371
372         selected= (me->editflag & ME_EDIT_PAINT_MASK);
373
374         mf = me->mface;
375         mcol = (unsigned int*)me->mcol;
376         for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
377                 if (!selected || mf->flag & ME_FACE_SEL) {
378                         mcol[0] = paintcol;
379                         mcol[1] = paintcol;
380                         mcol[2] = paintcol;
381                         mcol[3] = paintcol;
382                 }
383         }
384         
385         DAG_id_tag_update(&me->id, 0);
386 }
387
388
389 /* fills in the selected faces with the current weight and vertex group */
390 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
391 {
392         Mesh *me;
393         MFace *mface;
394         MDeformWeight *dw, *uw;
395         int *indexar;
396         int index, vgroup;
397         unsigned int faceverts[5]={0,0,0,0,0};
398         unsigned char i;
399         int vgroup_mirror= -1;
400         int selected;
401         
402         me= ob->data;
403         if(me==NULL || me->totface==0 || me->dvert==NULL || !me->mface) return;
404         
405         selected= (me->editflag & ME_EDIT_PAINT_MASK);
406
407         indexar= get_indexarray(me);
408
409         if(selected) {
410                 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
411                         if((mface->flag & ME_FACE_SEL)==0)
412                                 indexar[index]= 0;
413                         else
414                                 indexar[index]= index+1;
415                 }
416         }
417         else {
418                 for(index=0; index<me->totface; index++)
419                         indexar[index]= index+1;
420         }
421         
422         vgroup= ob->actdef-1;
423
424         /* if mirror painting, find the other group */          
425         if(me->editflag & ME_EDIT_MIRROR_X) {
426                 wpaint_mirror_vgroup_ensure(ob, &vgroup_mirror);
427         }
428         
429         copy_wpaint_prev(wp, me->dvert, me->totvert);
430         
431         for(index=0; index<me->totface; index++) {
432                 if(indexar[index] && indexar[index]<=me->totface) {
433                         mface= me->mface + (indexar[index]-1);
434                         /* just so we can loop through the verts */
435                         faceverts[0]= mface->v1;
436                         faceverts[1]= mface->v2;
437                         faceverts[2]= mface->v3;
438                         faceverts[3]= mface->v4;
439                         for (i=0; i<3 || faceverts[i]; i++) {
440                                 if(!((me->dvert+faceverts[i])->flag)) {
441                                         dw= defvert_verify_index(me->dvert+faceverts[i], vgroup);
442                                         if(dw) {
443                                                 uw= defvert_verify_index(wp->wpaint_prev+faceverts[i], vgroup);
444                                                 uw->weight= dw->weight; /* set the undo weight */
445                                                 dw->weight= paintweight;
446                                                 
447                                                 if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
448                                                         int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
449                                                         if(j>=0) {
450                                                                 /* copy, not paint again */
451                                                                 if(vgroup_mirror != -1) {
452                                                                         dw= defvert_verify_index(me->dvert+j, vgroup_mirror);
453                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup_mirror);
454                                                                 } else {
455                                                                         dw= defvert_verify_index(me->dvert+j, vgroup);
456                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup);
457                                                                 }
458                                                                 uw->weight= dw->weight; /* set the undo weight */
459                                                                 dw->weight= paintweight;
460                                                         }
461                                                 }
462                                         }
463                                         (me->dvert+faceverts[i])->flag= 1;
464                                 }
465                         }
466                 }
467         }
468         
469         index=0;
470         while (index<me->totvert) {
471                 (me->dvert+index)->flag= 0;
472                 index++;
473         }
474         
475         MEM_freeN(indexar);
476         copy_wpaint_prev(wp, NULL, 0);
477
478         DAG_id_tag_update(&me->id, 0);
479 }
480
481 /* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator
482  
483 void vpaint_dogamma(Scene *scene)
484 {
485         VPaint *vp= scene->toolsettings->vpaint;
486         Mesh *me;
487         Object *ob;
488         float igam, fac;
489         int a, temp;
490         unsigned char *cp, gamtab[256];
491
492         ob= OBACT;
493         me= get_mesh(ob);
494
495         if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
496         if(me==0 || me->mcol==0 || me->totface==0) return;
497
498         igam= 1.0/vp->gamma;
499         for(a=0; a<256; a++) {
500                 
501                 fac= ((float)a)/255.0;
502                 fac= vp->mul*pow( fac, igam);
503                 
504                 temp= 255.9*fac;
505                 
506                 if(temp<=0) gamtab[a]= 0;
507                 else if(temp>=255) gamtab[a]= 255;
508                 else gamtab[a]= temp;
509         }
510
511         a= 4*me->totface;
512         cp= (unsigned char *)me->mcol;
513         while(a--) {
514                 
515                 cp[1]= gamtab[ cp[1] ];
516                 cp[2]= gamtab[ cp[2] ];
517                 cp[3]= gamtab[ cp[3] ];
518                 
519                 cp+= 4;
520         }
521 }
522  */
523
524 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
525 {
526         char *cp1, *cp2, *cp;
527         int mfac;
528         unsigned int col=0;
529         
530         if(fac==0) return col1;
531         if(fac>=255) return col2;
532
533         mfac= 255-fac;
534         
535         cp1= (char *)&col1;
536         cp2= (char *)&col2;
537         cp=  (char *)&col;
538         
539         cp[0]= 255;
540         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
541         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
542         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
543         
544         return col;
545 }
546
547 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
548 {
549         char *cp1, *cp2, *cp;
550         int temp;
551         unsigned int col=0;
552         
553         if(fac==0) return col1;
554         
555         cp1= (char *)&col1;
556         cp2= (char *)&col2;
557         cp=  (char *)&col;
558         
559         cp[0]= 255;
560         temp= cp1[1] + ((fac*cp2[1])/255);
561         if(temp>254) cp[1]= 255; else cp[1]= temp;
562         temp= cp1[2] + ((fac*cp2[2])/255);
563         if(temp>254) cp[2]= 255; else cp[2]= temp;
564         temp= cp1[3] + ((fac*cp2[3])/255);
565         if(temp>254) cp[3]= 255; else cp[3]= temp;
566         
567         return col;
568 }
569
570 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
571 {
572         char *cp1, *cp2, *cp;
573         int temp;
574         unsigned int col=0;
575         
576         if(fac==0) return col1;
577         
578         cp1= (char *)&col1;
579         cp2= (char *)&col2;
580         cp=  (char *)&col;
581         
582         cp[0]= 255;
583         temp= cp1[1] - ((fac*cp2[1])/255);
584         if(temp<0) cp[1]= 0; else cp[1]= temp;
585         temp= cp1[2] - ((fac*cp2[2])/255);
586         if(temp<0) cp[2]= 0; else cp[2]= temp;
587         temp= cp1[3] - ((fac*cp2[3])/255);
588         if(temp<0) cp[3]= 0; else cp[3]= temp;
589         
590         return col;
591 }
592
593 static unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
594 {
595         char *cp1, *cp2, *cp;
596         int mfac;
597         unsigned int col=0;
598         
599         if(fac==0) return col1;
600
601         mfac= 255-fac;
602         
603         cp1= (char *)&col1;
604         cp2= (char *)&col2;
605         cp=  (char *)&col;
606         
607         /* first mul, then blend the fac */
608         cp[0]= 255;
609         cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255)  )/255;
610         cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255)  )/255;
611         cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255)  )/255;
612
613         
614         return col;
615 }
616
617 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
618 {
619         char *cp1, *cp2, *cp;
620         int mfac;
621         unsigned int col=0;
622         
623         if(fac==0) return col1;
624         if(fac>=255) return col2;
625
626         mfac= 255-fac;
627         
628         cp1= (char *)&col1;
629         cp2= (char *)&col2;
630         cp=  (char *)&col;
631         
632         /* See if are lighter, if so mix, else dont do anything.
633         if the paint col is darker then the original, then ignore */
634         if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
635                 return col1;
636         
637         cp[0]= 255;
638         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
639         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
640         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
641         
642         return col;
643 }
644
645 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
646 {
647         char *cp1, *cp2, *cp;
648         int mfac;
649         unsigned int col=0;
650         
651         if(fac==0) return col1;
652         if(fac>=255) return col2;
653
654         mfac= 255-fac;
655         
656         cp1= (char *)&col1;
657         cp2= (char *)&col2;
658         cp=  (char *)&col;
659         
660         /* See if were darker, if so mix, else dont do anything.
661         if the paint col is brighter then the original, then ignore */
662         if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
663                 return col1;
664         
665         cp[0]= 255;
666         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
667         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
668         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
669         return col;
670 }
671
672 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
673 {
674         Brush *brush = paint_brush(&vp->paint);
675
676         if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) *col= mcol_blend( *col, paintcol, alpha);
677         else if(brush->vertexpaint_tool==VP_ADD) *col= mcol_add( *col, paintcol, alpha);
678         else if(brush->vertexpaint_tool==VP_SUB) *col= mcol_sub( *col, paintcol, alpha);
679         else if(brush->vertexpaint_tool==VP_MUL) *col= mcol_mul( *col, paintcol, alpha);
680         else if(brush->vertexpaint_tool==VP_LIGHTEN) *col= mcol_lighten( *col, paintcol, alpha);
681         else if(brush->vertexpaint_tool==VP_DARKEN) *col= mcol_darken( *col, paintcol, alpha);
682         
683         /* if no spray, clip color adding with colorig & orig alpha */
684         if((vp->flag & VP_SPRAY)==0) {
685                 unsigned int testcol=0, a;
686                 char *cp, *ct, *co;
687                 
688                 alpha= (int)(255.0f*brush_alpha(brush));
689                 
690                 if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
691                 else if(brush->vertexpaint_tool==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
692                 else if(brush->vertexpaint_tool==VP_SUB) testcol= mcol_sub( *colorig, paintcol, alpha);
693                 else if(brush->vertexpaint_tool==VP_MUL) testcol= mcol_mul( *colorig, paintcol, alpha);
694                 else if(brush->vertexpaint_tool==VP_LIGHTEN)  testcol= mcol_lighten( *colorig, paintcol, alpha);
695                 else if(brush->vertexpaint_tool==VP_DARKEN)   testcol= mcol_darken( *colorig, paintcol, alpha);
696                 
697                 cp= (char *)col;
698                 ct= (char *)&testcol;
699                 co= (char *)colorig;
700                 
701                 for(a=0; a<4; a++) {
702                         if( ct[a]<co[a] ) {
703                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
704                                 else if( cp[a]>co[a] ) cp[a]= co[a];
705                         }
706                         else {
707                                 if( cp[a]<co[a] ) cp[a]= co[a];
708                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
709                         }
710                 }
711         }
712 }
713
714
715 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
716 {
717         struct ImBuf *ibuf;
718         int a, tot=0, index;
719         
720         /* brecht: disabled this because it obviously failes for
721            brushes with size > 64, why is this here? */
722         /*if(size>64.0) size= 64.0;*/
723         
724         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
725         if(ibuf) {
726                 unsigned int *rt= ibuf->rect;
727
728                 memset(indexar, 0, sizeof(int)*(totface+1));
729                 
730                 size= ibuf->x*ibuf->y;
731                 while(size--) {
732                                 
733                         if(*rt) {
734                                 index= WM_framebuffer_to_index(*rt);
735                                 if(index>0 && index<=totface)
736                                         indexar[index] = 1;
737                         }
738                 
739                         rt++;
740                 }
741                 
742                 for(a=1; a<=totface; a++) {
743                         if(indexar[a]) indexar[tot++]= a;
744                 }
745
746                 IMB_freeImBuf(ibuf);
747         }
748         
749         return tot;
750 }
751
752 static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, const float mval[2], float pressure)
753 {
754         Brush *brush = paint_brush(&vp->paint);
755         float fac, fac_2, size, dx, dy;
756         float alpha;
757         int vertco[2];
758         const int radius= brush_size(brush);
759
760         project_int_noclip(vc->ar, vert_nor, vertco);
761         dx= mval[0]-vertco[0];
762         dy= mval[1]-vertco[1];
763         
764         if (brush_use_size_pressure(brush))
765                 size = pressure * radius;
766         else
767                 size = radius;
768         
769         fac_2= dx*dx + dy*dy;
770         if(fac_2 > size*size) return 0.f;
771         fac = sqrtf(fac_2);
772         
773         alpha= brush_alpha(brush) * brush_curve_strength_clamp(brush, fac, size);
774         
775         if (brush_use_alpha_pressure(brush))
776                 alpha *= pressure;
777                 
778         if(vp->flag & VP_NORMALS) {
779                 float *no= vert_nor+3;
780                 
781                 /* transpose ! */
782                 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
783                 if(fac > 0.0f) {
784                         dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
785                         dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
786                         
787                         alpha*= fac/sqrtf(dx*dx + dy*dy + fac*fac);
788                 }
789                 else return 0.f;
790         }
791         
792         return alpha;
793 }
794
795 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip)
796 {
797         Brush *brush = paint_brush(&wp->paint);
798         int tool = brush->vertexpaint_tool;
799         
800         if(dw==NULL || uw==NULL) return;
801         
802         if (flip) {
803                 switch(tool) {
804                         case VP_MIX:
805                                 paintval = 1.f - paintval; break;
806                         case VP_ADD:
807                                 tool= VP_SUB; break;
808                         case VP_SUB:
809                                 tool= VP_ADD; break;
810                         case VP_LIGHTEN:
811                                 tool= VP_DARKEN; break;
812                         case VP_DARKEN:
813                                 tool= VP_LIGHTEN; break;
814                 }
815         }
816         
817         if(tool==VP_MIX || tool==VP_BLUR)
818                 dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
819         else if(tool==VP_ADD)
820                 dw->weight += paintval*alpha;
821         else if(tool==VP_SUB) 
822                 dw->weight -= paintval*alpha;
823         else if(tool==VP_MUL) 
824                 /* first mul, then blend the fac */
825                 dw->weight = ((1.0f-alpha) + alpha*paintval)*dw->weight;
826         else if(tool==VP_LIGHTEN) {
827                 if (dw->weight < paintval)
828                         dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
829         } else if(tool==VP_DARKEN) {
830                 if (dw->weight > paintval)
831                         dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
832         }
833         CLAMP(dw->weight, 0.0f, 1.0f);
834         
835         /* if no spray, clip result with orig weight & orig alpha */
836         if((wp->flag & VP_SPRAY)==0) {
837                 float testw=0.0f;
838                 
839                 alpha= brush_alpha(brush);
840                 if(tool==VP_MIX || tool==VP_BLUR)
841                         testw = paintval*alpha + uw->weight*(1.0f-alpha);
842                 else if(tool==VP_ADD)
843                         testw = uw->weight + paintval*alpha;
844                 else if(tool==VP_SUB) 
845                         testw = uw->weight - paintval*alpha;
846                 else if(tool==VP_MUL) 
847                         /* first mul, then blend the fac */
848                         testw = ((1.0f-alpha) + alpha*paintval)*uw->weight;
849                 else if(tool==VP_LIGHTEN) {
850                         if (uw->weight < paintval)
851                                 testw = paintval*alpha + uw->weight*(1.0f-alpha);
852                         else
853                                 testw = uw->weight;
854                 } else if(tool==VP_DARKEN) {
855                         if (uw->weight > paintval)
856                                 testw = paintval*alpha + uw->weight*(1.0f-alpha);
857                         else
858                                 testw = uw->weight;
859                 }
860                 CLAMP(testw, 0.0f, 1.0f);
861                 
862                 if( testw<uw->weight ) {
863                         if(dw->weight < testw) dw->weight= testw;
864                         else if(dw->weight > uw->weight) dw->weight= uw->weight;
865                 }
866                 else {
867                         if(dw->weight > testw) dw->weight= testw;
868                         else if(dw->weight < uw->weight) dw->weight= uw->weight;
869                 }
870         }
871         
872 }
873
874 /* ----------------------------------------------------- */
875
876
877 /* sets wp->weight to the closest weight value to vertex */
878 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
879 static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
880 {
881         ViewContext vc;
882         Mesh *me;
883         short change= FALSE;
884
885         view3d_set_viewcontext(C, &vc);
886         me= get_mesh(vc.obact);
887
888         if (me && me->dvert && vc.v3d && vc.rv3d) {
889                 int index;
890
891                 view3d_operator_needs_opengl(C);
892
893                 index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
894
895                 if(index && index<=me->totface) {
896                         DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
897
898                         if(dm->getVertCo==NULL) {
899                                 BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
900                         }
901                         else {
902                                 MFace *mf= ((MFace *)me->mface) + index-1;
903                                 const int vgroup= vc.obact->actdef - 1;
904                                 ToolSettings *ts= vc.scene->toolsettings;
905                                 float mval_f[2];
906                                 int v_idx_best= -1;
907                                 int fidx;
908                                 float len_best= FLT_MAX;
909
910                                 mval_f[0]= (float)event->mval[0];
911                                 mval_f[1]= (float)event->mval[1];
912
913                                 fidx= mf->v4 ? 3:2;
914                                 do {
915                                         float co[3], sco[3], len;
916                                         const int v_idx= (*(&mf->v1 + fidx));
917                                         dm->getVertCo(dm, v_idx, co);
918                                         project_float_noclip(vc.ar, co, sco);
919                                         len= len_squared_v2v2(mval_f, sco);
920                                         if(len < len_best) {
921                                                 len_best= len;
922                                                 v_idx_best= v_idx;
923                                         }
924                                 } while (fidx--);
925
926                                 if(v_idx_best != -1) { /* should always be valid */
927                                         ts->vgroup_weight= defvert_find_weight(&me->dvert[v_idx_best], vgroup);
928                                         change= TRUE;
929                                 }
930                         }
931                         dm->release(dm);
932                 }
933         }
934
935         if(change) {
936                 /* not really correct since the brush didnt change, but redraws the toolbar */
937                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, NULL); /* ts->wpaint->paint.brush */
938
939                 return OPERATOR_FINISHED;
940         }
941         else {
942                 return OPERATOR_CANCELLED;
943         }
944 }
945
946 void PAINT_OT_weight_sample(wmOperatorType *ot)
947 {
948         /* identifiers */
949         ot->name= "Weight Paint Sample Weight";
950         ot->idname= "PAINT_OT_weight_sample";
951
952         /* api callbacks */
953         ot->invoke= weight_sample_invoke;
954         ot->poll= weight_paint_mode_poll;
955
956         /* flags */
957         ot->flag= OPTYPE_UNDO;
958 }
959
960 /* samples cursor location, and gives menu with vertex groups to activate */
961 static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
962 {
963         if (C) {
964                 wmWindow *win= CTX_wm_window(C);
965                 if(win && win->eventstate) {
966                         ViewContext vc;
967                         Mesh *me;
968
969                         view3d_set_viewcontext(C, &vc);
970                         me= get_mesh(vc.obact);
971
972                         if (me && me->dvert && vc.v3d && vc.rv3d) {
973                                 int index;
974
975                                 view3d_operator_needs_opengl(C);
976
977                                 index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
978
979                                 if(index && index<=me->totface) {
980                                         const int totgroup= BLI_countlist(&vc.obact->defbase);
981                                         if(totgroup) {
982                                                 MFace *mf= ((MFace *)me->mface) + index-1;
983                                                 int fidx= mf->v4 ? 3:2;
984                                                 int *groups= MEM_callocN(totgroup*sizeof(int), "groups");
985                                                 int found= FALSE;
986
987                                                 do {
988                                                         MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
989                                                         int i= dvert->totweight;
990                                                         MDeformWeight *dw;
991                                                         for(dw= dvert->dw; i > 0; dw++, i--) {
992                                                                 groups[dw->def_nr]= TRUE;
993                                                                 found= TRUE;
994                                                         }
995                                                 } while (fidx--);
996
997                                                 if(found==FALSE) {
998                                                         MEM_freeN(groups);
999                                                 }
1000                                                 else {
1001                                                         EnumPropertyItem *item= NULL, item_tmp= {0};
1002                                                         int totitem= 0;
1003                                                         int i= 0;
1004                                                         bDeformGroup *dg;
1005                                                         for(dg= vc.obact->defbase.first; dg && i<totgroup; i++, dg= dg->next) {
1006                                                                 if(groups[i]) {
1007                                                                         item_tmp.identifier= item_tmp.name= dg->name;
1008                                                                         item_tmp.value= i;
1009                                                                         RNA_enum_item_add(&item, &totitem, &item_tmp);
1010                                                                 }
1011                                                         }
1012
1013                                                         RNA_enum_item_end(&item, &totitem);
1014                                                         *free= 1;
1015
1016                                                         MEM_freeN(groups);
1017                                                         return item;
1018                                                 }
1019                                         }
1020                                 }
1021                         }
1022                 }
1023         }
1024
1025         return DummyRNA_NULL_items;
1026 }
1027
1028 static int weight_sample_group_exec(bContext *C, wmOperator *op)
1029 {
1030         int type= RNA_enum_get(op->ptr, "group");
1031         ViewContext vc;
1032         view3d_set_viewcontext(C, &vc);
1033
1034         vc.obact->actdef= type + 1;
1035
1036         DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
1037         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, vc.obact);
1038         return OPERATOR_FINISHED;
1039 }
1040
1041 /* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
1042 void PAINT_OT_weight_sample_group(wmOperatorType *ot)
1043 {
1044         PropertyRNA *prop= NULL;
1045
1046         /* identifiers */
1047         ot->name= "Weight Paint Sample Group";
1048         ot->idname= "PAINT_OT_weight_sample_group";
1049
1050         /* api callbacks */
1051         ot->exec= weight_sample_group_exec;
1052         ot->invoke= WM_menu_invoke;
1053         ot->poll= weight_paint_mode_poll;
1054
1055         /* flags */
1056         ot->flag= OPTYPE_UNDO;
1057
1058         /* keyingset to use (dynamic enum) */
1059         prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
1060         RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
1061         ot->prop= prop;
1062 }
1063
1064
1065 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
1066                                            int paint_nr, char *map)
1067 {
1068 //      MDeformWeight *dw = dvert->dw;
1069         float sum=0.0f, fac=0.0f, paintw=0.0f;
1070         int i, tot=0;
1071
1072         if (!map)
1073                 return;
1074
1075         for (i=0; i<dvert->totweight; i++) {
1076                 if (dvert->dw[i].def_nr == paint_nr)
1077                         paintw = dvert->dw[i].weight;
1078
1079                 if (map[dvert->dw[i].def_nr]) {
1080                         tot += 1;
1081                         if (dvert->dw[i].def_nr != paint_nr)
1082                                 sum += dvert->dw[i].weight;
1083                 }
1084         }
1085         
1086         if (!tot || sum <= (1.0f - paintw))
1087                 return;
1088
1089         fac = sum / (1.0f - paintw);
1090         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1091
1092         for (i=0; i<dvert->totweight; i++) {
1093                 if (map[dvert->dw[i].def_nr]) {
1094                         if (dvert->dw[i].def_nr != paint_nr)
1095                                 dvert->dw[i].weight *= fac;
1096                 }
1097         }
1098 }
1099
1100 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
1101                                    float alpha, float paintweight, int flip, 
1102                                    int vgroup_mirror, char *validmap)
1103 {
1104         Mesh *me= ob->data;
1105         MDeformWeight *dw, *uw;
1106         int vgroup= ob->actdef-1;
1107         
1108         if(wp->flag & VP_ONLYVGROUP) {
1109                 dw= defvert_find_index(me->dvert+index, vgroup);
1110                 uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
1111         }
1112         else {
1113                 dw= defvert_verify_index(me->dvert+index, vgroup);
1114                 uw= defvert_verify_index(wp->wpaint_prev+index, vgroup);
1115         }
1116         if(dw==NULL || uw==NULL)
1117                 return;
1118         
1119         wpaint_blend(wp, dw, uw, alpha, paintweight, flip);
1120         do_weight_paint_auto_normalize(me->dvert+index, vgroup, validmap);
1121
1122         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
1123                 int j= mesh_get_x_mirror_vert(ob, index);
1124                 if(j>=0) {
1125                         /* copy, not paint again */
1126                         if(vgroup_mirror != -1)
1127                                 uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
1128                         else
1129                                 uw= defvert_verify_index(me->dvert+j, vgroup);
1130                                 
1131                         uw->weight= dw->weight;
1132
1133                         do_weight_paint_auto_normalize(me->dvert+j, vgroup, validmap);
1134                 }
1135         }
1136 }
1137
1138
1139 /* *************** set wpaint operator ****************** */
1140
1141 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))              /* toggle */
1142 {               
1143         Object *ob= CTX_data_active_object(C);
1144         Scene *scene= CTX_data_scene(C);
1145         VPaint *wp= scene->toolsettings->wpaint;
1146         Mesh *me;
1147         
1148         me= get_mesh(ob);
1149         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1150         
1151         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1152         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1153         
1154         
1155         /* Weightpaint works by overriding colors in mesh,
1156                 * so need to make sure we recalc on enter and
1157                 * exit (exit needs doing regardless because we
1158                                 * should redeform).
1159                 */
1160         DAG_id_tag_update(&me->id, 0);
1161         
1162         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1163                 Object *par;
1164                 
1165                 if(wp==NULL)
1166                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1167
1168                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1169                 paint_cursor_start(C, weight_paint_poll);
1170                 
1171                 mesh_octree_table(ob, NULL, NULL, 's');
1172                 
1173                 /* verify if active weight group is also active bone */
1174                 par= modifiers_isDeformedByArmature(ob);
1175                 if(par && (par->mode & OB_MODE_POSE)) {
1176                         bArmature *arm= par->data;
1177
1178                         if(arm->act_bone)
1179                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1180                 }
1181         }
1182         else {
1183                 mesh_octree_table(NULL, NULL, NULL, 'e');
1184                 mesh_mirrtopo_table(NULL, 'e');
1185         }
1186         
1187         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1188         
1189         return OPERATOR_FINISHED;
1190 }
1191
1192 /* for switching to/from mode */
1193 static int paint_poll_test(bContext *C)
1194 {
1195         if(CTX_data_edit_object(C))
1196                 return 0;
1197         if(CTX_data_active_object(C)==NULL)
1198                 return 0;
1199         return 1;
1200 }
1201
1202 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1203 {
1204         
1205         /* identifiers */
1206         ot->name= "Weight Paint Mode";
1207         ot->idname= "PAINT_OT_weight_paint_toggle";
1208         
1209         /* api callbacks */
1210         ot->exec= set_wpaint;
1211         ot->poll= paint_poll_test;
1212         
1213         /* flags */
1214         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1215         
1216 }
1217
1218 /* ************ weight paint operator ********** */
1219
1220 struct WPaintData {
1221         ViewContext vc;
1222         int *indexar;
1223         int vgroup_mirror;
1224         float *vertexcosnos;
1225         float wpimat[3][3];
1226         
1227         /*variables for auto normalize*/
1228         int auto_normalize;
1229         char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1230 };
1231
1232 static char *wpaint_make_validmap(Object *ob)
1233 {
1234         bDeformGroup *dg;
1235         ModifierData *md;
1236         char *validmap;
1237         bPose *pose;
1238         bPoseChannel *chan;
1239         ArmatureModifierData *amd;
1240         GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1241         int i = 0, step1=1;
1242
1243         /*add all names to a hash table*/
1244         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1245                 BLI_ghash_insert(gh, dg->name, NULL);
1246         }
1247
1248         if (!i)
1249                 return NULL;
1250
1251         validmap = MEM_callocN(i, "wpaint valid map");
1252
1253         /*now loop through the armature modifiers and identify deform bones*/
1254         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1255                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1256                         continue;
1257
1258                 if (md->type == eModifierType_Armature) 
1259                 {
1260                         amd = (ArmatureModifierData*) md;
1261
1262                         if(amd->object && amd->object->pose) {
1263                                 pose = amd->object->pose;
1264                                 
1265                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1266                                         if (chan->bone->flag & BONE_NO_DEFORM)
1267                                                 continue;
1268
1269                                         if (BLI_ghash_haskey(gh, chan->name)) {
1270                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1271                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1272                                         }
1273                                 }
1274                         }
1275                 }
1276         }
1277         
1278         /*add all names to a hash table*/
1279         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1280                 if (BLI_ghash_lookup(gh, dg->name) != NULL) {
1281                         validmap[i] = 1;
1282                 }
1283         }
1284
1285         BLI_ghash_free(gh, NULL, NULL);
1286
1287         return validmap;
1288 }
1289
1290 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1291 {
1292         Scene *scene= CTX_data_scene(C);
1293         struct PaintStroke *stroke = op->customdata;
1294         ToolSettings *ts= CTX_data_tool_settings(C);
1295         VPaint *wp= ts->wpaint;
1296         Object *ob= CTX_data_active_object(C);
1297         struct WPaintData *wpd;
1298         Mesh *me;
1299         float mat[4][4], imat[4][4];
1300         
1301         if(scene->obedit) return OPERATOR_CANCELLED;
1302         
1303         me= get_mesh(ob);
1304         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1305         
1306         /* if nothing was added yet, we make dverts and a vertex deform group */
1307         if (!me->dvert) {
1308                 ED_vgroup_data_create(&me->id);
1309                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1310         }
1311         
1312         /* make mode data storage */
1313         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1314         paint_stroke_set_mode_data(stroke, wpd);
1315         view3d_set_viewcontext(C, &wpd->vc);
1316         wpd->vgroup_mirror= -1;
1317         
1318         /*set up auto-normalize, and generate map for detecting which
1319           vgroups affect deform bones*/
1320         wpd->auto_normalize = ts->auto_normalize;
1321         if (wpd->auto_normalize)
1322                 wpd->vgroup_validmap = wpaint_make_validmap(ob);
1323         
1324         //      if(qual & LR_CTRLKEY) {
1325         //              sample_wpaint(scene, ar, v3d, 0);
1326         //              return;
1327         //      }
1328         //      if(qual & LR_SHIFTKEY) {
1329         //              sample_wpaint(scene, ar, v3d, 1);
1330         //              return;
1331         //      }
1332         
1333         /* ALLOCATIONS! no return after this line */
1334         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1335         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1336         wpd->indexar= get_indexarray(me);
1337         copy_wpaint_prev(wp, me->dvert, me->totvert);
1338         
1339         /* this happens on a Bone select, when no vgroup existed yet */
1340         if(ob->actdef<=0) {
1341                 Object *modob;
1342                 if((modob = modifiers_isDeformedByArmature(ob))) {
1343                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
1344                         if(actbone) {
1345                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
1346
1347                                 if(pchan) {
1348                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
1349                                         if(dg==NULL)
1350                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
1351                                         else
1352                                                 ob->actdef= 1 + defgroup_find_index(ob, dg);
1353                                 }
1354                         }
1355                 }
1356         }
1357         if(ob->defbase.first==NULL) {
1358                 ED_vgroup_add(ob);
1359         }
1360         
1361         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1362         
1363         /* imat for normals */
1364         mul_m4_m4m4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1365         invert_m4_m4(imat, mat);
1366         copy_m3_m4(wpd->wpimat, imat);
1367         
1368         /* if mirror painting, find the other group */
1369         if(me->editflag & ME_EDIT_MIRROR_X) {
1370                 wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
1371         }
1372         
1373         return 1;
1374 }
1375
1376 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1377 {
1378         ToolSettings *ts= CTX_data_tool_settings(C);
1379         VPaint *wp= ts->wpaint;
1380         Brush *brush = paint_brush(&wp->paint);
1381         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1382         ViewContext *vc;
1383         Object *ob;
1384         Mesh *me;
1385         float mat[4][4];
1386         float paintweight;
1387         int *indexar;
1388         int totindex, index, totw, flip;
1389         float alpha;
1390         float mval[2], pressure;
1391         
1392         /* cannot paint if there is no stroke data */
1393         if (wpd == NULL) {
1394                 // XXX: force a redraw here, since even though we can't paint, 
1395                 // at least view won't freeze until stroke ends
1396                 ED_region_tag_redraw(CTX_wm_region(C));
1397                 return;
1398         }
1399                 
1400         vc= &wpd->vc;
1401         ob= vc->obact;
1402         me= ob->data;
1403         indexar= wpd->indexar;
1404         
1405         view3d_operator_needs_opengl(C);
1406                 
1407         /* load projection matrix */
1408         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1409
1410         flip = RNA_boolean_get(itemptr, "pen_flip");
1411         pressure = RNA_float_get(itemptr, "pressure");
1412         RNA_float_get_array(itemptr, "mouse", mval);
1413         mval[0]-= vc->ar->winrct.xmin;
1414         mval[1]-= vc->ar->winrct.ymin;
1415                         
1416         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1417                         
1418         /* which faces are involved */
1419         if(wp->flag & VP_AREA) {
1420                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1421         }
1422         else {
1423                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1424                 if(indexar[0]) totindex= 1;
1425                 else totindex= 0;
1426         }
1427                         
1428         if(wp->flag & VP_COLINDEX) {
1429                 for(index=0; index<totindex; index++) {
1430                         if(indexar[index] && indexar[index]<=me->totface) {
1431                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1432                                                 
1433                                 if(mface->mat_nr!=ob->actcol-1) {
1434                                         indexar[index]= 0;
1435                                 }
1436                         }
1437                 }
1438         }
1439                         
1440         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1441                 for(index=0; index<totindex; index++) {
1442                         if(indexar[index] && indexar[index]<=me->totface) {
1443                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1444                                                 
1445                                 if((mface->flag & ME_FACE_SEL)==0) {
1446                                         indexar[index]= 0;
1447                                 }
1448                         }                                       
1449                 }
1450         }
1451                         
1452         /* make sure each vertex gets treated only once */
1453         /* and calculate filter weight */
1454         totw= 0;
1455         if(brush->vertexpaint_tool==VP_BLUR) 
1456                 paintweight= 0.0f;
1457         else
1458                 paintweight= ts->vgroup_weight;
1459                         
1460         for(index=0; index<totindex; index++) {
1461                 if(indexar[index] && indexar[index]<=me->totface) {
1462                         MFace *mface= me->mface + (indexar[index]-1);
1463                                         
1464                         (me->dvert+mface->v1)->flag= 1;
1465                         (me->dvert+mface->v2)->flag= 1;
1466                         (me->dvert+mface->v3)->flag= 1;
1467                         if(mface->v4) (me->dvert+mface->v4)->flag= 1;
1468                                         
1469                         if(brush->vertexpaint_tool==VP_BLUR) {
1470                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
1471                                                 
1472                                 if(wp->flag & VP_ONLYVGROUP)
1473                                         dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
1474                                 else
1475                                         dw_func= defvert_verify_index;
1476                                                 
1477                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
1478                                 if(dw) {paintweight+= dw->weight; totw++;}
1479                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
1480                                 if(dw) {paintweight+= dw->weight; totw++;}
1481                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
1482                                 if(dw) {paintweight+= dw->weight; totw++;}
1483                                 if(mface->v4) {
1484                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1485                                         if(dw) {paintweight+= dw->weight; totw++;}
1486                                 }
1487                         }
1488                 }
1489         }
1490                         
1491         if(brush->vertexpaint_tool==VP_BLUR) 
1492                 paintweight/= (float)totw;
1493                         
1494         for(index=0; index<totindex; index++) {
1495                                 
1496                 if(indexar[index] && indexar[index]<=me->totface) {
1497                         MFace *mface= me->mface + (indexar[index]-1);
1498                                         
1499                         if((me->dvert+mface->v1)->flag) {
1500                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
1501                                 if(alpha) {
1502                                         do_weight_paint_vertex(wp, ob, mface->v1, 
1503                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1504                                                 wpd->vgroup_validmap);
1505                                 }
1506                                 (me->dvert+mface->v1)->flag= 0;
1507                         }
1508                                         
1509                         if((me->dvert+mface->v2)->flag) {
1510                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
1511                                 if(alpha) {
1512                                         do_weight_paint_vertex(wp, ob, mface->v2, 
1513                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1514                                                 wpd->vgroup_validmap);
1515                                 }
1516                                 (me->dvert+mface->v2)->flag= 0;
1517                         }
1518                                         
1519                         if((me->dvert+mface->v3)->flag) {
1520                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
1521                                 if(alpha) {
1522                                         do_weight_paint_vertex(wp, ob, mface->v3, 
1523                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1524                                                 wpd->vgroup_validmap);
1525                                 }
1526                                 (me->dvert+mface->v3)->flag= 0;
1527                         }
1528                                         
1529                         if((me->dvert+mface->v4)->flag) {
1530                                 if(mface->v4) {
1531                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
1532                                         if(alpha) {
1533                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
1534                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
1535                                                         wpd->vgroup_validmap);
1536                                         }
1537                                         (me->dvert+mface->v4)->flag= 0;
1538                                 }
1539                         }
1540                 }
1541         }
1542                         
1543         swap_m4m4(vc->rv3d->persmat, mat);
1544                         
1545         DAG_id_tag_update(ob->data, 0);
1546         ED_region_tag_redraw(vc->ar);
1547 }
1548
1549 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1550 {
1551         ToolSettings *ts= CTX_data_tool_settings(C);
1552         Object *ob= CTX_data_active_object(C);
1553         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1554         
1555         if(wpd) {
1556                 if(wpd->vertexcosnos)
1557                         MEM_freeN(wpd->vertexcosnos);
1558                 MEM_freeN(wpd->indexar);
1559                 
1560                 if (wpd->vgroup_validmap)
1561                         MEM_freeN(wpd->vgroup_validmap);
1562                 
1563                 MEM_freeN(wpd);
1564         }
1565         
1566         /* frees prev buffer */
1567         copy_wpaint_prev(ts->wpaint, NULL, 0);
1568         
1569         /* and particles too */
1570         if(ob->particlesystem.first) {
1571                 ParticleSystem *psys;
1572                 int i;
1573                 
1574                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
1575                         for(i=0; i<PSYS_TOT_VG; i++) {
1576                                 if(psys->vgroup[i]==ob->actdef) {
1577                                         psys->recalc |= PSYS_RECALC_RESET;
1578                                         break;
1579                                 }
1580                         }
1581                 }
1582         }
1583         
1584         DAG_id_tag_update(ob->data, 0);
1585 }
1586
1587
1588 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1589 {
1590         
1591         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
1592                                           wpaint_stroke_update_step,
1593                                           wpaint_stroke_done, event->type);
1594         
1595         /* add modal handler */
1596         WM_event_add_modal_handler(C, op);
1597
1598         op->type->modal(C, op, event);
1599         
1600         return OPERATOR_RUNNING_MODAL;
1601 }
1602
1603 void PAINT_OT_weight_paint(wmOperatorType *ot)
1604 {
1605         
1606         /* identifiers */
1607         ot->name= "Weight Paint";
1608         ot->idname= "PAINT_OT_weight_paint";
1609         
1610         /* api callbacks */
1611         ot->invoke= wpaint_invoke;
1612         ot->modal= paint_stroke_modal;
1613         /* ot->exec= vpaint_exec; <-- needs stroke property */
1614         ot->poll= weight_paint_poll;
1615         
1616         /* flags */
1617         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1618
1619         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1620 }
1621
1622 static int weight_paint_set_exec(bContext *C, wmOperator *UNUSED(op))
1623 {
1624         struct Scene *scene= CTX_data_scene(C);
1625         Object *obact = CTX_data_active_object(C);
1626
1627         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
1628         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
1629         return OPERATOR_FINISHED;
1630 }
1631
1632 void PAINT_OT_weight_set(wmOperatorType *ot)
1633 {
1634         /* identifiers */
1635         ot->name= "Set Weight";
1636         ot->idname= "PAINT_OT_weight_set";
1637
1638         /* api callbacks */
1639         ot->exec= weight_paint_set_exec;
1640         ot->poll= facemask_paint_poll;
1641
1642         /* flags */
1643         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1644 }
1645
1646 /* ************ set / clear vertex paint mode ********** */
1647
1648
1649 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
1650 {       
1651         Object *ob= CTX_data_active_object(C);
1652         Scene *scene= CTX_data_scene(C);
1653         VPaint *vp= scene->toolsettings->vpaint;
1654         Mesh *me;
1655         
1656         me= get_mesh(ob);
1657         
1658         if(me==NULL || object_data_is_libdata(ob)) {
1659                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1660                 return OPERATOR_PASS_THROUGH;
1661         }
1662         
1663         if(me && me->mcol==NULL) make_vertexcol(ob);
1664         
1665         /* toggle: end vpaint */
1666         if(ob->mode & OB_MODE_VERTEX_PAINT) {
1667                 
1668                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1669         }
1670         else {
1671                 ob->mode |= OB_MODE_VERTEX_PAINT;
1672                 /* Turn off weight painting */
1673                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
1674                         set_wpaint(C, op);
1675                 
1676                 if(vp==NULL)
1677                         vp= scene->toolsettings->vpaint= new_vpaint(0);
1678                 
1679                 paint_cursor_start(C, vertex_paint_poll);
1680
1681                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
1682         }
1683         
1684         if (me)
1685                 /* update modifier stack for mapping requirements */
1686                 DAG_id_tag_update(&me->id, 0);
1687         
1688         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1689         
1690         return OPERATOR_FINISHED;
1691 }
1692
1693 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1694 {
1695         
1696         /* identifiers */
1697         ot->name= "Vertex Paint Mode";
1698         ot->idname= "PAINT_OT_vertex_paint_toggle";
1699         
1700         /* api callbacks */
1701         ot->exec= set_vpaint;
1702         ot->poll= paint_poll_test;
1703         
1704         /* flags */
1705         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1706 }
1707
1708
1709
1710 /* ********************** vertex paint operator ******************* */
1711
1712 /* Implementation notes:
1713
1714 Operator->invoke()
1715   - validate context (add mcol)
1716   - create customdata storage
1717   - call paint once (mouse click)
1718   - add modal handler 
1719
1720 Operator->modal()
1721   - for every mousemove, apply vertex paint
1722   - exit on mouse release, free customdata
1723         (return OPERATOR_FINISHED also removes handler and operator)
1724
1725 For future:
1726   - implement a stroke event (or mousemove with past positons)
1727   - revise whether op->customdata should be added in object, in set_vpaint
1728
1729 */
1730
1731 typedef struct VPaintData {
1732         ViewContext vc;
1733         unsigned int paintcol;
1734         int *indexar;
1735         float *vertexcosnos;
1736         float vpimat[3][3];
1737 } VPaintData;
1738
1739 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
1740 {
1741         ToolSettings *ts= CTX_data_tool_settings(C);
1742         struct PaintStroke *stroke = op->customdata;
1743         VPaint *vp= ts->vpaint;
1744         struct VPaintData *vpd;
1745         Object *ob= CTX_data_active_object(C);
1746         Mesh *me;
1747         float mat[4][4], imat[4][4];
1748
1749         /* context checks could be a poll() */
1750         me= get_mesh(ob);
1751         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1752         
1753         if(me->mcol==NULL) make_vertexcol(ob);
1754         if(me->mcol==NULL) return OPERATOR_CANCELLED;
1755         
1756         /* make mode data storage */
1757         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
1758         paint_stroke_set_mode_data(stroke, vpd);
1759         view3d_set_viewcontext(C, &vpd->vc);
1760         
1761         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
1762         vpd->indexar= get_indexarray(me);
1763         vpd->paintcol= vpaint_get_current_col(vp);
1764         
1765         /* for filtering */
1766         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
1767         
1768         /* some old cruft to sort out later */
1769         mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
1770         invert_m4_m4(imat, mat);
1771         copy_m3_m4(vpd->vpimat, imat);
1772
1773         return 1;
1774 }
1775
1776 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, const float mval[2], float pressure, int UNUSED(flip))
1777 {
1778         ViewContext *vc = &vpd->vc;
1779         Brush *brush = paint_brush(&vp->paint);
1780         Mesh *me = get_mesh(ob);
1781         MFace *mface= ((MFace*)me->mface) + index;
1782         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
1783         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
1784         float alpha;
1785         int i;
1786         
1787         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
1788            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
1789                 return;
1790
1791         if(brush->vertexpaint_tool==VP_BLUR) {
1792                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
1793                 if(mface->v4) {
1794                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
1795                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
1796                 }
1797                 else {
1798                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
1799                 }
1800                 
1801         }
1802
1803         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
1804                 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i], mval, pressure);
1805                 if(alpha)
1806                         vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0f));
1807         }
1808 }
1809
1810 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1811 {
1812         ToolSettings *ts= CTX_data_tool_settings(C);
1813         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
1814         VPaint *vp= ts->vpaint;
1815         Brush *brush = paint_brush(&vp->paint);
1816         ViewContext *vc= &vpd->vc;
1817         Object *ob= vc->obact;
1818         Mesh *me= ob->data;
1819         float mat[4][4];
1820         int *indexar= vpd->indexar;
1821         int totindex, index, flip;
1822         float pressure, mval[2];
1823
1824         RNA_float_get_array(itemptr, "mouse", mval);
1825         flip = RNA_boolean_get(itemptr, "pen_flip");
1826         pressure = RNA_float_get(itemptr, "pressure");
1827                         
1828         view3d_operator_needs_opengl(C);
1829                         
1830         /* load projection matrix */
1831         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1832
1833         mval[0]-= vc->ar->winrct.xmin;
1834         mval[1]-= vc->ar->winrct.ymin;
1835
1836                         
1837         /* which faces are involved */
1838         if(vp->flag & VP_AREA) {
1839                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1840         }
1841         else {
1842                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1843                 if(indexar[0]) totindex= 1;
1844                 else totindex= 0;
1845         }
1846                         
1847         swap_m4m4(vc->rv3d->persmat, mat);
1848                         
1849         for(index=0; index<totindex; index++) {                         
1850                 if(indexar[index] && indexar[index]<=me->totface)
1851                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, pressure, flip);
1852         }
1853                                                 
1854         swap_m4m4(vc->rv3d->persmat, mat);
1855
1856         /* was disabled because it is slow, but necessary for blur */
1857         if(brush->vertexpaint_tool == VP_BLUR)
1858                 do_shared_vertexcol(me);
1859                         
1860         ED_region_tag_redraw(vc->ar);
1861                         
1862         DAG_id_tag_update(ob->data, 0);
1863 }
1864
1865 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1866 {
1867         ToolSettings *ts= CTX_data_tool_settings(C);
1868         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
1869         
1870         if(vpd->vertexcosnos)
1871                 MEM_freeN(vpd->vertexcosnos);
1872         MEM_freeN(vpd->indexar);
1873         
1874         /* frees prev buffer */
1875         copy_vpaint_prev(ts->vpaint, NULL, 0);
1876         
1877         MEM_freeN(vpd);
1878 }
1879
1880 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1881 {
1882         
1883         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
1884                                           vpaint_stroke_update_step,
1885                                           vpaint_stroke_done, event->type);
1886         
1887         /* add modal handler */
1888         WM_event_add_modal_handler(C, op);
1889
1890         op->type->modal(C, op, event);
1891         
1892         return OPERATOR_RUNNING_MODAL;
1893 }
1894
1895 void PAINT_OT_vertex_paint(wmOperatorType *ot)
1896 {
1897         /* identifiers */
1898         ot->name= "Vertex Paint";
1899         ot->idname= "PAINT_OT_vertex_paint";
1900         
1901         /* api callbacks */
1902         ot->invoke= vpaint_invoke;
1903         ot->modal= paint_stroke_modal;
1904         /* ot->exec= vpaint_exec; <-- needs stroke property */
1905         ot->poll= vertex_paint_poll;
1906         
1907         /* flags */
1908         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1909
1910         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1911 }
1912
1913 /* ********************** weight from bones operator ******************* */
1914
1915 static int weight_from_bones_poll(bContext *C)
1916 {
1917         Object *ob= CTX_data_active_object(C);
1918
1919         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
1920 }
1921
1922 static int weight_from_bones_exec(bContext *C, wmOperator *op)
1923 {
1924         Scene *scene= CTX_data_scene(C);
1925         Object *ob= CTX_data_active_object(C);
1926         Object *armob= modifiers_isDeformedByArmature(ob);
1927         Mesh *me= ob->data;
1928         int type= RNA_enum_get(op->ptr, "type");
1929
1930         create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
1931
1932         DAG_id_tag_update(&me->id, 0);
1933         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1934
1935         return OPERATOR_FINISHED;
1936 }
1937
1938 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
1939 {
1940         static EnumPropertyItem type_items[]= {
1941                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
1942                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
1943                 {0, NULL, 0, NULL, NULL}};
1944
1945         /* identifiers */
1946         ot->name= "Weight from Bones";
1947         ot->idname= "PAINT_OT_weight_from_bones";
1948         
1949         /* api callbacks */
1950         ot->exec= weight_from_bones_exec;
1951         ot->invoke= WM_menu_invoke;
1952         ot->poll= weight_from_bones_poll;
1953         
1954         /* flags */
1955         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1956
1957         /* properties */
1958         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights.");
1959 }
1960