[Bf-blender-cvs] [b7b4f94] master: Fix for Freestyle Python API modules not found in the Python Console.

Tamito Kajiyama noreply at git.blender.org
Fri Apr 11 09:37:23 CEST 2014


Commit: b7b4f94e782f7e74c5acf0af19dfda07fdd92568
Author: Tamito Kajiyama
Date:   Fri Apr 11 16:35:46 2014 +0900
https://developer.blender.org/rBb7b4f94e782f7e74c5acf0af19dfda07fdd92568

Fix for Freestyle Python API modules not found in the Python Console.

Addition of the path to the Freestyle Python API modules to 'sys.path' was delayed until
the first Freestyle rendering, so that any import attempt of the modules in the Python
Console always failed.  Now the update of 'sys.path' is done at Blender start-up.
This allows the Freestyle-specific modules to be imported without running Freestyle,
facilitating quick interactive testing in the Console.

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

M	source/blender/freestyle/intern/application/Controller.cpp
M	source/blender/freestyle/intern/python/BPy_Freestyle.cpp
M	source/blender/freestyle/intern/system/Interpreter.h
M	source/blender/freestyle/intern/system/PythonInterpreter.cpp
M	source/blender/freestyle/intern/system/PythonInterpreter.h

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

diff --git a/source/blender/freestyle/intern/application/Controller.cpp b/source/blender/freestyle/intern/application/Controller.cpp
index 81034c1..81ce162 100644
--- a/source/blender/freestyle/intern/application/Controller.cpp
+++ b/source/blender/freestyle/intern/application/Controller.cpp
@@ -1024,7 +1024,6 @@ void Controller::init_options()
 
 	// Directories
 	ViewMapIO::Options::setModelsPath(cpath->getModelsPath());
-	PythonInterpreter::Options::setPythonPath(cpath->getPythonPath());
 	TextureManager::Options::setPatternsPath(cpath->getPatternsPath());
 	TextureManager::Options::setBrushesPath(cpath->getModelsPath());
 
diff --git a/source/blender/freestyle/intern/python/BPy_Freestyle.cpp b/source/blender/freestyle/intern/python/BPy_Freestyle.cpp
index dee058e..64e186b 100644
--- a/source/blender/freestyle/intern/python/BPy_Freestyle.cpp
+++ b/source/blender/freestyle/intern/python/BPy_Freestyle.cpp
@@ -485,6 +485,23 @@ PyObject *Freestyle_Init(void)
 	if (!module)
 		return NULL;
 	PyDict_SetItemString(PySys_GetObject("modules"), module_definition.m_name, module);
+
+	// update 'sys.path' for Freestyle Python API modules
+	const char * const path = BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, "freestyle");
+	if (path) {
+		char modpath[FILE_MAX];
+		BLI_join_dirfile(modpath, sizeof(modpath), path, "modules");
+		PyObject *sys_path = PySys_GetObject("path"); /* borrow */
+		PyObject *py_modpath = PyUnicode_FromString(modpath);
+		PyList_Append(sys_path, py_modpath);
+		Py_DECREF(py_modpath);
+#if 0
+		printf("Adding Python path: %s\n", modpath);
+#endif
+	}
+	else {
+		printf("Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.\n");
+	}
 	
 	// attach its classes (adding the object types to the module)
 	
diff --git a/source/blender/freestyle/intern/system/Interpreter.h b/source/blender/freestyle/intern/system/Interpreter.h
index e1269f4..f359fc7 100644
--- a/source/blender/freestyle/intern/system/Interpreter.h
+++ b/source/blender/freestyle/intern/system/Interpreter.h
@@ -46,7 +46,7 @@ public:
 		_language = "Unknown";
 	}
 
-	virtual ~Interpreter() {}; //soc
+	virtual ~Interpreter() {}
 
 	virtual int interpretFile(const string& filename) = 0;
 
diff --git a/source/blender/freestyle/intern/system/PythonInterpreter.cpp b/source/blender/freestyle/intern/system/PythonInterpreter.cpp
index 9e7ef39..852030e 100644
--- a/source/blender/freestyle/intern/system/PythonInterpreter.cpp
+++ b/source/blender/freestyle/intern/system/PythonInterpreter.cpp
@@ -26,10 +26,3 @@
  */
 
 #include "PythonInterpreter.h"
-
-namespace Freestyle {
-
-string PythonInterpreter::_path = "";
-bool PythonInterpreter::_initialized = false;
-
-} /* namespace Freestyle */
diff --git a/source/blender/freestyle/intern/system/PythonInterpreter.h b/source/blender/freestyle/intern/system/PythonInterpreter.h
index 30ee6d3..069fbcf 100644
--- a/source/blender/freestyle/intern/system/PythonInterpreter.h
+++ b/source/blender/freestyle/intern/system/PythonInterpreter.h
@@ -60,12 +60,6 @@ public:
 		_language = "Python";
 		_context = 0;
 		memset(&_freestyle_bmain, 0, sizeof(Main));
-		//Py_Initialize();
-	}
-
-	virtual ~PythonInterpreter()
-	{
-		//Py_Finalize();
 	}
 
 	void setContext(bContext *C)
@@ -75,8 +69,6 @@ public:
 
 	int interpretFile(const string& filename)
 	{
-		initPath();
-
 		ReportList *reports = CTX_wm_reports(_context);
 		BKE_reports_clear(reports);
 		char *fn = const_cast<char*>(filename.c_str());
@@ -112,8 +104,6 @@ public:
 
 	int interpretText(struct Text *text, const string& name)
 	{
-		initPath();
-
 		ReportList *reports = CTX_wm_reports(_context);
 
 		BKE_reports_clear(reports);
@@ -131,63 +121,14 @@ public:
 		return 0;
 	}
 
-	struct Options
-	{
-		static void setPythonPath(const string& path)
-		{
-			_path = path;
-		}
-
-		static string getPythonPath()
-		{
-			return _path;
-		}
-	};
-
 	void reset()
 	{
-		Py_Finalize();
-		Py_Initialize();
-		_initialized = false;
+		// nothing to do
 	}
 
 private:
 	bContext *_context;
 	Main _freestyle_bmain;
-
-	void initPath()
-	{
-		if (_initialized)
-			return;
-
-		vector<string> pathnames;
-		StringUtils::getPathName(_path, "", pathnames);
-
-		struct Text *text = BKE_text_add(&_freestyle_bmain, "tmp_freestyle_initpath.txt");
-		string cmd = "import sys\n";
-		txt_insert_buf(text, const_cast<char*>(cmd.c_str()));
-
-		for (vector<string>::const_iterator it = pathnames.begin(); it != pathnames.end(); ++it) {
-			if (!it->empty()) {
-				if (G.debug & G_DEBUG_FREESTYLE) {
-					cout << "Adding Python path: " << *it << endl;
-				}
-				cmd = "sys.path.append(r\"" + *it + "\")\n";
-				txt_insert_buf(text, const_cast<char *>(cmd.c_str()));
-			}
-		}
-
-		BPY_text_exec(_context, text, NULL, false);
-
-		// cleaning up
-		BKE_text_unlink(&_freestyle_bmain, text);
-		BKE_libblock_free(&_freestyle_bmain, text);
-
-		_initialized = true;
-	}
-
-	static bool _initialized;
-	static string _path;
 };
 
 } /* namespace Freestyle */




More information about the Bf-blender-cvs mailing list