106 int attribute_field_count_ = 0;
113 const bool is_for_cache)
114 : root_values_(root_values), data_block_map_(data_block_map), is_for_cache_(is_for_cache)
122 input_value.value.ensure_owns_direct_data();
125 this->top_level_fields_to_attributes();
131 auto scan_field = [&](
const fn::GField &field) {
133 const StringRef attribute_name = attribute_field->attribute_name();
135 referenced_anonymous_attributes_.lookup_or_add_cb_as(
136 attribute_name, [&]() {
return this->get_next_bake_attribute_name(); });
139 return VisitParams::continue_check(
true);
141 VisitParams visit_params;
142 visit_params.check_GField = scan_field;
144 check_recursive(input_value.value, visit_params);
151 for (
const int i : root_values_.index_range()) {
152 if (!this->runtime_to_bake__SocketValueVariant(root_values_[i].value)) {
153 inputs_to_remove.
append(i);
157 for (
int i = inputs_to_remove.
size() - 1; i >= 0; i--) {
158 root_values_.remove_and_reorder(inputs_to_remove[i]);
164 void top_level_fields_to_attributes()
167 for (
const int value_i : root_values_.
index_range()) {
183 std::string attribute_name = this->get_next_bake_attribute_name();
184 bool any_success =
false;
194 if (!prev_geo->
has(type)) {
199 component, attribute_name, *input_value.
field_domain, field);
210 std::string get_next_bake_attribute_name()
215 [[nodiscard]]
bool runtime_to_bake__SocketValueVariant(SocketValueVariant &value_variant)
217 if (value_variant.is_context_dependent_field()) {
218 const fn::GField &field = *value_variant.get_if<fn::GField>();
219 if (
const auto *attribute_field = field.get_input_if<AttributeFieldInput>()) {
220 if (
const std::string *new_name = referenced_anonymous_attributes_.lookup_ptr(
221 attribute_field->attribute_name()))
223 value_variant.emplace<fn::GField>(
229 value_variant.ensure_type(field.cpp_type());
233 if (value_variant.is_single()) {
234 GMutablePointer value_ptr = value_variant.
get();
235 return this->runtime_to_bake__GMutablePointer(value_ptr);
237 if (value_variant.is_list()) {
238 nodes::GListPtr list_ptr = value_variant.extract<nodes::GListPtr>();
240 nodes::GList &list = list_ptr.get_for_write();
241 if (!this->runtime_to_bake__List(list)) {
245 value_variant.emplace<nodes::GListPtr>(std::move(list_ptr));
248 if (value_variant.is_volume_grid()) {
254 [[nodiscard]]
bool runtime_to_bake__List(nodes::GList &list)
256 const CPPType &list_cpp_type = list.cpp_type();
257 if (list_cpp_type.is<SocketValueVariant>()) {
258 bool found_invalid =
false;
259 list.typed<SocketValueVariant>().foreach_for_write([&](SocketValueVariant &value_variant) {
260 if (!this->runtime_to_bake__SocketValueVariant(value_variant)) {
261 found_invalid =
true;
264 return !found_invalid;
266 if (list_cpp_type.is<GeometrySet>()) {
267 list.typed<GeometrySet>().foreach_for_write(
268 [&](GeometrySet &geometry) { this->runtime_to_bake__GeometrySet(geometry); });
274 this->runtime_to_bake__Bundle(bundle_ptr.ensure_mutable_inplace());
279 if (this->is_bakeable_single_value_type(list_cpp_type)) {
285 [[nodiscard]]
bool runtime_to_bake__GMutablePointer(GMutablePointer value_ptr)
287 const CPPType &type = *value_ptr.type();
288 if (type.is<GeometrySet>()) {
289 GeometrySet &geometry = *value_ptr.get<GeometrySet>();
290 this->runtime_to_bake__GeometrySet(geometry);
296 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
297 this->runtime_to_bake__Bundle(bundle);
301 if (this->is_bakeable_single_value_type(type)) {
307 void runtime_to_bake__GeometrySet(GeometrySet &geometry)
309 geometry.ensure_owns_all_data();
310 if (geometry.has_bundle()) {
313 this->runtime_to_bake__Bundle(bundle);
315 if (geometry.has_instances()) {
316 Instances &instances = *geometry.get_instances_for_write();
317 instances.ensure_geometry_instances();
318 this->runtime_to_bake__AttributeStorage(instances.attribute_storage());
319 for (bke::InstanceReference &reference : instances.references_for_write()) {
321 GeometrySet &geometry = reference.geometry_set();
322 this->runtime_to_bake__GeometrySet(geometry);
326 reference = bke::InstanceReference();
330 if (geometry.has_mesh()) {
331 Mesh &mesh = *geometry.get_mesh_for_write();
334 mesh.runtime->edit_mesh.reset();
336 this->runtime_to_bake__AttributeStorage(mesh.attribute_storage.wrap());
338 &mesh.mat, &mesh.totcol, data_block_map_);
340 if (geometry.has_curves()) {
341 Curves &curves = *geometry.get_curves_for_write();
342 this->runtime_to_bake__AttributeStorage(curves.geometry.attribute_storage.wrap());
344 &curves.mat, &curves.totcol, data_block_map_);
346 if (geometry.has_pointcloud()) {
347 PointCloud &pointcloud = *geometry.get_pointcloud_for_write();
348 this->runtime_to_bake__AttributeStorage(pointcloud.attribute_storage.wrap());
350 &pointcloud.mat, &pointcloud.totcol, data_block_map_);
352 if (geometry.has_grease_pencil()) {
353 GreasePencil &grease_pencil = *geometry.get_grease_pencil_for_write();
354 this->runtime_to_bake__AttributeStorage(grease_pencil.attribute_storage.wrap());
355 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
360 this->runtime_to_bake__AttributeStorage(
361 drawing.strokes_for_write().attribute_storage.wrap());
364 &grease_pencil.material_array, &grease_pencil.material_array_num, data_block_map_);
366 if (geometry.has_volume()) {
367 Volume &volume = *geometry.get_volume_for_write();
369 &volume.mat, &volume.totcol, data_block_map_);
373 void runtime_to_bake__AttributeStorage(AttributeStorage &attributes)
375 Set<const Attribute *> attributes_to_remove;
376 Map<Attribute *, StringRef> attributes_to_rename;
377 for (Attribute &
attribute : attributes) {
378 const StringRef attribute_name =
attribute.name();
382 const std::string *new_name = referenced_anonymous_attributes_.lookup_ptr(attribute_name);
384 attributes_to_rename.add_new(&
attribute, *new_name);
387 attributes_to_remove.add_new(&
attribute);
391 attributes.remove(attributes_to_remove);
392 attributes.rename(attributes_to_rename);
395 void runtime_to_bake__Bundle(nodes::Bundle &bundle)
397 Vector<nodes::BundleKey> values_to_remove;
398 for (
const auto &item : bundle.items()) {
399 if (
auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value)) {
400 if (!this->runtime_to_bake__SocketValueVariant(socket_value->value)) {
401 values_to_remove.append(item.key);
405 for (
const nodes::BundleKey &value_to_remove : values_to_remove) {
406 bundle.remove(value_to_remove);
410 bool is_bakeable_single_value_type(
const CPPType &type)
const
412 if (CPPType::get<std::string>() == type) {
425 std::string anonymous_attribute_name_mixin_;
435 : anonymous_attribute_name_mixin_(std::move(anonymous_attribute_name_mixin)),
436 data_block_map_(data_block_map)
444 attributes.foreach_attribute([&](
const AttributeIter &iter) {
448 this->attribute_field_types_.
add(iter.
name, &attr_cpp_type);
451 return VisitParams::continue_check(
true);
453 VisitParams visit_params;
454 visit_params.check_AttributeAccessor = scan_attributes;
455 check_recursive(root_value, visit_params);
460 this->bake_to_runtime__SocketValueVariant(root_value, name);
471 const StringRef bake_attribute_name = attribute_field->attribute_name();
473 std::string anonymous_attribute_name = this->get_anonymous_attribute_name(
474 bake_attribute_name);
477 attribute_field->cpp_type(),
478 std::move(socket_inspection)));
481 else if (
const auto *attribute_field = field.
get_input_if<DeferredTypeAttributeFieldInput>())
483 const StringRef bake_attribute_name = attribute_field->attribute_name;
488 std::string anonymous_attribute_name = this->get_anonymous_attribute_name(
489 bake_attribute_name);
491 std::move(anonymous_attribute_name), *cpp_type, std::move(socket_inspection)));
498 GMutablePointer value_ptr = value_variant.
get();
499 this->bake_to_runtime__GMutablePointer(value_ptr);
503 nodes::GListPtr list_ptr = value_variant.
extract<nodes::GListPtr>();
505 nodes::GList &list = list_ptr.get_for_write();
506 this->bake_to_runtime__GList(list);
508 value_variant.
emplace<nodes::GListPtr>(std::move(list_ptr));
512 void bake_to_runtime__GMutablePointer(GMutablePointer value_ptr)
514 const CPPType &type = *value_ptr.type();
515 if (type.is<GeometrySet>()) {
516 GeometrySet &geometry = *value_ptr.get<GeometrySet>();
517 this->bake_to_runtime__GeometrySet(geometry);
522 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
523 this->bake_to_runtime__Bundle(bundle);
528 void bake_to_runtime__GeometrySet(GeometrySet &geometry)
530 if (geometry.has_bundle()) {
533 this->bake_to_runtime__Bundle(bundle);
535 if (geometry.has_instances()) {
536 Instances &instances = *geometry.get_instances_for_write();
537 instances.ensure_geometry_instances();
538 for (bke::InstanceReference &reference : instances.references_for_write()) {
540 GeometrySet &geometry = reference.geometry_set();
541 this->bake_to_runtime__GeometrySet(geometry);
544 this->bake_to_runtime__AttributeStorage(instances.attribute_storage());
546 if (geometry.has_mesh()) {
547 Mesh &mesh = *geometry.get_mesh_for_write();
548 this->bake_to_runtime__AttributeStorage(mesh.attribute_storage.wrap());
550 &mesh.mat, &mesh.totcol, std::move(mesh.runtime->bake_materials), data_block_map_);
552 if (geometry.has_curves()) {
553 Curves &curves = *geometry.get_curves_for_write();
554 this->bake_to_runtime__AttributeStorage(curves.geometry.attribute_storage.wrap());
557 std::move(curves.geometry.runtime->bake_materials),
560 if (geometry.has_pointcloud()) {
561 PointCloud &pointcloud = *geometry.get_pointcloud_for_write();
562 this->bake_to_runtime__AttributeStorage(pointcloud.attribute_storage.wrap());
565 std::move(pointcloud.runtime->bake_materials),
568 if (geometry.has_grease_pencil()) {
569 GreasePencil &grease_pencil = *geometry.get_grease_pencil_for_write();
570 this->bake_to_runtime__AttributeStorage(grease_pencil.attribute_storage.wrap());
572 &grease_pencil.material_array_num,
573 std::move(grease_pencil.runtime->bake_materials),
575 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
580 this->bake_to_runtime__AttributeStorage(
581 drawing.strokes_for_write().attribute_storage.wrap());
584 if (geometry.has_volume()) {
585 Volume &volume = *geometry.get_volume_for_write();
587 &volume.mat, &volume.totcol, std::move(volume.runtime->bake_materials), data_block_map_);
591 void bake_to_runtime__AttributeStorage(AttributeStorage &attributes)
593 Vector<std::pair<std::string, std::string>> attributes_to_rename;
594 for (
const Attribute &
attribute : attributes) {
595 const StringRef attribute_name =
attribute.name();
597 attributes_to_rename.append(
598 {attribute_name, this->get_anonymous_attribute_name(attribute_name)});
601 for (
const std::pair<std::string, std::string> &attribute_to_rename : attributes_to_rename) {
602 attributes.rename(attribute_to_rename.first, attribute_to_rename.second);
606 void bake_to_runtime__Bundle(nodes::Bundle &bundle)
608 for (
auto &&item : bundle.items()) {
609 if (
auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value)) {
610 this->bake_to_runtime__SocketValueVariant(socket_value->value, item.key.ustr().ref());
615 void bake_to_runtime__GList(nodes::GList &list)
617 const CPPType &list_cpp_type = list.cpp_type();
618 if (list_cpp_type.is<SocketValueVariant>()) {
619 list.typed<SocketValueVariant>().foreach_for_write([&](SocketValueVariant &value_variant) {
620 this->bake_to_runtime__SocketValueVariant(value_variant,
TIP_(
"List Item"));
623 else if (list_cpp_type.is<GeometrySet>()) {
624 list.typed<GeometrySet>().foreach_for_write(
625 [&](GeometrySet &geometry) { this->bake_to_runtime__GeometrySet(geometry); });
630 this->bake_to_runtime__Bundle(bundle_ptr.ensure_mutable_inplace());
636 std::string get_anonymous_attribute_name(
const StringRef bake_attribute_name)
638 return runtime_name_by_bake_attribute_.lookup_or_add_cb(bake_attribute_name, [&]() {
640 bake_attribute_name);