misc changes from trunk (avoid confusion since these were not intentional differences)
[blender.git] / source / blender / blenkernel / intern / customdata.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Ben Batt <benbatt@gmail.com>
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  * Implementation of CustomData.
28  *
29  * BKE_customdata.h contains the function prototypes for this file.
30  *
31  */
32
33 /** \file blender/blenkernel/intern/customdata.c
34  *  \ingroup bke
35  */
36  
37
38 #include <math.h>
39 #include <string.h>
40 #include <assert.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "DNA_meshdata_types.h"
45 #include "DNA_ID.h"
46
47 #include "BLI_utildefines.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_linklist.h"
50 #include "BLI_math.h"
51 #include "BLI_mempool.h"
52 #include "BLI_utildefines.h"
53
54 #include "BKE_customdata.h"
55 #include "BKE_customdata_file.h"
56 #include "BKE_global.h"
57 #include "BKE_main.h"
58 #include "BKE_multires.h"
59
60 #include "bmesh.h"
61
62 #include <math.h>
63 #include <string.h>
64
65 /* number of layers to add when growing a CustomData object */
66 #define CUSTOMDATA_GROW 5
67
68 /********************* Layer type information **********************/
69 typedef struct LayerTypeInfo {
70         int size;          /* the memory size of one element of this layer's data */
71         const char *structname;  /* name of the struct used, for file writing */
72         int structnum;     /* number of structs per element, for file writing */
73         const char *defaultname; /* default layer name */
74
75         /* a function to copy count elements of this layer's data
76          * (deep copy if appropriate)
77          * if NULL, memcpy is used
78          */
79         void (*copy)(const void *source, void *dest, int count);
80
81         /* a function to free any dynamically allocated components of this
82          * layer's data (note the data pointer itself should not be freed)
83          * size should be the size of one element of this layer's data (e.g.
84          * LayerTypeInfo.size)
85          */
86         void (*free)(void *data, int count, int size);
87
88         /* a function to interpolate between count source elements of this
89          * layer's data and store the result in dest
90          * if weights == NULL or sub_weights == NULL, they should default to 1
91          *
92          * weights gives the weight for each element in sources
93          * sub_weights gives the sub-element weights for each element in sources
94          *    (there should be (sub element count)^2 weights per element)
95          * count gives the number of elements in sources
96          */
97         void (*interp)(void **sources, float *weights, float *sub_weights,
98                        int count, void *dest);
99
100         /* a function to swap the data in corners of the element */
101         void (*swap)(void *data, const int *corner_indices);
102
103         /* a function to set a layer's data to default values. if NULL, the
104            default is assumed to be all zeros */
105         void (*set_default)(void *data, int count);
106
107     /* functions necassary for geometry collapse*/
108         int (*equal)(void *data1, void *data2);
109         void (*multiply)(void *data, float fac);
110         void (*initminmax)(void *min, void *max);
111         void (*add)(void *data1, void *data2);
112         void (*dominmax)(void *data1, void *min, void *max);
113         void (*copyvalue)(void *source, void *dest);
114
115         /* a function to read data from a cdf file */
116         int (*read)(CDataFile *cdf, void *data, int count);
117
118         /* a function to write data to a cdf file */
119         int (*write)(CDataFile *cdf, void *data, int count);
120
121         /* a function to determine file size */
122         size_t (*filesize)(CDataFile *cdf, void *data, int count);
123
124         /* a function to validate layer contents depending on
125          * sub-elements count
126          */
127         void (*validate)(void *source, int sub_elements);
128 } LayerTypeInfo;
129
130 static void layerCopy_mdeformvert(const void *source, void *dest,
131                                                                   int count)
132 {
133         int i, size = sizeof(MDeformVert);
134
135         memcpy(dest, source, count * size);
136
137         for(i = 0; i < count; ++i) {
138                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
139
140                 if(dvert->totweight) {
141                         MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
142                                                                                         "layerCopy_mdeformvert dw");
143
144                         memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
145                         dvert->dw = dw;
146                 }
147                 else
148                         dvert->dw = NULL;
149         }
150 }
151
152 static void layerFree_mdeformvert(void *data, int count, int size)
153 {
154         int i;
155
156         for(i = 0; i < count; ++i) {
157                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
158
159                 if(dvert->dw) {
160                         MEM_freeN(dvert->dw);
161                         dvert->dw = NULL;
162                         dvert->totweight = 0;
163                 }
164         }
165 }
166
167 static void linklist_free_simple(void *link)
168 {
169         MEM_freeN(link);
170 }
171
172 static void layerInterp_mdeformvert(void **sources, float *weights,
173                                                                         float *UNUSED(sub_weights), int count, void *dest)
174 {
175         MDeformVert *dvert = dest;
176         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
177         LinkNode *node;
178         int i, j, totweight;
179
180         if(count <= 0) return;
181
182         /* build a list of unique def_nrs for dest */
183         totweight = 0;
184         for(i = 0; i < count; ++i) {
185                 MDeformVert *source = sources[i];
186                 float interp_weight = weights ? weights[i] : 1.0f;
187
188                 for(j = 0; j < source->totweight; ++j) {
189                         MDeformWeight *dw = &source->dw[j];
190
191                         for(node = dest_dw; node; node = node->next) {
192                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
193
194                                 if(tmp_dw->def_nr == dw->def_nr) {
195                                         tmp_dw->weight += dw->weight * interp_weight;
196                                         break;
197                                 }
198                         }
199
200                         /* if this def_nr is not in the list, add it */
201                         if(!node) {
202                                 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
203                                                                                         "layerInterp_mdeformvert tmp_dw");
204                                 tmp_dw->def_nr = dw->def_nr;
205                                 tmp_dw->weight = dw->weight * interp_weight;
206                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
207                                 totweight++;
208                         }
209                 }
210         }
211
212         /* now we know how many unique deform weights there are, so realloc */
213         if(dvert->dw) MEM_freeN(dvert->dw);
214
215         if(totweight) {
216                 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
217                                                                 "layerInterp_mdeformvert dvert->dw");
218                 dvert->totweight = totweight;
219
220                 for(i = 0, node = dest_dw; node; node = node->next, ++i)
221                         dvert->dw[i] = *((MDeformWeight *)node->link);
222         }
223         else
224                 memset(dvert, 0, sizeof(*dvert));
225
226         BLI_linklist_free(dest_dw, linklist_free_simple);
227 }
228
229
230 static void layerInterp_msticky(void **sources, float *weights,
231                                                                 float *UNUSED(sub_weights), int count, void *dest)
232 {
233         float co[2], w;
234         MSticky *mst;
235         int i;
236
237         co[0] = co[1] = 0.0f;
238         for(i = 0; i < count; i++) {
239                 w = weights ? weights[i] : 1.0f;
240                 mst = (MSticky*)sources[i];
241
242                 madd_v2_v2fl(co, mst->co, w);
243         }
244
245         mst = (MSticky*)dest;
246         copy_v2_v2(mst->co, co);
247 }
248
249
250 static void layerCopy_tface(const void *source, void *dest, int count)
251 {
252         const MTFace *source_tf = (const MTFace*)source;
253         MTFace *dest_tf = (MTFace*)dest;
254         int i;
255
256         for(i = 0; i < count; ++i)
257                 dest_tf[i] = source_tf[i];
258 }
259
260 static void layerInterp_tface(void **sources, float *weights,
261                                                           float *sub_weights, int count, void *dest)
262 {
263         MTFace *tf = dest;
264         int i, j, k;
265         float uv[4][2] = {{0.0f}};
266         float *sub_weight;
267
268         if(count <= 0) return;
269
270         sub_weight = sub_weights;
271         for(i = 0; i < count; ++i) {
272                 float weight = weights ? weights[i] : 1;
273                 MTFace *src = sources[i];
274
275                 for(j = 0; j < 4; ++j) {
276                         if(sub_weights) {
277                                 for(k = 0; k < 4; ++k, ++sub_weight) {
278                                         madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
279                                 }
280                         }
281                         else {
282                                 madd_v2_v2fl(uv[j], src->uv[j], weight);
283                         }
284                 }
285         }
286
287         *tf = *(MTFace *)(*sources);
288         memcpy(tf->uv, uv, sizeof(tf->uv));
289 }
290
291 static void layerSwap_tface(void *data, const int *corner_indices)
292 {
293         MTFace *tf = data;
294         float uv[4][2];
295         static const short pin_flags[4] =
296                 { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
297         static const char sel_flags[4] =
298                 { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
299         short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
300         char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
301         int j;
302
303         for(j = 0; j < 4; ++j) {
304                 const int source_index = corner_indices[j];
305
306                 copy_v2_v2(uv[j], tf->uv[source_index]);
307
308                 // swap pinning flags around
309                 if(tf->unwrap & pin_flags[source_index]) {
310                         unwrap |= pin_flags[j];
311                 }
312
313                 // swap selection flags around
314                 if(tf->flag & sel_flags[source_index]) {
315                         flag |= sel_flags[j];
316                 }
317         }
318
319         memcpy(tf->uv, uv, sizeof(tf->uv));
320         tf->unwrap = unwrap;
321         tf->flag = flag;
322 }
323
324 static void layerDefault_tface(void *data, int count)
325 {
326         static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
327                                                            0, 0, TF_DYNAMIC|TF_CONVERTED, 0, 0};
328         MTFace *tf = (MTFace*)data;
329         int i;
330
331         for(i = 0; i < count; i++)
332                 tf[i] = default_tf;
333 }
334
335 static void layerCopy_propFloat(const void *source, void *dest,
336                                                                   int count)
337 {
338         memcpy(dest, source, sizeof(MFloatProperty)*count);
339 }
340
341 static void layerCopy_propInt(const void *source, void *dest,
342                                                                   int count)
343 {
344         memcpy(dest, source, sizeof(MIntProperty)*count);
345 }
346
347 static void layerCopy_propString(const void *source, void *dest,
348                                                                   int count)
349 {
350         memcpy(dest, source, sizeof(MStringProperty)*count);
351 }
352
353 static void layerCopy_origspace_face(const void *source, void *dest, int count)
354 {
355         const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
356         OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
357         int i;
358
359         for(i = 0; i < count; ++i)
360                 dest_tf[i] = source_tf[i];
361 }
362
363 static void layerInterp_origspace_face(void **sources, float *weights,
364                                                           float *sub_weights, int count, void *dest)
365 {
366         OrigSpaceFace *osf = dest;
367         int i, j, k;
368         float uv[4][2] = {{0.0f}};
369         float *sub_weight;
370
371         if(count <= 0) return;
372
373         sub_weight = sub_weights;
374         for(i = 0; i < count; ++i) {
375                 float weight = weights ? weights[i] : 1;
376                 OrigSpaceFace *src = sources[i];
377
378                 for(j = 0; j < 4; ++j) {
379                         if(sub_weights) {
380                                 for(k = 0; k < 4; ++k, ++sub_weight) {
381                                         madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
382                                 }
383                         } else {
384                                 madd_v2_v2fl(uv[j], src->uv[j], weight);
385                         }
386                 }
387         }
388
389 #if 0 /* no need, this ONLY contains UV's */
390         *osf = *(OrigSpaceFace *)(*sources);
391 #endif
392         memcpy(osf->uv, uv, sizeof(osf->uv));
393 }
394
395 static void layerSwap_origspace_face(void *data, const int *corner_indices)
396 {
397         OrigSpaceFace *osf = data;
398         float uv[4][2];
399         int j;
400
401         for(j = 0; j < 4; ++j) {
402                 copy_v2_v2(uv[j], osf->uv[corner_indices[j]]);
403         }
404         memcpy(osf->uv, uv, sizeof(osf->uv));
405 }
406
407 static void layerDefault_origspace_face(void *data, int count)
408 {
409         static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
410         OrigSpaceFace *osf = (OrigSpaceFace*)data;
411         int i;
412
413         for(i = 0; i < count; i++)
414                 osf[i] = default_osf;
415 }
416
417 static void layerSwap_mdisps(void *data, const int *ci)
418 {
419         MDisps *s = data;
420         float (*d)[3] = NULL;
421         int corners, cornersize, S;
422
423         if(s->disps) {
424                 int nverts= (ci[1] == 3) ? 4 : 3; /* silly way to know vertex count of face */
425                 corners= multires_mdisp_corners(s);
426                 cornersize= s->totdisp/corners;
427
428                 if(corners!=nverts) {
429                         /* happens when face changed vertex count in edit mode
430                            if it happened, just forgot displacement */
431
432                         MEM_freeN(s->disps);
433                         s->totdisp= (s->totdisp/corners)*nverts;
434                         s->disps= MEM_callocN(s->totdisp*sizeof(float)*3, "mdisp swap");
435                         return;
436                 }
437
438                 d= MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
439
440                 for(S = 0; S < corners; S++)
441                         memcpy(d + cornersize*S, s->disps + cornersize*ci[S], cornersize*3*sizeof(float));
442                 
443                 MEM_freeN(s->disps);
444                 s->disps= d;
445         }
446 }
447
448 #if 1 /* BMESH_TODO: place holder function, dont actually interp */
449 static void layerInterp_mdisps(void **sources, float *UNUSED(weights),
450                                 float *UNUSED(sub_weights), int UNUSED(count), void *dest)
451 {
452         MDisps *d = dest;
453
454         /* happens when flipping normals of newly created mesh */
455         if(!d->totdisp) {
456                 d->totdisp = ((MDisps*)sources[0])->totdisp;
457         }
458
459         if (!d->disps && d->totdisp)
460                 d->disps = MEM_callocN(sizeof(float)*3*d->totdisp, "blank mdisps in layerInterp_mdisps");
461 }
462
463 #else // BMESH_TODO
464
465 static void layerInterp_mdisps(void **sources, float *UNUSED(weights),
466                                 float *sub_weights, int count, void *dest)
467 {
468         MDisps *d = dest;
469         MDisps *s = NULL;
470         int st, stl;
471         int i, x, y;
472         int side, S, dst_corners, src_corners;
473         float crn_weight[4][2];
474         float (*sw)[4] = (void*)sub_weights;
475         float (*disps)[3], (*out)[3];
476
477         /* happens when flipping normals of newly created mesh */
478         if(!d->totdisp)
479                 return;
480
481         s = sources[0];
482         dst_corners = multires_mdisp_corners(d);
483         src_corners = multires_mdisp_corners(s);
484
485         if(sub_weights && count == 2 && src_corners == 3) {
486                 src_corners = multires_mdisp_corners(sources[1]);
487
488                 /* special case -- converting two triangles to quad */
489                 if(src_corners == 3 && dst_corners == 4) {
490                         MDisps tris[2];
491                         int vindex[4] = {0};
492
493                         for(i = 0; i < 2; i++)
494                                 for(y = 0; y < 4; y++)
495                                         for(x = 0; x < 4; x++)
496                                                 if(sw[x+i*4][y])
497                                                         vindex[x] = y;
498
499                         for(i = 0; i < 2; i++) {
500                                 float sw_m4[4][4] = {{0}};
501                                 int a = 7 & ~(1 << vindex[i*2] | 1 << vindex[i*2+1]);
502
503                                 sw_m4[0][vindex[i*2+1]] = 1;
504                                 sw_m4[1][vindex[i*2]] = 1;
505
506                                 for(x = 0; x < 3; x++)
507                                         if(a & (1 << x))
508                                                 sw_m4[2][x] = 1;
509
510                                 tris[i] = *((MDisps*)sources[i]);
511                                 tris[i].disps = MEM_dupallocN(tris[i].disps);
512                                 layerInterp_mdisps(&sources[i], NULL, (float*)sw_m4, 1, &tris[i]);
513                         }
514
515                         mdisp_join_tris(d, &tris[0], &tris[1]);
516
517                         for(i = 0; i < 2; i++)
518                                 MEM_freeN(tris[i].disps);
519
520                         return;
521                 }
522         }
523
524         /* For now, some restrictions on the input */
525         if(count != 1 || !sub_weights) {
526                 for(i = 0; i < d->totdisp; ++i)
527                         zero_v3(d->disps[i]);
528
529                 return;
530         }
531
532         /* Initialize the destination */
533         disps = MEM_callocN(3*d->totdisp*sizeof(float), "iterp disps");
534
535         side = sqrt(d->totdisp / dst_corners);
536         st = (side<<1)-1;
537         stl = st - 1;
538
539         sw= (void*)sub_weights;
540         for(i = 0; i < 4; ++i) {
541                 crn_weight[i][0] = 0 * sw[i][0] + stl * sw[i][1] + stl * sw[i][2] + 0 * sw[i][3];
542                 crn_weight[i][1] = 0 * sw[i][0] + 0 * sw[i][1] + stl * sw[i][2] + stl * sw[i][3];
543         }
544
545         multires_mdisp_smooth_bounds(s);
546
547         out = disps;
548         for(S = 0; S < dst_corners; S++) {
549                 float base[2], axis_x[2], axis_y[2];
550
551                 mdisp_apply_weight(S, dst_corners, 0, 0, st, crn_weight, &base[0], &base[1]);
552                 mdisp_apply_weight(S, dst_corners, side-1, 0, st, crn_weight, &axis_x[0], &axis_x[1]);
553                 mdisp_apply_weight(S, dst_corners, 0, side-1, st, crn_weight, &axis_y[0], &axis_y[1]);
554
555                 sub_v2_v2(axis_x, base);
556                 sub_v2_v2(axis_y, base);
557                 normalize_v2(axis_x);
558                 normalize_v2(axis_y);
559
560                 for(y = 0; y < side; ++y) {
561                         for(x = 0; x < side; ++x, ++out) {
562                                 int crn;
563                                 float face_u, face_v, crn_u, crn_v;
564
565                                 mdisp_apply_weight(S, dst_corners, x, y, st, crn_weight, &face_u, &face_v);
566                                 crn = mdisp_rot_face_to_quad_crn(src_corners, st, face_u, face_v, &crn_u, &crn_v);
567
568                                 old_mdisps_bilinear((*out), &s->disps[crn*side*side], side, crn_u, crn_v);
569                                 mdisp_flip_disp(crn, dst_corners, axis_x, axis_y, *out);
570                         }
571                 }
572         }
573
574         MEM_freeN(d->disps);
575         d->disps = disps;
576 }
577 #endif // BMESH_TODO
578
579 static void layerCopy_mdisps(const void *source, void *dest, int count)
580 {
581         int i;
582         const MDisps *s = source;
583         MDisps *d = dest;
584
585         for(i = 0; i < count; ++i) {
586                 if(s[i].disps) {
587                         d[i].disps = MEM_dupallocN(s[i].disps);
588                         d[i].totdisp = s[i].totdisp;
589                 }
590                 else {
591                         d[i].disps = NULL;
592                         d[i].totdisp = 0;
593                 }
594                 
595         }
596 }
597
598 static void layerValidate_mdisps(void *data, int sub_elements)
599 {
600 #if 1 /*BMESH_TODO*/
601         (void)data;
602         (void)sub_elements;
603 #else
604         MDisps *disps = data;
605         if(disps->disps) {
606                 int corners = multires_mdisp_corners(disps);
607
608                 if(corners != sub_elements) {
609                         MEM_freeN(disps->disps);
610                         disps->totdisp = disps->totdisp / corners * sub_elements;
611                         disps->disps = MEM_callocN(3*disps->totdisp*sizeof(float), "layerValidate_mdisps");
612                 }
613         }
614 #endif
615 }
616
617 static void layerFree_mdisps(void *data, int count, int UNUSED(size))
618 {
619         int i;
620         MDisps *d = data;
621
622         for(i = 0; i < count; ++i) {
623                 if(d[i].disps)
624                         MEM_freeN(d[i].disps);
625                 d[i].disps = NULL;
626                 d[i].totdisp = 0;
627         }
628 }
629
630 static int layerRead_mdisps(CDataFile *cdf, void *data, int count)
631 {
632         MDisps *d = data;
633         int i;
634
635         for(i = 0; i < count; ++i) {
636                 if(!d[i].disps)
637                         d[i].disps = MEM_callocN(sizeof(float)*3*d[i].totdisp, "mdisps read");
638
639                 if(!cdf_read_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
640                         printf("failed to read multires displacement %d/%d %d\n", i, count, d[i].totdisp);
641                         return 0;
642                 }
643         }
644
645         return 1;
646 }
647
648 static int layerWrite_mdisps(CDataFile *cdf, void *data, int count)
649 {
650         MDisps *d = data;
651         int i;
652
653         for(i = 0; i < count; ++i) {
654                 if(!cdf_write_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
655                         printf("failed to write multires displacement %d/%d %d\n", i, count, d[i].totdisp);
656                         return 0;
657                 }
658         }
659
660         return 1;
661 }
662
663 static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), void *data, int count)
664 {
665         MDisps *d = data;
666         size_t size = 0;
667         int i;
668
669         for(i = 0; i < count; ++i)
670                 size += d[i].totdisp*3*sizeof(float);
671
672         return size;
673 }
674
675 /* --------- */
676 static void layerCopyValue_mloopcol(void *source, void *dest)
677 {
678         MLoopCol *m1 = source, *m2 = dest;
679         
680         m2->r = m1->r;
681         m2->g = m1->g;
682         m2->b = m1->b;
683         m2->a = m1->a;
684 }
685
686 static int layerEqual_mloopcol(void *data1, void *data2)
687 {
688         MLoopCol *m1 = data1, *m2 = data2;
689         float r, g, b, a;
690
691         r = m1->r - m2->r;
692         g = m1->g - m2->g;
693         b = m1->b - m2->b;
694         a = m1->a - m2->a;
695
696         return r*r + g*g + b*b + a*a < 0.001;
697 }
698
699 static void layerMultiply_mloopcol(void *data, float fac)
700 {
701         MLoopCol *m = data;
702
703         m->r = (float)m->r * fac;
704         m->g = (float)m->g * fac;
705         m->b = (float)m->b * fac;
706         m->a = (float)m->a * fac;
707 }
708
709 static void layerAdd_mloopcol(void *data1, void *data2)
710 {
711         MLoopCol *m = data1, *m2 = data2;
712
713         m->r += m2->r;
714         m->g += m2->g;
715         m->b += m2->b;
716         m->a += m2->a;
717 }
718
719 static void layerDoMinMax_mloopcol(void *data, void *vmin, void *vmax)
720 {
721         MLoopCol *m = data;
722         MLoopCol *min = vmin, *max = vmax;
723
724         if (m->r < min->r) min->r = m->r;
725         if (m->g < min->g) min->g = m->g;
726         if (m->b < min->b) min->b = m->b;
727         if (m->a < min->a) min->a = m->a;
728         
729         if (m->r > max->r) max->r = m->r;
730         if (m->g > max->g) max->g = m->g;
731         if (m->b > max->b) max->b = m->b;
732         if (m->a > max->a) max->a = m->a;
733 }
734
735 static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
736 {
737         MLoopCol *min = vmin, *max = vmax;
738
739         min->r = 255;
740         min->g = 255;
741         min->b = 255;
742         min->a = 255;
743
744         max->r = 0;
745         max->g = 0;
746         max->b = 0;
747         max->a = 0;
748 }
749
750 static void layerDefault_mloopcol(void *data, int count)
751 {
752         MLoopCol default_mloopcol = {255,255,255,255};
753         MLoopCol *mlcol = (MLoopCol*)data;
754         int i;
755         for(i = 0; i < count; i++)
756                 mlcol[i] = default_mloopcol;
757
758 }
759
760 static void layerInterp_mloopcol(void **sources, float *weights,
761                                 float *sub_weights, int count, void *dest)
762 {
763         MLoopCol *mc = dest;
764         int i;
765         float *sub_weight;
766         struct {
767                 float a;
768                 float r;
769                 float g;
770                 float b;
771         } col;
772         col.a = col.r = col.g = col.b = 0;
773
774         sub_weight = sub_weights;
775         for(i = 0; i < count; ++i){
776                 float weight = weights ? weights[i] : 1;
777                 MLoopCol *src = sources[i];
778                 if(sub_weights){
779                         col.a += src->a * (*sub_weight) * weight;
780                         col.r += src->r * (*sub_weight) * weight;
781                         col.g += src->g * (*sub_weight) * weight;
782                         col.b += src->b * (*sub_weight) * weight;
783                         sub_weight++;
784                 } else {
785                         col.a += src->a * weight;
786                         col.r += src->r * weight;
787                         col.g += src->g * weight;
788                         col.b += src->b * weight;
789                 }
790         }
791         
792         /* Subdivide smooth or fractal can cause problems without clamping
793          * although weights should also not cause this situation */
794         CLAMP(col.a, 0.0f, 255.0f);
795         CLAMP(col.r, 0.0f, 255.0f);
796         CLAMP(col.g, 0.0f, 255.0f);
797         CLAMP(col.b, 0.0f, 255.0f);
798         
799         mc->a = (int)col.a;
800         mc->r = (int)col.r;
801         mc->g = (int)col.g;
802         mc->b = (int)col.b;
803 }
804
805 static void layerCopyValue_mloopuv(void *source, void *dest)
806 {
807         MLoopUV *luv1 = source, *luv2 = dest;
808
809         copy_v2_v2(luv2->uv, luv1->uv);
810 }
811
812 static int layerEqual_mloopuv(void *data1, void *data2)
813 {
814         MLoopUV *luv1 = data1, *luv2 = data2;
815
816         return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
817 }
818
819 static void layerMultiply_mloopuv(void *data, float fac)
820 {
821         MLoopUV *luv = data;
822
823         mul_v2_fl(luv->uv, fac);
824 }
825
826 static void layerInitMinMax_mloopuv(void *vmin, void *vmax)
827 {
828         MLoopUV *min = vmin, *max = vmax;
829
830         INIT_MINMAX2(min->uv, max->uv);
831 }
832
833 static void layerDoMinMax_mloopuv(void *data, void *vmin, void *vmax)
834 {
835         MLoopUV *min = vmin, *max = vmax, *luv = data;
836
837         DO_MINMAX2(luv->uv, min->uv, max->uv);
838 }
839
840 static void layerAdd_mloopuv(void *data1, void *data2)
841 {
842         MLoopUV *l1 = data1, *l2 = data2;
843
844         add_v2_v2(l1->uv, l2->uv);
845 }
846
847 static void layerInterp_mloopuv(void **sources, float *weights,
848                                 float *sub_weights, int count, void *dest)
849 {
850         MLoopUV *mluv = dest;
851         float *uv= mluv->uv;
852         int i;
853
854         zero_v2(uv);
855
856         if (sub_weights) {
857                 const float *sub_weight = sub_weights;
858                 for(i = 0; i < count; i++) {
859                         float weight = weights ? weights[i] : 1.0f;
860                         MLoopUV *src = sources[i];
861                         madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
862                         sub_weight++;
863                 }
864         }
865         else {
866                 for(i = 0; i < count; i++) {
867                         float weight = weights ? weights[i] : 1;
868                         MLoopUV *src = sources[i];
869                         madd_v2_v2fl(uv, src->uv, weight);
870                 }
871         }
872 }
873
874 /* origspace is almost exact copy of mloopuv's, keep in sync */
875 static void layerCopyValue_mloop_origspace(void *source, void *dest)
876 {
877         OrigSpaceLoop *luv1 = source, *luv2 = dest;
878
879         copy_v2_v2(luv2->uv, luv1->uv);
880 }
881
882 static int layerEqual_mloop_origspace(void *data1, void *data2)
883 {
884         OrigSpaceLoop *luv1 = data1, *luv2 = data2;
885
886         return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
887 }
888
889 static void layerMultiply_mloop_origspace(void *data, float fac)
890 {
891         OrigSpaceLoop *luv = data;
892
893         mul_v2_fl(luv->uv, fac);
894 }
895
896 static void layerInitMinMax_mloop_origspace(void *vmin, void *vmax)
897 {
898         OrigSpaceLoop *min = vmin, *max = vmax;
899
900         INIT_MINMAX2(min->uv, max->uv);
901 }
902
903 static void layerDoMinMax_mloop_origspace(void *data, void *vmin, void *vmax)
904 {
905         OrigSpaceLoop *min = vmin, *max = vmax, *luv = data;
906
907         DO_MINMAX2(luv->uv, min->uv, max->uv);
908 }
909
910 static void layerAdd_mloop_origspace(void *data1, void *data2)
911 {
912         OrigSpaceLoop *l1 = data1, *l2 = data2;
913
914         add_v2_v2(l1->uv, l2->uv);
915 }
916
917 static void layerInterp_mloop_origspace(void **sources, float *weights,
918                                 float *sub_weights, int count, void *dest)
919 {
920         OrigSpaceLoop *mluv = dest;
921         float *uv= mluv->uv;
922         int i;
923
924         zero_v2(uv);
925
926         if (sub_weights) {
927                 const float *sub_weight = sub_weights;
928                 for(i = 0; i < count; i++) {
929                         float weight = weights ? weights[i] : 1.0f;
930                         OrigSpaceLoop *src = sources[i];
931                         madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
932                         sub_weight++;
933                 }
934         }
935         else {
936                 for(i = 0; i < count; i++) {
937                         float weight = weights ? weights[i] : 1;
938                         OrigSpaceLoop *src = sources[i];
939                         madd_v2_v2fl(uv, src->uv, weight);
940                 }
941         }
942 }
943 /* --- end copy */
944
945 static void layerInterp_mcol(void **sources, float *weights,
946                                                          float *sub_weights, int count, void *dest)
947 {
948         MCol *mc = dest;
949         int i, j, k;
950         struct {
951                 float a;
952                 float r;
953                 float g;
954                 float b;
955         } col[4] = {{0.0f}};
956
957         float *sub_weight;
958
959         if(count <= 0) return;
960         
961         sub_weight = sub_weights;
962         for(i = 0; i < count; ++i) {
963                 float weight = weights ? weights[i] : 1;
964
965                 for(j = 0; j < 4; ++j) {
966                         if(sub_weights) {
967                                 MCol *src = sources[i];
968                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
969                                         const float w= (*sub_weight) * weight;
970                                         col[j].a += src->a * w;
971                                         col[j].r += src->r * w;
972                                         col[j].g += src->g * w;
973                                         col[j].b += src->b * w;
974                                 }
975                         } else {
976                                 MCol *src = sources[i];
977                                 col[j].a += src[j].a * weight;
978                                 col[j].r += src[j].r * weight;
979                                 col[j].g += src[j].g * weight;
980                                 col[j].b += src[j].b * weight;
981                         }
982                 }
983         }
984
985         for(j = 0; j < 4; ++j) {
986                 
987                 /* Subdivide smooth or fractal can cause problems without clamping
988                  * although weights should also not cause this situation */
989                 CLAMP(col[j].a, 0.0f, 255.0f);
990                 CLAMP(col[j].r, 0.0f, 255.0f);
991                 CLAMP(col[j].g, 0.0f, 255.0f);
992                 CLAMP(col[j].b, 0.0f, 255.0f);
993                 
994                 mc[j].a = (int)col[j].a;
995                 mc[j].r = (int)col[j].r;
996                 mc[j].g = (int)col[j].g;
997                 mc[j].b = (int)col[j].b;
998         }
999 }
1000
1001 static void layerSwap_mcol(void *data, const int *corner_indices)
1002 {
1003         MCol *mcol = data;
1004         MCol col[4];
1005         int j;
1006
1007         for(j = 0; j < 4; ++j)
1008                 col[j] = mcol[corner_indices[j]];
1009
1010         memcpy(mcol, col, sizeof(col));
1011 }
1012
1013 static void layerDefault_mcol(void *data, int count)
1014 {
1015         static MCol default_mcol = {255, 255, 255, 255};
1016         MCol *mcol = (MCol*)data;
1017         int i;
1018
1019         for(i = 0; i < 4*count; i++) {
1020                 mcol[i] = default_mcol;
1021         }
1022 }
1023
1024 static void layerInterp_bweight(void **sources, float *weights,
1025                                 float *UNUSED(sub_weights), int count, void *dest)
1026 {
1027         float *f = dest;
1028         float **in = (float **)sources;
1029         int i;
1030         
1031         if(count <= 0) return;
1032
1033         *f = 0.0f;
1034
1035         if (weights) {
1036                 for(i = 0; i < count; ++i) {
1037                         *f += *in[i] * weights[i];
1038                 }
1039         }
1040         else {
1041                 for(i = 0; i < count; ++i) {
1042                         *f += *in[i];
1043                 }
1044         }
1045 }
1046
1047 static void layerInterp_shapekey(void **sources, float *weights,
1048                                  float *UNUSED(sub_weights), int count, void *dest)
1049 {
1050         float *co = dest;
1051         float **in = (float **)sources;
1052         int i;
1053
1054         if(count <= 0) return;
1055
1056         zero_v3(co);
1057
1058         if (weights) {
1059                 for(i = 0; i < count; ++i) {
1060                         madd_v3_v3fl(co, in[i], weights[i]);
1061                 }
1062         }
1063         else {
1064                 for(i = 0; i < count; ++i) {
1065                         add_v3_v3(co, in[i]);
1066                 }
1067         }
1068 }
1069
1070 static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
1071         /* 0: CD_MVERT */
1072         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1073         /* 1: CD_MSTICKY */
1074         {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
1075          NULL},
1076         /* 2: CD_MDEFORMVERT */
1077         {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
1078          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
1079         /* 3: CD_MEDGE */
1080         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1081         /* 4: CD_MFACE */
1082         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1083         /* 5: CD_MTFACE */
1084         {sizeof(MTFace), "MTFace", 1, "UVMap", layerCopy_tface, NULL,
1085          layerInterp_tface, layerSwap_tface, layerDefault_tface},
1086         /* 6: CD_MCOL */
1087         /* 4 MCol structs per face */
1088         {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
1089          layerSwap_mcol, layerDefault_mcol},
1090         /* 7: CD_ORIGINDEX */
1091         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1092         /* 8: CD_NORMAL */
1093         /* 3 floats per normal vector */
1094         {sizeof(float)*3, "vec3f", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1095         /* 9: CD_POLYINDEX */
1096         {sizeof(int), "MIntProperty", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1097         /* 10: CD_PROP_FLT */
1098         {sizeof(MFloatProperty), "MFloatProperty",1,"Float", layerCopy_propFloat,NULL,NULL,NULL},
1099         /* 11: CD_PROP_INT */
1100         {sizeof(MIntProperty), "MIntProperty",1,"Int",layerCopy_propInt,NULL,NULL,NULL},
1101         /* 12: CD_PROP_STR */
1102         {sizeof(MStringProperty), "MStringProperty",1,"String",layerCopy_propString,NULL,NULL,NULL},
1103         /* 13: CD_ORIGSPACE */
1104         {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVMap", layerCopy_origspace_face, NULL,
1105          layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
1106         /* 14: CD_ORCO */
1107         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1108         /* 15: CD_MTEXPOLY */
1109         {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
1110         /* 16: CD_MLOOPUV */
1111         {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
1112          layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv, 
1113          layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
1114         /* 17: CD_MLOOPCOL */
1115         {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, 
1116          layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol, 
1117          layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1118         /* 18: CD_TANGENT */
1119         {sizeof(float)*4*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1120         /* 19: CD_MDISPS */
1121         {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
1122          layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL,
1123          NULL, NULL, NULL, NULL, NULL, NULL, 
1124          layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps, layerValidate_mdisps},
1125         /* 20: CD_WEIGHT_MCOL */
1126         {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
1127          layerSwap_mcol, layerDefault_mcol},
1128         /* 21: CD_ID_MCOL */
1129         {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
1130          layerSwap_mcol, layerDefault_mcol},
1131         /* 22: CD_TEXTURE_MCOL */
1132         {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
1133          layerSwap_mcol, layerDefault_mcol},
1134         /* 23: CD_CLOTH_ORCO */
1135         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1136         /* 24: CD_RECAST */
1137         {sizeof(MRecast), "MRecast", 1,"Recast",NULL,NULL,NULL,NULL}
1138
1139 /* BMESH ONLY */
1140         ,
1141         /* 25: CD_MPOLY */
1142         {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
1143         /* 26: CD_MLOOP */
1144         {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
1145         /* 27: CD_SHAPE_KEYINDEX */
1146         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1147         /* 28: CD_SHAPEKEY */
1148         {sizeof(float)*3, "", 0, "ShapeKey", NULL, NULL, layerInterp_shapekey},
1149         /* 29: CD_BWEIGHT */
1150         {sizeof(float), "", 0, "BevelWeight", NULL, NULL, layerInterp_bweight},
1151         /* 30: CD_CREASE */
1152         {sizeof(float), "", 0, "SubSurfCrease", NULL, NULL, layerInterp_bweight},
1153     /* 31: CD_ORIGSPACE_MLOOP */
1154         {sizeof(OrigSpaceLoop), "OrigSpaceLoop", 1, "OS Loop", NULL, NULL, layerInterp_mloop_origspace, NULL, NULL,
1155          layerEqual_mloop_origspace, layerMultiply_mloop_origspace, layerInitMinMax_mloop_origspace,
1156          layerAdd_mloop_origspace, layerDoMinMax_mloop_origspace, layerCopyValue_mloop_origspace},
1157         /* 32: CD_WEIGHT_MLOOPCOL */
1158         {sizeof(MLoopCol), "MLoopCol", 1, "WeightLoopCol", NULL, NULL, layerInterp_mloopcol, NULL,
1159          layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1160          layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1161 /* END BMESH ONLY */
1162
1163
1164 };
1165
1166 /* note, numbers are from trunk and need updating for bmesh */
1167
1168 static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
1169         /*   0-4 */ "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace",
1170         /*   5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
1171         /* 10-14 */ "CDMFloatProperty", "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco",
1172         /* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
1173         /* 20-24 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco", "CDMRecast"
1174
1175 /* BMESH ONLY */
1176         ,
1177         /* 25-29 */ "CDMPoly", "CDMLoop", "CDShapeKeyIndex", "CDShapeKey", "CDBevelWeight",
1178         /* 30-32 */ "CDSubSurfCrease", "CDOrigSpaceLoop", "CDWeightLoopCol"
1179 /* END BMESH ONLY */
1180
1181 };
1182
1183
1184 const CustomDataMask CD_MASK_BAREMESH =
1185         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY | CD_MASK_BWEIGHT;
1186 const CustomDataMask CD_MASK_MESH =
1187         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
1188         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
1189         CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS |
1190         CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MPOLY | CD_MASK_MLOOP |
1191         CD_MASK_MTEXPOLY | CD_MASK_NORMAL | CD_MASK_RECAST;
1192 const CustomDataMask CD_MASK_EDITMESH =
1193         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MLOOPUV |
1194         CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_SHAPE_KEYINDEX |
1195         CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR |
1196         CD_MASK_MDISPS | CD_MASK_SHAPEKEY | CD_MASK_RECAST;
1197 const CustomDataMask CD_MASK_DERIVEDMESH =
1198         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
1199         CD_MASK_MCOL | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_CLOTH_ORCO |
1200         CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_WEIGHT_MLOOPCOL |
1201         CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORIGSPACE_MLOOP | CD_MASK_ORCO | CD_MASK_TANGENT |
1202         CD_MASK_WEIGHT_MCOL | CD_MASK_NORMAL | CD_MASK_SHAPEKEY | CD_MASK_RECAST |
1203         CD_MASK_ORIGINDEX | CD_MASK_POLYINDEX;
1204 const CustomDataMask CD_MASK_BMESH = CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
1205         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | 
1206         CD_MASK_PROP_STR | CD_MASK_SHAPEKEY | CD_MASK_SHAPE_KEYINDEX | CD_MASK_MDISPS;
1207 const CustomDataMask CD_MASK_FACECORNERS =
1208         CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
1209         CD_MASK_MLOOPCOL;
1210
1211 static const LayerTypeInfo *layerType_getInfo(int type)
1212 {
1213         if(type < 0 || type >= CD_NUMTYPES) return NULL;
1214
1215         return &LAYERTYPEINFO[type];
1216 }
1217
1218 static const char *layerType_getName(int type)
1219 {
1220         if(type < 0 || type >= CD_NUMTYPES) return NULL;
1221
1222         return LAYERTYPENAMES[type];
1223 }
1224
1225 /********************* CustomData functions *********************/
1226 static void customData_update_offsets(CustomData *data);
1227
1228 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1229         int type, int alloctype, void *layerdata, int totelem, const char *name);
1230
1231 void CustomData_update_typemap(CustomData *data)
1232 {
1233         int i, lasttype = -1;
1234
1235         /* since we cant do in a pre-processor do here as an assert */
1236         BLI_assert(sizeof(data->typemap) / sizeof(int) >= CD_NUMTYPES);
1237
1238         for (i=0; i<CD_NUMTYPES; i++) {
1239                 data->typemap[i] = -1;
1240         }
1241
1242         for (i=0; i<data->totlayer; i++) {
1243                 if (data->layers[i].type != lasttype) {
1244                         data->typemap[data->layers[i].type] = i;
1245                 }
1246                 lasttype = data->layers[i].type;
1247         }
1248 }
1249
1250 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
1251                                           CustomDataMask mask, int alloctype, int totelem)
1252 {
1253         /*const LayerTypeInfo *typeInfo;*/
1254         CustomDataLayer *layer, *newlayer;
1255         void *data;
1256         int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0, lastflag = 0;
1257
1258         for(i = 0; i < source->totlayer; ++i) {
1259                 layer = &source->layers[i];
1260                 /*typeInfo = layerType_getInfo(layer->type);*/ /*UNUSED*/
1261
1262                 type = layer->type;
1263
1264                 if (type != lasttype) {
1265                         number = 0;
1266                         lastactive = layer->active;
1267                         lastrender = layer->active_rnd;
1268                         lastclone = layer->active_clone;
1269                         lastmask = layer->active_mask;
1270                         lasttype = type;
1271                         lastflag = layer->flag;
1272                 }
1273                 else
1274                         number++;
1275
1276                 if(lastflag & CD_FLAG_NOCOPY) continue;
1277                 else if(!(mask & CD_TYPE_AS_MASK(type))) continue;
1278                 else if(number < CustomData_number_of_layers(dest, type)) continue;
1279
1280                 switch (alloctype) {
1281                         case CD_ASSIGN:
1282                         case CD_REFERENCE:
1283                         case CD_DUPLICATE:
1284                                 data = layer->data;
1285                                 break;
1286                         default:
1287                                 data = NULL;
1288                                 break;
1289                 }
1290
1291                 if((alloctype == CD_ASSIGN) && (lastflag & CD_FLAG_NOFREE))
1292                         newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
1293                                 data, totelem, layer->name);
1294                 else
1295                         newlayer = customData_add_layer__internal(dest, type, alloctype,
1296                                 data, totelem, layer->name);
1297                 
1298                 if(newlayer) {
1299                         newlayer->uid = layer->uid;
1300                         
1301                         newlayer->active = lastactive;
1302                         newlayer->active_rnd = lastrender;
1303                         newlayer->active_clone = lastclone;
1304                         newlayer->active_mask = lastmask;
1305                         newlayer->flag |= lastflag & (CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY);
1306                 }
1307         }
1308
1309         CustomData_update_typemap(dest);
1310 }
1311
1312 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
1313                                          CustomDataMask mask, int alloctype, int totelem)
1314 {
1315         memset(dest, 0, sizeof(*dest));
1316
1317         if(source->external)
1318                 dest->external= MEM_dupallocN(source->external);
1319
1320         CustomData_merge(source, dest, mask, alloctype, totelem);
1321 }
1322
1323 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
1324 {
1325         const LayerTypeInfo *typeInfo;
1326
1327         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
1328                 typeInfo = layerType_getInfo(layer->type);
1329
1330                 if(typeInfo->free)
1331                         typeInfo->free(layer->data, totelem, typeInfo->size);
1332
1333                 if(layer->data)
1334                         MEM_freeN(layer->data);
1335         }
1336 }
1337
1338 static void CustomData_external_free(CustomData *data)
1339 {
1340         if(data->external) {
1341                 MEM_freeN(data->external);
1342                 data->external= NULL;
1343         }
1344 }
1345
1346 void CustomData_free(CustomData *data, int totelem)
1347 {
1348         int i;
1349
1350         for(i = 0; i < data->totlayer; ++i)
1351                 customData_free_layer__internal(&data->layers[i], totelem);
1352
1353         if(data->layers)
1354                 MEM_freeN(data->layers);
1355         
1356         CustomData_external_free(data);
1357         
1358         memset(data, 0, sizeof(*data));
1359 }
1360
1361 static void customData_update_offsets(CustomData *data)
1362 {
1363         const LayerTypeInfo *typeInfo;
1364         int i, offset = 0;
1365
1366         for(i = 0; i < data->totlayer; ++i) {
1367                 typeInfo = layerType_getInfo(data->layers[i].type);
1368
1369                 data->layers[i].offset = offset;
1370                 offset += typeInfo->size;
1371         }
1372
1373         data->totsize = offset;
1374         CustomData_update_typemap(data);
1375 }
1376
1377 int CustomData_get_layer_index(const CustomData *data, int type)
1378 {
1379         int i; 
1380
1381         for(i=0; i < data->totlayer; ++i)
1382                 if(data->layers[i].type == type)
1383                         return i;
1384
1385         return -1;
1386 }
1387
1388 int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n)
1389 {
1390         int i = CustomData_get_layer_index(data, type);
1391
1392         if (i != -1) {
1393                 i = (data->layers[i + n].type == type) ? (i + n) : (-1);
1394         }
1395
1396         return i;
1397 }
1398
1399 int CustomData_get_named_layer_index(const CustomData *data, int type, const char *name)
1400 {
1401         int i;
1402
1403         for(i=0; i < data->totlayer; ++i)
1404                 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
1405                         return i;
1406
1407         return -1;
1408 }
1409
1410 int CustomData_get_active_layer_index(const CustomData *data, int type)
1411 {
1412         if (!data->totlayer)
1413                 return -1;
1414
1415         if (data->typemap[type] != -1) {
1416                 return data->typemap[type] + data->layers[data->typemap[type]].active;
1417         }
1418
1419         return -1;
1420 }
1421
1422 int CustomData_get_render_layer_index(const CustomData *data, int type)
1423 {
1424         int i;
1425
1426         for(i=0; i < data->totlayer; ++i)
1427                 if(data->layers[i].type == type)
1428                         return i + data->layers[i].active_rnd;
1429
1430         return -1;
1431 }
1432
1433 int CustomData_get_clone_layer_index(const CustomData *data, int type)
1434 {
1435         int i;
1436
1437         for(i=0; i < data->totlayer; ++i)
1438                 if(data->layers[i].type == type)
1439                         return i + data->layers[i].active_clone;
1440
1441         return -1;
1442 }
1443
1444 int CustomData_get_stencil_layer_index(const CustomData *data, int type)
1445 {
1446         int i;
1447
1448         for(i=0; i < data->totlayer; ++i)
1449                 if(data->layers[i].type == type)
1450                         return i + data->layers[i].active_mask;
1451
1452         return -1;
1453 }
1454
1455 int CustomData_get_active_layer(const CustomData *data, int type)
1456 {
1457         int i;
1458
1459         for(i=0; i < data->totlayer; ++i)
1460                 if(data->layers[i].type == type)
1461                         return data->layers[i].active;
1462
1463         return -1;
1464 }
1465
1466 int CustomData_get_render_layer(const CustomData *data, int type)
1467 {
1468         int i;
1469
1470         for(i=0; i < data->totlayer; ++i)
1471                 if(data->layers[i].type == type)
1472                         return data->layers[i].active_rnd;
1473
1474         return -1;
1475 }
1476
1477 int CustomData_get_clone_layer(const CustomData *data, int type)
1478 {
1479         int i;
1480
1481         for(i=0; i < data->totlayer; ++i)
1482                 if(data->layers[i].type == type)
1483                         return data->layers[i].active_clone;
1484
1485         return -1;
1486 }
1487
1488 int CustomData_get_stencil_layer(const CustomData *data, int type)
1489 {
1490         int i;
1491
1492         for(i=0; i < data->totlayer; ++i)
1493                 if(data->layers[i].type == type)
1494                         return data->layers[i].active_mask;
1495
1496         return -1;
1497 }
1498
1499 void CustomData_set_layer_active(CustomData *data, int type, int n)
1500 {
1501         int i;
1502
1503         for(i=0; i < data->totlayer; ++i)
1504                 if(data->layers[i].type == type)
1505                         data->layers[i].active = n;
1506 }
1507
1508 void CustomData_set_layer_render(CustomData *data, int type, int n)
1509 {
1510         int i;
1511
1512         for(i=0; i < data->totlayer; ++i)
1513                 if(data->layers[i].type == type)
1514                         data->layers[i].active_rnd = n;
1515 }
1516
1517 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1518 {
1519         int i;
1520
1521         for(i=0; i < data->totlayer; ++i)
1522                 if(data->layers[i].type == type)
1523                         data->layers[i].active_clone = n;
1524 }
1525
1526 void CustomData_set_layer_stencil(CustomData *data, int type, int n)
1527 {
1528         int i;
1529
1530         for(i=0; i < data->totlayer; ++i)
1531                 if(data->layers[i].type == type)
1532                         data->layers[i].active_mask = n;
1533 }
1534
1535 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1536 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1537 {
1538         int i;
1539
1540         for(i=0; i < data->totlayer; ++i)
1541                 if(data->layers[i].type == type)
1542                         data->layers[i].active = n-i;
1543 }
1544
1545 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1546 {
1547         int i;
1548
1549         for(i=0; i < data->totlayer; ++i)
1550                 if(data->layers[i].type == type)
1551                         data->layers[i].active_rnd = n-i;
1552 }
1553
1554 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1555 {
1556         int i;
1557
1558         for(i=0; i < data->totlayer; ++i)
1559                 if(data->layers[i].type == type)
1560                         data->layers[i].active_clone = n-i;
1561 }
1562
1563 void CustomData_set_layer_stencil_index(CustomData *data, int type, int n)
1564 {
1565         int i;
1566
1567         for(i=0; i < data->totlayer; ++i)
1568                 if(data->layers[i].type == type)
1569                         data->layers[i].active_mask = n-i;
1570 }
1571
1572 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1573 {
1574         int i;
1575
1576         for(i=0; i < data->totlayer; ++i)
1577                 if(data->layers[i].type == type)
1578                         data->layers[i].flag |= flag;
1579 }
1580
1581 static int customData_resize(CustomData *data, int amount)
1582 {
1583         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1584                                                                            "CustomData->layers");
1585         if(!tmp) return 0;
1586
1587         data->maxlayer += amount;
1588         if (data->layers) {
1589                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1590                 MEM_freeN(data->layers);
1591         }
1592         data->layers = tmp;
1593
1594         return 1;
1595 }
1596
1597 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1598         int type, int alloctype, void *layerdata, int totelem, const char *name)
1599 {
1600         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1601         int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1602         void *newlayerdata = NULL;
1603
1604         /* Passing a layerdata to copy from with an alloctype that won't copy is
1605            most likely a bug */
1606         BLI_assert(!layerdata ||
1607                    (alloctype == CD_ASSIGN) ||
1608                    (alloctype == CD_DUPLICATE) ||
1609                    (alloctype == CD_REFERENCE));
1610
1611         if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1612                 return &data->layers[CustomData_get_layer_index(data, type)];
1613
1614         if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1615                 newlayerdata = layerdata;
1616         }
1617         else if (size > 0) {
1618                 newlayerdata = MEM_callocN(size, layerType_getName(type));
1619                 if(!newlayerdata)
1620                         return NULL;
1621         }
1622
1623         if (alloctype == CD_DUPLICATE && layerdata) {
1624                 if(typeInfo->copy)
1625                         typeInfo->copy(layerdata, newlayerdata, totelem);
1626                 else
1627                         memcpy(newlayerdata, layerdata, size);
1628         }
1629         else if (alloctype == CD_DEFAULT) {
1630                 if(typeInfo->set_default)
1631                         typeInfo->set_default((char*)newlayerdata, totelem);
1632         }
1633         else if (alloctype == CD_REFERENCE)
1634                 flag |= CD_FLAG_NOFREE;
1635
1636         if(index >= data->maxlayer) {
1637                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
1638                         if(newlayerdata != layerdata)
1639                                 MEM_freeN(newlayerdata);
1640                         return NULL;
1641                 }
1642         }
1643         
1644         data->totlayer++;
1645
1646         /* keep layers ordered by type */
1647         for( ; index > 0 && data->layers[index - 1].type > type; --index)
1648                 data->layers[index] = data->layers[index - 1];
1649
1650         data->layers[index].type = type;
1651         data->layers[index].flag = flag;
1652         data->layers[index].data = newlayerdata;
1653
1654         if(name || (name=typeInfo->defaultname)) {
1655                 BLI_strncpy(data->layers[index].name, name, sizeof(data->layers[index].name));
1656                 CustomData_set_layer_unique_name(data, index);
1657         }
1658         else
1659                 data->layers[index].name[0] = '\0';
1660
1661         if(index > 0 && data->layers[index-1].type == type) {
1662                 data->layers[index].active = data->layers[index-1].active;
1663                 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1664                 data->layers[index].active_clone = data->layers[index-1].active_clone;
1665                 data->layers[index].active_mask = data->layers[index-1].active_mask;
1666         } else {
1667                 data->layers[index].active = 0;
1668                 data->layers[index].active_rnd = 0;
1669                 data->layers[index].active_clone = 0;
1670                 data->layers[index].active_mask = 0;
1671         }
1672         
1673         customData_update_offsets(data);
1674
1675         return &data->layers[index];
1676 }
1677
1678 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1679                                                    void *layerdata, int totelem)
1680 {
1681         CustomDataLayer *layer;
1682         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1683         
1684         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1685                                                                                    totelem, typeInfo->defaultname);
1686         CustomData_update_typemap(data);
1687
1688         if(layer)
1689                 return layer->data;
1690
1691         return NULL;
1692 }
1693
1694 /*same as above but accepts a name*/
1695 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1696                                                    void *layerdata, int totelem, const char *name)
1697 {
1698         CustomDataLayer *layer;
1699         
1700         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1701                                                                                    totelem, name);
1702         CustomData_update_typemap(data);
1703
1704         if(layer)
1705                 return layer->data;
1706
1707         return NULL;
1708 }
1709
1710
1711 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1712 {
1713         int i;
1714         
1715         if (index < 0) return 0;
1716
1717         customData_free_layer__internal(&data->layers[index], totelem);
1718
1719         for (i=index+1; i < data->totlayer; ++i)
1720                 data->layers[i-1] = data->layers[i];
1721
1722         data->totlayer--;
1723
1724         /* if layer was last of type in array, set new active layer */
1725         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1726                 i = CustomData_get_layer_index(data, type);
1727                 
1728                 if (i >= 0)
1729                         for (; i < data->totlayer && data->layers[i].type == type; i++) {
1730                                 data->layers[i].active--;
1731                                 data->layers[i].active_rnd--;
1732                                 data->layers[i].active_clone--;
1733                                 data->layers[i].active_mask--;
1734                         }
1735         }
1736
1737         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1738                 customData_resize(data, -CUSTOMDATA_GROW);
1739
1740         customData_update_offsets(data);
1741         CustomData_update_typemap(data);
1742
1743         return 1;
1744 }
1745
1746 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1747 {
1748         int index = 0;
1749         index = CustomData_get_active_layer_index(data, type);
1750         if (index < 0) return 0;
1751         return CustomData_free_layer(data, type, totelem, index);
1752 }
1753
1754
1755 void CustomData_free_layers(CustomData *data, int type, int totelem)
1756 {
1757         while (CustomData_has_layer(data, type))
1758                 CustomData_free_layer_active(data, type, totelem);
1759 }
1760
1761 int CustomData_has_layer(const CustomData *data, int type)
1762 {
1763         return (CustomData_get_layer_index(data, type) != -1);
1764 }
1765
1766 int CustomData_number_of_layers(const CustomData *data, int type)
1767 {
1768         int i, number = 0;
1769
1770         for(i = 0; i < data->totlayer; i++)
1771                 if(data->layers[i].type == type)
1772                         number++;
1773         
1774         return number;
1775 }
1776
1777 void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem)
1778 {
1779         CustomDataLayer *layer;
1780         int layer_index;
1781
1782         /* get the layer index of the first layer of type */
1783         layer_index = CustomData_get_active_layer_index(data, type);
1784         if(layer_index < 0) return NULL;
1785
1786         layer = &data->layers[layer_index];
1787
1788         if (layer->flag & CD_FLAG_NOFREE) {
1789                 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1790                  * CD_MDEFORMVERT, which has pointers to allocated data...
1791                  * So in case a custom copy function is defined, use it!
1792                  */
1793                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1794
1795                 if(typeInfo->copy) {
1796                         char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1797                         typeInfo->copy(layer->data, dest_data, totelem);
1798                         layer->data = dest_data;
1799                 }
1800                 else
1801                         layer->data = MEM_dupallocN(layer->data);
1802
1803                 layer->flag &= ~CD_FLAG_NOFREE;
1804         }
1805
1806         return layer->data;
1807 }
1808
1809 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1810                                                                                                   const int type, const char *name, const int totelem)
1811 {
1812         CustomDataLayer *layer;
1813         int layer_index;
1814
1815         /* get the layer index of the desired layer */
1816         layer_index = CustomData_get_named_layer_index(data, type, name);
1817         if(layer_index < 0) return NULL;
1818
1819         layer = &data->layers[layer_index];
1820
1821         if (layer->flag & CD_FLAG_NOFREE) {
1822                 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1823                  * CD_MDEFORMVERT, which has pointers to allocated data...
1824                  * So in case a custom copy function is defined, use it!
1825                  */
1826                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1827
1828                 if(typeInfo->copy) {
1829                         char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1830                         typeInfo->copy(layer->data, dest_data, totelem);
1831                         layer->data = dest_data;
1832                 }
1833                 else
1834                         layer->data = MEM_dupallocN(layer->data);
1835
1836                 layer->flag &= ~CD_FLAG_NOFREE;
1837         }
1838
1839         return layer->data;
1840 }
1841
1842 int CustomData_is_referenced_layer(struct CustomData *data, int type)
1843 {
1844         CustomDataLayer *layer;
1845         int layer_index;
1846
1847         /* get the layer index of the first layer of type */
1848         layer_index = CustomData_get_active_layer_index(data, type);
1849         if(layer_index < 0) return 0;
1850
1851         layer = &data->layers[layer_index];
1852
1853         return (layer->flag & CD_FLAG_NOFREE) != 0;
1854 }
1855
1856 void CustomData_free_temporary(CustomData *data, int totelem)
1857 {
1858         CustomDataLayer *layer;
1859         int i, j;
1860
1861         for(i = 0, j = 0; i < data->totlayer; ++i) {
1862                 layer = &data->layers[i];
1863
1864                 if (i != j)
1865                         data->layers[j] = data->layers[i];
1866
1867                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1868                         customData_free_layer__internal(layer, totelem);
1869                 else
1870                         j++;
1871         }
1872
1873         data->totlayer = j;
1874
1875         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1876                 customData_resize(data, -CUSTOMDATA_GROW);
1877
1878         customData_update_offsets(data);
1879 }
1880
1881 void CustomData_set_only_copy(const struct CustomData *data,
1882                               CustomDataMask mask)
1883 {
1884         int i;
1885
1886         for(i = 0; i < data->totlayer; ++i)
1887                 if(!(mask & CD_TYPE_AS_MASK(data->layers[i].type)))
1888                         data->layers[i].flag |= CD_FLAG_NOCOPY;
1889 }
1890
1891 void CustomData_copy_elements(int type, void *source, void *dest, int count)
1892 {
1893         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1894
1895         if (typeInfo->copy)
1896                 typeInfo->copy(source, dest, count);
1897         else
1898                 memcpy(dest, source, typeInfo->size*count);
1899 }
1900
1901 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1902                                                   int source_index, int dest_index, int count)
1903 {
1904         const LayerTypeInfo *typeInfo;
1905         int src_i, dest_i;
1906         int src_offset;
1907         int dest_offset;
1908
1909         /* copies a layer at a time */
1910         dest_i = 0;
1911         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1912
1913                 /* find the first dest layer with type >= the source type
1914                  * (this should work because layers are ordered by type)
1915                  */
1916                 while(dest_i < dest->totlayer
1917                           && dest->layers[dest_i].type < source->layers[src_i].type)
1918                         ++dest_i;
1919
1920                 /* if there are no more dest layers, we're done */
1921                 if(dest_i >= dest->totlayer) return;
1922
1923                 /* if we found a matching layer, copy the data */
1924                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1925                         char *src_data = source->layers[src_i].data;
1926                         char *dest_data = dest->layers[dest_i].data;
1927
1928                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1929
1930                         src_offset = source_index * typeInfo->size;
1931                         dest_offset = dest_index * typeInfo->size;
1932                         
1933                         if (!src_data || !dest_data) {
1934                                 printf("%s: warning null data for %s type (%p --> %p), skipping\n",
1935                                        __func__, layerType_getName(source->layers[src_i].type),
1936                                        (void *)src_data, (void *)dest_data);
1937                                 continue;
1938                         }
1939                         
1940                         if(typeInfo->copy)
1941                                 typeInfo->copy(src_data + src_offset,
1942                                                                 dest_data + dest_offset,
1943                                                                 count);
1944                         else
1945                                 memcpy(dest_data + dest_offset,
1946                                            src_data + src_offset,
1947                                            count * typeInfo->size);
1948
1949                         /* if there are multiple source & dest layers of the same type,
1950                          * we don't want to copy all source layers to the same dest, so
1951                          * increment dest_i
1952                          */
1953                         ++dest_i;
1954                 }
1955         }
1956 }
1957
1958 void CustomData_free_elem(CustomData *data, int index, int count)
1959 {
1960         int i;
1961         const LayerTypeInfo *typeInfo;
1962
1963         for(i = 0; i < data->totlayer; ++i) {
1964                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1965                         typeInfo = layerType_getInfo(data->layers[i].type);
1966
1967                         if(typeInfo->free) {
1968                                 int offset = typeInfo->size * index;
1969
1970                                 typeInfo->free((char *)data->layers[i].data + offset,
1971                                                            count, typeInfo->size);
1972                         }
1973                 }
1974         }
1975 }
1976
1977 #define SOURCE_BUF_SIZE 100
1978
1979 void CustomData_interp(const CustomData *source, CustomData *dest,
1980                                            int *src_indices, float *weights, float *sub_weights,
1981                                            int count, int dest_index)
1982 {
1983         int src_i, dest_i;
1984         int dest_offset;
1985         int j;
1986         void *source_buf[SOURCE_BUF_SIZE];
1987         void **sources = source_buf;
1988
1989         /* slow fallback in case we're interpolating a ridiculous number of
1990          * elements
1991          */
1992         if(count > SOURCE_BUF_SIZE)
1993                 sources = MEM_callocN(sizeof(*sources) * count,
1994                                                           "CustomData_interp sources");
1995
1996         /* interpolates a layer at a time */
1997         dest_i = 0;
1998         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1999                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
2000                 if(!typeInfo->interp) continue;
2001
2002                 /* find the first dest layer with type >= the source type
2003                  * (this should work because layers are ordered by type)
2004                  */
2005                 while(dest_i < dest->totlayer
2006                           && dest->layers[dest_i].type < source->layers[src_i].type)
2007                         ++dest_i;
2008
2009                 /* if there are no more dest layers, we're done */
2010                 if(dest_i >= dest->totlayer) return;
2011
2012                 /* if we found a matching layer, copy the data */
2013                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2014                         void *src_data = source->layers[src_i].data;
2015
2016                         for(j = 0; j < count; ++j)
2017                                 sources[j] = (char *)src_data
2018                                                          + typeInfo->size * src_indices[j];
2019
2020                         dest_offset = dest_index * typeInfo->size;
2021
2022                         typeInfo->interp(sources, weights, sub_weights, count,
2023                                                    (char *)dest->layers[dest_i].data + dest_offset);
2024
2025                         /* if there are multiple source & dest layers of the same type,
2026                          * we don't want to copy all source layers to the same dest, so
2027                          * increment dest_i
2028                          */
2029                         ++dest_i;
2030                 }
2031         }
2032
2033         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2034 }
2035
2036 void CustomData_swap(struct CustomData *data, int index, const int *corner_indices)
2037 {
2038         const LayerTypeInfo *typeInfo;
2039         int i;
2040
2041         for(i = 0; i < data->totlayer; ++i) {
2042                 typeInfo = layerType_getInfo(data->layers[i].type);
2043
2044                 if(typeInfo->swap) {
2045                         int offset = typeInfo->size * index;
2046
2047                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
2048                 }
2049         }
2050 }
2051
2052 void *CustomData_get(const CustomData *data, int index, int type)
2053 {
2054         int offset;
2055         int layer_index;
2056         
2057         /* get the layer index of the active layer of type */
2058         layer_index = CustomData_get_active_layer_index(data, type);
2059         if(layer_index < 0) return NULL;
2060
2061         /* get the offset of the desired element */
2062         offset = layerType_getInfo(type)->size * index;
2063
2064         return (char *)data->layers[layer_index].data + offset;
2065 }
2066
2067 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
2068 {
2069         int layer_index;
2070         int offset;
2071
2072         /* get the layer index of the first layer of type */
2073         layer_index = data->typemap[type];
2074         if(layer_index < 0) return NULL;
2075
2076         offset = layerType_getInfo(type)->size * index;
2077         return (char *)data->layers[layer_index+n].data + offset;
2078 }
2079
2080 void *CustomData_get_layer(const CustomData *data, int type)
2081 {
2082         /* get the layer index of the active layer of type */
2083         int layer_index = CustomData_get_active_layer_index(data, type);
2084         if(layer_index < 0) return NULL;
2085
2086         return data->layers[layer_index].data;
2087 }
2088
2089 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
2090 {
2091         /* get the layer index of the active layer of type */
2092         int layer_index = CustomData_get_layer_index_n(data, type, n);
2093         if(layer_index < 0) return NULL;
2094
2095         return data->layers[layer_index].data;
2096 }
2097
2098 void *CustomData_get_layer_named(const struct CustomData *data, int type,
2099                                                                  const char *name)
2100 {
2101         int layer_index = CustomData_get_named_layer_index(data, type, name);
2102         if(layer_index < 0) return NULL;
2103
2104         return data->layers[layer_index].data;
2105 }
2106
2107
2108 int CustomData_set_layer_name(const CustomData *data, int type, int n, const char *name)
2109 {
2110         /* get the layer index of the first layer of type */
2111         int layer_index = CustomData_get_layer_index_n(data, type, n);
2112
2113         if(layer_index < 0) return 0;
2114         if (!name) return 0;
2115         
2116         strcpy(data->layers[layer_index].name, name);
2117         
2118         return 1;
2119 }
2120
2121 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
2122 {
2123         /* get the layer index of the first layer of type */
2124         int layer_index = CustomData_get_active_layer_index(data, type);
2125
2126         if(layer_index < 0) return NULL;
2127
2128         data->layers[layer_index].data = ptr;
2129
2130         return ptr;
2131 }
2132
2133 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
2134 {
2135         /* get the layer index of the first layer of type */
2136         int layer_index = CustomData_get_layer_index_n(data, type, n);
2137         if(layer_index < 0) return NULL;
2138
2139         data->layers[layer_index].data = ptr;
2140
2141         return ptr;
2142 }
2143
2144 void CustomData_set(const CustomData *data, int index, int type, void *source)
2145 {
2146         void *dest = CustomData_get(data, index, type);
2147         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2148
2149         if(!dest) return;
2150
2151         if(typeInfo->copy)
2152                 typeInfo->copy(source, dest, 1);
2153         else
2154                 memcpy(dest, source, typeInfo->size);
2155 }
2156
2157 /* EditMesh functions */
2158
2159 void CustomData_em_free_block(CustomData *data, void **block)
2160 {
2161         const LayerTypeInfo *typeInfo;
2162         int i;
2163
2164         if(!*block) return;
2165
2166         for(i = 0; i < data->totlayer; ++i) {
2167                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2168                         typeInfo = layerType_getInfo(data->layers[i].type);
2169
2170                         if(typeInfo->free) {
2171                                 int offset = data->layers[i].offset;
2172                                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2173                         }
2174                 }
2175         }
2176
2177         MEM_freeN(*block);
2178         *block = NULL;
2179 }
2180
2181 static void CustomData_em_alloc_block(CustomData *data, void **block)
2182 {
2183         /* TODO: optimize free/alloc */
2184
2185         if (*block)
2186                 CustomData_em_free_block(data, block);
2187
2188         if (data->totsize > 0)
2189                 *block = MEM_callocN(data->totsize, "CustomData EM block");
2190         else
2191                 *block = NULL;
2192 }
2193
2194 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
2195                                                         void *src_block, void **dest_block)
2196 {
2197         const LayerTypeInfo *typeInfo;
2198         int dest_i, src_i;
2199
2200         if (!*dest_block)
2201                 CustomData_em_alloc_block(dest, dest_block);
2202         
2203         /* copies a layer at a time */
2204         dest_i = 0;
2205         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2206
2207                 /* find the first dest layer with type >= the source type
2208                  * (this should work because layers are ordered by type)
2209                  */
2210                 while(dest_i < dest->totlayer
2211                           && dest->layers[dest_i].type < source->layers[src_i].type)
2212                         ++dest_i;
2213
2214                 /* if there are no more dest layers, we're done */
2215                 if(dest_i >= dest->totlayer) return;
2216
2217                 /* if we found a matching layer, copy the data */
2218                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2219                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2220                         char *src_data = (char*)src_block + source->layers[src_i].offset;
2221                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2222
2223                         typeInfo = layerType_getInfo(source->layers[src_i].type);
2224
2225                         if(typeInfo->copy)
2226                                 typeInfo->copy(src_data, dest_data, 1);
2227                         else
2228                                 memcpy(dest_data, src_data, typeInfo->size);
2229
2230                         /* if there are multiple source & dest layers of the same type,
2231                          * we don't want to copy all source layers to the same dest, so
2232                          * increment dest_i
2233                          */
2234                         ++dest_i;
2235                 }
2236         }
2237 }
2238
2239 void CustomData_em_validate_data(CustomData *data, void *block, int sub_elements)
2240 {
2241         int i;
2242         for(i = 0; i < data->totlayer; i++) {
2243                 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[i].type);
2244                 char *leayer_data = (char*)block + data->layers[i].offset;
2245
2246                 if(typeInfo->validate)
2247                         typeInfo->validate(leayer_data, sub_elements);
2248         }
2249 }
2250
2251 void *CustomData_em_get(const CustomData *data, void *block, int type)
2252 {
2253         int layer_index;
2254         
2255         /* get the layer index of the first layer of type */
2256         layer_index = CustomData_get_active_layer_index(data, type);
2257         if(layer_index < 0) return NULL;
2258
2259         return (char *)block + data->layers[layer_index].offset;
2260 }
2261
2262 void *CustomData_em_get_n(const CustomData *data, void *block, int type, int n)
2263 {
2264         int layer_index;
2265         
2266         /* get the layer index of the first layer of type */
2267         layer_index = CustomData_get_layer_index_n(data, type, n);
2268         if(layer_index < 0) return NULL;
2269
2270         return (char *)block + data->layers[layer_index].offset;
2271 }
2272
2273 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
2274 {
2275         void *dest = CustomData_em_get(data, block, type);
2276         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2277
2278         if(!dest) return;
2279
2280         if(typeInfo->copy)
2281                 typeInfo->copy(source, dest, 1);
2282         else
2283                 memcpy(dest, source, typeInfo->size);
2284 }
2285
2286 void CustomData_em_set_n(CustomData *data, void *block, int type, int n, void *source)
2287 {
2288         void *dest = CustomData_em_get_n(data, block, type, n);
2289         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2290
2291         if(!dest) return;
2292
2293         if(typeInfo->copy)
2294                 typeInfo->copy(source, dest, 1);
2295         else
2296                 memcpy(dest, source, typeInfo->size);
2297 }
2298
2299 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
2300                                                   float *sub_weights, int count, void *dest_block)
2301 {
2302         int i, j;
2303         void *source_buf[SOURCE_BUF_SIZE];
2304         void **sources = source_buf;
2305
2306         /* slow fallback in case we're interpolating a ridiculous number of
2307          * elements
2308          */
2309         if(count > SOURCE_BUF_SIZE)
2310                 sources = MEM_callocN(sizeof(*sources) * count,
2311                                                           "CustomData_interp sources");
2312
2313         /* interpolates a layer at a time */
2314         for(i = 0; i < data->totlayer; ++i) {
2315                 CustomDataLayer *layer = &data->layers[i];
2316                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2317
2318                 if(typeInfo->interp) {
2319                         for(j = 0; j < count; ++j)
2320                                 sources[j] = (char *)src_blocks[j] + layer->offset;
2321
2322                         typeInfo->interp(sources, weights, sub_weights, count,
2323                                                           (char *)dest_block + layer->offset);
2324                 }
2325         }
2326
2327         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2328 }
2329
2330 void CustomData_em_set_default(CustomData *data, void **block)
2331 {
2332         const LayerTypeInfo *typeInfo;
2333         int i;
2334
2335         if (!*block)
2336                 CustomData_em_alloc_block(data, block);
2337
2338         for(i = 0; i < data->totlayer; ++i) {
2339                 int offset = data->layers[i].offset;
2340
2341                 typeInfo = layerType_getInfo(data->layers[i].type);
2342
2343                 if(typeInfo->set_default)
2344                         typeInfo->set_default((char*)*block + offset, 1);
2345         }
2346 }
2347
2348 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
2349                                                         int src_index, void **dest_block)
2350 {
2351         const LayerTypeInfo *typeInfo;
2352         int dest_i, src_i, src_offset;
2353
2354         if (!*dest_block)
2355                 CustomData_em_alloc_block(dest, dest_block);
2356         
2357         /* copies a layer at a time */
2358         dest_i = 0;
2359         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2360
2361                 /* find the first dest layer with type >= the source type
2362                  * (this should work because layers are ordered by type)
2363                  */
2364                 while(dest_i < dest->totlayer
2365                           && dest->layers[dest_i].type < source->layers[src_i].type)
2366                         ++dest_i;
2367
2368                 /* if there are no more dest layers, we're done */
2369                 if(dest_i >= dest->totlayer) return;
2370
2371                 /* if we found a matching layer, copy the data */
2372                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2373                         int offset = dest->layers[dest_i].offset;
2374                         char *src_data = source->layers[src_i].data;
2375                         char *dest_data = (char*)*dest_block + offset;
2376
2377                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2378                         src_offset = src_index * typeInfo->size;
2379
2380                         if(typeInfo->copy)
2381                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
2382                         else
2383                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2384
2385                         /* if there are multiple source & dest layers of the same type,
2386                          * we don't want to copy all source layers to the same dest, so
2387                          * increment dest_i
2388                          */
2389                         ++dest_i;
2390                 }
2391         }
2392 }
2393
2394 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
2395                                                           void *src_block, int dest_index)
2396 {
2397         const LayerTypeInfo *typeInfo;
2398         int dest_i, src_i, dest_offset;
2399
2400         /* copies a layer at a time */
2401         dest_i = 0;
2402         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2403
2404                 /* find the first dest layer with type >= the source type
2405                  * (this should work because layers are ordered by type)
2406                  */
2407                 while(dest_i < dest->totlayer
2408                           && dest->layers[dest_i].type < source->layers[src_i].type)
2409                         ++dest_i;
2410
2411                 /* if there are no more dest layers, we're done */
2412                 if(dest_i >= dest->totlayer) return;
2413
2414                 /* if we found a matching layer, copy the data */
2415                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2416                         int offset = source->layers[src_i].offset;
2417                         char *src_data = (char*)src_block + offset;
2418                         char *dest_data = dest->layers[dest_i].data;
2419
2420                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2421                         dest_offset = dest_index * typeInfo->size;
2422
2423                         if(typeInfo->copy)
2424                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2425                         else
2426                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2427
2428                         /* if there are multiple source & dest layers of the same type,
2429                          * we don't want to copy all source layers to the same dest, so
2430                          * increment dest_i
2431                          */
2432                         ++dest_i;
2433                 }
2434         }
2435
2436 }
2437
2438 /*Bmesh functions*/
2439 /*needed to convert to/from different face reps*/
2440 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata,
2441                              int totloop, int totpoly)
2442 {
2443         int i;
2444         for(i=0; i < fdata->totlayer; i++) {
2445                 if(fdata->layers[i].type == CD_MTFACE) {
2446                         CustomData_add_layer_named(pdata, CD_MTEXPOLY, CD_CALLOC, NULL, totpoly, fdata->layers[i].name);
2447                         CustomData_add_layer_named(ldata, CD_MLOOPUV, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2448                 }
2449                 else if (fdata->layers[i].type == CD_MCOL) {
2450                         CustomData_add_layer_named(ldata, CD_MLOOPCOL, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2451                 }
2452                 else if (fdata->layers[i].type == CD_MDISPS) {
2453                         CustomData_add_layer_named(ldata, CD_MDISPS, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2454                 }
2455         }
2456 }
2457
2458 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total)
2459 {
2460         int i;
2461         for(i=0; i < pdata->totlayer; i++) {
2462                 if (pdata->layers[i].type == CD_MTEXPOLY) {
2463                         CustomData_add_layer_named(fdata, CD_MTFACE, CD_CALLOC, NULL, total, pdata->layers[i].name);
2464                 }
2465         }
2466         for(i=0; i < ldata->totlayer; i++) {
2467                 if (ldata->layers[i].type == CD_MLOOPCOL) {
2468                         CustomData_add_layer_named(fdata, CD_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2469                 }
2470                 else if (ldata->layers[i].type == CD_WEIGHT_MLOOPCOL) {
2471                         CustomData_add_layer_named(fdata, CD_WEIGHT_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2472                 }
2473                 else if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) {
2474                         CustomData_add_layer_named(fdata, CD_ORIGSPACE, CD_CALLOC, NULL, total, ldata->layers[i].name);
2475                 }
2476         }
2477 }
2478
2479 void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *pdata, CustomData *ldata)
2480 {
2481         int act;
2482
2483         if (CustomData_has_layer(pdata, CD_MTEXPOLY)) {
2484                 act = CustomData_get_active_layer(pdata, CD_MTEXPOLY);
2485                 CustomData_set_layer_active(ldata, CD_MLOOPUV, act);
2486                 CustomData_set_layer_active(fdata, CD_MTFACE, act);
2487
2488                 act = CustomData_get_render_layer(pdata, CD_MTEXPOLY);
2489                 CustomData_set_layer_render(ldata, CD_MLOOPUV, act);
2490                 CustomData_set_layer_render(fdata, CD_MTFACE, act);
2491
2492                 act = CustomData_get_clone_layer(pdata, CD_MTEXPOLY);
2493                 CustomData_set_layer_clone(ldata, CD_MLOOPUV, act);
2494                 CustomData_set_layer_clone(fdata, CD_MTFACE, act);
2495
2496                 act = CustomData_get_stencil_layer(pdata, CD_MTEXPOLY);
2497                 CustomData_set_layer_stencil(ldata, CD_MLOOPUV, act);
2498                 CustomData_set_layer_stencil(fdata, CD_MTFACE, act);
2499         }
2500
2501         if (CustomData_has_layer(ldata, CD_MLOOPCOL)) {
2502                 act = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
2503                 CustomData_set_layer_active(fdata, CD_MCOL, act);
2504
2505                 act = CustomData_get_render_layer(ldata, CD_MLOOPCOL);
2506                 CustomData_set_layer_render(fdata, CD_MCOL, act);
2507
2508                 act = CustomData_get_clone_layer(ldata, CD_MLOOPCOL);
2509                 CustomData_set_layer_clone(fdata, CD_MCOL, act);
2510
2511                 act = CustomData_get_stencil_layer(ldata, CD_MLOOPCOL);
2512                 CustomData_set_layer_stencil(fdata, CD_MCOL, act);
2513         }
2514 }
2515
2516 void CustomData_bmesh_init_pool(CustomData *data, int allocsize)
2517 {
2518         /* Dispose old pools before calling here to avoid leaks */
2519         BLI_assert(data->pool == NULL);
2520
2521         /* If there are no layers, no pool is needed just yet */
2522         if (data->totlayer) {
2523                 data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize, TRUE, FALSE);
2524         }
2525 }
2526
2527 void CustomData_bmesh_merge(CustomData *source, CustomData *dest, 
2528                             int mask, int alloctype, BMesh *bm, int type)
2529 {
2530         BMHeader *h;
2531         BMIter iter;
2532         CustomData destold = *dest;
2533         void *tmp;
2534         int t;
2535         
2536         CustomData_merge(source, dest, mask, alloctype, 0);
2537         CustomData_bmesh_init_pool(dest, 512);
2538
2539         switch (type) {
2540                 case BM_VERT:
2541                         t = BM_VERTS_OF_MESH; break;
2542                 case BM_EDGE:
2543                         t = BM_EDGES_OF_MESH; break;
2544                 case BM_LOOP:
2545                         t = BM_LOOPS_OF_FACE; break;
2546                 case BM_FACE:
2547                         t = BM_FACES_OF_MESH; break;
2548                 default: /* should never happen */
2549                         BLI_assert(!"invalid type given");
2550                         t = BM_VERTS_OF_MESH;
2551         }
2552
2553         if (t != BM_LOOPS_OF_FACE) {
2554                 /*ensure all current elements follow new customdata layout*/
2555                 BM_ITER(h, &iter, bm, t, NULL) {
2556                         CustomData_bmesh_copy_data(&destold, dest, h->data, &tmp);
2557                         CustomData_bmesh_free_block(&destold, &h->data);
2558                         h->data = tmp;
2559                 }
2560         } else {
2561                 BMFace *f;
2562                 BMLoop *l;
2563                 BMIter liter;
2564
2565                 /*ensure all current elements follow new customdata layout*/
2566                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
2567                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
2568                                 CustomData_bmesh_copy_data(&destold, dest, l->head.data, &tmp);
2569                                 CustomData_bmesh_free_block(&destold, &l->head.data);
2570                                 l->head.data = tmp;
2571                         }
2572                 }
2573         }
2574
2575         if (destold.pool) BLI_mempool_destroy(destold.pool);
2576 }
2577
2578 void CustomData_bmesh_free_block(CustomData *data, void **block)
2579 {
2580         const LayerTypeInfo *typeInfo;
2581         int i;
2582
2583         if(!*block) return;
2584         for(i = 0; i < data->totlayer; ++i) {
2585                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2586                         typeInfo = layerType_getInfo(data->layers[i].type);
2587
2588                         if(typeInfo->free) {
2589                                 int offset = data->layers[i].offset;
2590                                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2591                         }
2592                 }
2593         }
2594
2595         if (data->totsize)
2596                 BLI_mempool_free(data->pool, *block);
2597
2598         *block = NULL;
2599 }
2600
2601 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
2602 {
2603
2604         if (*block)
2605                 CustomData_bmesh_free_block(data, block);
2606
2607         if (data->totsize > 0)
2608                 *block = BLI_mempool_alloc(data->pool);
2609         else
2610                 *block = NULL;
2611 }
2612
2613 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
2614                                                         void *src_block, void **dest_block)
2615 {
2616         const LayerTypeInfo *typeInfo;
2617         int dest_i, src_i;
2618
2619         if (!*dest_block)
2620                 CustomData_bmesh_alloc_block(dest, dest_block);
2621         
2622         /* copies a layer at a time */
2623         dest_i = 0;
2624         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2625
2626                 /* find the first dest layer with type >= the source type
2627                  * (this should work because layers are ordered by type)
2628                  */
2629                 while(dest_i < dest->totlayer
2630                           && dest->layers[dest_i].type < source->layers[src_i].type)
2631                         ++dest_i;
2632
2633                 /* if there are no more dest layers, we're done */
2634                 if(dest_i >= dest->totlayer) return;
2635
2636                 /* if we found a matching layer, copy the data */
2637                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2638                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2639                         char *src_data = (char*)src_block + source->layers[src_i].offset;
2640                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2641
2642                         typeInfo = layerType_getInfo(source->layers[src_i].type);
2643
2644                         if(typeInfo->copy)
2645                                 typeInfo->copy(src_data, dest_data, 1);
2646                         else
2647                                 memcpy(dest_data, src_data, typeInfo->size);
2648
2649                         /* if there are multiple source & dest layers of the same type,
2650                          * we don't want to copy all source layers to the same dest, so
2651                          * increment dest_i
2652                          */
2653                         ++dest_i;
2654                 }
2655         }
2656 }
2657
2658 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
2659 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
2660 {
2661         int layer_index;
2662         
2663         /* get the layer index of the first layer of type */
2664         layer_index = CustomData_get_active_layer_index(data, type);
2665         if(layer_index < 0) return NULL;
2666
2667         return (char *)block + data->layers[layer_index].offset;
2668 }
2669
2670 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
2671 {
2672         int layer_index;
2673         
2674         /* get the layer index of the first layer of type */
2675         layer_index = CustomData_get_layer_index(data, type);
2676         if(layer_index < 0) return NULL;
2677
2678         return (char *)block + data->layers[layer_index+n].offset;
2679 }
2680
2681 /*gets from the layer at physical index n, note: doesn't check type.*/
2682 void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
2683 {
2684         if(n < 0 || n >= data->totlayer) return NULL;
2685
2686         return (char *)block + data->layers[n].offset;
2687 }
2688
2689 int CustomData_layer_has_math(struct CustomData *data, int layern)
2690 {
2691         const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[layern].type);
2692         
2693         if (typeInfo->equal && typeInfo->add && typeInfo->multiply && 
2694             typeInfo->initminmax && typeInfo->dominmax) return 1;
2695         
2696         return 0;
2697 }
2698
2699 /*copies the "value" (e.g. mloopuv uv or mloopcol colors) from one block to
2700   another, while not overwriting anything else (e.g. flags)*/
2701 void CustomData_data_copy_value(int type, void *source, void *dest)
2702 {
2703         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2704
2705         if(!dest) return;
2706
2707         if(typeInfo->copyvalue)
2708                 typeInfo->copyvalue(source, dest);
2709         else
2710                 memcpy(dest, source, typeInfo->size);
2711 }
2712
2713 int CustomData_data_equals(int type, void *data1, void *data2)
2714 {
2715         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2716
2717         if (typeInfo->equal)
2718                 return typeInfo->equal(data1, data2);
2719         else return !memcmp(data1, data2, typeInfo->size);
2720 }
2721
2722 void CustomData_data_initminmax(int type, void *min, void *max)
2723 {
2724         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2725
2726         if (typeInfo->initminmax)
2727                 typeInfo->initminmax(min, max);
2728 }
2729
2730
2731 void CustomData_data_dominmax(int type, void *data, void *min, void *max)
2732 {
2733         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2734
2735         if (typeInfo->dominmax)
2736                 typeInfo->dominmax(data, min, max);
2737 }
2738
2739
2740 void CustomData_data_multiply(int type, void *data, float fac)
2741 {
2742         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2743
2744         if (typeInfo->multiply)
2745                 typeInfo->multiply(data, fac);
2746 }
2747
2748
2749 void CustomData_data_add(int type, void *data1, void *data2)
2750 {
2751         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2752
2753         if (typeInfo->add)
2754                 typeInfo->add(data1, data2);
2755 }
2756
2757 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
2758 {
2759         void *dest = CustomData_bmesh_get(data, block, type);
2760         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2761
2762         if(!dest) return;
2763
2764         if(typeInfo->copy)
2765                 typeInfo->copy(source, dest, 1);
2766         else
2767                 memcpy(dest, source, typeInfo->size);
2768 }
2769
2770 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
2771 {
2772         void *dest = CustomData_bmesh_get_n(data, block, type, n);
2773         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2774
2775         if(!dest) return;
2776
2777         if(typeInfo->copy)
2778                 typeInfo->copy(source, dest, 1);
2779         else
2780                 memcpy(dest, source, typeInfo->size);
2781 }
2782
2783 void CustomData_bmesh_set_layer_n(CustomData *data, void *block, int n, void *source)
2784 {
2785         void *dest = CustomData_bmesh_get_layer_n(data, block, n);
2786         const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[n].type);
2787
2788         if(!dest) return;
2789
2790         if(typeInfo->copy)
2791                 typeInfo->copy(source, dest, 1);
2792         else
2793                 memcpy(dest, source, typeInfo->size);
2794 }
2795
2796 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
2797                                                   float *sub_weights, int count, void *dest_block)
2798 {
2799         int i, j;
2800         void *source_buf[SOURCE_BUF_SIZE];
2801         void **sources = source_buf;
2802
2803         /* slow fallback in case we're interpolating a ridiculous number of
2804          * elements
2805          */
2806         if(count > SOURCE_BUF_SIZE)
2807                 sources = MEM_callocN(sizeof(*sources) * count,
2808                                                           "CustomData_interp sources");
2809
2810         /* interpolates a layer at a time */
2811         for(i = 0; i < data->totlayer; ++i) {
2812                 CustomDataLayer *layer = &data->layers[i];
2813                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2814                 if(typeInfo->interp) {
2815                         for(j = 0; j < count; ++j)
2816                                 sources[j] = (char *)src_blocks[j] + layer->offset;
2817
2818                         typeInfo->interp(sources, weights, sub_weights, count,
2819                                                           (char *)dest_block + layer->offset);
2820                 }
2821         }
2822
2823         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2824 }
2825
2826 void CustomData_bmesh_set_default(CustomData *data, void **block)
2827 {
2828         const LayerTypeInfo *typeInfo;
2829         int i;
2830
2831         if (!*block)
2832                 CustomData_bmesh_alloc_block(data, block);
2833
2834         for(i = 0; i < data->totlayer; ++i) {
2835                 int offset = data->layers[i].offset;
2836
2837                 typeInfo = layerType_getInfo(data->layers[i].type);
2838
2839                 if(typeInfo->set_default)
2840                         typeInfo->set_default((char*)*block + offset, 1);
2841                 else memset((char*)*block + offset, 0, typeInfo->size);
2842         }
2843 }
2844
2845 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2846                                                         int src_index, void **dest_block)
2847 {
2848         const LayerTypeInfo *typeInfo;
2849         int dest_i, src_i, src_offset;
2850
2851         if (!*dest_block)
2852                 CustomData_bmesh_alloc_block(dest, dest_block);
2853         
2854         /* copies a layer at a time */
2855         dest_i = 0;
2856         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2857
2858                 /* find the first dest layer with type >= the source type
2859                  * (this should work because layers are ordered by type)
2860                  */
2861                 while(dest_i < dest->totlayer
2862                           && dest->layers[dest_i].type < source->layers[src_i].type)
2863                         ++dest_i;
2864
2865                 /* if there are no more dest layers, we're done */
2866                 if(dest_i >= dest->totlayer) return;
2867
2868                 /* if we found a matching layer, copy the data */
2869                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2870                         int offset = dest->layers[dest_i].offset;
2871                         char *src_data = source->layers[src_i].data;
2872                         char *dest_data = (char*)*dest_block + offset;
2873
2874                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2875                         src_offset = src_index * typeInfo->size;
2876
2877                         if(typeInfo->copy)
2878                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
2879                         else
2880                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2881
2882                         /* if there are multiple source & dest layers of the same type,
2883                          * we don't want to copy all source layers to the same dest, so
2884                          * increment dest_i
2885                          */
2886                         ++dest_i;
2887                 }
2888         }
2889 }
2890
2891 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2892                                                           void *src_block, int dest_index)
2893 {
2894         const LayerTypeInfo *typeInfo;
2895         int dest_i, src_i, dest_offset;
2896
2897         /* copies a layer at a time */
2898         dest_i = 0;
2899         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2900
2901                 /* find the first dest layer with type >= the source type
2902                  * (this should work because layers are ordered by type)
2903                  */
2904                 while(dest_i < dest->totlayer
2905                           && dest->layers[dest_i].type < source->layers[src_i].type)
2906                         ++dest_i;
2907
2908                 /* if there are no more dest layers, we're done */
2909                 if(dest_i >= dest->totlayer) return;
2910
2911                 /* if we found a matching layer, copy the data */
2912                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2913                         int offset = source->layers[src_i].offset;
2914                         char *src_data = (char*)src_block + offset;
2915                         char *dest_data = dest->layers[dest_i].data;
2916
2917                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2918                         dest_offset = dest_index * typeInfo->size;
2919
2920                         if(typeInfo->copy)
2921                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2922                         else
2923                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2924
2925                         /* if there are multiple source & dest layers of the same type,
2926                          * we don't want to copy all source layers to the same dest, so
2927                          * increment dest_i
2928                          */
2929                         ++dest_i;
2930                 }
2931         }
2932
2933 }
2934
2935 void CustomData_file_write_info(int type, const char **structname, int *structnum)
2936 {
2937         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2938
2939         *structname = typeInfo->structname;
2940         *structnum = typeInfo->structnum;
2941 }
2942
2943 int CustomData_sizeof(int type)
2944 {
2945         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2946
2947         return typeInfo->size;
2948 }
2949
2950 const char *CustomData_layertype_name(int type)
2951 {
2952         return layerType_getName(type);
2953 }
2954
2955 static int  CustomData_is_property_layer(int type)
2956 {
2957         if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2958                 return 1;
2959         return 0;
2960 }
2961
2962 static int cd_layer_find_dupe(CustomData *data, const char *name, int type, int index)
2963 {
2964         int i;
2965         /* see if there is a duplicate */
2966         for(i=0; i<data->totlayer; i++) {
2967                 if(i != index) {
2968                         CustomDataLayer *layer= &data->layers[i];
2969                         
2970                         if(CustomData_is_property_layer(type)) {
2971                                 if(CustomData_is_property_layer(layer->type) && strcmp(layer->name, name)==0) {
2972                                         return 1;
2973                                 }
2974                         }
2975                         else{
2976                                 if(i!=index && layer->type==type && strcmp(layer->name, name)==0) {
2977                                         return 1;
2978                                 }
2979                         }
2980                 }
2981         }
2982         
2983         return 0;
2984 }
2985
2986 static int customdata_unique_check(void *arg, const char *name)
2987 {
2988         struct {CustomData *data; int type; int index;} *data_arg= arg;
2989         return cd_layer_find_dupe(data_arg->data, name, data_arg->type, data_arg->index);
2990 }
2991
2992 void CustomData_set_layer_unique_name(CustomData *data, int index)
2993 {       
2994         CustomDataLayer *nlayer= &data->layers[index];
2995         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2996
2997         struct {CustomData *data; int type; int index;} data_arg;
2998         data_arg.data= data;
2999         data_arg.type= nlayer->type;
3000         data_arg.index= index;
3001
3002         if (!typeInfo->defaultname)
3003                 return;
3004         
3005         BLI_uniquename_cb(customdata_unique_check, &data_arg, typeInfo->defaultname, '.', nlayer->name, sizeof(nlayer->name));
3006 }
3007
3008 void CustomData_validate_layer_name(const CustomData *data, int type, char *name, char *outname)
3009 {
3010         int index = -1;
3011
3012         /* if a layer name was given, try to find that layer */
3013         if(name[0])
3014                 index = CustomData_get_named_layer_index(data, type, name);
3015
3016         if(index < 0) {
3017                 /* either no layer was specified, or the layer we want has been
3018                 * deleted, so assign the active layer to name
3019                 */
3020                 index = CustomData_get_active_layer_index(data, type);
3021                 strcpy(outname, data->layers[index].name);
3022         }
3023         else
3024                 strcpy(outname, name);
3025 }
3026
3027 int CustomData_verify_versions(struct CustomData *data, int index)
3028 {
3029         const LayerTypeInfo *typeInfo;
3030         CustomDataLayer *layer = &data->layers[index];
3031         int i, keeplayer = 1;
3032
3033         if (layer->type >= CD_NUMTYPES) {
3034                 keeplayer = 0; /* unknown layer type from future version */
3035         }
3036         else {
3037                 typeInfo = layerType_getInfo(layer->type);
3038
3039                 if (!typeInfo->defaultname && (index > 0) &&
3040                         data->layers[index-1].type == layer->type)
3041                         keeplayer = 0; /* multiple layers of which we only support one */
3042         }
3043
3044         if (!keeplayer) {
3045                 for (i=index+1; i < data->totlayer; ++i)
3046                         data->layers[i-1] = data->layers[i];
3047                 data->totlayer--;
3048         }
3049
3050         return keeplayer;
3051 }
3052
3053 /****************************** External Files *******************************/
3054
3055 static void customdata_external_filename(char filename[FILE_MAX], ID *id, CustomDataExternal *external)
3056 {
3057         BLI_strncpy(filename, external->filename, FILE_MAX);
3058         BLI_path_abs(filename, ID_BLEND_PATH(G.main, id));
3059 }
3060
3061 void CustomData_external_reload(CustomData *data, ID *UNUSED(id), CustomDataMask mask, int totelem)
3062 {
3063         CustomDataLayer *layer;
3064         const LayerTypeInfo *typeInfo;
3065         int i;
3066
3067         for(i=0; i<data->totlayer; i++) {
3068                 layer = &data->layers[i];
3069                 typeInfo = layerType_getInfo(layer->type);
3070
3071                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3072                 else if((layer->flag & CD_FLAG_EXTERNAL) && (layer->flag & CD_FLAG_IN_MEMORY)) {
3073                         if(typeInfo->free)
3074                                 typeInfo->free(layer->data, totelem, typeInfo->size);
3075                         layer->flag &= ~CD_FLAG_IN_MEMORY;
3076                 }
3077         }
3078 }
3079
3080 void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem)
3081 {
3082         CustomDataExternal *external= data->external;
3083         CustomDataLayer *layer;
3084         CDataFile *cdf;
3085         CDataFileLayer *blay;
3086         char filename[FILE_MAX];
3087         const LayerTypeInfo *typeInfo;
3088         int i, update = 0;
3089
3090         if(!external)
3091                 return;
3092         
3093         for(i=0; i<data->totlayer; i++) {
3094                 layer = &data->layers[i];
3095                 typeInfo = layerType_getInfo(layer->type);
3096
3097                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3098                 else if(layer->flag & CD_FLAG_IN_MEMORY);
3099                 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read)
3100                         update= 1;
3101         }
3102
3103         if(!update)
3104                 return;
3105
3106         customdata_external_filename(filename, id, external);
3107
3108         cdf= cdf_create(CDF_TYPE_MESH);
3109         if(!cdf_read_open(cdf, filename)) {
3110                 fprintf(stderr, "Failed to read %s layer from %s.\n", layerType_getName(layer->type), filename);
3111                 return;
3112         }
3113
3114         for(i=0; i<data->totlayer; i++) {
3115                 layer = &data->layers[i];
3116                 typeInfo = layerType_getInfo(layer->type);
3117
3118                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3119                 else if(layer->flag & CD_FLAG_IN_MEMORY);
3120                 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
3121                         blay= cdf_layer_find(cdf, layer->type, layer->name);
3122
3123                         if(blay) {
3124                                 if(cdf_read_layer(cdf, blay)) {
3125                                         if(typeInfo->read(cdf, layer->data, totelem));
3126                                         else break;
3127                                         layer->flag |= CD_FLAG_IN_MEMORY;
3128                                 }
3129                                 else
3130                                         break;
3131                         }
3132                 }
3133         }
3134
3135         cdf_read_close(cdf);
3136         cdf_free(cdf);
3137 }
3138
3139 void CustomData_external_write(CustomData *data, ID *id, CustomDataMask mask, int totelem, int free)
3140 {
3141         CustomDataExternal *external= data->external;
3142         CustomDataLayer *layer;
3143         CDataFile *cdf;
3144         CDataFileLayer *blay;
3145         const LayerTypeInfo *typeInfo;
3146         int i, update = 0;
3147         char filename[FILE_MAX];
3148
3149         if(!external)
3150                 return;
3151
3152         /* test if there is anything to write */
3153         for(i=0; i<data->totlayer; i++) {
3154                 layer = &data->layers[i];
3155                 typeInfo = layerType_getInfo(layer->type);
3156
3157                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3158                 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write)
3159                         update= 1;
3160         }
3161
3162         if(!update)
3163                 return;
3164
3165         /* make sure data is read before we try to write */
3166         CustomData_external_read(data, id, mask, totelem);
3167         customdata_external_filename(filename, id, external);
3168
3169         cdf= cdf_create(CDF_TYPE_MESH);
3170
3171         for(i=0; i<data->totlayer; i++) {
3172                 layer = &data->layers[i];
3173                 typeInfo = layerType_getInfo(layer->type);
3174
3175                 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) {
3176                         if(layer->flag & CD_FLAG_IN_MEMORY) {
3177                                 cdf_layer_add(cdf, layer->type, layer->name,
3178                                         typeInfo->filesize(cdf, layer->data, totelem));
3179                         }
3180                         else {
3181                                 cdf_free(cdf);
3182                                 return; /* read failed for a layer! */
3183                         }
3184                 }
3185         }
3186
3187         if(!cdf_write_open(cdf, filename)) {
3188                 fprintf(stderr, "Failed to open %s for writing.\n", filename);
3189                 return;
3190         }
3191
3192         for(i=0; i<data->totlayer; i++) {
3193                 layer = &data->layers[i];
3194                 typeInfo = layerType_getInfo(layer->type);
3195
3196                 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
3197                         blay= cdf_layer_find(cdf, layer->type, layer->name);
3198
3199                         if(cdf_write_layer(cdf, blay)) {
3200                                 if(typeInfo->write(cdf, layer->data, totelem));
3201                                 else break;
3202                         }
3203                         else
3204                                 break;
3205                 }
3206         }
3207
3208         if(i != data->totlayer) {
3209                 fprintf(stderr, "Failed to write data to %s.\n", filename);
3210                 cdf_free(cdf);
3211                 return;
3212         }
3213
3214         for(i=0; i<data->totlayer; i++) {
3215                 layer = &data->layers[i];
3216                 typeInfo = layerType_getInfo(layer->type);
3217
3218                 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
3219                         if(free) {
3220                                 if(typeInfo->free)
3221                                         typeInfo->free(layer->data, totelem, typeInfo->size);
3222                                 layer->flag &= ~CD_FLAG_IN_MEMORY;
3223                         }
3224                 }
3225         }
3226
3227         cdf_write_close(cdf);
3228         cdf_free(cdf);
3229 }
3230
3231 void CustomData_external_add(CustomData *data, ID *UNUSED(id), int type, int UNUSED(totelem), const char *filename)
3232 {
3233         CustomDataExternal *external= data->external;
3234         CustomDataLayer *layer;
3235         int layer_index;
3236
3237         layer_index = CustomData_get_active_layer_index(data, type);
3238         if(layer_index < 0) return;
3239
3240         layer = &data->layers[layer_index];
3241
3242         if(layer->flag & CD_FLAG_EXTERNAL)
3243                 return;
3244
3245         if(!external) {
3246                 external= MEM_callocN(sizeof(CustomDataExternal), "CustomDataExternal");
3247                 data->external= external;
3248         }
3249         BLI_strncpy(external->filename, filename, sizeof(external->filename));
3250
3251         layer->flag |= CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY;
3252 }
3253
3254 void CustomData_external_remove(CustomData *data, ID *id, int type, int totelem)
3255 {
3256         CustomDataExternal *external= data->external;
3257         CustomDataLayer *layer;
3258         //char filename[FILE_MAX];
3259         int layer_index; // i, remove_file;
3260
3261         layer_index = CustomData_get_active_layer_index(data, type);
3262         if(layer_index < 0) return;
3263
3264         layer = &data->layers[layer_index];
3265
3266         if(!external)
3267                 return;
3268
3269         if(layer->flag & CD_FLAG_EXTERNAL) {
3270                 if(!(layer->flag & CD_FLAG_IN_MEMORY))
3271                         CustomData_external_read(data, id, CD_TYPE_AS_MASK(layer->type), totelem);
3272
3273                 layer->flag &= ~CD_FLAG_EXTERNAL;
3274
3275 #if 0
3276                 remove_file= 1;
3277                 for(i=0; i<data->totlayer; i++)
3278                         if(data->layers[i].flag & CD_FLAG_EXTERNAL)
3279                                 remove_file= 0;
3280
3281                 if(remove_file) {
3282                         customdata_external_filename(filename, id, external);
3283                         cdf_remove(filename);
3284                         CustomData_external_free(data);
3285                 }
3286 #endif
3287         }
3288 }
3289
3290 int CustomData_external_test(CustomData *data, int type)
3291 {
3292         CustomDataLayer *layer;
3293         int layer_index;
3294
3295         layer_index = CustomData_get_active_layer_index(data, type);
3296         if(layer_index < 0) return 0;
3297