Blender  V2.93
io_gpencil_import.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) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "BLI_path_util.h"
25 
26 #include "DNA_gpencil_types.h"
27 #include "DNA_space_types.h"
28 
29 #include "BKE_context.h"
30 #include "BKE_gpencil.h"
31 #include "BKE_report.h"
32 
33 #include "BLT_translation.h"
34 
35 #include "RNA_access.h"
36 #include "RNA_define.h"
37 
38 #include "UI_interface.h"
39 #include "UI_resources.h"
40 
41 #include "WM_api.h"
42 #include "WM_types.h"
43 
44 #include "DEG_depsgraph.h"
45 #include "DEG_depsgraph_query.h"
46 
47 #include "ED_gpencil.h"
48 
49 #include "io_gpencil.h"
50 
51 #include "gpencil_io.h"
52 
53 /* <-------- SVG single frame import. --------> */
55 {
56 
57  char filepath[FILE_MAX];
58  RNA_string_get(op->ptr, "filepath", filepath);
59 
60  if (!BLI_path_extension_check(filepath, ".svg")) {
61  BLI_path_extension_ensure(filepath, FILE_MAX, ".svg");
62  RNA_string_set(op->ptr, "filepath", filepath);
63  return true;
64  }
65 
66  return false;
67 }
68 
70 {
72 
74 }
75 
77 {
79 
80  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
81  BKE_report(op->reports, RPT_ERROR, "No filename given");
82  return OPERATOR_CANCELLED;
83  }
84 
85  ARegion *region = get_invoke_region(C);
86  if (region == NULL) {
87  BKE_report(op->reports, RPT_ERROR, "Unable to find valid 3D View area");
88  return OPERATOR_CANCELLED;
89  }
90  View3D *v3d = get_invoke_view3d(C);
91 
92  char filename[FILE_MAX];
93  RNA_string_get(op->ptr, "filepath", filename);
94 
95  /* Set flags. */
96  int flag = 0;
97 
98  const int resolution = RNA_int_get(op->ptr, "resolution");
99  const float scale = RNA_float_get(op->ptr, "scale");
100 
102  .C = C,
103  .region = region,
104  .v3d = v3d,
105  .ob = NULL,
106  .mode = GP_IMPORT_FROM_SVG,
107  .frame_start = CFRA,
108  .frame_end = CFRA,
109  .frame_cur = CFRA,
110  .flag = flag,
111  .scale = scale,
112  .select_mode = 0,
113  .frame_mode = 0,
114  .stroke_sample = 0.0f,
115  .resolution = resolution,
116  };
117 
118  /* Do Import. */
119  WM_cursor_wait(1);
120  const bool done = gpencil_io_import(filename, &params);
121  WM_cursor_wait(0);
122 
123  if (!done) {
124  BKE_report(op->reports, RPT_WARNING, "Unable to import SVG");
125  }
126 
127  return OPERATOR_FINISHED;
128 }
129 
131 {
132  uiLayoutSetPropSep(layout, true);
133  uiLayoutSetPropDecorate(layout, false);
134  uiLayout *col = uiLayoutColumn(layout, false);
135  uiItemR(col, imfptr, "resolution", 0, NULL, ICON_NONE);
136  uiItemR(col, imfptr, "scale", 0, NULL, ICON_NONE);
137 }
138 
140 {
142  PointerRNA ptr;
143  RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
144 
146 }
147 
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 
168 
171  FILE_BLENDER,
176 
178  "resolution",
179  10,
180  1,
181  30,
182  "Resolution",
183  "Resolution of the generated strokes",
184  1,
185  20);
186 
188  "scale",
189  10.0f,
190  0.001f,
191  100.0f,
192  "Scale",
193  "Scale of the final strokes",
194  0.001f,
195  100.0f);
196 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
@ CTX_MODE_OBJECT
Definition: BKE_context.h:128
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:699
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
Definition: context.c:1174
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
#define FILE_MAX
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1601
bool BLI_path_extension_check(const char *str, const char *ext) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1459
#define UNUSED(x)
#define CFRA
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_OBJECT_IO
@ FILE_OPENFILE
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
#define C
Definition: RandGen.cpp:39
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)
#define WM_FILESEL_SHOW_PROPS
Definition: WM_api.h:540
#define WM_FILESEL_RELPATH
Definition: WM_api.h:533
#define WM_FILESEL_FILEPATH
Definition: WM_api.h:537
Scene scene
bool gpencil_io_import(const char *filename, struct GpencilIOParams *iparams)
@ GP_IMPORT_FROM_SVG
Definition: gpencil_io.h:70
uint col
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
struct View3D * get_invoke_view3d(struct bContext *C)
struct ARegion * get_invoke_region(struct bContext *C)
void WM_OT_gpencil_import_svg(wmOperatorType *ot)
static int wm_gpencil_import_svg_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
static bool wm_gpencil_import_svg_poll(bContext *C)
static int wm_gpencil_import_svg_exec(bContext *C, wmOperator *op)
static bool wm_gpencil_import_svg_common_check(bContext *UNUSED(C), wmOperator *op)
static void ui_gpencil_import_svg_settings(uiLayout *layout, PointerRNA *imfptr)
static void wm_gpencil_import_svg_draw(bContext *C, wmOperator *op)
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
int RNA_int_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6308
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6355
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:6685
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:3825
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:3585
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
void(* ui)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:787
bool(* check)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:744
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
IDProperty * properties
struct uiLayout * layout
struct wmOperatorType * type
struct PointerRNA * ptr
void WM_cursor_wait(bool val)
Definition: wm_cursors.c:226
void WM_event_add_fileselect(bContext *C, wmOperator *op)
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display, short sort)