4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Creator-specific support for vertex deformation groups
29 * Added: apply deform function (ton)
34 #include "MEM_guardedalloc.h"
36 #include "DNA_cloth_types.h"
37 #include "DNA_curve_types.h"
38 #include "DNA_lattice_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_object_force.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_particle_types.h"
46 #include "DNA_windowmanager_types.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_editVert.h"
51 #include "BKE_context.h"
52 #include "BKE_customdata.h"
53 #include "BKE_deform.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_lattice.h"
60 #include "BKE_utildefines.h"
62 #include "RNA_access.h"
68 #include "ED_view3d.h"
70 #include "object_intern.h"
73 static void BIF_undo_push() {}
74 static void error() {}
76 static Lattice *def_get_lattice(Object *ob)
78 if(ob->type==OB_LATTICE) {
79 Lattice *lt= ob->data;
87 /* only in editmode */
88 void sel_verts_defgroup (Object *obedit, int select)
104 EditMesh *em = BKE_mesh_get_editmesh(me);
106 for (eve=em->verts.first; eve; eve=eve->next){
107 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
109 if (dvert && dvert->totweight){
110 for (i=0; i<dvert->totweight; i++){
111 if (dvert->dw[i].def_nr == (ob->actdef-1)){
112 if (select) eve->f |= SELECT;
113 else eve->f &= ~SELECT;
120 /* this has to be called, because this function operates on vertices only */
121 if(select) EM_select_flush(em); // vertices to edges/faces
122 else EM_deselect_flush(em);
124 BKE_mesh_end_editmesh(me, em);
129 Lattice *lt= def_get_lattice(ob);
137 tot= lt->pntsu*lt->pntsv*lt->pntsw;
138 for(a=0, bp= lt->def; a<tot; a++, bp++, dvert++) {
139 for (i=0; i<dvert->totweight; i++){
140 if (dvert->dw[i].def_nr == (ob->actdef-1)) {
141 if(select) bp->f1 |= SELECT;
142 else bp->f1 &= ~SELECT;
157 /* check if deform vertex has defgroup index */
158 MDeformWeight *get_defweight (MDeformVert *dv, int defgroup)
162 if (!dv || defgroup<0)
165 for (i=0; i<dv->totweight; i++){
166 if (dv->dw[i].def_nr == defgroup)
172 /* Ensures that mv has a deform weight entry for
173 the specified defweight group */
174 /* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
175 MDeformWeight *verify_defweight (MDeformVert *dv, int defgroup)
177 MDeformWeight *newdw;
179 /* do this check always, this function is used to check for it */
180 if (!dv || defgroup<0)
183 newdw = get_defweight (dv, defgroup);
187 newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight");
189 memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
194 dv->dw[dv->totweight].weight=0.0f;
195 dv->dw[dv->totweight].def_nr=defgroup;
200 return dv->dw+(dv->totweight-1);
203 bDeformGroup *add_defgroup_name (Object *ob, char *name)
205 bDeformGroup *defgroup;
210 defgroup = MEM_callocN (sizeof(bDeformGroup), "add deformGroup");
212 BLI_strncpy (defgroup->name, name, 32);
214 BLI_addtail(&ob->defbase, defgroup);
215 unique_vertexgroup_name(defgroup, ob);
217 ob->actdef = BLI_countlist(&ob->defbase);
222 void add_defgroup (Object *ob)
224 add_defgroup_name (ob, "Group");
228 void duplicate_defgroup ( Object *ob )
230 bDeformGroup *dg, *cdg;
231 char name[32], s[32];
232 MDeformWeight *org, *cpy;
233 MDeformVert *dvert, *dvert_array;
234 int i, idg, icdg, dvert_tot;
236 if (ob->type != OB_MESH && ob->type != OB_LATTICE)
239 dg = BLI_findlink (&ob->defbase, (ob->actdef-1));
243 if (strstr(dg->name, "_copy")) {
244 BLI_strncpy (name, dg->name, 32); /* will be renamed _copy.001... etc */
246 BLI_snprintf (name, 32, "%s_copy", dg->name);
247 while (get_named_vertexgroup (ob, name)) {
248 if ((strlen (name) + 6) > 32) {
249 error ("Error: the name for the new group is > 32 characters");
253 BLI_snprintf (name, 32, "%s_copy", s);
257 cdg = copy_defgroup (dg);
258 strcpy (cdg->name, name);
259 unique_vertexgroup_name(cdg, ob);
261 BLI_addtail (&ob->defbase, cdg);
263 idg = (ob->actdef-1);
264 ob->actdef = BLI_countlist (&ob->defbase);
265 icdg = (ob->actdef-1);
267 if(ob->type == OB_MESH) {
268 Mesh *me = get_mesh (ob);
269 dvert_array= me->dvert;
270 dvert_tot= me->totvert;
272 else if (ob->type == OB_LATTICE) {
273 Lattice *lt= (Lattice *)ob->data;
274 dvert_array= lt->dvert;
275 dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw;
281 for (i = 0; i < dvert_tot; i++) {
282 dvert = dvert_array+i;
283 org = get_defweight (dvert, idg);
285 float weight = org->weight;
286 /* verify_defweight re-allocs org so need to store the weight first */
287 cpy = verify_defweight (dvert, icdg);
288 cpy->weight = weight;
293 static void del_defgroup_update_users(Object *ob, int id)
295 ExplodeModifierData *emd;
297 ParticleSystem *psys;
298 ClothModifierData *clmd;
299 ClothSimSettings *clsim;
302 /* these cases don't use names to refer to vertex groups, so when
303 * they get deleted the numbers get out of sync, this corrects that */
306 if(ob->soft->vertgroup == id)
307 ob->soft->vertgroup= 0;
308 else if(ob->soft->vertgroup > id)
309 ob->soft->vertgroup--;
312 for(md=ob->modifiers.first; md; md=md->next) {
313 if(md->type == eModifierType_Explode) {
314 emd= (ExplodeModifierData*)md;
316 if(emd->vgroup == id)
318 else if(emd->vgroup > id)
321 else if(md->type == eModifierType_Cloth) {
322 clmd= (ClothModifierData*)md;
323 clsim= clmd->sim_parms;
326 if(clsim->vgroup_mass == id)
327 clsim->vgroup_mass= 0;
328 else if(clsim->vgroup_mass > id)
329 clsim->vgroup_mass--;
331 if(clsim->vgroup_bend == id)
332 clsim->vgroup_bend= 0;
333 else if(clsim->vgroup_bend > id)
334 clsim->vgroup_bend--;
336 if(clsim->vgroup_struct == id)
337 clsim->vgroup_struct= 0;
338 else if(clsim->vgroup_struct > id)
339 clsim->vgroup_struct--;
344 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
345 for(a=0; a<PSYS_TOT_VG; a++)
346 if(psys->vgroup[a] == id)
348 else if(psys->vgroup[a] > id)
353 void del_defgroup_in_object_mode ( Object *ob )
356 MDeformVert *dvert_array, *dvert;
360 if ((!ob) || (ob->type != OB_MESH && ob->type != OB_LATTICE))
363 if(ob->type == OB_MESH) {
364 Mesh *me = get_mesh (ob);
365 dvert_array= me->dvert;
366 dvert_tot= me->totvert;
368 else if (ob->type == OB_LATTICE) {
369 Lattice *lt= (Lattice *)ob->data;
370 dvert_array= lt->dvert;
371 dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw;
374 dg = BLI_findlink (&ob->defbase, (ob->actdef-1));
379 for (i = 0; i < dvert_tot; i++) {
380 dvert = dvert_array + i;
382 if (get_defweight (dvert, (ob->actdef-1)))
383 remove_vert_defgroup (ob, dg, i);
387 for (i = 0; i < dvert_tot; i++) {
388 dvert = dvert_array+i;
390 for (e = 0; e < dvert->totweight; e++) {
391 if (dvert->dw[e].def_nr > (ob->actdef-1))
392 dvert->dw[e].def_nr--;
398 del_defgroup_update_users(ob, ob->actdef);
400 /* Update the active deform index if necessary */
401 if (ob->actdef == BLI_countlist(&ob->defbase))
404 /* Remove the group */
405 BLI_freelinkN (&ob->defbase, dg);
408 void del_defgroup (Object *ob)
410 bDeformGroup *defgroup;
419 defgroup = BLI_findlink(&ob->defbase, ob->actdef-1);
423 /* Make sure that no verts are using this group */
424 remove_verts_defgroup(ob, 1);
426 /* Make sure that any verts with higher indices are adjusted accordingly */
427 if(ob->type==OB_MESH) {
429 EditMesh *em = BKE_mesh_get_editmesh(me);
433 for (eve=em->verts.first; eve; eve=eve->next){
434 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
437 for (i=0; i<dvert->totweight; i++)
438 if (dvert->dw[i].def_nr > (ob->actdef-1))
439 dvert->dw[i].def_nr--;
441 BKE_mesh_end_editmesh(me, em);
443 else if(ob->type==OB_LATTICE) {
444 Lattice *lt= def_get_lattice(ob);
446 MDeformVert *dvert= lt->dvert;
450 tot= lt->pntsu*lt->pntsv*lt->pntsw;
451 for(a=0, bp= lt->def; a<tot; a++, bp++, dvert++) {
452 for (i=0; i<dvert->totweight; i++){
453 if (dvert->dw[i].def_nr > (ob->actdef-1))
454 dvert->dw[i].def_nr--;
460 del_defgroup_update_users(ob, ob->actdef);
462 /* Update the active deform index if necessary */
463 if (ob->actdef==BLI_countlist(&ob->defbase))
466 /* Remove the group */
467 BLI_freelinkN (&ob->defbase, defgroup);
469 /* remove all dverts */
471 if(ob->type==OB_MESH) {
473 CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
476 else if(ob->type==OB_LATTICE) {
477 Lattice *lt= def_get_lattice(ob);
479 MEM_freeN(lt->dvert);
486 void del_all_defgroups (Object *ob)
492 /* Remove all DVerts */
493 if (ob->type==OB_MESH) {
495 CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
498 else if(ob->type==OB_LATTICE) {
499 Lattice *lt= def_get_lattice(ob);
501 MEM_freeN(lt->dvert);
506 /* Remove all DefGroups */
507 BLI_freelistN(&ob->defbase);
509 /* Fix counters/indices */
513 void create_dverts(ID *id)
515 /* create deform verts
518 if( GS(id->name)==ID_ME) {
519 Mesh *me= (Mesh *)id;
520 me->dvert= CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert);
522 else if( GS(id->name)==ID_LT) {
523 Lattice *lt= (Lattice *)id;
524 lt->dvert= MEM_callocN(sizeof(MDeformVert)*lt->pntsu*lt->pntsv*lt->pntsw, "lattice deformVert");
528 /* for mesh in object mode
529 lattice can be in editmode */
530 void remove_vert_def_nr (Object *ob, int def_nr, int vertnum)
532 /* This routine removes the vertex from the deform
533 * group with number def_nr.
535 * This routine is meant to be fast, so it is the
536 * responsibility of the calling routine to:
537 * a) test whether ob is non-NULL
538 * b) test whether ob is a mesh
539 * c) calculate def_nr
542 MDeformWeight *newdw;
543 MDeformVert *dvert= NULL;
546 /* get the deform vertices corresponding to the
549 if(ob->type==OB_MESH) {
550 if( ((Mesh*)ob->data)->dvert )
551 dvert = ((Mesh*)ob->data)->dvert + vertnum;
553 else if(ob->type==OB_LATTICE) {
554 Lattice *lt= def_get_lattice(ob);
557 dvert = lt->dvert + vertnum;
563 /* for all of the deform weights in the
566 for (i=dvert->totweight - 1 ; i>=0 ; i--){
568 /* if the def_nr is the same as the one
569 * for our weight group then remove it
570 * from this deform vert.
572 if (dvert->dw[i].def_nr == def_nr) {
575 /* if there are still other deform weights
576 * attached to this vert then remove this
577 * deform weight, and reshuffle the others
579 if (dvert->totweight) {
580 newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight),
583 memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i);
584 memcpy (newdw+i, dvert->dw+i+1,
585 sizeof(MDeformWeight)*(dvert->totweight-i));
586 MEM_freeN (dvert->dw);
590 /* if there are no other deform weights
591 * left then just remove the deform weight
594 MEM_freeN (dvert->dw);
603 /* for Mesh in Object mode */
604 /* allows editmode for Lattice */
605 void add_vert_defnr (Object *ob, int def_nr, int vertnum,
606 float weight, int assignmode)
608 /* add the vert to the deform group with the
611 MDeformVert *dv= NULL;
612 MDeformWeight *newdw;
616 if(ob->type==OB_MESH) {
617 if(((Mesh*)ob->data)->dvert)
618 dv = ((Mesh*)ob->data)->dvert + vertnum;
620 else if(ob->type==OB_LATTICE) {
621 Lattice *lt= def_get_lattice(ob);
624 dv = lt->dvert + vertnum;
630 /* Lets first check to see if this vert is
631 * already in the weight group -- if so
634 for (i=0; i<dv->totweight; i++){
636 /* if this weight cooresponds to the
637 * deform group, then add it using
638 * the assign mode provided
640 if (dv->dw[i].def_nr == def_nr){
642 switch (assignmode) {
644 dv->dw[i].weight=weight;
647 dv->dw[i].weight+=weight;
648 if (dv->dw[i].weight >= 1.0)
649 dv->dw[i].weight = 1.0;
651 case WEIGHT_SUBTRACT:
652 dv->dw[i].weight-=weight;
653 /* if the weight is zero or less then
654 * remove the vert from the deform group
656 if (dv->dw[i].weight <= 0.0)
657 remove_vert_def_nr(ob, def_nr, vertnum);
664 /* if the vert wasn't in the deform group then
665 * we must take a different form of action ...
668 switch (assignmode) {
669 case WEIGHT_SUBTRACT:
670 /* if we are subtracting then we don't
671 * need to do anything
677 /* if we are doing an additive assignment, then
678 * we need to create the deform weight
680 newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1),
683 memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
688 dv->dw[dv->totweight].weight=weight;
689 dv->dw[dv->totweight].def_nr=def_nr;
696 /* called while not in editmode */
697 void add_vert_to_defgroup (Object *ob, bDeformGroup *dg, int vertnum,
698 float weight, int assignmode)
700 /* add the vert to the deform group with the
701 * specified assign mode
705 /* get the deform group number, exit if
708 def_nr = get_defgroup_num(ob, dg);
709 if (def_nr < 0) return;
711 /* if there's no deform verts then
714 if(ob->type==OB_MESH) {
715 if (!((Mesh*)ob->data)->dvert)
716 create_dverts(ob->data);
718 else if(ob->type==OB_LATTICE) {
719 if (!((Lattice*)ob->data)->dvert)
720 create_dverts(ob->data);
723 /* call another function to do the work
725 add_vert_defnr (ob, def_nr, vertnum, weight, assignmode);
728 /* Only available in editmode */
729 void assign_verts_defgroup (Object *ob, float weight)
732 bDeformGroup *dg, *eg;
733 MDeformWeight *newdw;
740 dg=BLI_findlink(&ob->defbase, ob->actdef-1);
742 error ("No vertex group is active");
750 EditMesh *em = BKE_mesh_get_editmesh(me);
752 if (!CustomData_has_layer(&em->vdata, CD_MDEFORMVERT))
753 EM_add_data_layer(em, &em->vdata, CD_MDEFORMVERT);
755 /* Go through the list of editverts and assign them */
756 for (eve=em->verts.first; eve; eve=eve->next){
757 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
759 if (dvert && (eve->f & 1)){
761 /* See if this vert already has a reference to this group */
762 /* If so: Change its weight */
764 for (i=0; i<dvert->totweight; i++){
765 eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
766 /* Find the actual group */
768 dvert->dw[i].weight= weight;
773 /* If not: Add the group and set its weight */
775 newdw = MEM_callocN (sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
777 memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
778 MEM_freeN (dvert->dw);
782 dvert->dw[dvert->totweight].weight= weight;
783 dvert->dw[dvert->totweight].def_nr= ob->actdef-1;
790 BKE_mesh_end_editmesh(me, em);
795 Lattice *lt= def_get_lattice(ob);
800 create_dverts(<->id);
802 tot= lt->pntsu*lt->pntsv*lt->pntsw;
803 for(a=0, bp= lt->def; a<tot; a++, bp++) {
805 add_vert_defnr (ob, ob->actdef-1, a, weight, WEIGHT_REPLACE);
810 printf ("Assigning deformation groups to unknown object type\n");
816 /* mesh object mode, lattice can be in editmode */
817 void remove_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum)
819 /* This routine removes the vertex from the specified
825 /* if the object is NULL abort
830 /* get the deform number that cooresponds
831 * to this deform group, and abort if it
834 def_nr = get_defgroup_num(ob, dg);
835 if (def_nr < 0) return;
837 /* call another routine to do the work
839 remove_vert_def_nr (ob, def_nr, vertnum);
842 /* for mesh in object mode lattice can be in editmode */
843 static float get_vert_def_nr (Object *ob, int def_nr, int vertnum)
845 MDeformVert *dvert= NULL;
848 /* get the deform vertices corresponding to the
851 if(ob->type==OB_MESH) {
852 if( ((Mesh*)ob->data)->dvert )
853 dvert = ((Mesh*)ob->data)->dvert + vertnum;
855 else if(ob->type==OB_LATTICE) {
856 Lattice *lt= def_get_lattice(ob);
859 dvert = lt->dvert + vertnum;
865 for(i=dvert->totweight-1 ; i>=0 ; i--)
866 if(dvert->dw[i].def_nr == def_nr)
867 return dvert->dw[i].weight;
872 /* mesh object mode, lattice can be in editmode */
873 float get_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum)
880 def_nr = get_defgroup_num(ob, dg);
881 if(def_nr < 0) return 0.0f;
883 return get_vert_def_nr (ob, def_nr, vertnum);
886 /* Only available in editmode */
887 /* removes from active defgroup, if allverts==0 only selected vertices */
888 void remove_verts_defgroup (Object *ob, int allverts)
892 MDeformWeight *newdw;
893 bDeformGroup *dg, *eg;
899 dg=BLI_findlink(&ob->defbase, ob->actdef-1);
901 error ("No vertex group is active");
909 EditMesh *em = BKE_mesh_get_editmesh(me);
911 for (eve=em->verts.first; eve; eve=eve->next){
912 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
914 if (dvert && dvert->dw && ((eve->f & 1) || allverts)){
915 for (i=0; i<dvert->totweight; i++){
917 eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
920 if (dvert->totweight){
921 newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight), "deformWeight");
924 memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i);
925 memcpy (newdw+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
926 MEM_freeN (dvert->dw);
931 MEM_freeN (dvert->dw);
939 BKE_mesh_end_editmesh(me, em);
944 Lattice *lt= def_get_lattice(ob);
948 int a, tot= lt->pntsu*lt->pntsv*lt->pntsw;
950 for(a=0, bp= lt->def; a<tot; a++, bp++) {
951 if(allverts || (bp->f1 & SELECT))
952 remove_vert_defgroup (ob, dg, a);
959 printf ("Removing deformation groups from unknown object type\n");
964 /* Only available in editmode */
965 /* removes from all defgroup, if allverts==0 only selected vertices */
966 void remove_verts_defgroups(Object *ob, int allverts)
968 int actdef, defCount;
970 if (ob == NULL) return;
973 defCount= BLI_countlist(&ob->defbase);
976 error("Object has no vertex groups");
980 /* To prevent code redundancy, we just use remove_verts_defgroup, but that
981 * only operates on the active vgroup. So we iterate through all groups, by changing
984 for (ob->actdef= 1; ob->actdef <= defCount; ob->actdef++)
985 remove_verts_defgroup(ob, allverts);
990 void vertexgroup_select_by_name(Object *ob, char *name)
992 bDeformGroup *curdef;
997 for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++){
998 if (!strcmp(curdef->name, name)) {
1003 ob->actdef=0; // this signals on painting to create a new one, if a bone in posemode is selected */
1006 /* This function provides a shortcut for adding/removing verts from
1007 * vertex groups. It is called by the Ctrl-G hotkey in EditMode for Meshes
1008 * and Lattices. (currently only restricted to those two)
1009 * It is only responsible for
1011 void vgroup_assign_with_menu(Scene *scene, Object *ob)
1013 VPaint *wp= scene->toolsettings->wpaint;
1017 /* prevent crashes */
1018 if (wp==NULL || ob==NULL) return;
1020 defCount= BLI_countlist(&ob->defbase);
1022 /* give user choices of adding to current/new or removing from current */
1023 // XXX if (defCount && ob->actdef)
1024 // mode = pupmenu("Vertex Groups %t|Add Selected to New Group %x1|Add Selected to Active Group %x2|Remove Selected from Active Group %x3|Remove Selected from All Groups %x4");
1026 // mode= pupmenu("Vertex Groups %t|Add Selected to New Group %x1");
1028 /* handle choices */
1030 case 1: /* add to new group */
1032 assign_verts_defgroup(ob, wp->brush->alpha);
1033 BIF_undo_push("Assign to vertex group");
1035 case 2: /* add to current group */
1036 assign_verts_defgroup(ob, wp->brush->alpha);
1037 BIF_undo_push("Assign to vertex group");
1039 case 3: /* remove from current group */
1040 remove_verts_defgroup(ob, 0);
1041 BIF_undo_push("Remove from vertex group");
1043 case 4: /* remove from all groups */
1044 remove_verts_defgroups(ob, 0);
1045 BIF_undo_push("Remove from all vertex groups");
1050 /* This function provides a shortcut for commonly used vertex group
1051 * functions - change weight (not implemented), change active group, delete active group,
1052 * when Ctrl-Shift-G is used in EditMode, for Meshes and Lattices (only for now).
1054 void vgroup_operation_with_menu(Object *ob)
1059 /* prevent crashes and useless cases */
1060 if (ob==NULL) return;
1062 defCount= BLI_countlist(&ob->defbase);
1063 if (defCount == 0) return;
1065 /* give user choices of adding to current/new or removing from current */
1066 // XXX if (ob->actdef)
1067 // mode = pupmenu("Vertex Groups %t|Change Active Group%x1|Delete Active Group%x2|Delete All Groups%x3");
1069 // mode= pupmenu("Vertex Groups %t|Change Active Group%x1|Delete All Groups%x3");
1071 /* handle choices */
1073 case 1: /* change active group*/
1075 char *menustr= NULL; // XXX get_vertexgroup_menustr(ob);
1079 // XXX nr= pupmenu(menustr);
1081 if ((nr >= 1) && (nr <= defCount))
1088 case 2: /* delete active group */
1091 BIF_undo_push("Delete vertex group");
1094 case 3: /* delete all groups */
1096 del_all_defgroups(ob);
1097 BIF_undo_push("Delete all vertex groups");
1103 /********************** vertex group operators *********************/
1105 static int vertex_group_add_exec(bContext *C, wmOperator *op)
1107 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1108 Scene *scene= CTX_data_scene(C);
1111 return OPERATOR_CANCELLED;
1114 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1115 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
1117 return OPERATOR_FINISHED;
1120 void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
1123 ot->name= "Add Vertex Group";
1124 ot->idname= "OBJECT_OT_vertex_group_add";
1127 ot->exec= vertex_group_add_exec;
1130 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1133 static int vertex_group_remove_exec(bContext *C, wmOperator *op)
1135 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1136 Scene *scene= CTX_data_scene(C);
1139 return OPERATOR_CANCELLED;
1141 if(scene->obedit == ob) {
1143 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
1146 del_defgroup_in_object_mode(ob);
1147 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1148 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
1151 return OPERATOR_FINISHED;
1154 void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
1157 ot->name= "Remove Vertex Group";
1158 ot->idname= "OBJECT_OT_vertex_group_remove";
1161 ot->exec= vertex_group_remove_exec;
1164 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1167 static int vertex_group_assign_exec(bContext *C, wmOperator *op)
1169 Scene *scene= CTX_data_scene(C);
1170 ToolSettings *ts= CTX_data_tool_settings(C);
1171 Object *ob= CTX_data_edit_object(C);
1174 return OPERATOR_CANCELLED;
1176 assign_verts_defgroup(ob, ts->vgroup_weight);
1177 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1178 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
1180 return OPERATOR_FINISHED;
1183 void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
1186 ot->name= "Assign Vertex Group";
1187 ot->idname= "OBJECT_OT_vertex_group_assign";
1190 ot->exec= vertex_group_assign_exec;
1193 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1196 static int vertex_group_remove_from_exec(bContext *C, wmOperator *op)
1198 Scene *scene= CTX_data_scene(C);
1199 Object *ob= CTX_data_edit_object(C);
1202 return OPERATOR_CANCELLED;
1204 remove_verts_defgroup(ob, 0);
1205 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1206 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
1208 return OPERATOR_FINISHED;
1211 void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
1214 ot->name= "Remove from Vertex Group";
1215 ot->idname= "OBJECT_OT_vertex_group_remove_from";
1218 ot->exec= vertex_group_remove_from_exec;
1221 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1224 static int vertex_group_select_exec(bContext *C, wmOperator *op)
1226 Object *ob= CTX_data_edit_object(C);
1229 return OPERATOR_CANCELLED;
1231 sel_verts_defgroup(ob, 1); /* runs countall() */
1232 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, ob);
1234 return OPERATOR_FINISHED;
1237 void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
1240 ot->name= "Select Vertex Group";
1241 ot->idname= "OBJECT_OT_vertex_group_select";
1244 ot->exec= vertex_group_select_exec;
1247 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1250 static int vertex_group_deselect_exec(bContext *C, wmOperator *op)
1252 Object *ob= CTX_data_edit_object(C);
1255 return OPERATOR_CANCELLED;
1257 sel_verts_defgroup(ob, 0); /* runs countall() */
1258 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, ob);
1260 return OPERATOR_FINISHED;
1263 void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
1266 ot->name= "Deselect Vertex Group";
1267 ot->idname= "OBJECT_OT_vertex_group_deselect";
1270 ot->exec= vertex_group_deselect_exec;
1273 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1276 static int vertex_group_copy_exec(bContext *C, wmOperator *op)
1278 Scene *scene= CTX_data_scene(C);
1279 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1282 return OPERATOR_CANCELLED;
1284 duplicate_defgroup(ob);
1285 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1286 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
1288 return OPERATOR_FINISHED;
1291 void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
1294 ot->name= "Copy Vertex Group";
1295 ot->idname= "OBJECT_OT_vertex_group_copy";
1298 ot->exec= vertex_group_copy_exec;
1301 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1304 static int vertex_group_copy_to_linked_exec(bContext *C, wmOperator *op)
1306 Scene *scene= CTX_data_scene(C);
1307 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1309 int retval= OPERATOR_CANCELLED;
1314 for(base=scene->base.first; base; base= base->next) {
1315 if(base->object->type==ob->type) {
1316 if(base->object!=ob && base->object->data==ob->data) {
1317 BLI_freelistN(&base->object->defbase);
1318 BLI_duplicatelist(&base->object->defbase, &ob->defbase);
1319 base->object->actdef= ob->actdef;
1321 DAG_object_flush_update(scene, base->object, OB_RECALC_DATA);
1322 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, base->object);
1324 retval = OPERATOR_FINISHED;
1332 void OBJECT_OT_vertex_group_copy_to_linked(wmOperatorType *ot)
1335 ot->name= "Copy Vertex Group to Linked";
1336 ot->idname= "OBJECT_OT_vertex_group_copy_to_linked";
1339 ot->exec= vertex_group_copy_to_linked_exec;
1342 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;