diff options
author | Maxime Curioni <maxime.curioni@gmail.com> | 2008-05-26 23:52:55 +0400 |
---|---|---|
committer | Maxime Curioni <maxime.curioni@gmail.com> | 2008-05-26 23:52:55 +0400 |
commit | 373d42dbeaef014ce6d8c9d337242966d7ca8e5f (patch) | |
tree | bf150d1c812ae8de8702580fd49ccfa9fe88f59f /source/blender/freestyle/intern/system | |
parent | fc8cd192a3a22322f6127345dc14f8463b35ee1f (diff) |
soc-2008-mxcurioni: PythonInterpreter works, using Blender's Python functions. The drawing still does not produce anything because the SWIG module wrapper is missing
Diffstat (limited to 'source/blender/freestyle/intern/system')
3 files changed, 56 insertions, 42 deletions
diff --git a/source/blender/freestyle/intern/system/PythonInterpreter.h b/source/blender/freestyle/intern/system/PythonInterpreter.h index b8eaa4a8a3c..d9b563fabaa 100755 --- a/source/blender/freestyle/intern/system/PythonInterpreter.h +++ b/source/blender/freestyle/intern/system/PythonInterpreter.h @@ -35,6 +35,12 @@ # include "StringUtils.h" # include "Interpreter.h" +//soc +extern "C" { +#include "BKE_text.h" +#include "BPY_extern.h" +} + class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter { public: @@ -42,12 +48,6 @@ class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter PythonInterpreter() { _language = "Python"; //Py_Initialize(); - - cout << "Freestyle Python Init: " << endl; - cout << "- is init : " << Py_IsInitialized() << endl; - cout << "- prog path : " << Py_GetProgramFullPath() << endl; - cout << "- mod path : " << Py_GetPath() << endl; - cout << "- version : " << Py_GetVersion() << endl; } virtual ~PythonInterpreter() { @@ -64,11 +64,22 @@ class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter int interpretFile(const string& filename) { initPath(); - string cmd("execfile(\"" + filename + "\")"); - char* c_cmd = strdup(cmd.c_str()); - int err = PyRun_SimpleString(c_cmd); - free(c_cmd); - return err; + + char *fn = const_cast<char*>(filename.c_str()); + struct Text *text = add_text( fn ); + + if (text == NULL) { + cout << "\nError in PythonInterpreter::interpretFile:" << endl; + cout << "couldn't create Blender text from" << fn << endl; + } + + if (BPY_txt_do_python_Text(text) != 1) { + cout << "\nError executing Python script from PythonInterpreter::interpretFile:" << endl; + cout << fn << " (at line " << BPY_Err_getLinenumber() << ")" << endl; + return BPY_Err_getLinenumber(); + } + + return 0; } struct Options @@ -91,22 +102,20 @@ class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter private: static void initPath() { - if (_initialized) - return; - PyRun_SimpleString("import sys"); - vector<string> pathnames; - StringUtils::getPathName(_path, "", pathnames); - string cmd; - char* c_cmd; - for (vector<string>::const_iterator it = pathnames.begin(); - it != pathnames.end(); - ++it) { - cmd = "sys.path.append(\"" + *it + "\")"; - c_cmd = strdup(cmd.c_str()); - PyRun_SimpleString(c_cmd); - free(c_cmd); - } - PyRun_SimpleString("from Freestyle import *"); + if (_initialized) + return; + + // vector<string> pathnames; + // StringUtils::getPathName(_path, "", pathnames); + // + // for (vector<string>::const_iterator it = pathnames.begin(); it != pathnames.end();++it) { + // if ( !it->empty() ) { + // cout << "Adding Python path: " << *it << endl; + // syspath_append( const_cast<char*>(it->c_str()) ); + // } + // } + + //PyRun_SimpleString("from Freestyle import *"); _initialized = true; } diff --git a/source/blender/freestyle/intern/system/StringUtils.cpp b/source/blender/freestyle/intern/system/StringUtils.cpp index bbdbef10a10..58798daf42a 100755 --- a/source/blender/freestyle/intern/system/StringUtils.cpp +++ b/source/blender/freestyle/intern/system/StringUtils.cpp @@ -27,25 +27,29 @@ namespace StringUtils { void getPathName(const string& path, const string& base, vector<string>& pathnames) { string dir; + string res; + char cleaned[FILE_MAX]; unsigned size = path.size(); + pathnames.push_back(base); - for (unsigned pos = 0, sep = path.find(Config::PATH_SEP, pos); - pos < size; - pos = sep + 1, sep = path.find(Config::PATH_SEP, pos)) { - if (sep == (unsigned)string::npos) - sep = size; - dir = path.substr(pos, sep - pos); -//soc QFileInfo fi(dir.c_str()); -//soc string res = (const char*)fi.absoluteFilePath().toAscii(); - char cleaned[FILE_MAX]; - BLI_strncpy(cleaned, dir.c_str(), FILE_MAX); - BLI_cleanup_file(NULL, cleaned); - string res = toAscii( string(cleaned) ); + for ( unsigned pos = 0, sep = path.find(Config::PATH_SEP, pos); + pos < size; + pos = sep + 1, sep = path.find(Config::PATH_SEP, pos)) { + + if (sep == (unsigned)string::npos) + sep = size; + + dir = path.substr(pos, sep - pos); + + BLI_strncpy(cleaned, dir.c_str(), FILE_MAX); + BLI_cleanup_file(NULL, cleaned); + res = toAscii( string(cleaned) ); - if (!base.empty()) - res += Config::DIR_SEP + base; - pathnames.push_back(res); + if (!base.empty()) + res += Config::DIR_SEP + base; + + pathnames.push_back(res); } } diff --git a/source/blender/freestyle/intern/system/StringUtils.h b/source/blender/freestyle/intern/system/StringUtils.h index b30eaae2247..7b43886fa27 100755 --- a/source/blender/freestyle/intern/system/StringUtils.h +++ b/source/blender/freestyle/intern/system/StringUtils.h @@ -31,6 +31,7 @@ # include <vector> # include <string> # include <sstream> +# include <iostream> # include "FreestyleConfig.h" //soc |