Blender V5.0
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
11
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 }
34
36 {
37 _context = C;
38 }
39
40 int interpretFile(const string &filename)
41 {
42 char *fn = const_cast<char *>(filename.c_str());
43#if 0
44 bool ok = BPY_run_filepath(_context, fn, nullptr);
45#else
46 bool ok;
47 Text *text = BKE_text_load(&_freestyle_bmain, fn, G_MAIN->filepath);
48 if (text) {
49 ok = BPY_run_text(_context, text, nullptr, false);
50 BKE_id_delete(&_freestyle_bmain, text);
51 }
52 else {
53 cerr << "Cannot open file" << endl;
54 ok = false;
55 }
56#endif
57
58 if (ok == false) {
59 cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
60 cerr << "File: " << fn << endl;
61 return 1;
62 }
63
64 return 0;
65 }
66
67 int interpretString(const string &str, const string &name)
68 {
69 if (!BPY_run_string_eval(_context, nullptr, str.c_str())) {
70 cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl;
71 cerr << "Name: " << name << endl;
72 return 1;
73 }
74
75 return 0;
76 }
77
78 int interpretText(struct Text *text, const string &name)
79 {
80 if (!BPY_run_text(_context, text, nullptr, false)) {
81 cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
82 cerr << "Name: " << name << endl;
83 return 1;
84 }
85 return 0;
86 }
87
88 void reset()
89 {
90 // nothing to do
91 }
92
93 private:
94 bContext *_context = nullptr;
95 Main _freestyle_bmain = {};
96};
97
98} /* 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.
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
const char * name