diff options
Diffstat (limited to 'source/blender/python/intern/bpy_interface.c')
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 94 |
1 files changed, 53 insertions, 41 deletions
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 02eebde8a13..bf91b0498ed 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -1,5 +1,5 @@ /** - * $Id: + * $Id$ * * ***** BEGIN GPL LICENSE BLOCK ***** * @@ -23,10 +23,6 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> /* grr, python redefines */ @@ -34,33 +30,20 @@ #undef _POSIX_C_SOURCE #endif -#include <Python.h> -#include "compile.h" /* for the PyCodeObject */ -#include "eval.h" /* for PyEval_EvalCode */ #include "bpy.h" #include "bpy_rna.h" #include "bpy_util.h" -#ifndef WIN32 -#include <dirent.h> -#else -#include "BLI_winstuff.h" -#endif - #include "DNA_space_types.h" #include "DNA_text_types.h" #include "MEM_guardedalloc.h" - -#include "BLI_storage.h" -#include "BLI_fileops.h" -#include "BLI_string.h" #include "BLI_path_util.h" +#include "BLI_math_base.h" #include "BKE_context.h" #include "BKE_text.h" -#include "BKE_context.h" #include "BKE_main.h" #include "BKE_global.h" /* only for script checking */ @@ -167,7 +150,6 @@ void BPY_update_modules( void ) *****************************************************************************/ static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) { - PyObject *mod; PyObject *item; PyObject *dict = PyDict_New( ); PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) ); @@ -183,18 +165,13 @@ static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) Py_DECREF(item); } - /* add bpy to global namespace */ - mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0); - PyDict_SetItemString( dict, "bpy", mod ); - Py_DECREF(mod); - return dict; } /* must be called before Py_Initialize */ void BPY_start_python_path(void) { - char *py_path_bundle= BLI_gethome_folder("python", BLI_GETHOME_ALL); + char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL); if(py_path_bundle==NULL) return; @@ -248,12 +225,11 @@ void BPY_start_python( int argc, char **argv ) BPY_start_python_path(); /* allow to use our own included python */ + // Py_SetProgramName(); // extern char bprogname[FILE_MAXDIR+FILE_MAXFILE]; + Py_Initialize( ); - /*convert argv to wchar_t*/ // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal - - /*temporarily set argv*/ /* sigh, why do python guys not have a char** version anymore? :( */ { int i; @@ -347,16 +323,17 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc } bpy_context_set(C, &gilstate); - - py_dict = CreateGlobalDictionary(C, text?text->id.name+2:fn); if (text) { + char fn_dummy[FILE_MAXDIR]; + bpy_text_filename_get(fn_dummy, text); + py_dict = CreateGlobalDictionary(C, fn_dummy); if( !text->compiled ) { /* if it wasn't already compiled, do it now */ char *buf = txt_to_buf( text ); text->compiled = - Py_CompileString( buf, text->id.name+2, Py_file_input ); + Py_CompileString( buf, fn_dummy, Py_file_input ); MEM_freeN( buf ); @@ -367,8 +344,12 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc if(text->compiled) py_result = PyEval_EvalCode( text->compiled, py_dict, py_dict ); - } else { - FILE *fp= fopen(fn, "r"); + } + else { + FILE *fp= fopen(fn, "r"); + + py_dict = CreateGlobalDictionary(C, fn); + if(fp) { #ifdef _WIN32 /* Previously we used PyRun_File to run directly the code on a FILE @@ -547,14 +528,8 @@ int BPY_run_python_script_space(const char *modulename, const char *func) } #endif -// #define TIME_REGISTRATION - -#ifdef TIME_REGISTRATION -#include "PIL_time.h" -#endif - -int BPY_button_eval(bContext *C, char *expr, double *value) +int BPY_eval_button(bContext *C, const char *expr, double *value) { PyGILState_STATE gilstate; PyObject *dict, *mod, *retval; @@ -607,6 +582,9 @@ int BPY_button_eval(bContext *C, char *expr, double *value) if(val==-1 && PyErr_Occurred()) { error_ret= -1; } + else if (!finite(val)) { + *value= 0.0; + } else { *value= val; } @@ -622,6 +600,40 @@ int BPY_button_eval(bContext *C, char *expr, double *value) return error_ret; } +int BPY_eval_string(bContext *C, const char *expr) +{ + PyGILState_STATE gilstate; + PyObject *dict, *retval; + int error_ret = 0; + + if (!expr) return -1; + + if(expr[0]=='\0') { + return error_ret; + } + + bpy_context_set(C, &gilstate); + + dict= CreateGlobalDictionary(C, NULL); + + retval = PyRun_String(expr, Py_eval_input, dict, dict); + + if (retval == NULL) { + error_ret= -1; + + BPy_errors_to_report(CTX_wm_reports(C)); + } + else { + Py_DECREF(retval); + } + + Py_DECREF(dict); + bpy_context_clear(C, &gilstate); + + return error_ret; +} + + void BPY_load_user_modules(bContext *C) { PyGILState_STATE gilstate; |