2009-08-26 06:15:43 +00:00
|
|
|
/**
|
2010-03-21 01:14:04 +00:00
|
|
|
* $Id$
|
2009-08-26 06:15:43 +00:00
|
|
|
*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-08-26 06:15:43 +00:00
|
|
|
*
|
|
|
|
* Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
|
|
|
|
* Chris Keith, Chris Want, Ken Hughes, Campbell Barton
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2009-02-16 16:17:20 +00:00
|
|
|
|
2009-08-26 06:15:43 +00:00
|
|
|
/* grr, python redefines */
|
|
|
|
#ifdef _POSIX_C_SOURCE
|
|
|
|
#undef _POSIX_C_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2010-02-11 14:08:22 +00:00
|
|
|
#include "bpy.h"
|
2008-11-29 13:36:08 +00:00
|
|
|
#include "bpy_rna.h"
|
2009-06-05 16:11:35 +00:00
|
|
|
#include "bpy_util.h"
|
2009-02-28 13:27:45 +00:00
|
|
|
|
|
|
|
#include "DNA_space_types.h"
|
2009-03-04 13:26:33 +00:00
|
|
|
#include "DNA_text_types.h"
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2010-02-11 14:08:22 +00:00
|
|
|
#include "BLI_path_util.h"
|
2010-03-15 18:52:22 +00:00
|
|
|
#include "BLI_math_base.h"
|
2010-09-04 09:27:21 +00:00
|
|
|
#include "BLI_string.h"
|
2009-04-11 02:18:24 +00:00
|
|
|
|
|
|
|
#include "BKE_context.h"
|
|
|
|
#include "BKE_text.h"
|
2010-07-26 04:21:44 +00:00
|
|
|
#include "BKE_font.h" /* only for utf8towchar */
|
2009-11-20 15:01:09 +00:00
|
|
|
#include "BKE_main.h"
|
2010-02-27 01:27:22 +00:00
|
|
|
#include "BKE_global.h" /* only for script checking */
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2.5
More cleanup!
- removed old UI font completely, including from uiBeginBlock
- emboss hints for uiBlock only have three types now;
Regular, Pulldown, or "Nothing" (only icon/text)
- removed old font path from Userdef
- removed all old button theme hinting
- removed old "auto block" to merge buttons in groups
(was only in use for radiosity buttons)
And went over all warnings. One hooray for make giving clean output :)
Well, we need uniform definitions for warnings, so people at least fix
them... here's the real bad bugs I found:
- in mesh code, a call to editmesh mixed *em and *me
- in armature, ED_util.h was not included, so no warnings for wrong call
to ED_undo_push()
- The extern Py api .h was not included in the bpy_interface.c, showing
a several calls using different args.
Further just added the missing includes, and removed unused vars.
2009-04-14 15:59:52 +00:00
|
|
|
#include "BPY_extern.h"
|
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
#include "../generic/bpy_internal_import.h" // our own imports
|
2010-09-18 15:30:03 +00:00
|
|
|
#include "../generic/py_capi_utils.h"
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
/* for internal use, when starting and ending python scripts */
|
|
|
|
|
|
|
|
/* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
|
|
|
|
static int py_call_level= 0;
|
2010-02-11 14:08:22 +00:00
|
|
|
BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
// only for tests
|
|
|
|
#define TIME_PY_RUN
|
|
|
|
|
|
|
|
#ifdef TIME_PY_RUN
|
|
|
|
#include "PIL_time.h"
|
|
|
|
static int bpy_timer_count = 0;
|
|
|
|
static double bpy_timer; /* time since python starts */
|
|
|
|
static double bpy_timer_run; /* time for each python script run */
|
|
|
|
static double bpy_timer_run_tot; /* accumulate python runs */
|
|
|
|
#endif
|
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
|
|
|
|
{
|
|
|
|
py_call_level++;
|
|
|
|
|
|
|
|
if(gilstate)
|
|
|
|
*gilstate = PyGILState_Ensure();
|
|
|
|
|
|
|
|
if(py_call_level==1) {
|
|
|
|
|
|
|
|
if(C) { // XXX - should always be true.
|
|
|
|
BPy_SetContext(C);
|
|
|
|
bpy_import_main_set(CTX_data_main(C));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
|
|
|
|
}
|
2009-08-09 13:20:12 +00:00
|
|
|
|
2009-11-05 11:17:09 +00:00
|
|
|
BPY_update_modules(); /* can give really bad results if this isnt here */
|
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#ifdef TIME_PY_RUN
|
|
|
|
if(bpy_timer_count==0) {
|
|
|
|
/* record time from the beginning */
|
|
|
|
bpy_timer= PIL_check_seconds_timer();
|
|
|
|
bpy_timer_run = bpy_timer_run_tot = 0.0;
|
|
|
|
}
|
|
|
|
bpy_timer_run= PIL_check_seconds_timer();
|
|
|
|
|
|
|
|
|
|
|
|
bpy_timer_count++;
|
|
|
|
#endif
|
2009-08-07 16:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bpy_context_clear(bContext *C, PyGILState_STATE *gilstate)
|
|
|
|
{
|
|
|
|
py_call_level--;
|
|
|
|
|
|
|
|
if(gilstate)
|
|
|
|
PyGILState_Release(*gilstate);
|
|
|
|
|
|
|
|
if(py_call_level < 0) {
|
|
|
|
fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
|
|
|
|
}
|
|
|
|
else if(py_call_level==0) {
|
|
|
|
// XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
|
|
|
|
//BPy_SetContext(NULL);
|
|
|
|
//bpy_import_main_set(NULL);
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
#ifdef TIME_PY_RUN
|
|
|
|
bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
|
|
|
|
bpy_timer_count++;
|
|
|
|
#endif
|
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
void BPY_free_compiled_text( struct Text *text )
|
|
|
|
{
|
|
|
|
if( text->compiled ) {
|
|
|
|
Py_DECREF( ( PyObject * ) text->compiled );
|
|
|
|
text->compiled = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2009-04-11 16:17:39 +00:00
|
|
|
void BPY_update_modules( void )
|
|
|
|
{
|
2009-08-15 09:53:38 +00:00
|
|
|
#if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
|
2009-04-11 16:17:39 +00:00
|
|
|
PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
|
|
|
|
PyModule_AddObject( mod, "data", BPY_rna_module() );
|
2009-08-15 09:53:38 +00:00
|
|
|
PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* refreshes the main struct */
|
|
|
|
BPY_update_rna_module();
|
2009-11-05 11:17:09 +00:00
|
|
|
bpy_context_module->ptr.data= (void *)BPy_GetContext();
|
2009-04-11 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
2009-07-03 04:38:55 +00:00
|
|
|
/* must be called before Py_Initialize */
|
|
|
|
void BPY_start_python_path(void)
|
|
|
|
{
|
2010-07-04 15:35:23 +00:00
|
|
|
char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL);
|
2009-07-03 04:38:55 +00:00
|
|
|
|
|
|
|
if(py_path_bundle==NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* set the environment path */
|
|
|
|
printf("found bundled python: %s\n", py_path_bundle);
|
|
|
|
|
2009-12-08 12:16:34 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* OSX allow file/directory names to contain : character (represented as / in the Finder)
|
|
|
|
but current Python lib (release 3.1.1) doesn't handle these correctly */
|
|
|
|
if(strchr(py_path_bundle, ':'))
|
|
|
|
printf("Warning : Blender application is located in a path containing : or / chars\
|
|
|
|
\nThis may make python import function fail\n");
|
|
|
|
#endif
|
|
|
|
|
2010-01-11 11:11:21 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* cmake/MSVC debug build crashes without this, why only
|
|
|
|
in this case is unknown.. */
|
|
|
|
{
|
2010-09-15 15:29:31 +00:00
|
|
|
BLI_setenv("PYTHONPATH", py_path_bundle);
|
2010-01-11 11:11:21 +00:00
|
|
|
}
|
2009-09-13 18:09:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
2010-07-26 04:21:44 +00:00
|
|
|
static wchar_t py_path_bundle_wchar[FILE_MAX];
|
|
|
|
|
2010-07-27 04:02:09 +00:00
|
|
|
/* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
|
|
|
|
/* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
|
|
|
|
|
2010-07-26 04:21:44 +00:00
|
|
|
utf8towchar(py_path_bundle_wchar, py_path_bundle);
|
2009-09-13 18:09:13 +00:00
|
|
|
|
|
|
|
Py_SetPythonHome(py_path_bundle_wchar);
|
2010-07-26 04:21:44 +00:00
|
|
|
// printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
|
2009-09-13 18:09:13 +00:00
|
|
|
}
|
2009-07-03 04:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-13 09:28:05 +00:00
|
|
|
|
|
|
|
void BPY_set_context(bContext *C)
|
|
|
|
{
|
|
|
|
BPy_SetContext(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call BPY_set_context first */
|
2.5
More cleanup!
- removed old UI font completely, including from uiBeginBlock
- emboss hints for uiBlock only have three types now;
Regular, Pulldown, or "Nothing" (only icon/text)
- removed old font path from Userdef
- removed all old button theme hinting
- removed old "auto block" to merge buttons in groups
(was only in use for radiosity buttons)
And went over all warnings. One hooray for make giving clean output :)
Well, we need uniform definitions for warnings, so people at least fix
them... here's the real bad bugs I found:
- in mesh code, a call to editmesh mixed *em and *me
- in armature, ED_util.h was not included, so no warnings for wrong call
to ED_undo_push()
- The extern Py api .h was not included in the bpy_interface.c, showing
a several calls using different args.
Further just added the missing includes, and removed unused vars.
2009-04-14 15:59:52 +00:00
|
|
|
void BPY_start_python( int argc, char **argv )
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
|
|
|
PyThreadState *py_tstate = NULL;
|
2009-04-11 16:17:39 +00:00
|
|
|
|
2010-08-01 15:15:57 +00:00
|
|
|
/* not essential but nice to set our name */
|
|
|
|
static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
|
|
|
|
utf8towchar(bprogname_wchar, bprogname);
|
|
|
|
Py_SetProgramName(bprogname_wchar);
|
2009-07-03 04:38:55 +00:00
|
|
|
|
2010-08-01 15:15:57 +00:00
|
|
|
BPY_start_python_path(); /* allow to use our own included python */
|
2010-07-02 20:09:42 +00:00
|
|
|
|
2008-11-29 13:36:08 +00:00
|
|
|
Py_Initialize( );
|
|
|
|
|
2009-08-10 00:07:34 +00:00
|
|
|
// PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
|
2009-07-08 09:23:49 +00:00
|
|
|
/* sigh, why do python guys not have a char** version anymore? :( */
|
|
|
|
{
|
|
|
|
int i;
|
2009-12-07 10:41:16 +00:00
|
|
|
#if 0
|
2009-07-08 09:23:49 +00:00
|
|
|
PyObject *py_argv= PyList_New(argc);
|
|
|
|
for (i=0; i<argc; i++)
|
|
|
|
PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
|
|
|
|
|
2009-12-07 10:41:16 +00:00
|
|
|
#else // should fix bug #20021 - utf path name problems
|
|
|
|
PyObject *py_argv= PyList_New(0);
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
PyObject *item= PyUnicode_Decode(argv[i], strlen(argv[i]), Py_FileSystemDefaultEncoding, NULL);
|
|
|
|
if(item==NULL) { // should never happen
|
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyList_Append(py_argv, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-08 09:23:49 +00:00
|
|
|
PySys_SetObject("argv", py_argv);
|
|
|
|
Py_DECREF(py_argv);
|
|
|
|
}
|
2008-11-29 13:36:08 +00:00
|
|
|
|
|
|
|
/* Initialize thread support (also acquires lock) */
|
|
|
|
PyEval_InitThreads();
|
|
|
|
|
2009-04-11 16:17:39 +00:00
|
|
|
|
|
|
|
/* bpy.* and lets us import it */
|
2010-02-11 14:08:22 +00:00
|
|
|
BPy_init_modules();
|
2009-04-11 16:17:39 +00:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
{ /* our own import and reload functions */
|
|
|
|
PyObject *item;
|
|
|
|
//PyObject *m = PyImport_AddModule("__builtin__");
|
|
|
|
//PyObject *d = PyModule_GetDict(m);
|
|
|
|
PyObject *d = PyEval_GetBuiltins( );
|
|
|
|
PyDict_SetItemString(d, "reload", item=PyCFunction_New(bpy_reload_meth, NULL)); Py_DECREF(item);
|
|
|
|
PyDict_SetItemString(d, "__import__", item=PyCFunction_New(bpy_import_meth, NULL)); Py_DECREF(item);
|
|
|
|
}
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2009-08-15 09:53:38 +00:00
|
|
|
pyrna_alloc_types();
|
|
|
|
|
2008-11-29 13:36:08 +00:00
|
|
|
py_tstate = PyGILState_GetThisThreadState();
|
|
|
|
PyEval_ReleaseThread(py_tstate);
|
|
|
|
}
|
|
|
|
|
2008-12-27 04:55:45 +00:00
|
|
|
void BPY_end_python( void )
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2009-08-14 13:13:36 +00:00
|
|
|
// fprintf(stderr, "Ending Python!\n");
|
2009-08-14 12:29:55 +00:00
|
|
|
|
2008-11-29 13:36:08 +00:00
|
|
|
PyGILState_Ensure(); /* finalizing, no need to grab the state */
|
|
|
|
|
|
|
|
// free other python data.
|
2009-08-14 12:29:55 +00:00
|
|
|
pyrna_free_types();
|
|
|
|
|
|
|
|
/* clear all python data from structs */
|
2008-11-29 13:36:08 +00:00
|
|
|
|
|
|
|
Py_Finalize( );
|
2009-03-11 17:28:37 +00:00
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#ifdef TIME_PY_RUN
|
|
|
|
// measure time since py started
|
|
|
|
bpy_timer = PIL_check_seconds_timer() - bpy_timer;
|
|
|
|
|
|
|
|
printf("*bpy stats* - ");
|
|
|
|
printf("tot exec: %d, ", bpy_timer_count);
|
|
|
|
printf("tot run: %.4fsec, ", bpy_timer_run_tot);
|
|
|
|
if(bpy_timer_count>0)
|
|
|
|
printf("average run: %.6fsec, ", (bpy_timer_run_tot/bpy_timer_count));
|
|
|
|
|
|
|
|
if(bpy_timer>0.0)
|
|
|
|
printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
2009-08-14 13:13:36 +00:00
|
|
|
// fprintf(stderr, "Ending Python Done!\n");
|
2009-08-14 12:29:55 +00:00
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#endif
|
|
|
|
|
2008-11-29 13:36:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
/* Can run a file or text block */
|
2009-06-14 12:53:47 +00:00
|
|
|
int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2009-08-07 16:20:19 +00:00
|
|
|
PyObject *py_dict, *py_result= NULL;
|
2008-11-29 13:36:08 +00:00
|
|
|
PyGILState_STATE gilstate;
|
2009-03-04 13:26:33 +00:00
|
|
|
|
|
|
|
if (fn==NULL && text==NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-02 02:55:12 +00:00
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
|
|
|
if (text) {
|
2010-06-02 14:40:58 +00:00
|
|
|
char fn_dummy[FILE_MAXDIR];
|
|
|
|
bpy_text_filename_get(fn_dummy, text);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
|
|
|
if( !text->compiled ) { /* if it wasn't already compiled, do it now */
|
|
|
|
char *buf = txt_to_buf( text );
|
|
|
|
|
|
|
|
text->compiled =
|
2010-06-02 14:40:58 +00:00
|
|
|
Py_CompileString( buf, fn_dummy, Py_file_input );
|
2009-03-04 13:26:33 +00:00
|
|
|
|
|
|
|
MEM_freeN( buf );
|
|
|
|
|
|
|
|
if( PyErr_Occurred( ) ) {
|
|
|
|
BPY_free_compiled_text( text );
|
|
|
|
}
|
|
|
|
}
|
2010-09-18 15:30:03 +00:00
|
|
|
|
|
|
|
if(text->compiled) {
|
|
|
|
py_dict = PyC_DefaultNameSpace(fn_dummy);
|
|
|
|
py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict);
|
|
|
|
}
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2010-06-02 14:40:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-02 19:37:53 +00:00
|
|
|
FILE *fp= fopen(fn, "r");
|
|
|
|
|
2009-07-26 18:18:14 +00:00
|
|
|
if(fp) {
|
2010-09-18 15:30:03 +00:00
|
|
|
py_dict = PyC_DefaultNameSpace(fn);
|
|
|
|
|
2009-11-11 13:27:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Previously we used PyRun_File to run directly the code on a FILE
|
|
|
|
* object, but as written in the Python/C API Ref Manual, chapter 2,
|
|
|
|
* 'FILE structs for different C libraries can be different and
|
|
|
|
* incompatible'.
|
|
|
|
* So now we load the script file data to a buffer */
|
|
|
|
char *pystring;
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2010-02-08 13:55:31 +00:00
|
|
|
pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
|
2009-11-11 13:27:54 +00:00
|
|
|
pystring[0]= '\0';
|
|
|
|
sprintf(pystring, "exec(open(r'%s').read())", fn);
|
|
|
|
py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
|
2010-02-08 13:55:31 +00:00
|
|
|
MEM_freeN(pystring);
|
2009-11-11 13:27:54 +00:00
|
|
|
#else
|
2009-07-26 18:18:14 +00:00
|
|
|
py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
|
|
|
|
fclose(fp);
|
2009-11-11 13:27:54 +00:00
|
|
|
#endif
|
2009-07-26 18:18:14 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
|
|
|
|
py_result= NULL;
|
|
|
|
}
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
if (!py_result) {
|
2009-06-14 12:53:47 +00:00
|
|
|
BPy_errors_to_report(reports);
|
2009-03-04 13:26:33 +00:00
|
|
|
} else {
|
2008-11-29 13:36:08 +00:00
|
|
|
Py_DECREF( py_result );
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
2010-08-01 13:57:04 +00:00
|
|
|
|
|
|
|
PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
return py_result ? 1:0;
|
2008-11-29 13:36:08 +00:00
|
|
|
}
|
2009-02-28 13:27:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* TODO - move into bpy_space.c ? */
|
|
|
|
/* GUI interface routines */
|
|
|
|
|
|
|
|
/* Copied from Draw.c */
|
|
|
|
static void exit_pydraw( SpaceScript * sc, short err )
|
|
|
|
{
|
|
|
|
Script *script = NULL;
|
|
|
|
|
|
|
|
if( !sc || !sc->script )
|
|
|
|
return;
|
|
|
|
|
|
|
|
script = sc->script;
|
|
|
|
|
|
|
|
if( err ) {
|
2009-06-14 12:53:47 +00:00
|
|
|
BPy_errors_to_report(NULL); // TODO, reports
|
2009-02-28 13:27:45 +00:00
|
|
|
script->flags = 0; /* mark script struct for deletion */
|
|
|
|
SCRIPT_SET_NULL(script);
|
|
|
|
script->scriptname[0] = '\0';
|
|
|
|
script->scriptarg[0] = '\0';
|
|
|
|
// XXX 2.5 error_pyscript();
|
|
|
|
// XXX 2.5 scrarea_queue_redraw( sc->area );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 // XXX 2.5
|
|
|
|
BPy_Set_DrawButtonsList(sc->but_refs);
|
|
|
|
BPy_Free_DrawButtonsList(); /*clear all temp button references*/
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sc->but_refs = NULL;
|
|
|
|
|
|
|
|
Py_XDECREF( ( PyObject * ) script->py_draw );
|
|
|
|
Py_XDECREF( ( PyObject * ) script->py_event );
|
|
|
|
Py_XDECREF( ( PyObject * ) script->py_button );
|
|
|
|
|
|
|
|
script->py_draw = script->py_event = script->py_button = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bpy_run_script_init(bContext *C, SpaceScript * sc)
|
|
|
|
{
|
|
|
|
if (sc->script==NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
|
2009-06-14 12:53:47 +00:00
|
|
|
BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
|
2009-02-28 13:27:45 +00:00
|
|
|
|
|
|
|
if (sc->script->py_draw==NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-09 15:40:04 +00:00
|
|
|
int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
|
2009-02-28 13:27:45 +00:00
|
|
|
{
|
2009-07-09 15:40:04 +00:00
|
|
|
if (bpy_run_script_init( (bContext *)C, sc)) {
|
2009-02-28 13:27:45 +00:00
|
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
|
|
|
PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
|
|
|
|
|
|
|
|
if (result==NULL)
|
|
|
|
exit_pydraw(sc, 1);
|
2009-03-02 20:18:29 +00:00
|
|
|
|
|
|
|
PyGILState_Release(gilstate);
|
2009-02-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX - not used yet, listeners dont get a context
|
|
|
|
int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
|
|
|
|
{
|
|
|
|
if (bpy_run_script_init(C, sc)) {
|
|
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
|
|
|
|
|
|
|
PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
|
|
|
|
|
|
|
|
if (result==NULL)
|
|
|
|
exit_pydraw(sc, 1);
|
2009-03-02 20:18:29 +00:00
|
|
|
|
|
|
|
PyGILState_Release(gilstate);
|
2009-02-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-01 12:43:07 +00:00
|
|
|
void BPY_DECREF(void *pyob_ptr)
|
|
|
|
{
|
2009-12-08 10:36:46 +00:00
|
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
2009-04-01 12:43:07 +00:00
|
|
|
Py_DECREF((PyObject *)pyob_ptr);
|
2009-12-08 10:36:46 +00:00
|
|
|
PyGILState_Release(gilstate);
|
2009-04-01 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 13:27:45 +00:00
|
|
|
#if 0
|
|
|
|
/* called from the the scripts window, assume context is ok */
|
|
|
|
int BPY_run_python_script_space(const char *modulename, const char *func)
|
|
|
|
{
|
|
|
|
PyObject *py_dict, *py_result= NULL;
|
|
|
|
char pystring[512];
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
|
|
|
|
/* for calling the module function */
|
|
|
|
PyObject *py_func,
|
|
|
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
|
2010-09-18 15:30:03 +00:00
|
|
|
py_dict = PyC_DefaultNameSpace("<dummy>");
|
2009-02-28 13:27:45 +00:00
|
|
|
|
|
|
|
PyObject *module = PyImport_ImportModule(scpt->script.filename);
|
|
|
|
if (module==NULL) {
|
|
|
|
PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
py_func = PyObject_GetAttrString(modulename, func);
|
|
|
|
if (py_func==NULL) {
|
|
|
|
PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
|
|
|
|
}
|
|
|
|
else {
|
2009-03-18 22:22:58 +00:00
|
|
|
Py_DECREF(py_func);
|
2009-02-28 13:27:45 +00:00
|
|
|
if (!PyCallable_Check(py_func)) {
|
|
|
|
PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-23 09:15:42 +00:00
|
|
|
if (!py_result) {
|
2009-06-14 12:53:47 +00:00
|
|
|
BPy_errors_to_report(NULL); // TODO - reports
|
2009-04-23 09:15:42 +00:00
|
|
|
} else
|
2009-02-28 13:27:45 +00:00
|
|
|
Py_DECREF( py_result );
|
|
|
|
|
|
|
|
Py_XDECREF(module);
|
|
|
|
|
2010-08-01 13:57:04 +00:00
|
|
|
PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
|
2009-02-28 13:27:45 +00:00
|
|
|
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
int BPY_eval_button(bContext *C, const char *expr, double *value)
|
2009-08-10 11:58:53 +00:00
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2010-08-01 13:57:04 +00:00
|
|
|
PyObject *py_dict, *mod, *retval;
|
2009-08-10 11:58:53 +00:00
|
|
|
int error_ret = 0;
|
|
|
|
|
2010-02-27 22:53:37 +00:00
|
|
|
if (!value || !expr) return -1;
|
|
|
|
|
|
|
|
if(expr[0]=='\0') {
|
|
|
|
*value= 0.0;
|
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
2010-09-18 15:30:03 +00:00
|
|
|
py_dict= PyC_DefaultNameSpace("<blender button>");
|
2009-10-22 23:23:09 +00:00
|
|
|
|
|
|
|
mod = PyImport_ImportModule("math");
|
|
|
|
if (mod) {
|
2010-08-01 13:57:04 +00:00
|
|
|
PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
|
2009-10-22 23:23:09 +00:00
|
|
|
Py_DECREF(mod);
|
2010-02-27 22:53:37 +00:00
|
|
|
}
|
|
|
|
else { /* highly unlikely but possibly */
|
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2009-10-22 23:23:09 +00:00
|
|
|
|
2010-08-01 13:57:04 +00:00
|
|
|
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
2009-08-10 11:58:53 +00:00
|
|
|
|
|
|
|
if (retval == NULL) {
|
|
|
|
error_ret= -1;
|
|
|
|
}
|
|
|
|
else {
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
double val;
|
|
|
|
|
|
|
|
if(PyTuple_Check(retval)) {
|
|
|
|
/* Users my have typed in 10km, 2m
|
|
|
|
* add up all values */
|
|
|
|
int i;
|
|
|
|
val= 0.0;
|
|
|
|
|
|
|
|
for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
|
|
|
|
val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
val = PyFloat_AsDouble(retval);
|
|
|
|
}
|
2009-08-10 11:58:53 +00:00
|
|
|
Py_DECREF(retval);
|
|
|
|
|
|
|
|
if(val==-1 && PyErr_Occurred()) {
|
|
|
|
error_ret= -1;
|
|
|
|
}
|
2010-03-14 21:04:02 +00:00
|
|
|
else if (!finite(val)) {
|
|
|
|
*value= 0.0;
|
|
|
|
}
|
2009-08-10 11:58:53 +00:00
|
|
|
else {
|
|
|
|
*value= val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(error_ret) {
|
|
|
|
BPy_errors_to_report(CTX_wm_reports(C));
|
|
|
|
}
|
2010-07-28 23:24:17 +00:00
|
|
|
|
2010-08-01 13:57:04 +00:00
|
|
|
PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
|
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
int BPY_eval_string(bContext *C, const char *expr)
|
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2010-08-01 13:57:04 +00:00
|
|
|
PyObject *py_dict, *retval;
|
2010-05-30 14:05:58 +00:00
|
|
|
int error_ret = 0;
|
|
|
|
|
|
|
|
if (!expr) return -1;
|
|
|
|
|
|
|
|
if(expr[0]=='\0') {
|
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
2010-09-18 15:30:03 +00:00
|
|
|
py_dict= PyC_DefaultNameSpace("<blender string>");
|
2010-05-30 14:05:58 +00:00
|
|
|
|
2010-08-01 13:57:04 +00:00
|
|
|
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
2010-05-30 14:05:58 +00:00
|
|
|
|
|
|
|
if (retval == NULL) {
|
|
|
|
error_ret= -1;
|
|
|
|
|
|
|
|
BPy_errors_to_report(CTX_wm_reports(C));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
}
|
|
|
|
|
2010-08-01 13:57:04 +00:00
|
|
|
PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
|
2010-07-28 23:24:17 +00:00
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
bpy_context_clear(C, &gilstate);
|
2010-08-01 13:57:04 +00:00
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-20 15:01:09 +00:00
|
|
|
void BPY_load_user_modules(bContext *C)
|
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2010-01-10 20:01:13 +00:00
|
|
|
Main *bmain= CTX_data_main(C);
|
2009-11-20 15:01:09 +00:00
|
|
|
Text *text;
|
|
|
|
|
2010-01-10 20:01:13 +00:00
|
|
|
/* can happen on file load */
|
|
|
|
if(bmain==NULL)
|
|
|
|
return;
|
|
|
|
|
2009-11-20 15:01:09 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
|
|
for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
|
|
|
|
if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
|
2010-02-27 12:01:10 +00:00
|
|
|
if(!(G.f & G_SCRIPT_AUTOEXEC)) {
|
2010-02-27 01:27:22 +00:00
|
|
|
printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
|
2009-11-20 15:01:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-02-27 01:27:22 +00:00
|
|
|
PyObject *module= bpy_text_import(text);
|
|
|
|
|
|
|
|
if (module==NULL) {
|
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(module);
|
|
|
|
}
|
2009-11-20 15:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
}
|
2009-10-29 09:25:11 +00:00
|
|
|
|
2009-11-10 16:18:54 +00:00
|
|
|
int BPY_context_get(bContext *C, const char *member, bContextDataResult *result)
|
2009-10-29 09:25:11 +00:00
|
|
|
{
|
|
|
|
PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
|
|
|
|
PyObject *item= PyDict_GetItemString(pyctx, member);
|
|
|
|
PointerRNA *ptr= NULL;
|
|
|
|
int done= 0;
|
|
|
|
|
|
|
|
if(item==NULL) {
|
|
|
|
/* pass */
|
|
|
|
}
|
|
|
|
else if(item==Py_None) {
|
|
|
|
/* pass */
|
|
|
|
}
|
|
|
|
else if(BPy_StructRNA_Check(item)) {
|
|
|
|
ptr= &(((BPy_StructRNA *)item)->ptr);
|
|
|
|
|
|
|
|
//result->ptr= ((BPy_StructRNA *)item)->ptr;
|
|
|
|
CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
|
|
|
|
done= 1;
|
|
|
|
}
|
2009-10-29 10:03:34 +00:00
|
|
|
else if (PySequence_Check(item)) {
|
|
|
|
PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
|
|
|
|
if (seq_fast==NULL) {
|
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int len= PySequence_Fast_GET_SIZE(seq_fast);
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
|
|
|
|
|
|
|
|
if(BPy_StructRNA_Check(list_item)) {
|
|
|
|
/*
|
|
|
|
CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
|
|
|
|
link->ptr= ((BPy_StructRNA *)item)->ptr;
|
|
|
|
BLI_addtail(&result->list, link);
|
|
|
|
*/
|
|
|
|
ptr= &(((BPy_StructRNA *)list_item)->ptr);
|
|
|
|
CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("List item not a valid type\n");
|
|
|
|
}
|
|
|
|
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2009-10-29 10:03:34 +00:00
|
|
|
Py_DECREF(seq_fast);
|
2009-10-29 09:25:11 +00:00
|
|
|
|
2009-10-29 10:03:34 +00:00
|
|
|
done= 1;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(done==0) {
|
2009-11-10 19:57:04 +00:00
|
|
|
if (item) printf("Context '%s' not a valid type\n", member);
|
|
|
|
else printf("Context '%s' not found\n", member);
|
|
|
|
}
|
2010-01-31 21:52:26 +00:00
|
|
|
else {
|
2009-11-10 19:57:04 +00:00
|
|
|
printf("Context '%s' found\n", member);
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|