Blender V5.0
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_defaults.h"
16#include "DNA_modifier_types.h"
17#include "DNA_screen_types.h"
18
19#include "RNA_access.hh"
20
21#include "BKE_action.hh"
22#include "BKE_colortools.hh"
23#include "BKE_curves.hh"
24#include "BKE_geometry_set.hh"
25#include "BKE_grease_pencil.hh"
26#include "BKE_lib_query.hh"
27#include "BKE_modifier.hh"
28#include "BKE_object_types.hh"
29
30#include "UI_interface.hh"
32#include "UI_resources.hh"
33
35#include "MOD_modifiertypes.hh"
36#include "MOD_ui_common.hh"
37
38#include "RNA_prototypes.hh"
39
40namespace blender {
41
51
52static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
53{
54 const auto *gmd = reinterpret_cast<const GreasePencilHookModifierData *>(md);
55 auto *tgmd = reinterpret_cast<GreasePencilHookModifierData *>(target);
56
58 modifier::greasepencil::copy_influence_data(&gmd->influence, &tgmd->influence, flag);
59}
60
61static void free_data(ModifierData *md)
62{
63 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
64
66}
67
68static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
69{
70 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
71
72 return (mmd->object == nullptr);
73}
74
76{
77 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
78 if (mmd->object != nullptr) {
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, (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
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);
157 bke::CurvesGeometry &curves = drawing.strokes_for_write();
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 == eHook_Falloff_None) ? 0.0f : mmd.falloff;
174 const float falloff_sq = square_f(falloff);
175 const float fac_orig = mmd.force;
176 const bool use_falloff = falloff_sq != 0.0f;
177 const bool use_uniform = (mmd.flag & MOD_GREASE_PENCIL_HOOK_UNIFORM_SPACE) != 0;
178
179 const float3x3 mat_uniform = use_uniform ? float3x3(float4x4(mmd.parentinv)) :
181 const float3 cent = use_uniform ? math::transform_point(mat_uniform, float3(mmd.cent)) :
182 float3(mmd.cent);
183
184 float4x4 dmat;
185 /* Get world-space matrix of target, corrected for the space the verts are in. */
186 if (mmd.subtarget[0]) {
187 bPoseChannel *pchan = BKE_pose_channel_find_name(mmd.object->pose, mmd.subtarget);
188 if (pchan) {
189 /* Bone target if there's a matching pose-channel. */
190 dmat = mmd.object->object_to_world() * float4x4(pchan->pose_mat);
191 }
192 }
193 else {
194 /* Just object target. */
195 dmat = mmd.object->object_to_world();
196 }
197 float4x4 use_mat = ob.world_to_object() * dmat * float4x4(mmd.parentinv);
198
199 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
200 MutableSpan<float3> positions = curves.positions_for_write();
201
202 strokes.foreach_index(blender::GrainSize(128), [&](const int stroke) {
203 const IndexRange points_range = points_by_curve[stroke].index_range();
204 for (const int point_i : points_range) {
205 const int point = point_i + points_by_curve[stroke].first();
206 const float weight = input_weights[point];
207 if (weight < 0.0f) {
208 continue;
209 }
210
211 float fac;
212 if (use_falloff) {
213 float len_sq;
214 if (use_uniform) {
215 const float3 co_uniform = math::transform_point(mat_uniform, positions[point]);
216 len_sq = math::distance(cent, co_uniform);
217 }
218 else {
219 len_sq = math::distance(cent, positions[point]);
220 }
221 fac = hook_falloff(
222 falloff, falloff_type, falloff_sq, fac_orig, mmd.influence.custom_curve, len_sq);
223 }
224 else {
225 fac = fac_orig;
226 }
227
228 if (fac != 0.0f) {
229 const float3 co_tmp = math::transform_point(use_mat, positions[point]);
230 positions[point] = math::interpolate(positions[point], co_tmp, fac * weight);
231 }
232 }
233 });
234
235 drawing.tag_positions_changed();
236}
237
239 const ModifierEvalContext *ctx,
240 bke::GeometrySet *geometry_set)
241{
242 const GreasePencilHookModifierData *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
243
244 if (!geometry_set->has_grease_pencil()) {
245 return;
246 }
247
248 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
249
250 const int current_frame = grease_pencil.runtime->eval_frame;
251
252 IndexMaskMemory mask_memory;
254 grease_pencil, mmd->influence, mask_memory);
256 modifier::greasepencil::get_drawings_for_write(grease_pencil, layer_mask, current_frame);
257
259 deform_drawing(*md, *ctx->object, *drawing);
260 });
261}
262
263static void panel_draw(const bContext *C, Panel *panel)
264{
265 uiLayout *layout = panel->layout;
266
267 PointerRNA ob_ptr;
269
270 PointerRNA hook_object_ptr = RNA_pointer_get(ptr, "object");
271
272 layout->use_property_split_set(true);
273
274 uiLayout *col = &layout->column(false);
275 col->prop(ptr, "object", UI_ITEM_NONE, std::nullopt, ICON_NONE);
276 if (!RNA_pointer_is_null(&hook_object_ptr) &&
277 RNA_enum_get(&hook_object_ptr, "type") == OB_ARMATURE)
278 {
279 PointerRNA hook_object_data_ptr = RNA_pointer_get(&hook_object_ptr, "data");
280 col->prop_search(ptr, "subtarget", &hook_object_data_ptr, "bones", IFACE_("Bone"), ICON_NONE);
281 }
282
283 layout->prop(ptr, "strength", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
284
285 if (uiLayout *sub = layout->panel_prop(C, ptr, "open_falloff_panel", IFACE_("Falloff"))) {
286 sub->use_property_split_set(true);
287
288 sub->prop(ptr, "falloff_type", UI_ITEM_NONE, IFACE_("Type"), ICON_NONE);
289
290 bool use_falloff = RNA_enum_get(ptr, "falloff_type") != eWarp_Falloff_None;
291
292 uiLayout *row = &sub->row(false);
293 row->active_set(use_falloff);
294 row->prop(ptr, "falloff_radius", UI_ITEM_NONE, std::nullopt, ICON_NONE);
295
296 sub->prop(ptr, "use_falloff_uniform", UI_ITEM_NONE, std::nullopt, ICON_NONE);
297
298 if (RNA_enum_get(ptr, "falloff_type") == eWarp_Falloff_Curve) {
299 uiTemplateCurveMapping(sub, ptr, "custom_curve", 0, false, false, false, false, false);
300 }
301 }
302
303 if (uiLayout *influence_panel = layout->panel_prop(
304 C, ptr, "open_influence_panel", IFACE_("Influence")))
305 {
309 }
310
312}
313
318
319} // namespace blender
320
322 /*idname*/ "GreasePencilHookModifier",
323 /*name*/ N_("Hook"),
324 /*struct_name*/ "GreasePencilHookModifierData",
325 /*struct_size*/ sizeof(GreasePencilHookModifierData),
326 /*srna*/ &RNA_GreasePencilHookModifier,
328 /*flags*/
331 /*icon*/ ICON_HOOK,
332
333 /*copy_data*/ blender::copy_data,
334
335 /*deform_verts*/ nullptr,
336 /*deform_matrices*/ nullptr,
337 /*deform_verts_EM*/ nullptr,
338 /*deform_matrices_EM*/ nullptr,
339 /*modify_mesh*/ nullptr,
340 /*modify_geometry_set*/ blender::modify_geometry_set,
341
342 /*init_data*/ blender::init_data,
343 /*required_data_mask*/ nullptr,
344 /*free_data*/ blender::free_data,
345 /*is_disabled*/ blender::is_disabled,
346 /*update_depsgraph*/ blender::update_depsgraph,
347 /*depends_on_time*/ nullptr,
348 /*depends_on_normals*/ nullptr,
349 /*foreach_ID_link*/ blender::foreach_ID_link,
350 /*foreach_tex_link*/ nullptr,
351 /*free_runtime_data*/ nullptr,
352 /*panel_register*/ blender::panel_register,
353 /*blend_write*/ blender::blend_write,
354 /*blend_read*/ blender::blend_read,
355};
Blender kernel action and pose functionality.
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
Low-level operations for curves.
Low-level operations for grease pencil.
@ IDWALK_CB_NOP
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 ATTR_FALLTHROUGH
MINLINE float square_f(float a)
#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)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
@ DEG_OB_COMP_TRANSFORM
#define DNA_struct_default_get(struct_name)
@ eWarp_Falloff_Curve
@ eWarp_Falloff_None
@ MOD_GREASE_PENCIL_HOOK_UNIFORM_SPACE
@ eModifierType_GreasePencilHook
@ MOD_GREASE_PENCIL_HOOK_Falloff_Root
@ MOD_GREASE_PENCIL_HOOK_Falloff_InvSquare
@ MOD_GREASE_PENCIL_HOOK_Falloff_Const
@ MOD_GREASE_PENCIL_HOOK_Falloff_Linear
@ MOD_GREASE_PENCIL_HOOK_Falloff_Sphere
@ MOD_GREASE_PENCIL_HOOK_Falloff_Smooth
@ MOD_GREASE_PENCIL_HOOK_Falloff_Curve
@ MOD_GREASE_PENCIL_HOOK_Falloff_Sharp
@ eHook_Falloff_None
@ OB_ARMATURE
static bool is_disabled
ModifierTypeInfo modifierType_GreasePencilHook
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 uiTemplateCurveMapping(uiLayout *layout, PointerRNA *ptr, blender::StringRefNull propname, int type, bool levels, bool brush, bool neg_slope, bool tone, bool presets)
@ UI_ITEM_R_SLIDER
#define UI_ITEM_NONE
MutableSpan< float3 > positions_for_write()
OffsetIndices< int > points_by_curve() const
bke::CurvesGeometry & strokes_for_write()
void foreach_index(Fn &&fn) const
uint col
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 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
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)
MatBase< float, 4, 4 > float4x4
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)
MatBase< float, 3, 3 > float3x3
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)
static void update_depsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
VecBase< float, 3 > float3
static bool is_disabled(const Scene *, ModifierData *md, bool)
static void blend_read(BlendDataReader *reader, ModifierData *md)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
bool RNA_pointer_is_null(const PointerRNA *ptr)
int RNA_enum_get(PointerRNA *ptr, const char *name)
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:414
struct uiLayout * layout
float pose_mat[4][4]
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)
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