Blender V4.5
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_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
28#include "UI_interface.hh"
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
49
50static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
51{
53 reinterpret_cast<const GreasePencilWeightAngleModifierData *>(md);
55 reinterpret_cast<GreasePencilWeightAngleModifierData *>(target);
56
59}
60
68
69static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
70{
72
73 return (mmd->target_vgname[0] == '\0');
74}
75
76static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
77{
79 reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
80
82}
83
84static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
85{
87 reinterpret_cast<const GreasePencilWeightAngleModifierData *>(md);
88
91}
92
99
101 const ListBase &vertex_group_names)
102{
103 const int def_nr = BKE_defgroup_name_index(&vertex_group_names, name);
104 if (def_nr < 0) {
105 return false;
106 }
107 return true;
108}
109
111 const Object &ob,
113{
114 const auto &mmd = reinterpret_cast<const GreasePencilWeightAngleModifierData &>(md);
115 bke::CurvesGeometry &curves = drawing.strokes_for_write();
116 if (curves.is_empty()) {
117 return;
118 }
119 IndexMaskMemory memory;
121 &ob, curves, mmd.influence, memory);
122 if (strokes.is_empty()) {
123 return;
124 }
125
126 /* Make sure that the target vertex group is added to this drawing so we can write to it. */
128
130 bke::SpanAttributeWriter<float> dst_weights = attributes.lookup_for_write_span<float>(
131 mmd.target_vgname);
132
133 BLI_assert(!dst_weights.span.is_empty());
134
136 curves, mmd.influence);
137
138 /* Use default Z up. */
139 const float3 z_up(0.0f, 0.0f, 1.0f);
140 float3 axis(0.0f);
141 axis[mmd.axis] = 1.0f;
142 float3 vec_ref;
143 /* Apply modifier rotation (sub 90 degrees for Y axis due Z-Up vector). */
144 const float rot_angle = mmd.angle - ((mmd.axis == 1) ? M_PI_2 : 0.0f);
145 rotate_normalized_v3_v3v3fl(vec_ref, z_up, axis, rot_angle);
146
147 const float3x3 obmat3x3(ob.object_to_world());
148
149 /* Apply the rotation of the object. */
151 vec_ref = math::transform_point(obmat3x3, vec_ref);
152 }
153
154 const OffsetIndices points_by_curve = curves.points_by_curve();
155 const Span<float3> positions = curves.positions();
156
157 strokes.foreach_index(GrainSize(512), [&](const int stroke) {
158 const IndexRange points = points_by_curve[stroke];
159 if (points.size() == 1) {
160 dst_weights.span[points.start()] = 1.0f;
161 return;
162 }
163 for (const int point : points.drop_front(1)) {
164 const float influence_weight = influence_weights[point];
165 if (influence_weight <= 0.0f) {
166 continue;
167 }
168
169 const float3 p1 = math::transform_point(obmat3x3, positions[point]);
170 const float3 p2 = math::transform_point(obmat3x3, positions[point - 1]);
171 const float3 vec = p2 - p1;
172 const float angle = angle_on_axis_v3v3_v3(vec_ref, vec, axis);
173 float weight = 1.0f - math::sin(angle);
174
176 weight = 1.0f - weight;
177 }
178
179 dst_weights.span[point] = (mmd.flag & MOD_GREASE_PENCIL_WEIGHT_ANGLE_MULTIPLY_DATA) ?
180 dst_weights.span[point] * weight :
181 weight;
182 dst_weights.span[point] *= influence_weight;
183 dst_weights.span[point] = math::clamp(dst_weights.span[point], mmd.min_weight, 1.0f);
184 }
185 /* First point has the same weight as the second one. */
186 dst_weights.span[points[0]] = dst_weights.span[points[1]];
187 });
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
203 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
204
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);
215 modifier::greasepencil::get_drawings_for_write(grease_pencil, layer_mask, current_frame);
216
218 write_weights_for_drawing(*md, *ctx->object, *drawing);
219 });
220}
221
222static void panel_draw(const bContext *C, Panel *panel)
223{
224 uiLayout *row, *sub;
225 uiLayout *layout = panel->layout;
226
227 PointerRNA ob_ptr;
229
230 uiLayoutSetPropSep(layout, true);
231
232 row = &layout->row(true);
234 row, 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 uiLayoutSetPropDecorate(sub, false);
239 uiLayoutSetActive(sub, 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:529
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
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
void uiLayoutSetActive(uiLayout *layout, bool active)
void uiItemPointerR(uiLayout *layout, PointerRNA *ptr, blender::StringRefNull propname, PointerRNA *searchptr, blender::StringRefNull searchpropname, std::optional< blender::StringRefNull > name, int icon)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
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)
int RNA_string_length(PointerRNA *ptr, const char *name)
ListBase vertex_group_names
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:404
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 & row(bool align)
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:4226
uint8_t flag
Definition wm_window.cc:139