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