Blender V5.3
data_transfer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "MEM_guardedalloc.h"
11
13#include "DNA_mesh_types.h"
14#include "DNA_meshdata_types.h"
15#include "DNA_object_types.h"
16
17#include "BLI_math_base_c.hh"
18#include "BLI_math_matrix_c.hh"
19#include "BLI_string.hh"
20#include "BLI_utildefines.hh"
21
22#include "BKE_attribute.h"
23#include "BKE_attribute.hh"
24#include "BKE_customdata.hh"
25#include "BKE_data_transfer.h"
26#include "BKE_deform.hh"
27#include "BKE_mesh.hh"
28#include "BKE_mesh_mapping.hh"
29#include "BKE_mesh_remap.hh"
30#include "BKE_mesh_runtime.hh"
31#include "BKE_mesh_wrapper.hh"
32#include "BKE_modifier.hh"
33#include "BKE_object.hh"
34#include "BKE_report.hh"
35
37
39
40namespace blender {
41
43 CustomData_MeshMasks *r_data_masks)
44{
45 for (int i = 0; i < DT_TYPE_MAX; i++) {
46 const int dtdata_type = 1 << i;
47 int cddata_type;
48
49 if (!(dtdata_types & dtdata_type)) {
50 continue;
51 }
52
53 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
54 if (!(cddata_type & CD_FAKE)) {
55 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
56 r_data_masks->vmask |= 1LL << cddata_type;
57 }
58 else if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
59 r_data_masks->emask |= 1LL << cddata_type;
60 }
61 else if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
62 r_data_masks->lmask |= 1LL << cddata_type;
63 }
64 else if (DT_DATATYPE_IS_FACE(dtdata_type)) {
65 r_data_masks->pmask |= 1LL << cddata_type;
66 }
67 }
68 else if (cddata_type == CD_FAKE_MDEFORMVERT) {
69 r_data_masks->vmask |= CD_MASK_MDEFORMVERT; /* Exception for vgroups :/ */
70 }
71 else if (cddata_type == CD_FAKE_UV) {
72 r_data_masks->lmask |= CD_MASK_PROP_FLOAT2;
73 }
74 }
75}
76
78 bool *r_advanced_mixing,
79 bool *r_threshold)
80{
81 bool ret = false;
82
83 *r_advanced_mixing = false;
84 *r_threshold = false;
85
86 for (int i = 0; (i < DT_TYPE_MAX) && !(ret && *r_advanced_mixing && *r_threshold); i++) {
87 const int dtdata_type = 1 << i;
88
89 if (!(dtdata_types & dtdata_type)) {
90 continue;
91 }
92
93 switch (dtdata_type) {
94 /* Vertex data */
96 *r_advanced_mixing = true;
97 *r_threshold = true;
98 ret = true;
99 break;
100 case DT_TYPE_SKIN:
101 *r_threshold = true;
102 ret = true;
103 break;
105 ret = true;
106 break;
107 /* Edge data */
109 *r_threshold = true;
110 ret = true;
111 break;
112 case DT_TYPE_SEAM:
113 *r_threshold = true;
114 ret = true;
115 break;
116 case DT_TYPE_CREASE:
117 ret = true;
118 break;
120 ret = true;
121 break;
123 *r_threshold = true;
124 ret = true;
125 break;
126 /* Loop/Poly data */
127 case DT_TYPE_UV:
128 ret = true;
129 break;
134 *r_advanced_mixing = true;
135 *r_threshold = true;
136 ret = true;
137 break;
138 case DT_TYPE_LNOR:
139 *r_advanced_mixing = true;
140 ret = true;
141 break;
143 *r_threshold = true;
144 ret = true;
145 break;
147 *r_threshold = true;
148 ret = true;
149 break;
150 }
151 }
152
153 return ret;
154}
155
157{
158 int i, ret = 0;
159
160 for (i = 0; (i < DT_TYPE_MAX) && (ret ^ (ME_VERT | ME_EDGE | ME_LOOP | ME_POLY)); i++) {
161 const int dtdata_type = 1 << i;
162
163 if (!(dtdata_types & dtdata_type)) {
164 continue;
165 }
166
167 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
168 ret |= ME_VERT;
169 }
170 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
171 ret |= ME_EDGE;
172 }
173 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
174 ret |= ME_LOOP;
175 }
176 if (DT_DATATYPE_IS_FACE(dtdata_type)) {
177 ret |= ME_POLY;
178 }
179 }
180
181 return ret;
182}
183
185{
186 switch (dtdata_type) {
188 return CD_FAKE_MDEFORMVERT;
189 case DT_TYPE_SKIN:
190 return CD_FAKE_SKIN_RADIUS;
192 return CD_FAKE_BWEIGHT;
193
195 return CD_FAKE_SHARP;
196 case DT_TYPE_SEAM:
197 return CD_FAKE_SEAM;
198 case DT_TYPE_CREASE:
199 return CD_FAKE_CREASE;
201 return CD_FAKE_BWEIGHT;
204
205 case DT_TYPE_UV:
206 return CD_FAKE_UV;
208 return CD_FAKE_SHARP;
211 case DT_TYPE_LNOR:
212 return CD_FAKE_LNOR;
215 return CD_PROP_BYTE_COLOR;
218 return CD_PROP_COLOR;
219 default:
221 }
222 return 0; /* Should never be reached! */
223}
224
244
245/* ********** */
246
252static void transfer_active_color_string(Mesh *mesh_dst, const Mesh *mesh_src)
253{
254 if (mesh_dst->active_color_attribute) {
255 return;
256 }
257
258 const StringRef name = mesh_src->active_color_attribute;
259 const bke::AttributeAccessor attributes_src = mesh_src->attributes();
260 const bke::AttributeAccessor attributes_dst = mesh_dst->attributes();
261
262 if (!bke::mesh::is_color_attribute(attributes_src.lookup_meta_data(name))) {
263 return;
264 }
265
266 if (bke::mesh::is_color_attribute(attributes_dst.lookup_meta_data(name))) {
267 mesh_dst->active_color_attribute = BLI_strdupn(name.data(), name.size());
268 }
269 else {
270 mesh_dst->attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
271 if (mesh_dst->active_color_attribute) {
272 return;
273 }
275 return;
276 }
277 mesh_dst->active_color_attribute = BLI_strdupn(iter.name.data(), iter.name.size());
278 });
279 }
280}
281
287static void transfer_default_color_string(Mesh *mesh_dst, const Mesh *mesh_src)
288{
289 if (mesh_dst->default_color_attribute) {
290 return;
291 }
292
293 const StringRef name = mesh_src->default_color_attribute;
294 const bke::AttributeAccessor attributes_src = mesh_src->attributes();
295 const bke::AttributeAccessor attributes_dst = mesh_dst->attributes();
296
297 if (!bke::mesh::is_color_attribute(attributes_src.lookup_meta_data(name))) {
298 return;
299 }
300
301 if (bke::mesh::is_color_attribute(attributes_dst.lookup_meta_data(name))) {
302 mesh_dst->default_color_attribute = BLI_strdupn(name.data(), name.size());
303 }
304 else {
305 mesh_dst->attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
306 if (mesh_dst->default_color_attribute) {
307 return;
308 }
310 return;
311 }
312 mesh_dst->default_color_attribute = BLI_strdupn(iter.name.data(), iter.name.size());
313 });
314 }
315}
316
317static void transfer_active_uv_map_string(Mesh *mesh_dst, const Mesh *mesh_src)
318{
319 if (!mesh_dst->active_uv_map_name().is_empty()) {
320 return;
321 }
322 const StringRef name = mesh_src->active_uv_map_name();
323 const bke::AttributeAccessor attributes_src = mesh_src->attributes();
324 const bke::AttributeAccessor attributes_dst = mesh_dst->attributes();
325
326 if (!bke::mesh::is_uv_map(attributes_src.lookup_meta_data(name))) {
327 return;
328 }
329
330 if (bke::mesh::is_uv_map(attributes_dst.lookup_meta_data(name))) {
331 mesh_dst->uv_maps_active_set(name);
332 }
333 else {
334 mesh_dst->attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
335 if (!mesh_dst->active_uv_map_name().is_empty()) {
336 return;
337 }
338 if (!bke::mesh::is_uv_map({iter.domain, iter.data_type})) {
339 return;
340 }
341 mesh_dst->uv_maps_active_set(iter.name);
342 });
343 }
344}
345
346static void transfer_default_uv_map_string(Mesh *mesh_dst, const Mesh *mesh_src)
347{
348 if (!mesh_dst->default_uv_map_name().is_empty()) {
349 return;
350 }
351
352 const StringRef name = mesh_src->default_uv_map_name();
353 const bke::AttributeAccessor attributes_src = mesh_src->attributes();
354 const bke::AttributeAccessor attributes_dst = mesh_dst->attributes();
355
356 if (!bke::mesh::is_uv_map(attributes_src.lookup_meta_data(name))) {
357 return;
358 }
359
360 if (bke::mesh::is_uv_map(attributes_dst.lookup_meta_data(name))) {
361 mesh_dst->uv_maps_default_set(name);
362 }
363 else {
364 mesh_dst->attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
365 if (!mesh_dst->default_uv_map_name().is_empty()) {
366 return;
367 }
368 if (!bke::mesh::is_uv_map({iter.domain, iter.data_type})) {
369 return;
370 }
371 mesh_dst->uv_maps_default_set(iter.name);
372 });
373 }
374}
375
382 Mesh &mesh_dst,
383 const Mesh &mesh_src)
384{
385 if (ELEM(cddata_type, CD_PROP_COLOR, CD_PROP_BYTE_COLOR)) {
386 transfer_active_color_string(&mesh_dst, &mesh_src);
387 transfer_default_color_string(&mesh_dst, &mesh_src);
388 }
389 else if (ELEM(cddata_type, CD_PROP_FLOAT2)) {
390 transfer_active_uv_map_string(&mesh_dst, &mesh_src);
391 transfer_default_uv_map_string(&mesh_dst, &mesh_src);
392 }
393}
394
395/* ********** */
396
398 const int dtdata_type,
399 const bool changed)
400{
401 if (dtdata_type == DT_TYPE_LNOR) {
402 if (!changed) {
403 return;
404 }
405 /* Bake edited destination loop normals into custom normals again. */
406 CustomData *ldata_dst = &me_dst->corner_data;
407
408 float3 *loop_nors_dst = static_cast<float3 *>(
410
411 bke::MutableAttributeAccessor attributes = me_dst->attributes_for_write();
413 "custom_normal", bke::AttrDomain::Corner);
414 if (!custom_nors_dst) {
415 return;
416 }
418 "sharp_edge", bke::AttrDomain::Edge);
419 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
420 /* Note loop_nors_dst contains our custom normals as transferred from source... */
421 bke::mesh::normals_corner_custom_set(me_dst->vert_positions(),
422 me_dst->faces(),
423 me_dst->corner_verts(),
424 me_dst->corner_edges(),
425 me_dst->vert_to_face_map(),
426 me_dst->vert_normals(),
427 me_dst->face_normals_true(),
428 sharp_faces,
429 sharp_edges.span,
430 {loop_nors_dst, me_dst->corners_num},
431 custom_nors_dst.span);
432 custom_nors_dst.finish();
433 sharp_edges.finish();
435 }
436}
437
438/* ********** */
439
441{
442 switch (cddata_type) {
443 case CD_FAKE_UV:
445 default:
446 break;
447 }
448 return nullptr;
449}
450
451float data_transfer_interp_float_do(const int mix_mode,
452 const float val_dst,
453 const float val_src,
454 const float mix_factor)
455{
456 float val_ret;
457
458 if ((mix_mode == CDT_MIX_REPLACE_ABOVE_THRESHOLD && (val_dst < mix_factor)) ||
459 (mix_mode == CDT_MIX_REPLACE_BELOW_THRESHOLD && (val_dst > mix_factor)))
460 {
461 return val_dst; /* Do not affect destination. */
462 }
463
464 switch (mix_mode) {
467 return val_src;
468 case CDT_MIX_MIX:
469 val_ret = (val_dst + val_src) * 0.5f;
470 break;
471 case CDT_MIX_ADD:
472 val_ret = val_dst + val_src;
473 break;
474 case CDT_MIX_SUB:
475 val_ret = val_dst - val_src;
476 break;
477 case CDT_MIX_MUL:
478 val_ret = val_dst * val_src;
479 break;
480 case CDT_MIX_TRANSFER:
481 default:
482 val_ret = val_src;
483 break;
484 }
485 return interpf(val_ret, val_dst, mix_factor);
486}
487
488/* Helpers to match sources and destinations data layers
489 * (also handles 'conversions' in CD_FAKE cases). */
490
493 const int cddata_type,
494 const int mix_mode,
495 const float mix_factor,
496 const float *mix_weights,
497 std::variant<const void *, GVArray> data_src,
498 std::variant<void *, bke::GSpanAttributeWriter> data_dst,
499 const int data_src_n,
500 const int data_dst_n,
501 const size_t elem_size,
502 const size_t data_size,
503 const size_t data_offset,
505 void *interp_data)
506{
508
509 BLI_assert(std::visit([](const auto &value) { return bool(value); }, data_dst));
510
511 item.data_type = cddata_type;
512 item.mix_mode = mix_mode;
513 item.mix_factor = mix_factor;
514 item.mix_weights = mix_weights;
515
516 item.data_src = std::move(data_src);
517 if (auto *attr = std::get_if<bke::GSpanAttributeWriter>(&data_dst)) {
518 item.data_dst = std::move(attr->span);
519 item.tag_modified_fn = std::move(attr->tag_modified_fn);
520 }
521 else {
522 item.data_dst = std::get<void *>(data_dst);
523 }
524 item.data_src_n = data_src_n;
525 item.data_dst_n = data_dst_n;
526 item.elem_size = elem_size;
527
528 item.data_size = data_size;
529 item.data_offset = data_offset;
530
531 item.interp = interp;
532 item.interp_data = interp_data;
533
534 r_map->append(std::move(item));
535}
536
538 const int cddata_type,
539 const int mix_mode,
540 const float mix_factor,
541 const float *mix_weights,
542 const void *data_src,
543 void *data_dst,
544 const int data_src_n,
545 const int data_dst_n,
546 const size_t elem_size,
547 const size_t data_size,
548 const size_t data_offset,
550 void *interp_data)
551{
553 cddata_type,
554 mix_mode,
555 mix_factor,
556 mix_weights,
557 std::variant<const void *, GVArray>(data_src),
558 std::variant<void *, bke::GSpanAttributeWriter>(data_dst),
559 data_src_n,
560 data_dst_n,
561 elem_size,
562 data_size,
563 data_offset,
564 interp,
565 interp_data);
566}
567
570 const int cddata_type,
571 const int mix_mode,
572 const float mix_factor,
573 const float *mix_weights,
574 std::variant<const void *, GVArray> data_src,
575 std::variant<void *, bke::GSpanAttributeWriter> data_dst,
577 void *interp_data)
578{
580 cddata_type,
581 mix_mode,
582 mix_factor,
583 mix_weights,
584 std::move(data_src),
585 std::move(data_dst),
586 0,
587 0,
588 0,
589 0,
590 0,
591 interp,
592 interp_data);
593}
594
604 const eCustomDataType cddata_type,
605 const bke::AttrDomain domain,
606 const int mix_mode,
607 const float mix_factor,
608 const float *mix_weights,
609 const bool use_create,
610 const bool use_delete,
611 const Mesh &mesh_src,
612 Mesh &mesh_dst,
613 const Span<std::string> src_names,
614 const Span<std::string> dst_names,
615 const int tolayers,
616 const bool *use_layers_src,
617 const int num_layers_src)
618{
619 bke::AttributeAccessor src_attributes = mesh_src.attributes();
620 bke::MutableAttributeAccessor dst_attributes = mesh_dst.attributes_for_write();
621 const bke::AttrType attr_type = *bke::custom_data_type_to_attr_type(cddata_type);
622 std::variant<const void *, GVArray> data_src;
623 std::variant<void *, bke::GSpanAttributeWriter> data_dst = nullptr;
624 int idx_src = num_layers_src;
625 int idx_dst, tot_dst = dst_names.size();
626
627 if (!use_layers_src) {
628 /* No source at all, we can only delete all destination if requested. */
629 if (use_delete) {
630 idx_dst = tot_dst;
631 while (idx_dst--) {
632 dst_attributes.remove(dst_names[idx_dst]);
633 }
634 }
635 return true;
636 }
637
638 switch (tolayers) {
640 idx_dst = tot_dst;
641
642 /* Find last source actually used! */
643 while (idx_src-- && !use_layers_src[idx_src]) {
644 /* pass */
645 }
646 idx_src++;
647
648 if (idx_dst < idx_src) {
649 if (use_create) {
650 /* Create as much data layers as necessary! */
651 for (; idx_dst < idx_src; idx_dst++) {
652 dst_attributes.add(
653 src_names[idx_dst], domain, attr_type, bke::AttributeInitDefaultValue());
654 }
655 }
656 else {
657 /* Otherwise, just try to map what we can with existing dst data layers. */
658 idx_src = idx_dst;
659 }
660 }
661 else if (use_delete && idx_dst > idx_src) {
662 while (idx_dst-- > idx_src) {
663 dst_attributes.remove(dst_names[idx_dst]);
664 }
665 }
666 if (r_map) {
667 while (idx_src--) {
668 if (!use_layers_src[idx_src]) {
669 continue;
670 }
671 /* Layers created above use the source names and aren't part of #dst_names. */
672 const StringRef name_dst = idx_src < tot_dst ? StringRef(dst_names[idx_src]) :
673 StringRef(src_names[idx_src]);
674 data_src = *src_attributes.lookup(src_names[idx_src], domain, attr_type);
675 data_dst = dst_attributes.lookup_for_write_span(name_dst);
677 cddata_type,
678 mix_mode,
679 mix_factor,
680 mix_weights,
681 std::move(data_src),
682 std::move(data_dst),
683 nullptr,
684 nullptr);
685 }
686 }
687 break;
688 case DT_LAYERS_NAME_DST: {
689 Vector<std::string> data_dst_to_delete;
690
691 while (idx_src--) {
692 const char *name;
693
694 if (!use_layers_src[idx_src]) {
695 continue;
696 }
697
698 name = src_names[idx_src].c_str();
699 data_src = *src_attributes.lookup(src_names[idx_src], domain, attr_type);
700 data_dst = dst_attributes.lookup_for_write_span(name);
701 if (!std::get<bke::GSpanAttributeWriter>(data_dst)) {
702 if (use_create) {
703 dst_attributes.add(name, domain, attr_type, bke::AttributeInitDefaultValue());
704 data_dst = dst_attributes.lookup_for_write_span(name);
705 }
706 else {
707 /* If we are not allowed to create missing dst data layers,
708 * just skip matching src one. */
709 continue;
710 }
711 }
712 else if (use_delete) {
713 data_dst_to_delete.append(name);
714 }
715 if (r_map) {
717 cddata_type,
718 mix_mode,
719 mix_factor,
720 mix_weights,
721 std::move(data_src),
722 std::move(data_dst),
723 nullptr,
724 nullptr);
725 }
726 else {
727 /* Layout-only transfer, the writer isn't moved into the map so finish it. */
728 std::get<bke::GSpanAttributeWriter>(data_dst).finish();
729 }
730 }
731
732 /* NOTE:
733 * This won't affect newly created layers, if any, since tot_dst has not been updated!
734 * Also, looping backward ensures us we do not suffer
735 * from index shifting when deleting a layer. */
736 for (const StringRef name : data_dst_to_delete) {
737 dst_attributes.remove(name);
738 }
739
740 break;
741 }
742 default:
743 return false;
744 }
745
746 transfer_attribute_name_strings(cddata_type, mesh_dst, mesh_src);
747
748 return true;
749}
750
752 const eCustomDataType cddata_type,
753 const bke::AttrDomain domain,
754 const int mix_mode,
755 const float mix_factor,
756 const float *mix_weights,
757 const bool use_create,
758 const bool use_delete,
759 const Mesh &mesh_src,
760 Mesh &mesh_dst,
761 const int fromlayers,
762 const int tolayers)
763{
764 bke::AttributeAccessor src_attributes = mesh_src.attributes();
765 bke::MutableAttributeAccessor dst_attributes = mesh_dst.attributes_for_write();
766 const bke::AttrType attr_type = *bke::custom_data_type_to_attr_type(cddata_type);
767
768 Vector<std::string> src_names;
769 mesh_src.attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
770 if (iter.domain == domain && iter.data_type == attr_type) {
771 src_names.append(iter.name);
772 }
773 });
774 if (src_names.is_empty()) {
775 /* Can happen for colors, since we go over both CD_PROP_COLOR and CD_PROP_BYTE_COLOR. */
776 return true;
777 }
778
779 Vector<std::string> dst_names;
780 mesh_dst.attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
781 if (iter.domain == domain && iter.data_type == attr_type) {
782 dst_names.append(iter.name);
783 }
784 });
785
786 std::variant<void *, bke::GSpanAttributeWriter> data_dst = nullptr;
787
788 if (fromlayers == DT_LAYERS_ACTIVE_SRC || fromlayers >= 0) {
789 /* NOTE: use_delete has not much meaning in this case, ignored. */
790
791 std::string name_src;
792 if (fromlayers >= 0) { /* Real-layer index */
793 if (fromlayers >= src_names.size()) {
794 return true;
795 }
796 name_src = src_names[fromlayers];
797 }
798 else {
799 name_src = [&]() -> StringRef {
800 switch (cddata_type) {
801 case CD_PROP_FLOAT2:
802 return mesh_src.active_uv_map_name();
803 case CD_PROP_COLOR:
805 return StringRef(mesh_src.active_color_attribute);
806 default:
808 return "";
809 }
810 }();
811 if (name_src.empty()) {
812 return true;
813 }
814 }
815 const int idx_src = src_names.first_index_of(name_src);
816
817 GVArray data_src = *src_attributes.lookup(name_src, domain, attr_type);
818 if (!data_src) {
819 return true;
820 }
821
822 std::string name_dst;
823 int idx_dst;
824 if (tolayers >= 0) { /* Real-layer index */
825 /* The index can be out of range when the layers on the destination have changed, e.g. when
826 * an earlier modifier removed them. */
827 if (tolayers >= dst_names.size()) {
828 return false;
829 }
830 name_dst = dst_names[tolayers];
831 data_dst = dst_attributes.lookup_for_write_span(name_dst);
832 }
833 else if (tolayers == DT_LAYERS_ACTIVE_DST) {
834 name_dst = [&]() -> StringRef {
835 switch (cddata_type) {
836 case CD_PROP_FLOAT2:
837 return mesh_dst.active_uv_map_name();
838 case CD_PROP_COLOR:
840 return StringRef(mesh_dst.active_color_attribute);
841 default:
843 return "";
844 }
845 }();
846 if (name_dst.empty()) {
847 if (!use_create) {
848 return true;
849 }
850 name_dst = name_src;
851 dst_attributes.add(name_dst, domain, attr_type, bke::AttributeInitDefaultValue());
852 data_dst = dst_attributes.lookup_for_write_span(name_dst);
853 }
854 else {
855 data_dst = dst_attributes.lookup_for_write_span(name_dst);
856 }
857 }
858 else if (tolayers == DT_LAYERS_INDEX_DST) {
859 int num = dst_names.size();
860 idx_dst = idx_src;
861 if (num <= idx_dst) {
862 if (!use_create) {
863 return true;
864 }
865 /* Create as much data layers as necessary! */
866 for (; num <= idx_dst; num++) {
867 dst_attributes.add(src_names[num], domain, attr_type, bke::AttributeInitDefaultValue());
868 dst_names.append(src_names[num]);
869 }
870 }
871
872 name_dst = dst_names[idx_dst];
873 data_dst = dst_attributes.lookup_for_write_span(name_dst);
874 }
875 else if (tolayers == DT_LAYERS_NAME_DST) {
876 const char *name = name_src.c_str();
877 if (mesh_dst.attributes().lookup_meta_data(name) !=
878 bke::AttributeMetaData{domain, attr_type})
879 {
880 if (!use_create) {
881 return true;
882 }
883
884 dst_attributes.add(name, domain, attr_type, bke::AttributeInitDefaultValue());
885 }
886 data_dst = dst_attributes.lookup_for_write_span(name);
887 }
888 else {
889 return false;
890 }
891
892 if (!std::visit([&](const auto &value) { return bool(value); }, data_dst)) {
893 return false;
894 }
895
896 transfer_attribute_name_strings(cddata_type, mesh_dst, mesh_src);
897
898 if (r_map) {
900 cddata_type,
901 mix_mode,
902 mix_factor,
903 mix_weights,
904 data_src,
905 std::move(data_dst),
906 nullptr,
907 nullptr);
908 }
909 else {
910 /* Layout-only transfer, the writer isn't moved into the map so finish it. */
911 std::get<bke::GSpanAttributeWriter>(data_dst).finish();
912 }
913 }
914 else if (fromlayers == DT_LAYERS_ALL_SRC) {
915 int num_src = src_names.size();
916 bool *use_layers_src = num_src ? MEM_new_array_uninitialized<bool>(size_t(num_src), __func__) :
917 nullptr;
918 bool ret;
919
920 if (use_layers_src) {
921 memset(use_layers_src, true, sizeof(*use_layers_src) * num_src);
922 }
923
925 cddata_type,
926 domain,
927 mix_mode,
928 mix_factor,
929 mix_weights,
930 use_create,
931 use_delete,
932 mesh_src,
933 mesh_dst,
934 src_names,
935 dst_names,
936 tolayers,
937 use_layers_src,
938 num_src);
939
940 if (use_layers_src) {
941 MEM_delete(use_layers_src);
942 }
943
944 transfer_attribute_name_strings(cddata_type, mesh_dst, mesh_src);
945
946 return ret;
947 }
948 else {
949 return false;
950 }
951
952 return true;
953}
954
956 const eCustomDataType cddata_type,
957 const bke::AttrDomain domain,
958 const StringRef name,
959 const int mix_mode,
960 const float mix_factor,
961 const float *mix_weights,
962 const bool use_create,
963 const bool use_delete,
964 const Mesh &mesh_src,
965 Mesh &mesh_dst)
966{
967 bke::AttributeAccessor src_attributes = mesh_src.attributes();
968 bke::MutableAttributeAccessor dst_attributes = mesh_dst.attributes_for_write();
969 const bke::AttrType attr_type = *bke::custom_data_type_to_attr_type(cddata_type);
970
971 const GVArray data_src = *src_attributes.lookup(name, domain, attr_type);
972 if (data_src) {
973 bke::GSpanAttributeWriter data_dst = dst_attributes.lookup_for_write_span(name);
974 if (!data_dst && use_create) {
975 data_dst = dst_attributes.lookup_or_add_for_write_span(name, domain, attr_type);
976 }
977 if (r_map && data_dst && data_dst.domain == domain &&
978 bke::cpp_type_to_attribute_type(data_dst.span.type()) == attr_type)
979 {
981 cddata_type,
982 mix_mode,
983 mix_factor,
984 mix_weights,
985 std::move(data_src),
986 std::move(data_dst),
987 nullptr,
988 nullptr);
989 }
990 else if (data_dst) {
991 /* The writer wasn't moved into the map (layout-only transfer or mismatched domain/type). */
992 data_dst.finish();
993 }
994 }
995 else {
996 if (use_delete) {
997 dst_attributes.remove(name);
998 }
999 }
1000}
1001
1003 Object *ob_src,
1004 Object *ob_dst,
1005 const Mesh *me_src,
1006 Mesh *me_dst,
1007 const int elem_type,
1008 const int cddata_type,
1009 int mix_mode,
1010 float mix_factor,
1011 const float *mix_weights,
1012 const bool use_create,
1013 const bool use_delete,
1014 const int fromlayers,
1015 const int tolayers,
1016 SpaceTransform *space_transform)
1017{
1018 if (elem_type == ME_VERT) {
1019 if (cddata_type == CD_PROP_BYTE_COLOR) {
1023 mix_mode,
1024 mix_factor,
1025 mix_weights,
1026 use_create,
1027 use_delete,
1028 *me_src,
1029 *me_dst,
1030 fromlayers,
1031 tolayers))
1032 {
1033 return false;
1034 }
1035 return true;
1036 }
1037 else if (cddata_type == CD_PROP_COLOR) {
1041 mix_mode,
1042 mix_factor,
1043 mix_weights,
1044 use_create,
1045 use_delete,
1046 *me_src,
1047 *me_dst,
1048 fromlayers,
1049 tolayers))
1050 {
1051 return false;
1052 }
1053 return true;
1054 }
1055 if (cddata_type == CD_FAKE_MDEFORMVERT) {
1057 mix_mode,
1058 mix_factor,
1059 mix_weights,
1060 use_create,
1061 use_delete,
1062 ob_src,
1063 ob_dst,
1064 *me_src,
1065 *me_dst,
1066 id_cast<const ID *>(me_dst) != ob_dst->data,
1067 fromlayers,
1068 tolayers);
1069 }
1070 if (cddata_type == CD_FAKE_BWEIGHT) {
1074 "bevel_weight_vert",
1075 mix_mode,
1076 mix_factor,
1077 mix_weights,
1078 use_create,
1079 use_delete,
1080 *me_src,
1081 *me_dst);
1082 return true;
1083 }
1084 if (cddata_type == CD_FAKE_SKIN_RADIUS) {
1088 "skin_modifier_radius",
1089 mix_mode,
1090 mix_factor,
1091 mix_weights,
1092 use_create,
1093 use_delete,
1094 *me_src,
1095 *me_dst);
1096 return true;
1097 }
1098 }
1099 else if (elem_type == ME_EDGE) {
1100 if (cddata_type == CD_FAKE_SEAM) {
1104 "uv_seam",
1105 mix_mode,
1106 mix_factor,
1107 mix_weights,
1108 use_create,
1109 use_delete,
1110 *me_src,
1111 *me_dst);
1112 return true;
1113 }
1114 if (cddata_type == CD_FAKE_SHARP) {
1118 "sharp_edge",
1119 mix_mode,
1120 mix_factor,
1121 mix_weights,
1122 use_create,
1123 use_delete,
1124 *me_src,
1125 *me_dst);
1126 return true;
1127 }
1128 if (cddata_type == CD_FAKE_BWEIGHT) {
1132 "bevel_weight_edge",
1133 mix_mode,
1134 mix_factor,
1135 mix_weights,
1136 use_create,
1137 use_delete,
1138 *me_src,
1139 *me_dst);
1140 return true;
1141 }
1142 if (cddata_type == CD_FAKE_CREASE) {
1146 "crease_edge",
1147 mix_mode,
1148 mix_factor,
1149 mix_weights,
1150 use_create,
1151 use_delete,
1152 *me_src,
1153 *me_dst);
1154 return true;
1155 }
1156 if (r_map && cddata_type == CD_FAKE_FREESTYLE_EDGE) {
1160 "freestyle_edge",
1161 mix_mode,
1162 mix_factor,
1163 mix_weights,
1164 use_create,
1165 use_delete,
1166 *me_src,
1167 *me_dst);
1168 return true;
1169 }
1170
1171 return false;
1172 }
1173 else if (elem_type == ME_LOOP) {
1174 if (cddata_type == CD_FAKE_UV) {
1178 mix_mode,
1179 mix_factor,
1180 mix_weights,
1181 use_create,
1182 use_delete,
1183 *me_src,
1184 *me_dst,
1185 fromlayers,
1186 tolayers))
1187 {
1188 return false;
1189 }
1190 return true;
1191 }
1192 if (cddata_type == CD_FAKE_LNOR) {
1193 if (r_map) {
1194 /* Use #CD_NORMAL as a temporary storage for custom normals in 3D vector form.
1195 * A post-process step will convert this layer to "custom_normal". */
1196 float3 *dst_data = static_cast<float3 *>(
1198 if (!dst_data) {
1199 dst_data = static_cast<float3 *>(CustomData_add_layer(
1200 &me_dst->corner_data, CD_NORMAL, CD_SET_DEFAULT, me_dst->corners_num));
1201 }
1202 if (mix_factor != 1.0f || mix_weights) {
1203 MutableSpan(dst_data, me_dst->corners_num).copy_from(me_dst->corner_normals());
1204 }
1205 /* Post-process will convert it back to "custom_normal". */
1207 CD_NORMAL,
1208 mix_mode,
1209 mix_factor,
1210 mix_weights,
1211 me_src->corner_normals().data(),
1212 dst_data,
1214 space_transform);
1215 }
1216 return true;
1217 }
1218 if (cddata_type == CD_PROP_BYTE_COLOR) {
1222 mix_mode,
1223 mix_factor,
1224 mix_weights,
1225 use_create,
1226 use_delete,
1227 *me_src,
1228 *me_dst,
1229 fromlayers,
1230 tolayers))
1231 {
1232 return false;
1233 }
1234 return true;
1235 }
1236 if (cddata_type == CD_PROP_COLOR) {
1240 mix_mode,
1241 mix_factor,
1242 mix_weights,
1243 use_create,
1244 use_delete,
1245 *me_src,
1246 *me_dst,
1247 fromlayers,
1248 tolayers))
1249 {
1250 return false;
1251 }
1252 return true;
1253 }
1254 return false;
1255 }
1256 else if (elem_type == ME_POLY) {
1257 if (cddata_type == CD_FAKE_SHARP) {
1261 "sharp_face",
1262 mix_mode,
1263 mix_factor,
1264 mix_weights,
1265 use_create,
1266 use_delete,
1267 *me_src,
1268 *me_dst);
1269 return true;
1270 }
1271 if (cddata_type == CD_FAKE_FREESTYLE_FACE) {
1275 "freestyle_face",
1276 mix_mode,
1277 mix_factor,
1278 mix_weights,
1279 use_create,
1280 use_delete,
1281 *me_src,
1282 *me_dst);
1283 return true;
1284 }
1285 return false;
1286 }
1287
1288 return false;
1289}
1290
1291void BKE_object_data_transfer_layout(Depsgraph *depsgraph,
1292 Object *ob_src,
1293 Object *ob_dst,
1294 const int data_types,
1295 const bool use_delete,
1296 const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1297 const int tolayers_select[DT_MULTILAYER_INDEX_MAX])
1298{
1299 Mesh *me_dst;
1300
1301 const bool use_create = true; /* We always create needed layers here. */
1302
1303 BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1304
1305 me_dst = id_cast<Mesh *>(ob_dst->data);
1306
1307 /* Get source evaluated mesh. */
1308 const Object *ob_src_eval = DEG_get_evaluated(depsgraph, ob_src);
1309 const Mesh *me_src = BKE_object_get_evaluated_mesh(ob_src_eval);
1310 if (!me_src) {
1311 return;
1312 }
1313
1314 /* Check all possible data types. */
1315 for (int i = 0; i < DT_TYPE_MAX; i++) {
1316 const int dtdata_type = 1 << i;
1317 int cddata_type;
1318 int fromlayers, tolayers, fromto_idx;
1319
1320 if (!(data_types & dtdata_type)) {
1321 continue;
1322 }
1323
1324 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1325
1326 fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1327
1328 if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1329 fromlayers = fromlayers_select[fromto_idx];
1330 tolayers = tolayers_select[fromto_idx];
1331 }
1332 else {
1333 fromlayers = tolayers = 0;
1334 }
1335
1336 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1338 ob_src,
1339 ob_dst,
1340 me_src,
1341 me_dst,
1342 ME_VERT,
1343 cddata_type,
1344 0,
1345 0.0f,
1346 nullptr,
1347 use_create,
1348 use_delete,
1349 fromlayers,
1350 tolayers,
1351 nullptr);
1352 }
1353 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1355 ob_src,
1356 ob_dst,
1357 me_src,
1358 me_dst,
1359 ME_EDGE,
1360 cddata_type,
1361 0,
1362 0.0f,
1363 nullptr,
1364 use_create,
1365 use_delete,
1366 fromlayers,
1367 tolayers,
1368 nullptr);
1369 }
1370 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1372 ob_src,
1373 ob_dst,
1374 me_src,
1375 me_dst,
1376 ME_LOOP,
1377 cddata_type,
1378 0,
1379 0.0f,
1380 nullptr,
1381 use_create,
1382 use_delete,
1383 fromlayers,
1384 tolayers,
1385 nullptr);
1386 }
1387 if (DT_DATATYPE_IS_FACE(dtdata_type)) {
1389 ob_src,
1390 ob_dst,
1391 me_src,
1392 me_dst,
1393 ME_POLY,
1394 cddata_type,
1395 0,
1396 0.0f,
1397 nullptr,
1398 use_create,
1399 use_delete,
1400 fromlayers,
1401 tolayers,
1402 nullptr);
1403 }
1404
1405 transfer_attribute_name_strings(eCustomDataType(cddata_type), *me_dst, *me_src);
1406 }
1407}
1408
1409bool BKE_object_data_transfer_ex(Depsgraph *depsgraph,
1410 Object *ob_src,
1411 Object *ob_dst,
1412 Mesh *me_dst,
1413 const int data_types,
1414 bool use_create,
1415 const int map_vert_mode,
1416 const int map_edge_mode,
1417 const int map_loop_mode,
1418 const int map_face_mode,
1419 SpaceTransform *space_transform,
1420 const bool auto_transform,
1421 const float max_distance,
1422 const float ray_radius,
1423 const float islands_handling_precision,
1424 const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1425 const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1426 const int mix_mode,
1427 const float mix_factor,
1428 const char *vgroup_name,
1429 const bool invert_vgroup,
1430 ReportList *reports)
1431{
1432#define VDATA 0
1433#define EDATA 1
1434#define LDATA 2
1435#define PDATA 3
1436#define DATAMAX 4
1437
1438 SpaceTransform auto_space_transform;
1439
1440 const Mesh *me_src;
1441
1442 const MDeformVert *mdef = nullptr;
1443 int vg_idx = -1;
1444 float *weights[DATAMAX] = {nullptr};
1445
1446 MeshPairRemap geom_map[DATAMAX] = {{0}};
1447 bool geom_map_init[DATAMAX] = {false};
1449 bool changed = false;
1450 bool is_modifier = false;
1451
1452 const bool use_delete = false; /* We never delete data layers from destination here. */
1453
1454 BLI_assert((ob_src != ob_dst) && (ob_src->type == OB_MESH) && (ob_dst->type == OB_MESH));
1455
1456 if (me_dst) {
1457 /* Never create needed custom layers on passed destination mesh
1458 * (assumed to *not* be ob_dst->data, aka modifier case). */
1459 use_create = false;
1460 is_modifier = true;
1461 }
1462 else {
1463 me_dst = id_cast<Mesh *>(ob_dst->data);
1464 }
1465
1466 if (vgroup_name) {
1467 mdef = me_dst->deform_verts().data();
1468 if (mdef) {
1469 vg_idx = BKE_id_defgroup_name_index(&me_dst->id, vgroup_name);
1470 }
1471 }
1472
1473 /* Get source evaluated mesh. */
1474 if (is_modifier) {
1476 }
1477 else {
1478 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob_src);
1479 me_src = BKE_object_get_evaluated_mesh(ob_eval);
1480 }
1481 if (!me_src) {
1482 return changed;
1483 }
1484 BKE_mesh_wrapper_ensure_mdata(const_cast<Mesh *>(me_src));
1485
1486 if (auto_transform) {
1487 if (space_transform == nullptr) {
1488 space_transform = &auto_space_transform;
1489 }
1490
1491 if ((me_src->verts_num != 0) && (me_dst->verts_num != 0)) {
1492 BKE_mesh_remap_find_best_match_from_mesh(me_dst->vert_positions(), me_src, space_transform);
1493 }
1494 else {
1495 /* Just use the object matrices if there is no geometry, #160022. */
1496 BLI_SPACE_TRANSFORM_SETUP(space_transform, ob_dst, ob_src);
1497 }
1498 }
1499
1500 /* Check all possible data types.
1501 * Note item mappings and destination mix weights are cached. */
1502 for (int i = 0; i < DT_TYPE_MAX; i++) {
1503 const int dtdata_type = 1 << i;
1504 int cddata_type;
1505 int fromlayers, tolayers, fromto_idx;
1506
1507 if (!(data_types & dtdata_type)) {
1508 continue;
1509 }
1510
1511 cddata_type = BKE_object_data_transfer_dttype_to_cdtype(dtdata_type);
1512
1513 fromto_idx = BKE_object_data_transfer_dttype_to_srcdst_index(dtdata_type);
1514 if (fromto_idx != DT_MULTILAYER_INDEX_INVALID) {
1515 fromlayers = fromlayers_select[fromto_idx];
1516 tolayers = tolayers_select[fromto_idx];
1517 }
1518 else {
1519 fromlayers = tolayers = 0;
1520 }
1521
1522 if (DT_DATATYPE_IS_VERT(dtdata_type)) {
1523 MutableSpan<float3> positions_dst = me_dst->vert_positions_for_write();
1524 const int num_verts_dst = me_dst->verts_num;
1525
1526 if (!geom_map_init[VDATA]) {
1527 const int num_verts_src = me_src->verts_num;
1528
1529 if ((map_vert_mode == MREMAP_MODE_TOPOLOGY) && (num_verts_dst != num_verts_src)) {
1530 BKE_report(reports,
1531 RPT_ERROR,
1532 "Source and destination meshes do not have the same number of vertices, "
1533 "'Topology' mapping cannot be used in this case");
1534 continue;
1535 }
1536 if ((map_vert_mode & MREMAP_USE_EDGE) && (me_src->edges_num == 0)) {
1537 BKE_report(reports,
1538 RPT_ERROR,
1539 "Source mesh does not have any edges, "
1540 "none of the 'Edge' mappings can be used in this case");
1541 continue;
1542 }
1543 if ((map_vert_mode & MREMAP_USE_POLY) && (me_src->faces_num == 0)) {
1544 BKE_report(reports,
1545 RPT_ERROR,
1546 "Source mesh does not have any faces, "
1547 "none of the 'Face' mappings can be used in this case");
1548 continue;
1549 }
1550 if (ELEM(0, num_verts_dst, num_verts_src)) {
1551 BKE_report(reports,
1552 RPT_ERROR,
1553 "Source or destination meshes do not have any vertices, cannot transfer "
1554 "vertex data");
1555 continue;
1556 }
1557
1559 space_transform,
1560 max_distance,
1561 ray_radius,
1562 positions_dst,
1563 me_src,
1564 me_dst,
1565 &geom_map[VDATA]);
1566 geom_map_init[VDATA] = true;
1567 }
1568
1569 if (mdef && vg_idx != -1 && !weights[VDATA]) {
1570 weights[VDATA] = MEM_new_array_uninitialized<float>(size_t(num_verts_dst), __func__);
1572 mdef, vg_idx, num_verts_dst, invert_vgroup, weights[VDATA]);
1573 }
1574
1576 ob_src,
1577 ob_dst,
1578 me_src,
1579 me_dst,
1580 ME_VERT,
1581 cddata_type,
1582 mix_mode,
1583 mix_factor,
1584 weights[VDATA],
1585 use_create,
1586 use_delete,
1587 fromlayers,
1588 tolayers,
1589 space_transform))
1590 {
1591 changed |= !lay_map.is_empty();
1592
1593 for (CustomDataTransferLayerMap &lay_mapit : lay_map) {
1594 CustomData_data_transfer(&geom_map[VDATA], &lay_mapit);
1595 }
1596
1597 lay_map.clear();
1598 }
1599 }
1600 if (DT_DATATYPE_IS_EDGE(dtdata_type)) {
1601 MutableSpan<float3> positions_dst = me_dst->vert_positions_for_write();
1602
1603 const int num_verts_dst = me_dst->verts_num;
1604 const Span<int2> edges_dst = me_dst->edges();
1605
1606 if (!geom_map_init[EDATA]) {
1607 const int num_edges_src = me_src->edges_num;
1608
1609 if ((map_edge_mode == MREMAP_MODE_TOPOLOGY) && (edges_dst.size() != num_edges_src)) {
1610 BKE_report(reports,
1611 RPT_ERROR,
1612 "Source and destination meshes do not have the same number of edges, "
1613 "'Topology' mapping cannot be used in this case");
1614 continue;
1615 }
1616 if ((map_edge_mode & MREMAP_USE_POLY) && (me_src->faces_num == 0)) {
1617 BKE_report(reports,
1618 RPT_ERROR,
1619 "Source mesh does not have any faces, "
1620 "none of the 'Face' mappings can be used in this case");
1621 continue;
1622 }
1623 if (ELEM(0, edges_dst.size(), num_edges_src)) {
1624 BKE_report(
1625 reports,
1626 RPT_ERROR,
1627 "Source or destination meshes do not have any edges, cannot transfer edge data");
1628 continue;
1629 }
1630
1632 space_transform,
1633 max_distance,
1634 ray_radius,
1635 positions_dst,
1636 edges_dst,
1637 me_src,
1638 me_dst,
1639 &geom_map[EDATA]);
1640 geom_map_init[EDATA] = true;
1641 }
1642
1643 if (mdef && vg_idx != -1 && !weights[EDATA]) {
1644 weights[EDATA] = MEM_new_array_uninitialized<float>(size_t(edges_dst.size()), __func__);
1646 mdef, vg_idx, num_verts_dst, edges_dst, invert_vgroup, weights[EDATA]);
1647 }
1648
1650 ob_src,
1651 ob_dst,
1652 me_src,
1653 me_dst,
1654 ME_EDGE,
1655 cddata_type,
1656 mix_mode,
1657 mix_factor,
1658 weights[EDATA],
1659 use_create,
1660 use_delete,
1661 fromlayers,
1662 tolayers,
1663 space_transform))
1664 {
1665 changed |= !lay_map.is_empty();
1666
1667 for (CustomDataTransferLayerMap &lay_mapit : lay_map) {
1668 CustomData_data_transfer(&geom_map[EDATA], &lay_mapit);
1669 }
1670
1671 lay_map.clear();
1672 }
1673 }
1674 if (DT_DATATYPE_IS_LOOP(dtdata_type)) {
1675 const Span<float3> positions_dst = me_dst->vert_positions();
1676 const int num_verts_dst = me_dst->verts_num;
1677 const OffsetIndices faces_dst = me_dst->faces();
1678 const Span<int> corner_verts_dst = me_dst->corner_verts();
1679
1681
1682 if (!geom_map_init[LDATA]) {
1683 const int num_loops_src = me_src->corners_num;
1684
1685 if ((map_loop_mode == MREMAP_MODE_TOPOLOGY) && (corner_verts_dst.size() != num_loops_src))
1686 {
1687 BKE_report(reports,
1688 RPT_ERROR,
1689 "Source and destination meshes do not have the same number of face corners, "
1690 "'Topology' mapping cannot be used in this case");
1691 continue;
1692 }
1693 if ((map_loop_mode & MREMAP_USE_EDGE) && (me_src->edges_num == 0)) {
1694 BKE_report(reports,
1695 RPT_ERROR,
1696 "Source mesh does not have any edges, "
1697 "none of the 'Edge' mappings can be used in this case");
1698 continue;
1699 }
1700 if (ELEM(0, corner_verts_dst.size(), num_loops_src)) {
1701 BKE_report(
1702 reports,
1703 RPT_ERROR,
1704 "Source or destination meshes do not have any faces, cannot transfer corner data");
1705 continue;
1706 }
1707
1709 space_transform,
1710 max_distance,
1711 ray_radius,
1712 me_dst,
1713 positions_dst,
1714 corner_verts_dst,
1715 faces_dst,
1716 me_src,
1717 island_callback,
1718 islands_handling_precision,
1719 &geom_map[LDATA]);
1720 geom_map_init[LDATA] = true;
1721 }
1722
1723 if (mdef && vg_idx != -1 && !weights[LDATA]) {
1724 weights[LDATA] = MEM_new_array_uninitialized<float>(size_t(corner_verts_dst.size()),
1725 __func__);
1727 mdef, vg_idx, num_verts_dst, corner_verts_dst, invert_vgroup, weights[LDATA]);
1728 }
1729
1731 ob_src,
1732 ob_dst,
1733 me_src,
1734 me_dst,
1735 ME_LOOP,
1736 cddata_type,
1737 mix_mode,
1738 mix_factor,
1739 weights[LDATA],
1740 use_create,
1741 use_delete,
1742 fromlayers,
1743 tolayers,
1744 space_transform))
1745 {
1746 changed |= !lay_map.is_empty();
1747
1748 for (CustomDataTransferLayerMap &lay_mapit : lay_map) {
1749 CustomData_data_transfer(&geom_map[LDATA], &lay_mapit);
1750 }
1751
1752 lay_map.clear();
1753 }
1754 }
1755 if (DT_DATATYPE_IS_FACE(dtdata_type)) {
1756 const Span<float3> positions_dst = me_dst->vert_positions();
1757 const int num_verts_dst = me_dst->verts_num;
1758 const OffsetIndices faces_dst = me_dst->faces();
1759 const Span<int> corner_verts_dst = me_dst->corner_verts();
1760
1761 if (!geom_map_init[PDATA]) {
1762 const int num_faces_src = me_src->faces_num;
1763
1764 if ((map_face_mode == MREMAP_MODE_TOPOLOGY) && (faces_dst.size() != num_faces_src)) {
1765 BKE_report(reports,
1766 RPT_ERROR,
1767 "Source and destination meshes do not have the same number of faces, "
1768 "'Topology' mapping cannot be used in this case");
1769 continue;
1770 }
1771 if ((map_face_mode & MREMAP_USE_EDGE) && (me_src->edges_num == 0)) {
1772 BKE_report(reports,
1773 RPT_ERROR,
1774 "Source mesh does not have any edges, "
1775 "none of the 'Edge' mappings can be used in this case");
1776 continue;
1777 }
1778 if (ELEM(0, faces_dst.size(), num_faces_src)) {
1779 BKE_report(
1780 reports,
1781 RPT_ERROR,
1782 "Source or destination meshes do not have any faces, cannot transfer face data");
1783 continue;
1784 }
1785
1787 space_transform,
1788 max_distance,
1789 ray_radius,
1790 me_dst,
1791 positions_dst,
1792 corner_verts_dst,
1793 faces_dst,
1794 me_src,
1795 &geom_map[PDATA]);
1796 geom_map_init[PDATA] = true;
1797 }
1798
1799 if (mdef && vg_idx != -1 && !weights[PDATA]) {
1800 weights[PDATA] = MEM_new_array_uninitialized<float>(size_t(faces_dst.size()), __func__);
1802 vg_idx,
1803 num_verts_dst,
1804 corner_verts_dst,
1805 faces_dst,
1806 invert_vgroup,
1807 weights[PDATA]);
1808 }
1809
1811 ob_src,
1812 ob_dst,
1813 me_src,
1814 me_dst,
1815 ME_POLY,
1816 cddata_type,
1817 mix_mode,
1818 mix_factor,
1819 weights[PDATA],
1820 use_create,
1821 use_delete,
1822 fromlayers,
1823 tolayers,
1824 space_transform))
1825 {
1826 changed |= !lay_map.is_empty();
1827
1828 for (CustomDataTransferLayerMap &lay_mapit : lay_map) {
1829 CustomData_data_transfer(&geom_map[PDATA], &lay_mapit);
1830 }
1831
1832 lay_map.clear();
1833 }
1834 }
1835
1836 data_transfer_dtdata_type_postprocess(me_dst, dtdata_type, changed);
1837 }
1838
1839 for (int i = 0; i < DATAMAX; i++) {
1840 BKE_mesh_remap_free(&geom_map[i]);
1841 MEM_SAFE_DELETE(weights[i]);
1842 }
1843
1844 return changed;
1845
1846#undef VDATA
1847#undef EDATA
1848#undef LDATA
1849#undef PDATA
1850#undef DATAMAX
1851}
1852
1853bool BKE_object_data_transfer_mesh(Depsgraph *depsgraph,
1854 Object *ob_src,
1855 Object *ob_dst,
1856 const int data_types,
1857 const bool use_create,
1858 const int map_vert_mode,
1859 const int map_edge_mode,
1860 const int map_loop_mode,
1861 const int map_face_mode,
1862 SpaceTransform *space_transform,
1863 const bool auto_transform,
1864 const float max_distance,
1865 const float ray_radius,
1866 const float islands_handling_precision,
1867 const int fromlayers_select[DT_MULTILAYER_INDEX_MAX],
1868 const int tolayers_select[DT_MULTILAYER_INDEX_MAX],
1869 const int mix_mode,
1870 const float mix_factor,
1871 const char *vgroup_name,
1872 const bool invert_vgroup,
1873 ReportList *reports)
1874{
1875 return BKE_object_data_transfer_ex(depsgraph,
1876 ob_src,
1877 ob_dst,
1878 nullptr,
1879 data_types,
1880 use_create,
1881 map_vert_mode,
1882 map_edge_mode,
1883 map_loop_mode,
1884 map_face_mode,
1885 space_transform,
1886 auto_transform,
1887 max_distance,
1888 ray_radius,
1889 islands_handling_precision,
1890 fromlayers_select,
1891 tolayers_select,
1892 mix_mode,
1893 mix_factor,
1894 vgroup_name,
1895 invert_vgroup,
1896 reports);
1897}
1898
1899} // namespace blender
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
General operations, lookup, etc. for blender objects.
#define BLI_SPACE_TRANSFORM_SETUP(data, local, target)
#define DT_TYPE_MAX
#define DT_DATATYPE_IS_LOOP(_dt)
#define DT_DATATYPE_IS_EDGE(_dt)
#define DT_DATATYPE_IS_FACE(_dt)
#define DT_DATATYPE_IS_VERT(_dt)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define CD_MASK_MDEFORMVERT
#define CD_MASK_PROP_FLOAT2
const CPPType & type() const
constexpr MutableSpan()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr StringRef()
int64_t size() const
void append(const T &value)
bool is_empty() const
int64_t first_index_of(const T &value) const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GAttributeReader lookup(const StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef name)
constexpr int64_t size() const
constexpr const char * data() const
#define DATAMAX
#define EDATA
#define VDATA
#define LDATA
#define PDATA
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
bool is_uv_map(const AttributeMetaData &meta_data)
bool is_color_attribute(const AttributeMetaData &meta_data)
void normals_corner_custom_set(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, Span< float3 > vert_normals, Span< float3 > face_normals, Span< bool > sharp_faces, MutableSpan< bool > sharp_edges, MutableSpan< float3 > r_custom_corner_normals, MutableSpan< short2 > r_clnors_data)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
AttrType cpp_type_to_attribute_type(const CPPType &type)
BMesh Iterators.
static void transfer_active_uv_map_string(Mesh *mesh_dst, const Mesh *mesh_src)
@ CDT_MIX_REPLACE_ABOVE_THRESHOLD
@ CDT_MIX_REPLACE_BELOW_THRESHOLD
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool data_transfer_layersmapping_cdlayers(Vector< CustomDataTransferLayerMap > *r_map, const eCustomDataType cddata_type, const bke::AttrDomain domain, const int mix_mode, const float mix_factor, const float *mix_weights, const bool use_create, const bool use_delete, const Mesh &mesh_src, Mesh &mesh_dst, const int fromlayers, const int tolayers)
@ DT_MULTILAYER_INDEX_VCOL_VERT
@ DT_MULTILAYER_INDEX_MDEFORMVERT
@ DT_MULTILAYER_INDEX_INVALID
@ DT_MULTILAYER_INDEX_VCOL_LOOP
void BKE_mesh_remap_free(MeshPairRemap *map)
void BKE_mesh_remap_calc_edges_from_mesh(int mode, const SpaceTransform *space_transform, float max_dist, float ray_radius, Span< float3 > vert_positions_dst, Span< int2 > edges_dst, const Mesh *me_src, Mesh *me_dst, MeshPairRemap *r_map)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_mesh_remap_find_best_match_from_mesh(Span< float3 > vert_positions_dst, const Mesh *me_src, SpaceTransform *r_space_transform)
bool CustomData_free_layers(CustomData *data, eCustomDataType type)
static bool data_transfer_layersmapping_generate(Vector< CustomDataTransferLayerMap > *r_map, Object *ob_src, Object *ob_dst, const Mesh *me_src, Mesh *me_dst, const int elem_type, const int cddata_type, int mix_mode, float mix_factor, const float *mix_weights, const bool use_create, const bool use_delete, const int fromlayers, const int tolayers, SpaceTransform *space_transform)
int BKE_object_data_transfer_dttype_to_srcdst_index(int dtdata_type)
int BKE_object_data_transfer_get_dttypes_item_types(int dtdata_types)
static void transfer_active_color_string(Mesh *mesh_dst, const Mesh *mesh_src)
void BKE_mesh_wrapper_ensure_mdata(Mesh *mesh)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BKE_object_data_transfer_layout(struct Depsgraph *depsgraph, struct Object *ob_src, struct Object *ob_dst, int data_types, bool use_delete, const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX])
void BKE_defvert_extract_vgroup_to_faceweights(const MDeformVert *dvert, int defgroup, int verts_num, const Span< int > corner_verts, OffsetIndices< int > faces, bool invert_vgroup, float *r_weights)
Definition deform.cc:1165
void BKE_defvert_extract_vgroup_to_vertweights(const MDeformVert *dvert, int defgroup, int verts_num, bool invert_vgroup, float *r_weights)
Definition deform.cc:1094
int BKE_id_defgroup_name_index(const ID *id, StringRef name)
Definition deform.cc:557
Mesh * BKE_modifier_get_evaluated_mesh_from_evaluated_object(Object *ob_eval)
void customdata_data_transfer_interp_normal_normals(const CustomDataTransferLayerMap *laymap, void *data_dst, const void **sources, const float *weights, const int count, const float mix_factor)
bool BKE_object_data_transfer_ex(struct Depsgraph *depsgraph, struct Object *ob_src, struct Object *ob_dst, struct Mesh *me_dst, int data_types, bool use_create, int map_vert_mode, int map_edge_mode, int map_loop_mode, int map_face_mode, struct SpaceTransform *space_transform, bool auto_transform, float max_distance, float ray_radius, float islands_handling_precision, const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX], int mix_mode, float mix_factor, const char *vgroup_name, bool invert_vgroup, struct ReportList *reports)
static bool data_transfer_layersmapping_cdlayers_multisrc_to_dst(Vector< CustomDataTransferLayerMap > *r_map, const eCustomDataType cddata_type, const bke::AttrDomain domain, const int mix_mode, const float mix_factor, const float *mix_weights, const bool use_create, const bool use_delete, const Mesh &mesh_src, Mesh &mesh_dst, const Span< std::string > src_names, const Span< std::string > dst_names, const int tolayers, const bool *use_layers_src, const int num_layers_src)
void BKE_defvert_extract_vgroup_to_edgeweights(const MDeformVert *dvert, int defgroup, int verts_num, Span< int2 > edges, bool invert_vgroup, float *r_weights)
Definition deform.cc:1113
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool BKE_object_data_transfer_get_dttypes_capacity(int dtdata_types, bool *r_advanced_mixing, bool *r_threshold)
void BKE_defvert_extract_vgroup_to_loopweights(const MDeformVert *dvert, int defgroup, int verts_num, Span< int > corner_verts, bool invert_vgroup, float *r_weights)
Definition deform.cc:1140
bool BKE_object_data_transfer_mesh(struct Depsgraph *depsgraph, struct Object *ob_src, struct Object *ob_dst, int data_types, bool use_create, int map_vert_mode, int map_edge_mode, int map_loop_mode, int map_face_mode, struct SpaceTransform *space_transform, bool auto_transform, float max_distance, float ray_radius, float islands_handling_precision, const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX], int mix_mode, float mix_factor, const char *vgroup_name, bool invert_vgroup, struct ReportList *reports)
static MeshRemapIslandsCalc data_transfer_get_loop_islands_generator(const int cddata_type)
void BKE_mesh_remap_calc_loops_from_mesh(int mode, const SpaceTransform *space_transform, float max_dist, float ray_radius, const Mesh *mesh_dst, Span< float3 > vert_positions_dst, Span< int > corner_verts_dst, const OffsetIndices< int > faces_dst, const Mesh *me_src, MeshRemapIslandsCalc gen_islands_src, float islands_precision_src, MeshPairRemap *r_map)
bool data_transfer_layersmapping_vgroups(Vector< CustomDataTransferLayerMap > *r_map, int mix_mode, float mix_factor, const float *mix_weights, bool use_create, bool use_delete, Object *ob_src, Object *ob_dst, const Mesh &mesh_src, Mesh &mesh_dst, bool use_dupref_dst, int fromlayers, int tolayers)
Definition deform.cc:1397
ATTR_WARN_UNUSED_RESULT const size_t num
static void data_transfer_layersmapping_add_item_attr(Vector< CustomDataTransferLayerMap > *r_map, const eCustomDataType cddata_type, const bke::AttrDomain domain, const StringRef name, const int mix_mode, const float mix_factor, const float *mix_weights, const bool use_create, const bool use_delete, const Mesh &mesh_src, Mesh &mesh_dst)
static void transfer_default_color_string(Mesh *mesh_dst, const Mesh *mesh_src)
void BKE_mesh_remap_calc_faces_from_mesh(int mode, const SpaceTransform *space_transform, float max_dist, float ray_radius, const Mesh *mesh_dst, Span< float3 > vert_positions_dst, Span< int > corner_verts, const OffsetIndices< int > faces_dst, const Mesh *me_src, MeshPairRemap *r_map)
static void data_transfer_layersmapping_add_item(Vector< CustomDataTransferLayerMap > *r_map, const int cddata_type, const int mix_mode, const float mix_factor, const float *mix_weights, std::variant< const void *, GVArray > data_src, std::variant< void *, bke::GSpanAttributeWriter > data_dst, const int data_src_n, const int data_dst_n, const size_t elem_size, const size_t data_size, const size_t data_offset, cd_datatransfer_interp interp, void *interp_data)
static void data_transfer_dtdata_type_postprocess(Mesh *me_dst, const int dtdata_type, const bool changed)
float data_transfer_interp_float_do(const int mix_mode, const float val_dst, const float val_src, const float mix_factor)
static void transfer_default_uv_map_string(Mesh *mesh_dst, const Mesh *mesh_src)
VecBase< int16_t, 2 > short2
bool(*)(Span< float3 > vert_positions, Span< int2 > edges, Span< bool > uv_seams, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, MeshIslandStore *r_island_store) MeshRemapIslandsCalc
void(*)(const CustomDataTransferLayerMap *laymap, void *dest, const void **sources, const float *weights, int count, float mix_factor) cd_datatransfer_interp
MINLINE float interpf(float target, float origin, float t)
void BKE_object_data_transfer_dttypes_to_cdmask(int dtdata_types, struct CustomData_MeshMasks *r_data_masks)
static void data_transfer_layersmapping_add_item_cd(Vector< CustomDataTransferLayerMap > *r_map, const int cddata_type, const int mix_mode, const float mix_factor, const float *mix_weights, std::variant< const void *, GVArray > data_src, std::variant< void *, bke::GSpanAttributeWriter > data_dst, cd_datatransfer_interp interp, void *interp_data)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
VecBase< float, 3 > float3
static void transfer_attribute_name_strings(const eCustomDataType cddata_type, Mesh &mesh_dst, const Mesh &mesh_src)
void BKE_mesh_remap_calc_verts_from_mesh(int mode, const SpaceTransform *space_transform, float max_dist, float ray_radius, Span< float3 > vert_positions_dst, const Mesh *me_src, Mesh *me_dst, MeshPairRemap *r_map)
bool BKE_mesh_calc_islands_loop_face_edgeseam(Span< float3 > vert_positions, Span< int2 > edges, Span< bool > uv_seams, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, MeshIslandStore *r_island_store)
int BKE_object_data_transfer_dttype_to_cdtype(int dtdata_type)
void CustomData_data_transfer(const MeshPairRemap *me_remap, CustomDataTransferLayerMap *laymap)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
std::function< void()> tag_modified_fn
std::variant< const void *, GVArray > data_src
std::variant< void *, GMutableVArraySpan > data_dst
CustomData corner_data
char * active_color_attribute
char * default_color_attribute