2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-12-28 20:21:05 +00:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup freestyle
|
|
|
|
|
* \brief Python Interpreter
|
2012-12-28 20:21:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2014-04-17 12:25:41 +09:00
|
|
|
|
2012-12-28 20:21:05 +00:00
|
|
|
#include "Interpreter.h"
|
|
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
2024-02-10 18:25:14 +01:00
|
|
|
#include "BKE_global.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-12-01 19:43:16 +01:00
|
|
|
#include "BKE_main.hh"
|
2008-05-26 19:52:55 +00:00
|
|
|
#include "BKE_text.h"
|
2012-12-28 20:21:05 +00:00
|
|
|
|
2024-09-24 17:07:49 +02:00
|
|
|
#include "BPY_extern_run.hh"
|
2015-04-28 23:18:32 +09:00
|
|
|
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "bpy_capi_utils.hh"
|
2008-05-26 19:52:55 +00:00
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
namespace Freestyle {
|
|
|
|
|
|
2014-04-17 14:19:10 +09:00
|
|
|
class PythonInterpreter : public Interpreter {
|
2012-12-28 20:21:05 +00:00
|
|
|
public:
|
|
|
|
|
PythonInterpreter()
|
|
|
|
|
{
|
|
|
|
|
_language = "Python";
|
|
|
|
|
_context = 0;
|
2013-04-20 21:15:17 +00:00
|
|
|
memset(&_freestyle_bmain, 0, sizeof(Main));
|
2012-12-28 20:21:05 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-28 20:21:05 +00:00
|
|
|
void setContext(bContext *C)
|
|
|
|
|
{
|
|
|
|
|
_context = C;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-28 20:21:05 +00:00
|
|
|
int interpretFile(const string &filename)
|
|
|
|
|
{
|
|
|
|
|
char *fn = const_cast<char *>(filename.c_str());
|
2009-10-15 03:32:53 +00:00
|
|
|
#if 0
|
2023-11-28 13:24:44 +11:00
|
|
|
bool ok = BPY_run_filepath(_context, fn, nullptr);
|
2009-10-15 03:32:53 +00:00
|
|
|
#else
|
2015-12-31 21:15:29 +11:00
|
|
|
bool ok;
|
2021-12-13 16:22:19 +11:00
|
|
|
Text *text = BKE_text_load(&_freestyle_bmain, fn, G_MAIN->filepath);
|
2012-12-28 20:21:05 +00:00
|
|
|
if (text) {
|
2023-11-28 13:24:44 +11:00
|
|
|
ok = BPY_run_text(_context, text, nullptr, false);
|
2019-01-14 21:24:25 +01:00
|
|
|
BKE_id_delete(&_freestyle_bmain, text);
|
2012-12-28 20:21:05 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-11-28 13:24:44 +11:00
|
|
|
cerr << "Cannot open file" << endl;
|
2015-12-31 21:15:29 +11:00
|
|
|
ok = false;
|
2012-12-28 20:21:05 +00:00
|
|
|
}
|
2009-10-15 03:32:53 +00:00
|
|
|
#endif
|
2009-08-23 16:03:12 +00:00
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
if (ok == false) {
|
2013-01-03 23:27:20 +00:00
|
|
|
cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
|
|
|
|
|
cerr << "File: " << fn << endl;
|
2012-12-28 20:21:05 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2009-09-28 03:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-28 23:18:32 +09:00
|
|
|
int interpretString(const string &str, const string &name)
|
|
|
|
|
{
|
2023-08-01 21:15:52 +10:00
|
|
|
if (!BPY_run_string_eval(_context, nullptr, str.c_str())) {
|
2015-04-28 23:18:32 +09:00
|
|
|
cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl;
|
|
|
|
|
cerr << "Name: " << name << endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 03:36:33 +00:00
|
|
|
int interpretText(struct Text *text, const string &name)
|
|
|
|
|
{
|
2023-11-28 13:24:44 +11:00
|
|
|
if (!BPY_run_text(_context, text, nullptr, false)) {
|
2013-01-03 23:27:20 +00:00
|
|
|
cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
|
|
|
|
|
cerr << "Name: " << name << endl;
|
2012-12-28 20:21:05 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2010-07-26 01:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-28 20:21:05 +00:00
|
|
|
void reset()
|
|
|
|
|
{
|
2014-04-11 16:35:46 +09:00
|
|
|
// nothing to do
|
2012-12-28 20:21:05 +00:00
|
|
|
}
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2012-12-28 20:21:05 +00:00
|
|
|
private:
|
|
|
|
|
bContext *_context;
|
2013-04-18 08:58:21 +00:00
|
|
|
Main _freestyle_bmain;
|
2008-04-30 15:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
} /* namespace Freestyle */
|