Merge branch 'blender2.7'
[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         bool is_modifier = false;
1091
1092         const bool use_delete = false;  /* We never delete data layers from destination here. */
1093
1094         CustomDataMask me_src_mask = CD_MASK_BAREMESH;
1095
1096         BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1097
1098         if (me_dst) {
1099                 dirty_nors_dst = (me_dst->runtime.cd_dirty_vert & CD_NORMAL) != 0;
1100                 /* Never create needed custom layers on passed destination mesh
1101                  * (assumed to *not* be ob_dst->data, aka modifier case). */
1102                 use_create = false;
1103                 is_modifier = true;
1104         }
1105         else {
1106                 me_dst = ob_dst->data;
1107         }
1108
1109         if (vgroup_name) {
1110                 mdef = CustomData_get_layer(&me_dst->vdata, CD_MDEFORMVERT);
1111                 if (mdef) {
1112                         vg_idx = defgroup_name_index(ob_dst, vgroup_name);
1113                 }
1114         }
1115
1116         /* Get source evaluated mesh.*/
1117         me_src_mask |= BKE_object_data_transfer_dttypes_to_cdmask(data_types);
1118         if (is_modifier) {
1119                 me_src = ob_src->runtime.mesh_eval;
1120
1121                 if (me_src == NULL || (me_src_mask & ~ob_src->runtime.last_data_mask) != 0) {
1122                         printf("Data Transfer: source mesh data is not ready - dependency cycle?\n");
1123                         return changed;
1124                 }
1125         }
1126         else {
1127                 me_src = mesh_get_eval_final(depsgraph, scene, ob_src, me_src_mask);
1128         }
1129         if (!me_src) {
1130                 return changed;
1131         }
1132
1133         if (auto_transform) {
1134                 if (space_transform == NULL) {
1135                         space_transform = &auto_space_transform;
1136                 }
1137
1138                 BKE_mesh_remap_find_best_match_from_mesh(me_dst->mvert, me_dst->totvert, me_src, space_transform);
1139         }
1140
1141         /* Check all possible data types.
1142          * Note item mappings and dest mix weights are cached. */
1143         for (i = 0; i < DT_TYPE_MAX; i++) {
1144                 const int dtdata_type = 1 << i;
1145                 int cddata_type;
1146                 int fromlayers, tolayers, fromto_idx;
1147
1148                 if (!(data_types & dtdata_type)) {
1149                         continue;
1150                 }
1151
1152                 data_transfer_dtdata_type_preprocess(me_src, me_dst, dtdata_type, dirty_nors_dst);
1153
1154                 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1155
1156                 fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1157                 if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1158                         fromlayers = fromlayers_select[fromto_idx];
1159                         tolayers = tolayers_select[fromto_idx];
1160                 }
1161                 else {
1162                         fromlayers = tolayers = 0;
1163                 }
1164
1165                 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1166                         MVert *verts_dst = me_dst->mvert;
1167                         const int num_verts_dst = me_dst->totvert;
1168
1169                         if (!geom_map_init[VDATA]) {
1170                                 const int num_verts_src = me_src->totvert;
1171
1172                                 if ((map_vert_mode == MREMAP_MODE_TOPOLOGY) && (num_verts_dst != num_verts_src)) {
1173                                         BKE_report(reports, RPT_ERROR,
1174                                                    "Source and destination meshes do not have the same amount of vertices, "
1175                                                    "'Topology' mapping cannot be used in this case");
1176                                         continue;
1177                                 }
1178                                 if ((map_vert_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1179                                         BKE_report(reports, RPT_ERROR,
1180                                                    "Source mesh doesn't have any edges, "
1181                                                    "None of the 'Edge' mappings can be used in this case");
1182                                         continue;
1183                                 }
1184                                 if ((map_vert_mode & MREMAP_USE_POLY) && (me_src->totpoly == 0)) {
1185                                         BKE_report(reports, RPT_ERROR,
1186                                                    "Source mesh doesn't have any faces, "
1187                                                    "None of the 'Face' mappings can be used in this case");
1188                                         continue;
1189                                 }
1190                                 if (ELEM(0, num_verts_dst, num_verts_src)) {
1191                                         BKE_report(reports, RPT_ERROR,
1192                                                    "Source or destination meshes do not have any vertices, cannot transfer vertex data");
1193                                         continue;
1194                                 }
1195
1196                                 BKE_mesh_remap_calc_verts_from_mesh(
1197                                         map_vert_mode, space_transform, max_distance, ray_radius,
1198                                         verts_dst, num_verts_dst, dirty_nors_dst, me_src, &geom_map[VDATA]);
1199                                 geom_map_init[VDATA] = true;
1200                         }
1201
1202                         if (mdef && vg_idx != -1 && !weights[VDATA]) {
1203                                 weights[VDATA] = MEM_mallocN(sizeof(*(weights[VDATA])) * (size_t)num_verts_dst, __func__);
1204                                 BKE_defvert_extract_vgroup_to_vertweights(mdef, vg_idx, num_verts_dst, weights[VDATA], invert_vgroup);
1205                         }
1206
1207                         if (data_transfer_layersmapping_generate(
1208                                 &lay_map, ob_src, ob_dst, me_src, me_dst, ME_VERT,
1209                                 cddata_type, mix_mode, mix_factor, weights[VDATA],
1210                                 num_verts_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1211                         {
1212                                 CustomDataTransferLayerMap *lay_mapit;
1213
1214                                 changed = (lay_map.first != NULL);
1215
1216                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1217                                         CustomData_data_transfer(&geom_map[VDATA], lay_mapit);
1218                                 }
1219
1220                                 BLI_freelistN(&lay_map);
1221                         }
1222                 }
1223                 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1224                         MVert *verts_dst = me_dst->mvert;
1225                         const int num_verts_dst = me_dst->totvert;
1226                         MEdge *edges_dst = me_dst->medge;
1227                         const int num_edges_dst = me_dst->totedge;
1228
1229                         if (!geom_map_init[EDATA]) {
1230                                 const int num_edges_src = me_src->totedge;
1231
1232                                 if ((map_edge_mode == MREMAP_MODE_TOPOLOGY) && (num_edges_dst != num_edges_src)) {
1233                                         BKE_report(reports, RPT_ERROR,
1234                                                    "Source and destination meshes do not have the same amount of edges, "
1235                                                    "'Topology' mapping cannot be used in this case");
1236                                         continue;
1237                                 }
1238                                 if ((map_edge_mode & MREMAP_USE_POLY) && (me_src->totpoly == 0)) {
1239                                         BKE_report(reports, RPT_ERROR,
1240                                                    "Source mesh doesn't have any faces, "
1241                                                    "None of the 'Face' mappings can be used in this case");
1242                                         continue;
1243                                 }
1244                                 if (ELEM(0, num_edges_dst, num_edges_src)) {
1245                                         BKE_report(reports, RPT_ERROR,
1246                                                    "Source or destination meshes do not have any edges, cannot transfer edge data");
1247                                         continue;
1248                                 }
1249
1250                                 BKE_mesh_remap_calc_edges_from_mesh(
1251                                         map_edge_mode, space_transform, max_distance, ray_radius,
1252                                         verts_dst, num_verts_dst, edges_dst, num_edges_dst, dirty_nors_dst,
1253                                         me_src, &geom_map[EDATA]);
1254                                 geom_map_init[EDATA] = true;
1255                         }
1256
1257                         if (mdef && vg_idx != -1 && !weights[EDATA]) {
1258                                 weights[EDATA] = MEM_mallocN(sizeof(*weights[EDATA]) * (size_t)num_edges_dst, __func__);
1259                                 BKE_defvert_extract_vgroup_to_edgeweights(
1260                                         mdef, vg_idx, num_verts_dst, edges_dst, num_edges_dst,
1261                                         weights[EDATA], invert_vgroup);
1262                         }
1263
1264                         if (data_transfer_layersmapping_generate(
1265                                 &lay_map, ob_src, ob_dst, me_src, me_dst, ME_EDGE,
1266                                 cddata_type, mix_mode, mix_factor, weights[EDATA],
1267                                 num_edges_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1268                         {
1269                                 CustomDataTransferLayerMap *lay_mapit;
1270
1271                                 changed = (lay_map.first != NULL);
1272
1273                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1274                                         CustomData_data_transfer(&geom_map[EDATA], lay_mapit);
1275                                 }
1276
1277                                 BLI_freelistN(&lay_map);
1278                         }
1279                 }
1280                 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1281                         MVert *verts_dst = me_dst->mvert;
1282                         const int num_verts_dst = me_dst->totvert;
1283                         MEdge *edges_dst = me_dst->medge;
1284                         const int num_edges_dst = me_dst->totedge;
1285                         MPoly *polys_dst = me_dst->mpoly;
1286                         const int num_polys_dst = me_dst->totpoly;
1287                         MLoop *loops_dst = me_dst->mloop;
1288                         const int num_loops_dst = me_dst->totloop;
1289                         CustomData *pdata_dst = &me_dst->pdata;
1290                         CustomData *ldata_dst = &me_dst->ldata;
1291
1292                         MeshRemapIslandsCalc island_callback = data_transfer_get_loop_islands_generator(cddata_type);
1293
1294                         if (!geom_map_init[LDATA]) {
1295                                 const int num_loops_src = me_src->totloop;
1296
1297                                 if ((map_loop_mode == MREMAP_MODE_TOPOLOGY) && (num_loops_dst != num_loops_src)) {
1298                                         BKE_report(reports, RPT_ERROR,
1299                                                    "Source and destination meshes do not have the same amount of face corners, "
1300                                                    "'Topology' mapping cannot be used in this case");
1301                                         continue;
1302                                 }
1303                                 if ((map_loop_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1304                                         BKE_report(reports, RPT_ERROR,
1305                                                    "Source mesh doesn't have any edges, "
1306                                                    "None of the 'Edge' mappings can be used in this case");
1307                                         continue;
1308                                 }
1309                                 if (ELEM(0, num_loops_dst, num_loops_src)) {
1310                                         BKE_report(reports, RPT_ERROR,
1311                                                    "Source or destination meshes do not have any faces, cannot transfer corner data");
1312                                         continue;
1313                                 }
1314
1315                                 BKE_mesh_remap_calc_loops_from_mesh(
1316                                         map_loop_mode, space_transform, max_distance, ray_radius,
1317                                         verts_dst, num_verts_dst, edges_dst, num_edges_dst,
1318                                         loops_dst, num_loops_dst, polys_dst, num_polys_dst,
1319                                         ldata_dst, pdata_dst,
1320                                         (me_dst->flag & ME_AUTOSMOOTH) != 0, me_dst->smoothresh, dirty_nors_dst,
1321                                         me_src,
1322                                         island_callback, islands_handling_precision, &geom_map[LDATA]);
1323                                 geom_map_init[LDATA] = true;
1324                         }
1325
1326                         if (mdef && vg_idx != -1 && !weights[LDATA]) {
1327                                 weights[LDATA] = MEM_mallocN(sizeof(*weights[LDATA]) * (size_t)num_loops_dst, __func__);
1328                                 BKE_defvert_extract_vgroup_to_loopweights(
1329                                         mdef, vg_idx, num_verts_dst, loops_dst, num_loops_dst,
1330                                         weights[LDATA], invert_vgroup);
1331                         }
1332
1333                         if (data_transfer_layersmapping_generate(
1334                                 &lay_map, ob_src, ob_dst, me_src, me_dst, ME_LOOP,
1335                                 cddata_type, mix_mode, mix_factor, weights[LDATA],
1336                                 num_loops_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1337                         {
1338                                 CustomDataTransferLayerMap *lay_mapit;
1339
1340                                 changed = (lay_map.first != NULL);
1341
1342                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1343                                         CustomData_data_transfer(&geom_map[LDATA], lay_mapit);
1344                                 }
1345
1346                                 BLI_freelistN(&lay_map);
1347                         }
1348                 }
1349                 if (DT_DATATYPE_IS_POLY(dtdata_type)) {
1350                         MVert *verts_dst = me_dst->mvert;
1351                         const int num_verts_dst = me_dst->totvert;
1352                         MPoly *polys_dst = me_dst->mpoly;
1353                         const int num_polys_dst = me_dst->totpoly;
1354                         MLoop *loops_dst = me_dst->mloop;
1355                         const int num_loops_dst = me_dst->totloop;
1356                         CustomData *pdata_dst = &me_dst->pdata;
1357
1358                         if (!geom_map_init[PDATA]) {
1359                                 const int num_polys_src = me_src->totpoly;
1360
1361                                 if ((map_poly_mode == MREMAP_MODE_TOPOLOGY) && (num_polys_dst != num_polys_src)) {
1362                                         BKE_report(reports, RPT_ERROR,
1363                                                    "Source and destination meshes do not have the same amount of faces, "
1364                                                    "'Topology' mapping cannot be used in this case");
1365                                         continue;
1366                                 }
1367                                 if ((map_poly_mode & MREMAP_USE_EDGE) && (me_src->totedge == 0)) {
1368                                         BKE_report(reports, RPT_ERROR,
1369                                                    "Source mesh doesn't have any edges, "
1370                                                    "None of the 'Edge' mappings can be used in this case");
1371                                         continue;
1372                                 }
1373                                 if (ELEM(0, num_polys_dst, num_polys_src)) {
1374                                         BKE_report(reports, RPT_ERROR,
1375                                                    "Source or destination meshes do not have any faces, cannot transfer face data");
1376                                         continue;
1377                                 }
1378
1379                                 BKE_mesh_remap_calc_polys_from_mesh(
1380                                         map_poly_mode, space_transform, max_distance, ray_radius,
1381                                         verts_dst, num_verts_dst, loops_dst, num_loops_dst,
1382                                         polys_dst, num_polys_dst, pdata_dst, dirty_nors_dst,
1383                                         me_src, &geom_map[PDATA]);
1384                                 geom_map_init[PDATA] = true;
1385                         }
1386
1387                         if (mdef && vg_idx != -1 && !weights[PDATA]) {
1388                                 weights[PDATA] = MEM_mallocN(sizeof(*weights[PDATA]) * (size_t)num_polys_dst, __func__);
1389                                 BKE_defvert_extract_vgroup_to_polyweights(
1390                                         mdef, vg_idx, num_verts_dst, loops_dst, num_loops_dst,
1391                                         polys_dst, num_polys_dst, weights[PDATA], invert_vgroup);
1392                         }
1393
1394                         if (data_transfer_layersmapping_generate(
1395                                 &lay_map, ob_src, ob_dst, me_src, me_dst, ME_POLY,
1396                                 cddata_type, mix_mode, mix_factor, weights[PDATA],
1397                                 num_polys_dst, use_create, use_delete, fromlayers, tolayers, space_transform))
1398                         {
1399                                 CustomDataTransferLayerMap *lay_mapit;
1400
1401                                 changed = (lay_map.first != NULL);
1402
1403                                 for (lay_mapit = lay_map.first; lay_mapit; lay_mapit = lay_mapit->next) {
1404                                         CustomData_data_transfer(&geom_map[PDATA], lay_mapit);
1405                                 }
1406
1407                                 BLI_freelistN(&lay_map);
1408                         }
1409                 }
1410
1411                 data_transfer_dtdata_type_postprocess(ob_src, ob_dst, me_src, me_dst, dtdata_type, changed);
1412         }
1413
1414         for (i = 0; i < DATAMAX; i++) {
1415                 BKE_mesh_remap_free(&geom_map[i]);
1416                 MEM_SAFE_FREE(weights[i]);
1417         }
1418
1419         return changed;
1420
1421 #undef VDATA
1422 #undef EDATA
1423 #undef LDATA
1424 #undef PDATA
1425 #undef DATAMAX
1426 }
1427
1428 bool BKE_object_data_transfer_mesh(
1429         struct Depsgraph *depsgraph, Scene *scene, Object *ob_src, Object *ob_dst, const int data_types,
1430         const bool use_create, const int map_vert_mode, const int map_edge_mode, const int map_loop_mode,
1431         const int map_poly_mode, SpaceTransform *space_transform, const bool auto_transform,
1432         const float max_distance, const float ray_radius, const float islands_handling_precision,
1433         const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1434         const int mix_mode, const float mix_factor, const char *vgroup_name, const bool invert_vgroup,
1435         ReportList *reports)
1436 {
1437         return BKE_object_data_transfer_ex(
1438                 depsgraph, scene, ob_src, ob_dst, NULL, data_types, use_create,
1439                 map_vert_mode, map_edge_mode, map_loop_mode, map_poly_mode,
1440                 space_transform, auto_transform,
1441                 max_distance, ray_radius, islands_handling_precision,
1442                 fromlayers_select, tolayers_select,
1443                 mix_mode, mix_factor, vgroup_name, invert_vgroup, reports);
1444 }