Blender  V3.3
io_gpencil_import.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
8 #ifdef WITH_IO_GPENCIL
9 
10 # include "BLI_path_util.h"
11 
12 # include "MEM_guardedalloc.h"
13 
14 # include "DNA_gpencil_types.h"
15 # include "DNA_space_types.h"
16 
17 # include "BKE_context.h"
18 # include "BKE_gpencil.h"
19 # include "BKE_report.h"
20 
21 # include "BLT_translation.h"
22 
23 # include "RNA_access.h"
24 # include "RNA_define.h"
25 
26 # include "UI_interface.h"
27 # include "UI_resources.h"
28 
29 # include "WM_api.h"
30 # include "WM_types.h"
31 
32 # include "DEG_depsgraph.h"
33 # include "DEG_depsgraph_query.h"
34 
35 # include "ED_gpencil.h"
36 
37 # include "io_gpencil.h"
38 
39 # include "gpencil_io.h"
40 
41 /* <-------- SVG single frame import. --------> */
42 static bool wm_gpencil_import_svg_common_check(bContext *UNUSED(C), wmOperator *op)
43 {
44 
45  char filepath[FILE_MAX];
46  RNA_string_get(op->ptr, "filepath", filepath);
47 
48  if (!BLI_path_extension_check(filepath, ".svg")) {
49  BLI_path_extension_ensure(filepath, FILE_MAX, ".svg");
50  RNA_string_set(op->ptr, "filepath", filepath);
51  return true;
52  }
53 
54  return false;
55 }
56 
57 static int wm_gpencil_import_svg_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
58 {
60 
62 }
63 
64 static int wm_gpencil_import_svg_exec(bContext *C, wmOperator *op)
65 {
67 
68  if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false) ||
69  !(RNA_struct_find_property(op->ptr, "directory"))) {
70  BKE_report(op->reports, RPT_ERROR, "No filename given");
71  return OPERATOR_CANCELLED;
72  }
73 
74  ARegion *region = get_invoke_region(C);
75  if (region == NULL) {
76  BKE_report(op->reports, RPT_ERROR, "Unable to find valid 3D View area");
77  return OPERATOR_CANCELLED;
78  }
79  View3D *v3d = get_invoke_view3d(C);
80 
81  /* Set flags. */
82  int flag = 0;
83 
84  const int resolution = RNA_int_get(op->ptr, "resolution");
85  const float scale = RNA_float_get(op->ptr, "scale");
86 
88  .C = C,
89  .region = region,
90  .v3d = v3d,
91  .ob = NULL,
92  .mode = GP_IMPORT_FROM_SVG,
93  .frame_start = scene->r.cfra,
94  .frame_end = scene->r.cfra,
95  .frame_cur = scene->r.cfra,
96  .flag = flag,
97  .scale = scale,
98  .select_mode = 0,
99  .frame_mode = 0,
100  .stroke_sample = 0.0f,
101  .resolution = resolution,
102  };
103 
104  /* Loop all selected files to import them. All SVG imported shared the same import
105  * parameters, but they are created in separated grease pencil objects. */
106  PropertyRNA *prop;
107  if ((prop = RNA_struct_find_property(op->ptr, "directory"))) {
108  char *directory = RNA_string_get_alloc(op->ptr, "directory", NULL, 0, NULL);
109 
110  if ((prop = RNA_struct_find_property(op->ptr, "files"))) {
111  char file_path[FILE_MAX];
112  RNA_PROP_BEGIN (op->ptr, itemptr, prop) {
113  char *filename = RNA_string_get_alloc(&itemptr, "name", NULL, 0, NULL);
114  BLI_join_dirfile(file_path, sizeof(file_path), directory, filename);
115  MEM_freeN(filename);
116 
117  /* Do Import. */
118  WM_cursor_wait(1);
119  RNA_string_get(&itemptr, "name", params.filename);
120  const bool done = gpencil_io_import(file_path, &params);
121  WM_cursor_wait(0);
122  if (!done) {
123  BKE_reportf(op->reports, RPT_WARNING, "Unable to import '%s'", file_path);
124  }
125  }
126  RNA_PROP_END;
127  }
128  MEM_freeN(directory);
129  }
130 
131  return OPERATOR_FINISHED;
132 }
133 
134 static void ui_gpencil_import_svg_settings(uiLayout *layout, PointerRNA *imfptr)
135 {
136  uiLayoutSetPropSep(layout, true);
137  uiLayoutSetPropDecorate(layout, false);
138  uiLayout *col = uiLayoutColumn(layout, false);
139  uiItemR(col, imfptr, "resolution", 0, NULL, ICON_NONE);
140  uiItemR(col, imfptr, "scale", 0, NULL, ICON_NONE);
141 }
142 
143 static void wm_gpencil_import_svg_draw(bContext *UNUSED(C), wmOperator *op)
144 {
145  ui_gpencil_import_svg_settings(op->layout, op->ptr);
146 }
147 
148 static bool wm_gpencil_import_svg_poll(bContext *C)
149 {
151  return false;
152  }
153 
154  return true;
155 }
156 
158 {
159  ot->name = "Import SVG";
160  ot->description = "Import SVG into grease pencil";
161  ot->idname = "WM_OT_gpencil_import_svg";
162 
163  ot->invoke = wm_gpencil_import_svg_invoke;
164  ot->exec = wm_gpencil_import_svg_exec;
165  ot->poll = wm_gpencil_import_svg_poll;
166  ot->ui = wm_gpencil_import_svg_draw;
167  ot->check = wm_gpencil_import_svg_common_check;
168 
171  FILE_BLENDER,
177 
179  "resolution",
180  10,
181  1,
182  30,
183  "Resolution",
184  "Resolution of the generated strokes",
185  1,
186  20);
187 
189  "scale",
190  10.0f,
191  0.001f,
192  100.0f,
193  "Scale",
194  "Scale of the final strokes",
195  0.001f,
196  100.0f);
197 }
198 
199 #endif /* WITH_IO_GPENCIL */
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
@ CTX_MODE_OBJECT
Definition: BKE_context.h:118
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:723
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
Definition: context.c:1228
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition: report.c:83
#define FILE_MAX
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1420
bool BLI_path_extension_check(const char *str, const char *ext) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1299
void BLI_join_dirfile(char *__restrict dst, size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1531
#define UNUSED(x)
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_OBJECT_IO
@ FILE_TYPE_FOLDER
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Read Guarded memory(de)allocation.
#define RNA_PROP_END
Definition: RNA_access.h:563
#define RNA_PROP_BEGIN(sptr, itemptr, prop)
Definition: RNA_access.h:556
#define C
Definition: RandGen.cpp:25
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
@ WM_FILESEL_FILES
Definition: WM_api.h:756
@ WM_FILESEL_DIRECTORY
Definition: WM_api.h:753
@ WM_FILESEL_RELPATH
Definition: WM_api.h:752
@ WM_FILESEL_SHOW_PROPS
Definition: WM_api.h:758
@ WM_FILESEL_FILEPATH
Definition: WM_api.h:755
@ FILE_OPENFILE
Definition: WM_api.h:764
Scene scene
bool gpencil_io_import(const char *filepath, struct GpencilIOParams *iparams)
@ GP_IMPORT_FROM_SVG
Definition: gpencil_io.h:56
uint col
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void WM_OT_gpencil_import_svg(struct wmOperatorType *ot)
struct View3D * get_invoke_view3d(struct bContext *C)
struct ARegion * get_invoke_region(struct bContext *C)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:5155
bool RNA_struct_property_is_set_ex(PointerRNA *ptr, const char *identifier, bool use_ghost)
Definition: rna_access.c:5289
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:717
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:5116
int RNA_int_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4910
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
char * RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len)
Definition: rna_access.c:5129
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3836
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3597
struct RenderData r
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
void(* ui)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:954
bool(* check)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:911
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
struct ReportList * reports
struct uiLayout * layout
struct PointerRNA * ptr
void WM_cursor_wait(bool val)
Definition: wm_cursors.c:209
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition: wm_files.c:3479
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)