Blender V5.3
attribute_legacy_convert.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#define DNA_DEPRECATED_ALLOW
10
11#include <optional>
12
14#include "DNA_mesh_types.h"
16
17#include "BKE_attribute.hh"
18#include "BKE_curves.hh"
19#include "BKE_customdata.hh"
20
22
23namespace blender::bke {
24
25std::optional<AttrType> custom_data_type_to_attr_type(const eCustomDataType data_type)
26{
27 switch (data_type) {
28 /* These types are not used for actual #CustomData layers. */
29 case CD_NUMTYPES:
31 case CD_TANGENT:
33 return std::nullopt;
34
35 /* These types are only used for versioning old files. */
36 case CD_MVERT:
37 case CD_MSTICKY:
38 case CD_MEDGE:
39 case CD_FACEMAP:
40 case CD_MTEXPOLY:
41 case CD_MLOOPUV:
42 case CD_MPOLY:
43 case CD_MLOOP:
44 case CD_BWEIGHT:
45 case CD_CREASE:
46 case CD_PAINT_MASK:
47 case CD_CUSTOMLOOPNORMAL:
48 case CD_SCULPT_FACE_SETS:
49 case CD_MTFACE:
51 case CD_FREESTYLE_EDGE:
52 case CD_FREESTYLE_FACE:
53 case CD_MVERT_SKIN:
54 return std::nullopt;
55
56 /* These types are only used for #BMesh. */
57 case CD_SHAPEKEY:
60 return std::nullopt;
61
62 /* Only used for legacy #MFace data. */
63 case CD_MFACE:
64 case CD_ORIGSPACE:
65 case CD_MCOL:
66 return std::nullopt;
67
68 /* Custom data on vertices. */
69 case CD_MDEFORMVERT:
70 case CD_ORCO:
71 case CD_CLOTH_ORCO:
72 return std::nullopt;
73
74 /* Custom data on face corners. */
75 case CD_NORMAL:
76 case CD_MDISPS:
79 return std::nullopt;
80
81 /* Use for editing/selecting original data from evaluated mesh (vertices, edges, faces). */
82 case CD_ORIGINDEX:
83 return std::nullopt;
84
85 /* Used as a cache of tangents for current RNA API (face corners). */
86 case CD_MLOOPTANGENT:
87 return std::nullopt;
88
89 /* Attribute types. */
90 case CD_PROP_FLOAT:
91 return AttrType::Float;
92 case CD_PROP_INT32:
93 return AttrType::Int32;
97 return AttrType::Float4x4;
99 return AttrType::Int16_2D;
100 case CD_PROP_INT8:
101 return AttrType::Int8;
102 case CD_PROP_INT32_2D:
103 return AttrType::Int32_2D;
104 case CD_PROP_COLOR:
106 case CD_PROP_FLOAT3:
107 return AttrType::Float3;
108 case CD_PROP_FLOAT4:
109 return AttrType::Float4;
110 case CD_PROP_FLOAT2:
111 return AttrType::Float2;
112 case CD_PROP_BOOL:
113 return AttrType::Bool;
114 case CD_PROP_STRING:
115 return AttrType::String;
118 }
119 return std::nullopt;
120}
121
126
133{
134 struct AttributeToAdd {
135 StringRef name;
136 AttrDomain domain;
137 AttrType type;
138 void *array_data;
139 int array_size;
140 const ImplicitSharingInfo *sharing_info;
141 };
143 Vector<AttributeToAdd> attributes_to_add;
144 for (const auto &item : domains.items()) {
145 const AttrDomain domain = item.key;
146 const CustomData &custom_data = item.value.data;
147 const int domain_size = item.value.size;
148 for (const CustomDataLayer &layer : MutableSpan(custom_data.layers, custom_data.totlayer)) {
149 if (const std::optional<AttrType> attr_type = custom_data_type_to_attr_type(
150 eCustomDataType(layer.type)))
151 {
152 /* Skip adding a user. This #CustomDataLayer is just freed below. */
153 attributes_to_add.append(
154 {layer.name, domain, *attr_type, layer.data, domain_size, layer.sharing_info});
155 }
156 else {
157 layers_to_keep.lookup_or_add_default(domain).append(layer);
158 }
159 }
160 }
161
162 for (AttributeToAdd &attribute : attributes_to_add) {
163 bke::Attribute::ArrayData array_data;
164 array_data.data = attribute.array_data;
165 array_data.size = attribute.array_size;
166 array_data.sharing_info = ImplicitSharingPtr<>(attribute.sharing_info);
167 if (storage.lookup(attribute.name)) {
168 storage.remove(attribute.name);
169 }
170 storage.add(storage.unique_name_calc(attribute.name),
171 attribute.domain,
172 attribute.type,
173 std::move(array_data));
174 }
175
176 for (const auto &[domain, custom_data] : domains.items()) {
177 Vector layers_vector = layers_to_keep.pop_default(domain, {});
178 MEM_SAFE_DELETE(custom_data.data.layers);
179 custom_data.data.totlayer = 0;
180 custom_data.data.maxlayer = 0;
181 if (layers_vector.is_empty()) {
182 CustomData_update_typemap(&custom_data.data);
183 continue;
184 }
185 VectorData data = layers_vector.release();
186 custom_data.data.layers = data.data;
187 custom_data.data.totlayer = data.size;
188 custom_data.data.maxlayer = data.capacity;
189 CustomData_update_typemap(&custom_data.data);
190 }
191}
192
193std::optional<eCustomDataType> attr_type_to_custom_data_type(const AttrType attr_type)
194{
195 switch (attr_type) {
196 case AttrType::Bool:
197 return CD_PROP_BOOL;
198 case AttrType::Int8:
199 return CD_PROP_INT8;
201 return CD_PROP_INT16_2D;
202 case AttrType::Int32:
203 return CD_PROP_INT32;
205 return CD_PROP_INT32_2D;
206 case AttrType::Float:
207 return CD_PROP_FLOAT;
208 case AttrType::Float2:
209 return CD_PROP_FLOAT2;
210 case AttrType::Float3:
211 return CD_PROP_FLOAT3;
212 case AttrType::Float4:
213 return CD_PROP_FLOAT4;
215 return CD_PROP_FLOAT4X4;
217 return CD_PROP_BYTE_COLOR;
219 return CD_PROP_COLOR;
221 return CD_PROP_QUATERNION;
222 case AttrType::String:
223 return CD_PROP_STRING;
224 }
225 return std::nullopt;
226}
227
229{
231 {{AttrDomain::Point, {mesh.vert_data, mesh.verts_num}},
232 {AttrDomain::Edge, {mesh.edge_data, mesh.edges_num}},
233 {AttrDomain::Face, {mesh.face_data, mesh.faces_num}},
234 {AttrDomain::Corner, {mesh.corner_data, mesh.corners_num}}},
235 mesh.attribute_storage.wrap());
236}
237
239{
241 {{AttrDomain::Point, {curves.point_data, curves.points_num()}},
242 {AttrDomain::Curve, {curves.curve_data_legacy, curves.curves_num()}}},
243 curves.attribute_storage.wrap());
244 CustomData_reset(&curves.curve_data_legacy);
245 /* Update the curve type count again (the first time was done on file-read, where
246 * #AttributeStorage data doesn't exist yet for older files). */
247 curves.update_curve_types();
248}
249
257
259{
262 {grease_pencil.layers_data_legacy, int(grease_pencil.layers().size())}}},
263 grease_pencil.attribute_storage.wrap());
264 CustomData_reset(&grease_pencil.layers_data_legacy);
265}
266
267static const CustomData &get_custom_data(const Mesh &mesh, const AttrDomain domain)
268{
269 switch (domain) {
271 return mesh.vert_data;
272 case AttrDomain::Edge:
273 return mesh.edge_data;
274 case AttrDomain::Face:
275 return mesh.face_data;
277 return mesh.corner_data;
278 default:
280 return mesh.vert_data;
281 }
282}
283
285{
286 return const_cast<CustomData &>(get_custom_data(std::as_const(mesh), domain));
287}
288
289static int get_domain_size(const Mesh &mesh, const AttrDomain domain)
290{
291 switch (domain) {
293 return mesh.verts_num;
294 case AttrDomain::Edge:
295 return mesh.edges_num;
296 case AttrDomain::Face:
297 return mesh.faces_num;
299 return mesh.corners_num;
300 default:
302 return 0;
303 }
304}
305
307 : cd_src_(get_custom_data(src, domain)), cd_dst_(get_custom_data(dst, domain))
308{
309 const AttributeStorage &src_attributes = src.attribute_storage.wrap();
310 AttributeStorage &dst_attributes = dst.attribute_storage.wrap();
311 const int src_domain_size = get_domain_size(src, domain);
312 const int dst_domain_size = get_domain_size(dst, domain);
313 for (const Attribute &src_attr : src_attributes) {
314 if (src_attr.domain() != domain) {
315 continue;
316 }
317 Attribute *dst_attr = dst_attributes.lookup(src_attr.name());
318 if (!dst_attr) {
319 continue;
320 }
321 if (dst_attr->domain() != domain) {
322 continue;
323 }
324 if (dst_attr->data_type() != src_attr.data_type()) {
325 continue;
326 }
327 if (dst_attr->storage_type() != AttrStorageType::Array) {
328 continue;
329 }
330 const CPPType &cpp_type = attribute_type_to_cpp_type(src_attr.data_type());
331 switch (src_attr.storage_type()) {
333 const auto &value = std::get<Attribute::SingleData>(src_attr.data());
334 attrs_src_.append(GVArray::from_single_ref(cpp_type, src_domain_size, value.value));
335 break;
336 }
338 const auto &value = std::get<Attribute::ArrayData>(src_attr.data());
339 attrs_src_.append(GVArray::from_span({cpp_type, value.data, src_domain_size}));
340 break;
341 }
342 }
343 auto &value = std::get<Attribute::ArrayData>(dst_attr->data_for_write());
344 attrs_dst_.append({cpp_type, value.data, dst_domain_size});
345 }
346}
347
348void LegacyMeshInterpolator::copy(const int src_index, const int dst_index, const int count) const
349{
350 if (count == 0) {
351 return;
352 }
353 CustomData_copy_data(&cd_src_, &cd_dst_, src_index, dst_index, count);
354 for (const int i : attrs_src_.index_range()) {
355 const GVArraySpan &src = attrs_src_[i];
356 GMutableSpan dst = attrs_dst_[i];
357 src.type().copy_construct_compressed(src.data(), dst[dst_index], IndexRange(src_index, count));
358 }
359}
360
362 const std::optional<Span<float>> weights,
363 const int dst_index) const
364{
365 CustomData_interp(&cd_src_,
366 &cd_dst_,
367 src_indices.data(),
368 weights ? weights->data() : nullptr,
369 src_indices.size(),
370 dst_index);
371 for (const int attr_index : attrs_src_.index_range()) {
372 attribute_math::to_static_type(attrs_src_[attr_index].type(), [&]<typename T>() {
373 if constexpr (!std::is_void_v<bke::attribute_math::DefaultMixer<T>>) {
374 const Span src = attrs_src_[attr_index].typed<T>();
375 MutableSpan dst = attrs_dst_[attr_index].typed<T>();
376 if (weights) {
377 dst[dst_index] = attribute_math::mix_indices(src, src_indices, *weights);
378 }
379 else {
380 dst[dst_index] = attribute_math::mix_indices(src, src_indices);
381 }
382 }
383 });
384 }
385}
386
387} // namespace blender::bke
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
#define BLI_assert_unreachable()
void copy_construct_compressed(const void *src, void *dst, const IndexMask &mask) const
const CPPType & type() const
const void * data() const
static GVArray from_span(GSpan span)
static GVArray from_single_ref(const CPPType &type, int64_t size, const void *value)
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr MutableSpan()=default
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
bool is_empty() const
VectorData< T, Allocator > release()
Attribute * lookup(StringRef name)
AttrStorageType storage_type() const
void copy(int src_index, int dst_index, int count) const
LegacyMeshInterpolator(const Mesh &src, Mesh &dst, AttrDomain domain)
void mix(Span< int > src_indices, std::optional< Span< float > > weights, int dst_index) const
#define storage(slot, qualifiers)
#define attribute(slot)
#define layer
#define T
T mix_indices(const Span< T > src, const Span< int > indices)
void to_static_type(const CPPType &cpp_type, Fn &&fn)
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
void pointcloud_convert_customdata_to_storage(PointCloud &pointcloud)
static void attribute_legacy_convert_customdata_to_storage(const Map< AttrDomain, CustomDataAndSize > &domains, AttributeStorage &storage)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
static int get_domain_size(const Mesh &mesh, const AttrDomain domain)
void mesh_convert_customdata_to_storage(Mesh &mesh)
static const CustomData & get_custom_data(const Mesh &mesh, const AttrDomain domain)
void curves_convert_customdata_to_storage(CurvesGeometry &curves)
void grease_pencil_convert_customdata_to_storage(GreasePencil &grease_pencil)
const CPPType & attribute_type_to_cpp_type(AttrType type)
void CustomData_copy_data(const CustomData *source, CustomData *dest, int source_index, int dest_index, int count)
void CustomData_interp(const CustomData *source, CustomData *dest, const int *src_indices, const float *weights, int count, int dest_index)
void CustomData_reset(CustomData *data)
BMesh const char void * data
void CustomData_update_typemap(CustomData *data)
struct AttributeStorage attribute_storage
struct CustomData pdata_legacy
struct AttributeStorage attribute_storage