Blender V5.3
MOD_grease_pencil_hook.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_screen_types.h"
17
18#include "RNA_access.hh"
19
20#include "BKE_action.hh"
21#include "BKE_colortools.hh"
22#include "BKE_curves.hh"
23#include "BKE_geometry_set.hh"
24#include "BKE_grease_pencil.hh"
25#include "BKE_lib_query.hh"
26#include "BKE_modifier.hh"
27#include "BKE_object_types.hh"
28
29#include "UI_interface.hh"
31#include "UI_resources.hh"
32
34#include "MOD_modifiertypes.hh"
35#include "MOD_ui_common.hh"
36
37#include "RNA_prototypes.hh"
38
39namespace blender {
40
41static void init_data(ModifierData *md)
42{
43 auto *gpmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
45 modifier::greasepencil::init_influence_data(&gpmd->influence, true);
46}
47
48static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
49{
50 const auto *gmd = reinterpret_cast<const GreasePencilHookModifierData *>(md);
51 auto *tgmd = reinterpret_cast<GreasePencilHookModifierData *>(target);
52
53 BKE_modifier_copydata_generic(md, target, flag);
54 modifier::greasepencil::copy_influence_data(&gmd->influence, &tgmd->influence, flag);
55}
56
57static void free_data(ModifierData *md)
58{
59 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
60
62}
63
64static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
65{
66 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
67
68 return (mmd->object == nullptr);
69}
70
72{
73 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
74 if (mmd->object != nullptr) {
75 if (mmd->subtarget[0]) {
77 ctx->node, mmd->object, mmd->subtarget, DEG_OB_COMP_BONE, "Hook Modifier");
78 }
79 DEG_add_object_relation(ctx->node, mmd->object, DEG_OB_COMP_TRANSFORM, "Hook Modifier");
80 }
81 DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Hook Modifier");
82}
83
84static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
85{
86 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
87
88 modifier::greasepencil::foreach_influence_ID_link(&mmd->influence, ob, walk, user_data);
89
90 walk(user_data, ob, reinterpret_cast<ID **>(&mmd->object), IDWALK_CB_NOP);
91}
92
93static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
94{
95 const auto *mmd = reinterpret_cast<const GreasePencilHookModifierData *>(md);
96
97 writer->write_struct(mmd);
98 modifier::greasepencil::write_influence_data(writer, &mmd->influence);
99}
100
101static void blend_read(BlendDataReader *reader, ModifierData *md)
102{
103 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
104 modifier::greasepencil::read_influence_data(reader, &mmd->influence);
105}
106
107/* Calculate the factor of falloff. */
108static float hook_falloff(const float falloff,
109 const int falloff_type,
110 const float falloff_sq,
111 const float fac_orig,
112 const CurveMapping *curfalloff,
113 const float len_sq)
114{
115 BLI_assert(falloff_sq);
116 if (len_sq > falloff_sq) {
117 return 0.0f;
118 }
119 if (len_sq <= 0.0f) {
120 return fac_orig;
121 }
122 if (falloff_type == MOD_GREASE_PENCIL_HOOK_Falloff_Const) {
123 return fac_orig;
124 }
125 if (falloff_type == MOD_GREASE_PENCIL_HOOK_Falloff_InvSquare) {
126 /* Avoid sqrt below. */
127 return (1.0f - (len_sq / falloff_sq)) * fac_orig;
128 }
129
130 float fac = 1.0f - (math::sqrt(len_sq) / falloff);
131
132 switch (falloff_type) {
134 return BKE_curvemapping_evaluateF(curfalloff, 0, fac) * fac_orig;
136 return fac * fac * fac_orig;
138 return (3.0f * fac * fac - 2.0f * fac * fac * fac) * fac_orig;
140 return math::sqrt(fac) * fac_orig;
141 break;
143 return math::sqrt(2 * fac - fac * fac) * fac_orig;
145 ATTR_FALLTHROUGH; /* Pass. */
146 default:
147 return fac * fac_orig;
148 }
149}
150
151static void deform_drawing(const ModifierData &md,
152 const Object &ob,
154{
155 const auto &mmd = reinterpret_cast<const GreasePencilHookModifierData &>(md);
158
159 if (curves.is_empty()) {
160 return;
161 }
162 IndexMaskMemory memory;
164 &ob, curves, mmd.influence, memory);
165 if (strokes.is_empty()) {
166 return;
167 }
168
170 curves, mmd.influence);
171
172 const int falloff_type = mmd.falloff_type;
173 const float falloff = (mmd.falloff_type == MOD_GREASE_PENCIL_HOOK_Falloff_None) ? 0.0f :
174 mmd.falloff;
175 const float falloff_sq = square_f(falloff);
176 const float fac_orig = mmd.force;
177 const bool use_falloff = falloff_sq != 0.0f;
178 const bool use_uniform = (mmd.flag & MOD_GREASE_PENCIL_HOOK_UNIFORM_SPACE) != 0;
179
180 const float3x3 mat_uniform = use_uniform ? float3x3(float4x4(mmd.parentinv)) :
182 const float3 cent = use_uniform ? math::transform_point(mat_uniform, float3(mmd.cent)) :
183 float3(mmd.cent);
184
185 float4x4 dmat = mmd.object->object_to_world();
186 /* Get world-space matrix of target, corrected for the space the verts are in. */
187 if (mmd.subtarget[0]) {
188 bPoseChannel *pchan = BKE_pose_channel_find_name(mmd.object->pose, mmd.subtarget);
189 if (pchan) {
190 /* Bone target if there's a matching pose-channel. */
191 dmat = mmd.object->object_to_world() * float4x4(pchan->pose_mat);
192 }
193 }
194 float4x4 use_mat = ob.world_to_object() * dmat * float4x4(mmd.parentinv);
195
196 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
197 MutableSpan<float3> positions = curves.positions_for_write();
198
199 strokes.foreach_index(
200 [&](const int stroke) {
201 const IndexRange points_range = points_by_curve[stroke].index_range();
202 for (const int point_i : points_range) {
203 const int point = point_i + points_by_curve[stroke].first();
204 const float weight = input_weights[point];
205 if (weight < 0.0f) {
206 continue;
207 }
208
209 float fac;
210 if (use_falloff) {
211 float len_sq;
212 if (use_uniform) {
213 const float3 co_uniform = math::transform_point(mat_uniform, positions[point]);
214 len_sq = math::distance(cent, co_uniform);
215 }
216 else {
217 len_sq = math::distance(cent, positions[point]);
218 }
219 fac = hook_falloff(
220 falloff, falloff_type, falloff_sq, fac_orig, mmd.influence.custom_curve, len_sq);
221 }
222 else {
223 fac = fac_orig;
224 }
225
226 if (fac != 0.0f) {
227 const float3 co_tmp = math::transform_point(use_mat, positions[point]);
228 positions[point] = math::interpolate(positions[point], co_tmp, fac * weight);
229 }
230 }
231 },
233
234 drawing.tag_positions_changed();
235}
236
238 const ModifierEvalContext *ctx,
239 bke::GeometrySet *geometry_set)
240{
241 const GreasePencilHookModifierData *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
242
243 if (!geometry_set->has_grease_pencil()) {
244 return;
245 }
246
248
249 const int current_frame = grease_pencil.runtime->eval_frame;
250
251 IndexMaskMemory mask_memory;
253 grease_pencil, mmd->influence, mask_memory);
256
258 deform_drawing(*md, *ctx->object, *drawing);
259 });
260}
261
262static void panel_draw(const bContext *C, Panel *panel)
263{
264 ui::Layout &layout = *panel->layout;
265
266 PointerRNA ob_ptr;
268
269 PointerRNA hook_object_ptr = RNA_pointer_get(ptr, "object");
270
271 layout.use_property_split_set(true);
272
273 ui::Layout &col = layout.column(false);
274 col.prop(ptr, "object", UI_ITEM_NONE, std::nullopt, ICON_NONE);
275 if (hook_object_ptr && RNA_enum_get(&hook_object_ptr, "type") == OB_ARMATURE) {
276 PointerRNA hook_object_data_ptr = RNA_pointer_get(&hook_object_ptr, "data");
277 col.prop_search(ptr, "subtarget", &hook_object_data_ptr, "bones", IFACE_("Bone"), ICON_NONE);
278 }
279
280 layout.prop(ptr, "strength", ui::ITEM_R_SLIDER, std::nullopt, ICON_NONE);
281
282 if (ui::Layout *sub = layout.panel_prop(C, ptr, "open_falloff_panel", IFACE_("Falloff"))) {
283 sub->use_property_split_set(true);
284
285 sub->prop(ptr, "falloff_type", UI_ITEM_NONE, IFACE_("Type"), ICON_NONE);
286
287 bool use_falloff = RNA_enum_get(ptr, "falloff_type") != eWarp_Falloff_None;
288
289 ui::Layout &row = sub->row(false);
290 row.active_set(use_falloff);
291 row.prop(ptr, "falloff_radius", UI_ITEM_NONE, std::nullopt, ICON_NONE);
292
293 sub->prop(ptr, "use_falloff_uniform", UI_ITEM_NONE, std::nullopt, ICON_NONE);
294
295 if (RNA_enum_get(ptr, "falloff_type") == eWarp_Falloff_Curve) {
296 template_curve_mapping(sub, ptr, "custom_curve", 0, false, false, false, false, false);
297 }
298 }
299
300 if (ui::Layout *influence_panel = layout.panel_prop(
301 C, ptr, "open_influence_panel", IFACE_("Influence")))
302 {
306 }
307
308 modifier_error_message_draw(layout, ptr);
309}
310
315
317 /*idname*/ "GreasePencilHookModifier",
318 /*name*/ N_("Hook"),
319 /*struct_name*/ "GreasePencilHookModifierData",
320 /*struct_size*/ sizeof(GreasePencilHookModifierData),
321 /*srna*/ &RNA_GreasePencilHookModifier,
323 /*flags*/
326 /*icon*/ ICON_HOOK,
327
328 /*copy_data*/ copy_data,
329
330 /*deform_verts*/ nullptr,
331 /*deform_matrices*/ nullptr,
332 /*deform_verts_EM*/ nullptr,
333 /*deform_matrices_EM*/ nullptr,
334 /*modify_mesh*/ nullptr,
335 /*modify_geometry_set*/ modify_geometry_set,
336
337 /*init_data*/ init_data,
338 /*required_data_mask*/ nullptr,
339 /*free_data*/ free_data,
340 /*is_disabled*/ is_disabled,
341 /*update_depsgraph*/ update_depsgraph,
342 /*depends_on_time*/ nullptr,
343 /*depends_on_normals*/ nullptr,
344 /*foreach_ID_link*/ foreach_ID_link,
345 /*foreach_tex_link*/ nullptr,
346 /*free_runtime_data*/ nullptr,
347 /*panel_register*/ panel_register,
348 /*blend_write*/ blend_write,
349 /*blend_read*/ blend_read,
350};
351
352} // namespace blender
Blender kernel action and pose functionality.
Low-level operations for curves.
Low-level operations for grease pencil.
#define ATTR_FALLTHROUGH
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
bke::CurvesGeometry & strokes_for_write()
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
constexpr ParallelGrainSize grain_size(int grain_size)
T sqrt(const T &a)
T distance(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
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 ensure_no_bezier_curves(Drawing &drawing)
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)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:454
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
@ MOD_GREASE_PENCIL_HOOK_Falloff_Curve
@ MOD_GREASE_PENCIL_HOOK_Falloff_Sharp
@ MOD_GREASE_PENCIL_HOOK_Falloff_Linear
@ MOD_GREASE_PENCIL_HOOK_Falloff_InvSquare
@ MOD_GREASE_PENCIL_HOOK_Falloff_Root
@ MOD_GREASE_PENCIL_HOOK_Falloff_None
@ MOD_GREASE_PENCIL_HOOK_Falloff_Smooth
@ MOD_GREASE_PENCIL_HOOK_Falloff_Const
@ MOD_GREASE_PENCIL_HOOK_Falloff_Sphere
@ eModifierType_GreasePencilHook
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void update_depsgraph(tGraphSliderOp *gso)
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
MatBase< float, 4, 4 > float4x4
void DEG_add_bone_relation(DepsNodeHandle *handle, Object *object, const char *bone_name, eDepsObjectComponentType component, const char *description)
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)
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
@ MOD_GREASE_PENCIL_HOOK_UNIFORM_SPACE
MatBase< float, 3, 3 > float3x3
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
static void blend_read(BlendDataReader *, ModifierData *md)
static float hook_falloff(const float falloff, const int falloff_type, const float falloff_sq, const float fac_orig, const CurveMapping *curfalloff, const float len_sq)
ModifierTypeInfo modifierType_GreasePencilHook
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
MINLINE float square_f(float a)
static void panel_draw(const bContext *, Panel *panel)
VecBase< float, 3 > float3
static bool is_disabled(const Scene *, ModifierData *md, bool)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
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_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)
Layout & column(bool align)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
Layout & row(bool align)
#define N_(msgid)