Blender V4.3
PythonInterpreter.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
12#include <iostream>
13
14#include "Interpreter.h"
15
16#include "BKE_context.hh"
17#include "BKE_global.hh"
18#include "BKE_lib_id.hh"
19#include "BKE_main.hh"
20#include "BKE_text.h"
21
22#include "BPY_extern_run.hh"
23
24#include "bpy_capi_utils.hh"
25
26namespace Freestyle {
27
29 public:
31 {
32 _language = "Python";
33 _context = 0;
34 memset(&_freestyle_bmain, 0, sizeof(Main));
35 }
36
38 {
39 _context = C;
40 }
41
42 int interpretFile(const string &filename)
43 {
44 char *fn = const_cast<char *>(filename.c_str());
45#if 0
46 bool ok = BPY_run_filepath(_context, fn, nullptr);
47#else
48 bool ok;
49 Text *text = BKE_text_load(&_freestyle_bmain, fn, G_MAIN->filepath);
50 if (text) {
51 ok = BPY_run_text(_context, text, nullptr, false);
52 BKE_id_delete(&_freestyle_bmain, text);
53 }
54 else {
55 cerr << "Cannot open file" << endl;
56 ok = false;
57 }
58#endif
59
60 if (ok == false) {
61 cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
62 cerr << "File: " << fn << endl;
63 return 1;
64 }
65
66 return 0;
67 }
68
69 int interpretString(const string &str, const string &name)
70 {
71 if (!BPY_run_string_eval(_context, nullptr, str.c_str())) {
72 cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl;
73 cerr << "Name: " << name << endl;
74 return 1;
75 }
76
77 return 0;
78 }
79
80 int interpretText(struct Text *text, const string &name)
81 {
82 if (!BPY_run_text(_context, text, nullptr, false)) {
83 cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
84 cerr << "Name: " << name << endl;
85 return 1;
86 }
87 return 0;
88 }
89
90 void reset()
91 {
92 // nothing to do
93 }
94
95 private:
96 bContext *_context;
97 Main _freestyle_bmain;
98};
99
100} /* namespace Freestyle */
#define G_MAIN
void BKE_id_delete(Main *bmain, void *idv) ATTR_NONNULL()
struct Text struct Text * BKE_text_load(struct Main *bmain, const char *filepath, const char *relbase) ATTR_NONNULL(1
bool bool BPY_run_text(bContext *C, Text *text, ReportList *reports, bool do_jump) ATTR_NONNULL(1
bool BPY_run_filepath(bContext *C, const char *filepath, ReportList *reports) ATTR_NONNULL(1
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
Base Class of all script interpreters.
#define C
Definition RandGen.cpp:29
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:20
char filepath[1024]
Definition BKE_main.hh:136