Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / data_transfer.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2014 by Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Bastien Montagne.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/data_transfer.c
29  *  \ingroup bke
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_customdata_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_mesh_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "BLI_array.h"
41 #include "BLI_math.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_context.h"
47 #include "BKE_customdata.h"
48 #include "BKE_data_transfer.h"
49 #include "BKE_deform.h"
50 #include "BKE_DerivedMesh.h"
51 #include "BKE_mesh.h"
52 #include "BKE_mesh_mapping.h"
53 #include "BKE_mesh_remap.h"
54 #include "BKE_object.h"
55 #include "BKE_object_deform.h"
56 #include "BKE_report.h"
57
58 #include "data_transfer_intern.h"
59
60
61 CustomDataMask BKE_object_data_transfer_dttypes_to_cdmask(const int dtdata_types)
62 {
63         CustomDataMask cddata_mask = 0;
64         int i;
65
66         for (i = 0; i < DT_TYPE_MAX; i++) {
67                 const int dtdata_type = 1 << i;
68                 int cddata_type;
69
70                 if (!(dtdata_types & dtdata_type)) {
71                         continue;
72                 }
73
74                 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
75                 if (!(cddata_type & CD_FAKE)) {
76                         cddata_mask |= 1LL << cddata_type;
77                 }
78                 else if (cddata_type == CD_FAKE_MDEFORMVERT) {
79                         cddata_mask |= CD_MASK_MDEFORMVERT;  /* Exception for vgroups :/ */
80                 }
81                 else if (cddata_type == CD_FAKE_UV) {
82                         cddata_mask |= CD_MASK_MLOOPUV;
83                 }
84                 else if (cddata_type == CD_FAKE_LNOR) {
85                         cddata_mask |= CD_MASK_NORMAL | CD_MASK_CUSTOMLOOPNORMAL;
86                 }
87         }
88
89         return cddata_mask;
90 }
91
92 /* Check what can do each layer type (if it is actually handled by transferdata, if it supports advanced mixing... */
93 bool BKE_object_data_transfer_get_dttypes_capacity(
94         const int dtdata_types, bool *r_advanced_mixing, bool *r_threshold)
95 {
96         int i;
97         bool ret = false;
98
99         *r_advanced_mixing = false;
100         *r_threshold = false;
101
102         for (i = 0; (i < DT_TYPE_MAX) && !(ret && *r_advanced_mixing && *r_threshold); i++) {
103                 const int dtdata_type = 1 << i;
104
105                 if (!(dtdata_types & dtdata_type)) {
106                         continue;
107                 }
108
109                 switch (dtdata_type) {
110                 /* Vertex data */
111                         case DT_TYPE_MDEFORMVERT:
112                                 *r_advanced_mixing = true;
113                                 *r_threshold = true;
114                                 ret = true;
115                                 break;
116                         case DT_TYPE_SKIN:
117                                 *r_threshold = true;
118                                 ret = true;
119                                 break;
120                         case DT_TYPE_BWEIGHT_VERT:
121                                 ret = true;
122                                 break;
123                 /* Edge data */
124                         case DT_TYPE_SHARP_EDGE:
125                                 *r_threshold = true;
126                                 ret = true;
127                                 break;
128                         case DT_TYPE_SEAM:
129                                 *r_threshold = true;
130                                 ret = true;
131                                 break;
132                         case DT_TYPE_CREASE:
133                                 ret = true;
134                                 break;
135                         case DT_TYPE_BWEIGHT_EDGE:
136                                 ret = true;
137                                 break;
138                         case DT_TYPE_FREESTYLE_EDGE:
139                                 *r_threshold = true;
140                                 ret = true;
141                                 break;
142                 /* Loop/Poly data */
143                         case DT_TYPE_UV:
144                                 ret = true;
145                                 break;
146                         case DT_TYPE_VCOL:
147                                 *r_advanced_mixing = true;
148                                 *r_threshold = true;
149                                 ret = true;
150                                 break;
151                         case DT_TYPE_LNOR:
152                                 *r_advanced_mixing = true;
153                                 ret = true;
154                                 break;
155                         case DT_TYPE_SHARP_FACE:
156                                 *r_threshold = true;
157                                 ret = true;
158                                 break;
159                         case DT_TYPE_FREESTYLE_FACE:
160                                 *r_threshold = true;
161                                 ret = true;
162                                 break;
163                 }
164         }
165
166         return ret;
167 }
168
169 int BKE_object_data_transfer_get_dttypes_item_types(const int dtdata_types)
170 {
171         int i, ret = 0;
172
173         for (i = 0; (i < DT_TYPE_MAX) && (ret ^ (ME_VERT | ME_EDGE | ME_LOOP | ME_POLY)); i++) {
174                 const int dtdata_type = 1 << i;
175
176                 if (!(dtdata_types & dtdata_type)) {
177                         continue;
178                 }
179
180                 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
181                         ret |= ME_VERT;
182                 }
183                 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
184                         ret |= ME_EDGE;
185                 }
186                 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
187                         ret |= ME_LOOP;
188                 }
189                 if (DT_DATATYPE_IS_POLY(dtdata_type)) {
190                         ret |= ME_POLY;
191                 }
192         }
193
194         return ret;
195 }
196
197 int BKE_object_data_transfer_dttype_to_cdtype(const int dtdata_type)
198 {
199         switch (dtdata_type) {
200                 case DT_TYPE_MDEFORMVERT:
201                         return CD_FAKE_MDEFORMVERT;
202                 case DT_TYPE_SHAPEKEY:
203                         return CD_FAKE_SHAPEKEY;
204                 case DT_TYPE_SKIN:
205                         return CD_MVERT_SKIN;
206                 case DT_TYPE_BWEIGHT_VERT:
207                         return CD_FAKE_BWEIGHT;
208
209                 case DT_TYPE_SHARP_EDGE:
210                         return CD_FAKE_SHARP;
211                 case DT_TYPE_SEAM:
212                         return CD_FAKE_SEAM;
213                 case DT_TYPE_CREASE:
214                         return CD_FAKE_CREASE;
215                 case DT_TYPE_BWEIGHT_EDGE:
216                         return CD_FAKE_BWEIGHT;
217                 case DT_TYPE_FREESTYLE_EDGE:
218                         return CD_FREESTYLE_EDGE;
219
220                 case DT_TYPE_UV:
221                         return CD_FAKE_UV;
222                 case DT_TYPE_SHARP_FACE:
223                         return CD_FAKE_SHARP;
224                 case DT_TYPE_FREESTYLE_FACE:
225                         return CD_FREESTYLE_FACE;
226
227                 case DT_TYPE_VCOL:
228                         return CD_MLOOPCOL;
229                 case DT_TYPE_LNOR:
230                         return CD_FAKE_LNOR;
231
232                 default:
233                         BLI_assert(0);
234         }
235         return 0;  /* Should never be reached! */
236 }
237
238 int BKE_object_data_transfer_dttype_to_srcdst_index(const int dtdata_type)
239 {
240         switch (dtdata_type) {
241                 case DT_TYPE_MDEFORMVERT:
242                         return DT_MULTILAYER_INDEX_MDEFORMVERT;
243                 case DT_TYPE_SHAPEKEY:
244                         return DT_MULTILAYER_INDEX_SHAPEKEY;
245                 case DT_TYPE_UV:
246                         return DT_MULTILAYER_INDEX_UV;
247                 case DT_TYPE_VCOL:
248                         return DT_MULTILAYER_INDEX_VCOL;
249                 default:
250                         return DT_MULTILAYER_INDEX_INVALID;
251         }
252 }
253
254 /* ********** */
255
256 /* Generic pre/post processing, only used by custom loop normals currently. */
257
258 static void data_transfer_dtdata_type_preprocess(
259         Object *UNUSED(ob_src), Object *UNUSED(ob_dst), DerivedMesh *dm_src, DerivedMesh *dm_dst, Mesh *me_dst,
260         const int dtdata_type, const bool dirty_nors_dst, const bool use_split_nors_src, const float split_angle_src)
261 {
262         if (dtdata_type == DT_TYPE_LNOR) {
263                 /* Compute custom normals into regular loop normals, which will be used for the transfer. */
264                 MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
265                 const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
266                 MEdge *edges_dst = dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge;
267                 const int num_edges_dst = dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge;
268                 MPoly *polys_dst = dm_dst ? dm_dst->getPolyArray(dm_dst) : me_dst->mpoly;
269                 const int num_polys_dst = dm_dst ? dm_dst->getNumPolys(dm_dst) : me_dst->totpoly;
270                 MLoop *loops_dst = dm_dst ? dm_dst->getLoopArray(dm_dst) : me_dst->mloop;
271                 const int num_loops_dst = dm_dst ? dm_dst->getNumLoops(dm_dst) : me_dst->totloop;
272                 CustomData *pdata_dst = dm_dst ? dm_dst->getPolyDataLayout(dm_dst) : &me_dst->pdata;
273                 CustomData *ldata_dst = dm_dst ? dm_dst->getLoopDataLayout(dm_dst) : &me_dst->ldata;
274
275                 const bool use_split_nors_dst = (me_dst->flag & ME_AUTOSMOOTH) != 0;
276                 const float split_angle_dst = me_dst->smoothresh;
277
278                 dm_src->calcLoopNormals(dm_src, use_split_nors_src, split_angle_src);
279
280                 if (dm_dst) {
281                         dm_dst->calcLoopNormals(dm_dst, use_split_nors_dst, split_angle_dst);
282                 }
283                 else {
284                         float (*poly_nors_dst)[3];
285                         float (*loop_nors_dst)[3];
286                         short (*custom_nors_dst)[2] = CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL);
287
288                         /* Cache poly nors into a temp CDLayer. */
289                         poly_nors_dst = CustomData_get_layer(pdata_dst, CD_NORMAL);
290                         if (dirty_nors_dst || !poly_nors_dst) {
291                                 if (!poly_nors_dst) {
292                                         poly_nors_dst = CustomData_add_layer(pdata_dst, CD_NORMAL, CD_CALLOC, NULL, num_polys_dst);
293                                         CustomData_set_layer_flag(pdata_dst, CD_NORMAL, CD_FLAG_TEMPORARY);
294                                 }
295                                 BKE_mesh_calc_normals_poly(verts_dst, NULL, num_verts_dst, loops_dst, polys_dst,
296                                                            num_loops_dst, num_polys_dst, poly_nors_dst, true);
297                         }
298                         /* Cache loop nors into a temp CDLayer. */
299                         loop_nors_dst = CustomData_get_layer(ldata_dst, CD_NORMAL);
300                         if (dirty_nors_dst || loop_nors_dst) {
301                                 if (!loop_nors_dst) {
302                                         loop_nors_dst = CustomData_add_layer(ldata_dst, CD_NORMAL, CD_CALLOC, NULL, num_loops_dst);
303                                         CustomData_set_layer_flag(ldata_dst, CD_NORMAL, CD_FLAG_TEMPORARY);
304                                 }
305                                 BKE_mesh_normals_loop_split(verts_dst, num_verts_dst, edges_dst, num_edges_dst,
306                                                             loops_dst, loop_nors_dst, num_loops_dst,
307                                                             polys_dst, (const float (*)[3])poly_nors_dst, num_polys_dst,
308                                                             use_split_nors_dst, split_angle_dst, NULL, custom_nors_dst, NULL);
309                         }
310                 }
311         }
312 }
313
314 static void data_transfer_dtdata_type_postprocess(
315         Object *UNUSED(ob_src), Object *UNUSED(ob_dst), DerivedMesh *UNUSED(dm_src), DerivedMesh *dm_dst, Mesh *me_dst,
316         const int dtdata_type, const bool changed)
317 {
318         if (dtdata_type == DT_TYPE_LNOR) {
319                 /* Bake edited destination loop normals into custom normals again. */
320                 MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
321                 const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
322                 MEdge *edges_dst = dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge;
323                 const int num_edges_dst = dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge;
324                 MPoly *polys_dst = dm_dst ? dm_dst->getPolyArray(dm_dst) : me_dst->mpoly;
325                 const int num_polys_dst = dm_dst ? dm_dst->getNumPolys(dm_dst) : me_dst->totpoly;
326                 MLoop *loops_dst = dm_dst ? dm_dst->getLoopArray(dm_dst) : me_dst->mloop;
327                 const int num_loops_dst = dm_dst ? dm_dst->getNumLoops(dm_dst) : me_dst->totloop;
328                 CustomData *pdata_dst = dm_dst ? dm_dst->getPolyDataLayout(dm_dst) : &me_dst->pdata;
329                 CustomData *ldata_dst = dm_dst ? dm_dst->getLoopDataLayout(dm_dst) : &me_dst->ldata;
330
331                 const float (*poly_nors_dst)[3] = CustomData_get_layer(pdata_dst, CD_NORMAL);
332                 float (*loop_nors_dst)[3] = CustomData_get_layer(ldata_dst, CD_NORMAL);
333                 short (*custom_nors_dst)[2] = CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL);
334
335                 BLI_assert(poly_nors_dst);
336
337                 if (!changed) {
338                         return;
339                 }
340
341                 if (!custom_nors_dst) {
342                         custom_nors_dst = CustomData_add_layer(ldata_dst, CD_CUSTOMLOOPNORMAL, CD_CALLOC, NULL, num_loops_dst);
343                 }
344
345                 /* Note loop_nors_dst contains our custom normals as transferred from source... */
346                 BKE_mesh_normals_loop_custom_set(verts_dst, num_verts_dst, edges_dst, num_edges_dst,
347                                                  loops_dst, loop_nors_dst, num_loops_dst,
348                                                  polys_dst, poly_nors_dst, num_polys_dst,
349                                                  custom_nors_dst);
350         }
351 }
352
353 /* ********** */
354
355 static MeshRemapIslandsCalc data_transfer_get_loop_islands_generator(const int cddata_type)
356 {
357         switch (cddata_type) {
358                 case CD_FAKE_UV:
359                         return BKE_mesh_calc_islands_loop_poly_edgeseam;
360                 default:
361                         break;
362         }
363         return NULL;
364 }
365
366 float data_transfer_interp_float_do(
367         const int mix_mode, const float val_dst, const float val_src, const float mix_factor)
368 {
369         float val_ret;
370
371         if (((mix_mode == CDT_MIX_REPLACE_ABOVE_THRESHOLD && (val_dst < mix_factor)) ||
372              (mix_mode == CDT_MIX_REPLACE_BELOW_THRESHOLD && (val_dst > mix_factor))))
373         {
374                 return val_dst;  /* Do not affect destination. */
375         }
376
377         switch (mix_mode) {
378                 case CDT_MIX_REPLACE_ABOVE_THRESHOLD:
379                 case CDT_MIX_REPLACE_BELOW_THRESHOLD:
380                         return val_src;
381                 case CDT_MIX_MIX:
382                         val_ret = (val_dst + val_src) * 0.5f;
383                         break;
384                 case CDT_MIX_ADD:
385                         val_ret = val_dst + val_src;
386                         break;
387                 case CDT_MIX_SUB:
388                         val_ret = val_dst - val_src;
389                         break;
390                 case CDT_MIX_MUL:
391                         val_ret = val_dst * val_src;
392                         break;
393                 case CDT_MIX_TRANSFER:
394                 default:
395                         val_ret = val_src;
396                         break;
397         }
398         return interpf(val_ret, val_dst, mix_factor);
399 }
400
401 static void data_transfer_interp_char(
402         const CustomDataTransferLayerMap *laymap, void *dest,
403         const void **sources, const float *weights, const int count, const float mix_factor)
404 {
405         const char **data_src = (const char **)sources;
406         char *data_dst = (char *)dest;
407
408         const int mix_mode = laymap->mix_mode;
409         float val_src = 0.0f;
410         const float val_dst = (float)(*data_dst) / 255.0f;
411
412         int i;
413
414         for (i = count; i--;) {
415                 val_src += ((float)(*data_src[i]) / 255.0f) * weights[i];
416         }
417
418         val_src = data_transfer_interp_float_do(mix_mode, val_dst, val_src, mix_factor);
419
420         CLAMP(val_src, 0.0f, 1.0f);
421
422         *data_dst = (char)(val_src * 255.0f);
423 }
424
425 /* Helpers to match sources and destinations data layers (also handles 'conversions' in CD_FAKE cases). */
426
427 void data_transfer_layersmapping_add_item(
428         ListBase *r_map, const int cddata_type, const int mix_mode, const float mix_factor, const float *mix_weights,
429         const void *data_src, void *data_dst, const int data_src_n, const int data_dst_n,
430         const size_t elem_size, const size_t data_size, const size_t data_offset, const uint64_t data_flag,
431         cd_datatransfer_interp interp, void *interp_data)
432 {
433         CustomDataTransferLayerMap *item = MEM_mallocN(sizeof(*item), __func__);
434
435         BLI_assert(data_dst != NULL);
436
437         item->data_type = cddata_type;
438         item->mix_mode = mix_mode;
439         item->mix_factor = mix_factor;
440         item->mix_weights = mix_weights;
441
442         item->data_src = data_src;
443         item->data_dst = data_dst;
444         item->data_src_n = data_src_n;
445         item->data_dst_n = data_dst_n;
446         item->elem_size = elem_size;
447
448         item->data_size = data_size;
449         item->data_offset = data_offset;
450         item->data_flag = data_flag;
451
452         item->interp = interp;
453         item->interp_data = interp_data;
454
455         BLI_addtail(r_map, item);
456 }
457
458 static void data_transfer_layersmapping_add_item_cd(
459         ListBase *r_map, const int cddata_type, const int mix_mode, const float mix_factor, const float *mix_weights,
460         void *data_src, void *data_dst, cd_datatransfer_interp interp, void *interp_data)
461 {
462         uint64_t data_flag = 0;
463
464         if (cddata_type == CD_FREESTYLE_EDGE) {
465                 data_flag = FREESTYLE_EDGE_MARK;
466         }
467         else if (cddata_type == CD_FREESTYLE_FACE) {
468                 data_flag = FREESTYLE_FACE_MARK;
469         }
470
471         data_transfer_layersmapping_add_item(
472                 r_map, cddata_type, mix_mode, mix_factor, mix_weights, data_src, data_dst,
473                 0, 0, 0, 0, 0, data_flag, interp, interp_data);
474 }
475
476 /* Note: All those layer mapping handlers return false *only* if they were given invalid parameters.
477  *       This means that even if they do nothing, they will return true if all given parameters were OK.
478  *       Also, r_map may be NULL, in which case they will 'only' create/delete destination layers according
479  *       to given parameters.
480  */
481
482 static bool data_transfer_layersmapping_cdlayers_multisrc_to_dst(
483         ListBase *r_map, const int cddata_type, const int mix_mode, const float mix_factor, const float *mix_weights,
484         const int num_elem_dst, const bool use_create, const bool use_delete,
485         CustomData *cd_src, CustomData *cd_dst, const bool use_dupref_dst,
486         const int tolayers, bool *use_layers_src, const int num_layers_src,
487         cd_datatransfer_interp interp, void *interp_data)
488 {
489         void *data_src, *data_dst = NULL;
490         int idx_src = num_layers_src;
491         int idx_dst, tot_dst = CustomData_number_of_layers(cd_dst, cddata_type);
492         bool *data_dst_to_delete = NULL;
493
494         if (!use_layers_src) {
495                 /* No source at all, we can only delete all dest if requested... */
496                 if (use_delete) {
497                         idx_dst = tot_dst;
498                         while (idx_dst--) {
499                                 CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
500                         }
501                 }
502                 return true;
503         }
504
505         switch (tolayers) {
506                 case DT_LAYERS_INDEX_DST:
507                         idx_dst = tot_dst;
508
509                         /* Find last source actually used! */
510                         while (idx_src-- && !use_layers_src[idx_src]);
511                         idx_src++;
512
513                         if (idx_dst < idx_src) {
514                                 if (!use_create) {
515                                         return true;
516                                 }
517                                 /* Create as much data layers as necessary! */
518                                 for (; idx_dst < idx_src; idx_dst++) {
519                                         CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
520                                 }
521                         }
522                         else if (use_delete && idx_dst > idx_src) {
523                                 while (idx_dst-- > idx_src) {
524                                         CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
525                                 }
526                         }
527                         if (r_map) {
528                                 while (idx_src--) {
529                                         if (!use_layers_src[idx_src]) {
530                                                 continue;
531                                         }
532                                         data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
533                                         /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
534                                         if (use_dupref_dst) {
535                                                 data_dst = CustomData_duplicate_referenced_layer_n(cd_dst, cddata_type, idx_src, num_elem_dst);
536                                         }
537                                         else {
538                                                 data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_src);
539                                         }
540                                         data_transfer_layersmapping_add_item_cd(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
541                                                                                 data_src, data_dst, interp, interp_data);
542                                 }
543                         }
544                         break;
545                 case DT_LAYERS_NAME_DST:
546                         if (use_delete) {
547                                 if (tot_dst) {
548                                         data_dst_to_delete = MEM_mallocN(sizeof(*data_dst_to_delete) * (size_t)tot_dst, __func__);
549                                         memset(data_dst_to_delete, true, sizeof(*data_dst_to_delete) * (size_t)tot_dst);
550                                 }
551                         }
552
553                         while (idx_src--) {
554                                 const char *name;
555
556                                 if (!use_layers_src[idx_src]) {
557                                         continue;
558                                 }
559
560                                 name = CustomData_get_layer_name(cd_src, cddata_type, idx_src);
561                                 data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
562
563                                 if ((idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name)) == -1) {
564                                         if (!use_create) {
565                                                 if (r_map) {
566                                                         BLI_freelistN(r_map);
567                                                 }
568                                                 return true;
569                                         }
570                                         CustomData_add_layer_named(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst, name);
571                                         idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name);
572                                 }
573                                 else if (data_dst_to_delete) {
574                                         data_dst_to_delete[idx_dst] = false;
575                                 }
576                                 if (r_map) {
577                                         /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
578                                         if (use_dupref_dst) {
579                                                 data_dst = CustomData_duplicate_referenced_layer_n(cd_dst, cddata_type, idx_dst, num_elem_dst);
580                                         }
581                                         else {
582                                                 data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
583                                         }
584                                         data_transfer_layersmapping_add_item_cd(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
585                                                                                 data_src, data_dst, interp, interp_data);
586                                 }
587                         }
588
589                         if (data_dst_to_delete) {
590                                 /* Note: This won't affect newly created layers, if any, since tot_dst has not been updated!
591                                  *       Also, looping backward ensures us we do not suffer from index shifting when deleting a layer.
592                                  */
593                                 for (idx_dst = tot_dst; idx_dst--;) {
594                                         if (data_dst_to_delete[idx_dst]) {
595                                                 CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
596                                         }
597                                 }
598
599                                 MEM_freeN(data_dst_to_delete);
600                         }
601                         break;
602                 default:
603                         return false;
604         }
605
606         return true;
607 }
608
609 static bool data_transfer_layersmapping_cdlayers(
610         ListBase *r_map, const int cddata_type, const int mix_mode, const float mix_factor, const float *mix_weights,
611         const int num_elem_dst, const bool use_create, const bool use_delete,
612         CustomData *cd_src, CustomData *cd_dst, const bool use_dupref_dst,
613         const int fromlayers, const int tolayers,
614         cd_datatransfer_interp interp, void *interp_data)
615 {
616         int idx_src, idx_dst;
617         void *data_src, *data_dst = NULL;
618
619         if (CustomData_layertype_is_singleton(cddata_type)) {
620                 if (!(data_src = CustomData_get_layer(cd_src, cddata_type))) {
621                         if (use_delete) {
622                                 CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, 0);
623                         }
624                         return true;
625                 }
626
627                 data_dst = CustomData_get_layer(cd_dst, cddata_type);
628                 if (!data_dst) {
629                         if (!use_create) {
630                                 return true;
631                         }
632                         data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
633                 }
634                 else if (use_dupref_dst && r_map) {
635                         /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
636                         data_dst = CustomData_duplicate_referenced_layer(cd_dst, cddata_type, num_elem_dst);
637                 }
638
639                 if (r_map) {
640                         data_transfer_layersmapping_add_item_cd(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
641                                                                 data_src, data_dst, interp, interp_data);
642                 }
643         }
644         else if (fromlayers == DT_LAYERS_ACTIVE_SRC || fromlayers >= 0) {
645                 /* Note: use_delete has not much meaning in this case, ignored. */
646
647                 if (fromlayers >= 0) {  /* Real-layer index */
648                         idx_src = fromlayers;
649                 }
650                 else {
651                         if ((idx_src = CustomData_get_active_layer(cd_src, cddata_type)) == -1) {
652                                 return true;
653                         }
654                 }
655                 data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
656                 if (!data_src) {
657                         return true;
658                 }
659
660                 if (tolayers >= 0) {  /* Real-layer index */
661                         idx_dst = tolayers;
662                         /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
663                         if (use_dupref_dst && r_map) {
664                                 data_dst = CustomData_duplicate_referenced_layer_n(cd_dst, cddata_type, idx_dst, num_elem_dst);
665                         }
666                         else {
667                                 data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
668                         }
669                 }
670                 else if (tolayers == DT_LAYERS_ACTIVE_DST) {
671                         if ((idx_dst = CustomData_get_active_layer(cd_dst, cddata_type)) == -1) {
672                                 if (!use_create) {
673                                         return true;
674                                 }
675                                 data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
676                         }
677                         else {
678                                 /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
679                                 if (use_dupref_dst && r_map) {
680                                         data_dst = CustomData_duplicate_referenced_layer_n(cd_dst, cddata_type, idx_dst, num_elem_dst);
681                                 }
682                                 else {
683                                         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
684                                 }
685                         }
686                 }
687                 else if (tolayers == DT_LAYERS_INDEX_DST) {
688                         int num = CustomData_number_of_layers(cd_dst, cddata_type);
689                         idx_dst = idx_src;
690                         if (num <= idx_dst) {
691                                 if (!use_create) {
692                                         return true;
693                                 }
694                                 /* Create as much data layers as necessary! */
695                                 for (; num <= idx_dst; num++) {
696                                         CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
697                                 }
698                         }
699                         /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
700                         if (use_dupref_dst && r_map) {
701                                 data_dst = CustomData_duplicate_referenced_layer_n(cd_dst, cddata_type, idx_dst, num_elem_dst);
702                         }
703                         else {
704                                 data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
705                         }
706                 }
707                 else if (tolayers == DT_LAYERS_NAME_DST) {
708                         const char *name = CustomData_get_layer_name(cd_src, cddata_type, idx_src);
709                         if ((idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name)) == -1) {
710                                 if (!use_create) {
711                                         return true;
712                                 }
713                                 CustomData_add_layer_named(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst, name);
714                                 idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name);
715                         }
716                         /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
717                         if (use_dupref_dst && r_map) {
718                                 data_dst = CustomData_duplicate_referenced_layer_n(cd_dst, cddata_type, idx_dst, num_elem_dst);
719                         }
720                         else {
721                                 data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
722                         }
723                 }
724                 else {
725                         return false;
726                 }
727
728                 if (!data_dst) {
729                         return false;
730                 }
731
732                 if (r_map) {
733                         data_transfer_layersmapping_add_item_cd(
734                                 r_map, cddata_type, mix_mode, mix_factor, mix_weights, data_src, data_dst, interp, interp_data);
735                 }
736         }
737         else if (fromlayers == DT_LAYERS_ALL_SRC) {
738                 int num_src = CustomData_number_of_layers(cd_src, cddata_type);
739                 bool *use_layers_src = num_src ? MEM_mallocN(sizeof(*use_layers_src) * (size_t)num_src, __func__) : NULL;
740                 bool ret;
741
742                 if (use_layers_src) {
743                         memset(use_layers_src, true, sizeof(*use_layers_src) * num_src);
744                 }
745
746                 ret = data_transfer_layersmapping_cdlayers_multisrc_to_dst(
747                         r_map, cddata_type, mix_mode, mix_factor, mix_weights,
748                         num_elem_dst, use_create, use_delete, cd_src, cd_dst, use_dupref_dst,
749                         tolayers, use_layers_src, num_src,
750                         interp, interp_data);
751
752                 if (use_layers_src) {
753                         MEM_freeN(use_layers_src);
754                 }
755                 return ret;
756         }
757         else {
758                 return false;
759         }
760
761         return true;
762 }
763
764 static bool data_transfer_layersmapping_generate(
765         ListBase *r_map, Object *ob_src, Object *ob_dst, DerivedMesh *dm_src, DerivedMesh *dm_dst, Mesh *me_dst,
766         const int elem_type, int cddata_type, int mix_mode, float mix_factor, const float *mix_weights,
767         const int num_elem_dst, const bool use_create, const bool use_delete, const int fromlayers, const int tolayers,
768         SpaceTransform *space_transform)
769 {
770         CustomData *cd_src, *cd_dst;
771
772         cd_datatransfer_interp interp = NULL;
773         void *interp_data = NULL;
774
775         if (elem_type == ME_VERT) {
776                 if (!(cddata_type & CD_FAKE)) {
777                         cd_src = dm_src->getVertDataLayout(dm_src);
778                         cd_dst = dm_dst ? dm_dst->getVertDataLayout(dm_dst) : &me_dst->vdata;
779
780                         if (!data_transfer_layersmapping_cdlayers(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
781                                                                   num_elem_dst, use_create, use_delete,
782                                                                   cd_src, cd_dst, dm_dst != NULL,
783                                                                   fromlayers, tolayers,
784                                                                   interp, interp_data))
785                         {
786                                 /* We handle specific source selection cases here. */
787                                 return false;
788                         }
789                         return true;
790                 }
791                 else if (cddata_type == CD_FAKE_BWEIGHT) {
792                         const size_t elem_size = sizeof(*((MVert *)NULL));
793                         const size_t data_size = sizeof(((MVert *)NULL)->bweight);
794                         const size_t data_offset = offsetof(MVert, bweight);
795                         const uint64_t data_flag = 0;
796
797                         if (!(dm_src->cd_flag & ME_CDFLAG_VERT_BWEIGHT)) {
798                                 if (use_delete && !dm_dst) {
799                                         me_dst->cd_flag &= ~ME_CDFLAG_VERT_BWEIGHT;
800                                 }
801                                 return true;
802                         }
803                         if (dm_dst) {
804                                 dm_dst->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
805                         }
806                         else {
807                                 me_dst->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
808                         }
809                         if (r_map) {
810                                 data_transfer_layersmapping_add_item(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
811                                                                      dm_src->getVertArray(dm_src),
812                                                                      dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert,
813                                                                      dm_src->getNumVerts(dm_src),
814                                                                      dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert,
815                                                                      elem_size, data_size, data_offset, data_flag,
816                                                                      data_transfer_interp_char, interp_data);
817                         }
818                         return true;
819                 }
820                 else if (cddata_type == CD_FAKE_MDEFORMVERT) {
821                         bool ret;
822
823                         cd_src = dm_src->getVertDataLayout(dm_src);
824                         cd_dst = dm_dst ? dm_dst->getVertDataLayout(dm_dst) : &me_dst->vdata;
825
826                         ret = data_transfer_layersmapping_vgroups(r_map, mix_mode, mix_factor, mix_weights,
827                                                                   num_elem_dst, use_create, use_delete,
828                                                                   ob_src, ob_dst, cd_src, cd_dst, dm_dst != NULL,
829                                                                   fromlayers, tolayers);
830
831                         /* Mesh stores its dvert in a specific pointer too. :( */
832                         me_dst->dvert = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
833                         return ret;
834                 }
835                 else if (cddata_type == CD_FAKE_SHAPEKEY) {
836                         /* TODO: leaving shapekeys asside for now, quite specific case, since we can't access them from MVert :/ */
837                         return false;
838                 }
839         }
840         else if (elem_type == ME_EDGE) {
841                 if (!(cddata_type & CD_FAKE)) {  /* Unused for edges, currently... */
842                         cd_src = dm_src->getEdgeDataLayout(dm_src);
843                         cd_dst = dm_dst ? dm_dst->getEdgeDataLayout(dm_dst) : &me_dst->edata;
844
845                         if (!data_transfer_layersmapping_cdlayers(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
846                                                                   num_elem_dst, use_create, use_delete,
847                                                                   cd_src, cd_dst, dm_dst != NULL,
848                                                                   fromlayers, tolayers,
849                                                                   interp, interp_data))
850                         {
851                                 /* We handle specific source selection cases here. */
852                                 return false;
853                         }
854                         return true;
855                 }
856                 else if (cddata_type == CD_FAKE_CREASE) {
857                         const size_t elem_size = sizeof(*((MEdge *)NULL));
858                         const size_t data_size = sizeof(((MEdge *)NULL)->crease);
859                         const size_t data_offset = offsetof(MEdge, crease);
860                         const uint64_t data_flag = 0;
861
862                         if (!(dm_src->cd_flag & ME_CDFLAG_EDGE_CREASE)) {
863                                 if (use_delete && !dm_dst) {
864                                         me_dst->cd_flag &= ~ME_CDFLAG_EDGE_CREASE;
865                                 }
866                                 return true;
867                         }
868                         if (dm_dst) {
869                                 dm_dst->cd_flag |= ME_CDFLAG_EDGE_CREASE;
870                         }
871                         else {
872                                 me_dst->cd_flag |= ME_CDFLAG_EDGE_CREASE;
873                         }
874                         if (r_map) {
875                                 data_transfer_layersmapping_add_item(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
876                                                                      dm_src->getEdgeArray(dm_src),
877                                                                      dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge,
878                                                                      dm_src->getNumEdges(dm_src),
879                                                                      dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge,
880                                                                      elem_size, data_size, data_offset, data_flag,
881                                                                      data_transfer_interp_char, interp_data);
882                         }
883                         return true;
884                 }
885                 else if (cddata_type == CD_FAKE_BWEIGHT) {
886                         const size_t elem_size = sizeof(*((MEdge *)NULL));
887                         const size_t data_size = sizeof(((MEdge *)NULL)->bweight);
888                         const size_t data_offset = offsetof(MEdge, bweight);
889                         const uint64_t data_flag = 0;
890
891                         if (!(dm_src->cd_flag & ME_CDFLAG_EDGE_BWEIGHT)) {
892                                 if (use_delete && !dm_dst) {
893                                         me_dst->cd_flag &= ~ME_CDFLAG_EDGE_BWEIGHT;
894                                 }
895                                 return true;
896                         }
897                         if (dm_dst) {
898                                 dm_dst->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
899                         }
900                         else {
901                                 me_dst->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
902                         }
903                         if (r_map) {
904                                 data_transfer_layersmapping_add_item(r_map, cddata_type, mix_mode, mix_factor, mix_weights,
905                                                                      dm_src->getEdgeArray(dm_src),
906                                                                      dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge,
907                                                                      dm_src->getNumEdges(dm_src),
908                                                                      dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge,
909                                                                      elem_size, data_size, data_offset, data_flag,
910                                                                      data_transfer_interp_char, interp_data);
911                         }
912                         return true;
913                 }
914                 else if (r_map && ELEM(cddata_type, CD_FAKE_SHARP, CD_FAKE_SEAM)) {
915                         const size_t elem_size = sizeof(*((MEdge *)NULL));
916                         const size_t data_size = sizeof(((MEdge *)NULL)->flag);
917                         const size_t data_offset = offsetof(MEdge, flag);
918                         const uint64_t data_flag = (cddata_type == CD_FAKE_SHARP) ? ME_SHARP : ME_SEAM;
919
920                         data_transfer_layersmapping_add_item(
921                                 r_map, cddata_type, mix_mode, mix_factor, mix_weights,
922                                 dm_src->getEdgeArray(dm_src),
923                                 dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge,
924                                 dm_src->getNumEdges(dm_src),
925                                 dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge,
926                                 elem_size, data_size, data_offset, data_flag, NULL, interp_data);
927                         return true;
928                 }
929                 else {
930                         return false;
931                 }
932         }
933         else if (elem_type == ME_LOOP) {
934                 if (cddata_type == CD_FAKE_UV) {
935                         cddata_type = CD_MLOOPUV;
936                 }
937                 else if (cddata_type == CD_FAKE_LNOR) {
938                         /* Preprocess should have generated it, Postprocess will convert it back to CD_CUSTOMLOOPNORMAL. */
939                         cddata_type = CD_NORMAL;
940                         interp_data = space_transform;
941                         interp = customdata_data_transfer_interp_normal_normals;
942                 }
943
944                 if (!(cddata_type & CD_FAKE)) {
945                         cd_src = dm_src->getLoopDataLayout(dm_src);
946                         cd_dst = dm_dst ? dm_dst->getLoopDataLayout(dm_dst) : &me_dst->ldata;
947
948                         if (!data_transfer_layersmapping_cdlayers(
949                                 r_map, cddata_type, mix_mode, mix_factor, mix_weights,
950                                 num_elem_dst, use_create, use_delete, cd_src, cd_dst, dm_dst != NULL,
951                                 fromlayers, tolayers,
952                                 interp, interp_data))
953                         {
954                                 /* We handle specific source selection cases here. */
955                                 return false;
956                         }
957                         return true;
958                 }
959                 else {
960                         return false;
961                 }
962         }
963         else if (elem_type == ME_POLY) {
964                 if (cddata_type == CD_FAKE_UV) {
965                         cddata_type = CD_MLOOPUV;
966                 }
967
968                 if (!(cddata_type & CD_FAKE)) {
969                         cd_src = dm_src->getPolyDataLayout(dm_src);
970                         cd_dst = dm_dst ? dm_dst->getPolyDataLayout(dm_dst) : &me_dst->pdata;
971
972                         if (!data_transfer_layersmapping_cdlayers(
973                                 r_map, cddata_type, mix_mode, mix_factor, mix_weights,
974                                 num_elem_dst, use_create, use_delete, cd_src, cd_dst, dm_dst != NULL,
975                                 fromlayers, tolayers,
976                                 interp, interp_data))
977                         {
978                                 /* We handle specific source selection cases here. */
979                                 return false;
980                         }
981                         return true;
982                 }
983                 else if (r_map && cddata_type == CD_FAKE_SHARP) {
984                         const size_t elem_size = sizeof(*((MPoly *)NULL));
985                         const size_t data_size = sizeof(((MPoly *)NULL)->flag);
986                         const size_t data_offset = offsetof(MPoly, flag);
987                         const uint64_t data_flag = ME_SMOOTH;
988
989                         data_transfer_layersmapping_add_item(
990                                 r_map, cddata_type, mix_mode, mix_factor, mix_weights,
991                                 dm_src->getPolyArray(dm_src),
992                                 dm_dst ? dm_dst->getPolyArray(dm_dst) : me_dst->mpoly,
993                                 dm_src->getNumPolys(dm_src),
994                                 dm_dst ? dm_dst->getNumPolys(dm_dst) : me_dst->totpoly,
995                                 elem_size, data_size, data_offset, data_flag, NULL, interp_data);
996                         return true;
997                 }
998                 else {
999                         return false;
1000                 }
1001         }
1002
1003         return false;
1004 }
1005
1006 /**
1007  * Transfer data *layout* of selected types from source to destination object.
1008  * By default, it only creates new data layers if needed on \a ob_dst.
1009  * If \a use_delete is true, it will also delete data layers on \a ob_dst that do not match those from \a ob_src,
1010  * to get (as much as possible) exact copy of source data layout.
1011  */
1012 void BKE_object_data_transfer_layout(
1013         const struct EvaluationContext *eval_ctx, Scene *scene,
1014         Object *ob_src, Object *ob_dst, const int data_types, const bool use_delete,
1015         const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX])
1016 {
1017         DerivedMesh *dm_src;
1018         Mesh *me_dst;
1019         int i;
1020
1021         const bool use_create = true;  /* We always create needed layers here. */
1022
1023         CustomDataMask dm_src_mask = CD_MASK_BAREMESH;
1024
1025         BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1026
1027         me_dst = ob_dst->data;
1028
1029         /* Get source DM.*/
1030         dm_src_mask |= BKE_object_data_transfer_dttypes_to_cdmask(data_types);
1031         dm_src = mesh_get_derived_final(eval_ctx, scene, ob_src, dm_src_mask);
1032         if (!dm_src) {
1033                 return;
1034         }
1035
1036         /* Check all possible data types. */
1037         for (i = 0; i < DT_TYPE_MAX; i++) {
1038                 const int dtdata_type = 1 << i;
1039                 int cddata_type;
1040                 int fromlayers, tolayers, fromto_idx;
1041
1042                 if (!(data_types & dtdata_type)) {
1043                         continue;
1044                 }
1045
1046                 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1047
1048                 fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1049                 if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1050                         fromlayers = fromlayers_select[fromto_idx];
1051                         tolayers = tolayers_select[fromto_idx];
1052                 }
1053                 else {
1054                         fromlayers = tolayers = 0;
1055                 }
1056
1057                 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1058                         const int num_elem_dst = me_dst->totvert;
1059
1060                         data_transfer_layersmapping_generate(
1061                                 NULL, ob_src, ob_dst, dm_src, NULL, me_dst, ME_VERT, cddata_type, 0, 0.0f, NULL,
1062                                 num_elem_dst, use_create, use_delete, fromlayers, tolayers, NULL);
1063                 }
1064                 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1065                         const int num_elem_dst = me_dst->totedge;
1066
1067                         data_transfer_layersmapping_generate(
1068                                 NULL, ob_src, ob_dst, dm_src, NULL, me_dst, ME_EDGE, cddata_type, 0, 0.0f, NULL,
1069                                 num_elem_dst, use_create, use_delete, fromlayers, tolayers, NULL);
1070                 }
1071                 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1072                         const int num_elem_dst = me_dst->totloop;
1073
1074                         data_transfer_layersmapping_generate(
1075                                 NULL, ob_src, ob_dst, dm_src, NULL, me_dst, ME_LOOP, cddata_type, 0, 0.0f, NULL,
1076                                 num_elem_dst, use_create, use_delete, fromlayers, tolayers, NULL);
1077                 }
1078                 if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1079                         const int num_elem_dst = me_dst->totpoly;
1080
1081                         data_transfer_layersmapping_generate(
1082                                 NULL, ob_src, ob_dst, dm_src, NULL, me_dst, ME_POLY, cddata_type, 0, 0.0f, NULL,
1083                                 num_elem_dst, use_create, use_delete, fromlayers, tolayers, NULL);
1084                 }
1085         }
1086 }
1087
1088 bool BKE_object_data_transfer_dm(
1089         const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob_src, Object *ob_dst, DerivedMesh *dm_dst,
1090         const int data_types, bool use_create, const int map_vert_mode, const int map_edge_mode,
1091         const int map_loop_mode, const int map_poly_mode, SpaceTransform *space_transform, const bool auto_transform,
1092         const float max_distance, const float ray_radius, const float islands_handling_precision,
1093         const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1094         const int mix_mode, const float mix_factor, const char *vgroup_name, const bool invert_vgroup,
1095         ReportList *reports)
1096 {
1097 #define VDATA 0
1098 #define EDATA 1
1099 #define LDATA 2
1100 #define PDATA 3
1101 #define DATAMAX 4
1102
1103         SpaceTransform auto_space_transform;
1104
1105         DerivedMesh *dm_src;
1106         Mesh *me_dst, *me_src;
1107         bool dirty_nors_dst = true;  /* Assumed always true if not using a dm as destination. */
1108         int i;
1109
1110         MDeformVert *mdef = NULL;
1111         int vg_idx = -1;
1112         float *weights[DATAMAX] = {NULL};
1113
1114         MeshPairRemap geom_map[DATAMAX] = {{0}};
1115         bool geom_map_init[DATAMAX] = {0};
1116         ListBase lay_map = {NULL};
1117         bool changed = false;
1118
1119         const bool use_delete = false;  /* We never delete data layers from destination here. */
1120
1121         CustomDataMask dm_src_mask = CD_MASK_BAREMESH;
1122
1123         BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1124
1125         me_dst = ob_dst->data;
1126         me_src = ob_src->data;
1127         if (dm_dst) {
1128                 dirty_nors_dst = (dm_dst->dirty & DM_DIRTY_NORMALS) != 0;
1129                 use_create = false;  /* Never create needed custom layers on DM (modifier case). */
1130         }
1131
1132         if (vgroup_name) {
1133                 if (dm_dst) {
1134                         mdef = dm_dst->getVertDataArray(dm_dst, CD_MDEFORMVERT);
1135                 }
1136                 else {
1137                         mdef = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
1138                 }
1139                 if (mdef) {
1140                         vg_idx = defgroup_name_index(ob_dst, vgroup_name);
1141                 }
1142         }
1143
1144         /* Get source DM.*/
1145         dm_src_mask |= BKE_object_data_transfer_dttypes_to_cdmask(data_types);
1146         /* XXX Hack! In case this is being evaluated from dm stack, we cannot compute final dm,
1147          *     can lead to infinite recursion in case of dependency cycles of DataTransfer modifiers...
1148          *     Issue is, this means we cannot be sure to have requested cd layers in source.
1149          *
1150          *     Also, we need to make a local copy of dm_src, otherwise we may end with concurrent creation
1151          *     of data in it (multi-threaded evaluation of the modifier stack, see T46672).
1152          */
1153         dm_src = dm_dst ? ob_src->derivedFinal : mesh_get_derived_final(eval_ctx, scene, ob_src, dm_src_mask);
1154         if (!dm_src) {
1155                 return changed;
1156         }
1157         dm_src = CDDM_copy(dm_src);
1158
1159         if (auto_transform) {
1160                 MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
1161                 const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
1162
1163                 if (space_transform == NULL) {
1164                         space_transform = &auto_space_transform;
1165                 }
1166
1167                 BKE_mesh_remap_find_best_match_from_dm(verts_dst, num_verts_dst, dm_src, space_transform);
1168         }
1169
1170         /* Check all possible data types.
1171          * Note item mappings and dest mix weights are cached. */
1172         for (i = 0; i < DT_TYPE_MAX; i++) {
1173                 const int dtdata_type = 1 << i;
1174                 int cddata_type;
1175                 int fromlayers, tolayers, fromto_idx;
1176
1177                 if (!(data_types & dtdata_type)) {
1178                         continue;
1179                 }
1180
1181                 data_transfer_dtdata_type_preprocess(ob_src, ob_dst, dm_src, dm_dst, me_dst,
1182                                                      dtdata_type, dirty_nors_dst,
1183                                                      (me_src->flag & ME_AUTOSMOOTH) != 0, me_src->smoothresh);
1184
1185                 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1186
1187                 fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1188                 if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1189                         fromlayers = fromlayers_select[fromto_idx];
1190                         tolayers = tolayers_select[fromto_idx];
1191                 }
1192                 else {
1193                         fromlayers = tolayers = 0;
1194                 }
1195
1196                 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1197                         MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
1198                         const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
1199
1200                         if (!geom_map_init[VDATA]) {
1201                                 const int num_verts_src = dm_src->getNumVerts(dm_src);
1202
1203                                 if ((map_vert_mode == MREMAP_MODE_TOPOLOGY) && (num_verts_dst != num_verts_src)) {
1204                                         BKE_report(reports, RPT_ERROR,
1205                                                    "Source and destination meshes do not have the same amount of vertices, "
1206                                                    "'Topology' mapping cannot be used in this case");
1207                                         continue;
1208                                 }
1209                                 if ((map_vert_mode & MREMAP_USE_EDGE) && (dm_src->getNumEdges(dm_src) == 0)) {
1210                                         BKE_report(reports, RPT_ERROR,
1211                                                    "Source mesh doesn't have any edges, "
1212                                                    "None of the 'Edge' mappings can be used in this case");
1213                                         continue;
1214                                 }
1215                                 if ((map_vert_mode & MREMAP_USE_POLY) && (dm_src->getNumPolys(dm_src) == 0)) {
1216                                         BKE_report(reports, RPT_ERROR,
1217                                                    "Source mesh doesn't have any faces, "
1218                                                    "None of the 'Face' mappings can be used in this case");
1219                                         continue;
1220                                 }
1221                                 if (ELEM(0, num_verts_dst, num_verts_src)) {
1222                                         BKE_report(reports, RPT_ERROR,
1223                                                    "Source or destination meshes do not have any vertices, cannot transfer vertex data");
1224                                         continue;
1225                                 }
1226
1227                                 BKE_mesh_remap_calc_verts_from_dm(
1228                                         map_vert_mode, space_transform, max_distance, ray_radius,
1229                                         verts_dst, num_verts_dst, dirty_nors_dst, dm_src, &geom_map[VDATA]);
1230                                 geom_map_init[VDATA] = true;
1231                         }
1232
1233                         if (mdef && vg_idx != -1 && !weights[VDATA]) {
1234                                 weights[VDATA] = MEM_mallocN(sizeof(*(weights[VDATA])) * (size_t)num_verts_dst, __func__);
1235                                 BKE_defvert_extract_vgroup_to_vertweights(mdef, vg_idx, num_verts_dst, weights[VDATA], invert_vgroup);
1236                         }
1237
1238                         if (data_transfer_layersmapping_generate(
1239                                 &lay_map, ob_src, ob_dst, dm_src, dm_dst, me_dst, ME_VERT,
1240                                 cddata_type, mix_mode, mix_factor, weights[VDATA],
1241                                 num_verts_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1242                         {
1243                                 CustomDataTransferLayerMap *lay_mapit;
1244
1245                                 changed = (lay_map.first != NULL);
1246
1247                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1248                                         CustomData_data_transfer(&geom_map[VDATA], lay_mapit);
1249                                 }
1250
1251                                 BLI_freelistN(&lay_map);
1252                         }
1253                 }
1254                 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1255                         MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
1256                         const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
1257                         MEdge *edges_dst = dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge;
1258                         const int num_edges_dst = dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge;
1259
1260                         if (!geom_map_init[EDATA]) {
1261                                 const int num_edges_src = dm_src->getNumEdges(dm_src);
1262
1263                                 if ((map_edge_mode == MREMAP_MODE_TOPOLOGY) && (num_edges_dst != num_edges_src)) {
1264                                         BKE_report(reports, RPT_ERROR,
1265                                                    "Source and destination meshes do not have the same amount of edges, "
1266                                                    "'Topology' mapping cannot be used in this case");
1267                                         continue;
1268                                 }
1269                                 if ((map_edge_mode & MREMAP_USE_POLY) && (dm_src->getNumPolys(dm_src) == 0)) {
1270                                         BKE_report(reports, RPT_ERROR,
1271                                                    "Source mesh doesn't have any faces, "
1272                                                    "None of the 'Face' mappings can be used in this case");
1273                                         continue;
1274                                 }
1275                                 if (ELEM(0, num_edges_dst, num_edges_src)) {
1276                                         BKE_report(reports, RPT_ERROR,
1277                                                    "Source or destination meshes do not have any edges, cannot transfer edge data");
1278                                         continue;
1279                                 }
1280
1281                                 BKE_mesh_remap_calc_edges_from_dm(
1282                                         map_edge_mode, space_transform, max_distance, ray_radius,
1283                                         verts_dst, num_verts_dst, edges_dst, num_edges_dst, dirty_nors_dst,
1284                                         dm_src, &geom_map[EDATA]);
1285                                 geom_map_init[EDATA] = true;
1286                         }
1287
1288                         if (mdef && vg_idx != -1 && !weights[EDATA]) {
1289                                 weights[EDATA] = MEM_mallocN(sizeof(*weights[EDATA]) * (size_t)num_edges_dst, __func__);
1290                                 BKE_defvert_extract_vgroup_to_edgeweights(
1291                                         mdef, vg_idx, num_verts_dst, edges_dst, num_edges_dst,
1292                                         weights[EDATA], invert_vgroup);
1293                         }
1294
1295                         if (data_transfer_layersmapping_generate(
1296                                 &lay_map, ob_src, ob_dst, dm_src, dm_dst, me_dst, ME_EDGE,
1297                                 cddata_type, mix_mode, mix_factor, weights[EDATA],
1298                                 num_edges_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1299                         {
1300                                 CustomDataTransferLayerMap *lay_mapit;
1301
1302                                 changed = (lay_map.first != NULL);
1303
1304                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1305                                         CustomData_data_transfer(&geom_map[EDATA], lay_mapit);
1306                                 }
1307
1308                                 BLI_freelistN(&lay_map);
1309                         }
1310                 }
1311                 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1312                         MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
1313                         const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
1314                         MEdge *edges_dst = dm_dst ? dm_dst->getEdgeArray(dm_dst) : me_dst->medge;
1315                         const int num_edges_dst = dm_dst ? dm_dst->getNumEdges(dm_dst) : me_dst->totedge;
1316                         MPoly *polys_dst = dm_dst ? dm_dst->getPolyArray(dm_dst) : me_dst->mpoly;
1317                         const int num_polys_dst = dm_dst ? dm_dst->getNumPolys(dm_dst) : me_dst->totpoly;
1318                         MLoop *loops_dst = dm_dst ? dm_dst->getLoopArray(dm_dst) : me_dst->mloop;
1319                         const int num_loops_dst = dm_dst ? dm_dst->getNumLoops(dm_dst) : me_dst->totloop;
1320                         CustomData *pdata_dst = dm_dst ? dm_dst->getPolyDataLayout(dm_dst) : &me_dst->pdata;
1321                         CustomData *ldata_dst = dm_dst ? dm_dst->getLoopDataLayout(dm_dst) : &me_dst->ldata;
1322
1323                         MeshRemapIslandsCalc island_callback = data_transfer_get_loop_islands_generator(cddata_type);
1324
1325                         if (!geom_map_init[LDATA]) {
1326                                 const int num_loops_src = dm_src->getNumLoops(dm_src);
1327
1328                                 if ((map_loop_mode == MREMAP_MODE_TOPOLOGY) && (num_loops_dst != num_loops_src)) {
1329                                         BKE_report(reports, RPT_ERROR,
1330                                                    "Source and destination meshes do not have the same amount of face corners, "
1331                                                    "'Topology' mapping cannot be used in this case");
1332                                         continue;
1333                                 }
1334                                 if ((map_loop_mode & MREMAP_USE_EDGE) && (dm_src->getNumEdges(dm_src) == 0)) {
1335                                         BKE_report(reports, RPT_ERROR,
1336                                                    "Source mesh doesn't have any edges, "
1337                                                    "None of the 'Edge' mappings can be used in this case");
1338                                         continue;
1339                                 }
1340                                 if (ELEM(0, num_loops_dst, num_loops_src)) {
1341                                         BKE_report(reports, RPT_ERROR,
1342                                                    "Source or destination meshes do not have any faces, cannot transfer corner data");
1343                                         continue;
1344                                 }
1345
1346                                 BKE_mesh_remap_calc_loops_from_dm(
1347                                         map_loop_mode, space_transform, max_distance, ray_radius,
1348                                         verts_dst, num_verts_dst, edges_dst, num_edges_dst,
1349                                         loops_dst, num_loops_dst, polys_dst, num_polys_dst,
1350                                         ldata_dst, pdata_dst,
1351                                         (me_dst->flag & ME_AUTOSMOOTH) != 0, me_dst->smoothresh, dirty_nors_dst,
1352                                         dm_src, (me_src->flag & ME_AUTOSMOOTH) != 0, me_src->smoothresh,
1353                                         island_callback, islands_handling_precision, &geom_map[LDATA]);
1354                                 geom_map_init[LDATA] = true;
1355                         }
1356
1357                         if (mdef && vg_idx != -1 && !weights[LDATA]) {
1358                                 weights[LDATA] = MEM_mallocN(sizeof(*weights[LDATA]) * (size_t)num_loops_dst, __func__);
1359                                 BKE_defvert_extract_vgroup_to_loopweights(
1360                                         mdef, vg_idx, num_verts_dst, loops_dst, num_loops_dst,
1361                                         weights[LDATA], invert_vgroup);
1362                         }
1363
1364                         if (data_transfer_layersmapping_generate(
1365                                 &lay_map, ob_src, ob_dst, dm_src, dm_dst, me_dst, ME_LOOP,
1366                                 cddata_type, mix_mode, mix_factor, weights[LDATA],
1367                                 num_loops_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1368                         {
1369                                 CustomDataTransferLayerMap *lay_mapit;
1370
1371                                 changed = (lay_map.first != NULL);
1372
1373                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1374                                         CustomData_data_transfer(&geom_map[LDATA], lay_mapit);
1375                                 }
1376
1377                                 BLI_freelistN(&lay_map);
1378                         }
1379                 }
1380                 if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1381                         MVert *verts_dst = dm_dst ? dm_dst->getVertArray(dm_dst) : me_dst->mvert;
1382                         const int num_verts_dst = dm_dst ? dm_dst->getNumVerts(dm_dst) : me_dst->totvert;
1383                         MPoly *polys_dst = dm_dst ? dm_dst->getPolyArray(dm_dst) : me_dst->mpoly;
1384                         const int num_polys_dst = dm_dst ? dm_dst->getNumPolys(dm_dst) : me_dst->totpoly;
1385                         MLoop *loops_dst = dm_dst ? dm_dst->getLoopArray(dm_dst) : me_dst->mloop;
1386                         const int num_loops_dst = dm_dst ? dm_dst->getNumLoops(dm_dst) : me_dst->totloop;
1387                         CustomData *pdata_dst = dm_dst ? dm_dst->getPolyDataLayout(dm_dst) : &me_dst->pdata;
1388
1389                         if (!geom_map_init[PDATA]) {
1390                                 const int num_polys_src = dm_src->getNumPolys(dm_src);
1391
1392                                 if ((map_poly_mode == MREMAP_MODE_TOPOLOGY) && (num_polys_dst != num_polys_src)) {
1393                                         BKE_report(reports, RPT_ERROR,
1394                                                    "Source and destination meshes do not have the same amount of faces, "
1395                                                    "'Topology' mapping cannot be used in this case");
1396                                         continue;
1397                                 }
1398                                 if ((map_poly_mode & MREMAP_USE_EDGE) && (dm_src->getNumEdges(dm_src) == 0)) {
1399                                         BKE_report(reports, RPT_ERROR,
1400                                                    "Source mesh doesn't have any edges, "
1401                                                    "None of the 'Edge' mappings can be used in this case");
1402                                         continue;
1403                                 }
1404                                 if (ELEM(0, num_polys_dst, num_polys_src)) {
1405                                         BKE_report(reports, RPT_ERROR,
1406                                                    "Source or destination meshes do not have any faces, cannot transfer face data");
1407                                         continue;
1408                                 }
1409
1410                                 BKE_mesh_remap_calc_polys_from_dm(
1411                                         map_poly_mode, space_transform, max_distance, ray_radius,
1412                                         verts_dst, num_verts_dst, loops_dst, num_loops_dst,
1413                                         polys_dst, num_polys_dst, pdata_dst, dirty_nors_dst,
1414                                         dm_src, &geom_map[PDATA]);
1415                                 geom_map_init[PDATA] = true;
1416                         }
1417
1418                         if (mdef && vg_idx != -1 && !weights[PDATA]) {
1419                                 weights[PDATA] = MEM_mallocN(sizeof(*weights[PDATA]) * (size_t)num_polys_dst, __func__);
1420                                 BKE_defvert_extract_vgroup_to_polyweights(
1421                                         mdef, vg_idx, num_verts_dst, loops_dst, num_loops_dst,
1422                                         polys_dst, num_polys_dst, weights[PDATA], invert_vgroup);
1423                         }
1424
1425                         if (data_transfer_layersmapping_generate(
1426                                 &lay_map, ob_src, ob_dst, dm_src, dm_dst, me_dst, ME_POLY,
1427                                 cddata_type, mix_mode, mix_factor, weights[PDATA],
1428                                 num_polys_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1429                         {
1430                                 CustomDataTransferLayerMap *lay_mapit;
1431
1432                                 changed = (lay_map.first != NULL);
1433
1434                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1435                                         CustomData_data_transfer(&geom_map[PDATA], lay_mapit);
1436                                 }
1437
1438                                 BLI_freelistN(&lay_map);
1439                         }
1440                 }
1441
1442                 data_transfer_dtdata_type_postprocess(ob_src, ob_dst, dm_src, dm_dst, me_dst, dtdata_type, changed);
1443         }
1444
1445         for (i = 0; i < DATAMAX; i++) {
1446                 BKE_mesh_remap_free(&geom_map[i]);
1447                 MEM_SAFE_FREE(weights[i]);
1448         }
1449         dm_src->release(dm_src);
1450
1451         return changed;
1452
1453 #undef VDATA
1454 #undef EDATA
1455 #undef LDATA
1456 #undef PDATA
1457 #undef DATAMAX
1458 }
1459
1460 bool BKE_object_data_transfer_mesh(
1461         const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob_src, Object *ob_dst, const int data_types,
1462         const bool use_create, const int map_vert_mode, const int map_edge_mode, const int map_loop_mode,
1463         const int map_poly_mode, SpaceTransform *space_transform, const bool auto_transform,
1464         const float max_distance, const float ray_radius, const float islands_handling_precision,
1465         const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1466         const int mix_mode, const float mix_factor, const char *vgroup_name, const bool invert_vgroup,
1467         ReportList *reports)
1468 {
1469         return BKE_object_data_transfer_dm(
1470                 eval_ctx, scene, ob_src, ob_dst, NULL, data_types, use_create,
1471                 map_vert_mode, map_edge_mode, map_loop_mode, map_poly_mode,
1472                 space_transform, auto_transform,
1473                 max_distance, ray_radius, islands_handling_precision,
1474                 fromlayers_select, tolayers_select,
1475                 mix_mode, mix_factor, vgroup_name, invert_vgroup, reports);
1476 }