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