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