Blender  V2.93
MOD_gpencilmirror.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_utildefines.h"
27 
28 #include "BLI_listbase.h"
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_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 
40 #include "BKE_context.h"
41 #include "BKE_gpencil.h"
42 #include "BKE_gpencil_modifier.h"
43 #include "BKE_lib_query.h"
44 #include "BKE_main.h"
45 #include "BKE_modifier.h"
46 #include "BKE_scene.h"
47 #include "BKE_screen.h"
48 
49 #include "UI_interface.h"
50 #include "UI_resources.h"
51 
53 #include "MOD_gpencil_ui_common.h"
54 #include "MOD_gpencil_util.h"
55 
56 #include "DEG_depsgraph.h"
57 #include "DEG_depsgraph_build.h"
58 #include "DEG_depsgraph_query.h"
59 
60 static void initData(GpencilModifierData *md)
61 {
63 
64  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
65 
67 }
68 
69 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
70 {
72 }
73 
74 /* Mirror is using current object as origin. */
75 static void update_mirror_local(bGPDstroke *gps, int axis)
76 {
77  int i;
78  bGPDspoint *pt;
79  float factor[3] = {1.0f, 1.0f, 1.0f};
80  factor[axis] = -1.0f;
81 
82  for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
83  mul_v3_v3(&pt->x, factor);
84  }
85 }
86 
87 /* Mirror is using other object as origin. */
88 static void update_mirror_object(Object *ob,
90  bGPDstroke *gps,
91  int axis)
92 {
93  float mtx[4][4];
94  unit_m4(mtx);
95  mtx[axis][axis] = -1.0f;
96 
97  float tmp[4][4];
98  float itmp[4][4];
99  invert_m4_m4(tmp, mmd->object->obmat);
100  mul_m4_m4m4(tmp, tmp, ob->obmat);
101  invert_m4_m4(itmp, tmp);
102  mul_m4_series(mtx, itmp, mtx, tmp);
103 
104  for (int i = 0; i < gps->totpoints; i++) {
105  mul_m4_v3(mtx, &gps->points[i].x);
106  }
107 }
108 
109 static void update_position(Object *ob, MirrorGpencilModifierData *mmd, bGPDstroke *gps, int axis)
110 {
111  if (mmd->object == NULL) {
112  update_mirror_local(gps, axis);
113  }
114  else {
115  update_mirror_object(ob, mmd, gps, axis);
116  }
117 }
118 
120 {
122  bGPDstroke *gps, *gps_new = NULL;
123  int tot_strokes;
124  int i;
125 
126  /* check each axis for mirroring */
127  for (int xi = 0; xi < 3; xi++) {
128  if (mmd->flag & (GP_MIRROR_AXIS_X << xi)) {
129 
130  /* count strokes to avoid infinite loop after adding new strokes to tail of listbase */
131  tot_strokes = BLI_listbase_count(&gpf->strokes);
132 
133  for (i = 0, gps = gpf->strokes.first; i < tot_strokes; i++, gps = gps->next) {
135  mmd->layername,
136  mmd->material,
137  mmd->pass_index,
138  mmd->layer_pass,
139  1,
140  gpl,
141  gps,
146  gps_new = BKE_gpencil_stroke_duplicate(gps, true, true);
147  update_position(ob, mmd, gps_new, xi);
148  BLI_addtail(&gpf->strokes, gps_new);
149  }
150  }
151  }
152  }
153 }
154 
155 /* Generic "generateStrokes" callback */
157 {
159  bGPdata *gpd = (bGPdata *)ob->data;
160 
161  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
163  if (gpf == NULL) {
164  continue;
165  }
166  generate_geometry(md, ob, gpl, gpf);
167  }
168 }
169 
170 static void bakeModifier(Main *UNUSED(bmain),
173  Object *ob)
174 {
176  bGPdata *gpd = ob->data;
177  int oldframe = (int)DEG_get_ctime(depsgraph);
178 
179  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
180  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
181  /* apply mirror effects on this frame */
182  CFRA = gpf->framenum;
184 
185  /* compute mirror effects on this frame */
186  generate_geometry(md, ob, gpl, gpf);
187  }
188  }
189 
190  /* return frame state and DB to original state */
191  CFRA = oldframe;
193 }
194 
195 static bool isDisabled(GpencilModifierData *UNUSED(md), int UNUSED(userRenderParams))
196 {
197  // MirrorGpencilModifierData *mmd = (MirrorGpencilModifierData *)md;
198 
199  return false;
200 }
201 
204  const int UNUSED(mode))
205 {
207  if (lmd->object != NULL) {
208  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_GEOMETRY, "Mirror Modifier");
209  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_TRANSFORM, "Mirror Modifier");
210  }
211  DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Mirror Modifier");
212 }
213 
214 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
215 {
217 
218  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
219  walk(userData, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
220 }
221 
222 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
223 {
224  uiLayout *row;
225  uiLayout *layout = panel->layout;
227 
229 
230  uiLayoutSetPropSep(layout, true);
231 
232  row = uiLayoutRowWithHeading(layout, true, IFACE_("Axis"));
233  uiItemR(row, ptr, "use_axis_x", toggles_flag, NULL, ICON_NONE);
234  uiItemR(row, ptr, "use_axis_y", toggles_flag, NULL, ICON_NONE);
235  uiItemR(row, ptr, "use_axis_z", toggles_flag, NULL, ICON_NONE);
236 
237  uiItemR(layout, ptr, "object", 0, NULL, ICON_NONE);
238 
240 }
241 
242 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
243 {
244  gpencil_modifier_masking_panel_draw(panel, true, false);
245 }
246 
247 static void panelRegister(ARegionType *region_type)
248 {
252  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
253 }
254 
256  /* name */ "Mirror",
257  /* structName */ "MirrorGpencilModifierData",
258  /* structSize */ sizeof(MirrorGpencilModifierData),
261 
262  /* copyData */ copyData,
263 
264  /* deformStroke */ NULL,
265  /* generateStrokes */ generateStrokes,
266  /* bakeModifier */ bakeModifier,
267  /* remapTime */ NULL,
268 
269  /* initData */ initData,
270  /* freeData */ NULL,
271  /* isDisabled */ isDisabled,
272  /* updateDepsgraph */ updateDepsgraph,
273  /* dependsOnTime */ NULL,
274  /* foreachIDLink */ foreachIDLink,
275  /* foreachTexLink */ NULL,
276  /* panelRegister */ panelRegister,
277 };
struct bGPDstroke * BKE_gpencil_stroke_duplicate(struct bGPDstroke *gps_src, const bool dup_points, const bool dup_curve)
Definition: gpencil.c:957
void BKE_gpencil_modifier_copydata_generic(const struct GpencilModifierData *md_src, struct GpencilModifierData *md_dst)
@ eGpencilModifierTypeFlag_SupportsEditmode
struct bGPDframe * BKE_gpencil_frame_retime_get(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct bGPDlayer *gpl)
@ eGpencilModifierTypeType_Gpencil
@ 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
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void unit_m4(float m[4][4])
Definition: rct.c:1140
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
#define mul_m4_series(...)
MINLINE void mul_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_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
struct MirrorGpencilModifierData MirrorGpencilModifierData
@ GP_MIRROR_INVERT_MATERIAL
@ GP_MIRROR_INVERT_LAYER
@ GP_MIRROR_INVERT_LAYERPASS
@ eGpencilModifierType_Mirror
Object is a sort of wrapper for general info.
#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)
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 update_position(Object *ob, MirrorGpencilModifierData *mmd, bGPDstroke *gps, int axis)
static void updateDepsgraph(GpencilModifierData *md, const ModifierUpdateDepsgraphContext *ctx, const int UNUSED(mode))
static bool isDisabled(GpencilModifierData *UNUSED(md), int UNUSED(userRenderParams))
static void update_mirror_object(Object *ob, MirrorGpencilModifierData *mmd, bGPDstroke *gps, int axis)
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 generate_geometry(GpencilModifierData *md, Object *ob, bGPDlayer *gpl, bGPDframe *gpf)
static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
static void update_mirror_local(bGPDstroke *gps, int axis)
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
GpencilModifierTypeInfo modifierType_Gpencil_Mirror
static void panelRegister(ARegionType *region_type)
static void initData(GpencilModifierData *md)
static void generateStrokes(GpencilModifierData *md, Depsgraph *depsgraph, Object *ob)
#define C
Definition: RandGen.cpp:39
uiLayout * uiLayoutRowWithHeading(uiLayout *layout, bool align, const char *heading)
@ UI_ITEM_R_TOGGLE
@ UI_ITEM_R_FORCE_BLANK_DECORATE
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
Scene scene
const Depsgraph * depsgraph
static ulong * next
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
struct DepsNodeHandle * node
Definition: BKE_modifier.h:147
float obmat[4][4]
void * data
struct uiLayout * layout
ListBase strokes
bGPDspoint * points
ListBase layers
PointerRNA * ptr
Definition: wm_files.c:3157