Blender V5.0
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_defaults.h"
16#include "DNA_modifier_types.h"
17#include "DNA_object_types.h"
18#include "DNA_screen_types.h"
19
20#include "RNA_access.hh"
21
22#include "BKE_curves.hh"
23#include "BKE_deform.hh"
24#include "BKE_geometry_set.hh"
25#include "BKE_grease_pencil.hh"
27#include "BKE_modifier.hh"
28
30#include "UI_resources.hh"
31
33#include "MOD_modifiertypes.hh"
34#include "MOD_ui_common.hh"
35
36#include "RNA_prototypes.hh"
37
38namespace blender {
39
50
51static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
52{
54 reinterpret_cast<const GreasePencilWeightAngleModifierData *>(md);
56 reinterpret_cast<GreasePencilWeightAngleModifierData *>(target);
57
60}
61
69
70static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
71{
73
74 return (mmd->target_vgname[0] == '\0');
75}
76
77static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
78{
80 reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
81
83}
84
85static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
86{
88 reinterpret_cast<const GreasePencilWeightAngleModifierData *>(md);
89
92}
93
100
102 const ListBase &vertex_group_names)
103{
104 const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
105 if (def_nr < 0) {
106 return false;
107 }
108 return true;
109}
110
112 const Object &ob,
114{
115 const auto &mmd = reinterpret_cast<const GreasePencilWeightAngleModifierData &>(md);
116 bke::CurvesGeometry &curves = drawing.strokes_for_write();
117 if (curves.is_empty()) {
118 return;
119 }
120 IndexMaskMemory memory;
122 &ob, curves, mmd.influence, memory);
123 if (strokes.is_empty()) {
124 return;
125 }
126
127 /* Make sure that the target vertex group is added to this drawing so we can write to it. */
129
131 bke::SpanAttributeWriter<float> dst_weights = attributes.lookup_for_write_span<float>(
132 mmd.target_vgname);
133
134 BLI_assert(!dst_weights.span.is_empty());
135
137 curves, mmd.influence);
138
139 /* Use default Z up. */
140 const float3 z_up(0.0f, 0.0f, 1.0f);
141 float3 axis(0.0f);
142 axis[mmd.axis] = 1.0f;
143 float3 vec_ref;
144 /* Apply modifier rotation (sub 90 degrees for Y axis due Z-Up vector). */
145 const float rot_angle = mmd.angle - ((mmd.axis == 1) ? M_PI_2 : 0.0f);
146 rotate_normalized_v3_v3v3fl(vec_ref, z_up, axis, rot_angle);
147
148 const float3x3 obmat3x3(ob.object_to_world());
149
150 /* Apply the rotation of the object. */
152 vec_ref = math::transform_point(obmat3x3, vec_ref);
153 }
154
155 const OffsetIndices points_by_curve = curves.points_by_curve();
156 const Span<float3> positions = curves.positions();
157
158 strokes.foreach_index(GrainSize(512), [&](const int stroke) {
159 const IndexRange points = points_by_curve[stroke];
160 if (points.size() == 1) {
161 dst_weights.span[points.start()] = 1.0f;
162 return;
163 }
164 for (const int point : points.drop_front(1)) {
165 const float influence_weight = influence_weights[point];
166 if (influence_weight <= 0.0f) {
167 continue;
168 }
169
170 const float3 p1 = math::transform_point(obmat3x3, positions[point]);
171 const float3 p2 = math::transform_point(obmat3x3, positions[point - 1]);
172 const float3 vec = p2 - p1;
173 const float angle = angle_on_axis_v3v3_v3(vec_ref, vec, axis);
174 float weight = 1.0f - math::sin(angle);
175
177 weight = 1.0f - weight;
178 }
179
180 dst_weights.span[point] = (mmd.flag & MOD_GREASE_PENCIL_WEIGHT_ANGLE_MULTIPLY_DATA) ?
181 dst_weights.span[point] * weight :
182 weight;
183 dst_weights.span[point] *= influence_weight;
184 dst_weights.span[point] = math::clamp(dst_weights.span[point], mmd.min_weight, 1.0f);
185 }
186 /* First point has the same weight as the second one. */
187 dst_weights.span[points[0]] = dst_weights.span[points[1]];
188 });
189
190 dst_weights.finish();
191}
192
194 const ModifierEvalContext *ctx,
195 bke::GeometrySet *geometry_set)
196{
198 reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
199
200 if (!geometry_set->has_grease_pencil()) {
201 return;
202 }
203
204 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
205
207 return;
208 }
209
210 const int current_frame = grease_pencil.runtime->eval_frame;
211
212 IndexMaskMemory mask_memory;
214 grease_pencil, mmd->influence, mask_memory);
216 modifier::greasepencil::get_drawings_for_write(grease_pencil, layer_mask, current_frame);
217
219 write_weights_for_drawing(*md, *ctx->object, *drawing);
220 });
221}
222
223static void panel_draw(const bContext *C, Panel *panel)
224{
225 uiLayout *row, *sub;
226 uiLayout *layout = panel->layout;
227
228 PointerRNA ob_ptr;
230
231 layout->use_property_split_set(true);
232
233 row = &layout->row(true);
234 row->prop_search(ptr, "target_vertex_group", &ob_ptr, "vertex_groups", std::nullopt, ICON_NONE);
235
236 sub = &row->row(true);
237 bool has_output = RNA_string_length(ptr, "target_vertex_group") != 0;
238 sub->use_property_decorate_set(false);
239 sub->active_set(has_output);
240 sub->prop(ptr, "use_invert_output", UI_ITEM_NONE, "", ICON_ARROW_LEFTRIGHT);
241
242 layout->prop(ptr, "angle", UI_ITEM_NONE, std::nullopt, ICON_NONE);
243 layout->prop(ptr, "axis", UI_ITEM_NONE, std::nullopt, ICON_NONE);
244 layout->prop(ptr, "space", UI_ITEM_NONE, std::nullopt, ICON_NONE);
245
246 layout->prop(ptr, "minimum_weight", UI_ITEM_NONE, std::nullopt, ICON_NONE);
247 layout->prop(ptr, "use_multiply", UI_ITEM_NONE, std::nullopt, ICON_NONE);
248
249 if (uiLayout *influence_panel = layout->panel_prop(
250 C, ptr, "open_influence_panel", IFACE_("Influence")))
251 {
255 }
256
258}
259
264
265} // namespace blender
266
268 /*idname*/ "GreasePencilWeightAngleModifier",
269 /*name*/ N_("Weight Angle"),
270 /*struct_name*/ "GreasePencilWeightAngleModifierData",
271 /*struct_size*/ sizeof(GreasePencilWeightAngleModifierData),
272 /*srna*/ &RNA_GreasePencilWeightAngleModifier,
274 /*flags*/
277 /*icon*/ ICON_MOD_VERTEX_WEIGHT,
278
279 /*copy_data*/ blender::copy_data,
280
281 /*deform_verts*/ nullptr,
282 /*deform_matrices*/ nullptr,
283 /*deform_verts_EM*/ nullptr,
284 /*deform_matrices_EM*/ nullptr,
285 /*modify_mesh*/ nullptr,
286 /*modify_geometry_set*/ blender::modify_geometry_set,
287
288 /*init_data*/ blender::init_data,
289 /*required_data_mask*/ nullptr,
290 /*free_data*/ blender::free_data,
291 /*is_disabled*/ blender::is_disabled,
292 /*update_depsgraph*/ nullptr,
293 /*depends_on_time*/ nullptr,
294 /*depends_on_normals*/ nullptr,
295 /*foreach_ID_link*/ blender::foreach_ID_link,
296 /*foreach_tex_link*/ nullptr,
297 /*free_runtime_data*/ nullptr,
298 /*panel_register*/ blender::panel_register,
299 /*blend_write*/ blender::blend_write,
300 /*blend_read*/ blender::blend_read,
301};
Low-level operations for curves.
support for deformation groups and hooks.
int BKE_defgroup_name_index(const ListBase *defbase, blender::StringRef name)
Definition deform.cc:540
Low-level operations for grease pencil.
Utility functions for vertex groups in grease pencil objects.
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 M_PI_2
float angle_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axis[3], float angle)
#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 IFACE_(msgid)
#define DNA_struct_default_get(struct_name)
@ MOD_GREASE_PENCIL_WEIGHT_ANGLE_SPACE_LOCAL
@ MOD_GREASE_PENCIL_WEIGHT_ANGLE_MULTIPLY_DATA
@ MOD_GREASE_PENCIL_WEIGHT_ANGLE_INVERT_OUTPUT
@ eModifierType_GreasePencilWeightAngle
Object is a sort of wrapper for general info.
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
static bool is_disabled
ModifierTypeInfo modifierType_GreasePencilWeightAngle
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
#define UI_ITEM_NONE
constexpr int64_t size() const
constexpr int64_t start() const
constexpr IndexRange drop_front(int64_t n) const
OffsetIndices< int > points_by_curve() const
MutableAttributeAccessor attributes_for_write()
Span< float3 > positions() const
GSpanAttributeWriter lookup_for_write_span(StringRef attribute_id)
bke::CurvesGeometry & strokes_for_write()
void foreach_index(Fn &&fn) const
int ensure_vertex_group(StringRef name, ListBase &vertex_group_names)
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 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 parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:56
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 write_weights_for_drawing(const ModifierData &md, const Object &ob, bke::greasepencil::Drawing &drawing)
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 free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
MatBase< float, 3, 3 > float3x3
static bool target_vertex_group_available(const StringRefNull name, const ListBase &vertex_group_names)
VecBase< float, 3 > float3
static bool is_disabled(const Scene *, ModifierData *md, bool)
static void blend_read(BlendDataReader *reader, ModifierData *md)
const char * name
int RNA_string_length(PointerRNA *ptr, const char *name)
ListBase vertex_group_names
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:414
struct uiLayout * layout
GreasePencil * get_grease_pencil_for_write()
void use_property_decorate_set(bool is_sep)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, blender::StringRefNull open_prop_name)
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< blender::StringRefNull > name, int icon, bool results_are_suggestions)
void active_set(bool active)
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