Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2020-05-12 13:21:16 +0300
committerJacques Lucke <jacques@blender.org>2020-05-12 13:21:16 +0300
commit0c1b1e734e3bae449d8cb3735bb6296141d836a1 (patch)
tree808955bb937f1286549ce51e6bdc9debfac6e5fa /intern/mantaflow
parente0b5a202311eb08d67725670325697ee0afeed96 (diff)
parenta50c131a0d71d04915cc39c5a412f4f16c6f706b (diff)
Merge branch 'blender-v2.83-release'
Diffstat (limited to 'intern/mantaflow')
-rw-r--r--intern/mantaflow/intern/MANTA_main.cpp65
1 files changed, 41 insertions, 24 deletions
diff --git a/intern/mantaflow/intern/MANTA_main.cpp b/intern/mantaflow/intern/MANTA_main.cpp
index b8dc631b08e..6671fbc0d7d 100644
--- a/intern/mantaflow/intern/MANTA_main.cpp
+++ b/intern/mantaflow/intern/MANTA_main.cpp
@@ -552,31 +552,49 @@ MANTA::~MANTA()
(void)result; // not needed in release
}
+/**
+ * Store a pointer to the __main__ module used by mantaflow. This is necessary, because sometimes
+ * Blender will overwrite that module. That happens when e.g. scripts are executed in the text
+ * editor.
+ *
+ * Mantaflow stores many variables in the globals() dict of the __main__ module. To be able to
+ * access these variables, the same __main__ module has to be used every time.
+ *
+ * Unfortunately, we also depend on the fact that mantaflow dumps variables into this module using
+ * PyRun_SimpleString. So we can't easily create a separate module without changing mantaflow.
+ */
+static PyObject *manta_main_module = nullptr;
+
bool MANTA::runPythonString(vector<string> commands)
{
- int success = -1;
+ bool success = true;
PyGILState_STATE gilstate = PyGILState_Ensure();
- for (vector<string>::iterator it = commands.begin(); it != commands.end(); ++it) {
- string command = *it;
-
-#ifdef WIN32
- // special treatment for windows when running python code
- size_t cmdLength = command.length();
- char *buffer = new char[cmdLength + 1];
- memcpy(buffer, command.data(), cmdLength);
-
- buffer[cmdLength] = '\0';
- success = PyRun_SimpleString(buffer);
- delete[] buffer;
-#else
- success = PyRun_SimpleString(command.c_str());
-#endif
+
+ if (manta_main_module == nullptr) {
+ manta_main_module = PyImport_ImportModule("__main__");
+ }
+
+ for (vector<std::string>::iterator it = commands.begin(); it != commands.end(); ++it) {
+ std::string command = *it;
+
+ PyObject *globals_dict = PyModule_GetDict(manta_main_module);
+ PyObject *return_value = PyRun_String(
+ command.c_str(), Py_file_input, globals_dict, globals_dict);
+
+ if (return_value == nullptr) {
+ success = false;
+ if (PyErr_Occurred()) {
+ PyErr_Print();
+ }
+ }
+ else {
+ Py_DECREF(return_value);
+ }
}
PyGILState_Release(gilstate);
- /* PyRun_SimpleString returns 0 on success, -1 when an error occurred. */
- assert(success == 0);
- return (success != -1);
+ assert(success);
+ return success;
}
void MANTA::initializeMantaflow()
@@ -1988,19 +2006,18 @@ static PyObject *callPythonFunction(string varName, string functionName, bool is
}
PyGILState_STATE gilstate = PyGILState_Ensure();
- PyObject *main = nullptr, *var = nullptr, *func = nullptr, *returnedValue = nullptr;
+ PyObject *var = nullptr, *func = nullptr, *returnedValue = nullptr;
- /* Be sure to initialise Python before importing main. */
+ /* Be sure to initialize Python before using it. */
Py_Initialize();
// Get pyobject that holds result value
- main = PyImport_ImportModule("__main__");
- if (!main) {
+ if (!manta_main_module) {
PyGILState_Release(gilstate);
return nullptr;
}
- var = PyObject_GetAttrString(main, varName.c_str());
+ var = PyObject_GetAttrString(manta_main_module, varName.c_str());
if (!var) {
PyGILState_Release(gilstate);
return nullptr;