WIP: Pass on color_attribute metadata through instancing, only for mesh #107066

Draft
Martijn Versteegh wants to merge 5 commits from Baardaap/blender:pass_active_color_to_instances into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
4 changed files with 48 additions and 1 deletions

View File

@ -103,7 +103,8 @@ class Instances {
mutable blender::Array<int> almost_unique_ids_;
CustomDataAttributes attributes_;
std::string default_color_attribute_;
std::string active_color_attribute_;
public:
Instances() = default;
Instances(const Instances &other);
@ -176,6 +177,10 @@ class Instances {
bool owns_direct_data() const;
void ensure_owns_direct_data();
void set_color_attribute_metadata(const char *default_color_attribute, const char *active_color_attribute);
const std::string &default_color_attribute_name() const { return default_color_attribute_; }
const std::string &active_color_attribute_name() const { return active_color_attribute_; }
};
/* -------------------------------------------------------------------- */

View File

@ -330,4 +330,14 @@ Span<int> Instances::almost_unique_ids() const
return almost_unique_ids_;
}
void Instances::set_color_attribute_metadata(const char *default_color_attribute, const char *active_color_attribute)
{
if (default_color_attribute) {
default_color_attribute_ = default_color_attribute;
}
if (active_color_attribute) {
active_color_attribute_ = active_color_attribute;
}
}
} // namespace blender::bke

View File

@ -206,6 +206,8 @@ struct AllMeshesInfo {
/** True if we know that there are no loose edges in any of the input meshes. */
bool no_loose_edges_hint = false;
bool no_loose_verts_hint = false;
char const *active_color_attribute = nullptr;
char const *default_color_attribute = nullptr;
};
struct AllCurvesInfo {
@ -902,6 +904,13 @@ static AllMeshesInfo preprocess_meshes(const bke::GeometrySet &geometry_set,
info.materials.add(material);
}
}
if (info.default_color_attribute == nullptr) {
info.default_color_attribute = mesh->default_color_attribute;
}
if (info.active_color_attribute == nullptr) {
info.active_color_attribute = mesh->active_color_attribute;
}
}
info.create_material_index_attribute |= info.materials.size() > 1;
info.realize_info.reinitialize(info.order.size());
@ -1558,6 +1567,23 @@ bke::GeometrySet realize_instances(bke::GeometrySet geometry_set,
new_geometry_set.add(*gather_info.r_tasks.first_edit_data);
}
if (new_geometry_set.has_mesh())
{
Mesh *dst_mesh = new_geometry_set.get_mesh_for_write();
/* Get the default color attribute from the instance domain, this
* corresponds to the default specified on the instancer. */
const std::string &default_color_attribute = geometry_set.get_instances_for_read()->default_color_attribute_name();
if (default_color_attribute.size()) {
BKE_id_attributes_default_color_set(&dst_mesh->id, default_color_attribute.c_str());
}
const std::string &active_color_attribute = geometry_set.get_instances_for_read()->active_color_attribute_name();
if (active_color_attribute.size()) {
BKE_id_attributes_active_color_set(&dst_mesh->id, active_color_attribute.c_str());
}
}
return new_geometry_set;
}

View File

@ -15,6 +15,8 @@
#include "BKE_attribute_math.hh"
#include "BKE_instances.hh"
#include "DNA_mesh_types.h"
#include "node_geometry_util.hh"
namespace blender::nodes::node_geo_instance_on_points_cc {
@ -218,6 +220,10 @@ static void node_geo_exec(GeoNodeExecParams params)
instance,

geometry_set.get_mesh_for_read() is a simpler way of writing this.

`geometry_set.get_mesh_for_read()` is a simpler way of writing this.
params,
attributes_to_propagate);
if (type == GeometryComponent::Type::Mesh) {
const Mesh *src_mesh = geometry_set.get_mesh_for_read();
dst_instances->set_color_attribute_metadata(src_mesh->default_color_attribute, src_mesh->active_color_attribute);
}
}
}
geometry_set.remove_geometry_during_modify();