Blender  V2.93
MOD_gpencillattice.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2017, Blender Foundation
17  * This is a new part of Blender
18  */
19 
24 #include <stdio.h>
25 #include <string.h> /* For #MEMCPY_STRUCT_AFTER. */
26 
27 #include "BLI_listbase.h"
28 #include "BLI_utildefines.h"
29 
30 #include "BLT_translation.h"
31 
32 #include "DNA_defaults.h"
34 #include "DNA_gpencil_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 
40 #include "BKE_context.h"
41 #include "BKE_deform.h"
42 #include "BKE_gpencil_geom.h"
43 #include "BKE_gpencil_modifier.h"
44 #include "BKE_lattice.h"
45 #include "BKE_lib_query.h"
46 #include "BKE_main.h"
47 #include "BKE_modifier.h"
48 #include "BKE_scene.h"
49 #include "BKE_screen.h"
50 
51 #include "UI_interface.h"
52 #include "UI_resources.h"
53 
54 #include "RNA_access.h"
55 
57 #include "MOD_gpencil_ui_common.h"
58 #include "MOD_gpencil_util.h"
59 
60 #include "DEG_depsgraph.h"
61 #include "DEG_depsgraph_build.h"
62 #include "DEG_depsgraph_query.h"
63 
64 static void initData(GpencilModifierData *md)
65 {
67 
68  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
69 
71 }
72 
73 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
74 {
76 }
77 
80  Object *ob,
81  bGPDlayer *gpl,
82  bGPDframe *UNUSED(gpf),
83  bGPDstroke *gps)
84 {
85  bGPdata *gpd = ob->data;
87  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
88 
90  mmd->layername,
91  mmd->material,
92  mmd->pass_index,
93  mmd->layer_pass,
94  1,
95  gpl,
96  gps,
101  return;
102  }
103 
104  if (mmd->cache_data == NULL) {
105  return;
106  }
107 
108  for (int i = 0; i < gps->totpoints; i++) {
109  bGPDspoint *pt = &gps->points[i];
110  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
111 
112  /* verify vertex group */
113  const float weight = get_modifier_point_weight(
114  dvert, (mmd->flag & GP_LATTICE_INVERT_VGROUP) != 0, def_nr);
115  if (weight < 0.0f) {
116  continue;
117  }
119  (struct LatticeDeformData *)mmd->cache_data, &pt->x, mmd->strength * weight);
120  }
121  /* Calc geometry data. */
123 }
124 
125 /* FIXME: Ideally we be doing this on a copy of the main depsgraph
126  * (i.e. one where we don't have to worry about restoring state)
127  */
128 static void bakeModifier(Main *UNUSED(bmain),
131  Object *ob)
132 {
135  struct LatticeDeformData *ldata = NULL;
136  bGPdata *gpd = ob->data;
137  int oldframe = (int)DEG_get_ctime(depsgraph);
138 
139  if ((mmd->object == NULL) || (mmd->object->type != OB_LATTICE)) {
140  return;
141  }
142 
143  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
144  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
145  /* Apply lattice effects on this frame
146  * NOTE: this assumes that we don't want lattice animation on non-keyframed frames.
147  */
148  CFRA = gpf->framenum;
150 
151  /* Recalculate lattice data. */
152  if (mmd->cache_data) {
154  }
156 
157  /* Compute lattice effects on this frame. */
158  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
159  deformStroke(md, depsgraph, ob, gpl, gpf, gps);
160  }
161  }
162  }
163 
164  /* Free lingering data. */
165  ldata = (struct LatticeDeformData *)mmd->cache_data;
166  if (ldata) {
168  mmd->cache_data = NULL;
169  }
170 
171  /* Return frame state and DB to original state. */
172  CFRA = oldframe;
174 }
175 
177 {
179  struct LatticeDeformData *ldata = (struct LatticeDeformData *)mmd->cache_data;
180  /* free deform data */
181  if (ldata) {
183  }
184 }
185 
186 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
187 {
189 
190  /* The object type check is only needed here in case we have a placeholder
191  * object assigned (because the library containing the lattice is missing).
192  *
193  * In other cases it should be impossible to have a type mismatch.
194  */
195  return !mmd->object || mmd->object->type != OB_LATTICE;
196 }
197 
200  const int UNUSED(mode))
201 {
203  if (lmd->object != NULL) {
204  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_GEOMETRY, "Lattice Modifier");
205  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_TRANSFORM, "Lattice Modifier");
206  }
207  DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Lattice Modifier");
208 }
209 
210 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
211 {
213 
214  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
215  walk(userData, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
216 }
217 
218 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
219 {
220  uiLayout *sub, *row, *col;
221  uiLayout *layout = panel->layout;
222 
223  PointerRNA ob_ptr;
225 
226  PointerRNA hook_object_ptr = RNA_pointer_get(ptr, "object");
227  bool has_vertex_group = RNA_string_length(ptr, "vertex_group") != 0;
228 
229  uiLayoutSetPropSep(layout, true);
230 
231  col = uiLayoutColumn(layout, false);
232  uiItemR(col, ptr, "object", 0, NULL, ICON_NONE);
233  if (!RNA_pointer_is_null(&hook_object_ptr) &&
234  RNA_enum_get(&hook_object_ptr, "type") == OB_ARMATURE) {
235  PointerRNA hook_object_data_ptr = RNA_pointer_get(&hook_object_ptr, "data");
237  col, ptr, "subtarget", &hook_object_data_ptr, "bones", IFACE_("Bone"), ICON_NONE);
238  }
239 
240  row = uiLayoutRow(layout, true);
241  uiItemPointerR(row, ptr, "vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
242  sub = uiLayoutRow(row, true);
243  uiLayoutSetActive(sub, has_vertex_group);
244  uiLayoutSetPropSep(sub, false);
245  uiItemR(sub, ptr, "invert_vertex", 0, "", ICON_ARROW_LEFTRIGHT);
246 
247  uiItemR(layout, ptr, "strength", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
248 
250 }
251 
252 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
253 {
254  gpencil_modifier_masking_panel_draw(panel, true, false);
255 }
256 
257 static void panelRegister(ARegionType *region_type)
258 {
262  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
263 }
264 
266  /* name */ "Lattice",
267  /* structName */ "LatticeGpencilModifierData",
268  /* structSize */ sizeof(LatticeGpencilModifierData),
271 
272  /* copyData */ copyData,
273 
274  /* deformStroke */ deformStroke,
275  /* generateStrokes */ NULL,
276  /* bakeModifier */ bakeModifier,
277  /* remapTime */ NULL,
278 
279  /* initData */ initData,
280  /* freeData */ freeData,
281  /* isDisabled */ isDisabled,
282  /* updateDepsgraph */ updateDepsgraph,
283  /* dependsOnTime */ NULL,
284  /* foreachIDLink */ foreachIDLink,
285  /* foreachTexLink */ NULL,
286  /* panelRegister */ panelRegister,
287 };
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:87
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:47
void(* IDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: BKE_modifier.h:120
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.c:2794
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#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:51
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:44
@ 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
#define CFRA
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:39
void uiLayoutSetActive(uiLayout *layout, bool active)
@ UI_ITEM_R_SLIDER
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
uiLayout * uiLayoutRow(uiLayout *layout, bool align)
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:6562
int RNA_string_length(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6539
bool RNA_pointer_is_null(const PointerRNA *ptr)
Definition: rna_access.c:174
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
Definition: DNA_ID.h:273
struct LatticeDeformData * cache_data
Definition: BKE_main.h:116
struct DepsNodeHandle * node
Definition: BKE_modifier.h:147
void * data
struct uiLayout * layout
ListBase frames
bGPDspoint * points
struct MDeformVert * dvert
ListBase layers
PointerRNA * ptr
Definition: wm_files.c:3157