Blender  V2.93
MOD_gpencilarmature.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) 2018, Blender Foundation
17  * This is a new part of Blender
18  */
19 
24 #include <stdio.h>
25 
26 #include "BLI_listbase.h"
27 #include "BLI_utildefines.h"
28 
29 #include "BLI_math.h"
30 
31 #include "BLT_translation.h"
32 
33 #include "DNA_defaults.h"
35 #include "DNA_gpencil_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 
41 #include "BKE_armature.h"
42 #include "BKE_context.h"
43 #include "BKE_gpencil.h"
44 #include "BKE_gpencil_geom.h"
45 #include "BKE_gpencil_modifier.h"
46 #include "BKE_lib_query.h"
47 #include "BKE_main.h"
48 #include "BKE_modifier.h"
49 #include "BKE_scene.h"
50 #include "BKE_screen.h"
51 
52 #include "MEM_guardedalloc.h"
53 
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56 
57 #include "RNA_access.h"
58 
60 #include "MOD_gpencil_ui_common.h"
61 
62 #include "DEG_depsgraph.h"
63 #include "DEG_depsgraph_build.h"
64 #include "DEG_depsgraph_query.h"
65 
66 static void initData(GpencilModifierData *md)
67 {
69 
70  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
71 
73 }
74 
75 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
76 {
78 }
79 
81 {
82  bGPDspoint *pt = gps->points;
83  float(*vert_coords)[3] = MEM_mallocN(sizeof(float[3]) * gps->totpoints, __func__);
84  int i;
85 
87 
88  /* prepare array of points */
89  for (i = 0; i < gps->totpoints; i++, pt++) {
90  copy_v3_v3(vert_coords[i], &pt->x);
91  }
92 
93  /* deform verts */
95  target,
96  vert_coords,
97  NULL,
98  gps->totpoints,
99  mmd->deformflag,
100  mmd->vert_coords_prev,
101  mmd->vgname,
102  gps);
103 
104  /* Apply deformed coordinates */
105  pt = gps->points;
106  for (i = 0; i < gps->totpoints; i++, pt++) {
107  copy_v3_v3(&pt->x, vert_coords[i]);
108  }
109 
110  MEM_freeN(vert_coords);
111 }
112 
113 /* deform stroke */
116  Object *ob,
117  bGPDlayer *UNUSED(gpl),
118  bGPDframe *UNUSED(gpf),
119  bGPDstroke *gps)
120 {
122  if (!mmd->object) {
123  return;
124  }
125  bGPdata *gpd = ob->data;
126 
127  gpencil_deform_verts(mmd, ob, gps);
128  /* Calc geometry data. */
130 }
131 
132 static void bakeModifier(Main *UNUSED(bmain),
135  Object *ob)
136 {
138  Object *object_eval = DEG_get_evaluated_object(depsgraph, ob);
140  GpencilModifierData *md_eval = BKE_gpencil_modifiers_findby_name(object_eval, md->name);
141  bGPdata *gpd = (bGPdata *)ob->data;
142  int oldframe = (int)DEG_get_ctime(depsgraph);
143 
144  if (mmd->object == NULL) {
145  return;
146  }
147 
148  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
149  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
150  /* apply armature effects on this frame
151  * NOTE: this assumes that we don't want armature animation on non-keyframed frames
152  */
153  CFRA = gpf->framenum;
155 
156  /* compute armature effects on this frame */
157  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
158  deformStroke(md_eval, depsgraph, object_eval, gpl, gpf, gps);
159  }
160  }
161  }
162 
163  /* return frame state and DB to original state */
164  CFRA = oldframe;
166 }
167 
168 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
169 {
171 
172  /* The object type check is only needed here in case we have a placeholder
173  * object assigned (because the library containing the armature is missing).
174  *
175  * In other cases it should be impossible to have a type mismatch. */
176  return !mmd->object || mmd->object->type != OB_ARMATURE;
177 }
178 
181  const int UNUSED(mode))
182 {
184  if (lmd->object != NULL) {
185  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_EVAL_POSE, "Armature Modifier");
186  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_TRANSFORM, "Armature Modifier");
187  }
188  DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Armature Modifier");
189 }
190 
191 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
192 {
194 
195  walk(userData, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
196 }
197 
198 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
199 {
200  uiLayout *sub, *row, *col;
201  uiLayout *layout = panel->layout;
202 
203  PointerRNA ob_ptr;
205 
206  bool has_vertex_group = RNA_string_length(ptr, "vertex_group") != 0;
207 
208  uiLayoutSetPropSep(layout, true);
209 
210  uiItemR(layout, ptr, "object", 0, NULL, ICON_NONE);
211  row = uiLayoutRow(layout, true);
212  uiItemPointerR(row, ptr, "vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
213  sub = uiLayoutRow(row, true);
214  uiLayoutSetActive(sub, has_vertex_group);
215  uiLayoutSetPropDecorate(sub, false);
216  uiItemR(sub, ptr, "invert_vertex_group", 0, "", ICON_ARROW_LEFTRIGHT);
217 
218  col = uiLayoutColumnWithHeading(layout, true, IFACE_("Bind To"));
219  uiItemR(col, ptr, "use_vertex_groups", 0, IFACE_("Vertex Groups"), ICON_NONE);
220  uiItemR(col, ptr, "use_bone_envelopes", 0, IFACE_("Bone Envelopes"), ICON_NONE);
221 
223 }
224 
225 static void panelRegister(ARegionType *region_type)
226 {
228 }
229 
231  /* name */ "Armature",
232  /* structName */ "ArmatureGpencilModifierData",
233  /* structSize */ sizeof(ArmatureGpencilModifierData),
236 
237  /* copyData */ copyData,
238 
239  /* deformStroke */ deformStroke,
240  /* generateStrokes */ NULL,
241  /* bakeModifier */ bakeModifier,
242  /* remapTime */ NULL,
243  /* initData */ initData,
244  /* freeData */ NULL,
245  /* isDisabled */ isDisabled,
246  /* updateDepsgraph */ updateDepsgraph,
247  /* dependsOnTime */ NULL,
248  /* foreachIDLink */ foreachIDLink,
249  /* foreachTexLink */ NULL,
250  /* panelRegister */ panelRegister,
251 };
typedef float(TangentPoint)[2]
void BKE_armature_deform_coords_with_gpencil_stroke(const struct Object *ob_arm, const struct Object *ob_target, float(*vert_coords)[3], float(*vert_deform_mats)[3][3], int vert_coords_len, int deformflag, float(*vert_coords_prev)[3], const char *defgrp_name, struct bGPDstroke *gps_target)
void BKE_gpencil_dvert_ensure(struct bGPDstroke *gps)
Definition: gpencil.c:2067
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
struct GpencilModifierData * BKE_gpencil_modifiers_findby_name(struct Object *ob, const char *name)
@ eGpencilModifierTypeType_Gpencil
@ 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
MINLINE void copy_v3_v3(float r[3], const float a[3])
#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_EVAL_POSE
@ DEG_OB_COMP_TRANSFORM
float DEG_get_ctime(const Depsgraph *graph)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
struct ArmatureGpencilModifierData ArmatureGpencilModifierData
@ eGpencilModifierType_Armature
Object is a sort of wrapper for general info.
@ OB_ARMATURE
#define CFRA
Read Guarded memory(de)allocation.
PointerRNA * gpencil_modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void gpencil_modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
PanelType * gpencil_modifier_panel_register(ARegionType *region_type, GpencilModifierType type, PanelDrawFn draw)
static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
static void gpencil_deform_verts(ArmatureGpencilModifierData *mmd, Object *target, bGPDstroke *gps)
static void updateDepsgraph(GpencilModifierData *md, const ModifierUpdateDepsgraphContext *ctx, const int UNUSED(mode))
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 deformStroke(GpencilModifierData *md, Depsgraph *UNUSED(depsgraph), Object *ob, bGPDlayer *UNUSED(gpl), bGPDframe *UNUSED(gpf), bGPDstroke *gps)
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_Armature
#define C
Definition: RandGen.cpp:39
void uiLayoutSetActive(uiLayout *layout, bool active)
uiLayout * uiLayoutColumnWithHeading(uiLayout *layout, bool align, const char *heading)
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 uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
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
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
int RNA_string_length(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6539
Definition: DNA_ID.h:273
Definition: BKE_main.h:116
struct DepsNodeHandle * node
Definition: BKE_modifier.h:147
void * data
struct uiLayout * layout
bGPDspoint * points
ListBase layers
PointerRNA * ptr
Definition: wm_files.c:3157