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