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