Blender V4.5
node_geo_set_grease_pencil_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_curves.hh"
7
8#include "UI_interface.hh"
9
10#include "NOD_rna_define.hh"
11
12#include "RNA_enum_types.hh"
13
14#include "node_geometry_util.hh"
15
17
18enum class Mode : int8_t {
19 Stroke = 0,
20 Fill = 1,
21};
22
24{
25 b.use_custom_socket_order();
26 b.allow_any_socket_order();
27 b.add_default_layout();
28 b.add_input<decl::Geometry>("Grease Pencil")
29 .supported_type(GeometryComponent::Type::GreasePencil)
30 .align_with_previous();
31 b.add_output<decl::Geometry>("Grease Pencil").propagate_all().align_with_previous();
32 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
33 b.add_input<decl::Color>("Color")
34 .default_value(ColorGeometry4f(1.0f, 1.0f, 1.0f, 1.0f))
35 .field_on_all()
36 .hide_label();
37 b.add_input<decl::Float>("Opacity").default_value(1.0f).min(0.0f).max(1.0f).field_on_all();
38}
39
40static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
41{
42 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
43}
44static void node_init(bNodeTree * /*tree*/, bNode *node)
45{
46 node->custom1 = int(Mode::Stroke);
47}
48
50{
51 const bNode &node = params.node();
52 const AttrDomain domain = Mode(node.custom1) == Mode::Stroke ? AttrDomain::Point :
53 AttrDomain::Curve;
54
55 GeometrySet geometry_set = params.extract_input<GeometrySet>("Grease Pencil");
56 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
57 const Field<ColorGeometry4f> color_field = params.extract_input<Field<ColorGeometry4f>>("Color");
58 const Field<float> opacity_field = params.extract_input<Field<float>>("Opacity");
59
60 const StringRef color_attr_name = domain == AttrDomain::Point ? "vertex_color" : "fill_color";
61 const StringRef opacity_attr_name = domain == AttrDomain::Point ? "opacity" : "fill_opacity";
62
63 geometry_set.modify_geometry_sets([&](GeometrySet &geometry) {
64 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
65 using namespace bke::greasepencil;
66 for (const int layer_index : grease_pencil->layers().index_range()) {
67 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
68 if (drawing == nullptr) {
69 continue;
70 }
71 bke::CurvesGeometry &curves = drawing->strokes_for_write();
72 const int64_t domain_size = curves.attributes().domain_size(domain);
73
74 const bke::GreasePencilLayerFieldContext layer_field_context(
75 *grease_pencil, domain, layer_index);
76
77 /* FIXME: The default float value is 0, while the default opacity should be 1. So we have
78 * to initialize the attribute manually.
79 * TODO: Avoid doing this if the selection is false. */
80 if (!curves.attributes().contains(opacity_attr_name)) {
82 opacity_attr_name,
83 domain,
86 }
88 layer_field_context,
89 {color_attr_name, opacity_attr_name},
90 domain,
91 selection,
92 {color_field, opacity_field});
93 }
94 }
95 });
96
97 params.set_output("Grease Pencil", std::move(geometry_set));
98}
99
100static void node_rna(StructRNA *srna)
101{
102 static const EnumPropertyItem mode_items[] = {
103 {int(Mode::Stroke),
104 "STROKE",
105 ICON_NONE,
106 "Stroke",
107 "Set the color and opacity for the points of the stroke"},
108 {int(Mode::Fill),
109 "FILL",
110 ICON_NONE,
111 "Fill",
112 "Set the color and opacity for the stroke fills"},
113 {0, nullptr, 0, nullptr, nullptr},
114 };
115
116 RNA_def_node_enum(srna, "mode", "Mode", "", mode_items, NOD_inline_enum_accessors(custom1));
117}
118
119static void node_register()
120{
121 static blender::bke::bNodeType ntype;
122
123 geo_node_type_base(&ntype, "GeometryNodeSetGreasePencilColor");
124 ntype.ui_name = "Set Grease Pencil Color";
125 ntype.ui_description = "Set color and opacity attributes on Grease Pencil geometry";
128 ntype.declare = node_declare;
129 ntype.initfunc = node_init;
133
134 node_rna(ntype.rna_ext.srna);
135}
137
138} // namespace blender::nodes::node_geo_set_grease_pencil_color_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
constexpr int NODE_DEFAULT_MAX_WIDTH
Definition BKE_node.hh:1225
@ CD_PROP_FLOAT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
long long int int64_t
static VArray ForSingle(T value, const int64_t size)
bool contains(StringRef attribute_id) const
int domain_size(const AttrDomain domain) const
MutableAttributeAccessor attributes_for_write()
AttributeAccessor attributes() const
bool add(const StringRef attribute_id, const AttrDomain domain, const eCustomDataType data_type, const AttributeInit &initializer)
bke::CurvesGeometry & strokes_for_write()
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5573
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
bool try_capture_fields_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, Span< StringRef > attribute_ids, AttrDomain domain, const fn::Field< bool > &selection, Span< fn::GField > fields)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition BLI_color.hh:342
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
StructRNA * srna
Definition RNA_types.hh:909
int16_t custom1
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4227