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_customdata.h"
55 #include "BKE_customdata_file.h"
56 #include "BKE_global.h"
58 #include "BKE_multires.h"
65 /* number of layers to add when growing a CustomData object */
66 #define CUSTOMDATA_GROW 5
68 /********************* Layer type information **********************/
69 typedef struct LayerTypeInfo {
70 int size; /* the memory size of one element of this layer's data */
71 const char *structname; /* name of the struct used, for file writing */
72 int structnum; /* number of structs per element, for file writing */
73 const char *defaultname; /* default layer name */
75 /* a function to copy count elements of this layer's data
76 * (deep copy if appropriate)
77 * if NULL, memcpy is used
79 void (*copy)(const void *source, void *dest, int count);
81 /* a function to free any dynamically allocated components of this
82 * layer's data (note the data pointer itself should not be freed)
83 * size should be the size of one element of this layer's data (e.g.
86 void (*free)(void *data, int count, int size);
88 /* a function to interpolate between count source elements of this
89 * layer's data and store the result in dest
90 * if weights == NULL or sub_weights == NULL, they should default to 1
92 * weights gives the weight for each element in sources
93 * sub_weights gives the sub-element weights for each element in sources
94 * (there should be (sub element count)^2 weights per element)
95 * count gives the number of elements in sources
97 void (*interp)(void **sources, float *weights, float *sub_weights,
98 int count, void *dest);
100 /* a function to swap the data in corners of the element */
101 void (*swap)(void *data, const int *corner_indices);
103 /* a function to set a layer's data to default values. if NULL, the
104 default is assumed to be all zeros */
105 void (*set_default)(void *data, int count);
107 /* functions necassary for geometry collapse*/
108 int (*equal)(void *data1, void *data2);
109 void (*multiply)(void *data, float fac);
110 void (*initminmax)(void *min, void *max);
111 void (*add)(void *data1, void *data2);
112 void (*dominmax)(void *data1, void *min, void *max);
113 void (*copyvalue)(void *source, void *dest);
115 /* a function to read data from a cdf file */
116 int (*read)(CDataFile *cdf, void *data, int count);
118 /* a function to write data to a cdf file */
119 int (*write)(CDataFile *cdf, void *data, int count);
121 /* a function to determine file size */
122 size_t (*filesize)(CDataFile *cdf, void *data, int count);
124 /* a function to validate layer contents depending on
127 void (*validate)(void *source, int sub_elements);
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 static void linklist_free_simple(void *link)
172 static void layerInterp_mdeformvert(void **sources, float *weights,
173 float *UNUSED(sub_weights), int count, void *dest)
175 MDeformVert *dvert = dest;
176 LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
180 if(count <= 0) return;
182 /* build a list of unique def_nrs for dest */
184 for(i = 0; i < count; ++i) {
185 MDeformVert *source = sources[i];
186 float interp_weight = weights ? weights[i] : 1.0f;
188 for(j = 0; j < source->totweight; ++j) {
189 MDeformWeight *dw = &source->dw[j];
191 for(node = dest_dw; node; node = node->next) {
192 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
194 if(tmp_dw->def_nr == dw->def_nr) {
195 tmp_dw->weight += dw->weight * interp_weight;
200 /* if this def_nr is not in the list, add it */
202 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
203 "layerInterp_mdeformvert tmp_dw");
204 tmp_dw->def_nr = dw->def_nr;
205 tmp_dw->weight = dw->weight * interp_weight;
206 BLI_linklist_prepend(&dest_dw, tmp_dw);
212 /* now we know how many unique deform weights there are, so realloc */
213 if(dvert->dw) MEM_freeN(dvert->dw);
216 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
217 "layerInterp_mdeformvert dvert->dw");
218 dvert->totweight = totweight;
220 for(i = 0, node = dest_dw; node; node = node->next, ++i)
221 dvert->dw[i] = *((MDeformWeight *)node->link);
224 memset(dvert, 0, sizeof(*dvert));
226 BLI_linklist_free(dest_dw, linklist_free_simple);
230 static void layerInterp_msticky(void **sources, float *weights,
231 float *UNUSED(sub_weights), int count, void *dest)
237 co[0] = co[1] = 0.0f;
238 for(i = 0; i < count; i++) {
239 w = weights ? weights[i] : 1.0f;
240 mst = (MSticky*)sources[i];
242 madd_v2_v2fl(co, mst->co, w);
245 mst = (MSticky*)dest;
246 copy_v2_v2(mst->co, co);
250 static void layerCopy_tface(const void *source, void *dest, int count)
252 const MTFace *source_tf = (const MTFace*)source;
253 MTFace *dest_tf = (MTFace*)dest;
256 for(i = 0; i < count; ++i)
257 dest_tf[i] = source_tf[i];
260 static void layerInterp_tface(void **sources, float *weights,
261 float *sub_weights, int count, void *dest)
265 float uv[4][2] = {{0.0f}};
268 if(count <= 0) return;
270 sub_weight = sub_weights;
271 for(i = 0; i < count; ++i) {
272 float weight = weights ? weights[i] : 1;
273 MTFace *src = sources[i];
275 for(j = 0; j < 4; ++j) {
277 for(k = 0; k < 4; ++k, ++sub_weight) {
278 madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
282 madd_v2_v2fl(uv[j], src->uv[j], weight);
287 *tf = *(MTFace *)(*sources);
288 memcpy(tf->uv, uv, sizeof(tf->uv));
291 static void layerSwap_tface(void *data, const int *corner_indices)
295 static const short pin_flags[4] =
296 { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
297 static const char sel_flags[4] =
298 { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
299 short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
300 char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
303 for(j = 0; j < 4; ++j) {
304 const int source_index = corner_indices[j];
306 copy_v2_v2(uv[j], tf->uv[source_index]);
308 // swap pinning flags around
309 if(tf->unwrap & pin_flags[source_index]) {
310 unwrap |= pin_flags[j];
313 // swap selection flags around
314 if(tf->flag & sel_flags[source_index]) {
315 flag |= sel_flags[j];
319 memcpy(tf->uv, uv, sizeof(tf->uv));
324 static void layerDefault_tface(void *data, int count)
326 static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
327 0, 0, TF_DYNAMIC|TF_CONVERTED, 0, 0};
328 MTFace *tf = (MTFace*)data;
331 for(i = 0; i < count; i++)
335 static void layerCopy_propFloat(const void *source, void *dest,
338 memcpy(dest, source, sizeof(MFloatProperty)*count);
341 static void layerCopy_propInt(const void *source, void *dest,
344 memcpy(dest, source, sizeof(MIntProperty)*count);
347 static void layerCopy_propString(const void *source, void *dest,
350 memcpy(dest, source, sizeof(MStringProperty)*count);
353 static void layerCopy_origspace_face(const void *source, void *dest, int count)
355 const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
356 OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
359 for(i = 0; i < count; ++i)
360 dest_tf[i] = source_tf[i];
363 static void layerInterp_origspace_face(void **sources, float *weights,
364 float *sub_weights, int count, void *dest)
366 OrigSpaceFace *osf = dest;
368 float uv[4][2] = {{0.0f}};
371 if(count <= 0) return;
373 sub_weight = sub_weights;
374 for(i = 0; i < count; ++i) {
375 float weight = weights ? weights[i] : 1;
376 OrigSpaceFace *src = sources[i];
378 for(j = 0; j < 4; ++j) {
380 for(k = 0; k < 4; ++k, ++sub_weight) {
381 madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
384 madd_v2_v2fl(uv[j], src->uv[j], weight);
389 #if 0 /* no need, this ONLY contains UV's */
390 *osf = *(OrigSpaceFace *)(*sources);
392 memcpy(osf->uv, uv, sizeof(osf->uv));
395 static void layerSwap_origspace_face(void *data, const int *corner_indices)
397 OrigSpaceFace *osf = data;
401 for(j = 0; j < 4; ++j) {
402 copy_v2_v2(uv[j], osf->uv[corner_indices[j]]);
404 memcpy(osf->uv, uv, sizeof(osf->uv));
407 static void layerDefault_origspace_face(void *data, int count)
409 static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
410 OrigSpaceFace *osf = (OrigSpaceFace*)data;
413 for(i = 0; i < count; i++)
414 osf[i] = default_osf;
417 static void layerSwap_mdisps(void *data, const int *ci)
420 float (*d)[3] = NULL;
421 int corners, cornersize, S;
424 int nverts= (ci[1] == 3) ? 4 : 3; /* silly way to know vertex count of face */
425 corners= multires_mdisp_corners(s);
426 cornersize= s->totdisp/corners;
428 if(corners!=nverts) {
429 /* happens when face changed vertex count in edit mode
430 if it happened, just forgot displacement */
433 s->totdisp= (s->totdisp/corners)*nverts;
434 s->disps= MEM_callocN(s->totdisp*sizeof(float)*3, "mdisp swap");
438 d= MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
440 for(S = 0; S < corners; S++)
441 memcpy(d + cornersize*S, s->disps + cornersize*ci[S], cornersize*3*sizeof(float));
448 #if 1 /* BMESH_TODO: place holder function, dont actually interp */
449 static void layerInterp_mdisps(void **sources, float *UNUSED(weights),
450 float *UNUSED(sub_weights), int UNUSED(count), void *dest)
454 /* happens when flipping normals of newly created mesh */
456 d->totdisp = ((MDisps*)sources[0])->totdisp;
459 if (!d->disps && d->totdisp)
460 d->disps = MEM_callocN(sizeof(float)*3*d->totdisp, "blank mdisps in layerInterp_mdisps");
465 static void layerInterp_mdisps(void **sources, float *UNUSED(weights),
466 float *sub_weights, int count, void *dest)
472 int side, S, dst_corners, src_corners;
473 float crn_weight[4][2];
474 float (*sw)[4] = (void*)sub_weights;
475 float (*disps)[3], (*out)[3];
477 /* happens when flipping normals of newly created mesh */
482 dst_corners = multires_mdisp_corners(d);
483 src_corners = multires_mdisp_corners(s);
485 if(sub_weights && count == 2 && src_corners == 3) {
486 src_corners = multires_mdisp_corners(sources[1]);
488 /* special case -- converting two triangles to quad */
489 if(src_corners == 3 && dst_corners == 4) {
493 for(i = 0; i < 2; i++)
494 for(y = 0; y < 4; y++)
495 for(x = 0; x < 4; x++)
499 for(i = 0; i < 2; i++) {
500 float sw_m4[4][4] = {{0}};
501 int a = 7 & ~(1 << vindex[i*2] | 1 << vindex[i*2+1]);
503 sw_m4[0][vindex[i*2+1]] = 1;
504 sw_m4[1][vindex[i*2]] = 1;
506 for(x = 0; x < 3; x++)
510 tris[i] = *((MDisps*)sources[i]);
511 tris[i].disps = MEM_dupallocN(tris[i].disps);
512 layerInterp_mdisps(&sources[i], NULL, (float*)sw_m4, 1, &tris[i]);
515 mdisp_join_tris(d, &tris[0], &tris[1]);
517 for(i = 0; i < 2; i++)
518 MEM_freeN(tris[i].disps);
524 /* For now, some restrictions on the input */
525 if(count != 1 || !sub_weights) {
526 for(i = 0; i < d->totdisp; ++i)
527 zero_v3(d->disps[i]);
532 /* Initialize the destination */
533 disps = MEM_callocN(3*d->totdisp*sizeof(float), "iterp disps");
535 side = sqrt(d->totdisp / dst_corners);
539 sw= (void*)sub_weights;
540 for(i = 0; i < 4; ++i) {
541 crn_weight[i][0] = 0 * sw[i][0] + stl * sw[i][1] + stl * sw[i][2] + 0 * sw[i][3];
542 crn_weight[i][1] = 0 * sw[i][0] + 0 * sw[i][1] + stl * sw[i][2] + stl * sw[i][3];
545 multires_mdisp_smooth_bounds(s);
548 for(S = 0; S < dst_corners; S++) {
549 float base[2], axis_x[2], axis_y[2];
551 mdisp_apply_weight(S, dst_corners, 0, 0, st, crn_weight, &base[0], &base[1]);
552 mdisp_apply_weight(S, dst_corners, side-1, 0, st, crn_weight, &axis_x[0], &axis_x[1]);
553 mdisp_apply_weight(S, dst_corners, 0, side-1, st, crn_weight, &axis_y[0], &axis_y[1]);
555 sub_v2_v2(axis_x, base);
556 sub_v2_v2(axis_y, base);
557 normalize_v2(axis_x);
558 normalize_v2(axis_y);
560 for(y = 0; y < side; ++y) {
561 for(x = 0; x < side; ++x, ++out) {
563 float face_u, face_v, crn_u, crn_v;
565 mdisp_apply_weight(S, dst_corners, x, y, st, crn_weight, &face_u, &face_v);
566 crn = mdisp_rot_face_to_quad_crn(src_corners, st, face_u, face_v, &crn_u, &crn_v);
568 old_mdisps_bilinear((*out), &s->disps[crn*side*side], side, crn_u, crn_v);
569 mdisp_flip_disp(crn, dst_corners, axis_x, axis_y, *out);
579 static void layerCopy_mdisps(const void *source, void *dest, int count)
582 const MDisps *s = source;
585 for(i = 0; i < count; ++i) {
587 d[i].disps = MEM_dupallocN(s[i].disps);
588 d[i].totdisp = s[i].totdisp;
598 static void layerValidate_mdisps(void *data, int sub_elements)
604 MDisps *disps = data;
606 int corners = multires_mdisp_corners(disps);
608 if(corners != sub_elements) {
609 MEM_freeN(disps->disps);
610 disps->totdisp = disps->totdisp / corners * sub_elements;
611 disps->disps = MEM_callocN(3*disps->totdisp*sizeof(float), "layerValidate_mdisps");
617 static void layerFree_mdisps(void *data, int count, int UNUSED(size))
622 for(i = 0; i < count; ++i) {
624 MEM_freeN(d[i].disps);
630 static int layerRead_mdisps(CDataFile *cdf, void *data, int count)
635 for(i = 0; i < count; ++i) {
637 d[i].disps = MEM_callocN(sizeof(float)*3*d[i].totdisp, "mdisps read");
639 if(!cdf_read_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
640 printf("failed to read multires displacement %d/%d %d\n", i, count, d[i].totdisp);
648 static int layerWrite_mdisps(CDataFile *cdf, void *data, int count)
653 for(i = 0; i < count; ++i) {
654 if(!cdf_write_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
655 printf("failed to write multires displacement %d/%d %d\n", i, count, d[i].totdisp);
663 static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), void *data, int count)
669 for(i = 0; i < count; ++i)
670 size += d[i].totdisp*3*sizeof(float);
676 static void layerCopyValue_mloopcol(void *source, void *dest)
678 MLoopCol *m1 = source, *m2 = dest;
686 static int layerEqual_mloopcol(void *data1, void *data2)
688 MLoopCol *m1 = data1, *m2 = data2;
696 return r*r + g*g + b*b + a*a < 0.001;
699 static void layerMultiply_mloopcol(void *data, float fac)
703 m->r = (float)m->r * fac;
704 m->g = (float)m->g * fac;
705 m->b = (float)m->b * fac;
706 m->a = (float)m->a * fac;
709 static void layerAdd_mloopcol(void *data1, void *data2)
711 MLoopCol *m = data1, *m2 = data2;
719 static void layerDoMinMax_mloopcol(void *data, void *vmin, void *vmax)
722 MLoopCol *min = vmin, *max = vmax;
724 if (m->r < min->r) min->r = m->r;
725 if (m->g < min->g) min->g = m->g;
726 if (m->b < min->b) min->b = m->b;
727 if (m->a < min->a) min->a = m->a;
729 if (m->r > max->r) max->r = m->r;
730 if (m->g > max->g) max->g = m->g;
731 if (m->b > max->b) max->b = m->b;
732 if (m->a > max->a) max->a = m->a;
735 static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
737 MLoopCol *min = vmin, *max = vmax;
750 static void layerDefault_mloopcol(void *data, int count)
752 MLoopCol default_mloopcol = {255,255,255,255};
753 MLoopCol *mlcol = (MLoopCol*)data;
755 for(i = 0; i < count; i++)
756 mlcol[i] = default_mloopcol;
760 static void layerInterp_mloopcol(void **sources, float *weights,
761 float *sub_weights, int count, void *dest)
772 col.a = col.r = col.g = col.b = 0;
774 sub_weight = sub_weights;
775 for(i = 0; i < count; ++i){
776 float weight = weights ? weights[i] : 1;
777 MLoopCol *src = sources[i];
779 col.a += src->a * (*sub_weight) * weight;
780 col.r += src->r * (*sub_weight) * weight;
781 col.g += src->g * (*sub_weight) * weight;
782 col.b += src->b * (*sub_weight) * weight;
785 col.a += src->a * weight;
786 col.r += src->r * weight;
787 col.g += src->g * weight;
788 col.b += src->b * weight;
792 /* Subdivide smooth or fractal can cause problems without clamping
793 * although weights should also not cause this situation */
794 CLAMP(col.a, 0.0f, 255.0f);
795 CLAMP(col.r, 0.0f, 255.0f);
796 CLAMP(col.g, 0.0f, 255.0f);
797 CLAMP(col.b, 0.0f, 255.0f);
805 static void layerCopyValue_mloopuv(void *source, void *dest)
807 MLoopUV *luv1 = source, *luv2 = dest;
809 copy_v2_v2(luv2->uv, luv1->uv);
812 static int layerEqual_mloopuv(void *data1, void *data2)
814 MLoopUV *luv1 = data1, *luv2 = data2;
816 return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
819 static void layerMultiply_mloopuv(void *data, float fac)
823 mul_v2_fl(luv->uv, fac);
826 static void layerInitMinMax_mloopuv(void *vmin, void *vmax)
828 MLoopUV *min = vmin, *max = vmax;
830 INIT_MINMAX2(min->uv, max->uv);
833 static void layerDoMinMax_mloopuv(void *data, void *vmin, void *vmax)
835 MLoopUV *min = vmin, *max = vmax, *luv = data;
837 DO_MINMAX2(luv->uv, min->uv, max->uv);
840 static void layerAdd_mloopuv(void *data1, void *data2)
842 MLoopUV *l1 = data1, *l2 = data2;
844 add_v2_v2(l1->uv, l2->uv);
847 static void layerInterp_mloopuv(void **sources, float *weights,
848 float *sub_weights, int count, void *dest)
850 MLoopUV *mluv = dest;
857 const float *sub_weight = sub_weights;
858 for(i = 0; i < count; i++) {
859 float weight = weights ? weights[i] : 1.0f;
860 MLoopUV *src = sources[i];
861 madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
866 for(i = 0; i < count; i++) {
867 float weight = weights ? weights[i] : 1;
868 MLoopUV *src = sources[i];
869 madd_v2_v2fl(uv, src->uv, weight);
874 static void layerInterp_mcol(void **sources, float *weights,
875 float *sub_weights, int count, void *dest)
888 if(count <= 0) return;
890 sub_weight = sub_weights;
891 for(i = 0; i < count; ++i) {
892 float weight = weights ? weights[i] : 1;
894 for(j = 0; j < 4; ++j) {
896 MCol *src = sources[i];
897 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
898 const float w= (*sub_weight) * weight;
899 col[j].a += src->a * w;
900 col[j].r += src->r * w;
901 col[j].g += src->g * w;
902 col[j].b += src->b * w;
905 MCol *src = sources[i];
906 col[j].a += src[j].a * weight;
907 col[j].r += src[j].r * weight;
908 col[j].g += src[j].g * weight;
909 col[j].b += src[j].b * weight;
914 for(j = 0; j < 4; ++j) {
916 /* Subdivide smooth or fractal can cause problems without clamping
917 * although weights should also not cause this situation */
918 CLAMP(col[j].a, 0.0f, 255.0f);
919 CLAMP(col[j].r, 0.0f, 255.0f);
920 CLAMP(col[j].g, 0.0f, 255.0f);
921 CLAMP(col[j].b, 0.0f, 255.0f);
923 mc[j].a = (int)col[j].a;
924 mc[j].r = (int)col[j].r;
925 mc[j].g = (int)col[j].g;
926 mc[j].b = (int)col[j].b;
930 static void layerSwap_mcol(void *data, const int *corner_indices)
936 for(j = 0; j < 4; ++j)
937 col[j] = mcol[corner_indices[j]];
939 memcpy(mcol, col, sizeof(col));
942 static void layerDefault_mcol(void *data, int count)
944 static MCol default_mcol = {255, 255, 255, 255};
945 MCol *mcol = (MCol*)data;
948 for(i = 0; i < 4*count; i++) {
949 mcol[i] = default_mcol;
953 static void layerInterp_bweight(void **sources, float *weights,
954 float *UNUSED(sub_weights), int count, void *dest)
957 float **in = (float **)sources;
960 if(count <= 0) return;
965 for(i = 0; i < count; ++i) {
966 *f += *in[i] * weights[i];
970 for(i = 0; i < count; ++i) {
976 static void layerInterp_shapekey(void **sources, float *weights,
977 float *UNUSED(sub_weights), int count, void *dest)
980 float **in = (float **)sources;
983 if(count <= 0) return;
988 for(i = 0; i < count; ++i) {
989 madd_v3_v3fl(co, in[i], weights[i]);
993 for(i = 0; i < count; ++i) {
994 add_v3_v3(co, in[i]);
999 static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
1001 {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1003 {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
1005 /* 2: CD_MDEFORMVERT */
1006 {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
1007 layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
1009 {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1011 {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1013 {sizeof(MTFace), "MTFace", 1, "UVMap", layerCopy_tface, NULL,
1014 layerInterp_tface, layerSwap_tface, layerDefault_tface},
1016 /* 4 MCol structs per face */
1017 {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
1018 layerSwap_mcol, layerDefault_mcol},
1019 /* 7: CD_ORIGINDEX */
1020 {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1022 /* 3 floats per normal vector */
1023 {sizeof(float)*3, "vec3f", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1024 /* 9: CD_POLYINDEX */
1025 {sizeof(int), "MIntProperty", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1026 /* 10: CD_PROP_FLT */
1027 {sizeof(MFloatProperty), "MFloatProperty",1,"Float", layerCopy_propFloat,NULL,NULL,NULL},
1028 /* 11: CD_PROP_INT */
1029 {sizeof(MIntProperty), "MIntProperty",1,"Int",layerCopy_propInt,NULL,NULL,NULL},
1030 /* 12: CD_PROP_STR */
1031 {sizeof(MStringProperty), "MStringProperty",1,"String",layerCopy_propString,NULL,NULL,NULL},
1032 /* 13: CD_ORIGSPACE */
1033 {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVMap", layerCopy_origspace_face, NULL,
1034 layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
1036 {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1037 /* 15: CD_MTEXPOLY */
1038 {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
1039 /* 16: CD_MLOOPUV */
1040 {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
1041 layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv,
1042 layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
1043 /* 17: CD_MLOOPCOL */
1044 {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL,
1045 layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1046 layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1047 /* 18: CD_TANGENT */
1048 {sizeof(float)*4*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1050 {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
1051 layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL,
1052 NULL, NULL, NULL, NULL, NULL, NULL,
1053 layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps, layerValidate_mdisps},
1054 /* 20: CD_WEIGHT_MCOL */
1055 {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
1056 layerSwap_mcol, layerDefault_mcol},
1057 /* 21: CD_ID_MCOL */
1058 {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
1059 layerSwap_mcol, layerDefault_mcol},
1060 /* 22: CD_TEXTURE_MCOL */
1061 {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
1062 layerSwap_mcol, layerDefault_mcol},
1063 /* 23: CD_CLOTH_ORCO */
1064 {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1066 {sizeof(MRecast), "MRecast", 1,"Recast",NULL,NULL,NULL,NULL}
1071 {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
1073 {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
1074 /* 27: CD_SHAPE_KEYINDEX */
1075 {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1076 /* 28: CD_SHAPEKEY */
1077 {sizeof(float)*3, "", 0, "ShapeKey", NULL, NULL, layerInterp_shapekey},
1078 /* 29: CD_BWEIGHT */
1079 {sizeof(float), "", 0, "BevelWeight", NULL, NULL, layerInterp_bweight},
1081 {sizeof(float), "", 0, "SubSurfCrease", NULL, NULL, layerInterp_bweight},
1082 /* 31: CD_WEIGHT_MLOOPCOL */
1083 {sizeof(MLoopCol), "MLoopCol", 1, "WeightLoopCol", NULL, NULL, layerInterp_mloopcol, NULL,
1084 layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1085 layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1086 /* END BMESH ONLY */
1091 /* note, numbers are from trunk and need updating for bmesh */
1093 static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
1094 /* 0-4 */ "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace",
1095 /* 5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
1096 /* 10-14 */ "CDMFloatProperty", "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco",
1097 /* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
1098 /* 20-24 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco", "CDMRecast"
1102 /* 25-29 */ "CDMPoly", "CDMLoop", "CDShapeKeyIndex", "CDShapeKey", "CDBevelWeight",
1103 /* 30-31 */ "CDSubSurfCrease", "CDWeightLoopCol"
1104 /* END BMESH ONLY */
1109 const CustomDataMask CD_MASK_BAREMESH =
1110 CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY | CD_MASK_BWEIGHT;
1111 const CustomDataMask CD_MASK_MESH =
1112 CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
1113 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
1114 CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS |
1115 CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MPOLY | CD_MASK_MLOOP |
1116 CD_MASK_MTEXPOLY | CD_MASK_NORMAL | CD_MASK_MDISPS | CD_MASK_RECAST;
1117 const CustomDataMask CD_MASK_EDITMESH =
1118 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MLOOPUV |
1119 CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_SHAPE_KEYINDEX |
1120 CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR |
1121 CD_MASK_MDISPS | CD_MASK_SHAPEKEY | CD_MASK_RECAST;
1122 const CustomDataMask CD_MASK_DERIVEDMESH =
1123 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
1124 CD_MASK_MCOL | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_CLOTH_ORCO |
1125 CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_WEIGHT_MLOOPCOL |
1126 CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO | CD_MASK_TANGENT |
1127 CD_MASK_WEIGHT_MCOL | CD_MASK_NORMAL | CD_MASK_SHAPEKEY | CD_MASK_RECAST |
1128 CD_MASK_ORIGINDEX | CD_MASK_POLYINDEX;
1129 const CustomDataMask CD_MASK_BMESH = CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
1130 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT |
1131 CD_MASK_PROP_STR | CD_MASK_SHAPEKEY | CD_MASK_SHAPE_KEYINDEX | CD_MASK_MDISPS;
1132 const CustomDataMask CD_MASK_FACECORNERS =
1133 CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
1136 static const LayerTypeInfo *layerType_getInfo(int type)
1138 if(type < 0 || type >= CD_NUMTYPES) return NULL;
1140 return &LAYERTYPEINFO[type];
1143 static const char *layerType_getName(int type)
1145 if(type < 0 || type >= CD_NUMTYPES) return NULL;
1147 return LAYERTYPENAMES[type];
1150 /********************* CustomData functions *********************/
1151 static void customData_update_offsets(CustomData *data);
1153 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1154 int type, int alloctype, void *layerdata, int totelem, const char *name);
1156 void CustomData_update_typemap(CustomData *data)
1158 int i, lasttype = -1;
1160 /* since we cant do in a pre-processor do here as an assert */
1161 BLI_assert(sizeof(data->typemap) / sizeof(int) >= CD_NUMTYPES);
1163 for (i=0; i<CD_NUMTYPES; i++) {
1164 data->typemap[i] = -1;
1167 for (i=0; i<data->totlayer; i++) {
1168 if (data->layers[i].type != lasttype) {
1169 data->typemap[data->layers[i].type] = i;
1171 lasttype = data->layers[i].type;
1175 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
1176 CustomDataMask mask, int alloctype, int totelem)
1178 /*const LayerTypeInfo *typeInfo;*/
1179 CustomDataLayer *layer, *newlayer;
1181 int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0, lastflag = 0;
1183 for(i = 0; i < source->totlayer; ++i) {
1184 layer = &source->layers[i];
1185 /*typeInfo = layerType_getInfo(layer->type);*/ /*UNUSED*/
1189 if (type != lasttype) {
1191 lastactive = layer->active;
1192 lastrender = layer->active_rnd;
1193 lastclone = layer->active_clone;
1194 lastmask = layer->active_mask;
1196 lastflag = layer->flag;
1201 if(lastflag & CD_FLAG_NOCOPY) continue;
1202 else if(!(mask & CD_TYPE_AS_MASK(type))) continue;
1203 else if(number < CustomData_number_of_layers(dest, type)) continue;
1205 switch (alloctype) {
1216 if((alloctype == CD_ASSIGN) && (lastflag & CD_FLAG_NOFREE))
1217 newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
1218 data, totelem, layer->name);
1220 newlayer = customData_add_layer__internal(dest, type, alloctype,
1221 data, totelem, layer->name);
1224 newlayer->uid = layer->uid;
1226 newlayer->active = lastactive;
1227 newlayer->active_rnd = lastrender;
1228 newlayer->active_clone = lastclone;
1229 newlayer->active_mask = lastmask;
1230 newlayer->flag |= lastflag & (CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY);
1234 CustomData_update_typemap(dest);
1237 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
1238 CustomDataMask mask, int alloctype, int totelem)
1240 memset(dest, 0, sizeof(*dest));
1242 if(source->external)
1243 dest->external= MEM_dupallocN(source->external);
1245 CustomData_merge(source, dest, mask, alloctype, totelem);
1248 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
1250 const LayerTypeInfo *typeInfo;
1252 if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
1253 typeInfo = layerType_getInfo(layer->type);
1256 typeInfo->free(layer->data, totelem, typeInfo->size);
1259 MEM_freeN(layer->data);
1263 static void CustomData_external_free(CustomData *data)
1265 if(data->external) {
1266 MEM_freeN(data->external);
1267 data->external= NULL;
1271 void CustomData_free(CustomData *data, int totelem)
1275 for(i = 0; i < data->totlayer; ++i)
1276 customData_free_layer__internal(&data->layers[i], totelem);
1279 MEM_freeN(data->layers);
1281 CustomData_external_free(data);
1283 memset(data, 0, sizeof(*data));
1286 static void customData_update_offsets(CustomData *data)
1288 const LayerTypeInfo *typeInfo;
1291 for(i = 0; i < data->totlayer; ++i) {
1292 typeInfo = layerType_getInfo(data->layers[i].type);
1294 data->layers[i].offset = offset;
1295 offset += typeInfo->size;
1298 data->totsize = offset;
1299 CustomData_update_typemap(data);
1302 int CustomData_get_layer_index(const CustomData *data, int type)
1306 for(i=0; i < data->totlayer; ++i)
1307 if(data->layers[i].type == type)
1313 int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n)
1315 int i = CustomData_get_layer_index(data, type);
1318 i = (data->layers[i + n].type == type) ? (i + n) : (-1);
1324 int CustomData_get_named_layer_index(const CustomData *data, int type, const char *name)
1328 for(i=0; i < data->totlayer; ++i)
1329 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
1335 int CustomData_get_active_layer_index(const CustomData *data, int type)
1337 if (!data->totlayer)
1340 if (data->typemap[type] != -1) {
1341 return data->typemap[type] + data->layers[data->typemap[type]].active;
1347 int CustomData_get_render_layer_index(const CustomData *data, int type)
1351 for(i=0; i < data->totlayer; ++i)
1352 if(data->layers[i].type == type)
1353 return i + data->layers[i].active_rnd;
1358 int CustomData_get_clone_layer_index(const CustomData *data, int type)
1362 for(i=0; i < data->totlayer; ++i)
1363 if(data->layers[i].type == type)
1364 return i + data->layers[i].active_clone;
1369 int CustomData_get_stencil_layer_index(const CustomData *data, int type)
1373 for(i=0; i < data->totlayer; ++i)
1374 if(data->layers[i].type == type)
1375 return i + data->layers[i].active_mask;
1380 int CustomData_get_active_layer(const CustomData *data, int type)
1384 for(i=0; i < data->totlayer; ++i)
1385 if(data->layers[i].type == type)
1386 return data->layers[i].active;
1391 int CustomData_get_render_layer(const CustomData *data, int type)
1395 for(i=0; i < data->totlayer; ++i)
1396 if(data->layers[i].type == type)
1397 return data->layers[i].active_rnd;
1402 int CustomData_get_clone_layer(const CustomData *data, int type)
1406 for(i=0; i < data->totlayer; ++i)
1407 if(data->layers[i].type == type)
1408 return data->layers[i].active_clone;
1413 int CustomData_get_stencil_layer(const CustomData *data, int type)
1417 for(i=0; i < data->totlayer; ++i)
1418 if(data->layers[i].type == type)
1419 return data->layers[i].active_mask;
1424 void CustomData_set_layer_active(CustomData *data, int type, int n)
1428 for(i=0; i < data->totlayer; ++i)
1429 if(data->layers[i].type == type)
1430 data->layers[i].active = n;
1433 void CustomData_set_layer_render(CustomData *data, int type, int n)
1437 for(i=0; i < data->totlayer; ++i)
1438 if(data->layers[i].type == type)
1439 data->layers[i].active_rnd = n;
1442 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1446 for(i=0; i < data->totlayer; ++i)
1447 if(data->layers[i].type == type)
1448 data->layers[i].active_clone = n;
1451 void CustomData_set_layer_stencil(CustomData *data, int type, int n)
1455 for(i=0; i < data->totlayer; ++i)
1456 if(data->layers[i].type == type)
1457 data->layers[i].active_mask = n;
1460 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1461 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1465 for(i=0; i < data->totlayer; ++i)
1466 if(data->layers[i].type == type)
1467 data->layers[i].active = n-i;
1470 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1474 for(i=0; i < data->totlayer; ++i)
1475 if(data->layers[i].type == type)
1476 data->layers[i].active_rnd = n-i;
1479 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1483 for(i=0; i < data->totlayer; ++i)
1484 if(data->layers[i].type == type)
1485 data->layers[i].active_clone = n-i;
1488 void CustomData_set_layer_stencil_index(CustomData *data, int type, int n)
1492 for(i=0; i < data->totlayer; ++i)
1493 if(data->layers[i].type == type)
1494 data->layers[i].active_mask = n-i;
1497 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1501 for(i=0; i < data->totlayer; ++i)
1502 if(data->layers[i].type == type)
1503 data->layers[i].flag |= flag;
1506 static int customData_resize(CustomData *data, int amount)
1508 CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1509 "CustomData->layers");
1512 data->maxlayer += amount;
1514 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1515 MEM_freeN(data->layers);
1522 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1523 int type, int alloctype, void *layerdata, int totelem, const char *name)
1525 const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1526 int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1527 void *newlayerdata = NULL;
1529 /* Passing a layerdata to copy from with an alloctype that won't copy is
1530 most likely a bug */
1531 BLI_assert(!layerdata ||
1532 (alloctype == CD_ASSIGN) ||
1533 (alloctype == CD_DUPLICATE) ||
1534 (alloctype == CD_REFERENCE));
1536 if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1537 return &data->layers[CustomData_get_layer_index(data, type)];
1539 if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1540 newlayerdata = layerdata;
1542 else if (size > 0) {
1543 newlayerdata = MEM_callocN(size, layerType_getName(type));
1548 if (alloctype == CD_DUPLICATE && layerdata) {
1550 typeInfo->copy(layerdata, newlayerdata, totelem);
1552 memcpy(newlayerdata, layerdata, size);
1554 else if (alloctype == CD_DEFAULT) {
1555 if(typeInfo->set_default)
1556 typeInfo->set_default((char*)newlayerdata, totelem);
1558 else if (alloctype == CD_REFERENCE)
1559 flag |= CD_FLAG_NOFREE;
1561 if(index >= data->maxlayer) {
1562 if(!customData_resize(data, CUSTOMDATA_GROW)) {
1563 if(newlayerdata != layerdata)
1564 MEM_freeN(newlayerdata);
1571 /* keep layers ordered by type */
1572 for( ; index > 0 && data->layers[index - 1].type > type; --index)
1573 data->layers[index] = data->layers[index - 1];
1575 data->layers[index].type = type;
1576 data->layers[index].flag = flag;
1577 data->layers[index].data = newlayerdata;
1579 if(name || (name=typeInfo->defaultname)) {
1580 BLI_strncpy(data->layers[index].name, name, sizeof(data->layers[index].name));
1581 CustomData_set_layer_unique_name(data, index);
1584 data->layers[index].name[0] = '\0';
1586 if(index > 0 && data->layers[index-1].type == type) {
1587 data->layers[index].active = data->layers[index-1].active;
1588 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1589 data->layers[index].active_clone = data->layers[index-1].active_clone;
1590 data->layers[index].active_mask = data->layers[index-1].active_mask;
1592 data->layers[index].active = 0;
1593 data->layers[index].active_rnd = 0;
1594 data->layers[index].active_clone = 0;
1595 data->layers[index].active_mask = 0;
1598 customData_update_offsets(data);
1600 return &data->layers[index];
1603 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1604 void *layerdata, int totelem)
1606 CustomDataLayer *layer;
1607 const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1609 layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1610 totelem, typeInfo->defaultname);
1611 CustomData_update_typemap(data);
1619 /*same as above but accepts a name*/
1620 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1621 void *layerdata, int totelem, const char *name)
1623 CustomDataLayer *layer;
1625 layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1627 CustomData_update_typemap(data);
1636 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1640 if (index < 0) return 0;
1642 customData_free_layer__internal(&data->layers[index], totelem);
1644 for (i=index+1; i < data->totlayer; ++i)
1645 data->layers[i-1] = data->layers[i];
1649 /* if layer was last of type in array, set new active layer */
1650 if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1651 i = CustomData_get_layer_index(data, type);
1654 for (; i < data->totlayer && data->layers[i].type == type; i++) {
1655 data->layers[i].active--;
1656 data->layers[i].active_rnd--;
1657 data->layers[i].active_clone--;
1658 data->layers[i].active_mask--;
1662 if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1663 customData_resize(data, -CUSTOMDATA_GROW);
1665 customData_update_offsets(data);
1666 CustomData_update_typemap(data);
1671 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1674 index = CustomData_get_active_layer_index(data, type);
1675 if (index < 0) return 0;
1676 return CustomData_free_layer(data, type, totelem, index);
1680 void CustomData_free_layers(CustomData *data, int type, int totelem)
1682 while (CustomData_has_layer(data, type))
1683 CustomData_free_layer_active(data, type, totelem);
1686 int CustomData_has_layer(const CustomData *data, int type)
1688 return (CustomData_get_layer_index(data, type) != -1);
1691 int CustomData_number_of_layers(const CustomData *data, int type)
1695 for(i = 0; i < data->totlayer; i++)
1696 if(data->layers[i].type == type)
1702 void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem)
1704 CustomDataLayer *layer;
1707 /* get the layer index of the first layer of type */
1708 layer_index = CustomData_get_active_layer_index(data, type);
1709 if(layer_index < 0) return NULL;
1711 layer = &data->layers[layer_index];
1713 if (layer->flag & CD_FLAG_NOFREE) {
1714 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1715 * CD_MDEFORMVERT, which has pointers to allocated data...
1716 * So in case a custom copy function is defined, use it!
1718 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1720 if(typeInfo->copy) {
1721 char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1722 typeInfo->copy(layer->data, dest_data, totelem);
1723 layer->data = dest_data;
1726 layer->data = MEM_dupallocN(layer->data);
1728 layer->flag &= ~CD_FLAG_NOFREE;
1734 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1735 const int type, const char *name, const int totelem)
1737 CustomDataLayer *layer;
1740 /* get the layer index of the desired layer */
1741 layer_index = CustomData_get_named_layer_index(data, type, name);
1742 if(layer_index < 0) return NULL;
1744 layer = &data->layers[layer_index];
1746 if (layer->flag & CD_FLAG_NOFREE) {
1747 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1748 * CD_MDEFORMVERT, which has pointers to allocated data...
1749 * So in case a custom copy function is defined, use it!
1751 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1753 if(typeInfo->copy) {
1754 char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1755 typeInfo->copy(layer->data, dest_data, totelem);
1756 layer->data = dest_data;
1759 layer->data = MEM_dupallocN(layer->data);
1761 layer->flag &= ~CD_FLAG_NOFREE;
1767 int CustomData_is_referenced_layer(struct CustomData *data, int type)
1769 CustomDataLayer *layer;
1772 /* get the layer index of the first layer of type */
1773 layer_index = CustomData_get_active_layer_index(data, type);
1774 if(layer_index < 0) return 0;
1776 layer = &data->layers[layer_index];
1778 return (layer->flag & CD_FLAG_NOFREE) != 0;
1781 void CustomData_free_temporary(CustomData *data, int totelem)
1783 CustomDataLayer *layer;
1786 for(i = 0, j = 0; i < data->totlayer; ++i) {
1787 layer = &data->layers[i];
1790 data->layers[j] = data->layers[i];
1792 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1793 customData_free_layer__internal(layer, totelem);
1800 if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1801 customData_resize(data, -CUSTOMDATA_GROW);
1803 customData_update_offsets(data);
1806 void CustomData_set_only_copy(const struct CustomData *data,
1807 CustomDataMask mask)
1811 for(i = 0; i < data->totlayer; ++i)
1812 if(!(mask & CD_TYPE_AS_MASK(data->layers[i].type)))
1813 data->layers[i].flag |= CD_FLAG_NOCOPY;
1816 void CustomData_copy_elements(int type, void *source, void *dest, int count)
1818 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1821 typeInfo->copy(source, dest, count);
1823 memcpy(dest, source, typeInfo->size*count);
1826 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1827 int source_index, int dest_index, int count)
1829 const LayerTypeInfo *typeInfo;
1834 /* copies a layer at a time */
1836 for(src_i = 0; src_i < source->totlayer; ++src_i) {
1838 /* find the first dest layer with type >= the source type
1839 * (this should work because layers are ordered by type)
1841 while(dest_i < dest->totlayer
1842 && dest->layers[dest_i].type < source->layers[src_i].type)
1845 /* if there are no more dest layers, we're done */
1846 if(dest_i >= dest->totlayer) return;
1848 /* if we found a matching layer, copy the data */
1849 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1850 char *src_data = source->layers[src_i].data;
1851 char *dest_data = dest->layers[dest_i].data;
1853 typeInfo = layerType_getInfo(source->layers[src_i].type);
1855 src_offset = source_index * typeInfo->size;
1856 dest_offset = dest_index * typeInfo->size;
1858 if (!src_data || !dest_data) {
1859 printf("%s: warning null data for %s type (%p --> %p), skipping\n",
1860 __func__, layerType_getName(source->layers[src_i].type),
1861 (void *)src_data, (void *)dest_data);
1866 typeInfo->copy(src_data + src_offset,
1867 dest_data + dest_offset,
1870 memcpy(dest_data + dest_offset,
1871 src_data + src_offset,
1872 count * typeInfo->size);
1874 /* if there are multiple source & dest layers of the same type,
1875 * we don't want to copy all source layers to the same dest, so
1883 void CustomData_free_elem(CustomData *data, int index, int count)
1886 const LayerTypeInfo *typeInfo;
1888 for(i = 0; i < data->totlayer; ++i) {
1889 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1890 typeInfo = layerType_getInfo(data->layers[i].type);
1892 if(typeInfo->free) {
1893 int offset = typeInfo->size * index;
1895 typeInfo->free((char *)data->layers[i].data + offset,
1896 count, typeInfo->size);
1902 #define SOURCE_BUF_SIZE 100
1904 void CustomData_interp(const CustomData *source, CustomData *dest,
1905 int *src_indices, float *weights, float *sub_weights,
1906 int count, int dest_index)
1911 void *source_buf[SOURCE_BUF_SIZE];
1912 void **sources = source_buf;
1914 /* slow fallback in case we're interpolating a ridiculous number of
1917 if(count > SOURCE_BUF_SIZE)
1918 sources = MEM_callocN(sizeof(*sources) * count,
1919 "CustomData_interp sources");
1921 /* interpolates a layer at a time */
1923 for(src_i = 0; src_i < source->totlayer; ++src_i) {
1924 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
1925 if(!typeInfo->interp) continue;
1927 /* find the first dest layer with type >= the source type
1928 * (this should work because layers are ordered by type)
1930 while(dest_i < dest->totlayer
1931 && dest->layers[dest_i].type < source->layers[src_i].type)
1934 /* if there are no more dest layers, we're done */
1935 if(dest_i >= dest->totlayer) return;
1937 /* if we found a matching layer, copy the data */
1938 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1939 void *src_data = source->layers[src_i].data;
1941 for(j = 0; j < count; ++j)
1942 sources[j] = (char *)src_data
1943 + typeInfo->size * src_indices[j];
1945 dest_offset = dest_index * typeInfo->size;
1947 typeInfo->interp(sources, weights, sub_weights, count,
1948 (char *)dest->layers[dest_i].data + dest_offset);
1950 /* if there are multiple source & dest layers of the same type,
1951 * we don't want to copy all source layers to the same dest, so
1958 if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1961 void CustomData_swap(struct CustomData *data, int index, const int *corner_indices)
1963 const LayerTypeInfo *typeInfo;
1966 for(i = 0; i < data->totlayer; ++i) {
1967 typeInfo = layerType_getInfo(data->layers[i].type);
1969 if(typeInfo->swap) {
1970 int offset = typeInfo->size * index;
1972 typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
1977 void *CustomData_get(const CustomData *data, int index, int type)
1982 /* get the layer index of the active layer of type */
1983 layer_index = CustomData_get_active_layer_index(data, type);
1984 if(layer_index < 0) return NULL;
1986 /* get the offset of the desired element */
1987 offset = layerType_getInfo(type)->size * index;
1989 return (char *)data->layers[layer_index].data + offset;
1992 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
1997 /* get the layer index of the first layer of type */
1998 layer_index = data->typemap[type];
1999 if(layer_index < 0) return NULL;
2001 offset = layerType_getInfo(type)->size * index;
2002 return (char *)data->layers[layer_index+n].data + offset;
2005 void *CustomData_get_layer(const CustomData *data, int type)
2007 /* get the layer index of the active layer of type */
2008 int layer_index = CustomData_get_active_layer_index(data, type);
2009 if(layer_index < 0) return NULL;
2011 return data->layers[layer_index].data;
2014 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
2016 /* get the layer index of the active layer of type */
2017 int layer_index = CustomData_get_layer_index_n(data, type, n);
2018 if(layer_index < 0) return NULL;
2020 return data->layers[layer_index].data;
2023 void *CustomData_get_layer_named(const struct CustomData *data, int type,
2026 int layer_index = CustomData_get_named_layer_index(data, type, name);
2027 if(layer_index < 0) return NULL;
2029 return data->layers[layer_index].data;
2033 int CustomData_set_layer_name(const CustomData *data, int type, int n, const char *name)
2035 /* get the layer index of the first layer of type */
2036 int layer_index = CustomData_get_layer_index_n(data, type, n);
2038 if(layer_index < 0) return 0;
2039 if (!name) return 0;
2041 strcpy(data->layers[layer_index].name, name);
2046 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
2048 /* get the layer index of the first layer of type */
2049 int layer_index = CustomData_get_active_layer_index(data, type);
2051 if(layer_index < 0) return NULL;
2053 data->layers[layer_index].data = ptr;
2058 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
2060 /* get the layer index of the first layer of type */
2061 int layer_index = CustomData_get_layer_index_n(data, type, n);
2062 if(layer_index < 0) return NULL;
2064 data->layers[layer_index].data = ptr;
2069 void CustomData_set(const CustomData *data, int index, int type, void *source)
2071 void *dest = CustomData_get(data, index, type);
2072 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2077 typeInfo->copy(source, dest, 1);
2079 memcpy(dest, source, typeInfo->size);
2082 /* EditMesh functions */
2084 void CustomData_em_free_block(CustomData *data, void **block)
2086 const LayerTypeInfo *typeInfo;
2091 for(i = 0; i < data->totlayer; ++i) {
2092 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2093 typeInfo = layerType_getInfo(data->layers[i].type);
2095 if(typeInfo->free) {
2096 int offset = data->layers[i].offset;
2097 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2106 static void CustomData_em_alloc_block(CustomData *data, void **block)
2108 /* TODO: optimize free/alloc */
2111 CustomData_em_free_block(data, block);
2113 if (data->totsize > 0)
2114 *block = MEM_callocN(data->totsize, "CustomData EM block");
2119 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
2120 void *src_block, void **dest_block)
2122 const LayerTypeInfo *typeInfo;
2126 CustomData_em_alloc_block(dest, dest_block);
2128 /* copies a layer at a time */
2130 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2132 /* find the first dest layer with type >= the source type
2133 * (this should work because layers are ordered by type)
2135 while(dest_i < dest->totlayer
2136 && dest->layers[dest_i].type < source->layers[src_i].type)
2139 /* if there are no more dest layers, we're done */
2140 if(dest_i >= dest->totlayer) return;
2142 /* if we found a matching layer, copy the data */
2143 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2144 strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2145 char *src_data = (char*)src_block + source->layers[src_i].offset;
2146 char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2148 typeInfo = layerType_getInfo(source->layers[src_i].type);
2151 typeInfo->copy(src_data, dest_data, 1);
2153 memcpy(dest_data, src_data, typeInfo->size);
2155 /* if there are multiple source & dest layers of the same type,
2156 * we don't want to copy all source layers to the same dest, so
2164 void CustomData_em_validate_data(CustomData *data, void *block, int sub_elements)
2167 for(i = 0; i < data->totlayer; i++) {
2168 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[i].type);
2169 char *leayer_data = (char*)block + data->layers[i].offset;
2171 if(typeInfo->validate)
2172 typeInfo->validate(leayer_data, sub_elements);
2176 void *CustomData_em_get(const CustomData *data, void *block, int type)
2180 /* get the layer index of the first layer of type */
2181 layer_index = CustomData_get_active_layer_index(data, type);
2182 if(layer_index < 0) return NULL;
2184 return (char *)block + data->layers[layer_index].offset;
2187 void *CustomData_em_get_n(const CustomData *data, void *block, int type, int n)
2191 /* get the layer index of the first layer of type */
2192 layer_index = CustomData_get_layer_index_n(data, type, n);
2193 if(layer_index < 0) return NULL;
2195 return (char *)block + data->layers[layer_index].offset;
2198 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
2200 void *dest = CustomData_em_get(data, block, type);
2201 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2206 typeInfo->copy(source, dest, 1);
2208 memcpy(dest, source, typeInfo->size);
2211 void CustomData_em_set_n(CustomData *data, void *block, int type, int n, void *source)
2213 void *dest = CustomData_em_get_n(data, block, type, n);
2214 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2219 typeInfo->copy(source, dest, 1);
2221 memcpy(dest, source, typeInfo->size);
2224 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
2225 float *sub_weights, int count, void *dest_block)
2228 void *source_buf[SOURCE_BUF_SIZE];
2229 void **sources = source_buf;
2231 /* slow fallback in case we're interpolating a ridiculous number of
2234 if(count > SOURCE_BUF_SIZE)
2235 sources = MEM_callocN(sizeof(*sources) * count,
2236 "CustomData_interp sources");
2238 /* interpolates a layer at a time */
2239 for(i = 0; i < data->totlayer; ++i) {
2240 CustomDataLayer *layer = &data->layers[i];
2241 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2243 if(typeInfo->interp) {
2244 for(j = 0; j < count; ++j)
2245 sources[j] = (char *)src_blocks[j] + layer->offset;
2247 typeInfo->interp(sources, weights, sub_weights, count,
2248 (char *)dest_block + layer->offset);
2252 if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2255 void CustomData_em_set_default(CustomData *data, void **block)
2257 const LayerTypeInfo *typeInfo;
2261 CustomData_em_alloc_block(data, block);
2263 for(i = 0; i < data->totlayer; ++i) {
2264 int offset = data->layers[i].offset;
2266 typeInfo = layerType_getInfo(data->layers[i].type);
2268 if(typeInfo->set_default)
2269 typeInfo->set_default((char*)*block + offset, 1);
2273 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
2274 int src_index, void **dest_block)
2276 const LayerTypeInfo *typeInfo;
2277 int dest_i, src_i, src_offset;
2280 CustomData_em_alloc_block(dest, dest_block);
2282 /* copies a layer at a time */
2284 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2286 /* find the first dest layer with type >= the source type
2287 * (this should work because layers are ordered by type)
2289 while(dest_i < dest->totlayer
2290 && dest->layers[dest_i].type < source->layers[src_i].type)
2293 /* if there are no more dest layers, we're done */
2294 if(dest_i >= dest->totlayer) return;
2296 /* if we found a matching layer, copy the data */
2297 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2298 int offset = dest->layers[dest_i].offset;
2299 char *src_data = source->layers[src_i].data;
2300 char *dest_data = (char*)*dest_block + offset;
2302 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2303 src_offset = src_index * typeInfo->size;
2306 typeInfo->copy(src_data + src_offset, dest_data, 1);
2308 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2310 /* if there are multiple source & dest layers of the same type,
2311 * we don't want to copy all source layers to the same dest, so
2319 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
2320 void *src_block, int dest_index)
2322 const LayerTypeInfo *typeInfo;
2323 int dest_i, src_i, dest_offset;
2325 /* copies a layer at a time */
2327 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2329 /* find the first dest layer with type >= the source type
2330 * (this should work because layers are ordered by type)
2332 while(dest_i < dest->totlayer
2333 && dest->layers[dest_i].type < source->layers[src_i].type)
2336 /* if there are no more dest layers, we're done */
2337 if(dest_i >= dest->totlayer) return;
2339 /* if we found a matching layer, copy the data */
2340 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2341 int offset = source->layers[src_i].offset;
2342 char *src_data = (char*)src_block + offset;
2343 char *dest_data = dest->layers[dest_i].data;
2345 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2346 dest_offset = dest_index * typeInfo->size;
2349 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2351 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2353 /* if there are multiple source & dest layers of the same type,
2354 * we don't want to copy all source layers to the same dest, so
2364 /*needed to convert to/from different face reps*/
2365 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata,
2366 int totloop, int totpoly)
2369 for(i=0; i < fdata->totlayer; i++) {
2370 if(fdata->layers[i].type == CD_MTFACE) {
2371 CustomData_add_layer_named(pdata, CD_MTEXPOLY, CD_CALLOC, NULL, totpoly, fdata->layers[i].name);
2372 CustomData_add_layer_named(ldata, CD_MLOOPUV, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2374 else if (fdata->layers[i].type == CD_MCOL) {
2375 CustomData_add_layer_named(ldata, CD_MLOOPCOL, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2377 else if (fdata->layers[i].type == CD_MDISPS) {
2378 CustomData_add_layer_named(ldata, CD_MDISPS, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2383 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total)
2386 for(i=0; i < pdata->totlayer; i++) {
2387 if (pdata->layers[i].type == CD_MTEXPOLY) {
2388 CustomData_add_layer_named(fdata, CD_MTFACE, CD_CALLOC, NULL, total, pdata->layers[i].name);
2391 for(i=0; i < ldata->totlayer; i++) {
2392 if (ldata->layers[i].type == CD_MLOOPCOL) {
2393 CustomData_add_layer_named(fdata, CD_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2395 else if (ldata->layers[i].type == CD_WEIGHT_MLOOPCOL) {
2396 CustomData_add_layer_named(fdata, CD_WEIGHT_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2401 void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *pdata, CustomData *ldata)
2405 if (CustomData_has_layer(pdata, CD_MTEXPOLY)) {
2406 act = CustomData_get_active_layer(pdata, CD_MTEXPOLY);
2407 CustomData_set_layer_active(ldata, CD_MLOOPUV, act);
2408 CustomData_set_layer_active(fdata, CD_MTFACE, act);
2410 act = CustomData_get_render_layer(pdata, CD_MTEXPOLY);
2411 CustomData_set_layer_render(ldata, CD_MLOOPUV, act);
2412 CustomData_set_layer_render(fdata, CD_MTFACE, act);
2414 act = CustomData_get_clone_layer(pdata, CD_MTEXPOLY);
2415 CustomData_set_layer_clone(ldata, CD_MLOOPUV, act);
2416 CustomData_set_layer_clone(fdata, CD_MTFACE, act);
2418 act = CustomData_get_stencil_layer(pdata, CD_MTEXPOLY);
2419 CustomData_set_layer_stencil(ldata, CD_MLOOPUV, act);
2420 CustomData_set_layer_stencil(fdata, CD_MTFACE, act);
2423 if (CustomData_has_layer(ldata, CD_MLOOPCOL)) {
2424 act = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
2425 CustomData_set_layer_active(fdata, CD_MCOL, act);
2427 act = CustomData_get_render_layer(ldata, CD_MLOOPCOL);
2428 CustomData_set_layer_render(fdata, CD_MCOL, act);
2430 act = CustomData_get_clone_layer(ldata, CD_MLOOPCOL);
2431 CustomData_set_layer_clone(fdata, CD_MCOL, act);
2433 act = CustomData_get_stencil_layer(ldata, CD_MLOOPCOL);
2434 CustomData_set_layer_stencil(fdata, CD_MCOL, act);
2438 void CustomData_bmesh_init_pool(CustomData *data, int allocsize)
2440 /* Dispose old pools before calling here to avoid leaks */
2441 BLI_assert(data->pool == NULL);
2443 /* If there are no layers, no pool is needed just yet */
2444 if (data->totlayer) {
2445 data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize, TRUE, FALSE);
2449 void CustomData_bmesh_merge(CustomData *source, CustomData *dest,
2450 int mask, int alloctype, BMesh *bm, int type)
2454 CustomData destold = *dest;
2458 CustomData_merge(source, dest, mask, alloctype, 0);
2459 CustomData_bmesh_init_pool(dest, 512);
2463 t = BM_VERTS_OF_MESH; break;
2465 t = BM_EDGES_OF_MESH; break;
2467 t = BM_LOOPS_OF_FACE; break;
2469 t = BM_FACES_OF_MESH; break;
2470 default: /* should never happen */
2471 BLI_assert(!"invalid type given");
2472 t = BM_VERTS_OF_MESH;
2475 if (t != BM_LOOPS_OF_FACE) {
2476 /*ensure all current elements follow new customdata layout*/
2477 BM_ITER(h, &iter, bm, t, NULL) {
2478 CustomData_bmesh_copy_data(&destold, dest, h->data, &tmp);
2479 CustomData_bmesh_free_block(&destold, &h->data);
2487 /*ensure all current elements follow new customdata layout*/
2488 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
2489 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
2490 CustomData_bmesh_copy_data(&destold, dest, l->head.data, &tmp);
2491 CustomData_bmesh_free_block(&destold, &l->head.data);
2497 if (destold.pool) BLI_mempool_destroy(destold.pool);
2500 void CustomData_bmesh_free_block(CustomData *data, void **block)
2502 const LayerTypeInfo *typeInfo;
2506 for(i = 0; i < data->totlayer; ++i) {
2507 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2508 typeInfo = layerType_getInfo(data->layers[i].type);
2510 if(typeInfo->free) {
2511 int offset = data->layers[i].offset;
2512 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2518 BLI_mempool_free(data->pool, *block);
2523 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
2527 CustomData_bmesh_free_block(data, block);
2529 if (data->totsize > 0)
2530 *block = BLI_mempool_alloc(data->pool);
2535 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
2536 void *src_block, void **dest_block)
2538 const LayerTypeInfo *typeInfo;
2542 CustomData_bmesh_alloc_block(dest, dest_block);
2544 /* copies a layer at a time */
2546 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2548 /* find the first dest layer with type >= the source type
2549 * (this should work because layers are ordered by type)
2551 while(dest_i < dest->totlayer
2552 && dest->layers[dest_i].type < source->layers[src_i].type)
2555 /* if there are no more dest layers, we're done */
2556 if(dest_i >= dest->totlayer) return;
2558 /* if we found a matching layer, copy the data */
2559 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2560 strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2561 char *src_data = (char*)src_block + source->layers[src_i].offset;
2562 char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2564 typeInfo = layerType_getInfo(source->layers[src_i].type);
2567 typeInfo->copy(src_data, dest_data, 1);
2569 memcpy(dest_data, src_data, typeInfo->size);
2571 /* if there are multiple source & dest layers of the same type,
2572 * we don't want to copy all source layers to the same dest, so
2580 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
2581 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
2585 /* get the layer index of the first layer of type */
2586 layer_index = CustomData_get_active_layer_index(data, type);
2587 if(layer_index < 0) return NULL;
2589 return (char *)block + data->layers[layer_index].offset;
2592 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
2596 /* get the layer index of the first layer of type */
2597 layer_index = CustomData_get_layer_index(data, type);
2598 if(layer_index < 0) return NULL;
2600 return (char *)block + data->layers[layer_index+n].offset;
2603 /*gets from the layer at physical index n, note: doesn't check type.*/
2604 void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
2606 if(n < 0 || n >= data->totlayer) return NULL;
2608 return (char *)block + data->layers[n].offset;
2611 int CustomData_layer_has_math(struct CustomData *data, int layern)
2613 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[layern].type);
2615 if (typeInfo->equal && typeInfo->add && typeInfo->multiply &&
2616 typeInfo->initminmax && typeInfo->dominmax) return 1;
2621 /*copies the "value" (e.g. mloopuv uv or mloopcol colors) from one block to
2622 another, while not overwriting anything else (e.g. flags)*/
2623 void CustomData_data_copy_value(int type, void *source, void *dest)
2625 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2629 if(typeInfo->copyvalue)
2630 typeInfo->copyvalue(source, dest);
2632 memcpy(dest, source, typeInfo->size);
2635 int CustomData_data_equals(int type, void *data1, void *data2)
2637 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2639 if (typeInfo->equal)
2640 return typeInfo->equal(data1, data2);
2641 else return !memcmp(data1, data2, typeInfo->size);
2644 void CustomData_data_initminmax(int type, void *min, void *max)
2646 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2648 if (typeInfo->initminmax)
2649 typeInfo->initminmax(min, max);
2653 void CustomData_data_dominmax(int type, void *data, void *min, void *max)
2655 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2657 if (typeInfo->dominmax)
2658 typeInfo->dominmax(data, min, max);
2662 void CustomData_data_multiply(int type, void *data, float fac)
2664 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2666 if (typeInfo->multiply)
2667 typeInfo->multiply(data, fac);
2671 void CustomData_data_add(int type, void *data1, void *data2)
2673 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2676 typeInfo->add(data1, data2);
2679 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
2681 void *dest = CustomData_bmesh_get(data, block, type);
2682 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2687 typeInfo->copy(source, dest, 1);
2689 memcpy(dest, source, typeInfo->size);
2692 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
2694 void *dest = CustomData_bmesh_get_n(data, block, type, n);
2695 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2700 typeInfo->copy(source, dest, 1);
2702 memcpy(dest, source, typeInfo->size);
2705 void CustomData_bmesh_set_layer_n(CustomData *data, void *block, int n, void *source)
2707 void *dest = CustomData_bmesh_get_layer_n(data, block, n);
2708 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[n].type);
2713 typeInfo->copy(source, dest, 1);
2715 memcpy(dest, source, typeInfo->size);
2718 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
2719 float *sub_weights, int count, void *dest_block)
2722 void *source_buf[SOURCE_BUF_SIZE];
2723 void **sources = source_buf;
2725 /* slow fallback in case we're interpolating a ridiculous number of
2728 if(count > SOURCE_BUF_SIZE)
2729 sources = MEM_callocN(sizeof(*sources) * count,
2730 "CustomData_interp sources");
2732 /* interpolates a layer at a time */
2733 for(i = 0; i < data->totlayer; ++i) {
2734 CustomDataLayer *layer = &data->layers[i];
2735 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2736 if(typeInfo->interp) {
2737 for(j = 0; j < count; ++j)
2738 sources[j] = (char *)src_blocks[j] + layer->offset;
2740 typeInfo->interp(sources, weights, sub_weights, count,
2741 (char *)dest_block + layer->offset);
2745 if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2748 void CustomData_bmesh_set_default(CustomData *data, void **block)
2750 const LayerTypeInfo *typeInfo;
2754 CustomData_bmesh_alloc_block(data, block);
2756 for(i = 0; i < data->totlayer; ++i) {
2757 int offset = data->layers[i].offset;
2759 typeInfo = layerType_getInfo(data->layers[i].type);
2761 if(typeInfo->set_default)
2762 typeInfo->set_default((char*)*block + offset, 1);
2763 else memset((char*)*block + offset, 0, typeInfo->size);
2767 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2768 int src_index, void **dest_block)
2770 const LayerTypeInfo *typeInfo;
2771 int dest_i, src_i, src_offset;
2774 CustomData_bmesh_alloc_block(dest, dest_block);
2776 /* copies a layer at a time */
2778 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2780 /* find the first dest layer with type >= the source type
2781 * (this should work because layers are ordered by type)
2783 while(dest_i < dest->totlayer
2784 && dest->layers[dest_i].type < source->layers[src_i].type)
2787 /* if there are no more dest layers, we're done */
2788 if(dest_i >= dest->totlayer) return;
2790 /* if we found a matching layer, copy the data */
2791 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2792 int offset = dest->layers[dest_i].offset;
2793 char *src_data = source->layers[src_i].data;
2794 char *dest_data = (char*)*dest_block + offset;
2796 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2797 src_offset = src_index * typeInfo->size;
2800 typeInfo->copy(src_data + src_offset, dest_data, 1);
2802 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2804 /* if there are multiple source & dest layers of the same type,
2805 * we don't want to copy all source layers to the same dest, so
2813 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2814 void *src_block, int dest_index)
2816 const LayerTypeInfo *typeInfo;
2817 int dest_i, src_i, dest_offset;
2819 /* copies a layer at a time */
2821 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2823 /* find the first dest layer with type >= the source type
2824 * (this should work because layers are ordered by type)
2826 while(dest_i < dest->totlayer
2827 && dest->layers[dest_i].type < source->layers[src_i].type)
2830 /* if there are no more dest layers, we're done */
2831 if(dest_i >= dest->totlayer) return;
2833 /* if we found a matching layer, copy the data */
2834 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2835 int offset = source->layers[src_i].offset;
2836 char *src_data = (char*)src_block + offset;
2837 char *dest_data = dest->layers[dest_i].data;
2839 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2840 dest_offset = dest_index * typeInfo->size;
2843 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2845 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2847 /* if there are multiple source & dest layers of the same type,
2848 * we don't want to copy all source layers to the same dest, so
2857 void CustomData_file_write_info(int type, const char **structname, int *structnum)
2859 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2861 *structname = typeInfo->structname;
2862 *structnum = typeInfo->structnum;
2865 int CustomData_sizeof(int type)
2867 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2869 return typeInfo->size;
2872 const char *CustomData_layertype_name(int type)
2874 return layerType_getName(type);
2877 static int CustomData_is_property_layer(int type)
2879 if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2884 static int cd_layer_find_dupe(CustomData *data, const char *name, int type, int index)
2887 /* see if there is a duplicate */
2888 for(i=0; i<data->totlayer; i++) {
2890 CustomDataLayer *layer= &data->layers[i];
2892 if(CustomData_is_property_layer(type)) {
2893 if(CustomData_is_property_layer(layer->type) && strcmp(layer->name, name)==0) {
2898 if(i!=index && layer->type==type && strcmp(layer->name, name)==0) {
2908 static int customdata_unique_check(void *arg, const char *name)
2910 struct {CustomData *data; int type; int index;} *data_arg= arg;
2911 return cd_layer_find_dupe(data_arg->data, name, data_arg->type, data_arg->index);
2914 void CustomData_set_layer_unique_name(CustomData *data, int index)
2916 CustomDataLayer *nlayer= &data->layers[index];
2917 const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2919 struct {CustomData *data; int type; int index;} data_arg;
2920 data_arg.data= data;
2921 data_arg.type= nlayer->type;
2922 data_arg.index= index;
2924 if (!typeInfo->defaultname)
2927 BLI_uniquename_cb(customdata_unique_check, &data_arg, typeInfo->defaultname, '.', nlayer->name, sizeof(nlayer->name));
2930 void CustomData_validate_layer_name(const CustomData *data, int type, char *name, char *outname)
2934 /* if a layer name was given, try to find that layer */
2936 index = CustomData_get_named_layer_index(data, type, name);
2939 /* either no layer was specified, or the layer we want has been
2940 * deleted, so assign the active layer to name
2942 index = CustomData_get_active_layer_index(data, type);
2943 strcpy(outname, data->layers[index].name);
2946 strcpy(outname, name);
2949 int CustomData_verify_versions(struct CustomData *data, int index)
2951 const LayerTypeInfo *typeInfo;
2952 CustomDataLayer *layer = &data->layers[index];
2953 int i, keeplayer = 1;
2955 if (layer->type >= CD_NUMTYPES) {
2956 keeplayer = 0; /* unknown layer type from future version */
2959 typeInfo = layerType_getInfo(layer->type);
2961 if (!typeInfo->defaultname && (index > 0) &&
2962 data->layers[index-1].type == layer->type)
2963 keeplayer = 0; /* multiple layers of which we only support one */
2967 for (i=index+1; i < data->totlayer; ++i)
2968 data->layers[i-1] = data->layers[i];
2975 /****************************** External Files *******************************/
2977 static void customdata_external_filename(char filename[FILE_MAX], ID *id, CustomDataExternal *external)
2979 BLI_strncpy(filename, external->filename, FILE_MAX);
2980 BLI_path_abs(filename, ID_BLEND_PATH(G.main, id));
2983 void CustomData_external_reload(CustomData *data, ID *UNUSED(id), CustomDataMask mask, int totelem)
2985 CustomDataLayer *layer;
2986 const LayerTypeInfo *typeInfo;
2989 for(i=0; i<data->totlayer; i++) {
2990 layer = &data->layers[i];
2991 typeInfo = layerType_getInfo(layer->type);
2993 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
2994 else if((layer->flag & CD_FLAG_EXTERNAL) && (layer->flag & CD_FLAG_IN_MEMORY)) {
2996 typeInfo->free(layer->data, totelem, typeInfo->size);
2997 layer->flag &= ~CD_FLAG_IN_MEMORY;
3002 void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem)
3004 CustomDataExternal *external= data->external;
3005 CustomDataLayer *layer;
3007 CDataFileLayer *blay;
3008 char filename[FILE_MAX];
3009 const LayerTypeInfo *typeInfo;
3015 for(i=0; i<data->totlayer; i++) {
3016 layer = &data->layers[i];
3017 typeInfo = layerType_getInfo(layer->type);
3019 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3020 else if(layer->flag & CD_FLAG_IN_MEMORY);
3021 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read)
3028 customdata_external_filename(filename, id, external);
3030 cdf= cdf_create(CDF_TYPE_MESH);
3031 if(!cdf_read_open(cdf, filename)) {
3032 fprintf(stderr, "Failed to read %s layer from %s.\n", layerType_getName(layer->type), filename);
3036 for(i=0; i<data->totlayer; i++) {
3037 layer = &data->layers[i];
3038 typeInfo = layerType_getInfo(layer->type);
3040 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3041 else if(layer->flag & CD_FLAG_IN_MEMORY);
3042 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
3043 blay= cdf_layer_find(cdf, layer->type, layer->name);
3046 if(cdf_read_layer(cdf, blay)) {
3047 if(typeInfo->read(cdf, layer->data, totelem));
3049 layer->flag |= CD_FLAG_IN_MEMORY;
3057 cdf_read_close(cdf);
3061 void CustomData_external_write(CustomData *data, ID *id, CustomDataMask mask, int totelem, int free)
3063 CustomDataExternal *external= data->external;
3064 CustomDataLayer *layer;
3066 CDataFileLayer *blay;
3067 const LayerTypeInfo *typeInfo;
3069 char filename[FILE_MAX];
3074 /* test if there is anything to write */
3075 for(i=0; i<data->totlayer; i++) {
3076 layer = &data->layers[i];
3077 typeInfo = layerType_getInfo(layer->type);
3079 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3080 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write)
3087 /* make sure data is read before we try to write */
3088 CustomData_external_read(data, id, mask, totelem);
3089 customdata_external_filename(filename, id, external);
3091 cdf= cdf_create(CDF_TYPE_MESH);
3093 for(i=0; i<data->totlayer; i++) {
3094 layer = &data->layers[i];
3095 typeInfo = layerType_getInfo(layer->type);
3097 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) {
3098 if(layer->flag & CD_FLAG_IN_MEMORY) {
3099 cdf_layer_add(cdf, layer->type, layer->name,
3100 typeInfo->filesize(cdf, layer->data, totelem));
3104 return; /* read failed for a layer! */
3109 if(!cdf_write_open(cdf, filename)) {
3110 fprintf(stderr, "Failed to open %s for writing.\n", filename);
3114 for(i=0; i<data->totlayer; i++) {
3115 layer = &data->layers[i];
3116 typeInfo = layerType_getInfo(layer->type);
3118 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
3119 blay= cdf_layer_find(cdf, layer->type, layer->name);
3121 if(cdf_write_layer(cdf, blay)) {
3122 if(typeInfo->write(cdf, layer->data, totelem));
3130 if(i != data->totlayer) {
3131 fprintf(stderr, "Failed to write data to %s.\n", filename);
3136 for(i=0; i<data->totlayer; i++) {
3137 layer = &data->layers[i];
3138 typeInfo = layerType_getInfo(layer->type);
3140 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
3143 typeInfo->free(layer->data, totelem, typeInfo->size);
3144 layer->flag &= ~CD_FLAG_IN_MEMORY;
3149 cdf_write_close(cdf);
3153 void CustomData_external_add(CustomData *data, ID *UNUSED(id), int type, int UNUSED(totelem), const char *filename)
3155 CustomDataExternal *external= data->external;
3156 CustomDataLayer *layer;
3159 layer_index = CustomData_get_active_layer_index(data, type);
3160 if(layer_index < 0) return;
3162 layer = &data->layers[layer_index];
3164 if(layer->flag & CD_FLAG_EXTERNAL)
3168 external= MEM_callocN(sizeof(CustomDataExternal), "CustomDataExternal");
3169 data->external= external;
3171 BLI_strncpy(external->filename, filename, sizeof(external->filename));
3173 layer->flag |= CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY;
3176 void CustomData_external_remove(CustomData *data, ID *id, int type, int totelem)
3178 CustomDataExternal *external= data->external;
3179 CustomDataLayer *layer;
3180 //char filename[FILE_MAX];
3181 int layer_index; // i, remove_file;
3183 layer_index = CustomData_get_active_layer_index(data, type);
3184 if(layer_index < 0) return;
3186 layer = &data->layers[layer_index];
3191 if(layer->flag & CD_FLAG_EXTERNAL) {
3192 if(!(layer->flag & CD_FLAG_IN_MEMORY))
3193 CustomData_external_read(data, id, CD_TYPE_AS_MASK(layer->type), totelem);
3195 layer->flag &= ~CD_FLAG_EXTERNAL;
3199 for(i=0; i<data->totlayer; i++)
3200 if(data->layers[i].flag & CD_FLAG_EXTERNAL)
3204 customdata_external_filename(filename, id, external);
3205 cdf_remove(filename);
3206 CustomData_external_free(data);
3212 int CustomData_external_test(CustomData *data, int type)
3214 CustomDataLayer *layer;
3217 layer_index = CustomData_get_active_layer_index(data, type);
3218 if(layer_index < 0) return 0;
3220 layer = &data->layers[layer_index];
3221 return (layer->flag & CD_FLAG_EXTERNAL);
3225 void CustomData_external_remove_object(CustomData *data, ID *id)
3227 CustomDataExternal *external= data->external;
3228 char filename[FILE_MAX];
3233 customdata_external_filename(filename, id, external);
3234 cdf_remove(filename);
3235 CustomData_external_free(data);