2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2006 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Ben Batt <benbatt@gmail.com>
25 * ***** END GPL LICENSE BLOCK *****
27 * Implementation of CustomData.
29 * BKE_customdata.h contains the function prototypes for this file.
33 /** \file blender/blenkernel/intern/customdata.c
42 #include "MEM_guardedalloc.h"
44 #include "DNA_meshdata_types.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_linklist.h"
51 #include "BLI_mempool.h"
52 #include "BLI_utildefines.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_customdata.h"
56 #include "BKE_customdata_file.h"
57 #include "BKE_global.h"
59 #include "BKE_multires.h"
66 /* number of layers to add when growing a CustomData object */
67 #define CUSTOMDATA_GROW 5
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 */
75 /* default layer name.
76 * note! when NULL this is a way to ensure there is only ever one item
77 * see: CustomData_layertype_is_singleton() */
78 const char *defaultname;
80 /* a function to copy count elements of this layer's data
81 * (deep copy if appropriate)
82 * if NULL, memcpy is used
84 void (*copy)(const void *source, void *dest, int count);
86 /* a function to free any dynamically allocated components of this
87 * layer's data (note the data pointer itself should not be freed)
88 * size should be the size of one element of this layer's data (e.g.
91 void (*free)(void *data, int count, int size);
93 /* a function to interpolate between count source elements of this
94 * layer's data and store the result in dest
95 * if weights == NULL or sub_weights == NULL, they should default to 1
97 * weights gives the weight for each element in sources
98 * sub_weights gives the sub-element weights for each element in sources
99 * (there should be (sub element count)^2 weights per element)
100 * count gives the number of elements in sources
102 void (*interp)(void **sources, float *weights, float *sub_weights,
103 int count, void *dest);
105 /* a function to swap the data in corners of the element */
106 void (*swap)(void *data, const int *corner_indices);
108 /* a function to set a layer's data to default values. if NULL, the
109 * default is assumed to be all zeros */
110 void (*set_default)(void *data, int count);
112 /* functions necessary for geometry collapse*/
113 int (*equal)(void *data1, void *data2);
114 void (*multiply)(void *data, float fac);
115 void (*initminmax)(void *min, void *max);
116 void (*add)(void *data1, void *data2);
117 void (*dominmax)(void *data1, void *min, void *max);
118 void (*copyvalue)(void *source, void *dest);
120 /* a function to read data from a cdf file */
121 int (*read)(CDataFile *cdf, void *data, int count);
123 /* a function to write data to a cdf file */
124 int (*write)(CDataFile *cdf, void *data, int count);
126 /* a function to determine file size */
127 size_t (*filesize)(CDataFile *cdf, void *data, int count);
130 static void layerCopy_mdeformvert(const void *source, void *dest,
133 int i, size = sizeof(MDeformVert);
135 memcpy(dest, source, count * size);
137 for (i = 0; i < count; ++i) {
138 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
140 if (dvert->totweight) {
141 MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
142 "layerCopy_mdeformvert dw");
144 memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
152 static void layerFree_mdeformvert(void *data, int count, int size)
156 for (i = 0; i < count; ++i) {
157 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
160 MEM_freeN(dvert->dw);
162 dvert->totweight = 0;
167 /* copy just zeros in this case */
168 static void layerCopy_bmesh_elem_py_ptr(const void *UNUSED(source), void *dest,
171 int i, size = sizeof(void *);
173 for (i = 0; i < count; ++i) {
174 void **ptr = (void **)((char *)dest + i * size);
180 void bpy_bm_generic_invalidate(void *UNUSED(self))
186 static void layerFree_bmesh_elem_py_ptr(void *data, int count, int size)
188 extern void bpy_bm_generic_invalidate(void *self);
192 for (i = 0; i < count; ++i) {
193 void **ptr = (void *)((char *)data + i * size);
195 bpy_bm_generic_invalidate(*ptr);
201 static void linklist_free_simple(void *link)
206 static void layerInterp_mdeformvert(void **sources, float *weights,
207 float *UNUSED(sub_weights), int count, void *dest)
209 MDeformVert *dvert = dest;
210 LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
214 if (count <= 0) return;
216 /* build a list of unique def_nrs for dest */
218 for (i = 0; i < count; ++i) {
219 MDeformVert *source = sources[i];
220 float interp_weight = weights ? weights[i] : 1.0f;
222 for (j = 0; j < source->totweight; ++j) {
223 MDeformWeight *dw = &source->dw[j];
225 for (node = dest_dw; node; node = node->next) {
226 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
228 if (tmp_dw->def_nr == dw->def_nr) {
229 tmp_dw->weight += dw->weight * interp_weight;
234 /* if this def_nr is not in the list, add it */
236 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
237 "layerInterp_mdeformvert tmp_dw");
238 tmp_dw->def_nr = dw->def_nr;
239 tmp_dw->weight = dw->weight * interp_weight;
240 BLI_linklist_prepend(&dest_dw, tmp_dw);
246 /* now we know how many unique deform weights there are, so realloc */
247 if (dvert->dw) MEM_freeN(dvert->dw);
250 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
251 "layerInterp_mdeformvert dvert->dw");
252 dvert->totweight = totweight;
254 for (i = 0, node = dest_dw; node; node = node->next, ++i)
255 dvert->dw[i] = *((MDeformWeight *)node->link);
258 memset(dvert, 0, sizeof(*dvert));
260 BLI_linklist_free(dest_dw, linklist_free_simple);
264 static void layerInterp_msticky(void **sources, float *weights,
265 float *UNUSED(sub_weights), int count, void *dest)
271 co[0] = co[1] = 0.0f;
272 for (i = 0; i < count; i++) {
273 w = weights ? weights[i] : 1.0f;
274 mst = (MSticky*)sources[i];
276 madd_v2_v2fl(co, mst->co, w);
279 mst = (MSticky*)dest;
280 copy_v2_v2(mst->co, co);
284 static void layerCopy_tface(const void *source, void *dest, int count)
286 const MTFace *source_tf = (const MTFace*)source;
287 MTFace *dest_tf = (MTFace*)dest;
290 for (i = 0; i < count; ++i)
291 dest_tf[i] = source_tf[i];
294 static void layerInterp_tface(void **sources, float *weights,
295 float *sub_weights, int count, void *dest)
299 float uv[4][2] = {{0.0f}};
302 if (count <= 0) return;
304 sub_weight = sub_weights;
305 for (i = 0; i < count; ++i) {
306 float weight = weights ? weights[i] : 1;
307 MTFace *src = sources[i];
309 for (j = 0; j < 4; ++j) {
311 for (k = 0; k < 4; ++k, ++sub_weight) {
312 madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
316 madd_v2_v2fl(uv[j], src->uv[j], weight);
321 *tf = *(MTFace *)(*sources);
322 memcpy(tf->uv, uv, sizeof(tf->uv));
325 static void layerSwap_tface(void *data, const int *corner_indices)
329 static const short pin_flags[4] =
330 { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
331 static const char sel_flags[4] =
332 { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
333 short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
334 char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
337 for (j = 0; j < 4; ++j) {
338 const int source_index = corner_indices[j];
340 copy_v2_v2(uv[j], tf->uv[source_index]);
342 // swap pinning flags around
343 if (tf->unwrap & pin_flags[source_index]) {
344 unwrap |= pin_flags[j];
347 // swap selection flags around
348 if (tf->flag & sel_flags[source_index]) {
349 flag |= sel_flags[j];
353 memcpy(tf->uv, uv, sizeof(tf->uv));
358 static void layerDefault_tface(void *data, int count)
360 static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
361 0, 0, TF_DYNAMIC|TF_CONVERTED, 0, 0};
362 MTFace *tf = (MTFace*)data;
365 for (i = 0; i < count; i++)
369 static void layerCopy_propFloat(const void *source, void *dest,
372 memcpy(dest, source, sizeof(MFloatProperty)*count);
375 static void layerCopy_propInt(const void *source, void *dest,
378 memcpy(dest, source, sizeof(MIntProperty)*count);
381 static void layerCopy_propString(const void *source, void *dest,
384 memcpy(dest, source, sizeof(MStringProperty)*count);
387 static void layerCopy_origspace_face(const void *source, void *dest, int count)
389 const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
390 OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
393 for (i = 0; i < count; ++i)
394 dest_tf[i] = source_tf[i];
397 static void layerInterp_origspace_face(void **sources, float *weights,
398 float *sub_weights, int count, void *dest)
400 OrigSpaceFace *osf = dest;
402 float uv[4][2] = {{0.0f}};
405 if (count <= 0) return;
407 sub_weight = sub_weights;
408 for (i = 0; i < count; ++i) {
409 float weight = weights ? weights[i] : 1;
410 OrigSpaceFace *src = sources[i];
412 for (j = 0; j < 4; ++j) {
414 for (k = 0; k < 4; ++k, ++sub_weight) {
415 madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
419 madd_v2_v2fl(uv[j], src->uv[j], weight);
424 #if 0 /* no need, this ONLY contains UV's */
425 *osf = *(OrigSpaceFace *)(*sources);
427 memcpy(osf->uv, uv, sizeof(osf->uv));
430 static void layerSwap_origspace_face(void *data, const int *corner_indices)
432 OrigSpaceFace *osf = data;
436 for (j = 0; j < 4; ++j) {
437 copy_v2_v2(uv[j], osf->uv[corner_indices[j]]);
439 memcpy(osf->uv, uv, sizeof(osf->uv));
442 static void layerDefault_origspace_face(void *data, int count)
444 static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
445 OrigSpaceFace *osf = (OrigSpaceFace*)data;
448 for (i = 0; i < count; i++)
449 osf[i] = default_osf;
452 static void layerSwap_mdisps(void *data, const int *ci)
455 float (*d)[3] = NULL;
456 int corners, cornersize, S;
459 int nverts= (ci[1] == 3) ? 4 : 3; /* silly way to know vertex count of face */
460 corners= multires_mdisp_corners(s);
461 cornersize= s->totdisp/corners;
463 if (corners!=nverts) {
464 /* happens when face changed vertex count in edit mode
465 * if it happened, just forgot displacement */
468 s->totdisp= (s->totdisp/corners)*nverts;
469 s->disps= MEM_callocN(s->totdisp*sizeof(float)*3, "mdisp swap");
473 d= MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
475 for (S = 0; S < corners; S++)
476 memcpy(d + cornersize*S, s->disps + cornersize*ci[S], cornersize*3*sizeof(float));
483 static void layerCopy_mdisps(const void *source, void *dest, int count)
486 const MDisps *s = source;
489 for (i = 0; i < count; ++i) {
491 d[i].disps = MEM_dupallocN(s[i].disps);
492 d[i].hidden = MEM_dupallocN(s[i].hidden);
493 d[i].totdisp = s[i].totdisp;
494 d[i].level = s[i].level;
506 static void layerFree_mdisps(void *data, int count, int UNUSED(size))
511 for (i = 0; i < count; ++i) {
513 MEM_freeN(d[i].disps);
515 MEM_freeN(d[i].hidden);
523 static int layerRead_mdisps(CDataFile *cdf, void *data, int count)
528 for (i = 0; i < count; ++i) {
530 d[i].disps = MEM_callocN(sizeof(float)*3*d[i].totdisp, "mdisps read");
532 if (!cdf_read_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
533 printf("failed to read multires displacement %d/%d %d\n", i, count, d[i].totdisp);
541 static int layerWrite_mdisps(CDataFile *cdf, void *data, int count)
546 for (i = 0; i < count; ++i) {
547 if (!cdf_write_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
548 printf("failed to write multires displacement %d/%d %d\n", i, count, d[i].totdisp);
556 static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), void *data, int count)
562 for (i = 0; i < count; ++i)
563 size += d[i].totdisp*3*sizeof(float);
569 static void layerCopyValue_mloopcol(void *source, void *dest)
571 MLoopCol *m1 = source, *m2 = dest;
579 static int layerEqual_mloopcol(void *data1, void *data2)
581 MLoopCol *m1 = data1, *m2 = data2;
589 return r*r + g*g + b*b + a*a < 0.001;
592 static void layerMultiply_mloopcol(void *data, float fac)
596 m->r = (float)m->r * fac;
597 m->g = (float)m->g * fac;
598 m->b = (float)m->b * fac;
599 m->a = (float)m->a * fac;
602 static void layerAdd_mloopcol(void *data1, void *data2)
604 MLoopCol *m = data1, *m2 = data2;
612 static void layerDoMinMax_mloopcol(void *data, void *vmin, void *vmax)
615 MLoopCol *min = vmin, *max = vmax;
617 if (m->r < min->r) min->r = m->r;
618 if (m->g < min->g) min->g = m->g;
619 if (m->b < min->b) min->b = m->b;
620 if (m->a < min->a) min->a = m->a;
622 if (m->r > max->r) max->r = m->r;
623 if (m->g > max->g) max->g = m->g;
624 if (m->b > max->b) max->b = m->b;
625 if (m->a > max->a) max->a = m->a;
628 static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
630 MLoopCol *min = vmin, *max = vmax;
643 static void layerDefault_mloopcol(void *data, int count)
645 MLoopCol default_mloopcol = {255, 255, 255, 255};
646 MLoopCol *mlcol = (MLoopCol*)data;
648 for (i = 0; i < count; i++)
649 mlcol[i] = default_mloopcol;
653 static void layerInterp_mloopcol(void **sources, float *weights,
654 float *sub_weights, int count, void *dest)
665 col.a = col.r = col.g = col.b = 0;
667 sub_weight = sub_weights;
668 for (i = 0; i < count; ++i) {
669 float weight = weights ? weights[i] : 1;
670 MLoopCol *src = sources[i];
672 col.r += src->r * (*sub_weight) * weight;
673 col.g += src->g * (*sub_weight) * weight;
674 col.b += src->b * (*sub_weight) * weight;
675 col.a += src->a * (*sub_weight) * weight;
679 col.r += src->r * weight;
680 col.g += src->g * weight;
681 col.b += src->b * weight;
682 col.a += src->a * weight;
686 /* Subdivide smooth or fractal can cause problems without clamping
687 * although weights should also not cause this situation */
688 CLAMP(col.a, 0.0f, 255.0f);
689 CLAMP(col.r, 0.0f, 255.0f);
690 CLAMP(col.g, 0.0f, 255.0f);
691 CLAMP(col.b, 0.0f, 255.0f);
699 static void layerCopyValue_mloopuv(void *source, void *dest)
701 MLoopUV *luv1 = source, *luv2 = dest;
703 copy_v2_v2(luv2->uv, luv1->uv);
706 static int layerEqual_mloopuv(void *data1, void *data2)
708 MLoopUV *luv1 = data1, *luv2 = data2;
710 return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
713 static void layerMultiply_mloopuv(void *data, float fac)
717 mul_v2_fl(luv->uv, fac);
720 static void layerInitMinMax_mloopuv(void *vmin, void *vmax)
722 MLoopUV *min = vmin, *max = vmax;
724 INIT_MINMAX2(min->uv, max->uv);
727 static void layerDoMinMax_mloopuv(void *data, void *vmin, void *vmax)
729 MLoopUV *min = vmin, *max = vmax, *luv = data;
731 DO_MINMAX2(luv->uv, min->uv, max->uv);
734 static void layerAdd_mloopuv(void *data1, void *data2)
736 MLoopUV *l1 = data1, *l2 = data2;
738 add_v2_v2(l1->uv, l2->uv);
741 static void layerInterp_mloopuv(void **sources, float *weights,
742 float *sub_weights, int count, void *dest)
744 MLoopUV *mluv = dest;
751 const float *sub_weight = sub_weights;
752 for (i = 0; i < count; i++) {
753 float weight = weights ? weights[i] : 1.0f;
754 MLoopUV *src = sources[i];
755 madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
760 for (i = 0; i < count; i++) {
761 float weight = weights ? weights[i] : 1;
762 MLoopUV *src = sources[i];
763 madd_v2_v2fl(uv, src->uv, weight);
768 /* origspace is almost exact copy of mloopuv's, keep in sync */
769 static void layerCopyValue_mloop_origspace(void *source, void *dest)
771 OrigSpaceLoop *luv1 = source, *luv2 = dest;
773 copy_v2_v2(luv2->uv, luv1->uv);
776 static int layerEqual_mloop_origspace(void *data1, void *data2)
778 OrigSpaceLoop *luv1 = data1, *luv2 = data2;
780 return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
783 static void layerMultiply_mloop_origspace(void *data, float fac)
785 OrigSpaceLoop *luv = data;
787 mul_v2_fl(luv->uv, fac);
790 static void layerInitMinMax_mloop_origspace(void *vmin, void *vmax)
792 OrigSpaceLoop *min = vmin, *max = vmax;
794 INIT_MINMAX2(min->uv, max->uv);
797 static void layerDoMinMax_mloop_origspace(void *data, void *vmin, void *vmax)
799 OrigSpaceLoop *min = vmin, *max = vmax, *luv = data;
801 DO_MINMAX2(luv->uv, min->uv, max->uv);
804 static void layerAdd_mloop_origspace(void *data1, void *data2)
806 OrigSpaceLoop *l1 = data1, *l2 = data2;
808 add_v2_v2(l1->uv, l2->uv);
811 static void layerInterp_mloop_origspace(void **sources, float *weights,
812 float *sub_weights, int count, void *dest)
814 OrigSpaceLoop *mluv = dest;
821 const float *sub_weight = sub_weights;
822 for (i = 0; i < count; i++) {
823 float weight = weights ? weights[i] : 1.0f;
824 OrigSpaceLoop *src = sources[i];
825 madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
830 for (i = 0; i < count; i++) {
831 float weight = weights ? weights[i] : 1;
832 OrigSpaceLoop *src = sources[i];
833 madd_v2_v2fl(uv, src->uv, weight);
839 static void layerInterp_mcol(void **sources, float *weights,
840 float *sub_weights, int count, void *dest)
853 if (count <= 0) return;
855 sub_weight = sub_weights;
856 for (i = 0; i < count; ++i) {
857 float weight = weights ? weights[i] : 1;
859 for (j = 0; j < 4; ++j) {
861 MCol *src = sources[i];
862 for (k = 0; k < 4; ++k, ++sub_weight, ++src) {
863 const float w= (*sub_weight) * weight;
864 col[j].a += src->a * w;
865 col[j].r += src->r * w;
866 col[j].g += src->g * w;
867 col[j].b += src->b * w;
871 MCol *src = sources[i];
872 col[j].a += src[j].a * weight;
873 col[j].r += src[j].r * weight;
874 col[j].g += src[j].g * weight;
875 col[j].b += src[j].b * weight;
880 for (j = 0; j < 4; ++j) {
882 /* Subdivide smooth or fractal can cause problems without clamping
883 * although weights should also not cause this situation */
884 CLAMP(col[j].a, 0.0f, 255.0f);
885 CLAMP(col[j].r, 0.0f, 255.0f);
886 CLAMP(col[j].g, 0.0f, 255.0f);
887 CLAMP(col[j].b, 0.0f, 255.0f);
889 mc[j].a = (int)col[j].a;
890 mc[j].r = (int)col[j].r;
891 mc[j].g = (int)col[j].g;
892 mc[j].b = (int)col[j].b;
896 static void layerSwap_mcol(void *data, const int *corner_indices)
902 for (j = 0; j < 4; ++j)
903 col[j] = mcol[corner_indices[j]];
905 memcpy(mcol, col, sizeof(col));
908 static void layerDefault_mcol(void *data, int count)
910 static MCol default_mcol = {255, 255, 255, 255};
911 MCol *mcol = (MCol*)data;
914 for (i = 0; i < 4*count; i++) {
915 mcol[i] = default_mcol;
919 static void layerInterp_bweight(void **sources, float *weights,
920 float *UNUSED(sub_weights), int count, void *dest)
923 float **in = (float **)sources;
926 if (count <= 0) return;
931 for (i = 0; i < count; ++i) {
932 *f += *in[i] * weights[i];
936 for (i = 0; i < count; ++i) {
942 static void layerInterp_shapekey(void **sources, float *weights,
943 float *UNUSED(sub_weights), int count, void *dest)
946 float **in = (float **)sources;
949 if (count <= 0) return;
954 for (i = 0; i < count; ++i) {
955 madd_v3_v3fl(co, in[i], weights[i]);
959 for (i = 0; i < count; ++i) {
960 add_v3_v3(co, in[i]);
965 static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
967 {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
969 {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
971 /* 2: CD_MDEFORMVERT */
972 {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
973 layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
975 {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
977 {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
979 {sizeof(MTFace), "MTFace", 1, "UVMap", layerCopy_tface, NULL,
980 layerInterp_tface, layerSwap_tface, layerDefault_tface},
982 /* 4 MCol structs per face */
983 {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
984 layerSwap_mcol, layerDefault_mcol},
985 /* 7: CD_ORIGINDEX */
986 {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
988 /* 3 floats per normal vector */
989 {sizeof(float)*3, "vec3f", 1, NULL, NULL, NULL, NULL, NULL, NULL},
990 /* 9: CD_POLYINDEX */
991 {sizeof(int), "MIntProperty", 1, NULL, NULL, NULL, NULL, NULL, NULL},
992 /* 10: CD_PROP_FLT */
993 {sizeof(MFloatProperty), "MFloatProperty", 1, "Float", layerCopy_propFloat, NULL, NULL, NULL},
994 /* 11: CD_PROP_INT */
995 {sizeof(MIntProperty), "MIntProperty", 1, "Int", layerCopy_propInt, NULL, NULL, NULL},
996 /* 12: CD_PROP_STR */
997 {sizeof(MStringProperty), "MStringProperty", 1, "String", layerCopy_propString, NULL, NULL, NULL},
998 /* 13: CD_ORIGSPACE */
999 {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVMap", layerCopy_origspace_face, NULL,
1000 layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
1002 {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1003 /* 15: CD_MTEXPOLY */
1004 /* note, when we expose the UV Map / TexFace split to the user, change this back to face Texture */
1005 {sizeof(MTexPoly), "MTexPoly", 1, "UVMap"/* "Face Texture" */, NULL, NULL, NULL, NULL, NULL},
1006 /* 16: CD_MLOOPUV */
1007 {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
1008 layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv,
1009 layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
1010 /* 17: CD_MLOOPCOL */
1011 {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL,
1012 layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1013 layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1014 /* 18: CD_TANGENT */
1015 {sizeof(float)*4*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1017 {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
1018 layerFree_mdisps, NULL, layerSwap_mdisps, NULL,
1019 NULL, NULL, NULL, NULL, NULL, NULL,
1020 layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps},
1021 /* 20: CD_PREVIEW_MCOL */
1022 {sizeof(MCol)*4, "MCol", 4, "PreviewCol", NULL, NULL, layerInterp_mcol,
1023 layerSwap_mcol, layerDefault_mcol},
1024 /* 21: CD_ID_MCOL */
1025 {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
1026 layerSwap_mcol, layerDefault_mcol},
1027 /* 22: CD_TEXTURE_MCOL */
1028 {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
1029 layerSwap_mcol, layerDefault_mcol},
1030 /* 23: CD_CLOTH_ORCO */
1031 {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1033 {sizeof(MRecast), "MRecast", 1, "Recast", NULL, NULL, NULL, NULL}
1038 {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
1040 {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
1041 /* 27: CD_SHAPE_KEYINDEX */
1042 {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1043 /* 28: CD_SHAPEKEY */
1044 {sizeof(float)*3, "", 0, "ShapeKey", NULL, NULL, layerInterp_shapekey},
1045 /* 29: CD_BWEIGHT */
1046 {sizeof(float), "", 0, "BevelWeight", NULL, NULL, layerInterp_bweight},
1048 {sizeof(float), "", 0, "SubSurfCrease", NULL, NULL, layerInterp_bweight},
1049 /* 31: CD_ORIGSPACE_MLOOP */
1050 {sizeof(OrigSpaceLoop), "OrigSpaceLoop", 1, "OS Loop", NULL, NULL, layerInterp_mloop_origspace, NULL, NULL,
1051 layerEqual_mloop_origspace, layerMultiply_mloop_origspace, layerInitMinMax_mloop_origspace,
1052 layerAdd_mloop_origspace, layerDoMinMax_mloop_origspace, layerCopyValue_mloop_origspace},
1053 /* 32: CD_PREVIEW_MLOOPCOL */
1054 {sizeof(MLoopCol), "MLoopCol", 1, "PreviewLoopCol", NULL, NULL, layerInterp_mloopcol, NULL,
1055 layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1056 layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1057 /* 33: CD_BM_ELEM_PYPTR */
1058 {sizeof(void *), "", 1, NULL, layerCopy_bmesh_elem_py_ptr,
1059 layerFree_bmesh_elem_py_ptr, NULL, NULL, NULL},
1061 /* END BMESH ONLY */
1066 /* note, numbers are from trunk and need updating for bmesh */
1068 static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
1069 /* 0-4 */ "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace",
1070 /* 5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
1071 /* 10-14 */ "CDMFloatProperty", "CDMIntProperty", "CDMStringProperty", "CDOrigSpace", "CDOrco",
1072 /* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
1073 /* 20-24 */"CDPreviewMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco", "CDMRecast"
1077 /* 25-29 */ "CDMPoly", "CDMLoop", "CDShapeKeyIndex", "CDShapeKey", "CDBevelWeight",
1078 /* 30-32 */ "CDSubSurfCrease", "CDOrigSpaceLoop", "CDPreviewLoopCol"
1079 /* END BMESH ONLY */
1084 const CustomDataMask CD_MASK_BAREMESH =
1085 CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY | CD_MASK_BWEIGHT;
1086 const CustomDataMask CD_MASK_MESH =
1087 CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
1088 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
1089 CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS |
1090 CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MPOLY | CD_MASK_MLOOP |
1091 CD_MASK_MTEXPOLY | CD_MASK_NORMAL | CD_MASK_RECAST;
1092 const CustomDataMask CD_MASK_EDITMESH =
1093 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MLOOPUV |
1094 CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_SHAPE_KEYINDEX |
1095 CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR |
1096 CD_MASK_MDISPS | CD_MASK_SHAPEKEY | CD_MASK_RECAST;
1097 const CustomDataMask CD_MASK_DERIVEDMESH =
1098 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
1099 CD_MASK_MCOL | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_CLOTH_ORCO |
1100 CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_PREVIEW_MLOOPCOL |
1101 CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORIGSPACE_MLOOP | CD_MASK_ORCO | CD_MASK_TANGENT |
1102 CD_MASK_PREVIEW_MCOL | CD_MASK_NORMAL | CD_MASK_SHAPEKEY | CD_MASK_RECAST |
1103 CD_MASK_ORIGINDEX | CD_MASK_POLYINDEX;
1104 const CustomDataMask CD_MASK_BMESH = CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
1105 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT |
1106 CD_MASK_PROP_STR | CD_MASK_SHAPEKEY | CD_MASK_SHAPE_KEYINDEX | CD_MASK_MDISPS | CD_MASK_CREASE | CD_MASK_BWEIGHT | CD_MASK_RECAST;
1107 const CustomDataMask CD_MASK_FACECORNERS =
1108 CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
1111 static const LayerTypeInfo *layerType_getInfo(int type)
1113 if (type < 0 || type >= CD_NUMTYPES) return NULL;
1115 return &LAYERTYPEINFO[type];
1118 static const char *layerType_getName(int type)
1120 if (type < 0 || type >= CD_NUMTYPES) return NULL;
1122 return LAYERTYPENAMES[type];
1125 /********************* CustomData functions *********************/
1126 static void customData_update_offsets(CustomData *data);
1128 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1129 int type, int alloctype, void *layerdata, int totelem, const char *name);
1131 void CustomData_update_typemap(CustomData *data)
1133 int i, lasttype = -1;
1135 /* since we cant do in a pre-processor do here as an assert */
1136 BLI_assert(sizeof(data->typemap) / sizeof(int) >= CD_NUMTYPES);
1138 for (i=0; i<CD_NUMTYPES; i++) {
1139 data->typemap[i] = -1;
1142 for (i=0; i<data->totlayer; i++) {
1143 if (data->layers[i].type != lasttype) {
1144 data->typemap[data->layers[i].type] = i;
1146 lasttype = data->layers[i].type;
1150 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
1151 CustomDataMask mask, int alloctype, int totelem)
1153 /*const LayerTypeInfo *typeInfo;*/
1154 CustomDataLayer *layer, *newlayer;
1156 int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0, lastflag = 0;
1158 for (i = 0; i < source->totlayer; ++i) {
1159 layer = &source->layers[i];
1160 /*typeInfo = layerType_getInfo(layer->type);*/ /*UNUSED*/
1164 if (type != lasttype) {
1166 lastactive = layer->active;
1167 lastrender = layer->active_rnd;
1168 lastclone = layer->active_clone;
1169 lastmask = layer->active_mask;
1171 lastflag = layer->flag;
1176 if (lastflag & CD_FLAG_NOCOPY) continue;
1177 else if (!(mask & CD_TYPE_AS_MASK(type))) continue;
1178 else if (number < CustomData_number_of_layers(dest, type)) continue;
1180 switch (alloctype) {
1191 if ((alloctype == CD_ASSIGN) && (lastflag & CD_FLAG_NOFREE))
1192 newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
1193 data, totelem, layer->name);
1195 newlayer = customData_add_layer__internal(dest, type, alloctype,
1196 data, totelem, layer->name);
1199 newlayer->uid = layer->uid;
1201 newlayer->active = lastactive;
1202 newlayer->active_rnd = lastrender;
1203 newlayer->active_clone = lastclone;
1204 newlayer->active_mask = lastmask;
1205 newlayer->flag |= lastflag & (CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY);
1209 CustomData_update_typemap(dest);
1212 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
1213 CustomDataMask mask, int alloctype, int totelem)
1215 memset(dest, 0, sizeof(*dest));
1217 if (source->external)
1218 dest->external= MEM_dupallocN(source->external);
1220 CustomData_merge(source, dest, mask, alloctype, totelem);
1223 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
1225 const LayerTypeInfo *typeInfo;
1227 if (!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
1228 typeInfo = layerType_getInfo(layer->type);
1231 typeInfo->free(layer->data, totelem, typeInfo->size);
1234 MEM_freeN(layer->data);
1238 static void CustomData_external_free(CustomData *data)
1240 if (data->external) {
1241 MEM_freeN(data->external);
1242 data->external= NULL;
1246 void CustomData_free(CustomData *data, int totelem)
1250 for (i = 0; i < data->totlayer; ++i)
1251 customData_free_layer__internal(&data->layers[i], totelem);
1254 MEM_freeN(data->layers);
1256 CustomData_external_free(data);
1258 memset(data, 0, sizeof(*data));
1261 static void customData_update_offsets(CustomData *data)
1263 const LayerTypeInfo *typeInfo;
1266 for (i = 0; i < data->totlayer; ++i) {
1267 typeInfo = layerType_getInfo(data->layers[i].type);
1269 data->layers[i].offset = offset;
1270 offset += typeInfo->size;
1273 data->totsize = offset;
1274 CustomData_update_typemap(data);
1277 int CustomData_get_layer_index(const CustomData *data, int type)
1281 for (i=0; i < data->totlayer; ++i)
1282 if (data->layers[i].type == type)
1288 int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n)
1290 int i = CustomData_get_layer_index(data, type);
1293 i = (data->layers[i + n].type == type) ? (i + n) : (-1);
1299 int CustomData_get_named_layer_index(const CustomData *data, int type, const char *name)
1303 for (i=0; i < data->totlayer; ++i)
1304 if (data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
1310 int CustomData_get_active_layer_index(const CustomData *data, int type)
1312 if (!data->totlayer)
1315 if (data->typemap[type] != -1) {
1316 return data->typemap[type] + data->layers[data->typemap[type]].active;
1322 int CustomData_get_render_layer_index(const CustomData *data, int type)
1326 for (i=0; i < data->totlayer; ++i)
1327 if (data->layers[i].type == type)
1328 return i + data->layers[i].active_rnd;
1333 int CustomData_get_clone_layer_index(const CustomData *data, int type)
1337 for (i=0; i < data->totlayer; ++i)
1338 if (data->layers[i].type == type)
1339 return i + data->layers[i].active_clone;
1344 int CustomData_get_stencil_layer_index(const CustomData *data, int type)
1348 for (i=0; i < data->totlayer; ++i)
1349 if (data->layers[i].type == type)
1350 return i + data->layers[i].active_mask;
1355 int CustomData_get_active_layer(const CustomData *data, int type)
1359 for (i=0; i < data->totlayer; ++i)
1360 if (data->layers[i].type == type)
1361 return data->layers[i].active;
1366 int CustomData_get_render_layer(const CustomData *data, int type)
1370 for (i=0; i < data->totlayer; ++i)
1371 if (data->layers[i].type == type)
1372 return data->layers[i].active_rnd;
1377 int CustomData_get_clone_layer(const CustomData *data, int type)
1381 for (i=0; i < data->totlayer; ++i)
1382 if (data->layers[i].type == type)
1383 return data->layers[i].active_clone;
1388 int CustomData_get_stencil_layer(const CustomData *data, int type)
1392 for (i=0; i < data->totlayer; ++i)
1393 if (data->layers[i].type == type)
1394 return data->layers[i].active_mask;
1399 void CustomData_set_layer_active(CustomData *data, int type, int n)
1403 for (i=0; i < data->totlayer; ++i)
1404 if (data->layers[i].type == type)
1405 data->layers[i].active = n;
1408 void CustomData_set_layer_render(CustomData *data, int type, int n)
1412 for (i=0; i < data->totlayer; ++i)
1413 if (data->layers[i].type == type)
1414 data->layers[i].active_rnd = n;
1417 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1421 for (i=0; i < data->totlayer; ++i)
1422 if (data->layers[i].type == type)
1423 data->layers[i].active_clone = n;
1426 void CustomData_set_layer_stencil(CustomData *data, int type, int n)
1430 for (i=0; i < data->totlayer; ++i)
1431 if (data->layers[i].type == type)
1432 data->layers[i].active_mask = n;
1435 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1436 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1440 for (i=0; i < data->totlayer; ++i)
1441 if (data->layers[i].type == type)
1442 data->layers[i].active = n-i;
1445 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1449 for (i=0; i < data->totlayer; ++i)
1450 if (data->layers[i].type == type)
1451 data->layers[i].active_rnd = n-i;
1454 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1458 for (i=0; i < data->totlayer; ++i)
1459 if (data->layers[i].type == type)
1460 data->layers[i].active_clone = n-i;
1463 void CustomData_set_layer_stencil_index(CustomData *data, int type, int n)
1467 for (i=0; i < data->totlayer; ++i)
1468 if (data->layers[i].type == type)
1469 data->layers[i].active_mask = n-i;
1472 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1476 for (i=0; i < data->totlayer; ++i)
1477 if (data->layers[i].type == type)
1478 data->layers[i].flag |= flag;
1481 static int customData_resize(CustomData *data, int amount)
1483 CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1484 "CustomData->layers");
1487 data->maxlayer += amount;
1489 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1490 MEM_freeN(data->layers);
1497 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1498 int type, int alloctype, void *layerdata, int totelem, const char *name)
1500 const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1501 int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1502 void *newlayerdata = NULL;
1504 /* Passing a layerdata to copy from with an alloctype that won't copy is
1505 * most likely a bug */
1506 BLI_assert(!layerdata ||
1507 (alloctype == CD_ASSIGN) ||
1508 (alloctype == CD_DUPLICATE) ||
1509 (alloctype == CD_REFERENCE));
1511 if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1512 return &data->layers[CustomData_get_layer_index(data, type)];
1514 if ((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1515 newlayerdata = layerdata;
1517 else if (size > 0) {
1518 newlayerdata = MEM_callocN(size, layerType_getName(type));
1523 if (alloctype == CD_DUPLICATE && layerdata) {
1525 typeInfo->copy(layerdata, newlayerdata, totelem);
1527 memcpy(newlayerdata, layerdata, size);
1529 else if (alloctype == CD_DEFAULT) {
1530 if (typeInfo->set_default)
1531 typeInfo->set_default((char*)newlayerdata, totelem);
1533 else if (alloctype == CD_REFERENCE)
1534 flag |= CD_FLAG_NOFREE;
1536 if (index >= data->maxlayer) {
1537 if (!customData_resize(data, CUSTOMDATA_GROW)) {
1538 if (newlayerdata != layerdata)
1539 MEM_freeN(newlayerdata);
1546 /* keep layers ordered by type */
1547 for ( ; index > 0 && data->layers[index - 1].type > type; --index)
1548 data->layers[index] = data->layers[index - 1];
1550 data->layers[index].type = type;
1551 data->layers[index].flag = flag;
1552 data->layers[index].data = newlayerdata;
1554 if (name || (name=typeInfo->defaultname)) {
1555 BLI_strncpy(data->layers[index].name, name, sizeof(data->layers[index].name));
1556 CustomData_set_layer_unique_name(data, index);
1559 data->layers[index].name[0] = '\0';
1561 if (index > 0 && data->layers[index-1].type == type) {
1562 data->layers[index].active = data->layers[index-1].active;
1563 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1564 data->layers[index].active_clone = data->layers[index-1].active_clone;
1565 data->layers[index].active_mask = data->layers[index-1].active_mask;
1568 data->layers[index].active = 0;
1569 data->layers[index].active_rnd = 0;
1570 data->layers[index].active_clone = 0;
1571 data->layers[index].active_mask = 0;
1574 customData_update_offsets(data);
1576 return &data->layers[index];
1579 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1580 void *layerdata, int totelem)
1582 CustomDataLayer *layer;
1583 const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1585 layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1586 totelem, typeInfo->defaultname);
1587 CustomData_update_typemap(data);
1595 /*same as above but accepts a name*/
1596 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1597 void *layerdata, int totelem, const char *name)
1599 CustomDataLayer *layer;
1601 layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1603 CustomData_update_typemap(data);
1612 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1616 if (index < 0) return 0;
1618 customData_free_layer__internal(&data->layers[index], totelem);
1620 for (i=index+1; i < data->totlayer; ++i)
1621 data->layers[i-1] = data->layers[i];
1625 /* if layer was last of type in array, set new active layer */
1626 if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1627 i = CustomData_get_layer_index(data, type);
1630 for (; i < data->totlayer && data->layers[i].type == type; i++) {
1631 data->layers[i].active--;
1632 data->layers[i].active_rnd--;
1633 data->layers[i].active_clone--;
1634 data->layers[i].active_mask--;
1638 if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1639 customData_resize(data, -CUSTOMDATA_GROW);
1641 customData_update_offsets(data);
1642 CustomData_update_typemap(data);
1647 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1650 index = CustomData_get_active_layer_index(data, type);
1651 if (index < 0) return 0;
1652 return CustomData_free_layer(data, type, totelem, index);
1656 void CustomData_free_layers(CustomData *data, int type, int totelem)
1658 while (CustomData_has_layer(data, type))
1659 CustomData_free_layer_active(data, type, totelem);
1662 int CustomData_has_layer(const CustomData *data, int type)
1664 return (CustomData_get_layer_index(data, type) != -1);
1667 int CustomData_number_of_layers(const CustomData *data, int type)
1671 for (i = 0; i < data->totlayer; i++)
1672 if (data->layers[i].type == type)
1678 void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem)
1680 CustomDataLayer *layer;
1683 /* get the layer index of the first layer of type */
1684 layer_index = CustomData_get_active_layer_index(data, type);
1685 if (layer_index < 0) return NULL;
1687 layer = &data->layers[layer_index];
1689 if (layer->flag & CD_FLAG_NOFREE) {
1690 /* MEM_dupallocN won't work in case of complex layers, like e.g.
1691 * CD_MDEFORMVERT, which has pointers to allocated data...
1692 * So in case a custom copy function is defined, use it!
1694 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1696 if (typeInfo->copy) {
1697 char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1698 typeInfo->copy(layer->data, dest_data, totelem);
1699 layer->data = dest_data;
1702 layer->data = MEM_dupallocN(layer->data);
1704 layer->flag &= ~CD_FLAG_NOFREE;
1710 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1711 const int type, const char *name, const int totelem)
1713 CustomDataLayer *layer;
1716 /* get the layer index of the desired layer */
1717 layer_index = CustomData_get_named_layer_index(data, type, name);
1718 if (layer_index < 0) return NULL;
1720 layer = &data->layers[layer_index];
1722 if (layer->flag & CD_FLAG_NOFREE) {
1723 /* MEM_dupallocN won't work in case of complex layers, like e.g.
1724 * CD_MDEFORMVERT, which has pointers to allocated data...
1725 * So in case a custom copy function is defined, use it!
1727 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1729 if (typeInfo->copy) {
1730 char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1731 typeInfo->copy(layer->data, dest_data, totelem);
1732 layer->data = dest_data;
1735 layer->data = MEM_dupallocN(layer->data);
1737 layer->flag &= ~CD_FLAG_NOFREE;
1743 int CustomData_is_referenced_layer(struct CustomData *data, int type)
1745 CustomDataLayer *layer;
1748 /* get the layer index of the first layer of type */
1749 layer_index = CustomData_get_active_layer_index(data, type);
1750 if (layer_index < 0) return 0;
1752 layer = &data->layers[layer_index];
1754 return (layer->flag & CD_FLAG_NOFREE) != 0;
1757 void CustomData_free_temporary(CustomData *data, int totelem)
1759 CustomDataLayer *layer;
1762 for (i = 0, j = 0; i < data->totlayer; ++i) {
1763 layer = &data->layers[i];
1766 data->layers[j] = data->layers[i];
1768 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1769 customData_free_layer__internal(layer, totelem);
1776 if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1777 customData_resize(data, -CUSTOMDATA_GROW);
1779 customData_update_offsets(data);
1782 void CustomData_set_only_copy(const struct CustomData *data,
1783 CustomDataMask mask)
1787 for (i = 0; i < data->totlayer; ++i)
1788 if (!(mask & CD_TYPE_AS_MASK(data->layers[i].type)))
1789 data->layers[i].flag |= CD_FLAG_NOCOPY;
1792 void CustomData_copy_elements(int type, void *source, void *dest, int count)
1794 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1797 typeInfo->copy(source, dest, count);
1799 memcpy(dest, source, typeInfo->size*count);
1802 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1803 int source_index, int dest_index, int count)
1805 const LayerTypeInfo *typeInfo;
1810 /* copies a layer at a time */
1812 for (src_i = 0; src_i < source->totlayer; ++src_i) {
1814 /* find the first dest layer with type >= the source type
1815 * (this should work because layers are ordered by type)
1817 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
1821 /* if there are no more dest layers, we're done */
1822 if (dest_i >= dest->totlayer) return;
1824 /* if we found a matching layer, copy the data */
1825 if (dest->layers[dest_i].type == source->layers[src_i].type) {
1826 char *src_data = source->layers[src_i].data;
1827 char *dest_data = dest->layers[dest_i].data;
1829 typeInfo = layerType_getInfo(source->layers[src_i].type);
1831 src_offset = source_index * typeInfo->size;
1832 dest_offset = dest_index * typeInfo->size;
1834 if (!src_data || !dest_data) {
1835 if (src_data != NULL && dest_data != NULL) {
1836 printf("%s: warning null data for %s type (%p --> %p), skipping\n",
1837 __func__, layerType_getName(source->layers[src_i].type),
1838 (void *)src_data, (void *)dest_data);
1844 typeInfo->copy(src_data + src_offset,
1845 dest_data + dest_offset,
1848 memcpy(dest_data + dest_offset,
1849 src_data + src_offset,
1850 count * typeInfo->size);
1852 /* if there are multiple source & dest layers of the same type,
1853 * we don't want to copy all source layers to the same dest, so
1861 void CustomData_free_elem(CustomData *data, int index, int count)
1864 const LayerTypeInfo *typeInfo;
1866 for (i = 0; i < data->totlayer; ++i) {
1867 if (!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1868 typeInfo = layerType_getInfo(data->layers[i].type);
1870 if (typeInfo->free) {
1871 int offset = typeInfo->size * index;
1873 typeInfo->free((char *)data->layers[i].data + offset,
1874 count, typeInfo->size);
1880 #define SOURCE_BUF_SIZE 100
1882 void CustomData_interp(const CustomData *source, CustomData *dest,
1883 int *src_indices, float *weights, float *sub_weights,
1884 int count, int dest_index)
1889 void *source_buf[SOURCE_BUF_SIZE];
1890 void **sources = source_buf;
1892 /* slow fallback in case we're interpolating a ridiculous number of
1895 if (count > SOURCE_BUF_SIZE)
1896 sources = MEM_callocN(sizeof(*sources) * count,
1897 "CustomData_interp sources");
1899 /* interpolates a layer at a time */
1901 for (src_i = 0; src_i < source->totlayer; ++src_i) {
1902 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
1903 if (!typeInfo->interp) continue;
1905 /* find the first dest layer with type >= the source type
1906 * (this should work because layers are ordered by type)
1908 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
1912 /* if there are no more dest layers, we're done */
1913 if (dest_i >= dest->totlayer) return;
1915 /* if we found a matching layer, copy the data */
1916 if (dest->layers[dest_i].type == source->layers[src_i].type) {
1917 void *src_data = source->layers[src_i].data;
1919 for (j = 0; j < count; ++j)
1920 sources[j] = (char *)src_data
1921 + typeInfo->size * src_indices[j];
1923 dest_offset = dest_index * typeInfo->size;
1925 typeInfo->interp(sources, weights, sub_weights, count,
1926 (char *)dest->layers[dest_i].data + dest_offset);
1928 /* if there are multiple source & dest layers of the same type,
1929 * we don't want to copy all source layers to the same dest, so
1936 if (count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1939 void CustomData_swap(struct CustomData *data, int index, const int *corner_indices)
1941 const LayerTypeInfo *typeInfo;
1944 for (i = 0; i < data->totlayer; ++i) {
1945 typeInfo = layerType_getInfo(data->layers[i].type);
1947 if (typeInfo->swap) {
1948 int offset = typeInfo->size * index;
1950 typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
1955 void *CustomData_get(const CustomData *data, int index, int type)
1960 /* get the layer index of the active layer of type */
1961 layer_index = CustomData_get_active_layer_index(data, type);
1962 if (layer_index < 0) return NULL;
1964 /* get the offset of the desired element */
1965 offset = layerType_getInfo(type)->size * index;
1967 return (char *)data->layers[layer_index].data + offset;
1970 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
1975 /* get the layer index of the first layer of type */
1976 layer_index = data->typemap[type];
1977 if (layer_index < 0) return NULL;
1979 offset = layerType_getInfo(type)->size * index;
1980 return (char *)data->layers[layer_index+n].data + offset;
1983 void *CustomData_get_layer(const CustomData *data, int type)
1985 /* get the layer index of the active layer of type */
1986 int layer_index = CustomData_get_active_layer_index(data, type);
1987 if (layer_index < 0) return NULL;
1989 return data->layers[layer_index].data;
1992 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
1994 /* get the layer index of the active layer of type */
1995 int layer_index = CustomData_get_layer_index_n(data, type, n);
1996 if (layer_index < 0) return NULL;
1998 return data->layers[layer_index].data;
2001 void *CustomData_get_layer_named(const struct CustomData *data, int type,
2004 int layer_index = CustomData_get_named_layer_index(data, type, name);
2005 if (layer_index < 0) return NULL;
2007 return data->layers[layer_index].data;
2011 int CustomData_set_layer_name(const CustomData *data, int type, int n, const char *name)
2013 /* get the layer index of the first layer of type */
2014 int layer_index = CustomData_get_layer_index_n(data, type, n);
2016 if (layer_index < 0) return 0;
2017 if (!name) return 0;
2019 strcpy(data->layers[layer_index].name, name);
2024 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
2026 /* get the layer index of the first layer of type */
2027 int layer_index = CustomData_get_active_layer_index(data, type);
2029 if (layer_index < 0) return NULL;
2031 data->layers[layer_index].data = ptr;
2036 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
2038 /* get the layer index of the first layer of type */
2039 int layer_index = CustomData_get_layer_index_n(data, type, n);
2040 if (layer_index < 0) return NULL;
2042 data->layers[layer_index].data = ptr;
2047 void CustomData_set(const CustomData *data, int index, int type, void *source)
2049 void *dest = CustomData_get(data, index, type);
2050 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2055 typeInfo->copy(source, dest, 1);
2057 memcpy(dest, source, typeInfo->size);
2061 /*needed to convert to/from different face reps*/
2062 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata,
2063 int totloop, int totpoly)
2066 for (i=0; i < fdata->totlayer; i++) {
2067 if (fdata->layers[i].type == CD_MTFACE) {
2068 CustomData_add_layer_named(pdata, CD_MTEXPOLY, CD_CALLOC, NULL, totpoly, fdata->layers[i].name);
2069 CustomData_add_layer_named(ldata, CD_MLOOPUV, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2071 else if (fdata->layers[i].type == CD_MCOL) {
2072 CustomData_add_layer_named(ldata, CD_MLOOPCOL, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2074 else if (fdata->layers[i].type == CD_MDISPS) {
2075 CustomData_add_layer_named(ldata, CD_MDISPS, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2080 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total)
2083 for (i=0; i < pdata->totlayer; i++) {
2084 if (pdata->layers[i].type == CD_MTEXPOLY) {
2085 CustomData_add_layer_named(fdata, CD_MTFACE, CD_CALLOC, NULL, total, pdata->layers[i].name);
2088 for (i=0; i < ldata->totlayer; i++) {
2089 if (ldata->layers[i].type == CD_MLOOPCOL) {
2090 CustomData_add_layer_named(fdata, CD_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2092 else if (ldata->layers[i].type == CD_PREVIEW_MLOOPCOL) {
2093 CustomData_add_layer_named(fdata, CD_PREVIEW_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2095 else if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) {
2096 CustomData_add_layer_named(fdata, CD_ORIGSPACE, CD_CALLOC, NULL, total, ldata->layers[i].name);
2100 CustomData_bmesh_update_active_layers(fdata, pdata, ldata);
2103 void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *pdata, CustomData *ldata)
2107 if (CustomData_has_layer(pdata, CD_MTEXPOLY)) {
2108 act = CustomData_get_active_layer(pdata, CD_MTEXPOLY);
2109 CustomData_set_layer_active(ldata, CD_MLOOPUV, act);
2110 CustomData_set_layer_active(fdata, CD_MTFACE, act);
2112 act = CustomData_get_render_layer(pdata, CD_MTEXPOLY);
2113 CustomData_set_layer_render(ldata, CD_MLOOPUV, act);
2114 CustomData_set_layer_render(fdata, CD_MTFACE, act);
2116 act = CustomData_get_clone_layer(pdata, CD_MTEXPOLY);
2117 CustomData_set_layer_clone(ldata, CD_MLOOPUV, act);
2118 CustomData_set_layer_clone(fdata, CD_MTFACE, act);
2120 act = CustomData_get_stencil_layer(pdata, CD_MTEXPOLY);
2121 CustomData_set_layer_stencil(ldata, CD_MLOOPUV, act);
2122 CustomData_set_layer_stencil(fdata, CD_MTFACE, act);
2125 if (CustomData_has_layer(ldata, CD_MLOOPCOL)) {
2126 act = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
2127 CustomData_set_layer_active(fdata, CD_MCOL, act);
2129 act = CustomData_get_render_layer(ldata, CD_MLOOPCOL);
2130 CustomData_set_layer_render(fdata, CD_MCOL, act);
2132 act = CustomData_get_clone_layer(ldata, CD_MLOOPCOL);
2133 CustomData_set_layer_clone(fdata, CD_MCOL, act);
2135 act = CustomData_get_stencil_layer(ldata, CD_MLOOPCOL);
2136 CustomData_set_layer_stencil(fdata, CD_MCOL, act);
2140 void CustomData_bmesh_init_pool(CustomData *data, int totelem, const char htype)
2144 /* Dispose old pools before calling here to avoid leaks */
2145 BLI_assert(data->pool == NULL);
2148 case BM_VERT: chunksize = bm_mesh_chunksize_default.totvert; break;
2149 case BM_EDGE: chunksize = bm_mesh_chunksize_default.totedge; break;
2150 case BM_LOOP: chunksize = bm_mesh_chunksize_default.totloop; break;
2151 case BM_FACE: chunksize = bm_mesh_chunksize_default.totface; break;
2158 /* If there are no layers, no pool is needed just yet */
2159 if (data->totlayer) {
2160 data->pool = BLI_mempool_create(data->totsize, totelem, chunksize, BLI_MEMPOOL_SYSMALLOC);
2164 void CustomData_bmesh_merge(CustomData *source, CustomData *dest,
2165 CustomDataMask mask, int alloctype, BMesh *bm, const char htype)
2173 /* copy old layer description so that old data can be copied into
2174 * the new allocation */
2176 if (destold.layers) destold.layers = MEM_dupallocN(destold.layers);
2178 CustomData_merge(source, dest, mask, alloctype, 0);
2180 CustomData_bmesh_init_pool(dest, 512, htype);
2184 t = BM_VERTS_OF_MESH; break;
2186 t = BM_EDGES_OF_MESH; break;
2188 t = BM_LOOPS_OF_FACE; break;
2190 t = BM_FACES_OF_MESH; break;
2191 default: /* should never happen */
2192 BLI_assert(!"invalid type given");
2193 t = BM_VERTS_OF_MESH;
2196 if (t != BM_LOOPS_OF_FACE) {
2197 /*ensure all current elements follow new customdata layout*/
2198 BM_ITER_MESH (h, &iter, bm, t) {
2200 CustomData_bmesh_copy_data(&destold, dest, h->data, &tmp);
2201 CustomData_bmesh_free_block(&destold, &h->data);
2210 /*ensure all current elements follow new customdata layout*/
2211 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
2212 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2214 CustomData_bmesh_copy_data(&destold, dest, l->head.data, &tmp);
2215 CustomData_bmesh_free_block(&destold, &l->head.data);
2221 if (destold.pool) BLI_mempool_destroy(destold.pool);
2222 if (destold.layers) MEM_freeN(destold.layers);
2225 void CustomData_bmesh_free_block(CustomData *data, void **block)
2227 const LayerTypeInfo *typeInfo;
2230 if (!*block) return;
2231 for (i = 0; i < data->totlayer; ++i) {
2232 if (!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2233 typeInfo = layerType_getInfo(data->layers[i].type);
2235 if (typeInfo->free) {
2236 int offset = data->layers[i].offset;
2237 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2243 BLI_mempool_free(data->pool, *block);
2248 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
2252 CustomData_bmesh_free_block(data, block);
2254 if (data->totsize > 0)
2255 *block = BLI_mempool_alloc(data->pool);
2260 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
2261 void *src_block, void **dest_block)
2263 const LayerTypeInfo *typeInfo;
2267 CustomData_bmesh_alloc_block(dest, dest_block);
2269 memset(*dest_block, 0, dest->totsize);
2272 /* copies a layer at a time */
2274 for (src_i = 0; src_i < source->totlayer; ++src_i) {
2276 /* find the first dest layer with type >= the source type
2277 * (this should work because layers are ordered by type)
2279 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
2283 /* if there are no more dest layers, we're done */
2284 if (dest_i >= dest->totlayer) return;
2286 /* if we found a matching layer, copy the data */
2287 if (dest->layers[dest_i].type == source->layers[src_i].type &&
2288 strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2289 char *src_data = (char*)src_block + source->layers[src_i].offset;
2290 char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2292 typeInfo = layerType_getInfo(source->layers[src_i].type);
2295 typeInfo->copy(src_data, dest_data, 1);
2297 memcpy(dest_data, src_data, typeInfo->size);
2299 /* if there are multiple source & dest layers of the same type,
2300 * we don't want to copy all source layers to the same dest, so
2308 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
2309 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
2313 /* get the layer index of the first layer of type */
2314 layer_index = CustomData_get_active_layer_index(data, type);
2315 if (layer_index < 0) return NULL;
2317 return (char *)block + data->layers[layer_index].offset;
2320 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
2324 /* get the layer index of the first layer of type */
2325 layer_index = CustomData_get_layer_index(data, type);
2326 if (layer_index < 0) return NULL;
2328 return (char *)block + data->layers[layer_index+n].offset;
2331 /*gets from the layer at physical index n, note: doesn't check type.*/
2332 void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
2334 if (n < 0 || n >= data->totlayer) return NULL;
2336 return (char *)block + data->layers[n].offset;
2339 int CustomData_layer_has_math(struct CustomData *data, int layer_n)
2341 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[layer_n].type);
2343 if (typeInfo->equal && typeInfo->add && typeInfo->multiply &&
2344 typeInfo->initminmax && typeInfo->dominmax)
2352 /* copies the "value" (e.g. mloopuv uv or mloopcol colors) from one block to
2353 * another, while not overwriting anything else (e.g. flags)*/
2354 void CustomData_data_copy_value(int type, void *source, void *dest)
2356 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2360 if (typeInfo->copyvalue)
2361 typeInfo->copyvalue(source, dest);
2363 memcpy(dest, source, typeInfo->size);
2366 int CustomData_data_equals(int type, void *data1, void *data2)
2368 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2370 if (typeInfo->equal)
2371 return typeInfo->equal(data1, data2);
2372 else return !memcmp(data1, data2, typeInfo->size);
2375 void CustomData_data_initminmax(int type, void *min, void *max)
2377 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2379 if (typeInfo->initminmax)
2380 typeInfo->initminmax(min, max);
2384 void CustomData_data_dominmax(int type, void *data, void *min, void *max)
2386 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2388 if (typeInfo->dominmax)
2389 typeInfo->dominmax(data, min, max);
2393 void CustomData_data_multiply(int type, void *data, float fac)
2395 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2397 if (typeInfo->multiply)
2398 typeInfo->multiply(data, fac);
2402 void CustomData_data_add(int type, void *data1, void *data2)
2404 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2407 typeInfo->add(data1, data2);
2410 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
2412 void *dest = CustomData_bmesh_get(data, block, type);
2413 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2418 typeInfo->copy(source, dest, 1);
2420 memcpy(dest, source, typeInfo->size);
2423 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
2425 void *dest = CustomData_bmesh_get_n(data, block, type, n);
2426 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2431 typeInfo->copy(source, dest, 1);
2433 memcpy(dest, source, typeInfo->size);
2436 void CustomData_bmesh_set_layer_n(CustomData *data, void *block, int n, void *source)
2438 void *dest = CustomData_bmesh_get_layer_n(data, block, n);
2439 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[n].type);
2444 typeInfo->copy(source, dest, 1);
2446 memcpy(dest, source, typeInfo->size);
2449 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
2450 float *sub_weights, int count, void *dest_block)
2453 void *source_buf[SOURCE_BUF_SIZE];
2454 void **sources = source_buf;
2456 /* slow fallback in case we're interpolating a ridiculous number of
2459 if (count > SOURCE_BUF_SIZE)
2460 sources = MEM_callocN(sizeof(*sources) * count,
2461 "CustomData_interp sources");
2463 /* interpolates a layer at a time */
2464 for (i = 0; i < data->totlayer; ++i) {
2465 CustomDataLayer *layer = &data->layers[i];
2466 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2467 if (typeInfo->interp) {
2468 for (j = 0; j < count; ++j)
2469 sources[j] = (char *)src_blocks[j] + layer->offset;
2471 typeInfo->interp(sources, weights, sub_weights, count,
2472 (char *)dest_block + layer->offset);
2476 if (count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2479 void CustomData_bmesh_set_default(CustomData *data, void **block)
2481 const LayerTypeInfo *typeInfo;
2485 CustomData_bmesh_alloc_block(data, block);
2487 for (i = 0; i < data->totlayer; ++i) {
2488 int offset = data->layers[i].offset;
2490 typeInfo = layerType_getInfo(data->layers[i].type);
2492 if (typeInfo->set_default)
2493 typeInfo->set_default((char*)*block + offset, 1);
2494 else memset((char*)*block + offset, 0, typeInfo->size);
2498 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2499 int src_index, void **dest_block)
2501 const LayerTypeInfo *typeInfo;
2502 int dest_i, src_i, src_offset;
2505 CustomData_bmesh_alloc_block(dest, dest_block);
2507 /* copies a layer at a time */
2509 for (src_i = 0; src_i < source->totlayer; ++src_i) {
2511 /* find the first dest layer with type >= the source type
2512 * (this should work because layers are ordered by type)
2514 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
2518 /* if there are no more dest layers, we're done */
2519 if (dest_i >= dest->totlayer) return;
2521 /* if we found a matching layer, copy the data */
2522 if (dest->layers[dest_i].type == source->layers[src_i].type) {
2523 int offset = dest->layers[dest_i].offset;
2524 char *src_data = source->layers[src_i].data;
2525 char *dest_data = (char*)*dest_block + offset;
2527 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2528 src_offset = src_index * typeInfo->size;
2531 typeInfo->copy(src_data + src_offset, dest_data, 1);
2533 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2535 /* if there are multiple source & dest layers of the same type,
2536 * we don't want to copy all source layers to the same dest, so
2544 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2545 void *src_block, int dest_index)
2547 const LayerTypeInfo *typeInfo;
2548 int dest_i, src_i, dest_offset;
2550 /* copies a layer at a time */
2552 for (src_i = 0; src_i < source->totlayer; ++src_i) {
2554 /* find the first dest layer with type >= the source type
2555 * (this should work because layers are ordered by type)
2557 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
2561 /* if there are no more dest layers, we're done */
2562 if (dest_i >= dest->totlayer) return;
2564 /* if we found a matching layer, copy the data */
2565 if (dest->layers[dest_i].type == source->layers[src_i].type) {
2566 int offset = source->layers[src_i].offset;
2567 char *src_data = (char*)src_block + offset;
2568 char *dest_data = dest->layers[dest_i].data;
2570 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2571 dest_offset = dest_index * typeInfo->size;
2574 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2576 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2578 /* if there are multiple source & dest layers of the same type,
2579 * we don't want to copy all source layers to the same dest, so
2588 void CustomData_file_write_info(int type, const char **structname, int *structnum)
2590 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2592 *structname = typeInfo->structname;
2593 *structnum = typeInfo->structnum;
2596 int CustomData_sizeof(int type)
2598 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2600 return typeInfo->size;
2603 const char *CustomData_layertype_name(int type)
2605 return layerType_getName(type);
2610 * Can only ever be one of these.
2612 int CustomData_layertype_is_singleton(int type)
2614 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2615 return typeInfo->defaultname != NULL;
2618 static int CustomData_is_property_layer(int type)
2620 if ((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2625 static int cd_layer_find_dupe(CustomData *data, const char *name, int type, int index)
2628 /* see if there is a duplicate */
2629 for (i=0; i<data->totlayer; i++) {
2631 CustomDataLayer *layer= &data->layers[i];
2633 if (CustomData_is_property_layer(type)) {
2634 if (CustomData_is_property_layer(layer->type) && strcmp(layer->name, name)==0) {
2639 if (i!=index && layer->type==type && strcmp(layer->name, name)==0) {
2649 static int customdata_unique_check(void *arg, const char *name)
2651 struct {CustomData *data; int type; int index;} *data_arg= arg;
2652 return cd_layer_find_dupe(data_arg->data, name, data_arg->type, data_arg->index);
2655 void CustomData_set_layer_unique_name(CustomData *data, int index)
2657 CustomDataLayer *nlayer= &data->layers[index];
2658 const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2660 struct {CustomData *data; int type; int index;} data_arg;
2661 data_arg.data= data;
2662 data_arg.type= nlayer->type;
2663 data_arg.index= index;
2665 if (!typeInfo->defaultname)
2668 BLI_uniquename_cb(customdata_unique_check, &data_arg, typeInfo->defaultname, '.', nlayer->name, sizeof(nlayer->name));
2671 void CustomData_validate_layer_name(const CustomData *data, int type, char *name, char *outname)
2675 /* if a layer name was given, try to find that layer */
2677 index = CustomData_get_named_layer_index(data, type, name);
2680 /* either no layer was specified, or the layer we want has been
2681 * deleted, so assign the active layer to name
2683 index = CustomData_get_active_layer_index(data, type);
2684 strcpy(outname, data->layers[index].name);
2687 strcpy(outname, name);
2690 int CustomData_verify_versions(struct CustomData *data, int index)
2692 const LayerTypeInfo *typeInfo;
2693 CustomDataLayer *layer = &data->layers[index];
2694 int i, keeplayer = 1;
2696 if (layer->type >= CD_NUMTYPES) {
2697 keeplayer = 0; /* unknown layer type from future version */
2700 typeInfo = layerType_getInfo(layer->type);
2702 if (!typeInfo->defaultname && (index > 0) &&
2703 data->layers[index-1].type == layer->type)
2705 keeplayer = 0; /* multiple layers of which we only support one */
2710 for (i=index+1; i < data->totlayer; ++i)
2711 data->layers[i-1] = data->layers[i];
2718 /****************************** External Files *******************************/
2720 static void customdata_external_filename(char filename[FILE_MAX], ID *id, CustomDataExternal *external)
2722 BLI_strncpy(filename, external->filename, FILE_MAX);
2723 BLI_path_abs(filename, ID_BLEND_PATH(G.main, id));
2726 void CustomData_external_reload(CustomData *data, ID *UNUSED(id), CustomDataMask mask, int totelem)
2728 CustomDataLayer *layer;
2729 const LayerTypeInfo *typeInfo;
2732 for (i=0; i<data->totlayer; i++) {
2733 layer = &data->layers[i];
2734 typeInfo = layerType_getInfo(layer->type);
2736 if (!(mask & CD_TYPE_AS_MASK(layer->type)));
2737 else if ((layer->flag & CD_FLAG_EXTERNAL) && (layer->flag & CD_FLAG_IN_MEMORY)) {
2739 typeInfo->free(layer->data, totelem, typeInfo->size);
2740 layer->flag &= ~CD_FLAG_IN_MEMORY;
2745 void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem)
2747 CustomDataExternal *external= data->external;
2748 CustomDataLayer *layer;
2750 CDataFileLayer *blay;
2751 char filename[FILE_MAX];
2752 const LayerTypeInfo *typeInfo;
2758 for (i=0; i<data->totlayer; i++) {
2759 layer = &data->layers[i];
2760 typeInfo = layerType_getInfo(layer->type);
2762 if (!(mask & CD_TYPE_AS_MASK(layer->type)));
2763 else if (layer->flag & CD_FLAG_IN_MEMORY);
2764 else if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read)
2771 customdata_external_filename(filename, id, external);
2773 cdf= cdf_create(CDF_TYPE_MESH);
2774 if (!cdf_read_open(cdf, filename)) {
2775 fprintf(stderr, "Failed to read %s layer from %s.\n", layerType_getName(layer->type), filename);
2779 for (i=0; i<data->totlayer; i++) {
2780 layer = &data->layers[i];
2781 typeInfo = layerType_getInfo(layer->type);
2783 if (!(mask & CD_TYPE_AS_MASK(layer->type)));
2784 else if (layer->flag & CD_FLAG_IN_MEMORY);
2785 else if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
2786 blay= cdf_layer_find(cdf, layer->type, layer->name);
2789 if (cdf_read_layer(cdf, blay)) {
2790 if (typeInfo->read(cdf, layer->data, totelem));
2792 layer->flag |= CD_FLAG_IN_MEMORY;
2800 cdf_read_close(cdf);
2804 void CustomData_external_write(CustomData *data, ID *id, CustomDataMask mask, int totelem, int free)
2806 CustomDataExternal *external= data->external;
2807 CustomDataLayer *layer;
2809 CDataFileLayer *blay;
2810 const LayerTypeInfo *typeInfo;
2812 char filename[FILE_MAX];
2817 /* test if there is anything to write */
2818 for (i=0; i<data->totlayer; i++) {
2819 layer = &data->layers[i];
2820 typeInfo = layerType_getInfo(layer->type);
2822 if (!(mask & CD_TYPE_AS_MASK(layer->type)));
2823 else if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write)
2830 /* make sure data is read before we try to write */
2831 CustomData_external_read(data, id, mask, totelem);
2832 customdata_external_filename(filename, id, external);
2834 cdf= cdf_create(CDF_TYPE_MESH);
2836 for (i=0; i<data->totlayer; i++) {
2837 layer = &data->layers[i];
2838 typeInfo = layerType_getInfo(layer->type);
2840 if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) {
2841 if (layer->flag & CD_FLAG_IN_MEMORY) {
2842 cdf_layer_add(cdf, layer->type, layer->name,
2843 typeInfo->filesize(cdf, layer->data, totelem));
2847 return; /* read failed for a layer! */
2852 if (!cdf_write_open(cdf, filename)) {
2853 fprintf(stderr, "Failed to open %s for writing.\n", filename);
2857 for (i=0; i<data->totlayer; i++) {
2858 layer = &data->layers[i];
2859 typeInfo = layerType_getInfo(layer->type);
2861 if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
2862 blay= cdf_layer_find(cdf, layer->type, layer->name);
2864 if (cdf_write_layer(cdf, blay)) {
2865 if (typeInfo->write(cdf, layer->data, totelem));
2873 if (i != data->totlayer) {
2874 fprintf(stderr, "Failed to write data to %s.\n", filename);
2879 for (i=0; i<data->totlayer; i++) {
2880 layer = &data->layers[i];
2881 typeInfo = layerType_getInfo(layer->type);
2883 if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
2886 typeInfo->free(layer->data, totelem, typeInfo->size);
2887 layer->flag &= ~CD_FLAG_IN_MEMORY;
2892 cdf_write_close(cdf);
2896 void CustomData_external_add(CustomData *data, ID *UNUSED(id), int type, int UNUSED(totelem), const char *filename)
2898 CustomDataExternal *external= data->external;
2899 CustomDataLayer *layer;
2902 layer_index = CustomData_get_active_layer_index(data, type);
2903 if (layer_index < 0) return;
2905 layer = &data->layers[layer_index];
2907 if (layer->flag & CD_FLAG_EXTERNAL)
2911 external= MEM_callocN(sizeof(CustomDataExternal), "CustomDataExternal");
2912 data->external= external;
2914 BLI_strncpy(external->filename, filename, sizeof(external->filename));
2916 layer->flag |= CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY;
2919 void CustomData_external_remove(CustomData *data, ID *id, int type, int totelem)
2921 CustomDataExternal *external= data->external;
2922 CustomDataLayer *layer;
2923 //char filename[FILE_MAX];
2924 int layer_index; // i, remove_file;
2926 layer_index = CustomData_get_active_layer_index(data, type);
2927 if (layer_index < 0) return;
2929 layer = &data->layers[layer_index];
2934 if (layer->flag & CD_FLAG_EXTERNAL) {
2935 if (!(layer->flag & CD_FLAG_IN_MEMORY))
2936 CustomData_external_read(data, id, CD_TYPE_AS_MASK(layer->type), totelem);
2938 layer->flag &= ~CD_FLAG_EXTERNAL;
2942 for (i=0; i<data->totlayer; i++)
2943 if (data->layers[i].flag & CD_FLAG_EXTERNAL)
2947 customdata_external_filename(filename, id, external);
2948 cdf_remove(filename);
2949 CustomData_external_free(data);
2955 int CustomData_external_test(CustomData *data, int type)
2957 CustomDataLayer *layer;
2960 layer_index = CustomData_get_active_layer_index(data, type);
2961 if (layer_index < 0) return 0;
2963 layer = &data->layers[layer_index];
2964 return (layer->flag & CD_FLAG_EXTERNAL);
2968 void CustomData_external_remove_object(CustomData *data, ID *id)
2970 CustomDataExternal *external= data->external;
2971 char filename[FILE_MAX];
2976 customdata_external_filename(filename, id, external);
2977 cdf_remove(filename);
2978 CustomData_external_free(data);