Blender
V2.93
source
blender
freestyle
intern
system
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
51
class
PythonInterpreter
:
public
Interpreter
{
52
public
:
53
PythonInterpreter
()
54
{
55
_language
=
"Python"
;
56
_context = 0;
57
memset(&_freestyle_bmain, 0,
sizeof
(
Main
));
58
}
59
60
void
setContext
(
bContext
*C)
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 */
BKE_context.h
CTX_wm_reports
struct ReportList * CTX_wm_reports(const bContext *C)
Definition:
context.c:751
BKE_global.h
G_MAIN
#define G_MAIN
Definition:
BKE_global.h:232
BKE_lib_id.h
BKE_id_delete
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
Definition:
lib_id_delete.c:380
BKE_main.h
BKE_report.h
BKE_reports_print
void BKE_reports_print(ReportList *reports, ReportType level)
Definition:
report.c:282
BKE_reports_clear
void BKE_reports_clear(ReportList *reports)
Definition:
report.c:84
BKE_reportf
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
BKE_text.h
BKE_text_load
struct Text * BKE_text_load(struct Main *bmain, const char *file, const char *relpath)
Definition:
text.c:533
BPY_extern_run.h
BPY_run_string_eval
bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr)
Definition:
bpy_interface_run.c:276
BPY_run_text
bool BPY_run_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
Definition:
bpy_interface_run.c:220
BPY_run_filepath
bool BPY_run_filepath(struct bContext *C, const char *filepath, struct ReportList *reports)
Definition:
bpy_interface_run.c:215
DNA_text_types.h
RPT_ERROR
@ RPT_ERROR
Definition:
DNA_windowmanager_types.h:68
Interpreter.h
Base Class of all script interpreters.
MEM_guardedalloc.h
Read Guarded memory(de)allocation.
C
#define C
Definition:
RandGen.cpp:39
StringUtils.h
String utilities.
NULL
return NULL
Definition:
bmesh_operator_api_inline.h:224
BPy_errors_to_report
bool BPy_errors_to_report(ReportList *reports)
Definition:
bpy_capi_utils.c:159
bpy_capi_utils.h
Freestyle::Interpreter
Definition:
Interpreter.h:34
Freestyle::Interpreter::_language
string _language
Definition:
Interpreter.h:55
Freestyle::PythonInterpreter
Definition:
PythonInterpreter.h:51
Freestyle::PythonInterpreter::interpretFile
int interpretFile(const string &filename)
Definition:
PythonInterpreter.h:65
Freestyle::PythonInterpreter::PythonInterpreter
PythonInterpreter()
Definition:
PythonInterpreter.h:53
Freestyle::PythonInterpreter::reset
void reset()
Definition:
PythonInterpreter.h:138
Freestyle::PythonInterpreter::interpretString
int interpretString(const string &str, const string &name)
Definition:
PythonInterpreter.h:99
Freestyle::PythonInterpreter::setContext
void setContext(bContext *C)
Definition:
PythonInterpreter.h:60
Freestyle::PythonInterpreter::interpretText
int interpretText(struct Text *text, const string &name)
Definition:
PythonInterpreter.h:119
str
#define str(s)
Definition:
ffmpeg_codecs.cc:88
Freestyle
inherits from class Rep
Definition:
AppCanvas.cpp:32
Main
Definition:
BKE_main.h:116
ReportList
Definition:
DNA_windowmanager_types.h:104
Text
Definition:
DNA_text_types.h:46
bContext
Definition:
context.c:69
Generated on Tue Jan 31 2023 14:37:24 for Blender by
doxygen
1.9.1