PyAPI: add imports arg to BPY_execute_string
[blender.git] / source / blender / freestyle / intern / system / PythonInterpreter.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 #ifndef __FREESTYLE_PYTHON_INTERPRETER_H__
22 #define __FREESTYLE_PYTHON_INTERPRETER_H__
23
24 /** \file blender/freestyle/intern/system/PythonInterpreter.h
25  *  \ingroup freestyle
26  *  \brief Python Interpreter
27  *  \author Emmanuel Turquin
28  *  \date 17/04/2003
29  */
30
31 #include <iostream>
32
33 extern "C" {
34 #include <Python.h>
35 }
36
37 #include "StringUtils.h"
38 #include "Interpreter.h"
39
40 //soc
41 extern "C" {
42 #include "MEM_guardedalloc.h"
43
44 #include "DNA_text_types.h"
45
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_report.h"
51 #include "BKE_text.h"
52
53 #include "BPY_extern.h"
54
55 #include "bpy_capi_utils.h"
56 }
57
58 namespace Freestyle {
59
60 class PythonInterpreter : public Interpreter
61 {
62 public:
63         PythonInterpreter()
64         {
65                 _language = "Python";
66                 _context = 0;
67                 memset(&_freestyle_bmain, 0, sizeof(Main));
68         }
69
70         void setContext(bContext *C)
71         {
72                 _context = C;
73         }
74
75         int interpretFile(const string& filename)
76         {
77                 ReportList *reports = CTX_wm_reports(_context);
78                 BKE_reports_clear(reports);
79                 char *fn = const_cast<char*>(filename.c_str());
80 #if 0
81                 bool ok = BPY_execute_filepath(_context, fn, reports);
82 #else
83                 bool ok;
84                 Text *text = BKE_text_load(&_freestyle_bmain, fn, G_MAIN->name);
85                 if (text) {
86                         ok = BPY_execute_text(_context, text, reports, false);
87                         BKE_libblock_delete(&_freestyle_bmain, text);
88                 }
89                 else {
90                         BKE_reportf(reports, RPT_ERROR, "Cannot open file: %s", fn);
91                         ok = false;
92                 }
93 #endif
94
95                 if (ok == false) {
96                         cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
97                         cerr << "File: " << fn << endl;
98                         cerr << "Errors: " << endl;
99                         BKE_reports_print(reports, RPT_ERROR);
100                         return 1;
101                 }
102
103                 // cleaning up
104                 BKE_reports_clear(reports);
105
106                 return 0;
107         }
108
109         int interpretString(const string& str, const string& name)
110         {
111                 ReportList *reports = CTX_wm_reports(_context);
112
113                 BKE_reports_clear(reports);
114
115                 if (!BPY_execute_string(_context, NULL, str.c_str())) {
116                         BPy_errors_to_report(reports);
117                         cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl;
118                         cerr << "Name: " << name << endl;
119                         cerr << "Errors: " << endl;
120                         BKE_reports_print(reports, RPT_ERROR);
121                         return 1;
122                 }
123
124                 BKE_reports_clear(reports);
125
126                 return 0;
127         }
128
129         int interpretText(struct Text *text, const string& name)
130         {
131                 ReportList *reports = CTX_wm_reports(_context);
132
133                 BKE_reports_clear(reports);
134
135                 if (!BPY_execute_text(_context, text, reports, false)) {
136                         cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
137                         cerr << "Name: " << name << endl;
138                         cerr << "Errors: " << endl;
139                         BKE_reports_print(reports, RPT_ERROR);
140                         return 1;
141                 }
142
143                 BKE_reports_clear(reports);
144
145                 return 0;
146         }
147
148         void reset()
149         {
150                 // nothing to do
151         }
152
153 private:
154         bContext *_context;
155         Main _freestyle_bmain;
156 };
157
158 } /* namespace Freestyle */
159
160 #endif // __FREESTYLE_PYTHON_INTERPRETER_H__