UV Editor: added proportional edit buttons to the header.
[blender.git] / source / blender / blenkernel / intern / customdata.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
22 *
23 * The Original Code is: all of this file.
24 *
25 * Contributor(s): Ben Batt <benbatt@gmail.com>
26 *
27 * ***** END GPL LICENSE BLOCK *****
28 *
29 * Implementation of CustomData.
30 *
31 * BKE_customdata.h contains the function prototypes for this file.
32 *
33 */ 
34
35 #include "BKE_customdata.h"
36 #include "BKE_utildefines.h" // CLAMP
37 #include "BLI_math.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_linklist.h"
40 #include "BLI_mempool.h"
41
42 #include "DNA_customdata_types.h"
43 #include "DNA_listBase.h"
44 #include "DNA_meshdata_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include <math.h>
49 #include <string.h>
50
51 /* number of layers to add when growing a CustomData object */
52 #define CUSTOMDATA_GROW 5
53
54 /********************* Layer type information **********************/
55 typedef struct LayerTypeInfo {
56         int size;          /* the memory size of one element of this layer's data */
57         char *structname;  /* name of the struct used, for file writing */
58         int structnum;     /* number of structs per element, for file writing */
59         char *defaultname; /* default layer name */
60
61         /* a function to copy count elements of this layer's data
62          * (deep copy if appropriate)
63          * if NULL, memcpy is used
64          */
65         void (*copy)(const void *source, void *dest, int count);
66
67         /* a function to free any dynamically allocated components of this
68          * layer's data (note the data pointer itself should not be freed)
69          * size should be the size of one element of this layer's data (e.g.
70          * LayerTypeInfo.size)
71          */
72         void (*free)(void *data, int count, int size);
73
74         /* a function to interpolate between count source elements of this
75          * layer's data and store the result in dest
76          * if weights == NULL or sub_weights == NULL, they should default to 1
77          *
78          * weights gives the weight for each element in sources
79          * sub_weights gives the sub-element weights for each element in sources
80          *    (there should be (sub element count)^2 weights per element)
81          * count gives the number of elements in sources
82          */
83         void (*interp)(void **sources, float *weights, float *sub_weights,
84                        int count, void *dest);
85
86     /* a function to swap the data in corners of the element */
87         void (*swap)(void *data, int *corner_indices);
88
89     /* a function to set a layer's data to default values. if NULL, the
90            default is assumed to be all zeros */
91         void (*set_default)(void *data, int count);
92 } LayerTypeInfo;
93
94 static void layerCopy_mdeformvert(const void *source, void *dest,
95                                   int count)
96 {
97         int i, size = sizeof(MDeformVert);
98
99         memcpy(dest, source, count * size);
100
101         for(i = 0; i < count; ++i) {
102                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
103
104                 if(dvert->totweight) {
105                         MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
106                                                                                         "layerCopy_mdeformvert dw");
107
108                         memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
109                         dvert->dw = dw;
110                 }
111                 else
112                         dvert->dw = NULL;
113         }
114 }
115
116 static void layerFree_mdeformvert(void *data, int count, int size)
117 {
118         int i;
119
120         for(i = 0; i < count; ++i) {
121                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
122
123                 if(dvert->dw) {
124                         MEM_freeN(dvert->dw);
125                         dvert->dw = NULL;
126                         dvert->totweight = 0;
127                 }
128         }
129 }
130
131 static void linklist_free_simple(void *link)
132 {
133         MEM_freeN(link);
134 }
135
136 static void layerInterp_mdeformvert(void **sources, float *weights,
137                                     float *sub_weights, int count, void *dest)
138 {
139         MDeformVert *dvert = dest;
140         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
141         LinkNode *node;
142         int i, j, totweight;
143
144         if(count <= 0) return;
145
146         /* build a list of unique def_nrs for dest */
147         totweight = 0;
148         for(i = 0; i < count; ++i) {
149                 MDeformVert *source = sources[i];
150                 float interp_weight = weights ? weights[i] : 1.0f;
151
152                 for(j = 0; j < source->totweight; ++j) {
153                         MDeformWeight *dw = &source->dw[j];
154
155                         for(node = dest_dw; node; node = node->next) {
156                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
157
158                                 if(tmp_dw->def_nr == dw->def_nr) {
159                                         tmp_dw->weight += dw->weight * interp_weight;
160                                         break;
161                                 }
162                         }
163
164                         /* if this def_nr is not in the list, add it */
165                         if(!node) {
166                                 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
167                                                             "layerInterp_mdeformvert tmp_dw");
168                                 tmp_dw->def_nr = dw->def_nr;
169                                 tmp_dw->weight = dw->weight * interp_weight;
170                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
171                                 totweight++;
172                         }
173                 }
174         }
175
176         /* now we know how many unique deform weights there are, so realloc */
177         if(dvert->dw) MEM_freeN(dvert->dw);
178
179         if(totweight) {
180                 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
181                                         "layerInterp_mdeformvert dvert->dw");
182                 dvert->totweight = totweight;
183
184                 for(i = 0, node = dest_dw; node; node = node->next, ++i)
185                         dvert->dw[i] = *((MDeformWeight *)node->link);
186         }
187         else
188                 memset(dvert, 0, sizeof(*dvert));
189
190         BLI_linklist_free(dest_dw, linklist_free_simple);
191 }
192
193
194 static void layerInterp_msticky(void **sources, float *weights,
195                                 float *sub_weights, int count, void *dest)
196 {
197         float co[2], w;
198         MSticky *mst;
199         int i;
200
201         co[0] = co[1] = 0.0f;
202         for(i = 0; i < count; i++) {
203                 w = weights ? weights[i] : 1.0f;
204                 mst = (MSticky*)sources[i];
205
206                 co[0] += w*mst->co[0];
207                 co[1] += w*mst->co[1];
208         }
209
210         mst = (MSticky*)dest;
211         mst->co[0] = co[0];
212         mst->co[1] = co[1];
213 }
214
215
216 static void layerCopy_tface(const void *source, void *dest, int count)
217 {
218         const MTFace *source_tf = (const MTFace*)source;
219         MTFace *dest_tf = (MTFace*)dest;
220         int i;
221
222         for(i = 0; i < count; ++i)
223                 dest_tf[i] = source_tf[i];
224 }
225
226 static void layerInterp_tface(void **sources, float *weights,
227                               float *sub_weights, int count, void *dest)
228 {
229         MTFace *tf = dest;
230         int i, j, k;
231         float uv[4][2];
232         float *sub_weight;
233
234         if(count <= 0) return;
235
236         memset(uv, 0, sizeof(uv));
237
238         sub_weight = sub_weights;
239         for(i = 0; i < count; ++i) {
240                 float weight = weights ? weights[i] : 1;
241                 MTFace *src = sources[i];
242
243                 for(j = 0; j < 4; ++j) {
244                         if(sub_weights) {
245                                 for(k = 0; k < 4; ++k, ++sub_weight) {
246                                         float w = (*sub_weight) * weight;
247                                         float *tmp_uv = src->uv[k];
248
249                                         uv[j][0] += tmp_uv[0] * w;
250                                         uv[j][1] += tmp_uv[1] * w;
251                                 }
252                         } else {
253                                 uv[j][0] += src->uv[j][0] * weight;
254                                 uv[j][1] += src->uv[j][1] * weight;
255                         }
256                 }
257         }
258
259         *tf = *(MTFace *)sources[0];
260         for(j = 0; j < 4; ++j) {
261                 tf->uv[j][0] = uv[j][0];
262                 tf->uv[j][1] = uv[j][1];
263         }
264 }
265
266 static void layerSwap_tface(void *data, int *corner_indices)
267 {
268         MTFace *tf = data;
269         float uv[4][2];
270         static const short pin_flags[4] =
271             { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
272         static const char sel_flags[4] =
273             { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
274         short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
275         char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
276         int j;
277
278         for(j = 0; j < 4; ++j) {
279                 int source_index = corner_indices[j];
280
281                 uv[j][0] = tf->uv[source_index][0];
282                 uv[j][1] = tf->uv[source_index][1];
283
284                 // swap pinning flags around
285                 if(tf->unwrap & pin_flags[source_index]) {
286                         unwrap |= pin_flags[j];
287                 }
288
289                 // swap selection flags around
290                 if(tf->flag & sel_flags[source_index]) {
291                         flag |= sel_flags[j];
292                 }
293         }
294
295         memcpy(tf->uv, uv, sizeof(tf->uv));
296         tf->unwrap = unwrap;
297         tf->flag = flag;
298 }
299
300 static void layerDefault_tface(void *data, int count)
301 {
302         static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
303                                    0, 0, TF_DYNAMIC, 0, 0};
304         MTFace *tf = (MTFace*)data;
305         int i;
306
307         for(i = 0; i < count; i++)
308                 tf[i] = default_tf;
309 }
310
311 static void layerCopy_origspace_face(const void *source, void *dest, int count)
312 {
313         const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
314         OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
315         int i;
316
317         for(i = 0; i < count; ++i)
318                 dest_tf[i] = source_tf[i];
319 }
320
321 static void layerInterp_origspace_face(void **sources, float *weights,
322                                                           float *sub_weights, int count, void *dest)
323 {
324         OrigSpaceFace *osf = dest;
325         int i, j, k;
326         float uv[4][2];
327         float *sub_weight;
328
329         if(count <= 0) return;
330
331         memset(uv, 0, sizeof(uv));
332
333         sub_weight = sub_weights;
334         for(i = 0; i < count; ++i) {
335                 float weight = weights ? weights[i] : 1;
336                 OrigSpaceFace *src = sources[i];
337
338                 for(j = 0; j < 4; ++j) {
339                         if(sub_weights) {
340                                 for(k = 0; k < 4; ++k, ++sub_weight) {
341                                         float w = (*sub_weight) * weight;
342                                         float *tmp_uv = src->uv[k];
343
344                                         uv[j][0] += tmp_uv[0] * w;
345                                         uv[j][1] += tmp_uv[1] * w;
346                                 }
347                         } else {
348                                 uv[j][0] += src->uv[j][0] * weight;
349                                 uv[j][1] += src->uv[j][1] * weight;
350                         }
351                 }
352         }
353
354         *osf = *(OrigSpaceFace *)sources[0];
355         for(j = 0; j < 4; ++j) {
356                 osf->uv[j][0] = uv[j][0];
357                 osf->uv[j][1] = uv[j][1];
358         }
359 }
360
361 static void layerSwap_origspace_face(void *data, int *corner_indices)
362 {
363         OrigSpaceFace *osf = data;
364         float uv[4][2];
365         int j;
366
367         for(j = 0; j < 4; ++j) {
368                 uv[j][0] = osf->uv[corner_indices[j]][0];
369                 uv[j][1] = osf->uv[corner_indices[j]][1];
370         }
371         memcpy(osf->uv, uv, sizeof(osf->uv));
372 }
373
374 static void layerDefault_origspace_face(void *data, int count)
375 {
376         static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
377         OrigSpaceFace *osf = (OrigSpaceFace*)data;
378         int i;
379
380         for(i = 0; i < count; i++)
381                 osf[i] = default_osf;
382 }
383
384 /* Adapted from sculptmode.c */
385 static void mdisps_bilinear(float out[3], float (*disps)[3], int st, float u, float v)
386 {
387         int x, y, x2, y2;
388         const int st_max = st - 1;
389         float urat, vrat, uopp;
390         float d[4][3], d2[2][3];
391
392         if(u < 0)
393                 u = 0;
394         else if(u >= st)
395                 u = st_max;
396         if(v < 0)
397                 v = 0;
398         else if(v >= st)
399                 v = st_max;
400
401         x = floor(u);
402         y = floor(v);
403         x2 = x + 1;
404         y2 = y + 1;
405
406         if(x2 >= st) x2 = st_max;
407         if(y2 >= st) y2 = st_max;
408         
409         urat = u - x;
410         vrat = v - y;
411         uopp = 1 - urat;
412
413         copy_v3_v3(d[0], disps[y * st + x]);
414         copy_v3_v3(d[1], disps[y * st + x2]);
415         copy_v3_v3(d[2], disps[y2 * st + x]);
416         copy_v3_v3(d[3], disps[y2 * st + x2]);
417         mul_v3_fl(d[0], uopp);
418         mul_v3_fl(d[1], urat);
419         mul_v3_fl(d[2], uopp);
420         mul_v3_fl(d[3], urat);
421
422         add_v3_v3v3(d2[0], d[0], d[1]);
423         add_v3_v3v3(d2[1], d[2], d[3]);
424         mul_v3_fl(d2[0], 1 - vrat);
425         mul_v3_fl(d2[1], vrat);
426
427         add_v3_v3v3(out, d2[0], d2[1]);
428 }
429
430 static void layerSwap_mdisps(void *data, int *ci)
431 {
432         MDisps *s = data;
433         float (*d)[3] = NULL;
434         int x, y, st;
435
436         if(!(ci[0] == 2 && ci[1] == 3 && ci[2] == 0 && ci[3] == 1)) return;
437
438         d = MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
439         st = sqrt(s->totdisp);
440
441         for(y = 0; y < st; ++y) {
442                 for(x = 0; x < st; ++x) {
443                         copy_v3_v3(d[(st - y - 1) * st + (st - x - 1)], s->disps[y * st + x]);
444                 }
445         }
446         
447         if(s->disps)
448                 MEM_freeN(s->disps);
449         s->disps = d;
450 }
451
452 static void layerInterp_mdisps(void **sources, float *weights, float *sub_weights,
453                                int count, void *dest)
454 {
455         MDisps *d = dest;
456         MDisps *s = NULL;
457         int st, stl;
458         int i, x, y;
459         float crn[4][2];
460         float (*sw)[4] = NULL;
461
462         /* Initialize the destination */
463         for(i = 0; i < d->totdisp; ++i) {
464                 float z[3] = {0,0,0};
465                 copy_v3_v3(d->disps[i], z);
466         }
467
468         /* For now, some restrictions on the input */
469         if(count != 1 || !sub_weights) return;
470
471         st = sqrt(d->totdisp);
472         stl = st - 1;
473
474         sw = (void*)sub_weights;
475         for(i = 0; i < 4; ++i) {
476                 crn[i][0] = 0 * sw[i][0] + stl * sw[i][1] + stl * sw[i][2] + 0 * sw[i][3];
477                 crn[i][1] = 0 * sw[i][0] + 0 * sw[i][1] + stl * sw[i][2] + stl * sw[i][3];
478         }
479
480         s = sources[0];
481         for(y = 0; y < st; ++y) {
482                 for(x = 0; x < st; ++x) {
483                         /* One suspects this code could be cleaner. */
484                         float xl = (float)x / (st - 1);
485                         float yl = (float)y / (st - 1);
486                         float mid1[2] = {crn[0][0] * (1 - xl) + crn[1][0] * xl,
487                                          crn[0][1] * (1 - xl) + crn[1][1] * xl};
488                         float mid2[2] = {crn[3][0] * (1 - xl) + crn[2][0] * xl,
489                                          crn[3][1] * (1 - xl) + crn[2][1] * xl};
490                         float mid3[2] = {mid1[0] * (1 - yl) + mid2[0] * yl,
491                                          mid1[1] * (1 - yl) + mid2[1] * yl};
492
493                         float srcdisp[3];
494
495                         mdisps_bilinear(srcdisp, s->disps, st, mid3[0], mid3[1]);
496                         copy_v3_v3(d->disps[y * st + x], srcdisp);
497                 }
498         }
499 }
500
501 static void layerCopy_mdisps(const void *source, void *dest, int count)
502 {
503         int i;
504         const MDisps *s = source;
505         MDisps *d = dest;
506
507         for(i = 0; i < count; ++i) {
508                 if(s[i].disps) {
509                         d[i].disps = MEM_dupallocN(s[i].disps);
510                         d[i].totdisp = s[i].totdisp;
511                 }
512                 else {
513                         d[i].disps = NULL;
514                         d[i].totdisp = 0;
515                 }
516                 
517         }
518 }
519
520 static void layerFree_mdisps(void *data, int count, int size)
521 {
522         int i;
523         MDisps *d = data;
524
525         for(i = 0; i < count; ++i) {
526                 if(d[i].disps)
527                         MEM_freeN(d[i].disps);
528                 d[i].disps = NULL;
529                 d[i].totdisp = 0;
530         }
531 }
532
533 /* --------- */
534
535 static void layerDefault_mloopcol(void *data, int count)
536 {
537         static MLoopCol default_mloopcol = {255,255,255,255};
538         MLoopCol *mlcol = (MLoopCol*)data;
539         int i;
540         for(i = 0; i < count; i++)
541                 mlcol[i] = default_mloopcol;
542
543 }
544
545 static void layerInterp_mloopcol(void **sources, float *weights,
546                                 float *sub_weights, int count, void *dest)
547 {
548         MLoopCol *mc = dest;
549         int i;
550         float *sub_weight;
551         struct {
552                 float a;
553                 float r;
554                 float g;
555                 float b;
556         } col;
557         col.a = col.r = col.g = col.b = 0;
558
559         sub_weight = sub_weights;
560         for(i = 0; i < count; ++i){
561                 float weight = weights ? weights[i] : 1;
562                 MLoopCol *src = sources[i];
563                 if(sub_weights){
564                         col.a += src->a * (*sub_weight) * weight;
565                         col.r += src->r * (*sub_weight) * weight;
566                         col.g += src->g * (*sub_weight) * weight;
567                         col.b += src->b * (*sub_weight) * weight;
568                         sub_weight++;           
569                 } else {
570                         col.a += src->a * weight;
571                         col.r += src->r * weight;
572                         col.g += src->g * weight;
573                         col.b += src->b * weight;
574                 }
575         }
576         
577         /* Subdivide smooth or fractal can cause problems without clamping
578          * although weights should also not cause this situation */
579         CLAMP(col.a, 0.0f, 255.0f);
580         CLAMP(col.r, 0.0f, 255.0f);
581         CLAMP(col.g, 0.0f, 255.0f);
582         CLAMP(col.b, 0.0f, 255.0f);
583         
584         mc->a = (int)col.a;
585         mc->r = (int)col.r;
586         mc->g = (int)col.g;
587         mc->b = (int)col.b;
588 }
589 static void layerInterp_mloopuv(void **sources, float *weights,
590                                 float *sub_weights, int count, void *dest)
591 {
592         MLoopUV *mluv = dest;
593         int i;
594         float *sub_weight;
595         struct {
596                 float u;
597                 float v;
598         }uv;
599         uv.u = uv.v = 0.0;
600
601         sub_weight = sub_weights;
602         for(i = 0; i < count; ++i){
603                 float weight = weights ? weights[i] : 1;
604                 MLoopUV *src = sources[i];
605                 if(sub_weights){
606                         uv.u += src->uv[0] * (*sub_weight) * weight;
607                         uv.v += src->uv[1] * (*sub_weight) * weight;
608                         sub_weight++;           
609                 } else {
610                         uv.u += src->uv[0] * weight;
611                         uv.v += src->uv[1] * weight;
612                 }
613         }
614         mluv->uv[0] = uv.u;
615         mluv->uv[1] = uv.v;
616 }
617
618 static void layerInterp_mcol(void **sources, float *weights,
619                              float *sub_weights, int count, void *dest)
620 {
621         MCol *mc = dest;
622         int i, j, k;
623         struct {
624                 float a;
625                 float r;
626                 float g;
627                 float b;
628         } col[4];
629         float *sub_weight;
630
631         if(count <= 0) return;
632
633         memset(col, 0, sizeof(col));
634         
635         sub_weight = sub_weights;
636         for(i = 0; i < count; ++i) {
637                 float weight = weights ? weights[i] : 1;
638
639                 for(j = 0; j < 4; ++j) {
640                         if(sub_weights) {
641                                 MCol *src = sources[i];
642                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
643                                         col[j].a += src->a * (*sub_weight) * weight;
644                                         col[j].r += src->r * (*sub_weight) * weight;
645                                         col[j].g += src->g * (*sub_weight) * weight;
646                                         col[j].b += src->b * (*sub_weight) * weight;
647                                 }
648                         } else {
649                                 MCol *src = sources[i];
650                                 col[j].a += src[j].a * weight;
651                                 col[j].r += src[j].r * weight;
652                                 col[j].g += src[j].g * weight;
653                                 col[j].b += src[j].b * weight;
654                         }
655                 }
656         }
657
658         for(j = 0; j < 4; ++j) {
659                 
660                 /* Subdivide smooth or fractal can cause problems without clamping
661                  * although weights should also not cause this situation */
662                 CLAMP(col[j].a, 0.0f, 255.0f);
663                 CLAMP(col[j].r, 0.0f, 255.0f);
664                 CLAMP(col[j].g, 0.0f, 255.0f);
665                 CLAMP(col[j].b, 0.0f, 255.0f);
666                 
667                 mc[j].a = (int)col[j].a;
668                 mc[j].r = (int)col[j].r;
669                 mc[j].g = (int)col[j].g;
670                 mc[j].b = (int)col[j].b;
671         }
672 }
673
674 static void layerSwap_mcol(void *data, int *corner_indices)
675 {
676         MCol *mcol = data;
677         MCol col[4];
678         int j;
679
680         for(j = 0; j < 4; ++j)
681                 col[j] = mcol[corner_indices[j]];
682
683         memcpy(mcol, col, sizeof(col));
684 }
685
686 static void layerDefault_mcol(void *data, int count)
687 {
688         static MCol default_mcol = {255, 255, 255, 255};
689         MCol *mcol = (MCol*)data;
690         int i;
691
692         for(i = 0; i < 4*count; i++)
693                 mcol[i] = default_mcol;
694 }
695
696
697
698 const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
699         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
700         {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
701          NULL},
702         {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
703          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
704         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
705         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
706         {sizeof(MTFace), "MTFace", 1, "UVTex", layerCopy_tface, NULL,
707          layerInterp_tface, layerSwap_tface, layerDefault_tface},
708         /* 4 MCol structs per face */
709         {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
710          layerSwap_mcol, layerDefault_mcol},
711         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
712         /* 3 floats per normal vector */
713         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
714         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
715         {sizeof(MFloatProperty), "MFloatProperty",1,"Float",NULL,NULL,NULL,NULL},
716         {sizeof(MIntProperty), "MIntProperty",1,"Int",NULL,NULL,NULL,NULL},
717         {sizeof(MStringProperty), "MStringProperty",1,"String",NULL,NULL,NULL,NULL},
718         {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVTex", layerCopy_origspace_face, NULL,
719          layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
720         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
721         {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
722         {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL},
723         {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, layerDefault_mloopcol},
724         {sizeof(float)*3*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
725         {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
726          layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL},
727         {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
728          layerSwap_mcol, layerDefault_mcol},
729          {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
730          layerSwap_mcol, layerDefault_mcol},
731          {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
732          layerSwap_mcol, layerDefault_mcol},
733 };
734
735 const char *LAYERTYPENAMES[CD_NUMTYPES] = {
736         "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace",
737         "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags","CDMFloatProperty",
738         "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco", "CDMTexPoly", "CDMLoopUV",
739         "CDMloopCol", "CDTangent", "CDMDisps", "CDWeightMCol"};
740
741 const CustomDataMask CD_MASK_BAREMESH =
742         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE;
743 const CustomDataMask CD_MASK_MESH =
744         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
745         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
746         CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS;
747 const CustomDataMask CD_MASK_EDITMESH =
748         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
749         CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS;
750 const CustomDataMask CD_MASK_DERIVEDMESH =
751         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
752         CD_MASK_MCOL | CD_MASK_ORIGINDEX | CD_MASK_PROP_FLT | CD_MASK_PROP_INT |
753         CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO | CD_MASK_TANGENT | CD_MASK_WEIGHT_MCOL;
754 const CustomDataMask CD_MASK_BMESH = 
755         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR;
756 const CustomDataMask CD_MASK_FACECORNERS =
757         CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
758         CD_MASK_MLOOPCOL;
759
760
761 static const LayerTypeInfo *layerType_getInfo(int type)
762 {
763         if(type < 0 || type >= CD_NUMTYPES) return NULL;
764
765         return &LAYERTYPEINFO[type];
766 }
767
768 static const char *layerType_getName(int type)
769 {
770         if(type < 0 || type >= CD_NUMTYPES) return NULL;
771
772         return LAYERTYPENAMES[type];
773 }
774
775 /********************* CustomData functions *********************/
776 static void customData_update_offsets(CustomData *data);
777
778 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
779         int type, int alloctype, void *layerdata, int totelem, const char *name);
780
781 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
782                       CustomDataMask mask, int alloctype, int totelem)
783 {
784         const LayerTypeInfo *typeInfo;
785         CustomDataLayer *layer, *newlayer;
786         int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0;
787
788         for(i = 0; i < source->totlayer; ++i) {
789                 layer = &source->layers[i];
790                 typeInfo = layerType_getInfo(layer->type);
791
792                 type = layer->type;
793
794                 if (type != lasttype) {
795                         number = 0;
796                         lastactive = layer->active;
797                         lastrender = layer->active_rnd;
798                         lastclone = layer->active_clone;
799                         lastmask = layer->active_mask;
800                         lasttype = type;
801                 }
802                 else
803                         number++;
804
805                 if(layer->flag & CD_FLAG_NOCOPY) continue;
806                 else if(!((int)mask & (int)(1 << (int)type))) continue;
807                 else if(number < CustomData_number_of_layers(dest, type)) continue;
808
809                 if((alloctype == CD_ASSIGN) && (layer->flag & CD_FLAG_NOFREE))
810                         newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
811                                 layer->data, totelem, layer->name);
812                 else
813                         newlayer = customData_add_layer__internal(dest, type, alloctype,
814                                 layer->data, totelem, layer->name);
815                 
816                 if(newlayer) {
817                         newlayer->active = lastactive;
818                         newlayer->active_rnd = lastrender;
819                         newlayer->active_clone = lastclone;
820                         newlayer->active_mask = lastmask;
821                 }
822         }
823 }
824
825 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
826                      CustomDataMask mask, int alloctype, int totelem)
827 {
828         memset(dest, 0, sizeof(*dest));
829
830         CustomData_merge(source, dest, mask, alloctype, totelem);
831 }
832
833 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
834 {
835         const LayerTypeInfo *typeInfo;
836
837         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
838                 typeInfo = layerType_getInfo(layer->type);
839
840                 if(typeInfo->free)
841                         typeInfo->free(layer->data, totelem, typeInfo->size);
842
843                 if(layer->data)
844                         MEM_freeN(layer->data);
845         }
846 }
847
848 void CustomData_free(CustomData *data, int totelem)
849 {
850         int i;
851
852         for(i = 0; i < data->totlayer; ++i)
853                 customData_free_layer__internal(&data->layers[i], totelem);
854
855         if(data->layers)
856                 MEM_freeN(data->layers);
857         
858         memset(data, 0, sizeof(*data));
859 }
860
861 static void customData_update_offsets(CustomData *data)
862 {
863         const LayerTypeInfo *typeInfo;
864         int i, offset = 0;
865
866         for(i = 0; i < data->totlayer; ++i) {
867                 typeInfo = layerType_getInfo(data->layers[i].type);
868
869                 data->layers[i].offset = offset;
870                 offset += typeInfo->size;
871         }
872
873         data->totsize = offset;
874 }
875
876 int CustomData_get_layer_index(const CustomData *data, int type)
877 {
878         int i; 
879
880         for(i=0; i < data->totlayer; ++i)
881                 if(data->layers[i].type == type)
882                         return i;
883
884         return -1;
885 }
886
887 int CustomData_get_named_layer_index(const CustomData *data, int type, char *name)
888 {
889         int i;
890
891         for(i=0; i < data->totlayer; ++i)
892                 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
893                         return i;
894
895         return -1;
896 }
897
898 int CustomData_get_active_layer_index(const CustomData *data, int type)
899 {
900         int i;
901
902         for(i=0; i < data->totlayer; ++i)
903                 if(data->layers[i].type == type)
904                         return i + data->layers[i].active;
905
906         return -1;
907 }
908
909 int CustomData_get_render_layer_index(const CustomData *data, int type)
910 {
911         int i;
912
913         for(i=0; i < data->totlayer; ++i)
914                 if(data->layers[i].type == type)
915                         return i + data->layers[i].active_rnd;
916
917         return -1;
918 }
919
920 int CustomData_get_clone_layer_index(const CustomData *data, int type)
921 {
922         int i;
923
924         for(i=0; i < data->totlayer; ++i)
925                 if(data->layers[i].type == type)
926                         return i + data->layers[i].active_clone;
927
928         return -1;
929 }
930
931 int CustomData_get_mask_layer_index(const CustomData *data, int type)
932 {
933         int i;
934
935         for(i=0; i < data->totlayer; ++i)
936                 if(data->layers[i].type == type)
937                         return i + data->layers[i].active_mask;
938
939         return -1;
940 }
941
942 int CustomData_get_active_layer(const CustomData *data, int type)
943 {
944         int i;
945
946         for(i=0; i < data->totlayer; ++i)
947                 if(data->layers[i].type == type)
948                         return data->layers[i].active;
949
950         return -1;
951 }
952
953 int CustomData_get_render_layer(const CustomData *data, int type)
954 {
955         int i;
956
957         for(i=0; i < data->totlayer; ++i)
958                 if(data->layers[i].type == type)
959                         return data->layers[i].active_rnd;
960
961         return -1;
962 }
963
964 int CustomData_get_clone_layer(const CustomData *data, int type)
965 {
966         int i;
967
968         for(i=0; i < data->totlayer; ++i)
969                 if(data->layers[i].type == type)
970                         return data->layers[i].active_clone;
971
972         return -1;
973 }
974
975 int CustomData_get_mask_layer(const CustomData *data, int type)
976 {
977         int i;
978
979         for(i=0; i < data->totlayer; ++i)
980                 if(data->layers[i].type == type)
981                         return data->layers[i].active_mask;
982
983         return -1;
984 }
985
986 void CustomData_set_layer_active(CustomData *data, int type, int n)
987 {
988         int i;
989
990         for(i=0; i < data->totlayer; ++i)
991                 if(data->layers[i].type == type)
992                         data->layers[i].active = n;
993 }
994
995 void CustomData_set_layer_render(CustomData *data, int type, int n)
996 {
997         int i;
998
999         for(i=0; i < data->totlayer; ++i)
1000                 if(data->layers[i].type == type)
1001                         data->layers[i].active_rnd = n;
1002 }
1003
1004 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1005 {
1006         int i;
1007
1008         for(i=0; i < data->totlayer; ++i)
1009                 if(data->layers[i].type == type)
1010                         data->layers[i].active_clone = n;
1011 }
1012
1013 void CustomData_set_layer_mask(CustomData *data, int type, int n)
1014 {
1015         int i;
1016
1017         for(i=0; i < data->totlayer; ++i)
1018                 if(data->layers[i].type == type)
1019                         data->layers[i].active_mask = n;
1020 }
1021
1022 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1023 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1024 {
1025         int i;
1026
1027         for(i=0; i < data->totlayer; ++i)
1028                 if(data->layers[i].type == type)
1029                         data->layers[i].active = n-i;
1030 }
1031
1032 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1033 {
1034         int i;
1035
1036         for(i=0; i < data->totlayer; ++i)
1037                 if(data->layers[i].type == type)
1038                         data->layers[i].active_rnd = n-i;
1039 }
1040
1041 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1042 {
1043         int i;
1044
1045         for(i=0; i < data->totlayer; ++i)
1046                 if(data->layers[i].type == type)
1047                         data->layers[i].active_clone = n-i;
1048 }
1049
1050 void CustomData_set_layer_mask_index(CustomData *data, int type, int n)
1051 {
1052         int i;
1053
1054         for(i=0; i < data->totlayer; ++i)
1055                 if(data->layers[i].type == type)
1056                         data->layers[i].active_mask = n-i;
1057 }
1058
1059 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1060 {
1061         int i;
1062
1063         for(i=0; i < data->totlayer; ++i)
1064                 if(data->layers[i].type == type)
1065                         data->layers[i].flag |= flag;
1066 }
1067
1068 static int customData_resize(CustomData *data, int amount)
1069 {
1070         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1071                                        "CustomData->layers");
1072         if(!tmp) return 0;
1073
1074         data->maxlayer += amount;
1075         if (data->layers) {
1076                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1077                 MEM_freeN(data->layers);
1078         }
1079         data->layers = tmp;
1080
1081         return 1;
1082 }
1083
1084 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1085         int type, int alloctype, void *layerdata, int totelem, const char *name)
1086 {
1087         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1088         int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1089         void *newlayerdata;
1090
1091         if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1092                 return &data->layers[CustomData_get_layer_index(data, type)];
1093
1094         if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1095                 newlayerdata = layerdata;
1096         }
1097         else {
1098                 newlayerdata = MEM_callocN(size, layerType_getName(type));
1099                 if(!newlayerdata)
1100                         return NULL;
1101         }
1102
1103         if (alloctype == CD_DUPLICATE) {
1104                 if(typeInfo->copy)
1105                         typeInfo->copy(layerdata, newlayerdata, totelem);
1106                 else
1107                         memcpy(newlayerdata, layerdata, size);
1108         }
1109         else if (alloctype == CD_DEFAULT) {
1110                 if(typeInfo->set_default)
1111                         typeInfo->set_default((char*)newlayerdata, totelem);
1112         }
1113         else if (alloctype == CD_REFERENCE)
1114                 flag |= CD_FLAG_NOFREE;
1115
1116         if(index >= data->maxlayer) {
1117                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
1118                         if(newlayerdata != layerdata)
1119                                 MEM_freeN(newlayerdata);
1120                         return NULL;
1121                 }
1122         }
1123         
1124         data->totlayer++;
1125
1126         /* keep layers ordered by type */
1127         for( ; index > 0 && data->layers[index - 1].type > type; --index)
1128                 data->layers[index] = data->layers[index - 1];
1129
1130         data->layers[index].type = type;
1131         data->layers[index].flag = flag;
1132         data->layers[index].data = newlayerdata;
1133
1134         if(name) {
1135                 strcpy(data->layers[index].name, name);
1136                 CustomData_set_layer_unique_name(data, index);
1137         }
1138         else
1139                 data->layers[index].name[0] = '\0';
1140
1141         if(index > 0 && data->layers[index-1].type == type) {
1142                 data->layers[index].active = data->layers[index-1].active;
1143                 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1144                 data->layers[index].active_clone = data->layers[index-1].active_clone;
1145                 data->layers[index].active_mask = data->layers[index-1].active_mask;
1146         } else {
1147                 data->layers[index].active = 0;
1148                 data->layers[index].active_rnd = 0;
1149                 data->layers[index].active_clone = 0;
1150                 data->layers[index].active_mask = 0;
1151         }
1152         
1153         customData_update_offsets(data);
1154
1155         return &data->layers[index];
1156 }
1157
1158 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1159                            void *layerdata, int totelem)
1160 {
1161         CustomDataLayer *layer;
1162         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1163         
1164         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1165                                                totelem, typeInfo->defaultname);
1166
1167         if(layer)
1168                 return layer->data;
1169
1170         return NULL;
1171 }
1172
1173 /*same as above but accepts a name*/
1174 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1175                            void *layerdata, int totelem, char *name)
1176 {
1177         CustomDataLayer *layer;
1178         
1179         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1180                                                totelem, name);
1181
1182         if(layer)
1183                 return layer->data;
1184
1185         return NULL;
1186 }
1187
1188
1189 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1190 {
1191         int i;
1192         
1193         if (index < 0) return 0;
1194
1195         customData_free_layer__internal(&data->layers[index], totelem);
1196
1197         for (i=index+1; i < data->totlayer; ++i)
1198                 data->layers[i-1] = data->layers[i];
1199
1200         data->totlayer--;
1201
1202         /* if layer was last of type in array, set new active layer */
1203         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1204                 i = CustomData_get_layer_index(data, type);
1205                 
1206                 if (i >= 0)
1207                         for (; i < data->totlayer && data->layers[i].type == type; i++) {
1208                                 data->layers[i].active--;
1209                                 data->layers[i].active_rnd--;
1210                                 data->layers[i].active_clone--;
1211                                 data->layers[i].active_mask--;
1212                         }
1213         }
1214
1215         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1216                 customData_resize(data, -CUSTOMDATA_GROW);
1217
1218         customData_update_offsets(data);
1219
1220         return 1;
1221 }
1222
1223 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1224 {
1225         int index = 0;
1226         index = CustomData_get_active_layer_index(data, type);
1227         if (index < 0) return 0;
1228         return CustomData_free_layer(data, type, totelem, index);
1229 }
1230
1231
1232 void CustomData_free_layers(CustomData *data, int type, int totelem)
1233 {
1234         while (CustomData_has_layer(data, type))
1235                 CustomData_free_layer_active(data, type, totelem);
1236 }
1237
1238 int CustomData_has_layer(const CustomData *data, int type)
1239 {
1240         return (CustomData_get_layer_index(data, type) != -1);
1241 }
1242
1243 int CustomData_number_of_layers(const CustomData *data, int type)
1244 {
1245         int i, number = 0;
1246
1247         for(i = 0; i < data->totlayer; i++)
1248                 if(data->layers[i].type == type)
1249                         number++;
1250         
1251         return number;
1252 }
1253
1254 void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type)
1255 {
1256         CustomDataLayer *layer;
1257         int layer_index;
1258
1259         /* get the layer index of the first layer of type */
1260         layer_index = CustomData_get_active_layer_index(data, type);
1261         if(layer_index < 0) return NULL;
1262
1263         layer = &data->layers[layer_index];
1264
1265         if (layer->flag & CD_FLAG_NOFREE) {
1266                 layer->data = MEM_dupallocN(layer->data);
1267                 layer->flag &= ~CD_FLAG_NOFREE;
1268         }
1269
1270         return layer->data;
1271 }
1272
1273 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1274                                                   int type, char *name)
1275 {
1276         CustomDataLayer *layer;
1277         int layer_index;
1278
1279         /* get the layer index of the desired layer */
1280         layer_index = CustomData_get_named_layer_index(data, type, name);
1281         if(layer_index < 0) return NULL;
1282
1283         layer = &data->layers[layer_index];
1284
1285         if (layer->flag & CD_FLAG_NOFREE) {
1286                 layer->data = MEM_dupallocN(layer->data);
1287                 layer->flag &= ~CD_FLAG_NOFREE;
1288         }
1289
1290         return layer->data;
1291 }
1292
1293 void CustomData_free_temporary(CustomData *data, int totelem)
1294 {
1295         CustomDataLayer *layer;
1296         int i, j;
1297
1298         for(i = 0, j = 0; i < data->totlayer; ++i) {
1299                 layer = &data->layers[i];
1300
1301                 if (i != j)
1302                         data->layers[j] = data->layers[i];
1303
1304                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1305                         customData_free_layer__internal(layer, totelem);
1306                 else
1307                         j++;
1308         }
1309
1310         data->totlayer = j;
1311
1312         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1313                 customData_resize(data, -CUSTOMDATA_GROW);
1314
1315         customData_update_offsets(data);
1316 }
1317
1318 void CustomData_set_only_copy(const struct CustomData *data,
1319                               CustomDataMask mask)
1320 {
1321         int i;
1322
1323         for(i = 0; i < data->totlayer; ++i)
1324                 if(!((int)mask & (int)(1 << (int)data->layers[i].type)))
1325                         data->layers[i].flag |= CD_FLAG_NOCOPY;
1326 }
1327
1328 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1329                           int source_index, int dest_index, int count)
1330 {
1331         const LayerTypeInfo *typeInfo;
1332         int src_i, dest_i;
1333         int src_offset;
1334         int dest_offset;
1335
1336         /* copies a layer at a time */
1337         dest_i = 0;
1338         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1339
1340                 /* find the first dest layer with type >= the source type
1341                  * (this should work because layers are ordered by type)
1342                  */
1343                 while(dest_i < dest->totlayer
1344                       && dest->layers[dest_i].type < source->layers[src_i].type)
1345                         ++dest_i;
1346
1347                 /* if there are no more dest layers, we're done */
1348                 if(dest_i >= dest->totlayer) return;
1349
1350                 /* if we found a matching layer, copy the data */
1351                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1352                         char *src_data = source->layers[src_i].data;
1353                         char *dest_data = dest->layers[dest_i].data;
1354
1355                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1356
1357                         src_offset = source_index * typeInfo->size;
1358                         dest_offset = dest_index * typeInfo->size;
1359
1360                         if(typeInfo->copy)
1361                                 typeInfo->copy(src_data + src_offset,
1362                                                 dest_data + dest_offset,
1363                                                 count);
1364                         else
1365                                 memcpy(dest_data + dest_offset,
1366                                        src_data + src_offset,
1367                                        count * typeInfo->size);
1368
1369                         /* if there are multiple source & dest layers of the same type,
1370                          * we don't want to copy all source layers to the same dest, so
1371                          * increment dest_i
1372                          */
1373                         ++dest_i;
1374                 }
1375         }
1376 }
1377
1378 void CustomData_free_elem(CustomData *data, int index, int count)
1379 {
1380         int i;
1381         const LayerTypeInfo *typeInfo;
1382
1383         for(i = 0; i < data->totlayer; ++i) {
1384                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1385                         typeInfo = layerType_getInfo(data->layers[i].type);
1386
1387                         if(typeInfo->free) {
1388                                 int offset = typeInfo->size * index;
1389
1390                                 typeInfo->free((char *)data->layers[i].data + offset,
1391                                                count, typeInfo->size);
1392                         }
1393                 }
1394         }
1395 }
1396
1397 #define SOURCE_BUF_SIZE 100
1398
1399 void CustomData_interp(const CustomData *source, CustomData *dest,
1400                        int *src_indices, float *weights, float *sub_weights,
1401                        int count, int dest_index)
1402 {
1403         int src_i, dest_i;
1404         int dest_offset;
1405         int j;
1406         void *source_buf[SOURCE_BUF_SIZE];
1407         void **sources = source_buf;
1408
1409         /* slow fallback in case we're interpolating a ridiculous number of
1410          * elements
1411          */
1412         if(count > SOURCE_BUF_SIZE)
1413                 sources = MEM_callocN(sizeof(*sources) * count,
1414                                       "CustomData_interp sources");
1415
1416         /* interpolates a layer at a time */
1417         dest_i = 0;
1418         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1419                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
1420                 if(!typeInfo->interp) continue;
1421
1422                 /* find the first dest layer with type >= the source type
1423                  * (this should work because layers are ordered by type)
1424                  */
1425                 while(dest_i < dest->totlayer
1426                       && dest->layers[dest_i].type < source->layers[src_i].type)
1427                         ++dest_i;
1428
1429                 /* if there are no more dest layers, we're done */
1430                 if(dest_i >= dest->totlayer) return;
1431
1432                 /* if we found a matching layer, copy the data */
1433                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1434                         void *src_data = source->layers[src_i].data;
1435
1436                         for(j = 0; j < count; ++j)
1437                                 sources[j] = (char *)src_data
1438                                                          + typeInfo->size * src_indices[j];
1439
1440                         dest_offset = dest_index * typeInfo->size;
1441
1442                         typeInfo->interp(sources, weights, sub_weights, count,
1443                                                    (char *)dest->layers[dest_i].data + dest_offset);
1444
1445                         /* if there are multiple source & dest layers of the same type,
1446                          * we don't want to copy all source layers to the same dest, so
1447                          * increment dest_i
1448                          */
1449                         ++dest_i;
1450                 }
1451         }
1452
1453         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1454 }
1455
1456 void CustomData_swap(struct CustomData *data, int index, int *corner_indices)
1457 {
1458         const LayerTypeInfo *typeInfo;
1459         int i;
1460
1461         for(i = 0; i < data->totlayer; ++i) {
1462                 typeInfo = layerType_getInfo(data->layers[i].type);
1463
1464                 if(typeInfo->swap) {
1465                         int offset = typeInfo->size * index;
1466
1467                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
1468                 }
1469         }
1470 }
1471
1472 void *CustomData_get(const CustomData *data, int index, int type)
1473 {
1474         int offset;
1475         int layer_index;
1476         
1477         /* get the layer index of the active layer of type */
1478         layer_index = CustomData_get_active_layer_index(data, type);
1479         if(layer_index < 0) return NULL;
1480
1481         /* get the offset of the desired element */
1482         offset = layerType_getInfo(type)->size * index;
1483
1484         return (char *)data->layers[layer_index].data + offset;
1485 }
1486
1487 void *CustomData_get_layer(const CustomData *data, int type)
1488 {
1489         /* get the layer index of the active layer of type */
1490         int layer_index = CustomData_get_active_layer_index(data, type);
1491         if(layer_index < 0) return NULL;
1492
1493         return data->layers[layer_index].data;
1494 }
1495
1496 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
1497 {
1498         /* get the layer index of the active layer of type */
1499         int layer_index = CustomData_get_layer_index(data, type);
1500         if(layer_index < 0) return NULL;
1501
1502         return data->layers[layer_index+n].data;
1503 }
1504
1505 void *CustomData_get_layer_named(const struct CustomData *data, int type,
1506                                  char *name)
1507 {
1508         int layer_index = CustomData_get_named_layer_index(data, type, name);
1509         if(layer_index < 0) return NULL;
1510
1511         return data->layers[layer_index].data;
1512 }
1513
1514 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
1515 {
1516         /* get the layer index of the first layer of type */
1517         int layer_index = CustomData_get_active_layer_index(data, type);
1518
1519         if(layer_index < 0) return NULL;
1520
1521         data->layers[layer_index].data = ptr;
1522
1523         return ptr;
1524 }
1525
1526 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
1527 {
1528         /* get the layer index of the first layer of type */
1529         int layer_index = CustomData_get_layer_index(data, type);
1530         if(layer_index < 0) return NULL;
1531
1532         data->layers[layer_index+n].data = ptr;
1533
1534         return ptr;
1535 }
1536
1537 void CustomData_set(const CustomData *data, int index, int type, void *source)
1538 {
1539         void *dest = CustomData_get(data, index, type);
1540         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1541
1542         if(!dest) return;
1543
1544         if(typeInfo->copy)
1545                 typeInfo->copy(source, dest, 1);
1546         else
1547                 memcpy(dest, source, typeInfo->size);
1548 }
1549
1550 /* EditMesh functions */
1551
1552 void CustomData_em_free_block(CustomData *data, void **block)
1553 {
1554     const LayerTypeInfo *typeInfo;
1555     int i;
1556
1557         if(!*block) return;
1558
1559     for(i = 0; i < data->totlayer; ++i) {
1560         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1561             typeInfo = layerType_getInfo(data->layers[i].type);
1562
1563             if(typeInfo->free) {
1564                                 int offset = data->layers[i].offset;
1565                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
1566                         }
1567         }
1568     }
1569
1570         MEM_freeN(*block);
1571         *block = NULL;
1572 }
1573
1574 static void CustomData_em_alloc_block(CustomData *data, void **block)
1575 {
1576         /* TODO: optimize free/alloc */
1577
1578         if (*block)
1579                 CustomData_em_free_block(data, block);
1580
1581         if (data->totsize > 0)
1582                 *block = MEM_callocN(data->totsize, "CustomData EM block");
1583         else
1584                 *block = NULL;
1585 }
1586
1587 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
1588                             void *src_block, void **dest_block)
1589 {
1590         const LayerTypeInfo *typeInfo;
1591         int dest_i, src_i;
1592
1593         if (!*dest_block)
1594                 CustomData_em_alloc_block(dest, dest_block);
1595         
1596         /* copies a layer at a time */
1597         dest_i = 0;
1598         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1599
1600                 /* find the first dest layer with type >= the source type
1601                  * (this should work because layers are ordered by type)
1602                  */
1603                 while(dest_i < dest->totlayer
1604                       && dest->layers[dest_i].type < source->layers[src_i].type)
1605                         ++dest_i;
1606
1607                 /* if there are no more dest layers, we're done */
1608                 if(dest_i >= dest->totlayer) return;
1609
1610                 /* if we found a matching layer, copy the data */
1611                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
1612                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
1613                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1614                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1615
1616                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1617
1618                         if(typeInfo->copy)
1619                                 typeInfo->copy(src_data, dest_data, 1);
1620                         else
1621                                 memcpy(dest_data, src_data, typeInfo->size);
1622
1623                         /* if there are multiple source & dest layers of the same type,
1624                          * we don't want to copy all source layers to the same dest, so
1625                          * increment dest_i
1626                          */
1627                         ++dest_i;
1628                 }
1629         }
1630 }
1631
1632 void *CustomData_em_get(const CustomData *data, void *block, int type)
1633 {
1634         int layer_index;
1635         
1636         /* get the layer index of the first layer of type */
1637         layer_index = CustomData_get_active_layer_index(data, type);
1638         if(layer_index < 0) return NULL;
1639
1640         return (char *)block + data->layers[layer_index].offset;
1641 }
1642
1643 void *CustomData_em_get_n(const CustomData *data, void *block, int type, int n)
1644 {
1645         int layer_index;
1646         
1647         /* get the layer index of the first layer of type */
1648         layer_index = CustomData_get_layer_index(data, type);
1649         if(layer_index < 0) return NULL;
1650
1651         return (char *)block + data->layers[layer_index+n].offset;
1652 }
1653
1654 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
1655 {
1656         void *dest = CustomData_em_get(data, block, type);
1657         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1658
1659         if(!dest) return;
1660
1661         if(typeInfo->copy)
1662                 typeInfo->copy(source, dest, 1);
1663         else
1664                 memcpy(dest, source, typeInfo->size);
1665 }
1666
1667 void CustomData_em_set_n(CustomData *data, void *block, int type, int n, void *source)
1668 {
1669         void *dest = CustomData_em_get_n(data, block, type, n);
1670         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1671
1672         if(!dest) return;
1673
1674         if(typeInfo->copy)
1675                 typeInfo->copy(source, dest, 1);
1676         else
1677                 memcpy(dest, source, typeInfo->size);
1678 }
1679
1680 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
1681                           float *sub_weights, int count, void *dest_block)
1682 {
1683         int i, j;
1684         void *source_buf[SOURCE_BUF_SIZE];
1685         void **sources = source_buf;
1686
1687         /* slow fallback in case we're interpolating a ridiculous number of
1688          * elements
1689          */
1690         if(count > SOURCE_BUF_SIZE)
1691                 sources = MEM_callocN(sizeof(*sources) * count,
1692                                       "CustomData_interp sources");
1693
1694         /* interpolates a layer at a time */
1695         for(i = 0; i < data->totlayer; ++i) {
1696                 CustomDataLayer *layer = &data->layers[i];
1697                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1698
1699                 if(typeInfo->interp) {
1700                         for(j = 0; j < count; ++j)
1701                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1702
1703                         typeInfo->interp(sources, weights, sub_weights, count,
1704                                           (char *)dest_block + layer->offset);
1705                 }
1706         }
1707
1708         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1709 }
1710
1711 void CustomData_em_set_default(CustomData *data, void **block)
1712 {
1713         const LayerTypeInfo *typeInfo;
1714         int i;
1715
1716         if (!*block)
1717                 CustomData_em_alloc_block(data, block);
1718
1719         for(i = 0; i < data->totlayer; ++i) {
1720                 int offset = data->layers[i].offset;
1721
1722                 typeInfo = layerType_getInfo(data->layers[i].type);
1723
1724                 if(typeInfo->set_default)
1725                         typeInfo->set_default((char*)*block + offset, 1);
1726         }
1727 }
1728
1729 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
1730                             int src_index, void **dest_block)
1731 {
1732         const LayerTypeInfo *typeInfo;
1733         int dest_i, src_i, src_offset;
1734
1735         if (!*dest_block)
1736                 CustomData_em_alloc_block(dest, dest_block);
1737         
1738         /* copies a layer at a time */
1739         dest_i = 0;
1740         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1741
1742                 /* find the first dest layer with type >= the source type
1743                  * (this should work because layers are ordered by type)
1744                  */
1745                 while(dest_i < dest->totlayer
1746                       && dest->layers[dest_i].type < source->layers[src_i].type)
1747                         ++dest_i;
1748
1749                 /* if there are no more dest layers, we're done */
1750                 if(dest_i >= dest->totlayer) return;
1751
1752                 /* if we found a matching layer, copy the data */
1753                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1754                         int offset = dest->layers[dest_i].offset;
1755                         char *src_data = source->layers[src_i].data;
1756                         char *dest_data = (char*)*dest_block + offset;
1757
1758                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1759                         src_offset = src_index * typeInfo->size;
1760
1761                         if(typeInfo->copy)
1762                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
1763                         else
1764                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
1765
1766                         /* if there are multiple source & dest layers of the same type,
1767                          * we don't want to copy all source layers to the same dest, so
1768                          * increment dest_i
1769                          */
1770                         ++dest_i;
1771                 }
1772         }
1773 }
1774
1775 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
1776                               void *src_block, int dest_index)
1777 {
1778         const LayerTypeInfo *typeInfo;
1779         int dest_i, src_i, dest_offset;
1780
1781         /* copies a layer at a time */
1782         dest_i = 0;
1783         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1784
1785                 /* find the first dest layer with type >= the source type
1786                  * (this should work because layers are ordered by type)
1787                  */
1788                 while(dest_i < dest->totlayer
1789                       && dest->layers[dest_i].type < source->layers[src_i].type)
1790                         ++dest_i;
1791
1792                 /* if there are no more dest layers, we're done */
1793                 if(dest_i >= dest->totlayer) return;
1794
1795                 /* if we found a matching layer, copy the data */
1796                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1797                         int offset = source->layers[src_i].offset;
1798                         char *src_data = (char*)src_block + offset;
1799                         char *dest_data = dest->layers[dest_i].data;
1800
1801                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1802                         dest_offset = dest_index * typeInfo->size;
1803
1804                         if(typeInfo->copy)
1805                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
1806                         else
1807                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
1808
1809                         /* if there are multiple source & dest layers of the same type,
1810                          * we don't want to copy all source layers to the same dest, so
1811                          * increment dest_i
1812                          */
1813                         ++dest_i;
1814                 }
1815         }
1816
1817 }
1818
1819 /*Bmesh functions*/
1820 /*needed to convert to/from different face reps*/
1821 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata)
1822 {
1823         int i;
1824         for(i=0; i < fdata->totlayer; i++){
1825                 if(fdata->layers[i].type == CD_MTFACE){
1826                         CustomData_add_layer(pdata, CD_MTEXPOLY, CD_CALLOC, &(fdata->layers[i].name), 0);
1827                         CustomData_add_layer(ldata, CD_MLOOPUV, CD_CALLOC, &(fdata->layers[i].name), 0);
1828                 }
1829                 else if(fdata->layers[i].type == CD_MCOL)
1830                         CustomData_add_layer(ldata, CD_MLOOPCOL, CD_CALLOC, &(fdata->layers[i].name), 0);
1831         }               
1832 }
1833 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total){
1834         int i;
1835         for(i=0; i < pdata->totlayer; i++){
1836                 if(pdata->layers[i].type == CD_MTEXPOLY)
1837                         CustomData_add_layer(fdata, CD_MTFACE, CD_CALLOC, &(pdata->layers[i].name), total);
1838         }
1839         for(i=0; i < ldata->totlayer; i++){
1840                 if(ldata->layers[i].type == CD_MLOOPCOL)
1841                         CustomData_add_layer(fdata, CD_MCOL, CD_CALLOC, &(ldata->layers[i].name), total);
1842         }
1843 }
1844
1845
1846 void CustomData_bmesh_init_pool(CustomData *data, int allocsize){
1847         if(data->totlayer)data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize);
1848 }
1849
1850 void CustomData_bmesh_free_block(CustomData *data, void **block)
1851 {
1852     const LayerTypeInfo *typeInfo;
1853     int i;
1854
1855         if(!*block) return;
1856     for(i = 0; i < data->totlayer; ++i) {
1857         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1858             typeInfo = layerType_getInfo(data->layers[i].type);
1859
1860             if(typeInfo->free) {
1861                                 int offset = data->layers[i].offset;
1862                                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
1863                         }
1864         }
1865     }
1866
1867         BLI_mempool_free(data->pool, *block);
1868         *block = NULL;
1869 }
1870
1871 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
1872 {
1873
1874         if (*block)
1875                 CustomData_bmesh_free_block(data, block);
1876
1877         if (data->totsize > 0)
1878                 *block = BLI_mempool_calloc(data->pool);
1879         else
1880                 *block = NULL;
1881 }
1882
1883 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
1884                             void *src_block, void **dest_block)
1885 {
1886         const LayerTypeInfo *typeInfo;
1887         int dest_i, src_i;
1888
1889         if (!*dest_block)
1890                 CustomData_bmesh_alloc_block(dest, dest_block);
1891         
1892         /* copies a layer at a time */
1893         dest_i = 0;
1894         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1895
1896                 /* find the first dest layer with type >= the source type
1897                  * (this should work because layers are ordered by type)
1898                  */
1899                 while(dest_i < dest->totlayer
1900                       && dest->layers[dest_i].type < source->layers[src_i].type)
1901                         ++dest_i;
1902
1903                 /* if there are no more dest layers, we're done */
1904                 if(dest_i >= dest->totlayer) return;
1905
1906                 /* if we found a matching layer, copy the data */
1907                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
1908                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
1909                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1910                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1911
1912                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1913
1914                         if(typeInfo->copy)
1915                                 typeInfo->copy(src_data, dest_data, 1);
1916                         else
1917                                 memcpy(dest_data, src_data, typeInfo->size);
1918
1919                         /* if there are multiple source & dest layers of the same type,
1920                          * we don't want to copy all source layers to the same dest, so
1921                          * increment dest_i
1922                          */
1923                         ++dest_i;
1924                 }
1925         }
1926 }
1927
1928 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
1929 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
1930 {
1931         int layer_index;
1932         
1933         /* get the layer index of the first layer of type */
1934         layer_index = CustomData_get_active_layer_index(data, type);
1935         if(layer_index < 0) return NULL;
1936
1937         return (char *)block + data->layers[layer_index].offset;
1938 }
1939
1940 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
1941 {
1942         int layer_index;
1943         
1944         /* get the layer index of the first layer of type */
1945         layer_index = CustomData_get_layer_index(data, type);
1946         if(layer_index < 0) return NULL;
1947
1948         return (char *)block + data->layers[layer_index+n].offset;
1949 }
1950
1951 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
1952 {
1953         void *dest = CustomData_bmesh_get(data, block, type);
1954         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1955
1956         if(!dest) return;
1957
1958         if(typeInfo->copy)
1959                 typeInfo->copy(source, dest, 1);
1960         else
1961                 memcpy(dest, source, typeInfo->size);
1962 }
1963
1964 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
1965 {
1966         void *dest = CustomData_bmesh_get_n(data, block, type, n);
1967         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1968
1969         if(!dest) return;
1970
1971         if(typeInfo->copy)
1972                 typeInfo->copy(source, dest, 1);
1973         else
1974                 memcpy(dest, source, typeInfo->size);
1975 }
1976
1977 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
1978                           float *sub_weights, int count, void *dest_block)
1979 {
1980         int i, j;
1981         void *source_buf[SOURCE_BUF_SIZE];
1982         void **sources = source_buf;
1983
1984         /* slow fallback in case we're interpolating a ridiculous number of
1985          * elements
1986          */
1987         if(count > SOURCE_BUF_SIZE)
1988                 sources = MEM_callocN(sizeof(*sources) * count,
1989                                       "CustomData_interp sources");
1990
1991         /* interpolates a layer at a time */
1992         for(i = 0; i < data->totlayer; ++i) {
1993                 CustomDataLayer *layer = &data->layers[i];
1994                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1995                 if(typeInfo->interp) {
1996                         for(j = 0; j < count; ++j)
1997                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1998
1999                         typeInfo->interp(sources, weights, sub_weights, count,
2000                                           (char *)dest_block + layer->offset);
2001                 }
2002         }
2003
2004         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2005 }
2006
2007 void CustomData_bmesh_set_default(CustomData *data, void **block)
2008 {
2009         const LayerTypeInfo *typeInfo;
2010         int i;
2011
2012         if (!*block)
2013                 CustomData_bmesh_alloc_block(data, block);
2014
2015         for(i = 0; i < data->totlayer; ++i) {
2016                 int offset = data->layers[i].offset;
2017
2018                 typeInfo = layerType_getInfo(data->layers[i].type);
2019
2020                 if(typeInfo->set_default)
2021                         typeInfo->set_default((char*)*block + offset, 1);
2022         }
2023 }
2024
2025 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2026                             int src_index, void **dest_block)
2027 {
2028         const LayerTypeInfo *typeInfo;
2029         int dest_i, src_i, src_offset;
2030
2031         if (!*dest_block)
2032                 CustomData_bmesh_alloc_block(dest, dest_block);
2033         
2034         /* copies a layer at a time */
2035         dest_i = 0;
2036         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2037
2038                 /* find the first dest layer with type >= the source type
2039                  * (this should work because layers are ordered by type)
2040                  */
2041                 while(dest_i < dest->totlayer
2042                       && dest->layers[dest_i].type < source->layers[src_i].type)
2043                         ++dest_i;
2044
2045                 /* if there are no more dest layers, we're done */
2046                 if(dest_i >= dest->totlayer) return;
2047
2048                 /* if we found a matching layer, copy the data */
2049                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2050                         int offset = dest->layers[dest_i].offset;
2051                         char *src_data = source->layers[src_i].data;
2052                         char *dest_data = (char*)*dest_block + offset;
2053
2054                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2055                         src_offset = src_index * typeInfo->size;
2056
2057                         if(typeInfo->copy)
2058                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
2059                         else
2060                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2061
2062                         /* if there are multiple source & dest layers of the same type,
2063                          * we don't want to copy all source layers to the same dest, so
2064                          * increment dest_i
2065                          */
2066                         ++dest_i;
2067                 }
2068         }
2069 }
2070
2071 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2072                               void *src_block, int dest_index)
2073 {
2074         const LayerTypeInfo *typeInfo;
2075         int dest_i, src_i, dest_offset;
2076
2077         /* copies a layer at a time */
2078         dest_i = 0;
2079         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2080
2081                 /* find the first dest layer with type >= the source type
2082                  * (this should work because layers are ordered by type)
2083                  */
2084                 while(dest_i < dest->totlayer
2085                       && dest->layers[dest_i].type < source->layers[src_i].type)
2086                         ++dest_i;
2087
2088                 /* if there are no more dest layers, we're done */
2089                 if(dest_i >= dest->totlayer) return;
2090
2091                 /* if we found a matching layer, copy the data */
2092                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2093                         int offset = source->layers[src_i].offset;
2094                         char *src_data = (char*)src_block + offset;
2095                         char *dest_data = dest->layers[dest_i].data;
2096
2097                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2098                         dest_offset = dest_index * typeInfo->size;
2099
2100                         if(typeInfo->copy)
2101                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2102                         else
2103                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2104
2105                         /* if there are multiple source & dest layers of the same type,
2106                          * we don't want to copy all source layers to the same dest, so
2107                          * increment dest_i
2108                          */
2109                         ++dest_i;
2110                 }
2111         }
2112
2113 }
2114
2115 void CustomData_file_write_info(int type, char **structname, int *structnum)
2116 {
2117         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2118
2119         *structname = typeInfo->structname;
2120         *structnum = typeInfo->structnum;
2121 }
2122
2123 int CustomData_sizeof(int type)
2124 {
2125         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2126
2127         return typeInfo->size;
2128 }
2129
2130 const char *CustomData_layertype_name(int type)
2131 {
2132         return layerType_getName(type);
2133 }
2134
2135 static int  CustomData_is_property_layer(int type)
2136 {
2137         if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2138                 return 1;
2139         return 0;
2140 }
2141
2142 void CustomData_set_layer_unique_name(CustomData *data, int index)
2143 {
2144         char tempname[64];
2145         int number, i, type;
2146         char *dot, *name;
2147         CustomDataLayer *layer, *nlayer= &data->layers[index];
2148         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2149
2150         if (!typeInfo->defaultname)
2151                 return;
2152
2153         type = nlayer->type;
2154         name = nlayer->name;
2155
2156         if (name[0] == '\0')
2157                 BLI_strncpy(nlayer->name, typeInfo->defaultname, sizeof(nlayer->name));
2158         
2159         /* see if there is a duplicate */
2160         for(i=0; i<data->totlayer; i++) {
2161                 layer = &data->layers[i];
2162                 
2163                 if(CustomData_is_property_layer(type)){
2164                         if(i!=index && CustomData_is_property_layer(layer->type) && 
2165                                 strcmp(layer->name, name)==0)
2166                                         break;  
2167                 
2168                 }
2169                 else{
2170                         if(i!=index && layer->type==type && strcmp(layer->name, name)==0)
2171                                 break;
2172                 }
2173         }
2174
2175         if(i == data->totlayer)
2176                 return;
2177
2178         /* strip off the suffix */
2179         dot = strchr(nlayer->name, '.');
2180         if(dot) *dot=0;
2181         
2182         for(number=1; number <=999; number++) {
2183                 sprintf(tempname, "%s.%03d", nlayer->name, number);
2184
2185                 for(i=0; i<data->totlayer; i++) {
2186                         layer = &data->layers[i];
2187                         
2188                         if(CustomData_is_property_layer(type)){
2189                                 if(i!=index && CustomData_is_property_layer(layer->type) && 
2190                                         strcmp(layer->name, tempname)==0)
2191
2192                                 break;
2193                         }
2194                         else{
2195                                 if(i!=index && layer->type==type && strcmp(layer->name, tempname)==0)
2196                                         break;
2197                         }
2198                 }
2199
2200                 if(i == data->totlayer) {
2201                         BLI_strncpy(nlayer->name, tempname, sizeof(nlayer->name));
2202                         return;
2203                 }
2204         }       
2205 }
2206
2207 int CustomData_verify_versions(struct CustomData *data, int index)
2208 {
2209         const LayerTypeInfo *typeInfo;
2210         CustomDataLayer *layer = &data->layers[index];
2211         int i, keeplayer = 1;
2212
2213         if (layer->type >= CD_NUMTYPES) {
2214                 keeplayer = 0; /* unknown layer type from future version */
2215         }
2216         else {
2217                 typeInfo = layerType_getInfo(layer->type);
2218
2219                 if (!typeInfo->defaultname && (index > 0) &&
2220                         data->layers[index-1].type == layer->type)
2221                         keeplayer = 0; /* multiple layers of which we only support one */
2222         }
2223
2224         if (!keeplayer) {
2225             for (i=index+1; i < data->totlayer; ++i)
2226             data->layers[i-1] = data->layers[i];
2227                 data->totlayer--;
2228         }
2229
2230         return keeplayer;
2231 }
2232