~ubuntu-branches/ubuntu/trusty/blender/trusty-proposed

« back to all changes in this revision

Viewing changes to source/blender/freestyle/intern/system/PythonInterpreter.h

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2013-08-14 10:43:49 UTC
  • mfrom: (14.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20130814104349-t1d5mtwkphp12dyj
Tags: 2.68a-3
* Upload to unstable
* debian/: python3.3 Depends simplified
  - debian/control: python3.3 Depends dropped
    for blender-data package
  - 0001-blender_thumbnailer.patch refreshed
* debian/control: libavcodec b-dep versioning dropped

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <Python.h>
 
33
 
 
34
#include "StringUtils.h"
 
35
#include "Interpreter.h"
 
36
 
 
37
//soc
 
38
extern "C" {
 
39
#include "MEM_guardedalloc.h"
 
40
 
 
41
#include "DNA_text_types.h"
 
42
 
 
43
#include "BKE_context.h"
 
44
#include "BKE_global.h"
 
45
#include "BKE_library.h"
 
46
#include "BKE_main.h"
 
47
#include "BKE_report.h"
 
48
#include "BKE_text.h"
 
49
 
 
50
#include "BPY_extern.h"
 
51
}
 
52
 
 
53
namespace Freestyle {
 
54
 
 
55
class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter
 
56
{
 
57
public:
 
58
        PythonInterpreter()
 
59
        {
 
60
                _language = "Python";
 
61
                _context = 0;
 
62
                memset(&_freestyle_bmain, 0, sizeof(Main));
 
63
                //Py_Initialize();
 
64
        }
 
65
 
 
66
        virtual ~PythonInterpreter()
 
67
        {
 
68
                //Py_Finalize();
 
69
        }
 
70
 
 
71
        void setContext(bContext *C)
 
72
        {
 
73
                _context = C;
 
74
        }
 
75
 
 
76
        int interpretFile(const string& filename)
 
77
        {
 
78
                initPath();
 
79
 
 
80
                ReportList *reports = CTX_wm_reports(_context);
 
81
                BKE_reports_clear(reports);
 
82
                char *fn = const_cast<char*>(filename.c_str());
 
83
#if 0
 
84
                int status = BPY_filepath_exec(_context, fn, reports);
 
85
#else
 
86
                int status;
 
87
                Text *text = BKE_text_load(&_freestyle_bmain, fn, G.main->name);
 
88
                if (text) {
 
89
                        status = BPY_text_exec(_context, text, reports, false);
 
90
                        BKE_text_unlink(&_freestyle_bmain, text);
 
91
                        BKE_libblock_free(&_freestyle_bmain.text, text);
 
92
                }
 
93
                else {
 
94
                        BKE_reportf(reports, RPT_ERROR, "Cannot open file: %s", fn);
 
95
                        status = 0;
 
96
                }
 
97
#endif
 
98
 
 
99
                if (status != 1) {
 
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);
 
104
                        return 1;
 
105
                }
 
106
 
 
107
                // cleaning up
 
108
                BKE_reports_clear(reports);
 
109
 
 
110
                return 0;
 
111
        }
 
112
 
 
113
        int interpretText(struct Text *text, const string& name)
 
114
        {
 
115
                initPath();
 
116
 
 
117
                ReportList *reports = CTX_wm_reports(_context);
 
118
 
 
119
                BKE_reports_clear(reports);
 
120
 
 
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);
 
126
                        return 1;
 
127
                }
 
128
 
 
129
                BKE_reports_clear(reports);
 
130
 
 
131
                return 0;
 
132
        }
 
133
 
 
134
        struct Options
 
135
        {
 
136
                static void setPythonPath(const string& path)
 
137
                {
 
138
                        _path = path;
 
139
                }
 
140
 
 
141
                static string getPythonPath()
 
142
                {
 
143
                        return _path;
 
144
                }
 
145
        };
 
146
 
 
147
        void reset()
 
148
        {
 
149
                Py_Finalize();
 
150
                Py_Initialize();
 
151
                _initialized = false;
 
152
        }
 
153
 
 
154
private:
 
155
        bContext *_context;
 
156
        Main _freestyle_bmain;
 
157
 
 
158
        void initPath()
 
159
        {
 
160
                if (_initialized)
 
161
                        return;
 
162
 
 
163
                vector<string> pathnames;
 
164
                StringUtils::getPathName(_path, "", pathnames);
 
165
 
 
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()));
 
169
 
 
170
                for (vector<string>::const_iterator it = pathnames.begin(); it != pathnames.end(); ++it) {
 
171
                        if (!it->empty()) {
 
172
                                if (G.debug & G_DEBUG_FREESTYLE) {
 
173
                                        cout << "Adding Python path: " << *it << endl;
 
174
                                }
 
175
                                cmd = "sys.path.append(r\"" + *it + "\")\n";
 
176
                                txt_insert_buf(text, const_cast<char *>(cmd.c_str()));
 
177
                        }
 
178
                }
 
179
 
 
180
                BPY_text_exec(_context, text, NULL, false);
 
181
 
 
182
                // cleaning up
 
183
                BKE_text_unlink(&_freestyle_bmain, text);
 
184
                BKE_libblock_free(&_freestyle_bmain.text, text);
 
185
 
 
186
                _initialized = true;
 
187
        }
 
188
 
 
189
        static bool _initialized;
 
190
        static string _path;
 
191
};
 
192
 
 
193
} /* namespace Freestyle */
 
194
 
 
195
#endif // __FREESTYLE_PYTHON_INTERPRETER_H__