Cleanup: style, use braces for 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 (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       }
587       idx_src++;
588
589       if (idx_dst < idx_src) {
590         if (use_create) {
591           /* Create as much data layers as necessary! */
592           for (; idx_dst < idx_src; idx_dst++) {
593             CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
594           }
595         }
596         else {
597           /* Otherwise, just try to map what we can with existing dst data layers. */
598           idx_src = idx_dst;
599         }
600       }
601       else if (use_delete && idx_dst > idx_src) {
602         while (idx_dst-- > idx_src) {
603           CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
604         }
605       }
606       if (r_map) {
607         while (idx_src--) {
608           if (!use_layers_src[idx_src]) {
609             continue;
610           }
611           data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
612           /* If dest is a evaluated mesh (fro; ;odifier), we do not want to overwrite cdlayers of orig mesh! */
613           if (use_dupref_dst) {
614             data_dst = CustomData_duplicate_referenced_layer_n(
615                 cd_dst, cddata_type, idx_src, num_elem_dst);
616           }
617           else {
618             data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_src);
619           }
620           data_transfer_layersmapping_add_item_cd(r_map,
621                                                   cddata_type,
622                                                   mix_mode,
623                                                   mix_factor,
624                                                   mix_weights,
625                                                   data_src,
626                                                   data_dst,
627                                                   interp,
628                                                   interp_data);
629         }
630       }
631       break;
632     case DT_LAYERS_NAME_DST:
633       if (use_delete) {
634         if (tot_dst) {
635           data_dst_to_delete = MEM_mallocN(sizeof(*data_dst_to_delete) * (size_t)tot_dst,
636                                            __func__);
637           memset(data_dst_to_delete, true, sizeof(*data_dst_to_delete) * (size_t)tot_dst);
638         }
639       }
640
641       while (idx_src--) {
642         const char *name;
643
644         if (!use_layers_src[idx_src]) {
645           continue;
646         }
647
648         name = CustomData_get_layer_name(cd_src, cddata_type, idx_src);
649         data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
650
651         if ((idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name)) == -1) {
652           if (use_create) {
653             CustomData_add_layer_named(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst, name);
654             idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name);
655           }
656           else {
657             /* If we are not allowed to create missing dst data layers, just skip matching src one. */
658             continue;
659           }
660         }
661         else if (data_dst_to_delete) {
662           data_dst_to_delete[idx_dst] = false;
663         }
664         if (r_map) {
665           /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
666           if (use_dupref_dst) {
667             data_dst = CustomData_duplicate_referenced_layer_n(
668                 cd_dst, cddata_type, idx_dst, num_elem_dst);
669           }
670           else {
671             data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
672           }
673           data_transfer_layersmapping_add_item_cd(r_map,
674                                                   cddata_type,
675                                                   mix_mode,
676                                                   mix_factor,
677                                                   mix_weights,
678                                                   data_src,
679                                                   data_dst,
680                                                   interp,
681                                                   interp_data);
682         }
683       }
684
685       if (data_dst_to_delete) {
686         /* Note: This won't affect newly created layers, if any, since tot_dst has not been updated!
687          *       Also, looping backward ensures us we do not suffer from index shifting when deleting a layer.
688          */
689         for (idx_dst = tot_dst; idx_dst--;) {
690           if (data_dst_to_delete[idx_dst]) {
691             CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, idx_dst);
692           }
693         }
694
695         MEM_freeN(data_dst_to_delete);
696       }
697       break;
698     default:
699       return false;
700   }
701
702   return true;
703 }
704
705 static bool data_transfer_layersmapping_cdlayers(ListBase *r_map,
706                                                  const int cddata_type,
707                                                  const int mix_mode,
708                                                  const float mix_factor,
709                                                  const float *mix_weights,
710                                                  const int num_elem_dst,
711                                                  const bool use_create,
712                                                  const bool use_delete,
713                                                  CustomData *cd_src,
714                                                  CustomData *cd_dst,
715                                                  const bool use_dupref_dst,
716                                                  const int fromlayers,
717                                                  const int tolayers,
718                                                  cd_datatransfer_interp interp,
719                                                  void *interp_data)
720 {
721   int idx_src, idx_dst;
722   void *data_src, *data_dst = NULL;
723
724   if (CustomData_layertype_is_singleton(cddata_type)) {
725     if (!(data_src = CustomData_get_layer(cd_src, cddata_type))) {
726       if (use_delete) {
727         CustomData_free_layer(cd_dst, cddata_type, num_elem_dst, 0);
728       }
729       return true;
730     }
731
732     data_dst = CustomData_get_layer(cd_dst, cddata_type);
733     if (!data_dst) {
734       if (!use_create) {
735         return true;
736       }
737       data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
738     }
739     else if (use_dupref_dst && r_map) {
740       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
741       data_dst = CustomData_duplicate_referenced_layer(cd_dst, cddata_type, num_elem_dst);
742     }
743
744     if (r_map) {
745       data_transfer_layersmapping_add_item_cd(r_map,
746                                               cddata_type,
747                                               mix_mode,
748                                               mix_factor,
749                                               mix_weights,
750                                               data_src,
751                                               data_dst,
752                                               interp,
753                                               interp_data);
754     }
755   }
756   else if (fromlayers == DT_LAYERS_ACTIVE_SRC || fromlayers >= 0) {
757     /* Note: use_delete has not much meaning in this case, ignored. */
758
759     if (fromlayers >= 0) { /* Real-layer index */
760       idx_src = fromlayers;
761     }
762     else {
763       if ((idx_src = CustomData_get_active_layer(cd_src, cddata_type)) == -1) {
764         return true;
765       }
766     }
767     data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src);
768     if (!data_src) {
769       return true;
770     }
771
772     if (tolayers >= 0) { /* Real-layer index */
773       idx_dst = tolayers;
774       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
775       if (use_dupref_dst && r_map) {
776         data_dst = CustomData_duplicate_referenced_layer_n(
777             cd_dst, cddata_type, idx_dst, num_elem_dst);
778       }
779       else {
780         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
781       }
782     }
783     else if (tolayers == DT_LAYERS_ACTIVE_DST) {
784       if ((idx_dst = CustomData_get_active_layer(cd_dst, cddata_type)) == -1) {
785         if (!use_create) {
786           return true;
787         }
788         data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
789       }
790       else {
791         /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
792         if (use_dupref_dst && r_map) {
793           data_dst = CustomData_duplicate_referenced_layer_n(
794               cd_dst, cddata_type, idx_dst, num_elem_dst);
795         }
796         else {
797           data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
798         }
799       }
800     }
801     else if (tolayers == DT_LAYERS_INDEX_DST) {
802       int num = CustomData_number_of_layers(cd_dst, cddata_type);
803       idx_dst = idx_src;
804       if (num <= idx_dst) {
805         if (!use_create) {
806           return true;
807         }
808         /* Create as much data layers as necessary! */
809         for (; num <= idx_dst; num++) {
810           CustomData_add_layer(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst);
811         }
812       }
813       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
814       if (use_dupref_dst && r_map) {
815         data_dst = CustomData_duplicate_referenced_layer_n(
816             cd_dst, cddata_type, idx_dst, num_elem_dst);
817       }
818       else {
819         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
820       }
821     }
822     else if (tolayers == DT_LAYERS_NAME_DST) {
823       const char *name = CustomData_get_layer_name(cd_src, cddata_type, idx_src);
824       if ((idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name)) == -1) {
825         if (!use_create) {
826           return true;
827         }
828         CustomData_add_layer_named(cd_dst, cddata_type, CD_CALLOC, NULL, num_elem_dst, name);
829         idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name);
830       }
831       /* If dest is a evaluated mesh (from modifier), we do not want to overwrite cdlayers of orig mesh! */
832       if (use_dupref_dst && r_map) {
833         data_dst = CustomData_duplicate_referenced_layer_n(
834             cd_dst, cddata_type, idx_dst, num_elem_dst);
835       }
836       else {
837         data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst);
838       }
839     }
840     else {
841       return false;
842     }
843
844     if (!data_dst) {
845       return false;
846     }
847
848     if (r_map) {
849       data_transfer_layersmapping_add_item_cd(r_map,
850                                               cddata_type,
851                                               mix_mode,
852                                               mix_factor,
853                                               mix_weights,
854                                               data_src,
855                                               data_dst,
856                                               interp,
857                                               interp_data);
858     }
859   }
860   else if (fromlayers == DT_LAYERS_ALL_SRC) {
861     int num_src = CustomData_number_of_layers(cd_src, cddata_type);
862     bool *use_layers_src = num_src ?
863                                MEM_mallocN(sizeof(*use_layers_src) * (size_t)num_src, __func__) :
864                                NULL;
865     bool ret;
866
867     if (use_layers_src) {
868       memset(use_layers_src, true, sizeof(*use_layers_src) * num_src);
869     }
870
871     ret = data_transfer_layersmapping_cdlayers_multisrc_to_dst(r_map,
872                                                                cddata_type,
873                                                                mix_mode,
874                                                                mix_factor,
875                                                                mix_weights,
876                                                                num_elem_dst,
877                                                                use_create,
878                                                                use_delete,
879                                                                cd_src,
880                                                                cd_dst,
881                                                                use_dupref_dst,
882                                                                tolayers,
883                                                                use_layers_src,
884                                                                num_src,
885                                                                interp,
886                                                                interp_data);
887
888     if (use_layers_src) {
889       MEM_freeN(use_layers_src);
890     }
891     return ret;
892   }
893   else {
894     return false;
895   }
896
897   return true;
898 }
899
900 static bool data_transfer_layersmapping_generate(ListBase *r_map,
901                                                  Object *ob_src,
902                                                  Object *ob_dst,
903                                                  Mesh *me_src,
904                                                  Mesh *me_dst,
905                                                  const int elem_type,
906                                                  int cddata_type,
907                                                  int mix_mode,
908                                                  float mix_factor,
909                                                  const float *mix_weights,
910                                                  const int num_elem_dst,
911                                                  const bool use_create,
912                                                  const bool use_delete,
913                                                  const int fromlayers,
914                                                  const int tolayers,
915                                                  SpaceTransform *space_transform)
916 {
917   CustomData *cd_src, *cd_dst;
918
919   cd_datatransfer_interp interp = NULL;
920   void *interp_data = NULL;
921
922   if (elem_type == ME_VERT) {
923     if (!(cddata_type & CD_FAKE)) {
924       cd_src = &me_src->vdata;
925       cd_dst = &me_dst->vdata;
926
927       if (!data_transfer_layersmapping_cdlayers(r_map,
928                                                 cddata_type,
929                                                 mix_mode,
930                                                 mix_factor,
931                                                 mix_weights,
932                                                 num_elem_dst,
933                                                 use_create,
934                                                 use_delete,
935                                                 cd_src,
936                                                 cd_dst,
937                                                 me_dst != ob_dst->data,
938                                                 fromlayers,
939                                                 tolayers,
940                                                 interp,
941                                                 interp_data)) {
942         /* We handle specific source selection cases here. */
943         return false;
944       }
945       return true;
946     }
947     else if (cddata_type == CD_FAKE_BWEIGHT) {
948       const size_t elem_size = sizeof(*((MVert *)NULL));
949       const size_t data_size = sizeof(((MVert *)NULL)->bweight);
950       const size_t data_offset = offsetof(MVert, bweight);
951       const uint64_t data_flag = 0;
952
953       if (!(me_src->cd_flag & ME_CDFLAG_VERT_BWEIGHT)) {
954         if (use_delete) {
955           me_dst->cd_flag &= ~ME_CDFLAG_VERT_BWEIGHT;
956         }
957         return true;
958       }
959       me_dst->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
960       if (r_map) {
961         data_transfer_layersmapping_add_item(r_map,
962                                              cddata_type,
963                                              mix_mode,
964                                              mix_factor,
965                                              mix_weights,
966                                              me_src->mvert,
967                                              me_dst->mvert,
968                                              me_src->totvert,
969                                              me_dst->totvert,
970                                              elem_size,
971                                              data_size,
972                                              data_offset,
973                                              data_flag,
974                                              data_transfer_interp_char,
975                                              interp_data);
976       }
977       return true;
978     }
979     else if (cddata_type == CD_FAKE_MDEFORMVERT) {
980       bool ret;
981
982       cd_src = &me_src->vdata;
983       cd_dst = &me_dst->vdata;
984
985       ret = data_transfer_layersmapping_vgroups(r_map,
986                                                 mix_mode,
987                                                 mix_factor,
988                                                 mix_weights,
989                                                 num_elem_dst,
990                                                 use_create,
991                                                 use_delete,
992                                                 ob_src,
993                                                 ob_dst,
994                                                 cd_src,
995                                                 cd_dst,
996                                                 me_dst != ob_dst->data,
997                                                 fromlayers,
998                                                 tolayers);
999
1000       /* Mesh stores its dvert in a specific pointer too. :( */
1001       me_dst->dvert = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
1002       return ret;
1003     }
1004     else if (cddata_type == CD_FAKE_SHAPEKEY) {
1005       /* TODO: leaving shapekeys aside for now, quite specific case, since we can't access them from MVert :/ */
1006       return false;
1007     }
1008   }
1009   else if (elem_type == ME_EDGE) {
1010     if (!(cddata_type & CD_FAKE)) { /* Unused for edges, currently... */
1011       cd_src = &me_src->edata;
1012       cd_dst = &me_dst->edata;
1013
1014       if (!data_transfer_layersmapping_cdlayers(r_map,
1015                                                 cddata_type,
1016                                                 mix_mode,
1017                                                 mix_factor,
1018                                                 mix_weights,
1019                                                 num_elem_dst,
1020                                                 use_create,
1021                                                 use_delete,
1022                                                 cd_src,
1023                                                 cd_dst,
1024                                                 me_dst != ob_dst->data,
1025                                                 fromlayers,
1026                                                 tolayers,
1027                                                 interp,
1028                                                 interp_data)) {
1029         /* We handle specific source selection cases here. */
1030         return false;
1031       }
1032       return true;
1033     }
1034     else if (cddata_type == CD_FAKE_CREASE) {
1035       const size_t elem_size = sizeof(*((MEdge *)NULL));
1036       const size_t data_size = sizeof(((MEdge *)NULL)->crease);
1037       const size_t data_offset = offsetof(MEdge, crease);
1038       const uint64_t data_flag = 0;
1039
1040       if (!(me_src->cd_flag & ME_CDFLAG_EDGE_CREASE)) {
1041         if (use_delete && !me_dst) {
1042           me_dst->cd_flag &= ~ME_CDFLAG_EDGE_CREASE;
1043         }
1044         return true;
1045       }
1046       me_dst->cd_flag |= ME_CDFLAG_EDGE_CREASE;
1047       if (r_map) {
1048         data_transfer_layersmapping_add_item(r_map,
1049                                              cddata_type,
1050                                              mix_mode,
1051                                              mix_factor,
1052                                              mix_weights,
1053                                              me_src->medge,
1054                                              me_dst->medge,
1055                                              me_src->totedge,
1056                                              me_dst->totedge,
1057                                              elem_size,
1058                                              data_size,
1059                                              data_offset,
1060                                              data_flag,
1061                                              data_transfer_interp_char,
1062                                              interp_data);
1063       }
1064       return true;
1065     }
1066     else if (cddata_type == CD_FAKE_BWEIGHT) {
1067       const size_t elem_size = sizeof(*((MEdge *)NULL));
1068       const size_t data_size = sizeof(((MEdge *)NULL)->bweight);
1069       const size_t data_offset = offsetof(MEdge, bweight);
1070       const uint64_t data_flag = 0;
1071
1072       if (!(me_src->cd_flag & ME_CDFLAG_EDGE_BWEIGHT)) {
1073         if (use_delete && !me_dst) {
1074           me_dst->cd_flag &= ~ME_CDFLAG_EDGE_BWEIGHT;
1075         }
1076         return true;
1077       }
1078       me_dst->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
1079       if (r_map) {
1080         data_transfer_layersmapping_add_item(r_map,
1081                                              cddata_type,
1082                                              mix_mode,
1083                                              mix_factor,
1084                                              mix_weights,
1085                                              me_src->medge,
1086                                              me_dst->medge,
1087                                              me_src->totedge,
1088                                              me_dst->totedge,
1089                                              elem_size,
1090                                              data_size,
1091                                              data_offset,
1092                                              data_flag,
1093                                              data_transfer_interp_char,
1094                                              interp_data);
1095       }
1096       return true;
1097     }
1098     else if (r_map && ELEM(cddata_type, CD_FAKE_SHARP, CD_FAKE_SEAM)) {
1099       const size_t elem_size = sizeof(*((MEdge *)NULL));
1100       const size_t data_size = sizeof(((MEdge *)NULL)->flag);
1101       const size_t data_offset = offsetof(MEdge, flag);
1102       const uint64_t data_flag = (cddata_type == CD_FAKE_SHARP) ? ME_SHARP : ME_SEAM;
1103
1104       data_transfer_layersmapping_add_item(r_map,
1105                                            cddata_type,
1106                                            mix_mode,
1107                                            mix_factor,
1108                                            mix_weights,
1109                                            me_src->medge,
1110                                            me_dst->medge,
1111                                            me_src->totedge,
1112                                            me_dst->totedge,
1113                                            elem_size,
1114                                            data_size,
1115                                            data_offset,
1116                                            data_flag,
1117                                            NULL,
1118                                            interp_data);
1119       return true;
1120     }
1121     else {
1122       return false;
1123     }
1124   }
1125   else if (elem_type == ME_LOOP) {
1126     if (cddata_type == CD_FAKE_UV) {
1127       cddata_type = CD_MLOOPUV;
1128     }
1129     else if (cddata_type == CD_FAKE_LNOR) {
1130       /* Preprocess should have generated it, Postprocess will convert it back to CD_CUSTOMLOOPNORMAL. */
1131       cddata_type = CD_NORMAL;
1132       interp_data = space_transform;
1133       interp = customdata_data_transfer_interp_normal_normals;
1134     }
1135
1136     if (!(cddata_type & CD_FAKE)) {
1137       cd_src = &me_src->ldata;
1138       cd_dst = &me_dst->ldata;
1139
1140       if (!data_transfer_layersmapping_cdlayers(r_map,
1141                                                 cddata_type,
1142                                                 mix_mode,
1143                                                 mix_factor,
1144                                                 mix_weights,
1145                                                 num_elem_dst,
1146                                                 use_create,
1147                                                 use_delete,
1148                                                 cd_src,
1149                                                 cd_dst,
1150                                                 me_dst != ob_dst->data,
1151                                                 fromlayers,
1152                                                 tolayers,
1153                                                 interp,
1154                                                 interp_data)) {
1155         /* We handle specific source selection cases here. */
1156         return false;
1157       }
1158       return true;
1159     }
1160     else {
1161       return false;
1162     }
1163   }
1164   else if (elem_type == ME_POLY) {
1165     if (cddata_type == CD_FAKE_UV) {
1166       cddata_type = CD_MLOOPUV;
1167     }
1168
1169     if (!(cddata_type & CD_FAKE)) {
1170       cd_src = &me_src->pdata;
1171       cd_dst = &me_dst->pdata;
1172
1173       if (!data_transfer_layersmapping_cdlayers(r_map,
1174                                                 cddata_type,
1175                                                 mix_mode,
1176                                                 mix_factor,
1177                                                 mix_weights,
1178                                                 num_elem_dst,
1179                                                 use_create,
1180                                                 use_delete,
1181                                                 cd_src,
1182                                                 cd_dst,
1183                                                 me_dst != ob_dst->data,
1184                                                 fromlayers,
1185                                                 tolayers,
1186                                                 interp,
1187                                                 interp_data)) {
1188         /* We handle specific source selection cases here. */
1189         return false;
1190       }
1191       return true;
1192     }
1193     else if (r_map && cddata_type == CD_FAKE_SHARP) {
1194       const size_t elem_size = sizeof(*((MPoly *)NULL));
1195       const size_t data_size = sizeof(((MPoly *)NULL)->flag);
1196       const size_t data_offset = offsetof(MPoly, flag);
1197       const uint64_t data_flag = ME_SMOOTH;
1198
1199       data_transfer_layersmapping_add_item(r_map,
1200                                            cddata_type,
1201                                            mix_mode,
1202                                            mix_factor,
1203                                            mix_weights,
1204                                            me_src->mpoly,
1205                                            me_dst->mpoly,
1206                                            me_src->totpoly,
1207                                            me_dst->totpoly,
1208                                            elem_size,
1209                                            data_size,
1210                                            data_offset,
1211                                            data_flag,
1212                                            NULL,
1213                                            interp_data);
1214       return true;
1215     }
1216     else {
1217       return false;
1218     }
1219   }
1220
1221   return false;
1222 }
1223
1224 /**
1225  * Transfer data *layout* of selected types from source to destination object.
1226  * By default, it only creates new data layers if needed on \a ob_dst.
1227  * 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,
1228  * to get (as much as possible) exact copy of source data layout.
1229  */
1230 void BKE_object_data_transfer_layout(struct Depsgraph *depsgraph,
1231                                      Scene *scene,
1232                                      Object *ob_src,
1233                                      Object *ob_dst,
1234                                      const int data_types,
1235                                      const bool use_delete,
1236                                      const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1237                                      const int tolayers_select[DT_MULTILAYER_INDEX_MAX])
1238 {
1239   Mesh *me_src;
1240   Mesh *me_dst;
1241   int i;
1242
1243   const bool use_create = true; /* We always create needed layers here. */
1244
1245   CustomData_MeshMasks me_src_mask = CD_MASK_BAREMESH;
1246
1247   BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1248
1249   me_dst = ob_dst->data;
1250
1251   /* Get source evaluated mesh.*/
1252   BKE_object_data_transfer_dttypes_to_cdmask(data_types, &me_src_mask);
1253   me_src = mesh_get_eval_final(depsgraph, scene, ob_src, &me_src_mask);
1254   if (!me_src) {
1255     return;
1256   }
1257
1258   /* Check all possible data types. */
1259   for (i = 0; i < DT_TYPE_MAX; i++) {
1260     const int dtdata_type = 1 << i;
1261     int cddata_type;
1262     int fromlayers, tolayers, fromto_idx;
1263
1264     if (!(data_types & dtdata_type)) {
1265       continue;
1266     }
1267
1268     cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1269
1270     fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1271     if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1272       fromlayers = fromlayers_select[fromto_idx];
1273       tolayers = tolayers_select[fromto_idx];
1274     }
1275     else {
1276       fromlayers = tolayers = 0;
1277     }
1278
1279     if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1280       const int num_elem_dst = me_dst->totvert;
1281
1282       data_transfer_layersmapping_generate(NULL,
1283                                            ob_src,
1284                                            ob_dst,
1285                                            me_src,
1286                                            me_dst,
1287                                            ME_VERT,
1288                                            cddata_type,
1289                                            0,
1290                                            0.0f,
1291                                            NULL,
1292                                            num_elem_dst,
1293                                            use_create,
1294                                            use_delete,
1295                                            fromlayers,
1296                                            tolayers,
1297                                            NULL);
1298     }
1299     if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1300       const int num_elem_dst = me_dst->totedge;
1301
1302       data_transfer_layersmapping_generate(NULL,
1303                                            ob_src,
1304                                            ob_dst,
1305                                            me_src,
1306                                            me_dst,
1307                                            ME_EDGE,
1308                                            cddata_type,
1309                                            0,
1310                                            0.0f,
1311                                            NULL,
1312                                            num_elem_dst,
1313                                            use_create,
1314                                            use_delete,
1315                                            fromlayers,
1316                                            tolayers,
1317                                            NULL);
1318     }
1319     if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1320       const int num_elem_dst = me_dst->totloop;
1321
1322       data_transfer_layersmapping_generate(NULL,
1323                                            ob_src,
1324                                            ob_dst,
1325                                            me_src,
1326                                            me_dst,
1327                                            ME_LOOP,
1328                                            cddata_type,
1329                                            0,
1330                                            0.0f,
1331                                            NULL,
1332                                            num_elem_dst,
1333                                            use_create,
1334                                            use_delete,
1335                                            fromlayers,
1336                                            tolayers,
1337                                            NULL);
1338     }
1339     if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1340       const int num_elem_dst = me_dst->totpoly;
1341
1342       data_transfer_layersmapping_generate(NULL,
1343                                            ob_src,
1344                                            ob_dst,
1345                                            me_src,
1346                                            me_dst,
1347                                            ME_POLY,
1348                                            cddata_type,
1349                                            0,
1350                                            0.0f,
1351                                            NULL,
1352                                            num_elem_dst,
1353                                            use_create,
1354                                            use_delete,
1355                                            fromlayers,
1356                                            tolayers,
1357                                            NULL);
1358     }
1359   }
1360 }
1361
1362 bool BKE_object_data_transfer_ex(struct Depsgraph *depsgraph,
1363                                  Scene *scene,
1364                                  Object *ob_src,
1365                                  Object *ob_dst,
1366                                  Mesh *me_dst,
1367                                  const int data_types,
1368                                  bool use_create,
1369                                  const int map_vert_mode,
1370                                  const int map_edge_mode,
1371                                  const int map_loop_mode,
1372                                  const int map_poly_mode,
1373                                  SpaceTransform *space_transform,
1374                                  const bool auto_transform,
1375                                  const float max_distance,
1376                                  const float ray_radius,
1377                                  const float islands_handling_precision,
1378                                  const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1379                                  const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1380                                  const int mix_mode,
1381                                  const float mix_factor,
1382                                  const char *vgroup_name,
1383                                  const bool invert_vgroup,
1384                                  ReportList *reports)
1385 {
1386 #define VDATA 0
1387 #define EDATA 1
1388 #define LDATA 2
1389 #define PDATA 3
1390 #define DATAMAX 4
1391
1392   SpaceTransform auto_space_transform;
1393
1394   Mesh *me_src;
1395   bool dirty_nors_dst =
1396       true; /* Assumed always true if not using an evaluated mesh as destination. */
1397   int i;
1398
1399   MDeformVert *mdef = NULL;
1400   int vg_idx = -1;
1401   float *weights[DATAMAX] = {NULL};
1402
1403   MeshPairRemap geom_map[DATAMAX] = {{0}};
1404   bool geom_map_init[DATAMAX] = {0};
1405   ListBase lay_map = {NULL};
1406   bool changed = false;
1407   bool is_modifier = false;
1408
1409   const bool use_delete = false; /* We never delete data layers from destination here. */
1410
1411   CustomData_MeshMasks me_src_mask = CD_MASK_BAREMESH;
1412
1413   BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1414
1415   if (me_dst) {
1416     dirty_nors_dst = (me_dst->runtime.cd_dirty_vert & CD_NORMAL) != 0;
1417     /* Never create needed custom layers on passed destination mesh
1418      * (assumed to *not* be ob_dst->data, aka modifier case). */
1419     use_create = false;
1420     is_modifier = true;
1421   }
1422   else {
1423     me_dst = ob_dst->data;
1424   }
1425
1426   if (vgroup_name) {
1427     mdef = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
1428     if (mdef) {
1429       vg_idx = defgroup_name_index(ob_dst, vgroup_name);
1430     }
1431   }
1432
1433   /* Get source evaluated mesh.*/
1434   BKE_object_data_transfer_dttypes_to_cdmask(data_types, &me_src_mask);
1435   BKE_mesh_remap_calc_source_cddata_masks_from_map_modes(
1436       map_vert_mode, map_edge_mode, map_loop_mode, map_poly_mode, &me_src_mask);
1437   if (is_modifier) {
1438     me_src = BKE_modifier_get_evaluated_mesh_from_evaluated_object(ob_src, false);
1439
1440     if (me_src == NULL ||
1441         !CustomData_MeshMasks_are_matching(&ob_src->runtime.last_data_mask, &me_src_mask)) {
1442       CLOG_WARN(&LOG, "Data Transfer: source mesh data is not ready - dependency cycle?");
1443       return changed;
1444     }
1445   }
1446   else {
1447     me_src = mesh_get_eval_final(depsgraph, scene, ob_src, &me_src_mask);
1448   }
1449   if (!me_src) {
1450     return changed;
1451   }
1452
1453   if (auto_transform) {
1454     if (space_transform == NULL) {
1455       space_transform = &auto_space_transform;
1456     }
1457
1458     BKE_mesh_remap_find_best_match_from_mesh(
1459         me_dst->mvert, me_dst->totvert, me_src, space_transform);
1460   }
1461
1462   /* Check all possible data types.
1463    * Note item mappings and dest mix weights are cached. */
1464   for (i = 0; i < DT_TYPE_MAX; i++) {
1465     const int dtdata_type = 1 << i;
1466     int cddata_type;
1467     int fromlayers, tolayers, fromto_idx;
1468
1469     if (!(data_types & dtdata_type)) {
1470       continue;
1471     }
1472
1473     data_transfer_dtdata_type_preprocess(me_src, me_dst, dtdata_type, dirty_nors_dst);
1474
1475     cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1476
1477     fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1478     if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1479       fromlayers = fromlayers_select[fromto_idx];
1480       tolayers = tolayers_select[fromto_idx];
1481     }
1482     else {
1483       fromlayers = tolayers = 0;
1484     }
1485
1486     if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1487       MVert *verts_dst = me_dst->mvert;
1488       const int num_verts_dst = me_dst->totvert;
1489
1490       if (!geom_map_init[VDATA]) {
1491         const int num_verts_src = me_src->totvert;
1492
1493         if ((map_vert_mode == MREMAP_MODE_TOPOLOGY) && (num_verts_dst != num_verts_src)) {
1494           BKE_report(reports,
1495                      RPT_ERROR,
1496                      "Source and destination meshes do not have the same amount of vertices, "
1497                      "'Topology' mapping cannot be used in this case");
1498           continue;
1499         }
1500         if ((map_vert_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1501           BKE_report(reports,
1502                      RPT_ERROR,
1503                      "Source mesh doesn't have any edges, "
1504                      "None of the 'Edge' mappings can be used in this case");
1505           continue;
1506         }
1507         if ((map_vert_mode & MREMAP_USE_POLY) && (me_src->totpoly == 0)) {
1508           BKE_report(reports,
1509                      RPT_ERROR,
1510                      "Source mesh doesn't have any faces, "
1511                      "None of the 'Face' mappings can be used in this case");
1512           continue;
1513         }
1514         if (ELEM(0, num_verts_dst, num_verts_src)) {
1515           BKE_report(reports,
1516                      RPT_ERROR,
1517                      "Source or destination meshes do not have any vertices, cannot transfer "
1518                      "vertex data");
1519           continue;
1520         }
1521
1522         BKE_mesh_remap_calc_verts_from_mesh(map_vert_mode,
1523                                             space_transform,
1524                                             max_distance,
1525                                             ray_radius,
1526                                             verts_dst,
1527                                             num_verts_dst,
1528                                             dirty_nors_dst,
1529                                             me_src,
1530                                             &geom_map[VDATA]);
1531         geom_map_init[VDATA] = true;
1532       }
1533
1534       if (mdef && vg_idx != -1 && !weights[VDATA]) {
1535         weights[VDATA] = MEM_mallocN(sizeof(*(weights[VDATA])) * (size_t)num_verts_dst, __func__);
1536         BKE_defvert_extract_vgroup_to_vertweights(
1537             mdef, vg_idx, num_verts_dst, weights[VDATA], invert_vgroup);
1538       }
1539
1540       if (data_transfer_layersmapping_generate(&lay_map,
1541                                                ob_src,
1542                                                ob_dst,
1543                                                me_src,
1544                                                me_dst,
1545                                                ME_VERT,
1546                                                cddata_type,
1547                                                mix_mode,
1548                                                mix_factor,
1549                                                weights[VDATA],
1550                                                num_verts_dst,
1551                                                use_create,
1552                                                use_delete,
1553                                                fromlayers,
1554                                                tolayers,
1555                                                space_transform)) {
1556         CustomDataTransferLayerMap *lay_mapit;
1557
1558         changed = (lay_map.first != NULL);
1559
1560         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1561           CustomData_data_transfer(&geom_map[VDATA], lay_mapit);
1562         }
1563
1564         BLI_freelistN(&lay_map);
1565       }
1566     }
1567     if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1568       MVert *verts_dst = me_dst->mvert;
1569       const int num_verts_dst = me_dst->totvert;
1570       MEdge *edges_dst = me_dst->medge;
1571       const int num_edges_dst = me_dst->totedge;
1572
1573       if (!geom_map_init[EDATA]) {
1574         const int num_edges_src = me_src->totedge;
1575
1576         if ((map_edge_mode == MREMAP_MODE_TOPOLOGY) && (num_edges_dst != num_edges_src)) {
1577           BKE_report(reports,
1578                      RPT_ERROR,
1579                      "Source and destination meshes do not have the same amount of edges, "
1580                      "'Topology' mapping cannot be used in this case");
1581           continue;
1582         }
1583         if ((map_edge_mode & MREMAP_USE_POLY) && (me_src->totpoly == 0)) {
1584           BKE_report(reports,
1585                      RPT_ERROR,
1586                      "Source mesh doesn't have any faces, "
1587                      "None of the 'Face' mappings can be used in this case");
1588           continue;
1589         }
1590         if (ELEM(0, num_edges_dst, num_edges_src)) {
1591           BKE_report(
1592               reports,
1593               RPT_ERROR,
1594               "Source or destination meshes do not have any edges, cannot transfer edge data");
1595           continue;
1596         }
1597
1598         BKE_mesh_remap_calc_edges_from_mesh(map_edge_mode,
1599                                             space_transform,
1600                                             max_distance,
1601                                             ray_radius,
1602                                             verts_dst,
1603                                             num_verts_dst,
1604                                             edges_dst,
1605                                             num_edges_dst,
1606                                             dirty_nors_dst,
1607                                             me_src,
1608                                             &geom_map[EDATA]);
1609         geom_map_init[EDATA] = true;
1610       }
1611
1612       if (mdef && vg_idx != -1 && !weights[EDATA]) {
1613         weights[EDATA] = MEM_mallocN(sizeof(*weights[EDATA]) * (size_t)num_edges_dst, __func__);
1614         BKE_defvert_extract_vgroup_to_edgeweights(
1615             mdef, vg_idx, num_verts_dst, edges_dst, num_edges_dst, weights[EDATA], invert_vgroup);
1616       }
1617
1618       if (data_transfer_layersmapping_generate(&lay_map,
1619                                                ob_src,
1620                                                ob_dst,
1621                                                me_src,
1622                                                me_dst,
1623                                                ME_EDGE,
1624                                                cddata_type,
1625                                                mix_mode,
1626                                                mix_factor,
1627                                                weights[EDATA],
1628                                                num_edges_dst,
1629                                                use_create,
1630                                                use_delete,
1631                                                fromlayers,
1632                                                tolayers,
1633                                                space_transform)) {
1634         CustomDataTransferLayerMap *lay_mapit;
1635
1636         changed = (lay_map.first != NULL);
1637
1638         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1639           CustomData_data_transfer(&geom_map[EDATA], lay_mapit);
1640         }
1641
1642         BLI_freelistN(&lay_map);
1643       }
1644     }
1645     if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1646       MVert *verts_dst = me_dst->mvert;
1647       const int num_verts_dst = me_dst->totvert;
1648       MEdge *edges_dst = me_dst->medge;
1649       const int num_edges_dst = me_dst->totedge;
1650       MPoly *polys_dst = me_dst->mpoly;
1651       const int num_polys_dst = me_dst->totpoly;
1652       MLoop *loops_dst = me_dst->mloop;
1653       const int num_loops_dst = me_dst->totloop;
1654       CustomData *pdata_dst = &me_dst->pdata;
1655       CustomData *ldata_dst = &me_dst->ldata;
1656
1657       MeshRemapIslandsCalc island_callback = data_transfer_get_loop_islands_generator(cddata_type);
1658
1659       if (!geom_map_init[LDATA]) {
1660         const int num_loops_src = me_src->totloop;
1661
1662         if ((map_loop_mode == MREMAP_MODE_TOPOLOGY) && (num_loops_dst != num_loops_src)) {
1663           BKE_report(reports,
1664                      RPT_ERROR,
1665                      "Source and destination meshes do not have the same amount of face corners, "
1666                      "'Topology' mapping cannot be used in this case");
1667           continue;
1668         }
1669         if ((map_loop_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1670           BKE_report(reports,
1671                      RPT_ERROR,
1672                      "Source mesh doesn't have any edges, "
1673                      "None of the 'Edge' mappings can be used in this case");
1674           continue;
1675         }
1676         if (ELEM(0, num_loops_dst, num_loops_src)) {
1677           BKE_report(
1678               reports,
1679               RPT_ERROR,
1680               "Source or destination meshes do not have any faces, cannot transfer corner data");
1681           continue;
1682         }
1683
1684         BKE_mesh_remap_calc_loops_from_mesh(map_loop_mode,
1685                                             space_transform,
1686                                             max_distance,
1687                                             ray_radius,
1688                                             verts_dst,
1689                                             num_verts_dst,
1690                                             edges_dst,
1691                                             num_edges_dst,
1692                                             loops_dst,
1693                                             num_loops_dst,
1694                                             polys_dst,
1695                                             num_polys_dst,
1696                                             ldata_dst,
1697                                             pdata_dst,
1698                                             (me_dst->flag & ME_AUTOSMOOTH) != 0,
1699                                             me_dst->smoothresh,
1700                                             dirty_nors_dst,
1701                                             me_src,
1702                                             island_callback,
1703                                             islands_handling_precision,
1704                                             &geom_map[LDATA]);
1705         geom_map_init[LDATA] = true;
1706       }
1707
1708       if (mdef && vg_idx != -1 && !weights[LDATA]) {
1709         weights[LDATA] = MEM_mallocN(sizeof(*weights[LDATA]) * (size_t)num_loops_dst, __func__);
1710         BKE_defvert_extract_vgroup_to_loopweights(
1711             mdef, vg_idx, num_verts_dst, loops_dst, num_loops_dst, weights[LDATA], invert_vgroup);
1712       }
1713
1714       if (data_transfer_layersmapping_generate(&lay_map,
1715                                                ob_src,
1716                                                ob_dst,
1717                                                me_src,
1718                                                me_dst,
1719                                                ME_LOOP,
1720                                                cddata_type,
1721                                                mix_mode,
1722                                                mix_factor,
1723                                                weights[LDATA],
1724                                                num_loops_dst,
1725                                                use_create,
1726                                                use_delete,
1727                                                fromlayers,
1728                                                tolayers,
1729                                                space_transform)) {
1730         CustomDataTransferLayerMap *lay_mapit;
1731
1732         changed = (lay_map.first != NULL);
1733
1734         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1735           CustomData_data_transfer(&geom_map[LDATA], lay_mapit);
1736         }
1737
1738         BLI_freelistN(&lay_map);
1739       }
1740     }
1741     if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1742       MVert *verts_dst = me_dst->mvert;
1743       const int num_verts_dst = me_dst->totvert;
1744       MPoly *polys_dst = me_dst->mpoly;
1745       const int num_polys_dst = me_dst->totpoly;
1746       MLoop *loops_dst = me_dst->mloop;
1747       const int num_loops_dst = me_dst->totloop;
1748       CustomData *pdata_dst = &me_dst->pdata;
1749
1750       if (!geom_map_init[PDATA]) {
1751         const int num_polys_src = me_src->totpoly;
1752
1753         if ((map_poly_mode == MREMAP_MODE_TOPOLOGY) && (num_polys_dst != num_polys_src)) {
1754           BKE_report(reports,
1755                      RPT_ERROR,
1756                      "Source and destination meshes do not have the same amount of faces, "
1757                      "'Topology' mapping cannot be used in this case");
1758           continue;
1759         }
1760         if ((map_poly_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1761           BKE_report(reports,
1762                      RPT_ERROR,
1763                      "Source mesh doesn't have any edges, "
1764                      "None of the 'Edge' mappings can be used in this case");
1765           continue;
1766         }
1767         if (ELEM(0, num_polys_dst, num_polys_src)) {
1768           BKE_report(
1769               reports,
1770               RPT_ERROR,
1771               "Source or destination meshes do not have any faces, cannot transfer face data");
1772           continue;
1773         }
1774
1775         BKE_mesh_remap_calc_polys_from_mesh(map_poly_mode,
1776                                             space_transform,
1777                                             max_distance,
1778                                             ray_radius,
1779                                             verts_dst,
1780                                             num_verts_dst,
1781                                             loops_dst,
1782                                             num_loops_dst,
1783                                             polys_dst,
1784                                             num_polys_dst,
1785                                             pdata_dst,
1786                                             dirty_nors_dst,
1787                                             me_src,
1788                                             &geom_map[PDATA]);
1789         geom_map_init[PDATA] = true;
1790       }
1791
1792       if (mdef && vg_idx != -1 && !weights[PDATA]) {
1793         weights[PDATA] = MEM_mallocN(sizeof(*weights[PDATA]) * (size_t)num_polys_dst, __func__);
1794         BKE_defvert_extract_vgroup_to_polyweights(mdef,
1795                                                   vg_idx,
1796                                                   num_verts_dst,
1797                                                   loops_dst,
1798                                                   num_loops_dst,
1799                                                   polys_dst,
1800                                                   num_polys_dst,
1801                                                   weights[PDATA],
1802                                                   invert_vgroup);
1803       }
1804
1805       if (data_transfer_layersmapping_generate(&lay_map,
1806                                                ob_src,
1807                                                ob_dst,
1808                                                me_src,
1809                                                me_dst,
1810                                                ME_POLY,
1811                                                cddata_type,
1812                                                mix_mode,
1813                                                mix_factor,
1814                                                weights[PDATA],
1815                                                num_polys_dst,
1816                                                use_create,
1817                                                use_delete,
1818                                                fromlayers,
1819                                                tolayers,
1820                                                space_transform)) {
1821         CustomDataTransferLayerMap *lay_mapit;
1822
1823         changed = (lay_map.first != NULL);
1824
1825         for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1826           CustomData_data_transfer(&geom_map[PDATA], lay_mapit);
1827         }
1828
1829         BLI_freelistN(&lay_map);
1830       }
1831     }
1832
1833     data_transfer_dtdata_type_postprocess(ob_src, ob_dst, me_src, me_dst, dtdata_type, changed);
1834   }
1835
1836   for (i = 0; i < DATAMAX; i++) {
1837     BKE_mesh_remap_free(&geom_map[i]);
1838     MEM_SAFE_FREE(weights[i]);
1839   }
1840
1841   return changed;
1842
1843 #undef VDATA
1844 #undef EDATA
1845 #undef LDATA
1846 #undef PDATA
1847 #undef DATAMAX
1848 }
1849
1850 bool BKE_object_data_transfer_mesh(struct Depsgraph *depsgraph,
1851                                    Scene *scene,
1852                                    Object *ob_src,
1853                                    Object *ob_dst,
1854                                    const int data_types,
1855                                    const bool use_create,
1856                                    const int map_vert_mode,
1857                                    const int map_edge_mode,
1858                                    const int map_loop_mode,
1859                                    const int map_poly_mode,
1860                                    SpaceTransform *space_transform,
1861                                    const bool auto_transform,
1862                                    const float max_distance,
1863                                    const float ray_radius,
1864                                    const float islands_handling_precision,
1865                                    const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1866                                    const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1867                                    const int mix_mode,
1868                                    const float mix_factor,
1869                                    const char *vgroup_name,
1870                                    const bool invert_vgroup,
1871                                    ReportList *reports)
1872 {
1873   return BKE_object_data_transfer_ex(depsgraph,
1874                                      scene,
1875                                      ob_src,
1876                                      ob_dst,
1877                                      NULL,
1878                                      data_types,
1879                                      use_create,
1880                                      map_vert_mode,
1881                                      map_edge_mode,
1882                                      map_loop_mode,
1883                                      map_poly_mode,
1884                                      space_transform,
1885                                      auto_transform,
1886                                      max_distance,
1887                                      ray_radius,
1888                                      islands_handling_precision,
1889                                      fromlayers_select,
1890                                      tolayers_select,
1891                                      mix_mode,
1892                                      mix_factor,
1893                                      vgroup_name,
1894                                      invert_vgroup,
1895                                      reports);
1896 }