~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to source/blender/src/editdeform.c

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: editdeform.c 16366 2008-09-04 20:51:28Z blendix $
 
3
 *
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software Foundation,
 
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 *
 
20
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
21
 * All rights reserved.
 
22
 *
 
23
 * The Original Code is: all of this file.
 
24
 *
 
25
 * Contributor(s): none yet.
 
26
 *
 
27
 * ***** END GPL LICENSE BLOCK *****
 
28
 * Creator-specific support for vertex deformation groups
 
29
 * Added: apply deform function (ton)
 
30
 */
 
31
 
 
32
#include <string.h>
 
33
 
 
34
#include "MEM_guardedalloc.h"
 
35
 
 
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_particle_types.h"
 
45
 
 
46
#include "BLI_blenlib.h"
 
47
#include "BLI_editVert.h"
 
48
 
 
49
#include "BKE_customdata.h"
 
50
#include "BKE_DerivedMesh.h"
 
51
#include "BKE_depsgraph.h"
 
52
#include "BKE_deform.h"
 
53
#include "BKE_displist.h"
 
54
#include "BKE_global.h"
 
55
#include "BKE_lattice.h"
 
56
#include "BKE_mesh.h"
 
57
#include "BKE_utildefines.h"
 
58
 
 
59
#include "BIF_interface.h"
 
60
#include "BIF_editdeform.h"
 
61
#include "BIF_editmesh.h"
 
62
#include "BIF_space.h"
 
63
#include "BIF_toolbox.h"
 
64
 
 
65
#include "BSE_edit.h"
 
66
 
 
67
#include "butspace.h"
 
68
#include "mydevice.h"
 
69
#include "editmesh.h"
 
70
#include "multires.h"
 
71
 
 
72
#ifdef HAVE_CONFIG_H
 
73
#include <config.h>
 
74
#endif
 
75
 
 
76
/* only in editmode */
 
77
void sel_verts_defgroup (int select)
 
78
{
 
79
        EditVert *eve;
 
80
        Object *ob;
 
81
        int i;
 
82
        MDeformVert *dvert;
 
83
 
 
84
        ob= G.obedit;
 
85
 
 
86
        if (!ob)
 
87
                return;
 
88
 
 
89
        switch (ob->type){
 
90
        case OB_MESH:
 
91
                for (eve=G.editMesh->verts.first; eve; eve=eve->next){
 
92
                        dvert= CustomData_em_get(&G.editMesh->vdata, eve->data, CD_MDEFORMVERT);
 
93
 
 
94
                        if (dvert && dvert->totweight){
 
95
                                for (i=0; i<dvert->totweight; i++){
 
96
                                        if (dvert->dw[i].def_nr == (ob->actdef-1)){
 
97
                                                if (select) eve->f |= SELECT;
 
98
                                                else eve->f &= ~SELECT;
 
99
                                                
 
100
                                                break;
 
101
                                        }
 
102
                                }
 
103
                        }
 
104
                }
 
105
                /* this has to be called, because this function operates on vertices only */
 
106
                if(select) EM_select_flush();   // vertices to edges/faces
 
107
                else EM_deselect_flush();
 
108
                
 
109
                break;
 
110
        case OB_LATTICE:
 
111
                if(editLatt->dvert) {
 
112
                        BPoint *bp;
 
113
                        int a, tot;
 
114
                        
 
115
                        dvert= editLatt->dvert;
 
116
 
 
117
                        tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
 
118
                        for(a=0, bp= editLatt->def; a<tot; a++, bp++, dvert++) {
 
119
                                for (i=0; i<dvert->totweight; i++){
 
120
                                        if (dvert->dw[i].def_nr == (ob->actdef-1)) {
 
121
                                                if(select) bp->f1 |= SELECT;
 
122
                                                else bp->f1 &= ~SELECT;
 
123
                                                
 
124
                                                break;
 
125
                                        }
 
126
                                }
 
127
                        }
 
128
                }       
 
129
                break;
 
130
                
 
131
        default:
 
132
                break;
 
133
        }
 
134
        
 
135
        countall();
 
136
        
 
137
}
 
138
 
 
139
/* check if deform vertex has defgroup index */
 
140
MDeformWeight *get_defweight (MDeformVert *dv, int defgroup)
 
141
{
 
142
        int i;
 
143
        
 
144
        if (!dv || defgroup<0)
 
145
                return NULL;
 
146
        
 
147
        for (i=0; i<dv->totweight; i++){
 
148
                if (dv->dw[i].def_nr == defgroup)
 
149
                        return dv->dw+i;
 
150
        }
 
151
        return NULL;
 
152
}
 
153
 
 
154
/* Ensures that mv has a deform weight entry for
 
155
   the specified defweight group */
 
156
/* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
 
157
MDeformWeight *verify_defweight (MDeformVert *dv, int defgroup)
 
158
{
 
159
        MDeformWeight *newdw;
 
160
 
 
161
        /* do this check always, this function is used to check for it */
 
162
        if (!dv || defgroup<0)
 
163
                return NULL;
 
164
        
 
165
        newdw = get_defweight (dv, defgroup);
 
166
        if (newdw)
 
167
                return newdw;
 
168
        
 
169
        newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight");
 
170
        if (dv->dw){
 
171
                memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
 
172
                MEM_freeN (dv->dw);
 
173
        }
 
174
        dv->dw=newdw;
 
175
        
 
176
        dv->dw[dv->totweight].weight=0.0f;
 
177
        dv->dw[dv->totweight].def_nr=defgroup;
 
178
        /* Group index */
 
179
        
 
180
        dv->totweight++;
 
181
 
 
182
        return dv->dw+(dv->totweight-1);
 
183
}
 
184
 
 
185
void add_defgroup (Object *ob) 
 
186
{
 
187
        add_defgroup_name (ob, "Group");
 
188
}
 
189
 
 
190
bDeformGroup *add_defgroup_name (Object *ob, char *name)
 
191
{
 
192
        bDeformGroup    *defgroup;
 
193
        
 
194
        if (!ob)
 
195
                return NULL;
 
196
        
 
197
        defgroup = MEM_callocN (sizeof(bDeformGroup), "add deformGroup");
 
198
 
 
199
        BLI_strncpy (defgroup->name, name, 32);
 
200
 
 
201
        BLI_addtail(&ob->defbase, defgroup);
 
202
        unique_vertexgroup_name(defgroup, ob);
 
203
 
 
204
        ob->actdef = BLI_countlist(&ob->defbase);
 
205
 
 
206
        return defgroup;
 
207
}
 
208
 
 
209
void duplicate_defgroup ( Object *ob )
 
210
{
 
211
        bDeformGroup *dg, *cdg;
 
212
        char name[32], s[32];
 
213
        MDeformWeight *org, *cpy;
 
214
        MDeformVert *dvert;
 
215
        Mesh *me;
 
216
        int i, idg, icdg;
 
217
 
 
218
        if (ob->type != OB_MESH)
 
219
                return;
 
220
 
 
221
        dg = BLI_findlink (&ob->defbase, (ob->actdef-1));
 
222
        if (!dg)
 
223
                return;
 
224
        
 
225
        if (strstr(dg->name, "_copy")) {
 
226
                BLI_strncpy (name, dg->name, 32); /* will be renamed _copy.001... etc */
 
227
        } else {
 
228
                BLI_snprintf (name, 32, "%s_copy", dg->name);
 
229
                while (get_named_vertexgroup (ob, name)) {
 
230
                        if ((strlen (name) + 6) > 32) {
 
231
                                error ("Error: the name for the new group is > 32 characters");
 
232
                                return;
 
233
                        }
 
234
                        strcpy (s, name);
 
235
                        BLI_snprintf (name, 32, "%s_copy", s);
 
236
                }
 
237
        }               
 
238
 
 
239
        cdg = copy_defgroup (dg);
 
240
        strcpy (cdg->name, name);
 
241
        unique_vertexgroup_name(cdg, ob);
 
242
        
 
243
        BLI_addtail (&ob->defbase, cdg);
 
244
 
 
245
        idg = (ob->actdef-1);
 
246
        ob->actdef = BLI_countlist (&ob->defbase);
 
247
        icdg = (ob->actdef-1);
 
248
 
 
249
        me = get_mesh (ob);
 
250
        if (!me->dvert)
 
251
                return;
 
252
 
 
253
        for (i = 0; i < me->totvert; i++) {
 
254
                dvert = me->dvert+i;
 
255
                org = get_defweight (dvert, idg);
 
256
                if (org) {
 
257
                        cpy = verify_defweight (dvert, icdg);
 
258
                        cpy->weight = org->weight;
 
259
                }
 
260
        }
 
261
}
 
262
 
 
263
static void del_defgroup_update_users(Object *ob, int id)
 
264
{
 
265
        ExplodeModifierData *emd;
 
266
        ModifierData *md;
 
267
        ParticleSystem *psys;
 
268
        ClothModifierData *clmd;
 
269
        ClothSimSettings *clsim;
 
270
        int a;
 
271
 
 
272
        /* these cases don't use names to refer to vertex groups, so when
 
273
         * they get deleted the numbers get out of sync, this corrects that */
 
274
 
 
275
        if(ob->soft) {
 
276
                if(ob->soft->vertgroup == id)
 
277
                        ob->soft->vertgroup= 0;
 
278
                else if(ob->soft->vertgroup > id)
 
279
                        ob->soft->vertgroup--;
 
280
        }
 
281
 
 
282
        for(md=ob->modifiers.first; md; md=md->next) {
 
283
                if(md->type == eModifierType_Explode) {
 
284
                        emd= (ExplodeModifierData*)md;
 
285
 
 
286
                        if(emd->vgroup == id)
 
287
                                emd->vgroup= 0;
 
288
                        else if(emd->vgroup > id)
 
289
                                emd->vgroup--;
 
290
                }
 
291
                else if(md->type == eModifierType_Cloth) {
 
292
                        clmd= (ClothModifierData*)md;
 
293
                        clsim= clmd->sim_parms;
 
294
 
 
295
                        if(clsim) {
 
296
                                if(clsim->vgroup_mass == id)
 
297
                                        clsim->vgroup_mass= 0;
 
298
                                else if(clsim->vgroup_mass > id)
 
299
                                        clsim->vgroup_mass--;
 
300
 
 
301
                                if(clsim->vgroup_bend == id)
 
302
                                        clsim->vgroup_bend= 0;
 
303
                                else if(clsim->vgroup_bend > id)
 
304
                                        clsim->vgroup_bend--;
 
305
 
 
306
                                if(clsim->vgroup_struct == id)
 
307
                                        clsim->vgroup_struct= 0;
 
308
                                else if(clsim->vgroup_struct > id)
 
309
                                        clsim->vgroup_struct--;
 
310
                        }
 
311
                }
 
312
        }
 
313
 
 
314
        for(psys=ob->particlesystem.first; psys; psys=psys->next) {
 
315
                for(a=0; a<PSYS_TOT_VG; a++)
 
316
                        if(psys->vgroup[a] == id)
 
317
                                psys->vgroup[a]= 0;
 
318
                        else if(psys->vgroup[a] > id)
 
319
                                psys->vgroup[a]--;
 
320
        }
 
321
}
 
322
 
 
323
void del_defgroup_in_object_mode ( Object *ob )
 
324
{
 
325
        bDeformGroup *dg;
 
326
        MDeformVert *dvert;
 
327
        Mesh *me;
 
328
        int i, e;
 
329
 
 
330
        if ((!ob) || (ob->type != OB_MESH))
 
331
                return;
 
332
 
 
333
        dg = BLI_findlink (&ob->defbase, (ob->actdef-1));
 
334
        if (!dg)
 
335
                return;
 
336
 
 
337
        me = get_mesh (ob);
 
338
        if (me->dvert) {
 
339
                for (i = 0; i < me->totvert; i++) {
 
340
                        dvert = me->dvert + i;
 
341
                        if (dvert) {
 
342
                                if (get_defweight (dvert, (ob->actdef-1)))
 
343
                                        remove_vert_defgroup (ob, dg, i);
 
344
                        }
 
345
                }
 
346
 
 
347
                for (i = 0; i < me->totvert; i++) {
 
348
                        dvert = me->dvert+i;
 
349
                        if (dvert) {
 
350
                                for (e = 0; e < dvert->totweight; e++) {
 
351
                                        if (dvert->dw[e].def_nr > (ob->actdef-1))
 
352
                                                dvert->dw[e].def_nr--;
 
353
                                }
 
354
                        }
 
355
                }
 
356
        }
 
357
 
 
358
        del_defgroup_update_users(ob, ob->actdef);
 
359
 
 
360
        /* Update the active deform index if necessary */
 
361
        if (ob->actdef == BLI_countlist(&ob->defbase))
 
362
                ob->actdef--;
 
363
  
 
364
        /* Remove the group */
 
365
        BLI_freelinkN (&ob->defbase, dg);
 
366
}
 
367
 
 
368
void del_defgroup (Object *ob)
 
369
{
 
370
        bDeformGroup    *defgroup;
 
371
        int                             i;
 
372
 
 
373
        if (!ob)
 
374
                return;
 
375
 
 
376
        if (!ob->actdef)
 
377
                return;
 
378
 
 
379
        defgroup = BLI_findlink(&ob->defbase, ob->actdef-1);
 
380
        if (!defgroup)
 
381
                return;
 
382
 
 
383
        /* Make sure that no verts are using this group */
 
384
        remove_verts_defgroup(1);
 
385
 
 
386
        /* Make sure that any verts with higher indices are adjusted accordingly */
 
387
        if(ob->type==OB_MESH) {
 
388
                EditMesh *em = G.editMesh;
 
389
                EditVert *eve;
 
390
                MDeformVert *dvert;
 
391
                
 
392
                for (eve=em->verts.first; eve; eve=eve->next){
 
393
                        dvert= CustomData_em_get(&G.editMesh->vdata, eve->data, CD_MDEFORMVERT);
 
394
 
 
395
                        if (dvert)
 
396
                                for (i=0; i<dvert->totweight; i++)
 
397
                                        if (dvert->dw[i].def_nr > (ob->actdef-1))
 
398
                                                dvert->dw[i].def_nr--;
 
399
                }
 
400
        }
 
401
        else {
 
402
                BPoint *bp;
 
403
                MDeformVert *dvert= editLatt->dvert;
 
404
                int a, tot;
 
405
                
 
406
                if (dvert) {
 
407
                        tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
 
408
                        for(a=0, bp= editLatt->def; a<tot; a++, bp++, dvert++) {
 
409
                                for (i=0; i<dvert->totweight; i++){
 
410
                                        if (dvert->dw[i].def_nr > (ob->actdef-1))
 
411
                                                dvert->dw[i].def_nr--;
 
412
                                }
 
413
                        }
 
414
                }
 
415
        }
 
416
 
 
417
        del_defgroup_update_users(ob, ob->actdef);
 
418
 
 
419
        /* Update the active deform index if necessary */
 
420
        if (ob->actdef==BLI_countlist(&ob->defbase))
 
421
                ob->actdef--;
 
422
        
 
423
        /* Remove the group */
 
424
        BLI_freelinkN (&ob->defbase, defgroup);
 
425
        
 
426
        /* remove all dverts */
 
427
        if(ob->actdef==0) {
 
428
                if(ob->type==OB_MESH) {
 
429
                        Mesh *me= ob->data;
 
430
                        CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
 
431
                        me->dvert= NULL;
 
432
                }
 
433
                else {
 
434
                        if (editLatt->dvert) {
 
435
                                MEM_freeN(editLatt->dvert);
 
436
                                editLatt->dvert= NULL;
 
437
                        }
 
438
                }
 
439
        }
 
440
}
 
441
 
 
442
void create_dverts(ID *id)
 
443
{
 
444
        /* create deform verts
 
445
         */
 
446
 
 
447
        if( GS(id->name)==ID_ME) {
 
448
                Mesh *me= (Mesh *)id;
 
449
                me->dvert= CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert);
 
450
        }
 
451
        else if( GS(id->name)==ID_LT) {
 
452
                Lattice *lt= (Lattice *)id;
 
453
                lt->dvert= MEM_callocN(sizeof(MDeformVert)*lt->pntsu*lt->pntsv*lt->pntsw, "lattice deformVert");
 
454
        }
 
455
}
 
456
 
 
457
/* for mesh in object mode
 
458
   lattice can be in editmode */
 
459
void remove_vert_def_nr (Object *ob, int def_nr, int vertnum)
 
460
{
 
461
        /* This routine removes the vertex from the deform
 
462
         * group with number def_nr.
 
463
         *
 
464
         * This routine is meant to be fast, so it is the
 
465
         * responsibility of the calling routine to:
 
466
         *   a) test whether ob is non-NULL
 
467
         *   b) test whether ob is a mesh
 
468
         *   c) calculate def_nr
 
469
         */
 
470
 
 
471
        MDeformWeight *newdw;
 
472
        MDeformVert *dvert= NULL;
 
473
        int i;
 
474
 
 
475
        /* get the deform vertices corresponding to the
 
476
         * vertnum
 
477
         */
 
478
        if(ob->type==OB_MESH) {
 
479
                if( ((Mesh*)ob->data)->dvert )
 
480
                        dvert = ((Mesh*)ob->data)->dvert + vertnum;
 
481
        }
 
482
        else if(ob->type==OB_LATTICE) {
 
483
                Lattice *lt= ob->data;
 
484
                
 
485
                if(ob==G.obedit)
 
486
                        lt= editLatt;
 
487
                
 
488
                if(lt->dvert)
 
489
                        dvert = lt->dvert + vertnum;
 
490
        }
 
491
        
 
492
        if(dvert==NULL)
 
493
                return;
 
494
        
 
495
        /* for all of the deform weights in the
 
496
         * deform vert
 
497
         */
 
498
        for (i=dvert->totweight - 1 ; i>=0 ; i--){
 
499
 
 
500
                /* if the def_nr is the same as the one
 
501
                 * for our weight group then remove it
 
502
                 * from this deform vert.
 
503
                 */
 
504
                if (dvert->dw[i].def_nr == def_nr) {
 
505
                        dvert->totweight--;
 
506
        
 
507
                        /* if there are still other deform weights
 
508
                         * attached to this vert then remove this
 
509
                         * deform weight, and reshuffle the others
 
510
                         */
 
511
                        if (dvert->totweight) {
 
512
                                newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight), 
 
513
                                                                         "deformWeight");
 
514
                                if (dvert->dw){
 
515
                                        memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i);
 
516
                                        memcpy (newdw+i, dvert->dw+i+1, 
 
517
                                                        sizeof(MDeformWeight)*(dvert->totweight-i));
 
518
                                        MEM_freeN (dvert->dw);
 
519
                                }
 
520
                                dvert->dw=newdw;
 
521
                        }
 
522
                        /* if there are no other deform weights
 
523
                         * left then just remove the deform weight
 
524
                         */
 
525
                        else {
 
526
                                MEM_freeN (dvert->dw);
 
527
                                dvert->dw = NULL;
 
528
                                break;
 
529
                        }
 
530
                }
 
531
        }
 
532
 
 
533
}
 
534
 
 
535
/* for Mesh in Object mode */
 
536
/* allows editmode for Lattice */
 
537
void add_vert_defnr (Object *ob, int def_nr, int vertnum, 
 
538
                           float weight, int assignmode)
 
539
{
 
540
        /* add the vert to the deform group with the
 
541
         * specified number
 
542
         */
 
543
        MDeformVert *dv= NULL;
 
544
        MDeformWeight *newdw;
 
545
        int     i;
 
546
 
 
547
        /* get the vert */
 
548
        if(ob->type==OB_MESH) {
 
549
                if(((Mesh*)ob->data)->dvert)
 
550
                        dv = ((Mesh*)ob->data)->dvert + vertnum;
 
551
        }
 
552
        else if(ob->type==OB_LATTICE) {
 
553
                Lattice *lt;
 
554
                
 
555
                if(ob==G.obedit)
 
556
                        lt= editLatt;
 
557
                else
 
558
                        lt= ob->data;
 
559
                        
 
560
                if(lt->dvert)
 
561
                        dv = lt->dvert + vertnum;
 
562
        }
 
563
        
 
564
        if(dv==NULL)
 
565
                return;
 
566
        
 
567
        /* Lets first check to see if this vert is
 
568
         * already in the weight group -- if so
 
569
         * lets update it
 
570
         */
 
571
        for (i=0; i<dv->totweight; i++){
 
572
                
 
573
                /* if this weight cooresponds to the
 
574
                 * deform group, then add it using
 
575
                 * the assign mode provided
 
576
                 */
 
577
                if (dv->dw[i].def_nr == def_nr){
 
578
                        
 
579
                        switch (assignmode) {
 
580
                        case WEIGHT_REPLACE:
 
581
                                dv->dw[i].weight=weight;
 
582
                                break;
 
583
                        case WEIGHT_ADD:
 
584
                                dv->dw[i].weight+=weight;
 
585
                                if (dv->dw[i].weight >= 1.0)
 
586
                                        dv->dw[i].weight = 1.0;
 
587
                                break;
 
588
                        case WEIGHT_SUBTRACT:
 
589
                                dv->dw[i].weight-=weight;
 
590
                                /* if the weight is zero or less then
 
591
                                 * remove the vert from the deform group
 
592
                                 */
 
593
                                if (dv->dw[i].weight <= 0.0)
 
594
                                        remove_vert_def_nr(ob, def_nr, vertnum);
 
595
                                break;
 
596
                        }
 
597
                        return;
 
598
                }
 
599
        }
 
600
 
 
601
        /* if the vert wasn't in the deform group then
 
602
         * we must take a different form of action ...
 
603
         */
 
604
 
 
605
        switch (assignmode) {
 
606
        case WEIGHT_SUBTRACT:
 
607
                /* if we are subtracting then we don't
 
608
                 * need to do anything
 
609
                 */
 
610
                return;
 
611
 
 
612
        case WEIGHT_REPLACE:
 
613
        case WEIGHT_ADD:
 
614
                /* if we are doing an additive assignment, then
 
615
                 * we need to create the deform weight
 
616
                 */
 
617
                newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1), 
 
618
                                                         "deformWeight");
 
619
                if (dv->dw){
 
620
                        memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
 
621
                        MEM_freeN (dv->dw);
 
622
                }
 
623
                dv->dw=newdw;
 
624
    
 
625
                dv->dw[dv->totweight].weight=weight;
 
626
                dv->dw[dv->totweight].def_nr=def_nr;
 
627
    
 
628
                dv->totweight++;
 
629
                break;
 
630
        }
 
631
}
 
632
 
 
633
/* called while not in editmode */
 
634
void add_vert_to_defgroup (Object *ob, bDeformGroup *dg, int vertnum, 
 
635
                           float weight, int assignmode)
 
636
{
 
637
        /* add the vert to the deform group with the
 
638
         * specified assign mode
 
639
         */
 
640
        int     def_nr;
 
641
 
 
642
        /* get the deform group number, exit if
 
643
         * it can't be found
 
644
         */
 
645
        def_nr = get_defgroup_num(ob, dg);
 
646
        if (def_nr < 0) return;
 
647
 
 
648
        /* if there's no deform verts then
 
649
         * create some
 
650
         */
 
651
        if(ob->type==OB_MESH) {
 
652
                if (!((Mesh*)ob->data)->dvert)
 
653
                        create_dverts(ob->data);
 
654
        }
 
655
        else if(ob->type==OB_LATTICE) {
 
656
                if (!((Lattice*)ob->data)->dvert)
 
657
                        create_dverts(ob->data);
 
658
        }
 
659
 
 
660
        /* call another function to do the work
 
661
         */
 
662
        add_vert_defnr (ob, def_nr, vertnum, weight, assignmode);
 
663
}
 
664
 
 
665
/* Only available in editmode */
 
666
void assign_verts_defgroup (void)
 
667
{
 
668
        extern float editbutvweight;    /* buttons.c */
 
669
        Object *ob;
 
670
        EditVert *eve;
 
671
        bDeformGroup *dg, *eg;
 
672
        MDeformWeight *newdw;
 
673
        MDeformVert *dvert;
 
674
        int     i, done;
 
675
        
 
676
        if(multires_level1_test()) return;
 
677
 
 
678
        ob= G.obedit;
 
679
 
 
680
        if (!ob)
 
681
                return;
 
682
 
 
683
        dg=BLI_findlink(&ob->defbase, ob->actdef-1);
 
684
        if (!dg){
 
685
                error ("No vertex group is active");
 
686
                return;
 
687
        }
 
688
 
 
689
        switch (ob->type){
 
690
        case OB_MESH:
 
691
                if (!CustomData_has_layer(&G.editMesh->vdata, CD_MDEFORMVERT))
 
692
                        EM_add_data_layer(&G.editMesh->vdata, CD_MDEFORMVERT);
 
693
 
 
694
                /* Go through the list of editverts and assign them */
 
695
                for (eve=G.editMesh->verts.first; eve; eve=eve->next){
 
696
                        dvert= CustomData_em_get(&G.editMesh->vdata, eve->data, CD_MDEFORMVERT);
 
697
 
 
698
                        if (dvert && (eve->f & 1)){
 
699
                                done=0;
 
700
                                /* See if this vert already has a reference to this group */
 
701
                                /*              If so: Change its weight */
 
702
                                done=0;
 
703
                                for (i=0; i<dvert->totweight; i++){
 
704
                                        eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
 
705
                                        /* Find the actual group */
 
706
                                        if (eg==dg){
 
707
                                                dvert->dw[i].weight=editbutvweight;
 
708
                                                done=1;
 
709
                                                break;
 
710
                                        }
 
711
                                }
 
712
                                /*              If not: Add the group and set its weight */
 
713
                                if (!done){
 
714
                                        newdw = MEM_callocN (sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
 
715
                                        if (dvert->dw){
 
716
                                                memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
 
717
                                                MEM_freeN (dvert->dw);
 
718
                                        }
 
719
                                        dvert->dw=newdw;
 
720
 
 
721
                                        dvert->dw[dvert->totweight].weight= editbutvweight;
 
722
                                        dvert->dw[dvert->totweight].def_nr= ob->actdef-1;
 
723
 
 
724
                                        dvert->totweight++;
 
725
 
 
726
                                }
 
727
                        }
 
728
                }
 
729
                break;
 
730
        case OB_LATTICE:
 
731
                {
 
732
                        BPoint *bp;
 
733
                        int a, tot;
 
734
                        
 
735
                        if(editLatt->dvert==NULL)
 
736
                                create_dverts(&editLatt->id);
 
737
                        
 
738
                        tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
 
739
                        for(a=0, bp= editLatt->def; a<tot; a++, bp++) {
 
740
                                if(bp->f1 & SELECT)
 
741
                                        add_vert_defnr (ob, ob->actdef-1, a, editbutvweight, WEIGHT_REPLACE);
 
742
                        }
 
743
                }       
 
744
                break;
 
745
        default:
 
746
                printf ("Assigning deformation groups to unknown object type\n");
 
747
                break;
 
748
        }
 
749
 
 
750
}
 
751
 
 
752
/* mesh object mode, lattice can be in editmode */
 
753
void remove_vert_defgroup (Object *ob, bDeformGroup     *dg, int vertnum)
 
754
{
 
755
        /* This routine removes the vertex from the specified
 
756
         * deform group.
 
757
         */
 
758
 
 
759
        int def_nr;
 
760
 
 
761
        /* if the object is NULL abort
 
762
         */
 
763
        if (!ob)
 
764
                return;
 
765
 
 
766
        /* get the deform number that cooresponds
 
767
         * to this deform group, and abort if it
 
768
         * can not be found.
 
769
         */
 
770
        def_nr = get_defgroup_num(ob, dg);
 
771
        if (def_nr < 0) return;
 
772
 
 
773
        /* call another routine to do the work
 
774
         */
 
775
        remove_vert_def_nr (ob, def_nr, vertnum);
 
776
}
 
777
 
 
778
/* for mesh in object mode lattice can be in editmode */
 
779
static float get_vert_def_nr (Object *ob, int def_nr, int vertnum)
 
780
{
 
781
        MDeformVert *dvert= NULL;
 
782
        int i;
 
783
 
 
784
        /* get the deform vertices corresponding to the
 
785
         * vertnum
 
786
         */
 
787
        if(ob->type==OB_MESH) {
 
788
                if( ((Mesh*)ob->data)->dvert )
 
789
                        dvert = ((Mesh*)ob->data)->dvert + vertnum;
 
790
        }
 
791
        else if(ob->type==OB_LATTICE) {
 
792
                Lattice *lt= ob->data;
 
793
                
 
794
                if(ob==G.obedit)
 
795
                        lt= editLatt;
 
796
                
 
797
                if(lt->dvert)
 
798
                        dvert = lt->dvert + vertnum;
 
799
        }
 
800
        
 
801
        if(dvert==NULL)
 
802
                return 0.0f;
 
803
        
 
804
        for(i=dvert->totweight-1 ; i>=0 ; i--)
 
805
                if(dvert->dw[i].def_nr == def_nr)
 
806
                        return dvert->dw[i].weight;
 
807
 
 
808
        return 0.0f;
 
809
}
 
810
 
 
811
/* mesh object mode, lattice can be in editmode */
 
812
float get_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum)
 
813
{
 
814
        int def_nr;
 
815
 
 
816
        if(!ob)
 
817
                return 0.0f;
 
818
 
 
819
        def_nr = get_defgroup_num(ob, dg);
 
820
        if(def_nr < 0) return 0.0f;
 
821
 
 
822
        return get_vert_def_nr (ob, def_nr, vertnum);
 
823
}
 
824
 
 
825
/* Only available in editmode */
 
826
/* removes from active defgroup, if allverts==0 only selected vertices */
 
827
void remove_verts_defgroup (int allverts)
 
828
{
 
829
        Object *ob;
 
830
        EditVert *eve;
 
831
        MDeformVert *dvert;
 
832
        MDeformWeight *newdw;
 
833
        bDeformGroup *dg, *eg;
 
834
        int     i;
 
835
        
 
836
        if(multires_level1_test()) return;
 
837
 
 
838
        ob= G.obedit;
 
839
 
 
840
        if (!ob)
 
841
                return;
 
842
 
 
843
        dg=BLI_findlink(&ob->defbase, ob->actdef-1);
 
844
        if (!dg){
 
845
                error ("No vertex group is active");
 
846
                return;
 
847
        }
 
848
 
 
849
        switch (ob->type){
 
850
        case OB_MESH:
 
851
                for (eve=G.editMesh->verts.first; eve; eve=eve->next){
 
852
                        dvert= CustomData_em_get(&G.editMesh->vdata, eve->data, CD_MDEFORMVERT);
 
853
                
 
854
                        if (dvert && dvert->dw && ((eve->f & 1) || allverts)){
 
855
                                for (i=0; i<dvert->totweight; i++){
 
856
                                        /* Find group */
 
857
                                        eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
 
858
                                        if (eg == dg){
 
859
                                                dvert->totweight--;
 
860
                                                if (dvert->totweight){
 
861
                                                        newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight), "deformWeight");
 
862
                                                        
 
863
                                                        if (dvert->dw){
 
864
                                                                memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i);
 
865
                                                                memcpy (newdw+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
 
866
                                                                MEM_freeN (dvert->dw);
 
867
                                                        }
 
868
                                                        dvert->dw=newdw;
 
869
                                                }
 
870
                                                else{
 
871
                                                        MEM_freeN (dvert->dw);
 
872
                                                        dvert->dw=NULL;
 
873
                                                        break;
 
874
                                                }
 
875
                                        }
 
876
                                }
 
877
                        }
 
878
                }
 
879
                break;
 
880
        case OB_LATTICE:
 
881
                
 
882
                if(editLatt->dvert) {
 
883
                        BPoint *bp;
 
884
                        int a, tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
 
885
                                
 
886
                        for(a=0, bp= editLatt->def; a<tot; a++, bp++) {
 
887
                                if(allverts || (bp->f1 & SELECT))
 
888
                                        remove_vert_defgroup (ob, dg, a);
 
889
                        }
 
890
                }
 
891
                break;
 
892
                
 
893
        default:
 
894
                printf ("Removing deformation groups from unknown object type\n");
 
895
                break;
 
896
        }
 
897
}
 
898
 
 
899
/* Only available in editmode */
 
900
/* removes from all defgroup, if allverts==0 only selected vertices */
 
901
void remove_verts_defgroups(int allverts)
 
902
{
 
903
        Object *ob;
 
904
        int actdef, defCount;
 
905
        
 
906
        if (multires_level1_test()) return;
 
907
 
 
908
        ob= G.obedit;
 
909
        if (ob == NULL) return;
 
910
        
 
911
        actdef= ob->actdef;
 
912
        defCount= BLI_countlist(&ob->defbase);
 
913
        
 
914
        if (defCount == 0) {
 
915
                error("Object has no vertex groups");
 
916
                return;
 
917
        }
 
918
        
 
919
        /* To prevent code redundancy, we just use remove_verts_defgroup, but that
 
920
         * only operates on the active vgroup. So we iterate through all groups, by changing
 
921
         * active group index
 
922
         */
 
923
        for (ob->actdef= 1; ob->actdef <= defCount; ob->actdef++)
 
924
                remove_verts_defgroup(allverts);
 
925
                
 
926
        ob->actdef= actdef;
 
927
}
 
928
 
 
929
void vertexgroup_select_by_name(Object *ob, char *name)
 
930
{
 
931
        bDeformGroup *curdef;
 
932
        int actdef= 1;
 
933
        
 
934
        if(ob==NULL) return;
 
935
        
 
936
        for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++){
 
937
                if (!strcmp(curdef->name, name)) {
 
938
                        ob->actdef= actdef;
 
939
                        return;
 
940
                }
 
941
        }
 
942
        ob->actdef=0;   // this signals on painting to create a new one, if a bone in posemode is selected */
 
943
}
 
944
 
 
945
/* This function provides a shortcut for adding/removing verts from 
 
946
 * vertex groups. It is called by the Ctrl-G hotkey in EditMode for Meshes
 
947
 * and Lattices. (currently only restricted to those two)
 
948
 * It is only responsible for 
 
949
 */
 
950
void vgroup_assign_with_menu(void)
 
951
{
 
952
        Object *ob= G.obedit;
 
953
        int defCount;
 
954
        int mode;
 
955
        
 
956
        /* prevent crashes */
 
957
        if (ob==NULL) return;
 
958
        
 
959
        defCount= BLI_countlist(&ob->defbase);
 
960
        
 
961
        /* give user choices of adding to current/new or removing from current */
 
962
        if (defCount && ob->actdef)
 
963
                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");
 
964
        else
 
965
                mode= pupmenu("Vertex Groups %t|Add Selected to New Group %x1");
 
966
        
 
967
        /* handle choices */
 
968
        switch (mode) {
 
969
                case 1: /* add to new group */
 
970
                        add_defgroup(ob);
 
971
                        assign_verts_defgroup();
 
972
                        allqueue(REDRAWVIEW3D, 1);
 
973
                        BIF_undo_push("Assign to vertex group");
 
974
                        break;
 
975
                case 2: /* add to current group */
 
976
                        assign_verts_defgroup();
 
977
                        allqueue(REDRAWVIEW3D, 1);
 
978
                        BIF_undo_push("Assign to vertex group");
 
979
                        break;
 
980
                case 3: /* remove from current group */
 
981
                        remove_verts_defgroup(0);
 
982
                        allqueue(REDRAWVIEW3D, 1);
 
983
                        BIF_undo_push("Remove from vertex group");
 
984
                        break;
 
985
                case 4: /* remove from all groups */
 
986
                        remove_verts_defgroups(0);
 
987
                        allqueue(REDRAWVIEW3D, 1);
 
988
                        BIF_undo_push("Remove from all vertex groups");
 
989
                        break;
 
990
        }
 
991
}
 
992
 
 
993
/* This function provides a shortcut for commonly used vertex group 
 
994
 * functions - change weight (not implemented), change active group, delete active group,
 
995
 * when Ctrl-Shift-G is used in EditMode, for Meshes and Lattices (only for now).
 
996
 */
 
997
void vgroup_operation_with_menu(void)
 
998
{
 
999
        Object *ob= G.obedit;
 
1000
        int defCount;
 
1001
        int mode;
 
1002
        
 
1003
        /* prevent crashes and useless cases */
 
1004
        if (ob==NULL) return;
 
1005
        
 
1006
        defCount= BLI_countlist(&ob->defbase);
 
1007
        if (defCount == 0) return;
 
1008
        
 
1009
        /* give user choices of adding to current/new or removing from current */
 
1010
        if (ob->actdef)
 
1011
                mode = pupmenu("Vertex Groups %t|Change Active Group%x1|Delete Active Group%x2");
 
1012
        else
 
1013
                mode= pupmenu("Vertex Groups %t|Change Active Group%x1");
 
1014
        
 
1015
        /* handle choices */
 
1016
        switch (mode) {
 
1017
                case 1: /* change active group*/
 
1018
                        {
 
1019
                                char *menustr= get_vertexgroup_menustr(ob);
 
1020
                                short nr;
 
1021
                                
 
1022
                                if (menustr) {
 
1023
                                        nr= pupmenu(menustr);
 
1024
                                        
 
1025
                                        if ((nr >= 1) && (nr <= defCount)) 
 
1026
                                                ob->actdef= nr;
 
1027
                                                
 
1028
                                        MEM_freeN(menustr);
 
1029
                                }
 
1030
                                allqueue(REDRAWBUTSALL, 0);
 
1031
                        }
 
1032
                        break;
 
1033
                case 2: /* delete active group  */
 
1034
                        {
 
1035
                                del_defgroup(ob);
 
1036
                                allqueue (REDRAWVIEW3D, 1);
 
1037
                                allqueue(REDRAWOOPS, 0);
 
1038
                                BIF_undo_push("Delete vertex group");
 
1039
                        }
 
1040
                        break;
 
1041
        }
 
1042
}
 
1043
 
 
1044
/* ******************* other deform edit stuff ********** */
 
1045
 
 
1046
void object_apply_deform(Object *ob)
 
1047
{
 
1048
        notice("Apply Deformation now only availble in Modifier buttons");
 
1049
}
 
1050