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