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