ClangFormat: apply to source, most of intern
[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
21  * \ingroup bke
22  */
23
24 #include "CLG_log.h"
25
26 #include "MEM_guardedalloc.h"
27
28 #include "DNA_customdata_types.h"
29 #include "DNA_meshdata_types.h"
30 #include "DNA_mesh_types.h"
31 #include "DNA_object_types.h"
32 #include "DNA_scene_types.h"
33
34 #include "BLI_math.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_utildefines.h"
37
38 #include "BKE_customdata.h"
39 #include "BKE_data_transfer.h"
40 #include "BKE_deform.h"
41 #include "BKE_mesh.h"
42 #include "BKE_mesh_mapping.h"
43 #include "BKE_mesh_runtime.h"
44 #include "BKE_mesh_remap.h"
45 #include "BKE_modifier.h"
46 #include "BKE_object.h"
47 #include "BKE_object_deform.h"
48 #include "BKE_report.h"
49
50 #include "data_transfer_intern.h"
51
52 static CLG_LogRef LOG = {"bke.data_transfer"};
53
54 void BKE_object_data_transfer_dttypes_to_cdmask(const int dtdata_types,
55                                                 CustomData_MeshMasks *r_data_masks)
56 {
57   for (int 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       if (DT_DATATYPE_IS_VERT(dtdata_type)) {
68         r_data_masks->vmask |= 1LL << cddata_type;
69       }
70       else if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
71         r_data_masks->emask |= 1LL << cddata_type;
72       }
73       else if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
74         r_data_masks->lmask |= 1LL << cddata_type;
75       }
76       else if (DT_DATATYPE_IS_POLY(dtdata_type)) {
77         r_data_masks->pmask |= 1LL << cddata_type;
78       }
79     }
80     else if (cddata_type == CD_FAKE_MDEFORMVERT) {
81       r_data_masks->vmask |= CD_MASK_MDEFORMVERT; /* Exception for vgroups :/ */
82     }
83     else if (cddata_type == CD_FAKE_UV) {
84       r_data_masks->lmask |= CD_MASK_MLOOPUV;
85     }
86     else if (cddata_type == CD_FAKE_LNOR) {
87       r_data_masks->vmask |= CD_MASK_NORMAL;
88       r_data_masks->lmask |= CD_MASK_NORMAL | CD_MASK_CUSTOMLOOPNORMAL;
89     }
90   }
91 }
92
93 /* Check what can do each layer type (if it is actually handled by transferdata, if it supports advanced mixing... */
94 bool BKE_object_data_transfer_get_dttypes_capacity(const int dtdata_types,
95                                                    bool *r_advanced_mixing,
96                                                    bool *r_threshold)
97 {
98   int i;
99   bool ret = false;
100
101   *r_advanced_mixing = false;
102   *r_threshold = false;
103
104   for (i = 0; (i < DT_TYPE_MAX) && !(ret && *r_advanced_mixing && *r_threshold); i++) {
105     const int dtdata_type = 1 << i;
106
107     if (!(dtdata_types & dtdata_type)) {
108       continue;
109     }
110
111     switch (dtdata_type) {
112       /* Vertex data */
113       case DT_TYPE_MDEFORMVERT:
114         *r_advanced_mixing = true;
115         *r_threshold = true;
116         ret = true;
117         break;
118       case DT_TYPE_SKIN:
119         *r_threshold = true;
120         ret = true;
121         break;
122       case DT_TYPE_BWEIGHT_VERT:
123         ret = true;
124         break;
125       /* Edge data */
126       case DT_TYPE_SHARP_EDGE:
127         *r_threshold = true;
128         ret = true;
129         break;
130       case DT_TYPE_SEAM:
131         *r_threshold = true;
132         ret = true;
133         break;
134       case DT_TYPE_CREASE:
135         ret = true;
136         break;
137       case DT_TYPE_BWEIGHT_EDGE:
138         ret = true;
139         break;
140       case DT_TYPE_FREESTYLE_EDGE:
141         *r_threshold = true;
142         ret = true;
143         break;
144       /* Loop/Poly data */
145       case DT_TYPE_UV:
146         ret = true;
147         break;
148       case DT_TYPE_VCOL:
149         *r_advanced_mixing = true;
150         *r_threshold = true;
151         ret = true;
152         break;
153       case DT_TYPE_LNOR:
154         *r_advanced_mixing = true;
155         ret = true;
156         break;
157       case DT_TYPE_SHARP_FACE:
158         *r_threshold = true;
159         ret = true;
160         break;
161       case DT_TYPE_FREESTYLE_FACE:
162         *r_threshold = true;
163         ret = true;
164         break;
165     }
166   }
167
168   return ret;
169 }
170
171 int BKE_object_data_transfer_get_dttypes_item_types(const int dtdata_types)
172 {
173   int i, ret = 0;
174
175   for (i = 0; (i < DT_TYPE_MAX) && (ret ^ (ME_VERT | ME_EDGE | ME_LOOP | ME_POLY)); i++) {
176     const int dtdata_type = 1 << i;
177
178     if (!(dtdata_types & dtdata_type)) {
179       continue;
180     }
181
182     if (DT_DATATYPE_IS_VERT(dtdata_type)) {
183       ret |= ME_VERT;
184     }
185     if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
186       ret |= ME_EDGE;
187     }
188     if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
189       ret |= ME_LOOP;
190     }
191     if (DT_DATATYPE_IS_POLY(dtdata_type)) {
192       ret |= ME_POLY;
193     }
194   }
195
196   return ret;
197 }
198
199 int BKE_object_data_transfer_dttype_to_cdtype(const int dtdata_type)
200 {
201   switch (dtdata_type) {
202     case DT_TYPE_MDEFORMVERT:
203       return CD_FAKE_MDEFORMVERT;
204     case DT_TYPE_SHAPEKEY:
205       return CD_FAKE_SHAPEKEY;
206     case DT_TYPE_SKIN:
207       return CD_MVERT_SKIN;
208     case DT_TYPE_BWEIGHT_VERT:
209       return CD_FAKE_BWEIGHT;
210
211     case DT_TYPE_SHARP_EDGE:
212       return CD_FAKE_SHARP;
213     case DT_TYPE_SEAM:
214       return CD_FAKE_SEAM;
215     case DT_TYPE_CREASE:
216       return CD_FAKE_CREASE;
217     case DT_TYPE_BWEIGHT_EDGE:
218       return CD_FAKE_BWEIGHT;
219     case DT_TYPE_FREESTYLE_EDGE:
220       return CD_FREESTYLE_EDGE;
221
222     case DT_TYPE_UV:
223       return CD_FAKE_UV;
224     case DT_TYPE_SHARP_FACE:
225       return CD_FAKE_SHARP;
226     case DT_TYPE_FREESTYLE_FACE:
227       return CD_FREESTYLE_FACE;
228
229     case DT_TYPE_VCOL:
230       return CD_MLOOPCOL;
231     case DT_TYPE_LNOR:
232       return CD_FAKE_LNOR;
233
234     default:
235       BLI_assert(0);
236   }
237   return 0; /* Should never be reached! */
238 }
239
240 int BKE_object_data_transfer_dttype_to_srcdst_index(const int dtdata_type)
241 {
242   switch (dtdata_type) {
243     case DT_TYPE_MDEFORMVERT:
244       return DT_MULTILAYER_INDEX_MDEFORMVERT;
245     case DT_TYPE_SHAPEKEY:
246       return DT_MULTILAYER_INDEX_SHAPEKEY;
247     case DT_TYPE_UV:
248       return DT_MULTILAYER_INDEX_UV;
249     case DT_TYPE_VCOL:
250       return DT_MULTILAYER_INDEX_VCOL;
251     default:
252       return DT_MULTILAYER_INDEX_INVALID;
253   }
254 }
255
256 /* ********** */
257
258 /* Generic pre/post processing, only used by custom loop normals currently. */
259
260 static void data_transfer_dtdata_type_preprocess(Mesh *me_src,
261                                                  Mesh *me_dst,
262                                                  const int dtdata_type,
263                                                  const bool dirty_nors_dst)
264 {
265   if (dtdata_type == DT_TYPE_LNOR) {
266     /* Compute custom normals into regular loop normals, which will be used for the transfer. */
267     MVert *verts_dst = me_dst->mvert;
268     const int num_verts_dst = me_dst->totvert;
269     MEdge *edges_dst = me_dst->medge;
270     const int num_edges_dst = me_dst->totedge;
271     MPoly *polys_dst = me_dst->mpoly;
272     const int num_polys_dst = me_dst->totpoly;
273     MLoop *loops_dst = me_dst->mloop;
274     const int num_loops_dst = me_dst->totloop;
275     CustomData *pdata_dst = &me_dst->pdata;
276     CustomData *ldata_dst = &me_dst->ldata;
277
278     const bool use_split_nors_dst = (me_dst->flag & ME_AUTOSMOOTH) != 0;
279     const float split_angle_dst = me_dst->smoothresh;
280
281     /* This should be ensured by cddata_masks we pass to code generating/giving us me_src now. */
282     BLI_assert(CustomData_get_layer(&me_src->ldata, CD_NORMAL) != NULL);
283     BLI_assert(CustomData_get_layer(&me_src->pdata, CD_NORMAL) != NULL);
284     (void)me_src;
285
286     float(*poly_nors_dst)[3];
287     float(*loop_nors_dst)[3];
288     short(*custom_nors_dst)[2] = CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL);
289
290     /* Cache poly nors into a temp CDLayer. */
291     poly_nors_dst = CustomData_get_layer(pdata_dst, CD_NORMAL);
292     const bool do_poly_nors_dst = (poly_nors_dst == NULL);
293     if (do_poly_nors_dst) {
294       poly_nors_dst = CustomData_add_layer(pdata_dst, CD_NORMAL, CD_CALLOC, NULL, num_polys_dst);
295       CustomData_set_layer_flag(pdata_dst, CD_NORMAL, CD_FLAG_TEMPORARY);
296     }
297     if (dirty_nors_dst || do_poly_nors_dst) {
298       BKE_mesh_calc_normals_poly(verts_dst,
299                                  NULL,
300                                  num_verts_dst,
301                                  loops_dst,
302                                  polys_dst,
303                                  num_loops_dst,
304                                  num_polys_dst,
305                                  poly_nors_dst,
306                                  true);
307     }
308     /* Cache loop nors into a temp CDLayer. */
309     loop_nors_dst = CustomData_get_layer(ldata_dst, CD_NORMAL);
310     const bool do_loop_nors_dst = (loop_nors_dst == NULL);
311     if (do_loop_nors_dst) {
312       loop_nors_dst = CustomData_add_layer(ldata_dst, CD_NORMAL, CD_CALLOC, NULL, num_loops_dst);
313       CustomData_set_layer_flag(ldata_dst, CD_NORMAL, CD_FLAG_TEMPORARY);
314     }
315     if (dirty_nors_dst || do_loop_nors_dst) {
316       BKE_mesh_normals_loop_split(verts_dst,
317                                   num_verts_dst,
318                                   edges_dst,
319                                   num_edges_dst,
320                                   loops_dst,
321                                   loop_nors_dst,
322                                   num_loops_dst,
323                                   polys_dst,
324                                   (const float(*)[3])poly_nors_dst,
325                                   num_polys_dst,
326                                   use_split_nors_dst,
327                                   split_angle_dst,
328                                   NULL,
329                                   custom_nors_dst,
330                                   NULL);
331     }
332   }
333 }
334
335 static void data_transfer_dtdata_type_postprocess(Object *UNUSED(ob_src),
336                                                   Object *UNUSED(ob_dst),
337                                                   Mesh *UNUSED(me_src),
338                                                   Mesh *me_dst,
339                                                   const int dtdata_type,
340                                                   const bool changed)
341 {
342   if (dtdata_type == DT_TYPE_LNOR) {
343     /* Bake edited destination loop normals into custom normals again. */
344     MVert *verts_dst = me_dst->mvert;
345     const int num_verts_dst = me_dst->totvert;
346     MEdge *edges_dst = me_dst->medge;
347     const int num_edges_dst = me_dst->totedge;
348     MPoly *polys_dst = me_dst->mpoly;
349     const int num_polys_dst = me_dst->totpoly;
350     MLoop *loops_dst = me_dst->mloop;
351     const int num_loops_dst = me_dst->totloop;
352     CustomData *pdata_dst = &me_dst->pdata;
353     CustomData *ldata_dst = &me_dst->ldata;
354
355     const float(*poly_nors_dst)[3] = CustomData_get_layer(pdata_dst, CD_NORMAL);
356     float(*loop_nors_dst)[3] = CustomData_get_layer(ldata_dst, CD_NORMAL);
357     short(*custom_nors_dst)[2] = CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL);
358
359     BLI_assert(poly_nors_dst);
360
361     if (!changed) {
362       return;
363     }
364
365     if (!custom_nors_dst) {
366       custom_nors_dst = CustomData_add_layer(
367           ldata_dst, CD_CUSTOMLOOPNORMAL, CD_CALLOC, NULL, num_loops_dst);
368     }
369
370     /* Note loop_nors_dst contains our custom normals as transferred from source... */
371     BKE_mesh_normals_loop_custom_set(verts_dst,
372                                      num_verts_dst,
373                                      edges_dst,
374                                      num_edges_dst,
375                                      loops_dst,
376                                      loop_nors_dst,
377                                      num_loops_dst,
378                                      polys_dst,
379                                      poly_nors_dst,
380                                      num_polys_dst,
381                                      custom_nors_dst);
382   }
383 }
384
385 /* ********** */
386
387 static MeshRemapIslandsCalc data_transfer_get_loop_islands_generator(const int cddata_type)
388 {
389   switch (cddata_type) {
390     case CD_FAKE_UV:
391       return BKE_mesh_calc_islands_loop_poly_edgeseam;
392     default:
393       break;
394   }
395   return NULL;
396 }
397
398 float data_transfer_interp_float_do(const int mix_mode,
399                                     const float val_dst,
400                                     const float val_src,
401                                     const float mix_factor)
402 {
403   float val_ret;
404
405   if (((mix_mode == CDT_MIX_REPLACE_ABOVE_THRESHOLD && (val_dst < mix_factor)) ||
406        (mix_mode == CDT_MIX_REPLACE_BELOW_THRESHOLD && (val_dst > mix_factor)))) {
407     return val_dst; /* Do not affect destination. */
408   }
409
410   switch (mix_mode) {
411     case CDT_MIX_REPLACE_ABOVE_THRESHOLD:
412     case CDT_MIX_REPLACE_BELOW_THRESHOLD:
413       return val_src;
414     case CDT_MIX_MIX:
415       val_ret = (val_dst + val_src) * 0.5f;
416       break;
417     case CDT_MIX_ADD:
418       val_ret = val_dst + val_src;
419       break;
420     case CDT_MIX_SUB:
421       val_ret = val_dst - val_src;
422       break;
423     case CDT_MIX_MUL:
424       val_ret = val_dst * val_src;
425       break;
426     case CDT_MIX_TRANSFER:
427     default:
428       val_ret = val_src;
429       break;
430   }
431   return interpf(val_ret, val_dst, mix_factor);
432 }
433
434 static void data_transfer_interp_char(const CustomDataTransferLayerMap *laymap,
435                                       void *dest,
436                                       const void **sources,
437                                       const float *weights,
438                                       const int count,
439                                       const float mix_factor)
440 {
441   const char **data_src = (const char **)sources;
442   char *data_dst = (char *)dest;
443
444   const int mix_mode = laymap->mix_mode;
445   float val_src = 0.0f;
446   const float val_dst = (float)(*data_dst) / 255.0f;
447
448   int i;
449
450   for (i = count; i--;) {
451     val_src += ((float)(*data_src[i]) / 255.0f) * weights[i];
452   }
453
454   val_src = data_transfer_interp_float_do(mix_mode, val_dst, val_src, mix_factor);
455
456   CLAMP(val_src, 0.0f, 1.0f);
457
458   *data_dst = (char)(val_src * 255.0f);
459 }
460
461 /* Helpers to match sources and destinations data layers (also handles 'conversions' in CD_FAKE cases). */
462
463 void data_transfer_layersmapping_add_item(ListBase *r_map,
464                                           const int cddata_type,
465                                           const int mix_mode,
466                                           const float mix_factor,
467                                           const float *mix_weights,
468                                           const void *data_src,
469                                           void *data_dst,
470                                           const int data_src_n,
471                                           const int data_dst_n,
472                                           const size_t elem_size,
473                                           const size_t data_size,
474                                           const size_t data_offset,
475                                           const uint64_t data_flag,
476                                           cd_datatransfer_interp interp,
477                                           void *interp_data)
478 {
479   CustomDataTransferLayerMap *item = MEM_mallocN(sizeof(*item), __func__);
480
481   BLI_assert(data_dst != NULL);
482
483   item->data_type = cddata_type;
484   item->mix_mode = mix_mode;
485   item->mix_factor = mix_factor;
486   item->mix_weights = mix_weights;
487
488   item->data_src = data_src;
489   item->data_dst = data_dst;
490   item->data_src_n = data_src_n;
491   item->data_dst_n = data_dst_n;
492   item->elem_size = elem_size;
493
494   item->data_size = data_size;
495   item->data_offset = data_offset;
496   item->data_flag = data_flag;
497
498   item->interp = interp;
499   item->interp_data = interp_data;
500
501   BLI_addtail(r_map, item);
502 }
503
504 static void data_transfer_layersmapping_add_item_cd(ListBase *r_map,
505                                                     const int cddata_type,
506                                                     const int mix_mode,
507                                                     const float mix_factor,
508                                                     const float *mix_weights,
509                                                     void *data_src,
510                                                     void *data_dst,
511                                                     cd_datatransfer_interp interp,
512                                                     void *interp_data)
513 {
514   uint64_t data_flag = 0;
515
516   if (cddata_type == CD_FREESTYLE_EDGE) {
517     data_flag = FREESTYLE_EDGE_MARK;
518   }
519   else if (cddata_type == CD_FREESTYLE_FACE) {
520     data_flag = FREESTYLE_FACE_MARK;
521   }
522
523   data_transfer_layersmapping_add_item(r_map,
524                                        cddata_type,
525                                        mix_mode,
526                                        mix_factor,
527                                        mix_weights,
528                                        data_src,
529                                        data_dst,
530                                        0,
531                                        0,
532                                        0,
533                                        0,
534                                        0,
535                                        data_flag,
536                                        interp,
537                                        interp_data);
538 }
539
540 /* Note: All those layer mapping handlers return false *only* if they were given invalid parameters.
541  *       This means that even if they do nothing, they will return true if all given parameters were OK.
542  *       Also, r_map may be NULL, in which case they will 'only' create/delete destination layers according
543  *       to given parameters.
544  */
545
546 static bool data_transfer_layersmapping_cdlayers_multisrc_to_dst(ListBase *r_map,
547                                                                  const int cddata_type,
548                                                                  const int mix_mode,
549                                                                  const float mix_factor,
550                                                                  const float *mix_weights,
551                                                                  const int num_elem_dst,
552                                                                  const bool use_create,
553                                                                  const bool use_delete,
554                                                                  CustomData *cd_src,
555                                                                  CustomData *cd_dst,
556                                                                  const bool use_dupref_dst,
557                                                                  const int tolayers,
558                                                                  bool *use_layers_src,
559                                                                  const int num_layers_src,
560                                                                  cd_datatransfer_interp interp,
561                                                                  void *interp_data)
562 {
563   void *data_src, *data_dst = NULL;
564   int idx_src = num_layers_src;
565   int idx_dst, tot_dst = CustomData_number_of_layers(cd_dst, cddata_type);
566   bool *data_dst_to_delete = NULL;
567
568   if (!use_layers_src) {
569     /* No source at all, we can only delete all dest if requested... */
570     if (use_delete) {
571       idx_dst = tot_dst;
572       while (idx_dst--) {
573         CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
574       }
575     }
576     return true;
577   }
578
579   switch (tolayers) {
580     case DT_LAYERS_INDEX_DST:
581       idx_dst = tot_dst;
582
583       /* Find last source actually used! */
584       while (idx_src-- && !use_layers_src[idx_src])
585         ;
586       idx_src++;
587
588       if (idx_dst < idx_src) {
589         if (use_create) {
590           /* Create as much data layers as necessary! */
591           for (; idx_dst < idx_src; idx_dst++) {
592             CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
593           }
594         }
595         else {
596           /* Otherwise, just try to map what we can with existing dst data layers. */
597           idx_src = idx_dst;
598         }
599       }
600       else if (use_delete && idx_dst > idx_src) {
601         while (idx_dst-- > idx_src) {
602           CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
603         }
604       }
605       if (r_map) {
606         while (idx_src--) {
607           if (!use_layers_src[idx_src]) {
608             continue;
609           }
610           data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
611           /* If dest is a evaluated mesh (fro; ;odifier), we do not want to overwrite cdlayers of orig mesh! */
612           if (use_dupref_dst) {
613             data_dst = CustomData_duplicate_referenced_layer_n(
614                 cd_dst, cddata_type, idx_src, num_elem_dst);
615           }
616           else {
617             data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_src);
618           }
619           data_transfer_layersmapping_add_item_cd(r_map,
620                                                   cddata_type,
621                                                   mix_mode,
622                                                   mix_factor,
623                                                   mix_weights,
624                                                   data_src,
625                                                   data_dst,
626                                                   interp,
627                                                   interp_data);
628         }
629       }
630       break;
631     case DT_LAYERS_NAME_DST:
632       if (use_delete) {
633         if (tot_dst) {
634           data_dst_to_delete = MEM_mallocN(sizeof(*data_dst_to_delete) * (size_t)tot_dst,
635                                            __func__);
636           memset(data_dst_to_delete, true, sizeof(*data_dst_to_delete) * (size_t)tot_dst);
637         }
638       }
639
640       while (idx_src--) {
641         const char *name;
642
643         if (!use_layers_src[idx_src]) {
644           continue;
645         }
646
647         name = CustomData_get_layer_name(cd_src, cddata_type, idx_src);
648         data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
649
650         if ((idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name)) == -1) {
651           if (use_create) {
652             CustomData_add_layer_named(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst, name);
653             idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name);
654           }
655           else {
656             /* If we are not allowed to create missing dst data layers, just skip matching src one. */
657             continue;
658           }
659         }
660         else if (data_dst_to_delete) {
661           data_dst_to_delete[idx_dst] = false;
662         }
663         if (r_map) {
664           /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
665           if (use_dupref_dst) {
666             data_dst = CustomData_duplicate_referenced_layer_n(
667                 cd_dst, cddata_type, idx_dst, num_elem_dst);
668           }
669           else {
670             data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
671           }
672           data_transfer_layersmapping_add_item_cd(r_map,
673                                                   cddata_type,
674                                                   mix_mode,
675                                                   mix_factor,
676                                                   mix_weights,
677                                                   data_src,
678                                                   data_dst,
679                                                   interp,
680                                                   interp_data);
681         }
682       }
683
684       if (data_dst_to_delete) {
685         /* Note: This won't affect newly created layers, if any, since tot_dst has not been updated!
686          *       Also, looping backward ensures us we do not suffer from index shifting when deleting a layer.
687          */
688         for (idx_dst = tot_dst; idx_dst--;) {
689           if (data_dst_to_delete[idx_dst]) {
690             CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
691           }
692         }
693
694         MEM_freeN(data_dst_to_delete);
695       }
696       break;
697     default:
698       return false;
699   }
700
701   return true;
702 }
703
704 static bool data_transfer_layersmapping_cdlayers(ListBase *r_map,
705                                                  const int cddata_type,
706                                                  const int mix_mode,
707                                                  const float mix_factor,
708                                                  const float *mix_weights,
709                                                  const int num_elem_dst,
710                                                  const bool use_create,
711                                                  const bool use_delete,
712                                                  CustomData *cd_src,
713                                                  CustomData *cd_dst,
714                                                  const bool use_dupref_dst,
715                                                  const int fromlayers,
716                                                  const int tolayers,
717                                                  cd_datatransfer_interp interp,
718                                                  void *interp_data)
719 {
720   int idx_src, idx_dst;
721   void *data_src, *data_dst = NULL;
722
723   if (CustomData_layertype_is_singleton(cddata_type)) {
724     if (!(data_src = CustomData_get_layer(cd_src, cddata_type))) {
725       if (use_delete) {
726         CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, 0);
727       }
728       return true;
729     }
730
731     data_dst = CustomData_get_layer(cd_dst, cddata_type);
732     if (!data_dst) {
733       if (!use_create) {
734         return true;
735       }
736       data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
737     }
738     else if (use_dupref_dst && r_map) {
739       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
740       data_dst = CustomData_duplicate_referenced_layer(cd_dst, cddata_type, num_elem_dst);
741     }
742
743     if (r_map) {
744       data_transfer_layersmapping_add_item_cd(r_map,
745                                               cddata_type,
746                                               mix_mode,
747                                               mix_factor,
748                                               mix_weights,
749                                               data_src,
750                                               data_dst,
751                                               interp,
752                                               interp_data);
753     }
754   }
755   else if (fromlayers == DT_LAYERS_ACTIVE_SRC || fromlayers >= 0) {
756     /* Note: use_delete has not much meaning in this case, ignored. */
757
758     if (fromlayers >= 0) { /* Real-layer index */
759       idx_src = fromlayers;
760     }
761     else {
762       if ((idx_src = CustomData_get_active_layer(cd_src, cddata_type)) == -1) {
763         return true;
764       }
765     }
766     data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
767     if (!data_src) {
768       return true;
769     }
770
771     if (tolayers >= 0) { /* Real-layer index */
772       idx_dst = tolayers;
773       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
774       if (use_dupref_dst && r_map) {
775         data_dst = CustomData_duplicate_referenced_layer_n(
776             cd_dst, cddata_type, idx_dst, num_elem_dst);
777       }
778       else {
779         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
780       }
781     }
782     else if (tolayers == DT_LAYERS_ACTIVE_DST) {
783       if ((idx_dst = CustomData_get_active_layer(cd_dst, cddata_type)) == -1) {
784         if (!use_create) {
785           return true;
786         }
787         data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
788       }
789       else {
790         /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
791         if (use_dupref_dst && r_map) {
792           data_dst = CustomData_duplicate_referenced_layer_n(
793               cd_dst, cddata_type, idx_dst, num_elem_dst);
794         }
795         else {
796           data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
797         }
798       }
799     }
800     else if (tolayers == DT_LAYERS_INDEX_DST) {
801       int num = CustomData_number_of_layers(cd_dst, cddata_type);
802       idx_dst = idx_src;
803       if (num <= idx_dst) {
804         if (!use_create) {
805           return true;
806         }
807         /* Create as much data layers as necessary! */
808         for (; num <= idx_dst; num++) {
809           CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
810         }
811       }
812       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
813       if (use_dupref_dst && r_map) {
814         data_dst = CustomData_duplicate_referenced_layer_n(
815             cd_dst, cddata_type, idx_dst, num_elem_dst);
816       }
817       else {
818         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
819       }
820     }
821     else if (tolayers == DT_LAYERS_NAME_DST) {
822       const char *name = CustomData_get_layer_name(cd_src, cddata_type, idx_src);
823       if ((idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name)) == -1) {
824         if (!use_create) {
825           return true;
826         }
827         CustomData_add_layer_named(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst, name);
828         idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name);
829       }
830       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
831       if (use_dupref_dst && r_map) {
832         data_dst = CustomData_duplicate_referenced_layer_n(
833             cd_dst, cddata_type, idx_dst, num_elem_dst);
834       }
835       else {
836         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
837       }
838     }
839     else {
840       return false;
841     }
842
843     if (!data_dst) {
844       return false;
845     }
846
847     if (r_map) {
848       data_transfer_layersmapping_add_item_cd(r_map,
849                                               cddata_type,
850                                               mix_mode,
851                                               mix_factor,
852                                               mix_weights,
853                                               data_src,
854                                               data_dst,
855                                               interp,
856                                               interp_data);
857     }
858   }
859   else if (fromlayers == DT_LAYERS_ALL_SRC) {
860     int num_src = CustomData_number_of_layers(cd_src, cddata_type);
861     bool *use_layers_src = num_src ?
862                                MEM_mallocN(sizeof(*use_layers_src) * (size_t)num_src, __func__) :
863                                NULL;
864     bool ret;
865
866     if (use_layers_src) {
867       memset(use_layers_src, true, sizeof(*use_layers_src) * num_src);
868     }
869
870     ret = data_transfer_layersmapping_cdlayers_multisrc_to_dst(r_map,
871                                                                cddata_type,
872                                                                mix_mode,
873                                                                mix_factor,
874                                                                mix_weights,
875                                                                num_elem_dst,
876                                                                use_create,
877                                                                use_delete,
878                                                                cd_src,
879                                                                cd_dst,
880                                                                use_dupref_dst,
881                                                                tolayers,
882                                                                use_layers_src,
883                                                                num_src,
884                                                                interp,
885                                                                interp_data);
886
887     if (use_layers_src) {
888       MEM_freeN(use_layers_src);
889     }
890     return ret;
891   }
892   else {
893     return false;
894   }
895
896   return true;
897 }
898
899 static bool data_transfer_layersmapping_generate(ListBase *r_map,
900                                                  Object *ob_src,
901                                                  Object *ob_dst,
902                                                  Mesh *me_src,
903                                                  Mesh *me_dst,
904                                                  const int elem_type,
905                                                  int cddata_type,
906                                                  int mix_mode,
907                                                  float mix_factor,
908                                                  const float *mix_weights,
909                                                  const int num_elem_dst,
910                                                  const bool use_create,
911                                                  const bool use_delete,
912                                                  const int fromlayers,
913                                                  const int tolayers,
914                                                  SpaceTransform *space_transform)
915 {
916   CustomData *cd_src, *cd_dst;
917
918   cd_datatransfer_interp interp = NULL;
919   void *interp_data = NULL;
920
921   if (elem_type == ME_VERT) {
922     if (!(cddata_type & CD_FAKE)) {
923       cd_src = &me_src->vdata;
924       cd_dst = &me_dst->vdata;
925
926       if (!data_transfer_layersmapping_cdlayers(r_map,
927                                                 cddata_type,
928                                                 mix_mode,
929                                                 mix_factor,
930                                                 mix_weights,
931                                                 num_elem_dst,
932                                                 use_create,
933                                                 use_delete,
934                                                 cd_src,
935                                                 cd_dst,
936                                                 me_dst != ob_dst->data,
937                                                 fromlayers,
938                                                 tolayers,
939                                                 interp,
940                                                 interp_data)) {
941         /* We handle specific source selection cases here. */
942         return false;
943       }
944       return true;
945     }
946     else if (cddata_type == CD_FAKE_BWEIGHT) {
947       const size_t elem_size = sizeof(*((MVert *)NULL));
948       const size_t data_size = sizeof(((MVert *)NULL)->bweight);
949       const size_t data_offset = offsetof(MVert, bweight);
950       const uint64_t data_flag = 0;
951
952       if (!(me_src->cd_flag & ME_CDFLAG_VERT_BWEIGHT)) {
953         if (use_delete) {
954           me_dst->cd_flag &= ~ME_CDFLAG_VERT_BWEIGHT;
955         }
956         return true;
957       }
958       me_dst->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
959       if (r_map) {
960         data_transfer_layersmapping_add_item(r_map,
961                                              cddata_type,
962                                              mix_mode,
963                                              mix_factor,
964                                              mix_weights,
965                                              me_src->mvert,
966                                              me_dst->mvert,
967                                              me_src->totvert,
968                                              me_dst->totvert,
969                                              elem_size,
970                                              data_size,
971                                              data_offset,
972                                              data_flag,
973                                              data_transfer_interp_char,
974                                              interp_data);
975       }
976       return true;
977     }
978     else if (cddata_type == CD_FAKE_MDEFORMVERT) {
979       bool ret;
980
981       cd_src = &me_src->vdata;
982       cd_dst = &me_dst->vdata;
983
984       ret = data_transfer_layersmapping_vgroups(r_map,
985                                                 mix_mode,
986                                                 mix_factor,
987                                                 mix_weights,
988                                                 num_elem_dst,
989                                                 use_create,
990                                                 use_delete,
991                                                 ob_src,
992                                                 ob_dst,
993                                                 cd_src,
994                                                 cd_dst,
995                                                 me_dst != ob_dst->data,
996                                                 fromlayers,
997                                                 tolayers);
998
999       /* Mesh stores its dvert in a specific pointer too. :( */
1000       me_dst->dvert = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
1001       return ret;
1002     }
1003     else if (cddata_type == CD_FAKE_SHAPEKEY) {
1004       /* TODO: leaving shapekeys aside for now, quite specific case, since we can't access them from MVert :/ */
1005       return false;
1006     }
1007   }
1008   else if (elem_type == ME_EDGE) {
1009     if (!(cddata_type & CD_FAKE)) { /* Unused for edges, currently... */
1010       cd_src = &me_src->edata;
1011       cd_dst = &me_dst->edata;
1012
1013       if (!data_transfer_layersmapping_cdlayers(r_map,
1014                                                 cddata_type,
1015                                                 mix_mode,
1016                                                 mix_factor,
1017                                                 mix_weights,
1018                                                 num_elem_dst,
1019                                                 use_create,
1020                                                 use_delete,
1021                                                 cd_src,
1022                                                 cd_dst,
1023                                                 me_dst != ob_dst->data,
1024                                                 fromlayers,
1025                                                 tolayers,
1026                                                 interp,
1027                                                 interp_data)) {
1028         /* We handle specific source selection cases here. */
1029         return false;
1030       }
1031       return true;
1032     }
1033     else if (cddata_type == CD_FAKE_CREASE) {
1034       const size_t elem_size = sizeof(*((MEdge *)NULL));
1035       const size_t data_size = sizeof(((MEdge *)NULL)->crease);
1036       const size_t data_offset = offsetof(MEdge, crease);
1037       const uint64_t data_flag = 0;
1038
1039       if (!(me_src->cd_flag & ME_CDFLAG_EDGE_CREASE)) {
1040         if (use_delete && !me_dst) {
1041           me_dst->cd_flag &= ~ME_CDFLAG_EDGE_CREASE;
1042         }
1043         return true;
1044       }
1045       me_dst->cd_flag |= ME_CDFLAG_EDGE_CREASE;
1046       if (r_map) {
1047         data_transfer_layersmapping_add_item(r_map,
1048                                              cddata_type,
1049                                              mix_mode,
1050                                              mix_factor,
1051                                              mix_weights,
1052                                              me_src->medge,
1053                                              me_dst->medge,
1054                                              me_src->totedge,
1055                                              me_dst->totedge,
1056                                              elem_size,
1057                                              data_size,
1058                                              data_offset,
1059                                              data_flag,
1060                                              data_transfer_interp_char,
1061                                              interp_data);
1062       }
1063       return true;
1064     }
1065     else if (cddata_type == CD_FAKE_BWEIGHT) {
1066       const size_t elem_size = sizeof(*((MEdge *)NULL));
1067       const size_t data_size = sizeof(((MEdge *)NULL)->bweight);
1068       const size_t data_offset = offsetof(MEdge, bweight);
1069       const uint64_t data_flag = 0;
1070
1071       if (!(me_src->cd_flag & ME_CDFLAG_EDGE_BWEIGHT)) {
1072         if (use_delete && !me_dst) {
1073           me_dst->cd_flag &= ~ME_CDFLAG_EDGE_BWEIGHT;
1074         }
1075         return true;
1076       }
1077       me_dst->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
1078       if (r_map) {
1079         data_transfer_layersmapping_add_item(r_map,
1080                                              cddata_type,
1081                                              mix_mode,
1082                                              mix_factor,
1083                                              mix_weights,
1084                                              me_src->medge,
1085                                              me_dst->medge,
1086                                              me_src->totedge,
1087                                              me_dst->totedge,
1088                                              elem_size,
1089                                              data_size,
1090                                              data_offset,
1091                                              data_flag,
1092                                              data_transfer_interp_char,
1093                                              interp_data);
1094       }
1095       return true;
1096     }
1097     else if (r_map && ELEM(cddata_type, CD_FAKE_SHARP, CD_FAKE_SEAM)) {
1098       const size_t elem_size = sizeof(*((MEdge *)NULL));
1099       const size_t data_size = sizeof(((MEdge *)NULL)->flag);
1100       const size_t data_offset = offsetof(MEdge, flag);
1101       const uint64_t data_flag = (cddata_type == CD_FAKE_SHARP) ? ME_SHARP : ME_SEAM;
1102
1103       data_transfer_layersmapping_add_item(r_map,
1104                                            cddata_type,
1105                                            mix_mode,
1106                                            mix_factor,
1107                                            mix_weights,
1108                                            me_src->medge,
1109                                            me_dst->medge,
1110                                            me_src->totedge,
1111                                            me_dst->totedge,
1112                                            elem_size,
1113                                            data_size,
1114                                            data_offset,
1115                                            data_flag,
1116                                            NULL,
1117                                            interp_data);
1118       return true;
1119     }
1120     else {
1121       return false;
1122     }
1123   }
1124   else if (elem_type == ME_LOOP) {
1125     if (cddata_type == CD_FAKE_UV) {
1126       cddata_type = CD_MLOOPUV;
1127     }
1128     else if (cddata_type == CD_FAKE_LNOR) {
1129       /* Preprocess should have generated it, Postprocess will convert it back to CD_CUSTOMLOOPNORMAL. */
1130       cddata_type = CD_NORMAL;
1131       interp_data = space_transform;
1132       interp = customdata_data_transfer_interp_normal_normals;
1133     }
1134
1135     if (!(cddata_type & CD_FAKE)) {
1136       cd_src = &me_src->ldata;
1137       cd_dst = &me_dst->ldata;
1138
1139       if (!data_transfer_layersmapping_cdlayers(r_map,
1140                                                 cddata_type,
1141                                                 mix_mode,
1142                                                 mix_factor,
1143                                                 mix_weights,
1144                                                 num_elem_dst,
1145                                                 use_create,
1146                                                 use_delete,
1147                                                 cd_src,
1148                                                 cd_dst,
1149                                                 me_dst != ob_dst->data,
1150                                                 fromlayers,
1151                                                 tolayers,
1152                                                 interp,
1153                                                 interp_data)) {
1154         /* We handle specific source selection cases here. */
1155         return false;
1156       }
1157       return true;
1158     }
1159     else {
1160       return false;
1161     }
1162   }
1163   else if (elem_type == ME_POLY) {
1164     if (cddata_type == CD_FAKE_UV) {
1165       cddata_type = CD_MLOOPUV;
1166     }
1167
1168     if (!(cddata_type & CD_FAKE)) {
1169       cd_src = &me_src->pdata;
1170       cd_dst = &me_dst->pdata;
1171
1172       if (!data_transfer_layersmapping_cdlayers(r_map,
1173                                                 cddata_type,
1174                                                 mix_mode,
1175                                                 mix_factor,
1176                                                 mix_weights,
1177                                                 num_elem_dst,
1178                                                 use_create,
1179                                                 use_delete,
1180                                                 cd_src,
1181                                                 cd_dst,
1182                                                 me_dst != ob_dst->data,
1183                                                 fromlayers,
1184                                                 tolayers,
1185                                                 interp,
1186                                                 interp_data)) {
1187         /* We handle specific source selection cases here. */
1188         return false;
1189       }
1190       return true;
1191     }
1192     else if (r_map && cddata_type == CD_FAKE_SHARP) {
1193       const size_t elem_size = sizeof(*((MPoly *)NULL));
1194       const size_t data_size = sizeof(((MPoly *)NULL)->flag);
1195       const size_t data_offset = offsetof(MPoly, flag);
1196       const uint64_t data_flag = ME_SMOOTH;
1197
1198       data_transfer_layersmapping_add_item(r_map,
1199                                            cddata_type,
1200                                            mix_mode,
1201                                            mix_factor,
1202                                            mix_weights,
1203                                            me_src->mpoly,
1204                                            me_dst->mpoly,
1205                                            me_src->totpoly,
1206                                            me_dst->totpoly,
1207                                            elem_size,
1208                                            data_size,
1209                                            data_offset,
1210                                            data_flag,
1211                                            NULL,
1212                                            interp_data);
1213       return true;
1214     }
1215     else {
1216       return false;
1217     }
1218   }
1219
1220   return false;
1221 }
1222
1223 /**
1224  * Transfer data *layout* of selected types from source to destination object.
1225  * By default, it only creates new data layers if needed on \a ob_dst.
1226  * 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,
1227  * to get (as much as possible) exact copy of source data layout.
1228  */
1229 void BKE_object_data_transfer_layout(struct Depsgraph *depsgraph,
1230                                      Scene *scene,
1231                                      Object *ob_src,
1232                                      Object *ob_dst,
1233                                      const int data_types,
1234                                      const bool use_delete,
1235                                      const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1236                                      const int tolayers_select[DT_MULTILAYER_INDEX_MAX])
1237 {
1238   Mesh *me_src;
1239   Mesh *me_dst;
1240   int i;
1241
1242   const bool use_create = true; /* We always create needed layers here. */
1243
1244   CustomData_MeshMasks me_src_mask = CD_MASK_BAREMESH;
1245
1246   BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1247
1248   me_dst = ob_dst->data;
1249
1250   /* Get source evaluated mesh.*/
1251   BKE_object_data_transfer_dttypes_to_cdmask(data_types, &me_src_mask);
1252   me_src = mesh_get_eval_final(depsgraph, scene, ob_src, &me_src_mask);
1253   if (!me_src) {
1254     return;
1255   }
1256
1257   /* Check all possible data types. */
1258   for (i = 0; i < DT_TYPE_MAX; i++) {
1259     const int dtdata_type = 1 << i;
1260     int cddata_type;
1261     int fromlayers, tolayers, fromto_idx;
1262
1263     if (!(data_types & dtdata_type)) {
1264       continue;
1265     }
1266
1267     cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1268
1269     fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1270     if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1271       fromlayers = fromlayers_select[fromto_idx];
1272       tolayers = tolayers_select[fromto_idx];
1273     }
1274     else {
1275       fromlayers = tolayers = 0;
1276     }
1277
1278     if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1279       const int num_elem_dst = me_dst->totvert;
1280
1281       data_transfer_layersmapping_generate(NULL,
1282                                            ob_src,
1283                                            ob_dst,
1284                                            me_src,
1285                                            me_dst,
1286                                            ME_VERT,
1287                                            cddata_type,
1288                                            0,
1289                                            0.0f,
1290                                            NULL,
1291                                            num_elem_dst,
1292                                            use_create,
1293                                            use_delete,
1294                                            fromlayers,
1295                                            tolayers,
1296                                            NULL);
1297     }
1298     if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1299       const int num_elem_dst = me_dst->totedge;
1300
1301       data_transfer_layersmapping_generate(NULL,
1302                                            ob_src,
1303                                            ob_dst,
1304                                            me_src,
1305                                            me_dst,
1306                                            ME_EDGE,
1307                                            cddata_type,
1308                                            0,
1309                                            0.0f,
1310                                            NULL,
1311                                            num_elem_dst,
1312                                            use_create,
1313                                            use_delete,
1314                                            fromlayers,
1315                                            tolayers,
1316                                            NULL);
1317     }
1318     if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1319       const int num_elem_dst = me_dst->totloop;
1320
1321       data_transfer_layersmapping_generate(NULL,
1322                                            ob_src,
1323                                            ob_dst,
1324                                            me_src,
1325                                            me_dst,
1326                                            ME_LOOP,
1327                                            cddata_type,
1328                                            0,
1329                                            0.0f,
1330                                            NULL,
1331                                            num_elem_dst,
1332                                            use_create,
1333                                            use_delete,
1334                                            fromlayers,
1335                                            tolayers,
1336                                            NULL);
1337     }
1338     if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1339       const int num_elem_dst = me_dst->totpoly;
1340
1341       data_transfer_layersmapping_generate(NULL,
1342                                            ob_src,
1343                                            ob_dst,
1344                                            me_src,
1345                                            me_dst,
1346                                            ME_POLY,
1347                                            cddata_type,
1348                                            0,
1349                                            0.0f,
1350                                            NULL,
1351                                            num_elem_dst,
1352                                            use_create,
1353                                            use_delete,
1354                                            fromlayers,
1355                                            tolayers,
1356                                            NULL);
1357     }
1358   }
1359 }
1360
1361 bool BKE_object_data_transfer_ex(struct Depsgraph *depsgraph,
1362                                  Scene *scene,
1363                                  Object *ob_src,
1364                                  Object *ob_dst,
1365                                  Mesh *me_dst,
1366                                  const int data_types,
1367                                  bool use_create,
1368                                  const int map_vert_mode,
1369                                  const int map_edge_mode,
1370                                  const int map_loop_mode,
1371                                  const int map_poly_mode,
1372                                  SpaceTransform *space_transform,
1373                                  const bool auto_transform,
1374                                  const float max_distance,
1375                                  const float ray_radius,
1376                                  const float islands_handling_precision,
1377                                  const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1378                                  const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1379                                  const int mix_mode,
1380                                  const float mix_factor,
1381                                  const char *vgroup_name,
1382                                  const bool invert_vgroup,
1383                                  ReportList *reports)
1384 {
1385 #define VDATA 0
1386 #define EDATA 1
1387 #define LDATA 2
1388 #define PDATA 3
1389 #define DATAMAX 4
1390
1391   SpaceTransform auto_space_transform;
1392
1393   Mesh *me_src;
1394   bool dirty_nors_dst =
1395       true; /* Assumed always true if not using an evaluated mesh as destination. */
1396   int i;
1397
1398   MDeformVert *mdef = NULL;
1399   int vg_idx = -1;
1400   float *weights[DATAMAX] = {NULL};
1401
1402   MeshPairRemap geom_map[DATAMAX] = {{0}};
1403   bool geom_map_init[DATAMAX] = {0};
1404   ListBase lay_map = {NULL};
1405   bool changed = false;
1406   bool is_modifier = false;
1407
1408   const bool use_delete = false; /* We never delete data layers from destination here. */
1409
1410   CustomData_MeshMasks me_src_mask = CD_MASK_BAREMESH;
1411
1412   BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1413
1414   if (me_dst) {
1415     dirty_nors_dst = (me_dst->runtime.cd_dirty_vert & CD_NORMAL) != 0;
1416     /* Never create needed custom layers on passed destination mesh
1417      * (assumed to *not* be ob_dst->data, aka modifier case). */
1418     use_create = false;
1419     is_modifier = true;
1420   }
1421   else {
1422     me_dst = ob_dst->data;
1423   }
1424
1425   if (vgroup_name) {
1426     mdef = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
1427     if (mdef) {
1428       vg_idx = defgroup_name_index(ob_dst, vgroup_name);
1429     }
1430   }
1431
1432   /* Get source evaluated mesh.*/
1433   BKE_object_data_transfer_dttypes_to_cdmask(data_types, &me_src_mask);
1434   BKE_mesh_remap_calc_source_cddata_masks_from_map_modes(
1435       map_vert_mode, map_edge_mode, map_loop_mode, map_poly_mode, &me_src_mask);
1436   if (is_modifier) {
1437     me_src = BKE_modifier_get_evaluated_mesh_from_evaluated_object(ob_src, false);
1438
1439     if (me_src == NULL ||
1440         !CustomData_MeshMasks_are_matching(&ob_src->runtime.last_data_mask, &me_src_mask)) {
1441       CLOG_WARN(&LOG, "Data Transfer: source mesh data is not ready - dependency cycle?");
1442       return changed;
1443     }
1444   }
1445   else {
1446     me_src = mesh_get_eval_final(depsgraph, scene, ob_src, &me_src_mask);
1447   }
1448   if (!me_src) {
1449     return changed;
1450   }
1451
1452   if (auto_transform) {
1453     if (space_transform == NULL) {
1454       space_transform = &auto_space_transform;
1455     }
1456
1457     BKE_mesh_remap_find_best_match_from_mesh(
1458         me_dst->mvert, me_dst->totvert, me_src, space_transform);
1459   }
1460
1461   /* Check all possible data types.
1462    * Note item mappings and dest mix weights are cached. */
1463   for (i = 0; i < DT_TYPE_MAX; i++) {
1464     const int dtdata_type = 1 << i;
1465     int cddata_type;
1466     int fromlayers, tolayers, fromto_idx;
1467
1468     if (!(data_types & dtdata_type)) {
1469       continue;
1470     }
1471
1472     data_transfer_dtdata_type_preprocess(me_src, me_dst, dtdata_type, dirty_nors_dst);
1473
1474     cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1475
1476     fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1477     if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1478       fromlayers = fromlayers_select[fromto_idx];
1479       tolayers = tolayers_select[fromto_idx];
1480     }
1481     else {
1482       fromlayers = tolayers = 0;
1483     }
1484
1485     if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1486       MVert *verts_dst = me_dst->mvert;
1487       const int num_verts_dst = me_dst->totvert;
1488
1489       if (!geom_map_init[VDATA]) {
1490         const int num_verts_src = me_src->totvert;
1491
1492         if ((map_vert_mode == MREMAP_MODE_TOPOLOGY) && (num_verts_dst != num_verts_src)) {
1493           BKE_report(reports,
1494                      RPT_ERROR,
1495                      "Source and destination meshes do not have the same amount of vertices, "
1496                      "'Topology' mapping cannot be used in this case");
1497           continue;
1498         }
1499         if ((map_vert_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1500           BKE_report(reports,
1501                      RPT_ERROR,
1502                      "Source mesh doesn't have any edges, "
1503                      "None of the 'Edge' mappings can be used in this case");
1504           continue;
1505         }
1506         if ((map_vert_mode & MREMAP_USE_POLY) && (me_src->totpoly == 0)) {
1507           BKE_report(reports,
1508                      RPT_ERROR,
1509                      "Source mesh doesn't have any faces, "
1510                      "None of the 'Face' mappings can be used in this case");
1511           continue;
1512         }
1513         if (ELEM(0, num_verts_dst, num_verts_src)) {
1514           BKE_report(reports,
1515                      RPT_ERROR,
1516                      "Source or destination meshes do not have any vertices, cannot transfer "
1517                      "vertex data");
1518           continue;
1519         }
1520
1521         BKE_mesh_remap_calc_verts_from_mesh(map_vert_mode,
1522                                             space_transform,
1523                                             max_distance,
1524                                             ray_radius,
1525                                             verts_dst,
1526                                             num_verts_dst,
1527                                             dirty_nors_dst,
1528                                             me_src,
1529                                             &geom_map[VDATA]);
1530         geom_map_init[VDATA] = true;
1531       }
1532
1533       if (mdef && vg_idx != -1 && !weights[VDATA]) {
1534         weights[VDATA] = MEM_mallocN(sizeof(*(weights[VDATA])) * (size_t)num_verts_dst, __func__);
1535         BKE_defvert_extract_vgroup_to_vertweights(
1536             mdef, vg_idx, num_verts_dst, weights[VDATA], invert_vgroup);
1537       }
1538
1539       if (data_transfer_layersmapping_generate(&lay_map,
1540                                                ob_src,
1541                                                ob_dst,
1542                                                me_src,
1543                                                me_dst,
1544                                                ME_VERT,
1545                                                cddata_type,
1546                                                mix_mode,
1547                                                mix_factor,
1548                                                weights[VDATA],
1549                                                num_verts_dst,
1550                                                use_create,
1551                                                use_delete,
1552                                                fromlayers,
1553                                                tolayers,
1554                                                space_transform)) {
1555         CustomDataTransferLayerMap *lay_mapit;
1556
1557         changed = (lay_map.first != NULL);
1558
1559         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1560           CustomData_data_transfer(&geom_map[VDATA], lay_mapit);
1561         }
1562
1563         BLI_freelistN(&lay_map);
1564       }
1565     }
1566     if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1567       MVert *verts_dst = me_dst->mvert;
1568       const int num_verts_dst = me_dst->totvert;
1569       MEdge *edges_dst = me_dst->medge;
1570       const int num_edges_dst = me_dst->totedge;
1571
1572       if (!geom_map_init[EDATA]) {
1573         const int num_edges_src = me_src->totedge;
1574
1575         if ((map_edge_mode == MREMAP_MODE_TOPOLOGY) && (num_edges_dst != num_edges_src)) {
1576           BKE_report(reports,
1577                      RPT_ERROR,
1578                      "Source and destination meshes do not have the same amount of edges, "
1579                      "'Topology' mapping cannot be used in this case");
1580           continue;
1581         }
1582         if ((map_edge_mode & MREMAP_USE_POLY) && (me_src->totpoly == 0)) {
1583           BKE_report(reports,
1584                      RPT_ERROR,
1585                      "Source mesh doesn't have any faces, "
1586                      "None of the 'Face' mappings can be used in this case");
1587           continue;
1588         }
1589         if (ELEM(0, num_edges_dst, num_edges_src)) {
1590           BKE_report(
1591               reports,
1592               RPT_ERROR,
1593               "Source or destination meshes do not have any edges, cannot transfer edge data");
1594           continue;
1595         }
1596
1597         BKE_mesh_remap_calc_edges_from_mesh(map_edge_mode,
1598                                             space_transform,
1599                                             max_distance,
1600                                             ray_radius,
1601                                             verts_dst,
1602                                             num_verts_dst,
1603                                             edges_dst,
1604                                             num_edges_dst,
1605                                             dirty_nors_dst,
1606                                             me_src,
1607                                             &geom_map[EDATA]);
1608         geom_map_init[EDATA] = true;
1609       }
1610
1611       if (mdef && vg_idx != -1 && !weights[EDATA]) {
1612         weights[EDATA] = MEM_mallocN(sizeof(*weights[EDATA]) * (size_t)num_edges_dst, __func__);
1613         BKE_defvert_extract_vgroup_to_edgeweights(
1614             mdef, vg_idx, num_verts_dst, edges_dst, num_edges_dst, weights[EDATA], invert_vgroup);
1615       }
1616
1617       if (data_transfer_layersmapping_generate(&lay_map,
1618                                                ob_src,
1619                                                ob_dst,
1620                                                me_src,
1621                                                me_dst,
1622                                                ME_EDGE,
1623                                                cddata_type,
1624                                                mix_mode,
1625                                                mix_factor,
1626                                                weights[EDATA],
1627                                                num_edges_dst,
1628                                                use_create,
1629                                                use_delete,
1630                                                fromlayers,
1631                                                tolayers,
1632                                                space_transform)) {
1633         CustomDataTransferLayerMap *lay_mapit;
1634
1635         changed = (lay_map.first != NULL);
1636
1637         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1638           CustomData_data_transfer(&geom_map[EDATA], lay_mapit);
1639         }
1640
1641         BLI_freelistN(&lay_map);
1642       }
1643     }
1644     if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1645       MVert *verts_dst = me_dst->mvert;
1646       const int num_verts_dst = me_dst->totvert;
1647       MEdge *edges_dst = me_dst->medge;
1648       const int num_edges_dst = me_dst->totedge;
1649       MPoly *polys_dst = me_dst->mpoly;
1650       const int num_polys_dst = me_dst->totpoly;
1651       MLoop *loops_dst = me_dst->mloop;
1652       const int num_loops_dst = me_dst->totloop;
1653       CustomData *pdata_dst = &me_dst->pdata;
1654       CustomData *ldata_dst = &me_dst->ldata;
1655
1656       MeshRemapIslandsCalc island_callback = data_transfer_get_loop_islands_generator(cddata_type);
1657
1658       if (!geom_map_init[LDATA]) {
1659         const int num_loops_src = me_src->totloop;
1660
1661         if ((map_loop_mode == MREMAP_MODE_TOPOLOGY) && (num_loops_dst != num_loops_src)) {
1662           BKE_report(reports,
1663                      RPT_ERROR,
1664                      "Source and destination meshes do not have the same amount of face corners, "
1665                      "'Topology' mapping cannot be used in this case");
1666           continue;
1667         }
1668         if ((map_loop_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1669           BKE_report(reports,
1670                      RPT_ERROR,
1671                      "Source mesh doesn't have any edges, "
1672                      "None of the 'Edge' mappings can be used in this case");
1673           continue;
1674         }
1675         if (ELEM(0, num_loops_dst, num_loops_src)) {
1676           BKE_report(
1677               reports,
1678               RPT_ERROR,
1679               "Source or destination meshes do not have any faces, cannot transfer corner data");
1680           continue;
1681         }
1682
1683         BKE_mesh_remap_calc_loops_from_mesh(map_loop_mode,
1684                                             space_transform,
1685                                             max_distance,
1686                                             ray_radius,
1687                                             verts_dst,
1688                                             num_verts_dst,
1689                                             edges_dst,
1690                                             num_edges_dst,
1691                                             loops_dst,
1692                                             num_loops_dst,
1693                                             polys_dst,
1694                                             num_polys_dst,
1695                                             ldata_dst,
1696                                             pdata_dst,
1697                                             (me_dst->flag & ME_AUTOSMOOTH) != 0,
1698                                             me_dst->smoothresh,
1699                                             dirty_nors_dst,
1700                                             me_src,
1701                                             island_callback,
1702                                             islands_handling_precision,
1703                                             &geom_map[LDATA]);
1704         geom_map_init[LDATA] = true;
1705       }
1706
1707       if (mdef && vg_idx != -1 && !weights[LDATA]) {
1708         weights[LDATA] = MEM_mallocN(sizeof(*weights[LDATA]) * (size_t)num_loops_dst, __func__);
1709         BKE_defvert_extract_vgroup_to_loopweights(
1710             mdef, vg_idx, num_verts_dst, loops_dst, num_loops_dst, weights[LDATA], invert_vgroup);
1711       }
1712
1713       if (data_transfer_layersmapping_generate(&lay_map,
1714                                                ob_src,
1715                                                ob_dst,
1716                                                me_src,
1717                                                me_dst,
1718                                                ME_LOOP,
1719                                                cddata_type,
1720                                                mix_mode,
1721                                                mix_factor,
1722                                                weights[LDATA],
1723                                                num_loops_dst,
1724                                                use_create,
1725                                                use_delete,
1726                                                fromlayers,
1727                                                tolayers,
1728                                                space_transform)) {
1729         CustomDataTransferLayerMap *lay_mapit;
1730
1731         changed = (lay_map.first != NULL);
1732
1733         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1734           CustomData_data_transfer(&geom_map[LDATA], lay_mapit);
1735         }
1736
1737         BLI_freelistN(&lay_map);
1738       }
1739     }
1740     if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1741       MVert *verts_dst = me_dst->mvert;
1742       const int num_verts_dst = me_dst->totvert;
1743       MPoly *polys_dst = me_dst->mpoly;
1744       const int num_polys_dst = me_dst->totpoly;
1745       MLoop *loops_dst = me_dst->mloop;
1746       const int num_loops_dst = me_dst->totloop;
1747       CustomData *pdata_dst = &me_dst->pdata;
1748
1749       if (!geom_map_init[PDATA]) {
1750         const int num_polys_src = me_src->totpoly;
1751
1752         if ((map_poly_mode == MREMAP_MODE_TOPOLOGY) && (num_polys_dst != num_polys_src)) {
1753           BKE_report(reports,
1754                      RPT_ERROR,
1755                      "Source and destination meshes do not have the same amount of faces, "
1756                      "'Topology' mapping cannot be used in this case");
1757           continue;
1758         }
1759         if ((map_poly_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1760           BKE_report(reports,
1761                      RPT_ERROR,
1762                      "Source mesh doesn't have any edges, "
1763                      "None of the 'Edge' mappings can be used in this case");
1764           continue;
1765         }
1766         if (ELEM(0, num_polys_dst, num_polys_src)) {
1767           BKE_report(
1768               reports,
1769               RPT_ERROR,
1770               "Source or destination meshes do not have any faces, cannot transfer face data");
1771           continue;
1772         }
1773
1774         BKE_mesh_remap_calc_polys_from_mesh(map_poly_mode,
1775                                             space_transform,
1776                                             max_distance,
1777                                             ray_radius,
1778                                             verts_dst,
1779                                             num_verts_dst,
1780                                             loops_dst,
1781                                             num_loops_dst,
1782                                             polys_dst,
1783                                             num_polys_dst,
1784                                             pdata_dst,
1785                                             dirty_nors_dst,
1786                                             me_src,
1787                                             &geom_map[PDATA]);
1788         geom_map_init[PDATA] = true;
1789       }
1790
1791       if (mdef && vg_idx != -1 && !weights[PDATA]) {
1792         weights[PDATA] = MEM_mallocN(sizeof(*weights[PDATA]) * (size_t)num_polys_dst, __func__);
1793         BKE_defvert_extract_vgroup_to_polyweights(mdef,
1794                                                   vg_idx,
1795                                                   num_verts_dst,
1796                                                   loops_dst,
1797                                                   num_loops_dst,
1798                                                   polys_dst,
1799                                                   num_polys_dst,
1800                                                   weights[PDATA],
1801                                                   invert_vgroup);
1802       }
1803
1804       if (data_transfer_layersmapping_generate(&lay_map,
1805                                                ob_src,
1806                                                ob_dst,
1807                                                me_src,
1808                                                me_dst,
1809                                                ME_POLY,
1810                                                cddata_type,
1811                                                mix_mode,
1812                                                mix_factor,
1813                                                weights[PDATA],
1814                                                num_polys_dst,
1815                                                use_create,
1816                                                use_delete,
1817                                                fromlayers,
1818                                                tolayers,
1819                                                space_transform)) {
1820         CustomDataTransferLayerMap *lay_mapit;
1821
1822         changed = (lay_map.first != NULL);
1823
1824         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1825           CustomData_data_transfer(&geom_map[PDATA], lay_mapit);
1826         }
1827
1828         BLI_freelistN(&lay_map);
1829       }
1830     }
1831
1832     data_transfer_dtdata_type_postprocess(ob_src, ob_dst, me_src, me_dst, dtdata_type, changed);
1833   }
1834
1835   for (i = 0; i < DATAMAX; i++) {
1836     BKE_mesh_remap_free(&geom_map[i]);
1837     MEM_SAFE_FREE(weights[i]);
1838   }
1839
1840   return changed;
1841
1842 #undef VDATA
1843 #undef EDATA
1844 #undef LDATA
1845 #undef PDATA
1846 #undef DATAMAX
1847 }
1848
1849 bool BKE_object_data_transfer_mesh(struct Depsgraph *depsgraph,
1850                                    Scene *scene,
1851                                    Object *ob_src,
1852                                    Object *ob_dst,
1853                                    const int data_types,
1854                                    const bool use_create,
1855                                    const int map_vert_mode,
1856                                    const int map_edge_mode,
1857                                    const int map_loop_mode,
1858                                    const int map_poly_mode,
1859                                    SpaceTransform *space_transform,
1860                                    const bool auto_transform,
1861                                    const float max_distance,
1862                                    const float ray_radius,
1863                                    const float islands_handling_precision,
1864                                    const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1865                                    const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1866                                    const int mix_mode,
1867                                    const float mix_factor,
1868                                    const char *vgroup_name,
1869                                    const bool invert_vgroup,
1870                                    ReportList *reports)
1871 {
1872   return BKE_object_data_transfer_ex(depsgraph,
1873                                      scene,
1874                                      ob_src,
1875                                      ob_dst,
1876                                      NULL,
1877                                      data_types,
1878                                      use_create,
1879                                      map_vert_mode,
1880                                      map_edge_mode,
1881                                      map_loop_mode,
1882                                      map_poly_mode,
1883                                      space_transform,
1884                                      auto_transform,
1885                                      max_distance,
1886                                      ray_radius,
1887                                      islands_handling_precision,
1888                                      fromlayers_select,
1889                                      tolayers_select,
1890                                      mix_mode,
1891                                      mix_factor,
1892                                      vgroup_name,
1893                                      invert_vgroup,
1894                                      reports);
1895 }