2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* ***** END GPL LICENSE BLOCK *****
21
#ifndef __FREESTYLE_PYTHON_INTERPRETER_H__
22
#define __FREESTYLE_PYTHON_INTERPRETER_H__
24
/** \file blender/freestyle/intern/system/PythonInterpreter.h
26
* \brief Python Interpreter
27
* \author Emmanuel Turquin
34
#include "StringUtils.h"
35
#include "Interpreter.h"
39
#include "MEM_guardedalloc.h"
41
#include "DNA_text_types.h"
43
#include "BKE_context.h"
44
#include "BKE_global.h"
45
#include "BKE_library.h"
47
#include "BKE_report.h"
50
#include "BPY_extern.h"
55
class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter
62
memset(&_freestyle_bmain, 0, sizeof(Main));
66
virtual ~PythonInterpreter()
71
void setContext(bContext *C)
76
int interpretFile(const string& filename)
80
ReportList *reports = CTX_wm_reports(_context);
81
BKE_reports_clear(reports);
82
char *fn = const_cast<char*>(filename.c_str());
84
int status = BPY_filepath_exec(_context, fn, reports);
87
Text *text = BKE_text_load(&_freestyle_bmain, fn, G.main->name);
89
status = BPY_text_exec(_context, text, reports, false);
90
BKE_text_unlink(&_freestyle_bmain, text);
91
BKE_libblock_free(&_freestyle_bmain.text, text);
94
BKE_reportf(reports, RPT_ERROR, "Cannot open file: %s", fn);
100
cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
101
cerr << "File: " << fn << endl;
102
cerr << "Errors: " << endl;
103
BKE_reports_print(reports, RPT_ERROR);
108
BKE_reports_clear(reports);
113
int interpretText(struct Text *text, const string& name)
117
ReportList *reports = CTX_wm_reports(_context);
119
BKE_reports_clear(reports);
121
if (!BPY_text_exec(_context, text, reports, false)) {
122
cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
123
cerr << "Name: " << name << endl;
124
cerr << "Errors: " << endl;
125
BKE_reports_print(reports, RPT_ERROR);
129
BKE_reports_clear(reports);
136
static void setPythonPath(const string& path)
141
static string getPythonPath()
151
_initialized = false;
156
Main _freestyle_bmain;
163
vector<string> pathnames;
164
StringUtils::getPathName(_path, "", pathnames);
166
struct Text *text = BKE_text_add(&_freestyle_bmain, "tmp_freestyle_initpath.txt");
167
string cmd = "import sys\n";
168
txt_insert_buf(text, const_cast<char*>(cmd.c_str()));
170
for (vector<string>::const_iterator it = pathnames.begin(); it != pathnames.end(); ++it) {
172
if (G.debug & G_DEBUG_FREESTYLE) {
173
cout << "Adding Python path: " << *it << endl;
175
cmd = "sys.path.append(r\"" + *it + "\")\n";
176
txt_insert_buf(text, const_cast<char *>(cmd.c_str()));
180
BPY_text_exec(_context, text, NULL, false);
183
BKE_text_unlink(&_freestyle_bmain, text);
184
BKE_libblock_free(&_freestyle_bmain.text, text);
189
static bool _initialized;
193
} /* namespace Freestyle */
195
#endif // __FREESTYLE_PYTHON_INTERPRETER_H__