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