Blender V4.5
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"
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
50
51static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
52{
53 const auto *gmd = reinterpret_cast<const GreasePencilHookModifierData *>(md);
54 auto *tgmd = reinterpret_cast<GreasePencilHookModifierData *>(target);
55
57 modifier::greasepencil::copy_influence_data(&gmd->influence, &tgmd->influence, flag);
58}
59
60static void free_data(ModifierData *md)
61{
62 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
63
65}
66
67static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
68{
69 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
70
71 return (mmd->object == nullptr);
72}
73
75{
76 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
77 if (mmd->object != nullptr) {
78 DEG_add_object_relation(ctx->node, mmd->object, DEG_OB_COMP_TRANSFORM, "Hook Modifier");
79 }
80 DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Hook Modifier");
81}
82
83static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
84{
85 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
86
87 modifier::greasepencil::foreach_influence_ID_link(&mmd->influence, ob, walk, user_data);
88
89 walk(user_data, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
90}
91
92static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
93{
94 const auto *mmd = reinterpret_cast<const GreasePencilHookModifierData *>(md);
95
97 modifier::greasepencil::write_influence_data(writer, &mmd->influence);
98}
99
100static void blend_read(BlendDataReader *reader, ModifierData *md)
101{
102 auto *mmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
103 modifier::greasepencil::read_influence_data(reader, &mmd->influence);
104}
105
106/* Calculate the factor of falloff. */
107static float hook_falloff(const float falloff,
108 const int falloff_type,
109 const float falloff_sq,
110 const float fac_orig,
111 const CurveMapping *curfalloff,
112 const float len_sq)
113{
114 BLI_assert(falloff_sq);
115 if (len_sq > falloff_sq) {
116 return 0.0f;
117 }
118 if (len_sq <= 0.0f) {
119 return fac_orig;
120 }
121 if (falloff_type == MOD_GREASE_PENCIL_HOOK_Falloff_Const) {
122 return fac_orig;
123 }
124 if (falloff_type == MOD_GREASE_PENCIL_HOOK_Falloff_InvSquare) {
125 /* Avoid sqrt below. */
126 return (1.0f - (len_sq / falloff_sq)) * fac_orig;
127 }
128
129 float fac = 1.0f - (math::sqrt(len_sq) / falloff);
130
131 switch (falloff_type) {
133 return BKE_curvemapping_evaluateF(curfalloff, 0, fac) * fac_orig;
135 return fac * fac * fac_orig;
137 return (3.0f * fac * fac - 2.0f * fac * fac * fac) * fac_orig;
139 return math::sqrt(fac) * fac_orig;
140 break;
142 return math::sqrt(2 * fac - fac * fac) * fac_orig;
144 ATTR_FALLTHROUGH; /* Pass. */
145 default:
146 return fac * fac_orig;
147 }
148}
149
150static void deform_drawing(const ModifierData &md,
151 const Object &ob,
153{
154 const auto &mmd = reinterpret_cast<const GreasePencilHookModifierData &>(md);
156 bke::CurvesGeometry &curves = drawing.strokes_for_write();
157
158 if (curves.is_empty()) {
159 return;
160 }
161 IndexMaskMemory memory;
163 &ob, curves, mmd.influence, memory);
164 if (strokes.is_empty()) {
165 return;
166 }
167
169 curves, mmd.influence);
170
171 const int falloff_type = mmd.falloff_type;
172 const float falloff = (mmd.falloff_type == eHook_Falloff_None) ? 0.0f : mmd.falloff;
173 const float falloff_sq = square_f(falloff);
174 const float fac_orig = mmd.force;
175 const bool use_falloff = falloff_sq != 0.0f;
176 const bool use_uniform = (mmd.flag & MOD_GREASE_PENCIL_HOOK_UNIFORM_SPACE) != 0;
177
178 const float3x3 mat_uniform = use_uniform ? float3x3(float4x4(mmd.parentinv)) :
180 const float3 cent = use_uniform ? math::transform_point(mat_uniform, float3(mmd.cent)) :
181 float3(mmd.cent);
182
183 float4x4 dmat;
184 /* Get world-space matrix of target, corrected for the space the verts are in. */
185 if (mmd.subtarget[0]) {
186 bPoseChannel *pchan = BKE_pose_channel_find_name(mmd.object->pose, mmd.subtarget);
187 if (pchan) {
188 /* Bone target if there's a matching pose-channel. */
189 dmat = mmd.object->object_to_world() * float4x4(pchan->pose_mat);
190 }
191 }
192 else {
193 /* Just object target. */
194 dmat = mmd.object->object_to_world();
195 }
196 float4x4 use_mat = ob.world_to_object() * dmat * float4x4(mmd.parentinv);
197
198 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
199 MutableSpan<float3> positions = curves.positions_for_write();
200
201 strokes.foreach_index(blender::GrainSize(128), [&](const int stroke) {
202 const IndexRange points_range = points_by_curve[stroke].index_range();
203 for (const int point_i : points_range) {
204 const int point = point_i + points_by_curve[stroke].first();
205 const float weight = input_weights[point];
206 if (weight < 0.0f) {
207 continue;
208 }
209
210 float fac;
211 if (use_falloff) {
212 float len_sq;
213 if (use_uniform) {
214 const float3 co_uniform = math::transform_point(mat_uniform, positions[point]);
215 len_sq = math::distance(cent, co_uniform);
216 }
217 else {
218 len_sq = math::distance(cent, positions[point]);
219 }
220 fac = hook_falloff(
221 falloff, falloff_type, falloff_sq, fac_orig, mmd.influence.custom_curve, len_sq);
222 }
223 else {
224 fac = fac_orig;
225 }
226
227 if (fac != 0.0f) {
228 const float3 co_tmp = math::transform_point(use_mat, positions[point]);
229 positions[point] = math::interpolate(positions[point], co_tmp, fac * weight);
230 }
231 }
232 });
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
247 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
248
249 const int current_frame = grease_pencil.runtime->eval_frame;
250
251 IndexMaskMemory mask_memory;
253 grease_pencil, mmd->influence, mask_memory);
255 modifier::greasepencil::get_drawings_for_write(grease_pencil, layer_mask, current_frame);
256
258 deform_drawing(*md, *ctx->object, *drawing);
259 });
260}
261
262static void panel_draw(const bContext *C, Panel *panel)
263{
264 uiLayout *layout = panel->layout;
265
266 PointerRNA ob_ptr;
268
269 PointerRNA hook_object_ptr = RNA_pointer_get(ptr, "object");
270
271 uiLayoutSetPropSep(layout, true);
272
273 uiLayout *col = &layout->column(false);
274 col->prop(ptr, "object", UI_ITEM_NONE, std::nullopt, ICON_NONE);
275 if (!RNA_pointer_is_null(&hook_object_ptr) &&
276 RNA_enum_get(&hook_object_ptr, "type") == OB_ARMATURE)
277 {
278 PointerRNA hook_object_data_ptr = RNA_pointer_get(&hook_object_ptr, "data");
280 col, 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 uiLayoutSetPropSep(sub, 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 uiLayoutSetActive(row, 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);
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)
@ UI_ITEM_R_SLIDER
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
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:404
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)
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