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