Blender  V3.3
MOD_gpencillattice.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2017 Blender Foundation. */
3 
8 #include <stdio.h>
9 #include <string.h> /* For #MEMCPY_STRUCT_AFTER. */
10 
11 #include "BLI_listbase.h"
12 #include "BLI_utildefines.h"
13 
14 #include "BLT_translation.h"
15 
16 #include "DNA_defaults.h"
18 #include "DNA_gpencil_types.h"
19 #include "DNA_meshdata_types.h"
20 #include "DNA_object_types.h"
21 #include "DNA_scene_types.h"
22 #include "DNA_screen_types.h"
23 
24 #include "BKE_context.h"
25 #include "BKE_deform.h"
26 #include "BKE_gpencil_geom.h"
27 #include "BKE_gpencil_modifier.h"
28 #include "BKE_lattice.h"
29 #include "BKE_lib_query.h"
30 #include "BKE_main.h"
31 #include "BKE_modifier.h"
32 #include "BKE_scene.h"
33 #include "BKE_screen.h"
34 
35 #include "UI_interface.h"
36 #include "UI_resources.h"
37 
38 #include "RNA_access.h"
39 
41 #include "MOD_gpencil_ui_common.h"
42 #include "MOD_gpencil_util.h"
43 
44 #include "DEG_depsgraph.h"
45 #include "DEG_depsgraph_build.h"
46 #include "DEG_depsgraph_query.h"
47 
48 static void initData(GpencilModifierData *md)
49 {
51 
52  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
53 
55 }
56 
57 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
58 {
60 }
61 
64  Object *ob,
65  bGPDlayer *gpl,
66  bGPDframe *UNUSED(gpf),
67  bGPDstroke *gps)
68 {
69  bGPdata *gpd = ob->data;
71  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
72 
74  mmd->layername,
75  mmd->material,
76  mmd->pass_index,
77  mmd->layer_pass,
78  1,
79  gpl,
80  gps,
85  return;
86  }
87 
88  if (mmd->cache_data == NULL) {
89  return;
90  }
91 
92  for (int i = 0; i < gps->totpoints; i++) {
93  bGPDspoint *pt = &gps->points[i];
94  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
95 
96  /* verify vertex group */
97  const float weight = get_modifier_point_weight(
98  dvert, (mmd->flag & GP_LATTICE_INVERT_VGROUP) != 0, def_nr);
99  if (weight < 0.0f) {
100  continue;
101  }
103  (struct LatticeDeformData *)mmd->cache_data, &pt->x, mmd->strength * weight);
104  }
105  /* Calc geometry data. */
107 }
108 
109 /* FIXME: Ideally we be doing this on a copy of the main depsgraph
110  * (i.e. one where we don't have to worry about restoring state)
111  */
112 static void bakeModifier(Main *UNUSED(bmain),
115  Object *ob)
116 {
119  struct LatticeDeformData *ldata = NULL;
120  bGPdata *gpd = ob->data;
121  int oldframe = (int)DEG_get_ctime(depsgraph);
122 
123  if ((mmd->object == NULL) || (mmd->object->type != OB_LATTICE)) {
124  return;
125  }
126 
127  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
128  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
129  /* Apply lattice effects on this frame
130  * NOTE: this assumes that we don't want lattice animation on non-keyframed frames.
131  */
132  scene->r.cfra = gpf->framenum;
134 
135  /* Recalculate lattice data. */
136  if (mmd->cache_data) {
138  }
140 
141  /* Compute lattice effects on this frame. */
142  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
143  deformStroke(md, depsgraph, ob, gpl, gpf, gps);
144  }
145  }
146  }
147 
148  /* Free lingering data. */
149  ldata = (struct LatticeDeformData *)mmd->cache_data;
150  if (ldata) {
152  mmd->cache_data = NULL;
153  }
154 
155  /* Return frame state and DB to original state. */
156  scene->r.cfra = oldframe;
158 }
159 
161 {
163  struct LatticeDeformData *ldata = (struct LatticeDeformData *)mmd->cache_data;
164  /* free deform data */
165  if (ldata) {
167  }
168 }
169 
170 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
171 {
173 
174  /* The object type check is only needed here in case we have a placeholder
175  * object assigned (because the library containing the lattice is missing).
176  *
177  * In other cases it should be impossible to have a type mismatch.
178  */
179  return !mmd->object || mmd->object->type != OB_LATTICE;
180 }
181 
184  const int UNUSED(mode))
185 {
187  if (lmd->object != NULL) {
188  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_GEOMETRY, "Lattice Modifier");
189  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_TRANSFORM, "Lattice Modifier");
190  }
191  DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Lattice Modifier");
192 }
193 
194 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
195 {
197 
198  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
199  walk(userData, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
200 }
201 
202 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
203 {
204  uiLayout *sub, *row, *col;
205  uiLayout *layout = panel->layout;
206 
207  PointerRNA ob_ptr;
209 
210  PointerRNA hook_object_ptr = RNA_pointer_get(ptr, "object");
211  bool has_vertex_group = RNA_string_length(ptr, "vertex_group") != 0;
212 
213  uiLayoutSetPropSep(layout, true);
214 
215  col = uiLayoutColumn(layout, false);
216  uiItemR(col, ptr, "object", 0, NULL, ICON_NONE);
217  if (!RNA_pointer_is_null(&hook_object_ptr) &&
218  RNA_enum_get(&hook_object_ptr, "type") == OB_ARMATURE) {
219  PointerRNA hook_object_data_ptr = RNA_pointer_get(&hook_object_ptr, "data");
221  col, ptr, "subtarget", &hook_object_data_ptr, "bones", IFACE_("Bone"), ICON_NONE);
222  }
223 
224  row = uiLayoutRow(layout, true);
225  uiItemPointerR(row, ptr, "vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
226  sub = uiLayoutRow(row, true);
227  uiLayoutSetActive(sub, has_vertex_group);
228  uiLayoutSetPropSep(sub, false);
229  uiItemR(sub, ptr, "invert_vertex", 0, "", ICON_ARROW_LEFTRIGHT);
230 
231  uiItemR(layout, ptr, "strength", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
232 
234 }
235 
236 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
237 {
238  gpencil_modifier_masking_panel_draw(panel, true, false);
239 }
240 
241 static void panelRegister(ARegionType *region_type)
242 {
246  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
247 }
248 
250  /* name */ N_("Lattice"),
251  /* structName */ "LatticeGpencilModifierData",
252  /* structSize */ sizeof(LatticeGpencilModifierData),
255 
256  /* copyData */ copyData,
257 
258  /* deformStroke */ deformStroke,
259  /* generateStrokes */ NULL,
260  /* bakeModifier */ bakeModifier,
261  /* remapTime */ NULL,
262 
263  /* initData */ initData,
264  /* freeData */ freeData,
265  /* isDisabled */ isDisabled,
266  /* updateDepsgraph */ updateDepsgraph,
267  /* dependsOnTime */ NULL,
268  /* foreachIDLink */ foreachIDLink,
269  /* foreachTexLink */ NULL,
270  /* panelRegister */ panelRegister,
271 };
support for deformation groups and hooks.
int BKE_object_defgroup_name_index(const struct Object *ob, const char *name)
void BKE_gpencil_stroke_geometry_update(struct bGPdata *gpd, struct bGPDstroke *gps)
void BKE_gpencil_modifier_copydata_generic(const struct GpencilModifierData *md_src, struct GpencilModifierData *md_dst)
@ eGpencilModifierTypeFlag_SupportsEditmode
@ eGpencilModifierTypeType_Gpencil
void BKE_lattice_deform_data_destroy(struct LatticeDeformData *lattice_deform_data)
void BKE_lattice_deform_data_eval_co(struct LatticeDeformData *lattice_deform_data, float co[3], float weight)
struct LatticeDeformData * BKE_lattice_deform_data_create(const struct Object *oblatt, const struct Object *ob) ATTR_WARN_UNUSED_RESULT
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:73
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:33
void(* IDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: BKE_modifier.h:107
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.cc:2728
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define IFACE_(msgid)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
void DEG_add_object_relation(struct DepsNodeHandle *node_handle, struct Object *object, eDepsObjectComponentType component, const char *description)
@ DEG_OB_COMP_GEOMETRY
@ DEG_OB_COMP_TRANSFORM
float DEG_get_ctime(const Depsgraph *graph)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
@ GP_LATTICE_INVERT_LAYERPASS
@ GP_LATTICE_INVERT_PASS
@ GP_LATTICE_INVERT_LAYER
@ GP_LATTICE_INVERT_VGROUP
@ GP_LATTICE_INVERT_MATERIAL
struct LatticeGpencilModifierData LatticeGpencilModifierData
@ eGpencilModifierType_Lattice
Object is a sort of wrapper for general info.
@ OB_LATTICE
@ OB_ARMATURE
PointerRNA * gpencil_modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void gpencil_modifier_masking_panel_draw(Panel *panel, bool use_material, bool use_vertex)
void gpencil_modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
PanelType * gpencil_modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
PanelType * gpencil_modifier_panel_register(ARegionType *region_type, GpencilModifierType type, PanelDrawFn draw)
float get_modifier_point_weight(MDeformVert *dvert, bool inverse, int def_nr)
bool is_stroke_affected_by_modifier(Object *ob, char *mlayername, Material *material, const int mpassindex, const int gpl_passindex, const int minpoints, bGPDlayer *gpl, bGPDstroke *gps, const bool inv1, const bool inv2, const bool inv3, const bool inv4)
static void deformStroke(GpencilModifierData *md, Depsgraph *UNUSED(depsgraph), Object *ob, bGPDlayer *gpl, bGPDframe *UNUSED(gpf), bGPDstroke *gps)
static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
static void freeData(GpencilModifierData *md)
static void updateDepsgraph(GpencilModifierData *md, const ModifierUpdateDepsgraphContext *ctx, const int UNUSED(mode))
static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
static void bakeModifier(Main *UNUSED(bmain), Depsgraph *depsgraph, GpencilModifierData *md, Object *ob)
static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
static void panelRegister(ARegionType *region_type)
static void initData(GpencilModifierData *md)
GpencilModifierTypeInfo modifierType_Gpencil_Lattice
#define C
Definition: RandGen.cpp:25
void uiLayoutSetActive(uiLayout *layout, bool active)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
uiLayout * uiLayoutRow(uiLayout *layout, bool align)
@ UI_ITEM_R_SLIDER
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon)
Scene scene
const Depsgraph * depsgraph
uint col
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5167
int RNA_string_length(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5144
bool RNA_pointer_is_null(const PointerRNA *ptr)
Definition: rna_access.c:164
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
Definition: DNA_ID.h:368
struct LatticeDeformData * cache_data
Definition: BKE_main.h:121
struct DepsNodeHandle * node
Definition: BKE_modifier.h:134
void * data
struct uiLayout * layout
struct RenderData r
ListBase frames
bGPDspoint * points
struct MDeformVert * dvert
ListBase layers
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480