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