|
|
|
@@ -27,6 +27,7 @@
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_array.hh"
|
|
|
|
|
#include "BLI_float3.hh"
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
|
#include "BLI_multi_value_map.hh"
|
|
|
|
@@ -88,10 +89,12 @@
|
|
|
|
|
#include "NOD_derived_node_tree.hh"
|
|
|
|
|
#include "NOD_geometry.h"
|
|
|
|
|
#include "NOD_geometry_nodes_eval_log.hh"
|
|
|
|
|
#include "NOD_node_declaration.hh"
|
|
|
|
|
|
|
|
|
|
#include "FN_field.hh"
|
|
|
|
|
#include "FN_multi_function.hh"
|
|
|
|
|
|
|
|
|
|
using blender::Array;
|
|
|
|
|
using blender::ColorGeometry4f;
|
|
|
|
|
using blender::destruct_ptr;
|
|
|
|
|
using blender::float3;
|
|
|
|
@@ -105,7 +108,11 @@ using blender::StringRefNull;
|
|
|
|
|
using blender::Vector;
|
|
|
|
|
using blender::fn::GMutablePointer;
|
|
|
|
|
using blender::fn::GPointer;
|
|
|
|
|
using blender::nodes::FieldInferencingInterface;
|
|
|
|
|
using blender::nodes::GeoNodeExecParams;
|
|
|
|
|
using blender::nodes::InputSocketFieldType;
|
|
|
|
|
using blender::nodes::OutputFieldDependency;
|
|
|
|
|
using blender::nodes::OutputSocketFieldType;
|
|
|
|
|
using blender::threading::EnumerableThreadSpecific;
|
|
|
|
|
using namespace blender::fn::multi_function_types;
|
|
|
|
|
using namespace blender::nodes::derived_node_tree_types;
|
|
|
|
@@ -298,12 +305,44 @@ static bool logging_enabled(const ModifierEvalContext *ctx)
|
|
|
|
|
static const std::string use_attribute_suffix = "_use_attribute";
|
|
|
|
|
static const std::string attribute_name_suffix = "_attribute_name";
|
|
|
|
|
|
|
|
|
|
/* TODO: Use a better check for this. */
|
|
|
|
|
static bool socket_type_can_be_attribute(const bNodeSocket &socket)
|
|
|
|
|
{
|
|
|
|
|
return ELEM(socket.type, SOCK_FLOAT, SOCK_VECTOR, SOCK_BOOLEAN, SOCK_RGBA, SOCK_INT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return Whether using an attribute to input values of this type is supported.
|
|
|
|
|
*/
|
|
|
|
|
static bool socket_type_has_attribute_toggle(const bNodeSocket &socket)
|
|
|
|
|
static bool input_has_attribute_toggle(const bNodeTree &node_tree, const int socket_index)
|
|
|
|
|
{
|
|
|
|
|
return ELEM(socket.type, SOCK_FLOAT, SOCK_VECTOR, SOCK_BOOLEAN, SOCK_RGBA, SOCK_INT);
|
|
|
|
|
if (node_tree.field_inferencing_interface == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
BLI_assert(node_tree.field_inferencing_interface != nullptr);
|
|
|
|
|
const FieldInferencingInterface &field_interface = *node_tree.field_inferencing_interface;
|
|
|
|
|
return field_interface.inputs[socket_index] != InputSocketFieldType::None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Array<bool> inputs_use_attribute(const bNodeTree &node_tree, const NodesModifierData &nmd)
|
|
|
|
|
{
|
|
|
|
|
Vector<bNodeSocket *> inputs = node_tree.inputs;
|
|
|
|
|
Array<bool> result(inputs.size(), false);
|
|
|
|
|
for (const int i : inputs.index_range()) {
|
|
|
|
|
if (!input_has_attribute_toggle(node_tree, i)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const std::string use_attribute_prop_name = inputs[i]->identifier + use_attribute_suffix;
|
|
|
|
|
const IDProperty *use_attribute_prop = IDP_GetPropertyFromGroup(
|
|
|
|
|
nmd.settings.properties, use_attribute_prop_name.c_str());
|
|
|
|
|
if (use_attribute_prop == nullptr) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (IDP_Int(use_attribute_prop) != 0) {
|
|
|
|
|
result[i] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static IDProperty *id_property_create_from_socket(const bNodeSocket &socket)
|
|
|
|
@@ -531,7 +570,8 @@ void MOD_nodes_update_interface(Object *object, NodesModifierData *nmd)
|
|
|
|
|
nmd->settings.properties = IDP_New(IDP_GROUP, &idprop, "Nodes Modifier Settings");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH (bNodeSocket *, socket, &nmd->node_group->inputs) {
|
|
|
|
|
int socket_index;
|
|
|
|
|
LISTBASE_FOREACH_INDEX (bNodeSocket *, socket, &nmd->node_group->inputs, socket_index) {
|
|
|
|
|
IDProperty *new_prop = id_property_create_from_socket(*socket);
|
|
|
|
|
if (new_prop == nullptr) {
|
|
|
|
|
/* Out of the set of supported input sockets, only
|
|
|
|
@@ -562,7 +602,7 @@ void MOD_nodes_update_interface(Object *object, NodesModifierData *nmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (socket_type_has_attribute_toggle(*socket)) {
|
|
|
|
|
if (input_has_attribute_toggle(*nmd->node_group, socket_index)) {
|
|
|
|
|
const std::string use_attribute_id = socket->identifier + use_attribute_suffix;
|
|
|
|
|
const std::string attribute_name_id = socket->identifier + attribute_name_suffix;
|
|
|
|
|
|
|
|
|
@@ -624,37 +664,38 @@ void MOD_nodes_init(Main *bmain, NodesModifierData *nmd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void initialize_group_input(NodesModifierData &nmd,
|
|
|
|
|
const bNodeSocket &socket,
|
|
|
|
|
const OutputSocketRef &socket,
|
|
|
|
|
void *r_value)
|
|
|
|
|
{
|
|
|
|
|
const bNodeSocket &bsocket = *socket.bsocket();
|
|
|
|
|
if (nmd.settings.properties == nullptr) {
|
|
|
|
|
socket.typeinfo->get_geometry_nodes_cpp_value(socket, r_value);
|
|
|
|
|
socket.typeinfo()->get_geometry_nodes_cpp_value(bsocket, r_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const IDProperty *property = IDP_GetPropertyFromGroup(nmd.settings.properties,
|
|
|
|
|
socket.identifier);
|
|
|
|
|
socket.identifier().c_str());
|
|
|
|
|
if (property == nullptr) {
|
|
|
|
|
socket.typeinfo->get_geometry_nodes_cpp_value(socket, r_value);
|
|
|
|
|
socket.typeinfo()->get_geometry_nodes_cpp_value(bsocket, r_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!id_property_type_matches_socket(socket, *property)) {
|
|
|
|
|
socket.typeinfo->get_geometry_nodes_cpp_value(socket, r_value);
|
|
|
|
|
if (!id_property_type_matches_socket(bsocket, *property)) {
|
|
|
|
|
socket.typeinfo()->get_geometry_nodes_cpp_value(bsocket, r_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!socket_type_has_attribute_toggle(socket)) {
|
|
|
|
|
if (!input_has_attribute_toggle(*nmd.node_group, socket.index())) {
|
|
|
|
|
init_socket_cpp_value_from_property(
|
|
|
|
|
*property, static_cast<eNodeSocketDatatype>(socket.type), r_value);
|
|
|
|
|
*property, static_cast<eNodeSocketDatatype>(bsocket.type), r_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IDProperty *property_use_attribute = IDP_GetPropertyFromGroup(
|
|
|
|
|
nmd.settings.properties, (socket.identifier + use_attribute_suffix).c_str());
|
|
|
|
|
nmd.settings.properties, (socket.identifier() + use_attribute_suffix).c_str());
|
|
|
|
|
const IDProperty *property_attribute_name = IDP_GetPropertyFromGroup(
|
|
|
|
|
nmd.settings.properties, (socket.identifier + attribute_name_suffix).c_str());
|
|
|
|
|
nmd.settings.properties, (socket.identifier() + attribute_name_suffix).c_str());
|
|
|
|
|
if (property_use_attribute == nullptr || property_attribute_name == nullptr) {
|
|
|
|
|
init_socket_cpp_value_from_property(
|
|
|
|
|
*property, static_cast<eNodeSocketDatatype>(socket.type), r_value);
|
|
|
|
|
*property, static_cast<eNodeSocketDatatype>(bsocket.type), r_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -662,12 +703,12 @@ static void initialize_group_input(NodesModifierData &nmd,
|
|
|
|
|
if (use_attribute) {
|
|
|
|
|
const StringRef attribute_name{IDP_String(property_attribute_name)};
|
|
|
|
|
auto attribute_input = std::make_shared<blender::bke::AttributeFieldInput>(
|
|
|
|
|
attribute_name, *socket.typeinfo->get_base_cpp_type());
|
|
|
|
|
attribute_name, *socket.typeinfo()->get_base_cpp_type());
|
|
|
|
|
new (r_value) blender::fn::GField(std::move(attribute_input), 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
init_socket_cpp_value_from_property(
|
|
|
|
|
*property, static_cast<eNodeSocketDatatype>(socket.type), r_value);
|
|
|
|
|
*property, static_cast<eNodeSocketDatatype>(bsocket.type), r_value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -778,14 +819,44 @@ static void clear_runtime_data(NodesModifierData *nmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO: Somehow choose which domain to use. */
|
|
|
|
|
static void evalaute_attributes_on_result_component(GeometryComponent &component,
|
|
|
|
|
Span<GMutablePointer> attribute_outputs)
|
|
|
|
|
{
|
|
|
|
|
GeometryComponentFieldContext field_context{component, ATTR_DOMAIN_POINT};
|
|
|
|
|
const int domain_size = component.attribute_domain_size(domain);
|
|
|
|
|
const IndexMask mask{IndexMask(domain_size)};
|
|
|
|
|
|
|
|
|
|
const CustomDataType data_type = bke::cpp_type_to_custom_data_type(field.cpp_type());
|
|
|
|
|
OutputAttribute output_attribute = component.attribute_try_get_for_output_only(
|
|
|
|
|
attribute_id, domain, data_type);
|
|
|
|
|
|
|
|
|
|
fn::FieldEvaluator evaluator{field_context, domain_size};
|
|
|
|
|
evaluator.add_with_destination(field, output_attribute.varray());
|
|
|
|
|
evaluator.evaluate();
|
|
|
|
|
|
|
|
|
|
output_attribute.save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void evaluate_attributes_on_result_geometry(GeometrySet &geometry_set,
|
|
|
|
|
Span<GMutablePointer> attribute_outputs)
|
|
|
|
|
{
|
|
|
|
|
for (const GeometryComponentType type :
|
|
|
|
|
{GEO_COMPONENT_TYPE_MESH, GEO_COMPONENT_TYPE_POINT_CLOUD, GEO_COMPONENT_TYPE_CURVE}) {
|
|
|
|
|
if (geometry_set.has(type)) {
|
|
|
|
|
GeometryComponent &component = geometry_set.get_component_for_write(type);
|
|
|
|
|
evalaute_attributes_on_result_component(component, attribute_outputs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Evaluate a node group to compute the output geometry.
|
|
|
|
|
* Currently, this uses a fairly basic and inefficient algorithm that might compute things more
|
|
|
|
|
* often than necessary. It's going to be replaced soon.
|
|
|
|
|
*/
|
|
|
|
|
static GeometrySet compute_geometry(const DerivedNodeTree &tree,
|
|
|
|
|
Span<const NodeRef *> group_input_nodes,
|
|
|
|
|
const InputSocketRef &socket_to_compute,
|
|
|
|
|
const InputSocketRef &result_geometry_socket,
|
|
|
|
|
Span<const InputSocketRef *> attribute_output_sockets,
|
|
|
|
|
GeometrySet input_geometry_set,
|
|
|
|
|
NodesModifierData *nmd,
|
|
|
|
|
const ModifierEvalContext *ctx)
|
|
|
|
@@ -819,7 +890,7 @@ static GeometrySet compute_geometry(const DerivedNodeTree &tree,
|
|
|
|
|
for (const OutputSocketRef *socket : remaining_input_sockets) {
|
|
|
|
|
const CPPType &cpp_type = *socket->typeinfo()->get_geometry_nodes_cpp_type();
|
|
|
|
|
void *value_in = allocator.allocate(cpp_type.size(), cpp_type.alignment());
|
|
|
|
|
initialize_group_input(*nmd, *socket->bsocket(), value_in);
|
|
|
|
|
initialize_group_input(*nmd, *socket, value_in);
|
|
|
|
|
group_inputs.add_new({root_context, socket}, {cpp_type, value_in});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -828,7 +899,10 @@ static GeometrySet compute_geometry(const DerivedNodeTree &tree,
|
|
|
|
|
input_geometry_set.clear();
|
|
|
|
|
|
|
|
|
|
Vector<DInputSocket> group_outputs;
|
|
|
|
|
group_outputs.append({root_context, &socket_to_compute});
|
|
|
|
|
group_outputs.append({root_context, &result_geometry_socket});
|
|
|
|
|
for (const InputSocketRef *attribute_output : attribute_output_sockets) {
|
|
|
|
|
group_outputs.append({root_context, attribute_output});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<geo_log::GeoLogger> geo_logger;
|
|
|
|
|
|
|
|
|
@@ -856,9 +930,13 @@ static GeometrySet compute_geometry(const DerivedNodeTree &tree,
|
|
|
|
|
nmd_orig->runtime_eval_log = new geo_log::ModifierLog(*geo_logger);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_assert(eval_params.r_output_values.size() == 1);
|
|
|
|
|
GMutablePointer result = eval_params.r_output_values[0];
|
|
|
|
|
return result.relocate_out<GeometrySet>();
|
|
|
|
|
BLI_assert(eval_params.r_output_values.size() >= 1);
|
|
|
|
|
GeometrySet geometry_set = eval_params.r_output_values[0].relocate_out<GeometrySet>();
|
|
|
|
|
|
|
|
|
|
evaluate_attributes_on_result_geometry(geometry_set,
|
|
|
|
|
eval_params.r_output_values.as_span().drop_front(1));
|
|
|
|
|
|
|
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -939,13 +1017,27 @@ static void modifyGeometry(ModifierData *md,
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const InputSocketRef *group_output = group_outputs[0];
|
|
|
|
|
if (group_output->idname() != "NodeSocketGeometry") {
|
|
|
|
|
const InputSocketRef &first_group_output = *group_outputs[0];
|
|
|
|
|
if (first_group_output.idname() != "NodeSocketGeometry") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geometry_set = compute_geometry(
|
|
|
|
|
tree, input_nodes, *group_outputs[0], std::move(geometry_set), nmd, ctx);
|
|
|
|
|
/* TODO: Only add outputs that can be attributes and only outputs that have a corresponding name
|
|
|
|
|
* to put the data in. */
|
|
|
|
|
Vector<const InputSocketRef *> attribute_output_sockets;
|
|
|
|
|
for (const InputSocketRef *group_output : group_outputs) {
|
|
|
|
|
if (socket_type_can_be_attribute(*group_output->bsocket())) {
|
|
|
|
|
attribute_output_sockets.append(group_output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geometry_set = compute_geometry(tree,
|
|
|
|
|
input_nodes,
|
|
|
|
|
first_group_output,
|
|
|
|
|
attribute_output_sockets,
|
|
|
|
|
std::move(geometry_set),
|
|
|
|
|
nmd,
|
|
|
|
|
ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
|
|
|
|
@@ -981,7 +1073,8 @@ static void draw_property_for_socket(uiLayout *layout,
|
|
|
|
|
NodesModifierData *nmd,
|
|
|
|
|
PointerRNA *bmain_ptr,
|
|
|
|
|
PointerRNA *md_ptr,
|
|
|
|
|
const bNodeSocket &socket)
|
|
|
|
|
const bNodeSocket &socket,
|
|
|
|
|
const int socket_index)
|
|
|
|
|
{
|
|
|
|
|
/* The property should be created in #MOD_nodes_update_interface with the correct type. */
|
|
|
|
|
IDProperty *property = IDP_GetPropertyFromGroup(nmd->settings.properties, socket.identifier);
|
|
|
|
@@ -1026,7 +1119,7 @@ static void draw_property_for_socket(uiLayout *layout,
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
if (socket_type_has_attribute_toggle(socket) &&
|
|
|
|
|
if (input_has_attribute_toggle(*nmd->node_group, socket_index) &&
|
|
|
|
|
USER_EXPERIMENTAL_TEST(&U, use_geometry_nodes_fields)) {
|
|
|
|
|
const std::string rna_path_use_attribute = "[\"" + std::string(socket_id_esc) +
|
|
|
|
|
use_attribute_suffix + "\"]";
|
|
|
|
@@ -1086,8 +1179,9 @@ static void panel_draw(const bContext *C, Panel *panel)
|
|
|
|
|
PointerRNA bmain_ptr;
|
|
|
|
|
RNA_main_pointer_create(bmain, &bmain_ptr);
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH (bNodeSocket *, socket, &nmd->node_group->inputs) {
|
|
|
|
|
draw_property_for_socket(layout, nmd, &bmain_ptr, ptr, *socket);
|
|
|
|
|
int socket_index;
|
|
|
|
|
LISTBASE_FOREACH_INDEX (bNodeSocket *, socket, &nmd->node_group->inputs, socket_index) {
|
|
|
|
|
draw_property_for_socket(layout, nmd, &bmain_ptr, ptr, *socket, socket_index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -1118,9 +1212,27 @@ static void panel_draw(const bContext *C, Panel *panel)
|
|
|
|
|
modifier_panel_end(layout, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void output_attributes_panel_draw(const bContext *C, Panel *panel)
|
|
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
|
|
|
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
|
|
|
|
|
NodesModifierData *nmd = static_cast<NodesModifierData *>(ptr->data);
|
|
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
uiLayoutSetPropDecorate(layout, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void panelRegister(ARegionType *region_type)
|
|
|
|
|
{
|
|
|
|
|
modifier_panel_register(region_type, eModifierType_Nodes, panel_draw);
|
|
|
|
|
PanelType *panel_type = modifier_panel_register(region_type, eModifierType_Nodes, panel_draw);
|
|
|
|
|
modifier_subpanel_register(region_type,
|
|
|
|
|
"output_attributes",
|
|
|
|
|
"Output Attributes",
|
|
|
|
|
nullptr,
|
|
|
|
|
output_attributes_panel_draw,
|
|
|
|
|
panel_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void blendWrite(BlendWriter *writer, const ModifierData *md)
|
|
|
|
|