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