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