Blender V5.0
MOD_grease_pencil_smooth.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_index_mask.hh"
10
11#include "BLT_translation.hh"
12
13#include "BLO_read_write.hh"
14
15#include "DNA_defaults.h"
16#include "DNA_modifier_types.h"
17#include "DNA_screen_types.h"
18
19#include "RNA_access.hh"
20
21#include "BKE_curves.hh"
22#include "BKE_geometry_set.hh"
23#include "BKE_grease_pencil.hh"
24#include "BKE_modifier.hh"
25
27#include "UI_resources.hh"
28
29#include "GEO_smooth_curves.hh"
30
32#include "MOD_ui_common.hh"
33
34#include "RNA_prototypes.hh"
35
36namespace blender {
37
47
48static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
49{
51 reinterpret_cast<const GreasePencilSmoothModifierData *>(md);
53 target);
54
57}
58
65
66static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
67{
69
71}
72
73static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
74{
76 reinterpret_cast<const GreasePencilSmoothModifierData *>(md);
77
80}
81
87
88static void deform_drawing(const ModifierData &md,
89 const Object &ob,
91{
92 const auto &mmd = reinterpret_cast<const GreasePencilSmoothModifierData &>(md);
93
94 const int iterations = mmd.step;
95 const float influence = mmd.factor;
96 const bool keep_shape = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_KEEP_SHAPE);
97 const bool smooth_ends = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_SMOOTH_ENDS);
98
99 const bool smooth_position = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_LOCATION);
100 const bool smooth_radius = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_THICKNESS);
101 const bool smooth_opacity = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_STRENGTH);
102 const bool smooth_uv = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_UV);
103
104 if (iterations <= 0 || influence <= 0.0f) {
105 return;
106 }
107
108 if (!(smooth_position || smooth_radius || smooth_opacity || smooth_uv)) {
109 return;
110 }
111
113 bke::CurvesGeometry &curves = drawing.strokes_for_write();
114 if (curves.is_empty()) {
115 return;
116 }
117
118 IndexMaskMemory memory;
120 &ob, curves, mmd.influence, memory);
121
122 if (strokes.is_empty()) {
123 return;
124 }
125
127 const OffsetIndices points_by_curve = curves.points_by_curve();
128 const VArray<bool> cyclic = curves.cyclic();
129 const VArray<bool> point_selection = VArray<bool>::from_single(true, curves.points_num());
130
131 VArray<float> influences;
132 const bool use_influence_vertex_group = mmd.influence.vertex_group_name[0] != '\0';
133 if (use_influence_vertex_group) {
135 curves, mmd.influence);
136 Array<float> vgroup_weights_factored(vgroup_weights.size());
138 vgroup_weights_factored.index_range(), 4096, [&](const IndexRange range) {
139 for (const int i : range) {
140 vgroup_weights_factored[i] = vgroup_weights[i] * influence;
141 }
142 });
143 influences = VArray<float>::from_container(vgroup_weights_factored);
144 }
145 else {
146 influences = VArray<float>::from_single(influence, curves.points_num());
147 }
148
149 if (smooth_position) {
150 bke::GSpanAttributeWriter positions = attributes.lookup_for_write_span("position");
152 points_by_curve,
153 point_selection,
154 cyclic,
155 iterations,
156 influences,
157 smooth_ends,
158 keep_shape,
159 positions.span);
160 positions.finish();
161 drawing.tag_positions_changed();
162 }
163 if (smooth_opacity && drawing.opacities().is_span()) {
164 bke::GSpanAttributeWriter opacities = attributes.lookup_for_write_span("opacity");
166 points_by_curve,
167 point_selection,
168 cyclic,
169 iterations,
170 influences,
171 smooth_ends,
172 false,
173 opacities.span);
174 opacities.finish();
175 }
176 if (smooth_radius && drawing.radii().is_span()) {
177 bke::GSpanAttributeWriter radii = attributes.lookup_for_write_span("radius");
179 points_by_curve,
180 point_selection,
181 cyclic,
182 iterations,
183 influences,
184 smooth_ends,
185 false,
186 radii.span);
187 radii.finish();
188 }
189 if (smooth_uv) {
190 bke::SpanAttributeWriter<float> rotation = attributes.lookup_for_write_span<float>("rotation");
191 if (rotation) {
193 points_by_curve,
194 point_selection,
195 cyclic,
196 iterations,
197 influences,
198 smooth_ends,
199 false,
200 rotation.span);
201 }
202 rotation.finish();
203 }
204}
205
207 const ModifierEvalContext *ctx,
208 bke::GeometrySet *geometry_set)
209{
211
212 if (!geometry_set->has_grease_pencil()) {
213 return;
214 }
215 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
216 const int current_frame = grease_pencil.runtime->eval_frame;
217
218 IndexMaskMemory mask_memory;
220 grease_pencil, mmd->influence, mask_memory);
222 modifier::greasepencil::get_drawings_for_write(grease_pencil, layer_mask, current_frame);
223
225 deform_drawing(*md, *ctx->object, *drawing);
226 });
227}
228
229static void panel_draw(const bContext *C, Panel *panel)
230{
231 uiLayout *row, *col;
232 uiLayout *layout = panel->layout;
233
235
236 row = &layout->row(true);
237 row->prop(ptr, "use_edit_position", UI_ITEM_R_TOGGLE, IFACE_("Position"), ICON_NONE);
238 row->prop(ptr,
239 "use_edit_strength",
242 ICON_NONE);
243 row->prop(ptr, "use_edit_thickness", UI_ITEM_R_TOGGLE, IFACE_("Thickness"), ICON_NONE);
244
245 row->prop(ptr, "use_edit_uv", UI_ITEM_R_TOGGLE, IFACE_("UV"), ICON_NONE);
246
247 layout->use_property_split_set(true);
248
249 layout->prop(ptr, "factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
250 layout->prop(ptr, "step", UI_ITEM_NONE, IFACE_("Repeat"), ICON_NONE);
251
252 col = &layout->column(false);
253 col->active_set(RNA_boolean_get(ptr, "use_edit_position"));
254 col->prop(ptr, "use_keep_shape", UI_ITEM_NONE, std::nullopt, ICON_NONE);
255 col->prop(ptr, "use_smooth_ends", UI_ITEM_NONE, std::nullopt, ICON_NONE);
256
257 if (uiLayout *influence_panel = layout->panel_prop(
258 C, ptr, "open_influence_panel", IFACE_("Influence")))
259 {
263 }
264
266}
267
272
273} // namespace blender
274
276 /*idname*/ "GreasePencilSmoothModifier",
277 /*name*/ N_("Smooth"),
278 /*struct_name*/ "GreasePencilSmoothModifierData",
279 /*struct_size*/ sizeof(GreasePencilSmoothModifierData),
280 /*srna*/ &RNA_GreasePencilSmoothModifier,
282 /*flags*/
285 /*icon*/ ICON_SMOOTHCURVE,
286
287 /*copy_data*/ blender::copy_data,
288
289 /*deform_verts*/ nullptr,
290 /*deform_matrices*/ nullptr,
291 /*deform_verts_EM*/ nullptr,
292 /*deform_matrices_EM*/ nullptr,
293 /*modify_mesh*/ nullptr,
294 /*modify_geometry_set*/ blender::modify_geometry_set,
295
296 /*init_data*/ blender::init_data,
297 /*required_data_mask*/ nullptr,
298 /*free_data*/ blender::free_data,
299 /*is_disabled*/ nullptr,
300 /*update_depsgraph*/ nullptr,
301 /*depends_on_time*/ nullptr,
302 /*depends_on_normals*/ nullptr,
303 /*foreach_ID_link*/ blender::foreach_ID_link,
304 /*foreach_tex_link*/ nullptr,
305 /*free_runtime_data*/ nullptr,
306 /*panel_register*/ blender::panel_register,
307 /*blend_write*/ blender::blend_write,
308 /*blend_read*/ blender::blend_read,
309};
Low-level operations for curves.
Low-level operations for grease pencil.
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
#define BLI_assert(a)
Definition BLI_assert.h:46
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define BLT_I18NCONTEXT_ID_GPENCIL
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define DNA_struct_default_get(struct_name)
@ MOD_GREASE_PENCIL_SMOOTH_MOD_LOCATION
@ MOD_GREASE_PENCIL_SMOOTH_MOD_STRENGTH
@ MOD_GREASE_PENCIL_SMOOTH_KEEP_SHAPE
@ MOD_GREASE_PENCIL_SMOOTH_MOD_UV
@ MOD_GREASE_PENCIL_SMOOTH_SMOOTH_ENDS
@ MOD_GREASE_PENCIL_SMOOTH_MOD_THICKNESS
@ eModifierType_GreasePencilSmooth
static void panel_register(ARegionType *region_type)
static void panel_draw(const bContext *, Panel *panel)
ModifierTypeInfo modifierType_GreasePencilSmooth
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, blender::bke::GeometrySet *geometry_set)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void modifier_error_message_draw(uiLayout *layout, PointerRNA *ptr)
#define C
Definition RandGen.cpp:29
@ UI_ITEM_R_TOGGLE
#define UI_ITEM_NONE
IndexRange index_range() const
Definition BLI_array.hh:360
static VArray from_single(T value, const int64_t size)
static VArray from_container(ContainerT container)
OffsetIndices< int > points_by_curve() const
MutableAttributeAccessor attributes_for_write()
VArray< bool > cyclic() const
bke::CurvesGeometry & strokes_for_write()
uint col
void smooth_curve_attribute(const IndexMask &curves_to_smooth, const OffsetIndices< int > points_by_curve, const VArray< bool > &point_selection, const VArray< bool > &cyclic, int iterations, float influence, bool smooth_ends, bool keep_shape, GMutableSpan attribute_data)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
static IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const Material *material_filter, const std::optional< int > material_pass_filter, const bool material_filter_invert, const bool material_pass_filter_invert, IndexMaskMemory &memory)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
void draw_vertex_group_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
VArray< float > get_influence_vertex_weights(const bke::CurvesGeometry &curves, const GreasePencilModifierInfluenceData &influence_data)
static IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const std::optional< StringRef > tree_node_name_filter, const std::optional< int > layer_pass_filter, const bool layer_filter_invert, const bool layer_pass_filter_invert, IndexMaskMemory &memory)
Vector< bke::greasepencil::Drawing * > get_drawings_for_write(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
void draw_material_filter_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void draw_layer_filter_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void free_influence_data(GreasePencilModifierInfluenceData *influence_data)
void foreach_influence_ID_link(GreasePencilModifierInfluenceData *influence_data, Object *ob, IDWalkFunc walk, void *user_data)
void copy_influence_data(const GreasePencilModifierInfluenceData *influence_data_src, GreasePencilModifierInfluenceData *influence_data_dst, const int)
void ensure_no_bezier_curves(Drawing &drawing)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:56
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
static void init_data(ModifierData *md)
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
static void panel_draw(const bContext *C, Panel *panel)
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
static void deform_drawing(const ModifierData &md, const Object &ob, bke::greasepencil::Drawing &drawing)
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
static void blend_read(BlendDataReader *reader, ModifierData *md)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:414
struct uiLayout * layout
GreasePencil * get_grease_pencil_for_write()
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, blender::StringRefNull open_prop_name)
uiLayout & column(bool align)
uiLayout & row(bool align)
void use_property_split_set(bool value)
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)
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4238
uint8_t flag
Definition wm_window.cc:145