[Bf-blender-cvs] [0c1b1e734e3] master: Merge branch 'blender-v2.83-release'

Jacques Lucke noreply at git.blender.org
Tue May 12 12:21:30 CEST 2020


Commit: 0c1b1e734e3bae449d8cb3735bb6296141d836a1
Author: Jacques Lucke
Date:   Tue May 12 12:21:16 2020 +0200
Branches: master
https://developer.blender.org/rB0c1b1e734e3bae449d8cb3735bb6296141d836a1

Merge branch 'blender-v2.83-release'

===================================================================



===================================================================

diff --cc intern/mantaflow/intern/MANTA_main.cpp
index b8dc631b08e,35d4629d195..6671fbc0d7d
--- a/intern/mantaflow/intern/MANTA_main.cpp
+++ b/intern/mantaflow/intern/MANTA_main.cpp
@@@ -552,25 -538,44 +552,44 @@@ 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(std::vector<std::string> commands)
 +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 (std::vector<std::string>::iterator it = commands.begin(); it != commands.end(); ++it) {
++  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);



More information about the Bf-blender-cvs mailing list