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