2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-07-28 18:30:32 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
|
|
|
|
|
#include "BKE_bake_items_socket.hh"
|
|
|
|
|
#include "BKE_geometry_fields.hh"
|
2023-12-17 14:00:07 +01:00
|
|
|
#include "BKE_node.hh"
|
|
|
|
|
#include "BKE_node_socket_value.hh"
|
2024-04-23 18:56:41 +02:00
|
|
|
#include "BKE_volume_grid.hh"
|
2023-07-28 18:30:32 +02:00
|
|
|
|
2025-04-03 15:44:06 +02:00
|
|
|
#include "NOD_geometry_nodes_bundle.hh"
|
|
|
|
|
|
2023-09-03 13:37:24 +02:00
|
|
|
namespace blender::bke::bake {
|
2023-07-28 18:30:32 +02:00
|
|
|
|
2025-03-04 11:00:27 +01:00
|
|
|
static void capture_field_on_geometry_components(GeometrySet &geometry,
|
|
|
|
|
const fn::GField &field,
|
|
|
|
|
const AttrDomain domain,
|
|
|
|
|
const StringRef attribute_name)
|
|
|
|
|
{
|
|
|
|
|
if (geometry.has_pointcloud()) {
|
|
|
|
|
PointCloudComponent &component = geometry.get_component_for_write<PointCloudComponent>();
|
|
|
|
|
try_capture_field_on_geometry(component, attribute_name, domain, field);
|
|
|
|
|
}
|
|
|
|
|
if (geometry.has_mesh()) {
|
|
|
|
|
MeshComponent &component = geometry.get_component_for_write<MeshComponent>();
|
|
|
|
|
try_capture_field_on_geometry(component, attribute_name, domain, field);
|
|
|
|
|
}
|
|
|
|
|
if (geometry.has_curves()) {
|
|
|
|
|
CurveComponent &component = geometry.get_component_for_write<CurveComponent>();
|
|
|
|
|
try_capture_field_on_geometry(component, attribute_name, domain, field);
|
|
|
|
|
}
|
|
|
|
|
if (geometry.has_grease_pencil()) {
|
|
|
|
|
GreasePencilComponent &component = geometry.get_component_for_write<GreasePencilComponent>();
|
|
|
|
|
try_capture_field_on_geometry(component, attribute_name, domain, field);
|
|
|
|
|
}
|
|
|
|
|
if (geometry.has_instances()) {
|
|
|
|
|
InstancesComponent &component = geometry.get_component_for_write<InstancesComponent>();
|
|
|
|
|
try_capture_field_on_geometry(component, attribute_name, domain, field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 15:44:06 +02:00
|
|
|
static std::unique_ptr<BakeItem> move_common_socket_value_to_bake_item(
|
|
|
|
|
const bNodeSocketType &stype,
|
2025-08-12 07:59:18 +02:00
|
|
|
SocketValueVariant &socket_value,
|
2025-04-03 15:44:06 +02:00
|
|
|
std::optional<StringRef> name,
|
|
|
|
|
Vector<GeometryBakeItem *> &r_geometry_bake_items)
|
|
|
|
|
{
|
2025-06-02 13:50:07 +02:00
|
|
|
switch (stype.type) {
|
2025-04-03 15:44:06 +02:00
|
|
|
case SOCK_GEOMETRY: {
|
2025-08-12 07:59:18 +02:00
|
|
|
GeometrySet geometry = socket_value.extract<GeometrySet>();
|
2025-04-03 15:44:06 +02:00
|
|
|
auto item = std::make_unique<GeometryBakeItem>(std::move(geometry));
|
|
|
|
|
r_geometry_bake_items.append(item.get());
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
case SOCK_STRING: {
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::make_unique<StringBakeItem>(socket_value.extract<std::string>());
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
|
|
|
|
case SOCK_FLOAT:
|
|
|
|
|
case SOCK_VECTOR:
|
|
|
|
|
case SOCK_INT:
|
|
|
|
|
case SOCK_BOOLEAN:
|
|
|
|
|
case SOCK_ROTATION:
|
|
|
|
|
case SOCK_MATRIX:
|
|
|
|
|
case SOCK_RGBA: {
|
2025-08-12 07:59:18 +02:00
|
|
|
if (socket_value.is_context_dependent_field()) {
|
2025-04-03 15:44:06 +02:00
|
|
|
/* Not supported here because it's not known which geometry this field belongs to. */
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
#ifdef WITH_OPENVDB
|
2025-08-12 07:59:18 +02:00
|
|
|
if (socket_value.is_volume_grid()) {
|
|
|
|
|
bke::GVolumeGrid grid = socket_value.get<bke::GVolumeGrid>();
|
2025-04-03 15:44:06 +02:00
|
|
|
if (name) {
|
|
|
|
|
grid.get_for_write().set_name(*name);
|
|
|
|
|
}
|
|
|
|
|
return std::make_unique<VolumeGridBakeItem>(
|
|
|
|
|
std::make_unique<bke::GVolumeGrid>(std::move(grid)));
|
|
|
|
|
}
|
2025-04-04 10:33:33 +11:00
|
|
|
#else
|
|
|
|
|
UNUSED_VARS(name);
|
2025-04-03 15:44:06 +02:00
|
|
|
#endif
|
2025-04-04 10:33:33 +11:00
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
socket_value.convert_to_single();
|
|
|
|
|
GPointer value = socket_value.get_single_ptr();
|
2025-04-03 15:44:06 +02:00
|
|
|
return std::make_unique<PrimitiveBakeItem>(*value.type(), value.get());
|
|
|
|
|
}
|
|
|
|
|
case SOCK_BUNDLE: {
|
2025-08-12 07:59:18 +02:00
|
|
|
nodes::BundlePtr bundle_ptr = socket_value.extract<nodes::BundlePtr>();
|
2025-04-03 15:44:06 +02:00
|
|
|
auto bundle_bake_item = std::make_unique<BundleBakeItem>();
|
|
|
|
|
if (bundle_ptr) {
|
|
|
|
|
const nodes::Bundle &bundle = *bundle_ptr;
|
|
|
|
|
for (const nodes::Bundle::StoredItem &bundle_item : bundle.items()) {
|
2025-08-12 07:59:18 +02:00
|
|
|
if (const auto *item_socket_value = std::get_if<nodes::BundleItemSocketValue>(
|
2025-07-28 19:05:18 +02:00
|
|
|
&bundle_item.value.value))
|
2025-04-03 15:44:06 +02:00
|
|
|
{
|
2025-08-12 09:12:08 +02:00
|
|
|
SocketValueVariant value_variant = item_socket_value->value;
|
2025-07-28 19:05:18 +02:00
|
|
|
if (std::unique_ptr<BakeItem> bake_item = move_common_socket_value_to_bake_item(
|
2025-08-12 09:12:08 +02:00
|
|
|
*item_socket_value->type, value_variant, std::nullopt, r_geometry_bake_items))
|
2025-07-28 19:05:18 +02:00
|
|
|
{
|
2025-08-12 07:59:18 +02:00
|
|
|
bundle_bake_item->items.append(
|
|
|
|
|
BundleBakeItem::Item{bundle_item.key,
|
|
|
|
|
BundleBakeItem::SocketValue{item_socket_value->type->idname,
|
|
|
|
|
std::move(bake_item)}});
|
2025-07-28 19:05:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (const auto *internal_value = std::get_if<nodes::BundleItemInternalValue>(
|
|
|
|
|
&bundle_item.value.value))
|
|
|
|
|
{
|
|
|
|
|
const ImplicitSharingInfo *sharing_info = internal_value->value.get();
|
|
|
|
|
if (sharing_info) {
|
|
|
|
|
sharing_info->add_user();
|
|
|
|
|
}
|
2025-04-03 15:44:06 +02:00
|
|
|
bundle_bake_item->items.append(BundleBakeItem::Item{
|
2025-07-28 19:05:18 +02:00
|
|
|
bundle_item.key,
|
|
|
|
|
BundleBakeItem::InternalValue{ImplicitSharingPtr<>{sharing_info}}});
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bundle_bake_item;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
Array<std::unique_ptr<BakeItem>> move_socket_values_to_bake_items(
|
|
|
|
|
MutableSpan<SocketValueVariant> socket_values,
|
|
|
|
|
const BakeSocketConfig &config,
|
|
|
|
|
BakeDataBlockMap *data_block_map)
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
|
|
|
|
BLI_assert(socket_values.size() == config.types.size());
|
|
|
|
|
BLI_assert(socket_values.size() == config.geometries_by_attribute.size());
|
|
|
|
|
|
|
|
|
|
Array<std::unique_ptr<BakeItem>> bake_items(socket_values.size());
|
|
|
|
|
|
2025-03-04 11:00:27 +01:00
|
|
|
Vector<GeometryBakeItem *> geometry_bake_items;
|
|
|
|
|
|
2023-07-28 18:30:32 +02:00
|
|
|
/* Create geometry bake items first because they are used for field evaluation. */
|
|
|
|
|
for (const int i : socket_values.index_range()) {
|
|
|
|
|
const eNodeSocketDatatype socket_type = config.types[i];
|
|
|
|
|
if (socket_type != SOCK_GEOMETRY) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
GeometrySet geometry = socket_values[i].extract<GeometrySet>();
|
2025-03-04 11:00:27 +01:00
|
|
|
auto geometry_item = std::make_unique<GeometryBakeItem>(std::move(geometry));
|
|
|
|
|
geometry_bake_items.append(geometry_item.get());
|
|
|
|
|
bake_items[i] = std::move(geometry_item);
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const int i : socket_values.index_range()) {
|
|
|
|
|
const eNodeSocketDatatype socket_type = config.types[i];
|
2025-04-03 15:44:06 +02:00
|
|
|
const bNodeSocketType &stype = *node_socket_type_find_static(socket_type);
|
2025-08-12 07:59:18 +02:00
|
|
|
SocketValueVariant &socket_value = socket_values[i];
|
2023-07-28 18:30:32 +02:00
|
|
|
switch (socket_type) {
|
|
|
|
|
case SOCK_GEOMETRY: {
|
|
|
|
|
/* Handled already. */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SOCK_STRING: {
|
2025-04-03 15:44:06 +02:00
|
|
|
bake_items[i] = move_common_socket_value_to_bake_item(
|
|
|
|
|
stype, socket_value, config.names[i], geometry_bake_items);
|
2023-07-28 18:30:32 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SOCK_FLOAT:
|
|
|
|
|
case SOCK_VECTOR:
|
|
|
|
|
case SOCK_INT:
|
|
|
|
|
case SOCK_BOOLEAN:
|
|
|
|
|
case SOCK_ROTATION:
|
2024-02-13 18:59:36 +01:00
|
|
|
case SOCK_MATRIX:
|
2023-07-28 18:30:32 +02:00
|
|
|
case SOCK_RGBA: {
|
2025-08-12 07:59:18 +02:00
|
|
|
if (socket_value.is_context_dependent_field()) {
|
|
|
|
|
const fn::GField &field = socket_value.get<fn::GField>();
|
2023-12-20 13:13:16 -05:00
|
|
|
const AttrDomain domain = config.domains[i];
|
2023-12-17 14:00:07 +01:00
|
|
|
const std::string attribute_name = ".bake_" + std::to_string(i);
|
|
|
|
|
const Span<int> geometry_indices = config.geometries_by_attribute[i];
|
|
|
|
|
for (const int geometry_i : geometry_indices) {
|
|
|
|
|
BLI_assert(config.types[geometry_i] == SOCK_GEOMETRY);
|
|
|
|
|
GeometrySet &geometry =
|
|
|
|
|
static_cast<GeometryBakeItem *>(bake_items[geometry_i].get())->geometry;
|
2025-03-04 11:00:27 +01:00
|
|
|
capture_field_on_geometry_components(geometry, field, domain, attribute_name);
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2023-12-17 14:00:07 +01:00
|
|
|
bake_items[i] = std::make_unique<AttributeBakeItem>(attribute_name);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2025-04-03 15:44:06 +02:00
|
|
|
bake_items[i] = move_common_socket_value_to_bake_item(
|
|
|
|
|
stype, socket_value, config.names[i], geometry_bake_items);
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-03 15:44:06 +02:00
|
|
|
case SOCK_BUNDLE: {
|
|
|
|
|
bake_items[i] = move_common_socket_value_to_bake_item(
|
|
|
|
|
stype, socket_value, config.names[i], geometry_bake_items);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-07-28 18:30:32 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cleanup geometries after fields have been evaluated. */
|
2025-03-04 11:00:27 +01:00
|
|
|
for (GeometryBakeItem *geometry_item : geometry_bake_items) {
|
|
|
|
|
GeometryBakeItem::prepare_geometry_for_bake(geometry_item->geometry, data_block_map);
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-02 10:02:25 +01:00
|
|
|
for (const int i : bake_items.index_range()) {
|
|
|
|
|
if (std::unique_ptr<BakeItem> &item = bake_items[i]) {
|
|
|
|
|
item->name = config.names[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 18:30:32 +02:00
|
|
|
return bake_items;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 15:44:06 +02:00
|
|
|
/**
|
|
|
|
|
* \return True if #r_value has been constructed.
|
|
|
|
|
*/
|
2025-08-12 07:59:18 +02:00
|
|
|
[[nodiscard]] static std::optional<SocketValueVariant> copy_bake_item_to_socket_value(
|
2023-07-28 18:30:32 +02:00
|
|
|
const BakeItem &bake_item,
|
|
|
|
|
const eNodeSocketDatatype socket_type,
|
2024-09-03 15:38:51 +02:00
|
|
|
const FunctionRef<std::shared_ptr<AttributeFieldInput>(const CPPType &type)>
|
2023-07-28 18:30:32 +02:00
|
|
|
make_attribute_field,
|
2025-08-11 08:03:43 +02:00
|
|
|
BakeDataBlockMap *data_block_map,
|
2025-08-12 07:59:18 +02:00
|
|
|
Map<std::string, std::string> &r_attribute_map)
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
|
|
|
|
switch (socket_type) {
|
|
|
|
|
case SOCK_GEOMETRY: {
|
|
|
|
|
if (const auto *item = dynamic_cast<const GeometryBakeItem *>(&bake_item)) {
|
2025-08-11 15:18:08 +02:00
|
|
|
bke::GeometrySet geometry = item->geometry;
|
|
|
|
|
GeometryBakeItem::try_restore_data_blocks(geometry, data_block_map);
|
2025-08-12 07:59:18 +02:00
|
|
|
return SocketValueVariant::From(std::move(geometry));
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
case SOCK_FLOAT:
|
|
|
|
|
case SOCK_VECTOR:
|
|
|
|
|
case SOCK_INT:
|
|
|
|
|
case SOCK_BOOLEAN:
|
|
|
|
|
case SOCK_ROTATION:
|
2024-02-13 18:59:36 +01:00
|
|
|
case SOCK_MATRIX:
|
2023-07-28 18:30:32 +02:00
|
|
|
case SOCK_RGBA: {
|
2023-12-17 14:00:07 +01:00
|
|
|
const CPPType &base_type = *socket_type_to_geo_nodes_base_cpp_type(socket_type);
|
2023-07-28 18:30:32 +02:00
|
|
|
if (const auto *item = dynamic_cast<const PrimitiveBakeItem *>(&bake_item)) {
|
|
|
|
|
if (item->type() == base_type) {
|
2025-08-12 07:59:18 +02:00
|
|
|
SocketValueVariant value_variant;
|
|
|
|
|
value_variant.store_single(socket_type, item->value());
|
|
|
|
|
return value_variant;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
if (const auto *item = dynamic_cast<const AttributeBakeItem *>(&bake_item)) {
|
2025-04-03 15:44:06 +02:00
|
|
|
if (!make_attribute_field) {
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
2024-09-03 15:38:51 +02:00
|
|
|
std::shared_ptr<AttributeFieldInput> attribute_field = make_attribute_field(base_type);
|
|
|
|
|
r_attribute_map.add(item->name(), attribute_field->attribute_name());
|
2023-07-28 18:30:32 +02:00
|
|
|
fn::GField field{attribute_field};
|
2025-08-12 07:59:18 +02:00
|
|
|
return SocketValueVariant::From(std::move(field));
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2024-04-23 18:56:41 +02:00
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
if (const auto *item = dynamic_cast<const VolumeGridBakeItem *>(&bake_item)) {
|
|
|
|
|
const GVolumeGrid &grid = *item->grid;
|
|
|
|
|
const VolumeGridType grid_type = grid->grid_type();
|
|
|
|
|
const std::optional<eNodeSocketDatatype> grid_socket_type = grid_type_to_socket_type(
|
|
|
|
|
grid_type);
|
|
|
|
|
if (!grid_socket_type) {
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2024-04-23 18:56:41 +02:00
|
|
|
}
|
|
|
|
|
if (grid_socket_type == socket_type) {
|
2025-08-12 07:59:18 +02:00
|
|
|
return bke::SocketValueVariant::From(*item->grid);
|
2024-04-23 18:56:41 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2024-04-23 18:56:41 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
case SOCK_STRING: {
|
|
|
|
|
if (const auto *item = dynamic_cast<const StringBakeItem *>(&bake_item)) {
|
2025-08-12 07:59:18 +02:00
|
|
|
return SocketValueVariant(std::string(item->value()));
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2025-04-03 15:44:06 +02:00
|
|
|
case SOCK_BUNDLE: {
|
|
|
|
|
if (const auto *item = dynamic_cast<const BundleBakeItem *>(&bake_item)) {
|
|
|
|
|
nodes::BundlePtr bundle_ptr = nodes::Bundle::create();
|
|
|
|
|
nodes::Bundle &bundle = const_cast<nodes::Bundle &>(*bundle_ptr);
|
|
|
|
|
for (const BundleBakeItem::Item &item : item->items) {
|
2025-07-28 19:05:18 +02:00
|
|
|
if (const auto *socket_value = std::get_if<BundleBakeItem::SocketValue>(&item.value)) {
|
|
|
|
|
const bNodeSocketType *stype = node_socket_type_find(socket_value->socket_idname);
|
|
|
|
|
if (!stype) {
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2025-07-28 19:05:18 +02:00
|
|
|
}
|
2025-08-12 16:03:09 +02:00
|
|
|
if (!stype->geometry_nodes_default_value) {
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2025-07-28 19:05:18 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
if (std::optional<SocketValueVariant> child_value_variant =
|
|
|
|
|
copy_bake_item_to_socket_value(
|
|
|
|
|
*socket_value->value, stype->type, {}, data_block_map, r_attribute_map))
|
2025-07-28 19:05:18 +02:00
|
|
|
{
|
2025-08-12 09:12:08 +02:00
|
|
|
bundle.add(item.key, nodes::BundleItemSocketValue{stype, *child_value_variant});
|
2025-08-12 07:59:18 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return std::nullopt;
|
2025-07-28 19:05:18 +02:00
|
|
|
}
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
2025-07-28 19:05:18 +02:00
|
|
|
if (const auto *internal_value = std::get_if<BundleBakeItem::InternalValue>(&item.value))
|
2025-04-03 15:44:06 +02:00
|
|
|
{
|
2025-07-28 19:05:18 +02:00
|
|
|
const auto *internal_data = dynamic_cast<const nodes::BundleItemInternalValueMixin *>(
|
|
|
|
|
internal_value->value.get());
|
|
|
|
|
if (!internal_data) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
internal_data->add_user();
|
|
|
|
|
bundle.add(item.key,
|
|
|
|
|
nodes::BundleItemInternalValue{ImplicitSharingPtr{internal_data}});
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return bke::SocketValueVariant::From(std::move(bundle_ptr));
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2025-04-03 15:44:06 +02:00
|
|
|
}
|
2023-07-28 18:30:32 +02:00
|
|
|
default:
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
return std::nullopt;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rename_attributes(const Span<GeometrySet *> geometries,
|
2024-09-03 15:38:51 +02:00
|
|
|
const Map<std::string, std::string> &attribute_map)
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
|
|
|
|
for (GeometrySet *geometry : geometries) {
|
|
|
|
|
for (const GeometryComponent::Type type : {GeometryComponent::Type::Mesh,
|
|
|
|
|
GeometryComponent::Type::Curve,
|
2024-07-16 10:11:48 +02:00
|
|
|
GeometryComponent::Type::GreasePencil,
|
2023-07-28 18:30:32 +02:00
|
|
|
GeometryComponent::Type::PointCloud,
|
|
|
|
|
GeometryComponent::Type::Instance})
|
|
|
|
|
{
|
|
|
|
|
if (!geometry->has(type)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* Avoid write access on the geometry when unnecessary to avoid copying data-blocks. */
|
2023-08-03 17:09:18 +02:00
|
|
|
const AttributeAccessor attributes_read_only = *geometry->get_component(type)->attributes();
|
2023-07-28 18:30:32 +02:00
|
|
|
if (std::none_of(attribute_map.keys().begin(),
|
|
|
|
|
attribute_map.keys().end(),
|
|
|
|
|
[&](const StringRef name) { return attributes_read_only.contains(name); }))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometryComponent &component = geometry->get_component_for_write(type);
|
|
|
|
|
MutableAttributeAccessor attributes = *component.attributes_for_write();
|
2024-09-03 15:38:51 +02:00
|
|
|
for (const MapItem<std::string, std::string> &attribute_item : attribute_map.items()) {
|
|
|
|
|
attributes.rename(attribute_item.key, attribute_item.value);
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
static SocketValueVariant default_initialize_socket_value(const eNodeSocketDatatype socket_type)
|
2024-01-19 11:19:22 +01:00
|
|
|
{
|
2025-03-14 10:16:50 +01:00
|
|
|
const bke::bNodeSocketType *typeinfo = bke::node_socket_type_find_static(socket_type);
|
2025-08-12 08:19:40 +02:00
|
|
|
return *static_cast<const SocketValueVariant *>(typeinfo->geometry_nodes_default_value);
|
2024-01-19 11:19:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
Vector<SocketValueVariant> move_bake_items_to_socket_values(
|
2023-07-28 18:30:32 +02:00
|
|
|
const Span<BakeItem *> bake_items,
|
|
|
|
|
const BakeSocketConfig &config,
|
Geometry Nodes: support baking data block references
With this patch, materials are kept intact in simulation zones and bake nodes
without any additional user action.
This implements the design proposed in #108410 to support referencing
data-blocks (only materials for now) in the baked data. The task also describes
why this is not a trivial issue. A previous attempt was implemented in #109703
but it didn't work well-enough.
The solution is to have an explicit `name (+ library name) -> data-block`
mapping that is stored in the modifier for each bake node and simulation zone.
The `library name` is necessary for it to be unique within a .blend file. Note
that this refers to the name of the `Library` data-block and not a file path.
The baked data only contains the names of the used data-blocks. When the baked
data is loaded, the correct material data-block is looked up from the mapping.
### Automatic Mapping Generation
The most tricky aspect of this approach is to make it feel mostly automatic.
From the user point-of-view, it should just work. Therefore, we don't want the
user to have to create the mapping manually in the majority of cases. Creating
the mapping automatically is difficult because the data-blocks that should
become part of the mapping are only known during depsgraph evaluation. So we
somehow have to gather the missing data blocks during evaluation and then write
the new mappings back to the original data.
While writing back to original data is something we do in some cases already,
the situation here is different, because we are actually creating new relations
between data-blocks. This also means that we'll have to do user-counting. Since
user counts in data-blocks are *not* atomic, we can't do that from multiple
threads at the same time. Also, under some circumstances, it may be necessary to
trigger depsgraph evaluation again after the write-back because it actually
affects the result.
To solve this, a small new API is added in `DEG_depsgraph_writeback_sync.hh`. It
allows gathering tasks which write back to original data in a synchronous way
which may also require a reevaluation.
### Accessing the Mapping
A new `BakeDataBlockMap` is passed to geometry nodes evaluation by the modifier.
This map allows getting the `ID` pointer that should be used for a specific
data-block name that is stored in baked data. It's also used to gather all the
missing data mappings during evaluation.
### Weak ID References
The baked/cached geometries may have references to other data-blocks (currently
only materials, but in the future also e.g. instanced objects/collections).
However, the pointers of these data-blocks are not stable over time. That is
especially true when storing/loading the data from disk, but also just when
playing back the animation. Therefore, the used data-blocks have to referenced
in a different way at run-time.
This is solved by adding `std::unique_ptr<bake::BakeMaterialsList>` to the
run-time data of various geometry data-blocks. If the data-block is cached over
a longer period of time (such that material pointers can't be used directly), it
stores the material name (+ library name) used by each material slot. When the
geometry is used again, the material pointers are restored using these weak name
references and the `BakeDataBlockMap`.
### Manual Mapping Management
There is a new `Data-Blocks` panel in the bake settings in the node editor
sidebar that allows inspecting and modifying the data-blocks that are used when
baking. The user can change what data-block a specific name is mapped to.
Pull Request: https://projects.blender.org/blender/blender/pulls/117043
2024-02-01 09:21:55 +01:00
|
|
|
BakeDataBlockMap *data_block_map,
|
2025-08-12 07:59:18 +02:00
|
|
|
FunctionRef<std::shared_ptr<AttributeFieldInput>(int, const CPPType &)> make_attribute_field)
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
2024-09-03 15:38:51 +02:00
|
|
|
Map<std::string, std::string> attribute_map;
|
2025-08-12 07:59:18 +02:00
|
|
|
Vector<SocketValueVariant> socket_values;
|
|
|
|
|
socket_values.reserve(bake_items.size());
|
2023-07-28 18:30:32 +02:00
|
|
|
|
|
|
|
|
for (const int i : bake_items.index_range()) {
|
|
|
|
|
const eNodeSocketDatatype socket_type = config.types[i];
|
|
|
|
|
BakeItem *bake_item = bake_items[i];
|
|
|
|
|
if (bake_item == nullptr) {
|
2025-08-12 07:59:18 +02:00
|
|
|
socket_values.append(default_initialize_socket_value(socket_type));
|
2023-07-28 18:30:32 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
if (std::optional<SocketValueVariant> socket_value = copy_bake_item_to_socket_value(
|
2023-07-28 18:30:32 +02:00
|
|
|
*bake_item,
|
|
|
|
|
socket_type,
|
|
|
|
|
[&](const CPPType &attr_type) { return make_attribute_field(i, attr_type); },
|
2025-08-11 08:03:43 +02:00
|
|
|
data_block_map,
|
2025-08-12 07:59:18 +02:00
|
|
|
attribute_map))
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
2025-08-12 07:59:18 +02:00
|
|
|
socket_values.append(std::move(*socket_value));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
socket_values.append(default_initialize_socket_value(socket_type));
|
2023-07-28 18:30:32 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (socket_type == SOCK_GEOMETRY) {
|
|
|
|
|
auto &item = *static_cast<GeometryBakeItem *>(bake_item);
|
|
|
|
|
item.geometry.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
for (SocketValueVariant &socket_value : socket_values) {
|
|
|
|
|
if (socket_value.valid_for_socket(SOCK_GEOMETRY)) {
|
|
|
|
|
GeometrySet *geometry = socket_value.get_single_ptr().get<GeometrySet>();
|
|
|
|
|
rename_attributes({geometry}, attribute_map);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return socket_values;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
Vector<SocketValueVariant> copy_bake_items_to_socket_values(
|
2023-07-28 18:30:32 +02:00
|
|
|
const Span<const BakeItem *> bake_items,
|
|
|
|
|
const BakeSocketConfig &config,
|
Geometry Nodes: support baking data block references
With this patch, materials are kept intact in simulation zones and bake nodes
without any additional user action.
This implements the design proposed in #108410 to support referencing
data-blocks (only materials for now) in the baked data. The task also describes
why this is not a trivial issue. A previous attempt was implemented in #109703
but it didn't work well-enough.
The solution is to have an explicit `name (+ library name) -> data-block`
mapping that is stored in the modifier for each bake node and simulation zone.
The `library name` is necessary for it to be unique within a .blend file. Note
that this refers to the name of the `Library` data-block and not a file path.
The baked data only contains the names of the used data-blocks. When the baked
data is loaded, the correct material data-block is looked up from the mapping.
### Automatic Mapping Generation
The most tricky aspect of this approach is to make it feel mostly automatic.
From the user point-of-view, it should just work. Therefore, we don't want the
user to have to create the mapping manually in the majority of cases. Creating
the mapping automatically is difficult because the data-blocks that should
become part of the mapping are only known during depsgraph evaluation. So we
somehow have to gather the missing data blocks during evaluation and then write
the new mappings back to the original data.
While writing back to original data is something we do in some cases already,
the situation here is different, because we are actually creating new relations
between data-blocks. This also means that we'll have to do user-counting. Since
user counts in data-blocks are *not* atomic, we can't do that from multiple
threads at the same time. Also, under some circumstances, it may be necessary to
trigger depsgraph evaluation again after the write-back because it actually
affects the result.
To solve this, a small new API is added in `DEG_depsgraph_writeback_sync.hh`. It
allows gathering tasks which write back to original data in a synchronous way
which may also require a reevaluation.
### Accessing the Mapping
A new `BakeDataBlockMap` is passed to geometry nodes evaluation by the modifier.
This map allows getting the `ID` pointer that should be used for a specific
data-block name that is stored in baked data. It's also used to gather all the
missing data mappings during evaluation.
### Weak ID References
The baked/cached geometries may have references to other data-blocks (currently
only materials, but in the future also e.g. instanced objects/collections).
However, the pointers of these data-blocks are not stable over time. That is
especially true when storing/loading the data from disk, but also just when
playing back the animation. Therefore, the used data-blocks have to referenced
in a different way at run-time.
This is solved by adding `std::unique_ptr<bake::BakeMaterialsList>` to the
run-time data of various geometry data-blocks. If the data-block is cached over
a longer period of time (such that material pointers can't be used directly), it
stores the material name (+ library name) used by each material slot. When the
geometry is used again, the material pointers are restored using these weak name
references and the `BakeDataBlockMap`.
### Manual Mapping Management
There is a new `Data-Blocks` panel in the bake settings in the node editor
sidebar that allows inspecting and modifying the data-blocks that are used when
baking. The user can change what data-block a specific name is mapped to.
Pull Request: https://projects.blender.org/blender/blender/pulls/117043
2024-02-01 09:21:55 +01:00
|
|
|
BakeDataBlockMap *data_block_map,
|
2025-08-12 07:59:18 +02:00
|
|
|
FunctionRef<std::shared_ptr<AttributeFieldInput>(int, const CPPType &)> make_attribute_field)
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
2024-09-03 15:38:51 +02:00
|
|
|
Map<std::string, std::string> attribute_map;
|
2025-08-12 07:59:18 +02:00
|
|
|
Vector<SocketValueVariant> socket_values;
|
|
|
|
|
socket_values.reserve(bake_items.size());
|
2023-07-28 18:30:32 +02:00
|
|
|
|
|
|
|
|
for (const int i : bake_items.index_range()) {
|
|
|
|
|
const eNodeSocketDatatype socket_type = config.types[i];
|
|
|
|
|
const BakeItem *bake_item = bake_items[i];
|
|
|
|
|
if (bake_item == nullptr) {
|
2025-08-12 07:59:18 +02:00
|
|
|
socket_values.append(default_initialize_socket_value(socket_type));
|
2023-07-28 18:30:32 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
|
|
|
|
|
if (std::optional<SocketValueVariant> socket_value = copy_bake_item_to_socket_value(
|
2023-07-28 18:30:32 +02:00
|
|
|
*bake_item,
|
|
|
|
|
socket_type,
|
|
|
|
|
[&](const CPPType &attr_type) { return make_attribute_field(i, attr_type); },
|
2025-08-11 08:03:43 +02:00
|
|
|
data_block_map,
|
2025-08-12 07:59:18 +02:00
|
|
|
attribute_map))
|
2023-07-28 18:30:32 +02:00
|
|
|
{
|
2025-08-12 07:59:18 +02:00
|
|
|
socket_values.append(std::move(*socket_value));
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
2025-08-12 07:59:18 +02:00
|
|
|
else {
|
|
|
|
|
socket_values.append(default_initialize_socket_value(socket_type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (SocketValueVariant &socket_value : socket_values) {
|
|
|
|
|
if (socket_value.valid_for_socket(SOCK_GEOMETRY)) {
|
|
|
|
|
GeometrySet *geometry = socket_value.get_single_ptr().get<GeometrySet>();
|
|
|
|
|
rename_attributes({geometry}, attribute_map);
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:59:18 +02:00
|
|
|
return socket_values;
|
2023-07-28 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-03 13:37:24 +02:00
|
|
|
} // namespace blender::bke::bake
|