Blender V5.3
MOD_grease_pencil_weight_angle.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_modifier_types.h"
16#include "DNA_object_types.h"
17#include "DNA_screen_types.h"
18
19#include "RNA_access.hh"
20
21#include "BKE_curves.hh"
22#include "BKE_deform.hh"
23#include "BKE_geometry_set.hh"
24#include "BKE_grease_pencil.hh"
26#include "BKE_modifier.hh"
27
29#include "UI_resources.hh"
30
32#include "MOD_modifiertypes.hh"
33#include "MOD_ui_common.hh"
34
35#include "RNA_prototypes.hh"
36
37namespace blender {
38
46
47static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
48{
50 reinterpret_cast<const GreasePencilWeightAngleModifierData *>(md);
52 reinterpret_cast<GreasePencilWeightAngleModifierData *>(target);
53
54 BKE_modifier_copydata_generic(md, target, flag);
56}
57
65
66static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
67{
69 reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
70
71 return (mmd->target_vgname[0] == '\0');
72}
73
74static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
75{
77 reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
78
80}
81
82static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
83{
85 reinterpret_cast<const GreasePencilWeightAngleModifierData *>(md);
86
87 writer->write_struct(mmd);
89}
90
97
99 const ListBaseT<bDeformGroup> &vertex_group_names)
100{
101 const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
102 if (def_nr < 0) {
103 return false;
104 }
105 return true;
106}
107
109 const Object &ob,
111{
112 const auto &mmd = reinterpret_cast<const GreasePencilWeightAngleModifierData &>(md);
114 if (curves.is_empty()) {
115 return;
116 }
117 IndexMaskMemory memory;
119 &ob, curves, mmd.influence, memory);
120 if (strokes.is_empty()) {
121 return;
122 }
123
124 /* Make sure that the target vertex group is added to this drawing so we can write to it. */
125 bke::greasepencil::ensure_vertex_group(mmd.target_vgname, curves.vertex_group_names);
126
127 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
128 bke::SpanAttributeWriter<float> dst_weights = attributes.lookup_for_write_span<float>(
129 mmd.target_vgname);
130
131 BLI_assert(!dst_weights.span.is_empty());
132
134 curves, mmd.influence);
135
136 /* Use default Z up. */
137 const float3 z_up(0.0f, 0.0f, 1.0f);
138 float3 axis(0.0f);
139 axis[mmd.axis] = 1.0f;
140 float3 vec_ref;
141 /* Apply modifier rotation (sub 90 degrees for Y axis due Z-Up vector). */
142 const float rot_angle = mmd.angle - ((mmd.axis == 1) ? M_PI_2 : 0.0f);
143 rotate_normalized_v3_v3v3fl(vec_ref, z_up, axis, rot_angle);
144
145 const float3x3 obmat3x3(ob.object_to_world());
146
147 /* Apply the rotation of the object. */
149 vec_ref = math::transform_point(obmat3x3, vec_ref);
150 }
151
152 const OffsetIndices points_by_curve = curves.points_by_curve();
153 const Span<float3> positions = curves.positions();
154
155 strokes.foreach_index(
156 [&](const int stroke) {
157 const IndexRange points = points_by_curve[stroke];
158 if (points.size() == 1) {
159 dst_weights.span[points.start()] = 1.0f;
160 return;
161 }
162 for (const int point : points.drop_front(1)) {
163 const float influence_weight = influence_weights[point];
164 if (influence_weight <= 0.0f) {
165 continue;
166 }
167
168 const float3 p1 = math::transform_point(obmat3x3, positions[point]);
169 const float3 p2 = math::transform_point(obmat3x3, positions[point - 1]);
170 const float3 vec = p2 - p1;
171 const float angle = angle_on_axis_v3v3_v3(vec_ref, vec, axis);
172 float weight = 1.0f - math::sin(angle);
173
175 weight = 1.0f - weight;
176 }
177
178 dst_weights.span[point] = (mmd.flag & MOD_GREASE_PENCIL_WEIGHT_ANGLE_MULTIPLY_DATA) ?
179 dst_weights.span[point] * weight :
180 weight;
181 dst_weights.span[point] *= influence_weight;
182 dst_weights.span[point] = math::clamp(dst_weights.span[point], mmd.min_weight, 1.0f);
183 }
184 /* First point has the same weight as the second one. */
185 dst_weights.span[points[0]] = dst_weights.span[points[1]];
186 },
188
189 dst_weights.finish();
190}
191
193 const ModifierEvalContext *ctx,
194 bke::GeometrySet *geometry_set)
195{
197 reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
198
199 if (!geometry_set->has_grease_pencil()) {
200 return;
201 }
202
204
205 if (!target_vertex_group_available(mmd->target_vgname, grease_pencil.vertex_group_names)) {
206 return;
207 }
208
209 const int current_frame = grease_pencil.runtime->eval_frame;
210
211 IndexMaskMemory mask_memory;
213 grease_pencil, mmd->influence, mask_memory);
216
218 write_weights_for_drawing(*md, *ctx->object, *drawing);
219 });
220}
221
222static void panel_draw(const bContext *C, Panel *panel)
223{
224 ui::Layout &layout = *panel->layout;
225
226 PointerRNA ob_ptr;
228
229 layout.use_property_split_set(true);
230
231 ui::Layout &row = layout.row(true);
232 row.prop_search(ptr, "target_vertex_group", &ob_ptr, "vertex_groups", std::nullopt, ICON_NONE);
233
234 ui::Layout &sub = row.row(true);
235 bool has_output = RNA_string_length(ptr, "target_vertex_group") != 0;
236 sub.use_property_decorate_set(false);
237 sub.active_set(has_output);
238 sub.prop(ptr, "use_invert_output", UI_ITEM_NONE, "", ICON_ARROW_LEFTRIGHT);
239
240 layout.prop(ptr, "angle", UI_ITEM_NONE, std::nullopt, ICON_NONE);
241 layout.prop(ptr, "axis", UI_ITEM_NONE, std::nullopt, ICON_NONE);
242 layout.prop(ptr, "space", UI_ITEM_NONE, std::nullopt, ICON_NONE);
243
244 layout.prop(ptr, "minimum_weight", UI_ITEM_NONE, std::nullopt, ICON_NONE);
245 layout.prop(ptr, "use_multiply", UI_ITEM_NONE, std::nullopt, ICON_NONE);
246
247 if (ui::Layout *influence_panel = layout.panel_prop(
248 C, ptr, "open_influence_panel", IFACE_("Influence")))
249 {
253 }
254
255 modifier_error_message_draw(layout, ptr);
256}
257
262
264 /*idname*/ "GreasePencilWeightAngleModifier",
265 /*name*/ N_("Weight Angle"),
266 /*struct_name*/ "GreasePencilWeightAngleModifierData",
267 /*struct_size*/ sizeof(GreasePencilWeightAngleModifierData),
268 /*srna*/ &RNA_GreasePencilWeightAngleModifier,
270 /*flags*/
273 /*icon*/ ICON_MOD_VERTEX_WEIGHT,
274
275 /*copy_data*/ copy_data,
276
277 /*deform_verts*/ nullptr,
278 /*deform_matrices*/ nullptr,
279 /*deform_verts_EM*/ nullptr,
280 /*deform_matrices_EM*/ nullptr,
281 /*modify_mesh*/ nullptr,
282 /*modify_geometry_set*/ modify_geometry_set,
283
284 /*init_data*/ init_data,
285 /*required_data_mask*/ nullptr,
286 /*free_data*/ free_data,
287 /*is_disabled*/ is_disabled,
288 /*update_depsgraph*/ nullptr,
289 /*depends_on_time*/ nullptr,
290 /*depends_on_normals*/ nullptr,
291 /*foreach_ID_link*/ foreach_ID_link,
292 /*foreach_tex_link*/ nullptr,
293 /*free_runtime_data*/ nullptr,
294 /*panel_register*/ panel_register,
295 /*blend_write*/ blend_write,
296 /*blend_read*/ blend_read,
297};
298
299} // namespace blender
Low-level operations for curves.
support for deformation groups and hooks.
Low-level operations for grease pencil.
Utility functions for vertex groups in grease pencil objects.
#define M_PI_2
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr int64_t size() const
constexpr int64_t start() const
constexpr IndexRange drop_front(int64_t n) const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
bke::CurvesGeometry & strokes_for_write()
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
int ensure_vertex_group(StringRef name, ListBaseT< bDeformGroup > &vertex_group_names)
constexpr ParallelGrainSize grain_size(int grain_size)
T clamp(const T &a, const T &min, const T &max)
T sin(const AngleRadianBase< T > &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void draw_material_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
void draw_layer_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
VArray< float > get_influence_vertex_weights(const bke::CurvesGeometry &curves, const GreasePencilModifierInfluenceData &influence_data)
Vector< bke::greasepencil::Drawing * > get_drawings_for_write(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
void draw_vertex_group_settings(const bContext *, ui::Layout &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 parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
@ MOD_GREASE_PENCIL_WEIGHT_ANGLE_MULTIPLY_DATA
@ MOD_GREASE_PENCIL_WEIGHT_ANGLE_INVERT_OUTPUT
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:454
void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axis[3], float angle)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
static void write_weights_for_drawing(const ModifierData &md, const Object &ob, bke::greasepencil::Drawing &drawing)
@ eModifierType_GreasePencilWeightAngle
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
@ MOD_GREASE_PENCIL_WEIGHT_ANGLE_SPACE_LOCAL
static bool target_vertex_group_available(const StringRefNull name, const ListBaseT< bDeformGroup > &vertex_group_names)
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
Definition deform.cc:544
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
ModifierTypeInfo modifierType_GreasePencilWeightAngle
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
MatBase< float, 3, 3 > float3x3
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
int RNA_string_length(PointerRNA *ptr, const char *name)
static void blend_read(BlendDataReader *, ModifierData *md)
float angle_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void panel_draw(const bContext *, Panel *panel)
VecBase< float, 3 > float3
static bool is_disabled(const Scene *, ModifierData *md, bool)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
GreasePencilModifierInfluenceData influence
ui::Layout * layout
GreasePencil * get_grease_pencil_for_write()
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
Layout & row(bool align)
#define N_(msgid)