Blender  V2.93
gpencil_io_capi.cc
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 <cstdio>
25 
26 #include "BLI_listbase.h"
27 
28 #include "DNA_gpencil_types.h"
29 #include "DNA_screen_types.h"
30 #include "DNA_space_types.h"
31 
32 #include "BKE_context.h"
33 #include "BKE_gpencil.h"
34 #include "BKE_main.h"
35 #include "BKE_scene.h"
36 
37 #include "DEG_depsgraph.h"
38 #include "DEG_depsgraph_query.h"
39 
40 #include "../gpencil_io.h"
41 
42 #ifdef WITH_HARU
43 # include "gpencil_io_export_pdf.hh"
44 #endif
45 
46 #ifdef WITH_PUGIXML
47 # include "gpencil_io_export_svg.hh"
48 #endif
49 
50 #include "gpencil_io_import_svg.hh"
51 
52 #ifdef WITH_HARU
54 #endif
55 #ifdef WITH_PUGIXML
57 #endif
59 
60 /* Check if frame is included. */
61 #ifdef WITH_HARU
62 static bool is_keyframe_included(bGPdata *gpd_, const int32_t framenum, const bool use_selected)
63 {
64  /* Check if exist a frame. */
65  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd_->layers) {
66  if (gpl->flag & GP_LAYER_HIDE) {
67  continue;
68  }
69  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
70  if (gpf->framenum == framenum) {
71  if ((!use_selected) || (use_selected && (gpf->flag & GP_FRAME_SELECT))) {
72  return true;
73  }
74  }
75  }
76  }
77  return false;
78 }
79 #endif
80 
81 /* Import frame. */
82 static bool gpencil_io_import_frame(void *in_importer, const GpencilIOParams &iparams)
83 {
84 
85  bool result = false;
86  switch (iparams.mode) {
87  case GP_IMPORT_FROM_SVG: {
88  GpencilImporterSVG *importer = (GpencilImporterSVG *)in_importer;
89  result |= importer->read();
90  break;
91  }
92  /* Add new import formats here. */
93  default:
94  break;
95  }
96 
97  return result;
98 }
99 
100 /* Export frame in PDF. */
101 #ifdef WITH_HARU
102 static bool gpencil_io_export_pdf(Depsgraph *depsgraph,
103  Scene *scene,
104  Object *ob,
105  GpencilExporterPDF *exporter,
106  const GpencilIOParams *iparams)
107 {
108  bool result = false;
109  Object *ob_eval_ = (Object *)DEG_get_evaluated_id(depsgraph, &ob->id);
110  bGPdata *gpd_eval = (bGPdata *)ob_eval_->data;
111 
112  exporter->frame_number_set(iparams->frame_cur);
113  result |= exporter->new_document();
114 
115  const bool use_frame_selected = (iparams->frame_mode == GP_EXPORT_FRAME_SELECTED);
116  if (use_frame_selected) {
117  for (int32_t i = iparams->frame_start; i < iparams->frame_end + 1; i++) {
118  if (!is_keyframe_included(gpd_eval, i, use_frame_selected)) {
119  continue;
120  }
121 
122  CFRA = i;
124  exporter->prepare_camera_params(iparams);
125  exporter->frame_number_set(i);
126  exporter->add_newpage();
127  exporter->add_body();
128  }
129  result = exporter->write();
130  /* Back to original frame. */
131  exporter->frame_number_set(iparams->frame_cur);
132  CFRA = iparams->frame_cur;
134  }
135  else {
136  exporter->prepare_camera_params(iparams);
137  exporter->add_newpage();
138  exporter->add_body();
139  result = exporter->write();
140  }
141 
142  return result;
143 }
144 #endif
145 
146 /* Export current frame in SVG. */
147 #ifdef WITH_PUGIXML
148 static bool gpencil_io_export_frame_svg(GpencilExporterSVG *exporter,
149  const GpencilIOParams *iparams,
150  const bool newpage,
151  const bool body,
152  const bool savepage)
153 {
154  bool result = false;
155  exporter->frame_number_set(iparams->frame_cur);
156  exporter->prepare_camera_params(iparams);
157 
158  if (newpage) {
159  result |= exporter->add_newpage();
160  }
161  if (body) {
162  result |= exporter->add_body();
163  }
164  if (savepage) {
165  result = exporter->write();
166  }
167  return result;
168 }
169 #endif
170 
171 /* Main import entry point function. */
172 bool gpencil_io_import(const char *filename, GpencilIOParams *iparams)
173 {
174  GpencilImporterSVG importer = GpencilImporterSVG(filename, iparams);
175 
176  return gpencil_io_import_frame(&importer, *iparams);
177 }
178 
179 /* Main export entry point function. */
180 bool gpencil_io_export(const char *filename, GpencilIOParams *iparams)
181 {
182  Depsgraph *depsgraph_ = CTX_data_depsgraph_pointer(iparams->C);
183  Scene *scene_ = CTX_data_scene(iparams->C);
184  Object *ob = CTX_data_active_object(iparams->C);
185 
186  UNUSED_VARS(filename, depsgraph_, scene_, ob);
187 
188  switch (iparams->mode) {
189 #ifdef WITH_PUGIXML
190  case GP_EXPORT_TO_SVG: {
191  GpencilExporterSVG exporter = GpencilExporterSVG(filename, iparams);
192  return gpencil_io_export_frame_svg(&exporter, iparams, true, true, true);
193  break;
194  }
195 #endif
196 #ifdef WITH_HARU
197  case GP_EXPORT_TO_PDF: {
198  GpencilExporterPDF exporter = GpencilExporterPDF(filename, iparams);
199  return gpencil_io_export_pdf(depsgraph_, scene_, ob, &exporter, iparams);
200  break;
201  }
202 #endif
203  /* Add new export formats here. */
204  default:
205  break;
206  }
207  return false;
208 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1279
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Definition: context.c:1401
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.c:2794
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#define UNUSED_VARS(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
struct ID * DEG_get_evaluated_id(const struct Depsgraph *depsgraph, struct ID *id)
@ GP_LAYER_HIDE
@ GP_FRAME_SELECT
#define CFRA
Scene scene
const Depsgraph * depsgraph
@ GP_EXPORT_FRAME_SELECTED
Definition: gpencil_io.h:84
@ GP_EXPORT_TO_SVG
Definition: gpencil_io.h:67
@ GP_IMPORT_FROM_SVG
Definition: gpencil_io.h:70
@ GP_EXPORT_TO_PDF
Definition: gpencil_io.h:68
bool gpencil_io_export(const char *filename, GpencilIOParams *iparams)
bool gpencil_io_import(const char *filename, GpencilIOParams *iparams)
static bool gpencil_io_import_frame(void *in_importer, const GpencilIOParams &iparams)
signed int int32_t
Definition: stdint.h:80
uint16_t frame_mode
Definition: gpencil_io.h:50
int32_t frame_cur
Definition: gpencil_io.h:44
int32_t frame_start
Definition: gpencil_io.h:42
bContext * C
Definition: gpencil_io.h:35
uint16_t mode
Definition: gpencil_io.h:41
void * data
ListBase layers