Blender  V2.93
PythonInterpreter.h
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 
17 #pragma once
18 
24 #include <iostream>
25 
26 extern "C" {
27 #include <Python.h>
28 }
29 
30 #include "Interpreter.h"
31 #include "StringUtils.h"
32 
33 #include "MEM_guardedalloc.h"
34 
35 // soc
36 #include "DNA_text_types.h"
37 
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40 #include "BKE_lib_id.h"
41 #include "BKE_main.h"
42 #include "BKE_report.h"
43 #include "BKE_text.h"
44 
45 #include "BPY_extern_run.h"
46 
47 #include "bpy_capi_utils.h"
48 
49 namespace Freestyle {
50 
52  public:
54  {
55  _language = "Python";
56  _context = 0;
57  memset(&_freestyle_bmain, 0, sizeof(Main));
58  }
59 
61  {
62  _context = C;
63  }
64 
65  int interpretFile(const string &filename)
66  {
67  ReportList *reports = CTX_wm_reports(_context);
68  BKE_reports_clear(reports);
69  char *fn = const_cast<char *>(filename.c_str());
70 #if 0
71  bool ok = BPY_run_filepath(_context, fn, reports);
72 #else
73  bool ok;
74  Text *text = BKE_text_load(&_freestyle_bmain, fn, G_MAIN->name);
75  if (text) {
76  ok = BPY_run_text(_context, text, reports, false);
77  BKE_id_delete(&_freestyle_bmain, text);
78  }
79  else {
80  BKE_reportf(reports, RPT_ERROR, "Cannot open file: %s", fn);
81  ok = false;
82  }
83 #endif
84 
85  if (ok == false) {
86  cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
87  cerr << "File: " << fn << endl;
88  cerr << "Errors: " << endl;
89  BKE_reports_print(reports, RPT_ERROR);
90  return 1;
91  }
92 
93  // cleaning up
94  BKE_reports_clear(reports);
95 
96  return 0;
97  }
98 
99  int interpretString(const string &str, const string &name)
100  {
101  ReportList *reports = CTX_wm_reports(_context);
102 
103  BKE_reports_clear(reports);
104 
105  if (!BPY_run_string_eval(_context, NULL, str.c_str())) {
106  BPy_errors_to_report(reports);
107  cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl;
108  cerr << "Name: " << name << endl;
109  cerr << "Errors: " << endl;
110  BKE_reports_print(reports, RPT_ERROR);
111  return 1;
112  }
113 
114  BKE_reports_clear(reports);
115 
116  return 0;
117  }
118 
119  int interpretText(struct Text *text, const string &name)
120  {
121  ReportList *reports = CTX_wm_reports(_context);
122 
123  BKE_reports_clear(reports);
124 
125  if (!BPY_run_text(_context, text, reports, false)) {
126  cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
127  cerr << "Name: " << name << endl;
128  cerr << "Errors: " << endl;
129  BKE_reports_print(reports, RPT_ERROR);
130  return 1;
131  }
132 
133  BKE_reports_clear(reports);
134 
135  return 0;
136  }
137 
138  void reset()
139  {
140  // nothing to do
141  }
142 
143  private:
144  bContext *_context;
145  Main _freestyle_bmain;
146 };
147 
148 } /* namespace Freestyle */
struct ReportList * CTX_wm_reports(const bContext *C)
Definition: context.c:751
#define G_MAIN
Definition: BKE_global.h:232
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
void BKE_reports_print(ReportList *reports, ReportType level)
Definition: report.c:282
void BKE_reports_clear(ReportList *reports)
Definition: report.c:84
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
struct Text * BKE_text_load(struct Main *bmain, const char *file, const char *relpath)
Definition: text.c:533
bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr)
bool BPY_run_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
bool BPY_run_filepath(struct bContext *C, const char *filepath, struct ReportList *reports)
Base Class of all script interpreters.
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
String utilities.
bool BPy_errors_to_report(ReportList *reports)
int interpretFile(const string &filename)
int interpretString(const string &str, const string &name)
int interpretText(struct Text *text, const string &name)
#define str(s)
inherits from class Rep
Definition: AppCanvas.cpp:32
Definition: BKE_main.h:116