Blender V5.3
bake_items_serialize.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
13#include "BKE_curves.hh"
14#include "BKE_customdata.hh"
16#include "BKE_grease_pencil.hh"
17#include "BKE_instances.hh"
18#include "BKE_lib_id.hh"
19#include "BKE_mesh.hh"
20#include "BKE_pointcloud.hh"
21#include "BKE_volume.hh"
22
23#include "BLI_generic_array.hh"
24#include "BLI_listbase.hh"
26#include "BLI_path_utils.hh"
27#include "BLI_string.hh"
28#include "BLI_string_utf8.hh"
29
30#include "DNA_object_types.h"
31#include "DNA_volume_types.h"
32
33#include "FN_field.hh"
34
35#include "RNA_access.hh"
36#include "RNA_enum_types.hh"
37
40
41#include <fmt/format.h>
42#include <sstream>
43#include <xxhash.h>
44
45#ifdef WITH_OPENVDB
46# include <openvdb/io/Stream.h>
47# include <openvdb/openvdb.h>
48
49# include "BKE_volume_grid.hh"
50#endif
51
52namespace blender::bke::bake {
53
54using namespace io::serialize;
55using DictionaryValuePtr = std::shared_ptr<DictionaryValue>;
56
57static std::optional<SocketValueVariant> deserialize_bake_item(
58 const DictionaryValue &io_item,
59 const BlobReader &blob_reader,
60 const BlobReadSharing &blob_sharing);
61static void serialize_socket_value_variant(const SocketValueVariant &value_variant,
62 BlobWriter &blob_writer,
63 BlobWriteSharing &blob_sharing,
64 DictionaryValue &r_io_item);
65static void serialize_bundle_items(const nodes::Bundle &bundle,
66 ArrayValue &r_io_items,
67 BlobWriter &blob_writer,
68 BlobWriteSharing &blob_sharing);
69[[nodiscard]] static bool deserialize_bundle_items(
70 const io::serialize::ArrayValue &io_bundle_items,
71 const BlobReader &blob_reader,
72 const BlobReadSharing &blob_sharing,
73 nodes::Bundle &r_bundle);
74
75std::shared_ptr<DictionaryValue> BlobSlice::serialize() const
76{
77 auto io_slice = std::make_shared<DictionaryValue>();
78 io_slice->append_str("name", this->name);
79 io_slice->append_int("start", range.start());
80 io_slice->append_int("size", range.size());
81 return io_slice;
82}
83
84std::optional<BlobSlice> BlobSlice::deserialize(const DictionaryValue &io_slice)
85{
86 const std::optional<StringRefNull> name = io_slice.lookup_str("name");
87 const std::optional<int64_t> start = io_slice.lookup_int("start");
88 const std::optional<int64_t> size = io_slice.lookup_int("size");
89 if (!name || !start || !size) {
90 return std::nullopt;
91 }
92
93 return BlobSlice{*name, {*start, *size}};
94}
95
97 const FunctionRef<void(std::ostream &)> fn)
98{
99 std::ostringstream stream{std::ios::binary};
100 fn(stream);
101 std::string data = stream.rdbuf()->str();
102 return this->write(data.data(), data.size());
103}
104
105bool BlobReader::read_as_stream(const BlobSlice &slice, FunctionRef<bool(std::istream &)> fn) const
106{
107 const int64_t size = slice.range.size();
108 std::string buffer;
109 buffer.resize(size);
110 if (!this->read(slice, buffer.data())) {
111 return false;
112 }
113 std::istringstream stream{buffer, std::ios::binary};
114 if (!fn(stream)) {
115 return false;
116 }
117 return true;
118}
119
120DiskBlobReader::DiskBlobReader(std::string blobs_dir) : blobs_dir_(std::move(blobs_dir)) {}
121
122[[nodiscard]] bool DiskBlobReader::read(const BlobSlice &slice, void *r_data) const
123{
124 if (slice.range.is_empty()) {
125 return true;
126 }
127
128 char blob_path[FILE_MAX];
129 BLI_path_join(blob_path, sizeof(blob_path), blobs_dir_.c_str(), slice.name.c_str());
130
131 std::lock_guard lock{mutex_};
132 std::unique_ptr<fstream> &blob_file = open_input_streams_.lookup_or_add_cb_as(blob_path, [&]() {
133 return std::make_unique<fstream>(blob_path, std::ios::in | std::ios::binary);
134 });
135 blob_file->seekg(slice.range.start());
136 blob_file->read(static_cast<char *>(r_data), slice.range.size());
137 if (blob_file->gcount() != slice.range.size()) {
138 return false;
139 }
140 return true;
141}
142
143DiskBlobWriter::DiskBlobWriter(std::string blob_dir, std::string base_name)
144 : blob_dir_(std::move(blob_dir)), base_name_(std::move(base_name))
145{
146 blob_name_ = base_name_ + ".blob";
147}
148
150{
151 if (!blob_stream_.is_open()) {
152 char blob_path[FILE_MAX];
153 BLI_path_join(blob_path, sizeof(blob_path), blob_dir_.c_str(), blob_name_.c_str());
155 blob_stream_.open(blob_path, std::ios::out | std::ios::binary);
156 }
157
158 const int64_t old_offset = current_offset_;
159 blob_stream_.write(static_cast<const char *>(data), size);
160 current_offset_ += size;
161 total_written_size_ += size;
162 return {blob_name_, {old_offset, size}};
163}
164
165static std::string make_independent_file_name(const StringRef base_name,
166 const int file_index,
167 const StringRef extension)
168{
169 return fmt::format("{}_file_{}{}", base_name, file_index, extension);
170}
171
173 const FunctionRef<void(std::ostream &)> fn)
174{
175 BLI_assert(file_extension.startswith("."));
176 independent_file_count_++;
177 const std::string file_name = make_independent_file_name(
178 base_name_, independent_file_count_, file_extension);
179
180 char path[FILE_MAX];
181 BLI_path_join(path, sizeof(path), blob_dir_.c_str(), file_name.c_str());
183 std::fstream stream{path, std::ios::out | std::ios::binary};
184 fn(stream);
185 const int64_t written_bytes_num = stream.tellg();
186 total_written_size_ += written_bytes_num;
187 return {file_name, {0, written_bytes_num}};
188}
189
191{
192 blob_by_name_.add(name, blob);
193}
194
195bool MemoryBlobReader::read(const BlobSlice &slice, void *r_data) const
196{
197 if (slice.range.is_empty()) {
198 return true;
199 }
200 const Span<std::byte> blob_data = blob_by_name_.lookup_default(slice.name, {});
201 if (!blob_data.index_range().contains(slice.range)) {
202 return false;
203 }
204 const void *copy_src = blob_data.slice(slice.range).data();
205 memcpy(r_data, copy_src, slice.range.size());
206 return true;
207}
208
209MemoryBlobWriter::MemoryBlobWriter(std::string base_name) : base_name_(std::move(base_name))
210{
211 blob_name_ = base_name_ + ".blob";
212 stream_by_name_.add(blob_name_, {std::make_unique<std::ostringstream>(std::ios::binary)});
213}
214
216{
217 OutputStream &stream = stream_by_name_.lookup(blob_name_);
218 const int64_t old_offset = stream.offset;
219 stream.stream->write(static_cast<const char *>(data), size);
220 stream.offset += size;
221 total_written_size_ += size;
222 return {blob_name_, IndexRange::from_begin_size(old_offset, size)};
223}
224
226 const FunctionRef<void(std::ostream &)> fn)
227{
228 BLI_assert(file_extension.startswith("."));
229 independent_file_count_++;
230 const std::string name = make_independent_file_name(
231 base_name_, independent_file_count_, file_extension);
232 OutputStream stream{std::make_unique<std::ostringstream>(std::ios::binary)};
233 fn(*stream.stream);
234 const int64_t size = stream.stream->tellp();
235 stream_by_name_.add_new(name, std::move(stream));
236 total_written_size_ += size;
237 return {name, IndexRange(size)};
238}
239
241{
242 for (const ImplicitSharingInfo *sharing_info : stored_by_runtime_.keys()) {
243 sharing_info->remove_weak_user_and_delete_if_last();
244 }
245}
246
248{
249 for (const ImplicitSharingInfoAndData &value : runtime_by_stored_.values()) {
250 if (value.sharing_info) {
251 value.sharing_info->remove_user_and_delete_if_last();
252 }
253 }
254}
255
257 const ImplicitSharingInfo *sharing_info, FunctionRef<DictionaryValuePtr()> write_fn)
258{
259 if (sharing_info == nullptr) {
260 return write_fn();
261 }
262 return stored_by_runtime_.add_or_modify(
263 sharing_info,
264 /* Create new value. */
265 [&](StoredByRuntimeValue *value) {
266 new (value) StoredByRuntimeValue();
267 value->io_data = write_fn();
268 value->sharing_info_version = sharing_info->version();
269 sharing_info->add_weak_user();
270 return value->io_data;
271 },
272 /* Potentially modify existing value. */
273 [&](StoredByRuntimeValue *value) {
274 const int64_t new_version = sharing_info->version();
275 BLI_assert(value->sharing_info_version <= new_version);
276 if (value->sharing_info_version < new_version) {
277 value->io_data = write_fn();
278 value->sharing_info_version = new_version;
279 }
280 return value->io_data;
281 });
282}
283
284std::shared_ptr<io::serialize::DictionaryValue> BlobWriteSharing::write_deduplicated(
285 BlobWriter &writer, const void *data, const int64_t size_in_bytes)
286{
287 const uint64_t content_hash = XXH3_64bits(data, size_in_bytes);
288 const BlobSlice slice = slice_by_content_hash_.lookup_or_add_cb(
289 content_hash, [&]() { return writer.write(data, size_in_bytes); });
290 return slice.serialize();
291}
292
293std::optional<ImplicitSharingInfoAndData> BlobReadSharing::read_shared(
294 const DictionaryValue &io_data,
295 FunctionRef<std::optional<ImplicitSharingInfoAndData>()> read_fn) const
296{
297 std::lock_guard lock{mutex_};
298
300 std::stringstream ss;
301 formatter.serialize(ss, io_data);
302 const std::string key = ss.str();
303
304 if (const ImplicitSharingInfoAndData *shared_data = runtime_by_stored_.lookup_ptr(key)) {
305 shared_data->sharing_info->add_user();
306 return *shared_data;
307 }
308 std::optional<ImplicitSharingInfoAndData> data = read_fn();
309 if (!data) {
310 return std::nullopt;
311 }
312 if (data->sharing_info != nullptr) {
313 data->sharing_info->add_user();
314 runtime_by_stored_.add_new(key, *data);
315 }
316 return data;
317}
318
320{
321 const char *io_name = "unknown";
323 return io_name;
324}
325
327{
328 const char *io_name = "unknown";
330 return io_name;
331}
332
334{
335 const char *io_name = "unknown";
336 RNA_enum_id_from_value(rna_enum_attr_storage_type_items, int(storage_type), &io_name);
337 return io_name;
338}
339
340static std::optional<AttrDomain> get_domain_from_io_name(const StringRefNull io_name)
341{
342 int domain;
344 return std::nullopt;
345 }
346 return AttrDomain(domain);
347}
348
349static std::optional<eCustomDataType> get_data_type_from_io_name(const StringRefNull io_name)
350{
351 int domain;
353 return std::nullopt;
354 }
355 return eCustomDataType(domain);
356}
357
358static std::optional<AttrStorageType> get_storage_type_from_io_name(const StringRefNull io_name)
359{
360 int storage_type;
362 rna_enum_attr_storage_type_items, io_name.c_str(), &storage_type))
363 {
364 return std::nullopt;
365 }
366 return AttrStorageType(storage_type);
367}
368
369static std::shared_ptr<DictionaryValue> write_blob_raw_bytes(BlobWriter &blob_writer,
370 BlobWriteSharing &blob_sharing,
371 const void *data,
372 const int64_t size_in_bytes)
373{
374 return blob_sharing.write_deduplicated(blob_writer, data, size_in_bytes);
375}
376
377[[nodiscard]] static bool read_blob_raw_bytes(const BlobReader &blob_reader,
378 const DictionaryValue &io_data,
379 const int64_t bytes_num,
380 void *r_data)
381{
382 const std::optional<BlobSlice> slice = BlobSlice::deserialize(io_data);
383 if (!slice) {
384 return false;
385 }
386 if (slice->range.size() != bytes_num) {
387 return false;
388 }
389 return blob_reader.read(*slice, r_data);
390}
391
392static std::shared_ptr<DictionaryValue> write_blob_simple_gspan(BlobWriter &blob_writer,
393 BlobWriteSharing &blob_sharing,
394 const GSpan data)
395{
396 return write_blob_raw_bytes(blob_writer, blob_sharing, data.data(), data.size_in_bytes());
397}
398
399[[nodiscard]] static bool read_blob_simple_gspan(const BlobReader &blob_reader,
400 const DictionaryValue &io_data,
401 GMutableSpan r_data)
402{
403 return read_blob_raw_bytes(blob_reader, io_data, r_data.size_in_bytes(), r_data.data());
404}
405
406static std::shared_ptr<DictionaryValue> write_blob_shared_simple_gspan(
407 BlobWriter &blob_writer,
408 BlobWriteSharing &blob_sharing,
409 const GSpan data,
410 const ImplicitSharingInfo *sharing_info)
411{
412 return blob_sharing.write_implicitly_shared(
413 sharing_info, [&]() { return write_blob_simple_gspan(blob_writer, blob_sharing, data); });
414}
415
416[[nodiscard]] static const void *read_blob_shared_simple_gspan(
417 const DictionaryValue &io_data,
418 const BlobReader &blob_reader,
419 const BlobReadSharing &blob_sharing,
420 const CPPType &cpp_type,
421 const int size,
422 const ImplicitSharingInfo **r_sharing_info)
423{
424 const std::optional<ImplicitSharingInfoAndData> sharing_info_and_data = blob_sharing.read_shared(
425 io_data, [&]() -> std::optional<ImplicitSharingInfoAndData> {
426 GArray<> data(cpp_type, size, NoInitialization());
427 if (!read_blob_simple_gspan(blob_reader, io_data, data)) {
428 return std::nullopt;
429 }
430 auto *sharing_info = new ImplicitSharedValue<GArray<>>(std::move(data));
431 return ImplicitSharingInfoAndData{sharing_info, sharing_info->data.data()};
432 });
433 if (!sharing_info_and_data) {
434 *r_sharing_info = nullptr;
435 return nullptr;
436 }
437 *r_sharing_info = sharing_info_and_data->sharing_info;
438 return sharing_info_and_data->data;
439}
440
441template<typename T>
442[[nodiscard]] static bool read_blob_shared_simple_span(const DictionaryValue &io_data,
443 const BlobReader &blob_reader,
444 const BlobReadSharing &blob_sharing,
445 const int size,
446 T **r_data,
447 const ImplicitSharingInfo **r_sharing_info)
448{
449 *r_data = const_cast<T *>(static_cast<const T *>(read_blob_shared_simple_gspan(
450 io_data, blob_reader, blob_sharing, CPPType::get<T>(), size, r_sharing_info)));
451 return *r_data != nullptr;
452}
453
454[[nodiscard]] static bool load_materials(const io::serialize::ArrayValue &io_materials,
455 std::unique_ptr<BakeMaterialsList> &materials)
456{
457 if (io_materials.elements().is_empty()) {
458 return true;
459 }
460 materials = std::make_unique<BakeMaterialsList>();
461 for (const auto &io_material_value : io_materials.elements()) {
462 if (io_material_value->type() == io::serialize::eValueType::Null) {
463 materials->append(std::nullopt);
464 continue;
465 }
466 const auto *io_material = io_material_value->as_dictionary_value();
467 if (!io_material) {
468 return false;
469 }
470 std::optional<std::string> id_name = io_material->lookup_str("name");
471 if (!id_name) {
472 return false;
473 }
474 std::string lib_name = io_material->lookup_str("lib_name").value_or("");
475 materials->append(BakeDataBlockID(ID_MA, std::move(*id_name), std::move(lib_name)));
476 }
477 return true;
478}
479
480[[nodiscard]] static bool load_attributes(const io::serialize::ArrayValue &io_attributes,
481 MutableAttributeAccessor &attributes,
482 const BlobReader &blob_reader,
483 const BlobReadSharing &blob_sharing)
484{
485 for (const auto &io_attribute_value : io_attributes.elements()) {
486 const auto *io_attribute = io_attribute_value->as_dictionary_value();
487 if (!io_attribute) {
488 return false;
489 }
490 const std::optional<StringRefNull> name = io_attribute->lookup_str("name");
491 const std::optional<StringRefNull> domain_str = io_attribute->lookup_str("domain");
492 const std::optional<StringRefNull> type_str = io_attribute->lookup_str("type");
493 const auto *io_data = io_attribute->lookup_dict("data");
494 if (!name || !domain_str || !type_str || !io_data) {
495 return false;
496 }
497
498 const std::optional<AttrDomain> domain = get_domain_from_io_name(*domain_str);
499 const std::optional<eCustomDataType> data_type = get_data_type_from_io_name(*type_str);
500 if (!domain || !data_type) {
501 return false;
502 }
503 const CPPType *cpp_type = custom_data_type_to_cpp_type(*data_type);
504 if (!cpp_type) {
505 return false;
506 }
507
508 const AttrStorageType storage_type = [&]() {
509 if (const std::optional<StringRefNull> str = io_attribute->lookup_str("storage_type")) {
511 }
513 }();
514 switch (storage_type) {
516 const int domain_size = attributes.domain_size(*domain);
517 const ImplicitSharingInfo *attribute_sharing_info;
518 const void *attribute_data = read_blob_shared_simple_gspan(
519 *io_data, blob_reader, blob_sharing, *cpp_type, domain_size, &attribute_sharing_info);
520 if (!attribute_data) {
521 return false;
522 }
523 BLI_SCOPED_DEFER([&]() { attribute_sharing_info->remove_user_and_delete_if_last(); });
524
525 if (attributes.contains(*name)) {
526 /* If the attribute exists already, copy the values over to the existing array. */
528 *name, *domain, *custom_data_type_to_attr_type(*data_type));
529 if (!attribute) {
530 return false;
531 }
532 cpp_type->copy_assign_n(attribute_data, attribute.span.data(), domain_size);
533 attribute.finish();
534 }
535 else {
536 /* Add a new attribute that shares the data. */
537 if (!attributes.add(*name,
538 *domain,
540 AttributeInitShared(attribute_data, *attribute_sharing_info)))
541 {
542 return false;
543 }
544 }
545 break;
546 }
548 const ImplicitSharingInfo *sharing_info;
549 const void *value = read_blob_shared_simple_gspan(
550 *io_data, blob_reader, blob_sharing, *cpp_type, 1, &sharing_info);
551 if (!value) {
552 return false;
553 }
554 BLI_SCOPED_DEFER([&]() { sharing_info->remove_user_and_delete_if_last(); });
555
556 const AttributeInitValue init(GPointer(*cpp_type, value));
557 if (attributes.contains(*name)) {
558 if (!attributes.assign_data(*name, init)) {
559 return false;
560 }
561 }
562 else {
563 if (!attributes.add(*name, *domain, *custom_data_type_to_attr_type(*data_type), init)) {
564 return false;
565 }
566 }
567 break;
568 }
569 }
570 }
571
572 return true;
573}
574
576 const BlobReader &blob_reader,
577 const BlobReadSharing &blob_sharing)
578{
579 const DictionaryValue *io_pointcloud = io_geometry.lookup_dict("pointcloud");
580 if (!io_pointcloud) {
581 return nullptr;
582 }
583 const io::serialize::ArrayValue *io_attributes = io_pointcloud->lookup_array("attributes");
584 if (!io_attributes) {
585 return nullptr;
586 }
587 const PointCloudType type = PointCloudType(
588 io_pointcloud->lookup_int("type").value_or(int(PointCloudType::Points)));
589 const int points_num = io_pointcloud->lookup_int("num_points").value_or(0);
590 PointCloud *pointcloud = bke::pointcloud_new_no_attributes(type, points_num);
591
592 auto cancel = [&]() {
593 BKE_id_free(nullptr, pointcloud);
594 return nullptr;
595 };
596
597 MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
598 if (!load_attributes(*io_attributes, attributes, blob_reader, blob_sharing)) {
599 return cancel();
600 }
601
602 if (const io::serialize::ArrayValue *io_materials = io_pointcloud->lookup_array("materials")) {
603 if (!load_materials(*io_materials, pointcloud->runtime->bake_materials)) {
604 return cancel();
605 }
606 }
607 return pointcloud;
608}
609
610static std::optional<CurvesGeometry> try_load_curves_geometry(const DictionaryValue &io_curves,
611 const BlobReader &blob_reader,
612 const BlobReadSharing &blob_sharing)
613{
614 const io::serialize::ArrayValue *io_attributes = io_curves.lookup_array("attributes");
615 if (!io_attributes) {
616 return std::nullopt;
617 }
618
620 curves.attribute_storage.wrap().remove("position");
621 curves.point_num = io_curves.lookup_int("num_points").value_or(0);
622 curves.curve_num = io_curves.lookup_int("num_curves").value_or(0);
623
624 if (curves.curves_num() > 0) {
625 const auto *io_curve_offsets = io_curves.lookup_dict("curve_offsets");
626 if (!io_curve_offsets) {
627 return std::nullopt;
628 }
629 if (!read_blob_shared_simple_span(*io_curve_offsets,
630 blob_reader,
631 blob_sharing,
632 curves.curves_num() + 1,
633 &curves.curve_offsets,
634 &curves.runtime->curve_offsets_sharing_info))
635 {
636 return std::nullopt;
637 }
638 }
639
640 MutableAttributeAccessor attributes = curves.attributes_for_write();
641 if (!load_attributes(*io_attributes, attributes, blob_reader, blob_sharing)) {
642 return std::nullopt;
643 }
644
645 if (const io::serialize::ArrayValue *io_materials = io_curves.lookup_array("materials")) {
646 if (!load_materials(*io_materials, curves.runtime->bake_materials)) {
647 return std::nullopt;
648 }
649 }
650
651 curves.update_curve_types();
652 return curves;
653}
654
655static Curves *try_load_curves(const DictionaryValue &io_geometry,
656 const BlobReader &blob_reader,
657 const BlobReadSharing &blob_sharing)
658{
659 const DictionaryValue *io_curves = io_geometry.lookup_dict("curves");
660 if (!io_curves) {
661 return nullptr;
662 }
663
664 const io::serialize::ArrayValue *io_attributes = io_curves->lookup_array("attributes");
665 if (!io_attributes) {
666 return nullptr;
667 }
668
669 std::optional<CurvesGeometry> curves_opt = try_load_curves_geometry(
670 *io_curves, blob_reader, blob_sharing);
671 if (!curves_opt) {
672 return nullptr;
673 }
674
675 Curves *curves_id = curves_new_nomain(std::move(*curves_opt));
676 CurvesGeometry &curves = curves_id->geometry.wrap();
677
678 auto cancel = [&]() {
679 BKE_id_free(nullptr, curves_id);
680 return nullptr;
681 };
682
683 if (const io::serialize::ArrayValue *io_materials = io_curves->lookup_array("materials")) {
684 if (!load_materials(*io_materials, curves.runtime->bake_materials)) {
685 return cancel();
686 }
687 }
688
689 return curves_id;
690}
691
693 const BlobReader &blob_reader,
694 const BlobReadSharing &blob_sharing)
695{
696 const DictionaryValue *io_grease_pencil = io_geometry.lookup_dict("grease_pencil");
697 if (!io_grease_pencil) {
698 return nullptr;
699 }
700
701 const io::serialize::ArrayValue *io_layers = io_grease_pencil->lookup_array("layers");
702 if (!io_layers) {
703 return nullptr;
704 }
705
706 const io::serialize::ArrayValue *io_layer_attributes = io_grease_pencil->lookup_array(
707 "layer_attributes");
708 if (!io_layer_attributes) {
709 return nullptr;
710 }
711
713 auto cancel = [&]() {
714 BKE_id_free(nullptr, grease_pencil);
715 return nullptr;
716 };
717
718 const int layers_num = io_layers->elements().size();
719 grease_pencil->add_layers_with_empty_drawings_for_eval(layers_num);
720
721 for (const int layer_i : io_layers->elements().index_range()) {
722 const auto &io_layer_value = io_layers->elements()[layer_i];
723 const io::serialize::DictionaryValue *io_layer = io_layer_value->as_dictionary_value();
724 if (!io_layer) {
725 return cancel();
726 }
727 const io::serialize::DictionaryValue *io_strokes = io_layer->lookup_dict("strokes");
728 if (!io_strokes) {
729 return cancel();
730 }
731 const std::optional<std::string> layer_name = io_layer->lookup_str("name");
732 if (!layer_name) {
733 return cancel();
734 }
735 greasepencil::Layer &layer = grease_pencil->layer(layer_i);
736 layer.set_name(*layer_name);
737 std::optional<CurvesGeometry> curves_opt = try_load_curves_geometry(
738 *io_strokes, blob_reader, blob_sharing);
739 if (!curves_opt) {
740 return cancel();
741 }
742 greasepencil::Drawing &drawing = *grease_pencil->get_eval_drawing(layer);
743 drawing.strokes_for_write() = std::move(*curves_opt);
744 }
745
746 MutableAttributeAccessor attributes = grease_pencil->attributes_for_write();
747 if (!load_attributes(*io_layer_attributes, attributes, blob_reader, blob_sharing)) {
748 return cancel();
749 }
750
751 const DictionaryValue *io_layer_opacities = io_grease_pencil->lookup_dict("opacities");
752 Array<float> layer_opacities(layers_num);
753 if (!io_layer_opacities ||
754 !read_blob_simple_gspan(blob_reader, *io_layer_opacities, layer_opacities.as_mutable_span()))
755 {
756 return cancel();
757 }
758
759 const DictionaryValue *io_layer_blend_modes = io_grease_pencil->lookup_dict("blend_modes");
760 Array<int8_t> layer_blend_modes(layers_num);
761 if (!io_layer_opacities || !read_blob_simple_gspan(blob_reader,
762 *io_layer_blend_modes,
763 layer_blend_modes.as_mutable_span()))
764 {
765 return cancel();
766 }
767
768 const DictionaryValue *io_layer_transforms = io_grease_pencil->lookup_dict("transforms");
769 Array<float4x4> layer_transforms(layers_num);
770 if (!io_layer_transforms || !read_blob_simple_gspan(blob_reader,
771 *io_layer_transforms,
772 layer_transforms.as_mutable_span()))
773 {
774 return cancel();
775 }
776
777 for (const int layer_i : IndexRange(layers_num)) {
778 greasepencil::Layer &layer = grease_pencil->layer(layer_i);
779 layer.opacity = layer_opacities[layer_i];
780 layer.blend_mode = GreasePencilLayerBlendMode(layer_blend_modes[layer_i]);
781 layer.set_local_transform(layer_transforms[layer_i]);
782 }
783
784 if (const io::serialize::ArrayValue *io_materials = io_grease_pencil->lookup_array("materials"))
785 {
786 if (!load_materials(*io_materials, grease_pencil->runtime->bake_materials)) {
787 return cancel();
788 }
789 }
790
791 return grease_pencil;
792}
793
794static void load_texspace(const DictionaryValue *io_mesh, Mesh *mesh)
795{
796
797 const auto reset_texspace_default = [](Mesh *mesh) {
798 std::ranges::fill(mesh->texspace_location, 0.0f);
799 std::ranges::fill(mesh->texspace_size, 1.0f);
800 };
801
802 const auto fill_texspace_values = [](const io::serialize::ArrayValue *io_array,
803 float r_texspace_property[3]) -> bool {
804 size_t i = 0;
805 for (const std::shared_ptr<Value> &value : io_array->elements()) {
806 if (value->type() != io::serialize::eValueType::Double) {
807 return false;
808 }
809 r_texspace_property[i++] = float(value->as_double_value()->value());
810 }
811 return true;
812 };
813
814 mesh->texspace_flag = io_mesh->lookup_bool("texture_space_flag").value_or(false);
815
816 const io::serialize::ArrayValue *io_texloc_array = io_mesh->lookup_array(
817 "texture_space_location");
818 const io::serialize::ArrayValue *io_texsize_array = io_mesh->lookup_array("texture_space_size");
819
820 if (!io_texloc_array || !io_texsize_array) {
821 reset_texspace_default(mesh);
822 return;
823 }
824
825 BLI_assert(io_texloc_array->elements().size() == 3);
826 BLI_assert(io_texsize_array->elements().size() == 3);
827
828 if (!fill_texspace_values(io_texloc_array, mesh->texspace_location) ||
829 !fill_texspace_values(io_texsize_array, mesh->texspace_size))
830 {
831 reset_texspace_default(mesh);
832 return;
833 }
834}
835
836static Mesh *try_load_mesh(const DictionaryValue &io_geometry,
837 const BlobReader &blob_reader,
838 const BlobReadSharing &blob_sharing)
839{
840 const DictionaryValue *io_mesh = io_geometry.lookup_dict("mesh");
841 if (!io_mesh) {
842 return nullptr;
843 }
844
845 const io::serialize::ArrayValue *io_attributes = io_mesh->lookup_array("attributes");
846 if (!io_attributes) {
847 return nullptr;
848 }
849
851 mesh->verts_num = io_mesh->lookup_int("num_vertices").value_or(0);
852 mesh->edges_num = io_mesh->lookup_int("num_edges").value_or(0);
853 mesh->faces_num = io_mesh->lookup_int("num_polygons").value_or(0);
854 mesh->corners_num = io_mesh->lookup_int("num_corners").value_or(0);
855
856 auto cancel = [&]() {
857 BKE_id_free(nullptr, mesh);
858 return nullptr;
859 };
860
861 if (mesh->faces_num > 0) {
862 const auto *io_poly_offsets = io_mesh->lookup_dict("poly_offsets");
863 if (!io_poly_offsets) {
864 return cancel();
865 }
866 if (!read_blob_shared_simple_span(*io_poly_offsets,
867 blob_reader,
868 blob_sharing,
869 mesh->faces_num + 1,
870 &mesh->face_offset_indices,
871 &mesh->runtime->face_offsets_sharing_info))
872 {
873 return cancel();
874 }
875 }
876
877 /* Create the vertex group name list, then later on when processing generic attributes, these
878 * names will be stored as vertex groups. */
879 if (const auto *io_attributes = io_mesh->lookup_array("vertex_group_names")) {
880 for (const std::shared_ptr<Value> &value : io_attributes->elements()) {
881 if (value->type() != io::serialize::eValueType::String) {
882 return cancel();
883 }
884 bDeformGroup *defgroup = MEM_new<bDeformGroup>(__func__);
885 STRNCPY_UTF8(defgroup->name, value->as_string_value()->value().c_str());
886 BLI_addtail(&mesh->vertex_group_names, defgroup);
887 }
888 }
889
890 MutableAttributeAccessor attributes = mesh->attributes_for_write();
891 if (!load_attributes(*io_attributes, attributes, blob_reader, blob_sharing)) {
892 return cancel();
893 }
894
895 if (const io::serialize::ArrayValue *io_materials = io_mesh->lookup_array("materials")) {
896 if (!load_materials(*io_materials, mesh->runtime->bake_materials)) {
897 return cancel();
898 }
899 }
900
901 load_texspace(io_mesh, mesh);
902
903 if (const std::optional<StringRefNull> default_uv_map_name = io_mesh->lookup_str(
904 "default_uv_map_name"))
905 {
906 mesh->uv_maps_default_set(*default_uv_map_name);
907 }
908 if (const std::optional<StringRefNull> default_color_attribute = io_mesh->lookup_str(
909 "default_color_name"))
910 {
911 mesh->default_color_attribute = BLI_strdupn(default_color_attribute->data(),
912 default_color_attribute->size());
913 }
914
915 return mesh;
916}
917
918static GeometrySet load_geometry(const DictionaryValue &io_geometry,
919 const BlobReader &blob_reader,
920 const BlobReadSharing &blob_sharing);
921
922static std::unique_ptr<Instances> try_load_instances(const DictionaryValue &io_geometry,
923 const BlobReader &blob_reader,
924 const BlobReadSharing &blob_sharing)
925{
926 const DictionaryValue *io_instances = io_geometry.lookup_dict("instances");
927 if (!io_instances) {
928 return nullptr;
929 }
930 const int num_instances = io_instances->lookup_int("num_instances").value_or(0);
931 if (num_instances == 0) {
932 return nullptr;
933 }
934 const io::serialize::ArrayValue *io_attributes = io_instances->lookup_array("attributes");
935 if (!io_attributes) {
936 return nullptr;
937 }
938 const io::serialize::ArrayValue *io_references = io_instances->lookup_array("references");
939 if (!io_references) {
940 return nullptr;
941 }
942
943 std::unique_ptr<Instances> instances = std::make_unique<Instances>(num_instances);
944
945 for (const auto &io_reference_value : io_references->elements()) {
946 const DictionaryValue *io_reference = io_reference_value->as_dictionary_value();
947 GeometrySet reference_geometry;
948 if (io_reference) {
949 reference_geometry = load_geometry(*io_reference, blob_reader, blob_sharing);
950 }
951 instances->add_new_reference(std::move(reference_geometry));
952 }
953
954 MutableAttributeAccessor attributes = instances->attributes_for_write();
955 if (!load_attributes(*io_attributes, attributes, blob_reader, blob_sharing)) {
956 return {};
957 }
958
959 if (!attributes.contains(".reference_index")) {
960 /* Try reading the reference index attribute from the old bake format from before it was an
961 * attribute. */
962 const auto *io_handles = io_instances->lookup_dict("handles");
963 if (!io_handles) {
964 return {};
965 }
967 blob_reader, *io_handles, instances->reference_handles_for_write()))
968 {
969 return {};
970 }
971 }
972
973 if (!attributes.contains("instance_transform")) {
974 /* Try reading the transform attribute from the old bake format from before it was an
975 * attribute. */
976 const auto *io_handles = io_instances->lookup_dict("transforms");
977 if (!io_handles) {
978 return {};
979 }
980 if (!read_blob_simple_gspan(blob_reader, *io_handles, instances->transforms_for_write())) {
981 return {};
982 }
983 }
984
985 return instances;
986}
987
988#ifdef WITH_OPENVDB
989static Volume *try_load_volume(const DictionaryValue &io_geometry, const BlobReader &blob_reader)
990{
991 const DictionaryValue *io_volume = io_geometry.lookup_dict("volume");
992 if (!io_volume) {
993 return nullptr;
994 }
995 const auto *io_vdb = io_volume->lookup_dict("vdb");
996 if (!io_vdb) {
997 return nullptr;
998 }
999 openvdb::GridPtrVecPtr vdb_grids;
1000 if (std::optional<BlobSlice> vdb_slice = BlobSlice::deserialize(*io_vdb)) {
1001 if (!blob_reader.read_as_stream(*vdb_slice,
1002 [&](std::istream &stream) {
1003 try {
1004 openvdb::io::Stream vdb_stream{stream};
1005 vdb_grids = vdb_stream.getGrids();
1006 return true;
1007 }
1008 catch (...) {
1009 return false;
1010 }
1011 }))
1012 {
1013 return nullptr;
1014 }
1015 }
1016 Volume *volume = BKE_id_new_nomain<Volume>(nullptr);
1017 auto cancel = [&]() {
1018 BKE_id_free(nullptr, volume);
1019 return nullptr;
1020 };
1021
1022 for (openvdb::GridBase::Ptr &vdb_grid : *vdb_grids) {
1023 if (vdb_grid) {
1024 bke::GVolumeGrid grid{std::move(vdb_grid)};
1025 BKE_volume_grid_add(volume, *grid.release());
1026 }
1027 }
1028 if (const io::serialize::ArrayValue *io_materials = io_volume->lookup_array("materials")) {
1029 if (!load_materials(*io_materials, volume->runtime->bake_materials)) {
1030 return cancel();
1031 }
1032 }
1033 return volume;
1034}
1035#endif
1036
1038 const BlobReader &blob_reader,
1039 const BlobReadSharing &blob_sharing)
1040{
1041 const DictionaryValue *io_bundle = io_geometry.lookup_dict("bundle");
1042 if (!io_bundle) {
1043 return {};
1044 }
1045 const ArrayValue *io_bundle_items = io_bundle->lookup_array("items");
1046 if (!io_bundle_items) {
1047 return {};
1048 }
1050 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
1051 if (!deserialize_bundle_items(*io_bundle_items, blob_reader, blob_sharing, bundle)) {
1052 return {};
1053 }
1054 return bundle_ptr;
1055}
1056
1058 const BlobReader &blob_reader,
1059 const BlobReadSharing &blob_sharing)
1060{
1062 geometry.replace_mesh(try_load_mesh(io_geometry, blob_reader, blob_sharing));
1063 geometry.replace_pointcloud(try_load_pointcloud(io_geometry, blob_reader, blob_sharing));
1064 geometry.replace_curves(try_load_curves(io_geometry, blob_reader, blob_sharing));
1065 geometry.replace_grease_pencil(try_load_grease_pencil(io_geometry, blob_reader, blob_sharing));
1066 geometry.replace_instances(try_load_instances(io_geometry, blob_reader, blob_sharing).release());
1067#ifdef WITH_OPENVDB
1068 geometry.replace_volume(try_load_volume(io_geometry, blob_reader));
1069#endif
1070 geometry.bundle_ptr() = try_load_geometry_bundle(io_geometry, blob_reader, blob_sharing);
1071 return geometry;
1072}
1073
1074static std::shared_ptr<io::serialize::ArrayValue> serialize_materials(
1075 const std::unique_ptr<BakeMaterialsList> &materials)
1076{
1077 auto io_materials = std::make_shared<io::serialize::ArrayValue>();
1078 if (!materials) {
1079 return io_materials;
1080 }
1081 for (const std::optional<BakeDataBlockID> &material : *materials) {
1082 if (material) {
1083 auto io_material = io_materials->append_dict();
1084 io_material->append_str("name", material->id_name);
1085 if (!material->lib_name.empty()) {
1086 io_material->append_str("lib_name", material->lib_name);
1087 }
1088 }
1089 else {
1090 io_materials->append_null();
1091 }
1092 }
1093 return io_materials;
1094}
1095
1096static std::shared_ptr<io::serialize::ArrayValue> serialize_attributes(
1097 const AttributeAccessor &attributes,
1098 BlobWriter &blob_writer,
1099 BlobWriteSharing &blob_sharing,
1100 const Set<std::string> &attributes_to_ignore)
1101{
1102 auto io_attributes = std::make_shared<io::serialize::ArrayValue>();
1103 attributes.foreach_attribute([&](const AttributeIter &iter) {
1105 if (attributes_to_ignore.contains_as(iter.name)) {
1106 return;
1107 }
1108
1109 auto io_attribute = io_attributes->append_dict();
1110
1111 io_attribute->append_str("name", iter.name);
1112
1113 const StringRefNull domain_name = get_domain_io_name(iter.domain);
1114 io_attribute->append_str("domain", domain_name);
1115
1116 const StringRefNull type_name = get_data_type_io_name(
1118 io_attribute->append_str("type", type_name);
1119
1120 const GAttributeReader attribute = iter.get();
1121 const CommonVArrayInfo info = attribute.varray.common_info();
1123 io_attribute->append_str("storage_type", get_storage_type_io_name(AttrStorageType::Single));
1124 const GSpan attribute_span(attribute.varray.type(), info.data, 1);
1125 io_attribute->append("data",
1127 blob_writer, blob_sharing, attribute_span, attribute.sharing_info));
1128 return;
1129 }
1130 /* Save "storage_type" with a default of ARRAY; don't store it in this case. */
1131 const GVArraySpan attribute_span(attribute.varray);
1132 io_attribute->append("data",
1134 blob_sharing,
1135 attribute_span,
1137 attribute.sharing_info :
1138 nullptr));
1139 });
1140 return io_attributes;
1141}
1142
1144 const CurvesGeometry &curves,
1145 BlobWriter &blob_writer,
1146 BlobWriteSharing &blob_sharing)
1147{
1148 io_curves.append_int("num_points", curves.point_num);
1149 io_curves.append_int("num_curves", curves.curve_num);
1150
1151 if (curves.curve_num > 0) {
1152 io_curves.append("curve_offsets",
1154 blob_sharing,
1155 curves.offsets(),
1156 curves.runtime->curve_offsets_sharing_info));
1157 }
1158
1159 auto io_attributes = serialize_attributes(curves.attributes(), blob_writer, blob_sharing, {});
1160 io_curves.append("attributes", io_attributes);
1161}
1162
1163static void serialize_texture_space(const Mesh &mesh, std::shared_ptr<DictionaryValue> io_mesh)
1164{
1165 io_mesh->append_bool("texture_space_flag", mesh.texspace_flag);
1166
1167 std::shared_ptr<ArrayValue> texspace_location_array = io_mesh->append_array(
1168 "texture_space_location");
1169 for (float value : Span(mesh.texspace_location, 3)) {
1170 texspace_location_array->append_double(value);
1171 }
1172
1173 std::shared_ptr<ArrayValue> texspace_size_array = io_mesh->append_array("texture_space_size");
1174 for (float value : Span(mesh.texspace_size, 3)) {
1175 texspace_size_array->append_double(value);
1176 }
1177}
1178
1179static std::shared_ptr<DictionaryValue> serialize_geometry_set(const GeometrySet &geometry,
1180 BlobWriter &blob_writer,
1181 BlobWriteSharing &blob_sharing)
1182{
1183 auto io_geometry = std::make_shared<DictionaryValue>();
1184 if (geometry.has_mesh()) {
1185 const Mesh &mesh = *geometry.get_mesh();
1186 auto io_mesh = io_geometry->append_dict("mesh");
1187
1188 io_mesh->append_int("num_vertices", mesh.verts_num);
1189 io_mesh->append_int("num_edges", mesh.edges_num);
1190 io_mesh->append_int("num_polygons", mesh.faces_num);
1191 io_mesh->append_int("num_corners", mesh.corners_num);
1192
1193 if (mesh.faces_num > 0) {
1194 io_mesh->append("poly_offsets",
1196 blob_sharing,
1197 mesh.face_offsets(),
1198 mesh.runtime->face_offsets_sharing_info));
1199 }
1200
1201 auto io_materials = serialize_materials(mesh.runtime->bake_materials);
1202 io_mesh->append("materials", io_materials);
1203
1204 if (!mesh.vertex_group_names.is_empty()) {
1205 auto io_vertex_group_names = io_mesh->append_array("vertex_group_names");
1206 for (bDeformGroup &defgroup : mesh.vertex_group_names) {
1207 io_vertex_group_names->append_str(defgroup.name);
1208 }
1209 }
1210
1211 serialize_texture_space(mesh, io_mesh);
1212
1213 const StringRef default_uv_map_name = mesh.default_uv_map_name();
1214 if (!default_uv_map_name.is_empty()) {
1215 io_mesh->append_str("default_uv_map_name", default_uv_map_name);
1216 }
1217 const StringRef default_color_attribute = mesh.default_color_attribute;
1218 if (!default_color_attribute.is_empty()) {
1219 io_mesh->append_str("default_color_name", default_color_attribute);
1220 }
1221
1222 auto io_attributes = serialize_attributes(mesh.attributes(), blob_writer, blob_sharing, {});
1223 io_mesh->append("attributes", io_attributes);
1224 }
1225 if (geometry.has_pointcloud()) {
1226 const PointCloud &pointcloud = *geometry.get_pointcloud();
1227 auto io_pointcloud = io_geometry->append_dict("pointcloud");
1228
1229 io_pointcloud->append_int("type", int(pointcloud.type));
1230 io_pointcloud->append_int("num_points", pointcloud.totpoint);
1231
1232 auto io_materials = serialize_materials(pointcloud.runtime->bake_materials);
1233 io_pointcloud->append("materials", io_materials);
1234
1235 auto io_attributes = serialize_attributes(
1236 pointcloud.attributes(), blob_writer, blob_sharing, {});
1237 io_pointcloud->append("attributes", io_attributes);
1238 }
1239 if (geometry.has_curves()) {
1240 const Curves &curves_id = *geometry.get_curves();
1241 const CurvesGeometry &curves = curves_id.geometry.wrap();
1242
1243 auto io_curves = io_geometry->append_dict("curves");
1244
1245 serialize_curves_geometry(*io_curves, curves, blob_writer, blob_sharing);
1246
1247 auto io_materials = serialize_materials(curves.runtime->bake_materials);
1248 io_curves->append("materials", io_materials);
1249 }
1250 if (geometry.has_grease_pencil()) {
1251 const GreasePencil &grease_pencil = *geometry.get_grease_pencil();
1252 auto io_grease_pencil = io_geometry->append_dict("grease_pencil");
1253 auto io_layers = io_grease_pencil->append_array("layers");
1254
1255 Vector<float> layer_opacities;
1256 Vector<int8_t> layer_blend_modes;
1257 Vector<float4x4> layer_transforms;
1258 for (const greasepencil::Layer *layer : grease_pencil.layers()) {
1259 auto io_layer = io_layers->append_dict();
1260 io_layer->append_str("name", layer->name());
1261 auto io_strokes = io_layer->append_dict("strokes");
1262 const greasepencil::Drawing *drawing = grease_pencil.get_eval_drawing(*layer);
1263 if (drawing) {
1264 serialize_curves_geometry(*io_strokes, drawing->strokes(), blob_writer, blob_sharing);
1265 }
1266 else {
1267 serialize_curves_geometry(*io_strokes, CurvesGeometry(), blob_writer, blob_sharing);
1268 }
1269
1270 layer_opacities.append(layer->opacity);
1271 layer_blend_modes.append(int8_t(layer->blend_mode));
1272 layer_transforms.append(layer->local_transform());
1273 }
1274
1275 io_grease_pencil->append(
1276 "opacities",
1277 write_blob_simple_gspan(blob_writer, blob_sharing, layer_opacities.as_span()));
1278 io_grease_pencil->append(
1279 "blend_modes",
1280 write_blob_simple_gspan(blob_writer, blob_sharing, layer_blend_modes.as_span()));
1281 io_grease_pencil->append(
1282 "transforms",
1283 write_blob_simple_gspan(blob_writer, blob_sharing, layer_transforms.as_span()));
1284
1285 auto io_layer_attributes = serialize_attributes(
1286 grease_pencil.attributes(), blob_writer, blob_sharing, {});
1287 io_grease_pencil->append("layer_attributes", io_layer_attributes);
1288
1289 auto io_materials = serialize_materials(grease_pencil.runtime->bake_materials);
1290 io_grease_pencil->append("materials", io_materials);
1291 }
1292#ifdef WITH_OPENVDB
1293 if (geometry.has_volume()) {
1294 const Volume &volume = *geometry.get_volume();
1295 const int grids_num = BKE_volume_num_grids(&volume);
1296
1297 auto io_volume = io_geometry->append_dict("volume");
1298 auto io_vdb = blob_writer
1299 .write_as_stream(".vdb",
1300 [&](std::ostream &stream) {
1301 openvdb::GridCPtrVec vdb_grids;
1303 for (const int i : IndexRange(grids_num)) {
1304 const bke::VolumeGridData *grid = BKE_volume_grid_get(
1305 &volume, i);
1306 tree_tokens.append_as();
1307 vdb_grids.push_back(grid->grid_ptr(tree_tokens.last()));
1308 }
1309
1310 openvdb::io::Stream vdb_stream(stream);
1311 vdb_stream.write(vdb_grids);
1312 })
1313 .serialize();
1314 io_volume->append("vdb", std::move(io_vdb));
1315
1316 auto io_materials = serialize_materials(volume.runtime->bake_materials);
1317 io_volume->append("materials", io_materials);
1318 }
1319#endif
1320 if (geometry.has_instances()) {
1321 const Instances &instances = *geometry.get_instances();
1322 auto io_instances = io_geometry->append_dict("instances");
1323
1324 io_instances->append_int("num_instances", instances.instances_num());
1325
1326 auto io_references = io_instances->append_array("references");
1327 for (const InstanceReference &reference : instances.references()) {
1328 if (reference.type() == InstanceReference::Type::GeometrySet) {
1329 const GeometrySet &geometry = reference.geometry_set();
1330 io_references->append(serialize_geometry_set(geometry, blob_writer, blob_sharing));
1331 }
1332 else {
1333 /* TODO: Support serializing object and collection references. */
1334 io_references->append(serialize_geometry_set({}, blob_writer, blob_sharing));
1335 }
1336 }
1337
1338 auto io_attributes = serialize_attributes(
1339 instances.attributes(), blob_writer, blob_sharing, {});
1340 io_instances->append("attributes", io_attributes);
1341 }
1342 if (geometry.has_bundle()) {
1343 const nodes::BundlePtr &bundle = geometry.bundle_ptr();
1344 auto io_bundle = io_geometry->append_dict("bundle");
1345 auto io_bundle_items = io_bundle->append_array("items");
1346 serialize_bundle_items(*bundle, *io_bundle_items, blob_writer, blob_sharing);
1347 }
1348 return io_geometry;
1349}
1350
1351static std::shared_ptr<io::serialize::ArrayValue> serialize_float_array(const Span<float> values)
1352{
1353 auto io_value = std::make_shared<io::serialize::ArrayValue>();
1354 for (const float value : values) {
1355 io_value->append_double(value);
1356 }
1357 return io_value;
1358}
1359
1360static std::shared_ptr<io::serialize::ArrayValue> serialize_int_array(const Span<int> values)
1361{
1362 auto io_value = std::make_shared<io::serialize::ArrayValue>();
1363 for (const int value : values) {
1364 io_value->append_int(value);
1365 }
1366 return io_value;
1367}
1368
1369static std::shared_ptr<io::serialize::Value> serialize_primitive_value(
1370 const eCustomDataType data_type, const void *value_ptr)
1371{
1372 switch (data_type) {
1373 case CD_PROP_FLOAT: {
1374 const float value = *static_cast<const float *>(value_ptr);
1375 return std::make_shared<io::serialize::DoubleValue>(value);
1376 }
1377 case CD_PROP_FLOAT2: {
1378 const float2 value = *static_cast<const float2 *>(value_ptr);
1379 return serialize_float_array({&value.x, 2});
1380 }
1381 case CD_PROP_FLOAT3: {
1382 const float3 value = *static_cast<const float3 *>(value_ptr);
1383 return serialize_float_array({&value.x, 3});
1384 }
1385 case CD_PROP_FLOAT4: {
1386 const float4 value = *static_cast<const float4 *>(value_ptr);
1387 return serialize_float_array({&value.x, 4});
1388 }
1389 case CD_PROP_BOOL: {
1390 const bool value = *static_cast<const bool *>(value_ptr);
1391 return std::make_shared<io::serialize::BooleanValue>(value);
1392 }
1393 case CD_PROP_INT32: {
1394 const int value = *static_cast<const int *>(value_ptr);
1395 return std::make_shared<io::serialize::IntValue>(value);
1396 }
1397 case CD_PROP_INT16_2D: {
1398 const int2 value = int2(*static_cast<const short2 *>(value_ptr));
1399 return serialize_int_array({&value.x, 2});
1400 }
1401 case CD_PROP_INT32_2D: {
1402 const int2 value = *static_cast<const int2 *>(value_ptr);
1403 return serialize_int_array({&value.x, 2});
1404 }
1405 case CD_PROP_BYTE_COLOR: {
1406 const ColorGeometry4b value = *static_cast<const ColorGeometry4b *>(value_ptr);
1407 const int4 value_int{&value.r};
1408 return serialize_int_array({&value_int.x, 4});
1409 }
1410 case CD_PROP_COLOR: {
1411 const ColorGeometry4f value = *static_cast<const ColorGeometry4f *>(value_ptr);
1412 return serialize_float_array({&value.r, 4});
1413 }
1414 case CD_PROP_QUATERNION: {
1415 const math::Quaternion value = *static_cast<const math::Quaternion *>(value_ptr);
1416 return serialize_float_array({&value.w, 4});
1417 }
1418 case CD_PROP_FLOAT4X4: {
1419 const float4x4 value = *static_cast<const float4x4 *>(value_ptr);
1421 }
1422 default:
1423 break;
1424 }
1426 return {};
1427}
1428
1429template<typename T>
1430[[nodiscard]] static bool deserialize_typed_array(
1431 const io::serialize::Value &io_value,
1432 FunctionRef<std::optional<T>(const io::serialize::Value &io_element)> fn,
1433 MutableSpan<T> r_values)
1434{
1435 const io::serialize::ArrayValue *io_array = io_value.as_array_value();
1436 if (!io_array) {
1437 return false;
1438 }
1439 if (io_array->elements().size() != r_values.size()) {
1440 return false;
1441 }
1442 for (const int i : r_values.index_range()) {
1443 const io::serialize::Value &io_element = *io_array->elements()[i];
1444 std::optional<T> element = fn(io_element);
1445 if (!element) {
1446 return false;
1447 }
1448 r_values[i] = std::move(*element);
1449 }
1450 return true;
1451}
1452
1453template<typename T> static std::optional<T> deserialize_int(const io::serialize::Value &io_value)
1454{
1455 const io::serialize::IntValue *io_int = io_value.as_int_value();
1456 if (!io_int) {
1457 return std::nullopt;
1458 }
1459 const int64_t value = io_int->value();
1460 if (value < std::numeric_limits<T>::lowest()) {
1461 return std::nullopt;
1462 }
1463 if (value > std::numeric_limits<T>::max()) {
1464 return std::nullopt;
1465 }
1466 return value;
1467}
1468
1469static std::optional<float> deserialize_float(const io::serialize::Value &io_value)
1470{
1471 if (const io::serialize::DoubleValue *io_double = io_value.as_double_value()) {
1472 return io_double->value();
1473 }
1474 if (const io::serialize::IntValue *io_int = io_value.as_int_value()) {
1475 return io_int->value();
1476 }
1477 return std::nullopt;
1478}
1479
1480[[nodiscard]] static bool deserialize_float_array(const io::serialize::Value &io_value,
1481 MutableSpan<float> r_values)
1482{
1483 return deserialize_typed_array<float>(io_value, deserialize_float, r_values);
1484}
1485
1486template<typename T>
1487[[nodiscard]] static bool deserialize_int_array(const io::serialize::Value &io_value,
1488 MutableSpan<T> r_values)
1489{
1490 static_assert(std::is_integral_v<T>);
1491 return deserialize_typed_array<T>(io_value, deserialize_int<T>, r_values);
1492}
1493
1494[[nodiscard]] static bool deserialize_primitive_value(const io::serialize::Value &io_value,
1495 const eCustomDataType type,
1496 void *r_value)
1497{
1498 switch (type) {
1499 case CD_PROP_FLOAT: {
1500 const std::optional<float> value = deserialize_float(io_value);
1501 if (!value) {
1502 return false;
1503 }
1504 *static_cast<float *>(r_value) = *value;
1505 return true;
1506 }
1507 case CD_PROP_FLOAT2: {
1508 return deserialize_float_array(io_value, {static_cast<float *>(r_value), 2});
1509 }
1510 case CD_PROP_FLOAT3: {
1511 return deserialize_float_array(io_value, {static_cast<float *>(r_value), 3});
1512 }
1513 case CD_PROP_FLOAT4: {
1514 return deserialize_float_array(io_value, {static_cast<float *>(r_value), 4});
1515 }
1516 case CD_PROP_BOOL: {
1517 if (const io::serialize::BooleanValue *io_value_boolean = io_value.as_boolean_value()) {
1518 *static_cast<bool *>(r_value) = io_value_boolean->value();
1519 return true;
1520 }
1521 return false;
1522 }
1523 case CD_PROP_INT32: {
1524 const std::optional<int> value = deserialize_int<int>(io_value);
1525 if (!value) {
1526 return false;
1527 }
1528 *static_cast<int *>(r_value) = *value;
1529 return true;
1530 }
1531 case CD_PROP_INT16_2D: {
1532 return deserialize_int_array<int16_t>(io_value, {static_cast<int16_t *>(r_value), 2});
1533 }
1534 case CD_PROP_INT32_2D: {
1535 return deserialize_int_array<int>(io_value, {static_cast<int *>(r_value), 2});
1536 }
1537 case CD_PROP_BYTE_COLOR: {
1538 return deserialize_int_array<uint8_t>(io_value, {static_cast<uint8_t *>(r_value), 4});
1539 }
1540 case CD_PROP_COLOR: {
1541 return deserialize_float_array(io_value, {static_cast<float *>(r_value), 4});
1542 }
1543 case CD_PROP_QUATERNION: {
1544 return deserialize_float_array(io_value, {static_cast<float *>(r_value), 4});
1545 }
1546 case CD_PROP_FLOAT4X4: {
1547 return deserialize_float_array(io_value, {static_cast<float *>(r_value), 4 * 4});
1548 }
1549 default:
1550 break;
1551 }
1552 return false;
1553}
1554
1555[[nodiscard]] static bool deserialize_bundle_items(
1556 const io::serialize::ArrayValue &io_bundle_items,
1557 const BlobReader &blob_reader,
1558 const BlobReadSharing &blob_sharing,
1559 nodes::Bundle &r_bundle)
1560{
1561 for (const auto &io_item_ : io_bundle_items.elements()) {
1562 const DictionaryValue *io_item = io_item_->as_dictionary_value();
1563 if (!io_item) {
1564 return false;
1565 }
1566 const std::optional<std::string> key_str = io_item->lookup_str("key");
1567 if (!key_str) {
1568 return false;
1569 }
1570 const std::optional<StringRefNull> socket_idname = io_item->lookup_str("socket_idname");
1571 if (!socket_idname) {
1572 return false;
1573 }
1574 const DictionaryValue *io_item_value = io_item->lookup_dict("value");
1575 std::optional<SocketValueVariant> value = deserialize_bake_item(
1576 *io_item_value, blob_reader, blob_sharing);
1577 if (!value) {
1578 return false;
1579 }
1580 const bNodeSocketType *stype = node_socket_type_find(*socket_idname);
1581 if (!stype) {
1582 return false;
1583 }
1584 std::optional<nodes::BundleKey> key = nodes::BundleKey::from_str(*key_str);
1585 if (!key) {
1586 return false;
1587 }
1588 r_bundle.add(*key, nodes::BundleItemSocketValue{stype, std::move(*value)});
1589 }
1590 return true;
1591}
1592
1593static void serialize_bundle_items(const nodes::Bundle &bundle,
1594 ArrayValue &r_io_items,
1595 BlobWriter &blob_writer,
1596 BlobWriteSharing &blob_sharing)
1597{
1598 for (const auto &item : bundle.items()) {
1599 if (const auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value)) {
1600 DictionaryValue &io_bundle_item = *r_io_items.append_dict();
1601 io_bundle_item.append_str("key", item.key.ustr().string());
1602 io_bundle_item.append_str("socket_idname", socket_value->type->idname.string());
1603 io::serialize::DictionaryValue &io_bundle_item_value = *io_bundle_item.append_dict("value");
1605 socket_value->value, blob_writer, blob_sharing, io_bundle_item_value);
1606 }
1607 }
1608}
1609
1610static void serialize_single_value(const GPointer value,
1611 BlobWriter &blob_writer,
1612 BlobWriteSharing &blob_sharing,
1613 DictionaryValue &r_io_item)
1614{
1615 const CPPType &type = *value.type();
1616 if (type.is<SocketValueVariant>()) {
1617 const SocketValueVariant &socket_value = *value.get<SocketValueVariant>();
1618 serialize_socket_value_variant(socket_value, blob_writer, blob_sharing, r_io_item);
1619 return;
1620 }
1621 if (type.is<GeometrySet>()) {
1622 const GeometrySet &geometry = *value.get<GeometrySet>();
1623 r_io_item.append_str("type", "GEOMETRY");
1624 auto io_geometry = serialize_geometry_set(geometry, blob_writer, blob_sharing);
1625 r_io_item.append("data", io_geometry);
1626 return;
1627 }
1628 if (type.is<std::string>()) {
1629 const StringRefNull str = *value.get<std::string>();
1630 r_io_item.append_str("type", "STRING");
1631 /* Small strings are inlined, larger strings are stored separately. */
1632 const int64_t blob_threshold = 100;
1633 if (str.size() < blob_threshold) {
1634 r_io_item.append_str("data", str);
1635 }
1636 else {
1637 r_io_item.append("data",
1638 write_blob_raw_bytes(blob_writer, blob_sharing, str.data(), str.size()));
1639 }
1640 return;
1641 }
1642 if (type.is<nodes::BundlePtr>()) {
1643 const nodes::BundlePtr &bundle_ptr = *value.get<nodes::BundlePtr>();
1644 r_io_item.append_str("type", "BUNDLE");
1645 ArrayValue &io_items = *r_io_item.append_array("items");
1646 if (bundle_ptr) {
1647 serialize_bundle_items(*bundle_ptr, io_items, blob_writer, blob_sharing);
1648 }
1649 return;
1650 }
1651 const std::optional<eCustomDataType> data_type = cpp_type_to_custom_data_type(type);
1652 BLI_assert(data_type);
1653 r_io_item.append_str("type", get_data_type_io_name(*data_type));
1654 auto io_data = serialize_primitive_value(*data_type, value.get());
1655 r_io_item.append("data", std::move(io_data));
1656}
1657
1658static void serialize_field(const fn::GField &field,
1659 BlobWriter & /*blob_writer*/,
1660 BlobWriteSharing & /*blob_sharing*/,
1661 DictionaryValue &r_io_item)
1662{
1663 /* Only fields that are pure attribute references can be serialized currently. */
1664 if (const auto *attribute_field_input = field.get_input_if<AttributeFieldInput>()) {
1665 const StringRef attribute_name = attribute_field_input->attribute_name();
1666 const CPPType &type = field.cpp_type();
1667 const std::optional<eCustomDataType> data_type = cpp_type_to_custom_data_type(type);
1668 BLI_assert(data_type);
1669 r_io_item.append_str("type", "ATTRIBUTE");
1670 r_io_item.append_str("name", attribute_name);
1671 r_io_item.append_str("data_type", get_data_type_io_name(*data_type));
1672 }
1673}
1674
1675#ifdef WITH_OPENVDB
1676static void serialize_volume_grid(const volume_grid::GVolumeGrid &volume_grid,
1677 BlobWriter &blob_writer,
1678 BlobWriteSharing & /*blob_sharing*/,
1679 DictionaryValue &r_io_item)
1680{
1681 r_io_item.append_str("type", "GRID");
1682 auto io_vdb = blob_writer
1683 .write_as_stream(".vdb",
1684 [&](std::ostream &stream) {
1685 openvdb::GridCPtrVec vdb_grids;
1686 bke::VolumeTreeAccessToken tree_token;
1687 vdb_grids.push_back(volume_grid->grid_ptr(tree_token));
1688 openvdb::io::Stream vdb_stream(stream);
1689 vdb_stream.write(vdb_grids);
1690 })
1691 .serialize();
1692 r_io_item.append("vdb", std::move(io_vdb));
1693}
1694#endif
1695
1696static void serialize_list(const nodes::GListPtr &list_ptr,
1697 BlobWriter &blob_writer,
1698 BlobWriteSharing &blob_sharing,
1699 DictionaryValue &r_io_item)
1700{
1701 r_io_item.append_str("type", "LIST");
1702 if (!list_ptr) {
1703 return;
1704 }
1705 const nodes::GList &list = *list_ptr;
1706 const CPPType &type = list.cpp_type();
1707 std::optional<eCustomDataType> data_type;
1708 if (type.is<SocketValueVariant>()) {
1709 r_io_item.append_str("item_type", "SOCKET_VALUE_VARIANT");
1710 }
1711 else if (type.is<GeometrySet>()) {
1712 r_io_item.append_str("item_type", "GEOMETRY");
1713 }
1714 else if (type.is<nodes::BundlePtr>()) {
1715 r_io_item.append_str("item_type", "BUNDLE");
1716 }
1717 else if (type.is<std::string>()) {
1718 r_io_item.append_str("item_type", "STRING");
1719 }
1720 else {
1721 data_type = cpp_type_to_custom_data_type(type);
1722 BLI_assert(data_type);
1723 r_io_item.append_str("item_type", get_data_type_io_name(*data_type));
1724 }
1725 r_io_item.append_int("num_items", list.size());
1726 if (const auto *single_data = std::get_if<nodes::GList::SingleData>(&list.data())) {
1727 DictionaryValue &io_single_value = *r_io_item.append_dict("value");
1729 GPointer{type, single_data->value}, blob_writer, blob_sharing, io_single_value);
1730 }
1731 else if (const auto *array_data = std::get_if<nodes::GList::ArrayData>(&list.data())) {
1732 const GSpan array_span{type, array_data->data, list.size()};
1733 if (type.is_trivial && data_type) {
1734 r_io_item.append("data",
1736 blob_writer, blob_sharing, array_span, array_data->sharing_info.get()));
1737 }
1738 else {
1739 ArrayValue &io_values = *r_io_item.append_array("data");
1740 for (const int64_t i : IndexRange(list.size())) {
1741 DictionaryValue &io_value = *io_values.append_dict();
1742 serialize_single_value(GPointer{type, array_span[i]}, blob_writer, blob_sharing, io_value);
1743 }
1744 }
1745 }
1746}
1747
1748template<typename T>
1749static std::optional<T> deserialize_list_item(const DictionaryValue &io_value,
1750 const BlobReader &blob_reader,
1751 const BlobReadSharing &blob_sharing)
1752{
1753 std::optional<SocketValueVariant> value = deserialize_bake_item(
1754 io_value, blob_reader, blob_sharing);
1755 if (!value || !value->is_single()) {
1756 return std::nullopt;
1757 }
1758 return value->extract<T>();
1759}
1760
1761template<typename T>
1762static std::optional<SocketValueVariant> deserialize_value_list(
1763 const DictionaryValue &io_item,
1764 const int num_items,
1765 const BlobReader &blob_reader,
1766 const BlobReadSharing &blob_sharing)
1767{
1768 const CPPType &cpp_type = CPPType::get<T>();
1769 if (const DictionaryValue *io_value = io_item.lookup_dict("value")) {
1770 std::optional<T> value = deserialize_list_item<T>(*io_value, blob_reader, blob_sharing);
1771 if (!value) {
1772 return std::nullopt;
1773 }
1775 cpp_type, nodes::GList::SingleData::ForValue(GPointer{cpp_type, &*value}), num_items);
1776 return SocketValueVariant::from(std::move(list));
1777 }
1778 const ArrayValue *io_values = io_item.lookup_array("data");
1779 if (!io_values || io_values->elements().size() != num_items) {
1780 return std::nullopt;
1781 }
1782 GArray<> values(cpp_type, num_items);
1783 MutableSpan<T> values_span = values.as_mutable_span().typed<T>();
1784 for (const int i : IndexRange(num_items)) {
1785 const DictionaryValue *io_value = io_values->elements()[i]->as_dictionary_value();
1786 if (!io_value) {
1787 return std::nullopt;
1788 }
1789 std::optional<T> value = deserialize_list_item<T>(*io_value, blob_reader, blob_sharing);
1790 if (!value) {
1791 return std::nullopt;
1792 }
1793 values_span[i] = std::move(*value);
1794 }
1795 return SocketValueVariant::from(nodes::GList::from_garray(std::move(values)));
1796}
1797
1799 BlobWriter &blob_writer,
1800 BlobWriteSharing &blob_sharing,
1801 DictionaryValue &r_io_item)
1802{
1803 if (value_variant.is_single()) {
1804 const GPointer single_value = value_variant.get();
1805 serialize_single_value(single_value, blob_writer, blob_sharing, r_io_item);
1806 return;
1807 }
1808 if (value_variant.is_context_dependent_field()) {
1809 const fn::GField &field = *value_variant.get_if<fn::GField>();
1810 serialize_field(field, blob_writer, blob_sharing, r_io_item);
1811 return;
1812 }
1813#ifdef WITH_OPENVDB
1814 if (value_variant.is_volume_grid()) {
1815 const auto &volume_grid = *value_variant.get_if<volume_grid::GVolumeGrid>();
1816 serialize_volume_grid(volume_grid, blob_writer, blob_sharing, r_io_item);
1817 return;
1818 }
1819#endif
1820 if (value_variant.is_list()) {
1821 const nodes::GListPtr &list = *value_variant.get_if<nodes::GListPtr>();
1822 serialize_list(list, blob_writer, blob_sharing, r_io_item);
1823 return;
1824 }
1825}
1826
1828 BlobWriter &blob_writer,
1829 BlobWriteSharing &blob_sharing,
1830 DictionaryValue &r_io_item)
1831{
1832 if (item.name.has_value()) {
1833 r_io_item.append_str("value_name", *item.name);
1834 }
1835 serialize_socket_value_variant(item.value, blob_writer, blob_sharing, r_io_item);
1836}
1837
1838static std::optional<SocketValueVariant> deserialize_bake_item(const DictionaryValue &io_item,
1839 const BlobReader &blob_reader,
1840 const BlobReadSharing &blob_sharing)
1841{
1842 const std::optional<StringRefNull> state_item_type = io_item.lookup_str("type");
1843 if (!state_item_type) {
1844 return {};
1845 }
1846 if (*state_item_type == StringRef("GEOMETRY")) {
1847 const DictionaryValue *io_geometry = io_item.lookup_dict("data");
1848 if (!io_geometry) {
1849 return {};
1850 }
1851 GeometrySet geometry = load_geometry(*io_geometry, blob_reader, blob_sharing);
1852 return SocketValueVariant::from(std::move(geometry));
1853 }
1854 if (*state_item_type == StringRef("ATTRIBUTE")) {
1855 const DictionaryValue *io_attribute = &io_item;
1856 if (!io_attribute) {
1857 return {};
1858 }
1859 std::optional<StringRefNull> name = io_attribute->lookup_str("name");
1860 if (!name) {
1861 return {};
1862 }
1863 const CPPType *field_cpp_type = nullptr;
1864 if (const std::optional<StringRefNull> type = io_attribute->lookup_str("data_type")) {
1865 if (const std::optional<eCustomDataType> data_type = get_data_type_from_io_name(*type)) {
1866 field_cpp_type = custom_data_type_to_cpp_type(*data_type);
1867 }
1868 }
1869 if (field_cpp_type) {
1870 return SocketValueVariant::from(AttributeFieldInput::from(*name, *field_cpp_type));
1871 }
1872 /* The data type hasn't been written to the bake, it will be derived later from the attribute
1873 * type. */
1876 }
1877#ifdef WITH_OPENVDB
1878 if (*state_item_type == StringRef("GRID")) {
1879 const DictionaryValue &io_grid = io_item;
1880 const auto *io_vdb = io_grid.lookup_dict("vdb");
1881 if (!io_vdb) {
1882 return {};
1883 }
1884 std::optional<BlobSlice> vdb_slice = BlobSlice::deserialize(*io_vdb);
1885 if (!vdb_slice) {
1886 return {};
1887 }
1888 openvdb::GridPtrVecPtr vdb_grids;
1889 if (!blob_reader.read_as_stream(*vdb_slice,
1890 [&](std::istream &stream) {
1891 try {
1892 openvdb::io::Stream vdb_stream{stream};
1893 vdb_grids = vdb_stream.getGrids();
1894 return true;
1895 }
1896 catch (...) {
1897 return false;
1898 }
1899 }))
1900 {
1901 return {};
1902 }
1903 if (vdb_grids->size() != 1) {
1904 return {};
1905 }
1906 std::shared_ptr<openvdb::GridBase> vdb_grid = std::move((*vdb_grids)[0]);
1907 GVolumeGrid grid{std::move(vdb_grid)};
1908 return SocketValueVariant::from(std::move(grid));
1909 }
1910#endif
1911 if (*state_item_type == StringRef("STRING")) {
1912 const std::shared_ptr<io::serialize::Value> *io_data = io_item.lookup("data");
1913 if (!io_data) {
1914 return {};
1915 }
1916 if (io_data->get()->type() == io::serialize::eValueType::String) {
1917 const io::serialize::StringValue &io_string = *io_data->get()->as_string_value();
1918 return SocketValueVariant::from(std::string(io_string.value()));
1919 }
1920 if (const io::serialize::DictionaryValue *io_string = io_data->get()->as_dictionary_value()) {
1921 const std::optional<int64_t> size = io_string->lookup_int("size");
1922 if (!size) {
1923 return {};
1924 }
1925 std::string str;
1926 str.resize(*size);
1927 if (!read_blob_raw_bytes(blob_reader, *io_string, *size, str.data())) {
1928 return {};
1929 }
1930 return SocketValueVariant::from(std::move(str));
1931 }
1932 }
1933 if (*state_item_type == StringRef("BUNDLE")) {
1935 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
1936 const ArrayValue *io_bundle_items = io_item.lookup_array("items");
1937 if (!io_bundle_items) {
1938 return {};
1939 }
1940 if (!deserialize_bundle_items(*io_bundle_items, blob_reader, blob_sharing, bundle)) {
1941 return {};
1942 }
1943 return SocketValueVariant::from(std::move(bundle_ptr));
1944 }
1945 if (*state_item_type == StringRef("LIST")) {
1946 const std::optional<StringRefNull> io_list_item_type = io_item.lookup_str("item_type");
1947 if (!io_list_item_type) {
1948 return {};
1949 }
1950 const std::optional<int> num_items = io_item.lookup_int("num_items");
1951 if (!num_items) {
1952 return {};
1953 }
1954 if (io_list_item_type == "SOCKET_VALUE_VARIANT") {
1955 Vector<SocketValueVariant> values(*num_items);
1956 const ArrayValue *io_values = io_item.lookup_array("data");
1957 if (!io_values) {
1958 return {};
1959 }
1960 if (io_values->elements().size() != *num_items) {
1961 return {};
1962 }
1963 for (const int64_t i : IndexRange(*num_items)) {
1964 const DictionaryValue *io_value = io_values->elements()[i]->as_dictionary_value();
1965 if (!io_value) {
1966 return {};
1967 }
1968 std::optional<SocketValueVariant> value = deserialize_bake_item(
1969 *io_value, blob_reader, blob_sharing);
1970 if (!value) {
1971 return {};
1972 }
1973 values[i] = std::move(*value);
1974 }
1976 }
1977 if (io_list_item_type == "GEOMETRY") {
1978 return deserialize_value_list<GeometrySet>(io_item, *num_items, blob_reader, blob_sharing);
1979 }
1980 if (io_list_item_type == "BUNDLE") {
1981 return deserialize_value_list<nodes::BundlePtr>(
1982 io_item, *num_items, blob_reader, blob_sharing);
1983 }
1984 if (io_list_item_type == "STRING") {
1985 return deserialize_value_list<std::string>(io_item, *num_items, blob_reader, blob_sharing);
1986 }
1987 if (const std::optional<eCustomDataType> data_type = get_data_type_from_io_name(
1988 *io_list_item_type))
1989 {
1990 const CPPType *cpp_type = custom_data_type_to_cpp_type(*data_type);
1991 BLI_assert(cpp_type);
1992 if (const std::shared_ptr<io::serialize::Value> *io_value = io_item.lookup("value")) {
1993 BUFFER_FOR_CPP_TYPE_VALUE(*cpp_type, buffer);
1994 if (!deserialize_primitive_value(**io_value, *data_type, buffer)) {
1995 return {};
1996 }
1997 auto list = nodes::GList::create(
1998 *cpp_type, nodes::GList::SingleData::ForValue(GPointer{cpp_type, buffer}), *num_items);
1999 return SocketValueVariant::from(std::move(list));
2000 }
2001 if (const io::serialize::DictionaryValue *io_data = io_item.lookup_dict("data")) {
2002 GArray<> buffer(*cpp_type, *num_items);
2004 blob_reader, *io_data, GMutableSpan{cpp_type, buffer.data(), *num_items}))
2005 {
2006 return {};
2007 }
2008 return SocketValueVariant::from(nodes::GList::from_garray(std::move(buffer)));
2009 }
2010 }
2011 }
2012 const std::shared_ptr<io::serialize::Value> *io_data = io_item.lookup("data");
2013 if (!io_data) {
2014 return {};
2015 }
2016 const std::optional<eCustomDataType> data_type = get_data_type_from_io_name(*state_item_type);
2017 if (data_type) {
2018 const CPPType &cpp_type = *custom_data_type_to_cpp_type(*data_type);
2019 BUFFER_FOR_CPP_TYPE_VALUE(cpp_type, buffer);
2020 if (!deserialize_primitive_value(**io_data, *data_type, buffer)) {
2021 return {};
2022 }
2023 BLI_SCOPED_DEFER([&]() { cpp_type.destruct(buffer); });
2024 SocketValueVariant value;
2025 cpp_type.copy_construct(buffer, value.allocate_single(cpp_type));
2026 return value;
2027 }
2028 return {};
2029}
2030
2031static constexpr int bake_file_version = 3;
2032
2033void serialize_bake(const BakeValues &bake_values,
2034 BlobWriter &blob_writer,
2035 BlobWriteSharing &blob_sharing,
2036 std::ostream &r_stream)
2037{
2040 io_root.append_int("version", bake_file_version);
2041 io::serialize::DictionaryValue &io_items = *io_root.append_dict("items");
2042 for (const auto &item : bake_values.values_by_id().items()) {
2043 io::serialize::DictionaryValue &io_item = *io_items.append_dict(std::to_string(item.key));
2044 serialize_bake_item(item.value, blob_writer, blob_sharing, io_item);
2045 }
2046
2048 formatter.serialize(r_stream, io_root);
2049}
2050
2051std::optional<BakeValues> deserialize_bake(std::istream &stream,
2052 const BlobReader &blob_reader,
2053 const BlobReadSharing &blob_sharing)
2054{
2056 JsonFormatter formatter;
2057 std::unique_ptr<io::serialize::Value> io_root_value;
2058 try {
2059 io_root_value = formatter.deserialize(stream);
2060 }
2061 catch (...) {
2062 return std::nullopt;
2063 }
2064 if (!io_root_value) {
2065 return std::nullopt;
2066 }
2067 const io::serialize::DictionaryValue *io_root = io_root_value->as_dictionary_value();
2068 if (!io_root) {
2069 return std::nullopt;
2070 }
2071 const std::optional<int> version = io_root->lookup_int("version");
2072 if (!version.has_value() || *version != bake_file_version) {
2073 return std::nullopt;
2074 }
2075 const io::serialize::DictionaryValue *io_items = io_root->lookup_dict("items");
2076 if (!io_items) {
2077 return std::nullopt;
2078 }
2079 Map<int, BakeValues::Item> bake_values;
2080 for (const auto &io_item_value : io_items->elements()) {
2081 const io::serialize::DictionaryValue *io_item = io_item_value.second->as_dictionary_value();
2082 if (!io_item) {
2083 return std::nullopt;
2084 }
2085 int id;
2086 try {
2087 id = std::stoi(io_item_value.first);
2088 }
2089 catch (...) {
2090 return std::nullopt;
2091 }
2092 if (bake_values.contains(id)) {
2093 return std::nullopt;
2094 }
2095 std::optional<SocketValueVariant> value = deserialize_bake_item(
2096 *io_item, blob_reader, blob_sharing);
2097 if (!value) {
2098 return std::nullopt;
2099 }
2100 bake_values.add_new(id, BakeValues::Item{std::move(*value)});
2101 }
2102 return BakeValues(std::move(bake_values));
2103}
2104
2105} // namespace blender::bke::bake
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
Low-level operations for grease pencil.
General operations for point clouds.
Volume data-block.
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define BLI_path_join(...)
#define STRNCPY_UTF8(dst, src)
Object is a sort of wrapper for general info.
#define PRF_scope(category)
volatile int lock
#define BLI_assert_unreachable()
#define BLI_assert(a)
virtual bool serialize(void *o_alignedDataBuffer, unsigned i_dataBufferSize, bool i_swapEndian) const
Data buffer MUST be 16 byte aligned.
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
static const CPPType & get()
bool is() const
void copy_assign_n(const void *src, void *dst, int64_t n) const
ChannelStorageType r
GMutableSpan as_mutable_span()
int64_t size_in_bytes() const
MutableSpan< T > typed() const
const CPPType * type() const
GPointer()=default
const void * get() const
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr IndexRange()=default
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr int64_t start() const
constexpr bool contains(int64_t value) const
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool contains_as(const ForwardKey &key) const
Definition BLI_set.hh:314
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const char * c_str() const
constexpr StringRef()
void append(const T &value)
const T & last(const int64_t n=0) const
Span< T > as_span() const
void append_as(ForwardValue &&...value)
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
int domain_size(const AttrDomain domain) const
static fn::GField from(std::string name, const CPPType &type, std::optional< std::string > socket_inspection_name=std::nullopt)
GAttributeReader get() const
Span< InstanceReference > references() const
Definition instances.cc:290
bke::AttributeAccessor attributes() const
Definition instances.cc:68
int instances_num() const
Definition instances.cc:408
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
bool assign_data(const StringRef name, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
static SocketValueVariant from(T &&value)
const Map< int, Item > & values_by_id() const
std::optional< ImplicitSharingInfoAndData > read_shared(const io::serialize::DictionaryValue &io_data, FunctionRef< std::optional< ImplicitSharingInfoAndData >()> read_fn) const
virtual bool read_as_stream(const BlobSlice &slice, FunctionRef< bool(std::istream &)> fn) const
virtual bool read(const BlobSlice &slice, void *r_data) const =0
std::shared_ptr< io::serialize::DictionaryValue > write_implicitly_shared(const ImplicitSharingInfo *sharing_info, FunctionRef< std::shared_ptr< io::serialize::DictionaryValue >()> write_fn)
std::shared_ptr< io::serialize::DictionaryValue > write_deduplicated(BlobWriter &writer, const void *data, int64_t size_in_bytes)
virtual BlobSlice write(const void *data, int64_t size)=0
virtual BlobSlice write_as_stream(StringRef file_extension, FunctionRef< void(std::ostream &)> fn)
bool read(const BlobSlice &slice, void *r_data) const override
DiskBlobWriter(std::string blob_dir, std::string base_name)
BlobSlice write_as_stream(StringRef file_extension, FunctionRef< void(std::ostream &)> fn) override
BlobSlice write(const void *data, int64_t size) override
void add(StringRef name, Span< std::byte > blob)
bool read(const BlobSlice &slice, void *r_data) const override
BlobSlice write(const void *data, int64_t size) override
BlobSlice write_as_stream(StringRef file_extension, FunctionRef< void(std::ostream &)> fn) override
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
static GField from_input(Args &&...args)
Definition FN_field.hh:482
const InputT * get_input_if() const
Definition FN_field.hh:623
const CPPType & cpp_type() const
Definition FN_field.hh:518
Span< std::shared_ptr< Value > > elements() const
std::shared_ptr< DictionaryValue > append_dict()
Definition serialize.cc:242
const std::shared_ptr< Value > * lookup(StringRef key) const
Definition serialize.cc:265
void append(std::string key, std::shared_ptr< Value > value)
Definition serialize.cc:331
std::optional< StringRefNull > lookup_str(StringRef key) const
Definition serialize.cc:275
void append_str(std::string key, std::string value)
Definition serialize.cc:351
std::shared_ptr< DictionaryValue > append_dict(std::string key)
Definition serialize.cc:356
std::optional< int64_t > lookup_int(StringRef key) const
Definition serialize.cc:285
void append_int(std::string key, int64_t value)
Definition serialize.cc:336
std::shared_ptr< ArrayValue > append_array(std::string key)
Definition serialize.cc:363
const DictionaryValue * lookup_dict(StringRef key) const
Definition serialize.cc:315
const ArrayValue * lookup_array(StringRef key) const
Definition serialize.cc:323
std::optional< bool > lookup_bool(StringRef key) const
Definition serialize.cc:295
std::unique_ptr< Value > deserialize(std::istream &is) override
Definition serialize.cc:382
void serialize(std::ostream &os, const Value &value) override
Definition serialize.cc:370
const BooleanValue * as_boolean_value() const
Definition serialize.cc:40
const ArrayValue * as_array_value() const
Definition serialize.cc:56
const IntValue * as_int_value() const
Definition serialize.cc:24
const DoubleValue * as_double_value() const
Definition serialize.cc:32
const DictionaryValue * as_dictionary_value() const
Definition serialize.cc:64
static std::optional< BundleKey > from_str(StringRef key)
bool add(BundleKey key, const BundleItemValue &value)
BundleItemMap::ItemIterator items() const
static SingleData ForValue(const GPointer &value)
static GListPtr from_garray(GArray<> array)
static GListPtr from_container(ContainerT &&container)
static GListPtr create(const CPPType &type, DataVariant data, const int64_t size)
#define str(s)
#define attribute(slot)
#define layer
#define T
static bool deserialize_typed_array(const io::serialize::Value &io_value, FunctionRef< std::optional< T >(const io::serialize::Value &io_element)> fn, MutableSpan< T > r_values)
static StringRefNull get_domain_io_name(const AttrDomain domain)
static constexpr int bake_file_version
static bool read_blob_shared_simple_span(const DictionaryValue &io_data, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing, const int size, T **r_data, const ImplicitSharingInfo **r_sharing_info)
static std::optional< AttrDomain > get_domain_from_io_name(const StringRefNull io_name)
static std::optional< eCustomDataType > get_data_type_from_io_name(const StringRefNull io_name)
static void serialize_field(const fn::GField &field, BlobWriter &, BlobWriteSharing &, DictionaryValue &r_io_item)
static bool deserialize_int_array(const io::serialize::Value &io_value, MutableSpan< T > r_values)
static bool read_blob_simple_gspan(const BlobReader &blob_reader, const DictionaryValue &io_data, GMutableSpan r_data)
static std::shared_ptr< io::serialize::ArrayValue > serialize_int_array(const Span< int > values)
static bool read_blob_raw_bytes(const BlobReader &blob_reader, const DictionaryValue &io_data, const int64_t bytes_num, void *r_data)
static StringRefNull get_data_type_io_name(const eCustomDataType data_type)
static std::optional< T > deserialize_list_item(const DictionaryValue &io_value, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static bool load_materials(const io::serialize::ArrayValue &io_materials, std::unique_ptr< BakeMaterialsList > &materials)
static void serialize_single_value(const GPointer value, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, DictionaryValue &r_io_item)
static std::optional< SocketValueVariant > deserialize_value_list(const DictionaryValue &io_item, const int num_items, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static StringRefNull get_storage_type_io_name(const AttrStorageType storage_type)
static std::optional< CurvesGeometry > try_load_curves_geometry(const DictionaryValue &io_curves, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static std::optional< float > deserialize_float(const io::serialize::Value &io_value)
static std::optional< T > deserialize_int(const io::serialize::Value &io_value)
static std::shared_ptr< io::serialize::Value > serialize_primitive_value(const eCustomDataType data_type, const void *value_ptr)
static bool load_attributes(const io::serialize::ArrayValue &io_attributes, MutableAttributeAccessor &attributes, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static void serialize_curves_geometry(DictionaryValue &io_curves, const CurvesGeometry &curves, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing)
static std::shared_ptr< DictionaryValue > write_blob_raw_bytes(BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, const void *data, const int64_t size_in_bytes)
static std::shared_ptr< DictionaryValue > serialize_geometry_set(const GeometrySet &geometry, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing)
static Curves * try_load_curves(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static std::optional< SocketValueVariant > deserialize_bake_item(const DictionaryValue &io_item, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static std::shared_ptr< DictionaryValue > write_blob_shared_simple_gspan(BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, const GSpan data, const ImplicitSharingInfo *sharing_info)
static std::optional< AttrStorageType > get_storage_type_from_io_name(const StringRefNull io_name)
void serialize_bake(const BakeValues &bake_values, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, std::ostream &r_stream)
static GeometrySet load_geometry(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static Mesh * try_load_mesh(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
std::optional< BakeValues > deserialize_bake(std::istream &stream, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static PointCloud * try_load_pointcloud(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static bool deserialize_primitive_value(const io::serialize::Value &io_value, const eCustomDataType type, void *r_value)
std::shared_ptr< DictionaryValue > DictionaryValuePtr
static std::shared_ptr< DictionaryValue > write_blob_simple_gspan(BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, const GSpan data)
static GreasePencil * try_load_grease_pencil(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static const void * read_blob_shared_simple_gspan(const DictionaryValue &io_data, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing, const CPPType &cpp_type, const int size, const ImplicitSharingInfo **r_sharing_info)
static std::unique_ptr< Instances > try_load_instances(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static bool deserialize_float_array(const io::serialize::Value &io_value, MutableSpan< float > r_values)
static void serialize_texture_space(const Mesh &mesh, std::shared_ptr< DictionaryValue > io_mesh)
static void serialize_list(const nodes::GListPtr &list_ptr, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, DictionaryValue &r_io_item)
static void serialize_bundle_items(const nodes::Bundle &bundle, ArrayValue &r_io_items, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing)
static std::string make_independent_file_name(const StringRef base_name, const int file_index, const StringRef extension)
static nodes::BundlePtr try_load_geometry_bundle(const DictionaryValue &io_geometry, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
static void load_texspace(const DictionaryValue *io_mesh, Mesh *mesh)
static std::shared_ptr< io::serialize::ArrayValue > serialize_float_array(const Span< float > values)
static bool deserialize_bundle_items(const io::serialize::ArrayValue &io_bundle_items, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing, nodes::Bundle &r_bundle)
static std::shared_ptr< io::serialize::ArrayValue > serialize_attributes(const AttributeAccessor &attributes, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, const Set< std::string > &attributes_to_ignore)
static void serialize_bake_item(const BakeValues::Item &item, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, DictionaryValue &r_io_item)
static void serialize_socket_value_variant(const SocketValueVariant &value_variant, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, DictionaryValue &r_io_item)
static std::shared_ptr< io::serialize::ArrayValue > serialize_materials(const std::unique_ptr< BakeMaterialsList > &materials)
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
bool attribute_name_is_anonymous(const StringRef name)
std::optional< eCustomDataType > cpp_type_to_custom_data_type(const CPPType &type)
const CPPType * custom_data_type_to_cpp_type(eCustomDataType type)
PointCloud * pointcloud_new_no_attributes(PointCloudType type, int totpoint)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
Mesh * mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num)
bNodeSocketType * node_socket_type_find(StringRef idname)
Definition node.cc:2966
Curves * curves_new_nomain(int points_num, int curves_num)
int size(const AssetLibraryReference *library_reference)
PrimitiveValue< int64_t, eValueType::Int > IntValue
PrimitiveValue< bool, eValueType::Boolean > BooleanValue
PrimitiveValue< double, eValueType::Double > DoubleValue
QuaternionBase< float > Quaternion
ImplicitSharingPtr< Bundle > BundlePtr
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
VecBase< int32_t, 4 > int4
int BKE_volume_num_grids(const Volume *volume)
const bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
const EnumPropertyItem rna_enum_attr_storage_type_items[]
const EnumPropertyItem rna_enum_attribute_type_items[]
void BKE_volume_grid_add(Volume *volume, const bke::VolumeGridData &grid)
bool RNA_enum_id_from_value(const EnumPropertyItem *item, int value, const char **r_identifier)
void BKE_id_free(Main *bmain, void *idv)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
bool RNA_enum_value_from_identifier(const EnumPropertyItem *item, const char *identifier, int *r_value)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
VecBase< int16_t, 2 > short2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
ATTR_WARN_UNUSED_RESULT const void * element
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
GreasePencil * BKE_grease_pencil_new_nomain()
const EnumPropertyItem rna_enum_attribute_domain_items[]
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
signed short int16_t
Definition BKE_text.h:77
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const T * base_ptr() const
bke::PointCloudRuntime * runtime
bke::VolumeRuntime * runtime
AttributeInitShared(const void *data, const ImplicitSharingInfo &sharing_info)
std::unique_ptr< bake::BakeMaterialsList > bake_materials
std::unique_ptr< bake::BakeMaterialsList > bake_materials
Defines a socket type.
Definition BKE_node.hh:163
BakeDataBlockID(ID_Type type, std::string id_name, std::string lib_name)
std::optional< std::string > name
std::shared_ptr< io::serialize::DictionaryValue > serialize() const
static std::optional< BlobSlice > deserialize(const io::serialize::DictionaryValue &io_slice)