2011-02-23 10:52:22 +00:00
|
|
|
/*
|
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 *****
|
|
|
|
*/
|
2011-02-27 20:10:08 +00:00
|
|
|
|
|
|
|
/** \file blender/python/intern/bpy_interface.c
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
|
|
|
* This file deals with embedding the python interpreter within blender,
|
|
|
|
* starting and stopping python and exposing blender/python modules so they can
|
|
|
|
* be accesses from scripts.
|
2011-02-27 20:10:08 +00:00
|
|
|
*/
|
|
|
|
|
2009-08-26 06:15:43 +00:00
|
|
|
|
|
|
|
/* grr, python redefines */
|
|
|
|
#ifdef _POSIX_C_SOURCE
|
2011-09-20 12:22:19 +00:00
|
|
|
# undef _POSIX_C_SOURCE
|
2009-08-26 06:15:43 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-27 06:05:22 +00:00
|
|
|
#include <Python.h>
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2011-03-02 04:51:43 +00:00
|
|
|
#include "RNA_types.h"
|
|
|
|
|
2010-02-11 14:08:22 +00:00
|
|
|
#include "bpy.h"
|
2011-09-09 11:55:38 +00:00
|
|
|
#include "gpu.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"
|
2011-02-22 11:32:29 +00:00
|
|
|
#include "bpy_traceback.h"
|
2011-06-18 08:45:45 +00:00
|
|
|
#include "bpy_intern_string.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
|
|
|
|
2010-02-11 14:08:22 +00:00
|
|
|
#include "BLI_path_util.h"
|
2012-03-20 02:17:37 +00:00
|
|
|
#include "BLI_fileops.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"
|
2011-10-20 09:47:05 +00:00
|
|
|
#include "BLI_string_utf8.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2012-04-13 16:03:52 +00:00
|
|
|
#include "BLI_threads.h"
|
2009-04-11 02:18:24 +00:00
|
|
|
|
|
|
|
#include "BKE_context.h"
|
|
|
|
#include "BKE_text.h"
|
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
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
#include "CCL_api.h"
|
|
|
|
|
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
|
|
|
|
2011-02-14 04:15:25 +00:00
|
|
|
/* inittab initialization functions */
|
|
|
|
#include "../generic/bgl.h"
|
|
|
|
#include "../generic/blf_py_api.h"
|
2012-04-15 14:54:15 +00:00
|
|
|
#include "../generic/idprop_py_api.h"
|
2012-02-22 09:19:53 +00:00
|
|
|
#include "../bmesh/bmesh_py_api.h"
|
2011-07-15 04:01:47 +00:00
|
|
|
#include "../mathutils/mathutils.h"
|
2011-02-14 04:15:25 +00:00
|
|
|
|
2012-04-15 14:54:15 +00:00
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
/* for internal use, when starting and ending python scripts */
|
|
|
|
|
2012-03-09 00:41:09 +00:00
|
|
|
/* in case a python script triggers another python call, stop bpy_context_clear from invalidating */
|
2011-12-26 12:26:11 +00:00
|
|
|
static int py_call_level = 0;
|
|
|
|
BPy_StructRNA *bpy_context_module = NULL; /* for fast access */
|
2009-08-09 13:20:12 +00:00
|
|
|
|
2011-01-19 09:13:24 +00:00
|
|
|
// #define TIME_PY_RUN // simple python tests. prints on exit.
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
#ifdef TIME_PY_RUN
|
|
|
|
#include "PIL_time.h"
|
2012-03-26 20:41:54 +00:00
|
|
|
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 */
|
2009-08-09 13:20:12 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-20 13:29:42 +00:00
|
|
|
/* use for updating while a python script runs - in case of file load */
|
|
|
|
void bpy_context_update(bContext *C)
|
|
|
|
{
|
2012-04-13 16:03:52 +00:00
|
|
|
/* don't do this from a non-main (e.g. render) thread, it can cause a race
|
|
|
|
condition on C->data.recursion. ideal solution would be to disable
|
|
|
|
context entirely from non-main threads, but that's more complicated */
|
|
|
|
if(!BLI_thread_is_main())
|
|
|
|
return;
|
|
|
|
|
2011-08-20 13:29:42 +00:00
|
|
|
BPy_SetContext(C);
|
|
|
|
bpy_import_main_set(CTX_data_main(C));
|
2012-03-18 07:38:51 +00:00
|
|
|
BPY_modules_update(C); /* can give really bad results if this isn't here */
|
2011-08-20 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
|
|
|
|
{
|
|
|
|
py_call_level++;
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (gilstate)
|
2011-12-26 12:26:11 +00:00
|
|
|
*gilstate = PyGILState_Ensure();
|
2009-08-07 16:20:19 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (py_call_level == 1) {
|
2011-08-20 13:29:42 +00:00
|
|
|
bpy_context_update(C);
|
2009-11-05 11:17:09 +00:00
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#ifdef TIME_PY_RUN
|
2011-12-26 12:26:11 +00:00
|
|
|
if (bpy_timer_count == 0) {
|
2009-08-09 13:20:12 +00:00
|
|
|
/* record time from the beginning */
|
2011-12-26 12:26:11 +00:00
|
|
|
bpy_timer = PIL_check_seconds_timer();
|
|
|
|
bpy_timer_run = bpy_timer_run_tot = 0.0;
|
2009-08-09 13:20:12 +00:00
|
|
|
}
|
2011-12-26 12:26:11 +00:00
|
|
|
bpy_timer_run = PIL_check_seconds_timer();
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
bpy_timer_count++;
|
|
|
|
#endif
|
2009-08-07 16:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-13 23:25:08 +00:00
|
|
|
/* context should be used but not now because it causes some bugs */
|
|
|
|
void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate)
|
2009-08-07 16:20:19 +00:00
|
|
|
{
|
|
|
|
py_call_level--;
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (gilstate)
|
2009-08-07 16:20:19 +00:00
|
|
|
PyGILState_Release(*gilstate);
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (py_call_level < 0) {
|
2009-08-07 16:20:19 +00:00
|
|
|
fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
|
|
|
|
}
|
2011-12-26 12:26:11 +00:00
|
|
|
else if (py_call_level == 0) {
|
2011-11-26 15:18:30 +00:00
|
|
|
/* XXX - Calling classes currently wont store the context :\,
|
|
|
|
* cant set NULL because of this. but this is very flakey still. */
|
|
|
|
#if 0
|
|
|
|
BPy_SetContext(NULL);
|
|
|
|
bpy_import_main_set(NULL);
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_text_free_code(Text *text)
|
2009-03-04 13:26:33 +00:00
|
|
|
{
|
2011-10-13 01:29:08 +00:00
|
|
|
if (text->compiled) {
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_DECREF((PyObject *)text->compiled);
|
2011-12-26 12:26:11 +00:00
|
|
|
text->compiled = NULL;
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_modules_update(bContext *C)
|
2009-04-11 16:17:39 +00:00
|
|
|
{
|
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.
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *mod = PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
|
2011-03-19 11:12:48 +00:00
|
|
|
PyModule_AddObject(mod, "data", BPY_rna_module());
|
|
|
|
PyModule_AddObject(mod, "types", BPY_rna_types()); // atm this does not need updating
|
2009-08-15 09:53:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* refreshes the main struct */
|
|
|
|
BPY_update_rna_module();
|
2011-12-26 12:26:11 +00:00
|
|
|
bpy_context_module->ptr.data = (void *)C;
|
2009-04-11 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_context_set(bContext *C)
|
2009-11-13 09:28:05 +00:00
|
|
|
{
|
|
|
|
BPy_SetContext(C);
|
|
|
|
}
|
|
|
|
|
2011-02-14 04:15:25 +00:00
|
|
|
/* defined in AUD_C-API.cpp */
|
2010-10-29 22:59:39 +00:00
|
|
|
extern PyObject *AUD_initPython(void);
|
2012-01-09 16:58:01 +00:00
|
|
|
|
|
|
|
#ifdef WITH_CYCLES
|
|
|
|
/* defined in cycles module */
|
|
|
|
static PyObject *CCL_initPython(void)
|
|
|
|
{
|
2012-03-26 20:41:54 +00:00
|
|
|
return (PyObject *)CCL_python_module_init();
|
2012-01-09 16:58:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-10-29 22:59:39 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
static struct _inittab bpy_internal_modules[] = {
|
2011-07-15 04:01:47 +00:00
|
|
|
{(char *)"mathutils", PyInit_mathutils},
|
|
|
|
// {(char *)"mathutils.geometry", PyInit_mathutils_geometry},
|
2011-11-29 10:28:52 +00:00
|
|
|
// {(char *)"mathutils.noise", PyInit_mathutils_noise},
|
2010-12-03 17:05:21 +00:00
|
|
|
{(char *)"bgl", BPyInit_bgl},
|
|
|
|
{(char *)"blf", BPyInit_blf},
|
2012-02-22 10:41:07 +00:00
|
|
|
{(char *)"bmesh", BPyInit_bmesh},
|
2012-03-26 20:41:54 +00:00
|
|
|
// {(char *)"bmesh.types", BPyInit_bmesh_types},
|
|
|
|
// {(char *)"bmesh.utils", BPyInit_bmesh_utils},
|
2011-06-23 09:27:56 +00:00
|
|
|
#ifdef WITH_AUDASPACE
|
2010-12-03 17:05:21 +00:00
|
|
|
{(char *)"aud", AUD_initPython},
|
2011-08-16 16:15:34 +00:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_CYCLES
|
2012-01-09 16:58:01 +00:00
|
|
|
{(char *)"_cycles", CCL_initPython},
|
2011-06-23 09:27:56 +00:00
|
|
|
#endif
|
2011-09-09 11:55:38 +00:00
|
|
|
{(char *)"gpu", GPU_initPython},
|
2012-04-15 14:54:15 +00:00
|
|
|
{(char *)"idprop", BPyInit_idprop},
|
2010-10-29 22:59:39 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
/* call BPY_context_set first */
|
2011-02-19 12:05:20 +00:00
|
|
|
void BPY_python_start(int argc, const char **argv)
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2011-02-20 23:39:29 +00:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2011-12-26 12:26:11 +00:00
|
|
|
PyThreadState *py_tstate = NULL;
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2010-08-01 15:15:57 +00:00
|
|
|
/* not essential but nice to set our name */
|
2011-11-26 04:07:38 +00:00
|
|
|
static wchar_t program_path_wchar[FILE_MAX]; /* python holds a reference */
|
2011-10-21 17:37:38 +00:00
|
|
|
BLI_strncpy_wchar_from_utf8(program_path_wchar, BLI_program_path(), sizeof(program_path_wchar) / sizeof(wchar_t));
|
|
|
|
Py_SetProgramName(program_path_wchar);
|
2009-07-03 04:38:55 +00:00
|
|
|
|
2011-03-25 01:55:00 +00:00
|
|
|
/* must run before python initializes */
|
2010-10-29 22:59:39 +00:00
|
|
|
PyImport_ExtendInittab(bpy_internal_modules);
|
|
|
|
|
2011-04-18 08:27:50 +00:00
|
|
|
/* allow to use our own included python */
|
Windows installer and Path changes, fixing various issues:
* Windows installer not working for non-admin users and multiple users
* Addon scripts not installing next to user configuration
* Portable install not being taken into account in all places
The main problem was the windows installer was installing system scripts in
AppData next to the user configuration directory, which is not shared between
users. Now these are installed in ProgramFiles, and only addon scripts added
by the users go to AppData.
On all platforms, addon scripts were sometimes getting installed between
system scripts, because the scripts folder in the executable directory was
given precedence over the user configuration folder, that is no longer done
now. So addons now behave like user configuration, they are preserved even
if you download a newer build of the same blender version.
If you have an installation of 2.57 on windows, the addon install location
will not change until we do the version bump to 2.58, to avoid conflicts with
the existing the installed 2.57 version.
The old behavior of giving precedence to the local folder was done to support
portable install, where all configuration is written to the local folder. This
is now implemented differently: if and only if a "config" folder exists in the
local folder, portable install will be assumed, and files will only be written
to that local folder.
2011-05-27 09:57:53 +00:00
|
|
|
PyC_SetHomePath(BLI_get_folder(BLENDER_SYSTEM_PYTHON, NULL));
|
2010-07-02 20:09:42 +00:00
|
|
|
|
2011-10-23 07:51:21 +00:00
|
|
|
/* without this the sys.stdout may be set to 'ascii'
|
|
|
|
* (it is on my system at least), where printing unicode values will raise
|
|
|
|
* an error, this is highly annoying, another stumbling block for devs,
|
|
|
|
* so use a more relaxed error handler and enforce utf-8 since the rest of
|
|
|
|
* blender is utf-8 too - campbell */
|
|
|
|
BLI_setenv("PYTHONIOENCODING", "utf-8:surrogateescape");
|
|
|
|
|
2011-10-22 10:49:35 +00:00
|
|
|
/* Python 3.2 now looks for '2.xx/python/include/python3.2d/pyconfig.h' to
|
|
|
|
* parse from the 'sysconfig' module which is used by 'site',
|
|
|
|
* so for now disable site. alternatively we could copy the file. */
|
2011-12-26 12:26:11 +00:00
|
|
|
Py_NoSiteFlag = 1;
|
2011-02-08 06:22:06 +00:00
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_Initialize();
|
2011-06-18 08:45:45 +00:00
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
// PySys_SetArgv(argc, argv); // broken in py3, not a huge deal
|
2012-03-16 21:39:56 +00:00
|
|
|
/* sigh, why do python guys not have a (char **) version anymore? */
|
2009-07-08 09:23:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *py_argv = PyList_New(argc);
|
|
|
|
for (i = 0; i < argc; i++) {
|
2011-10-22 10:49:35 +00:00
|
|
|
/* should fix bug #20021 - utf path name problems, by replacing
|
|
|
|
* PyUnicode_FromString, with this one */
|
|
|
|
PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i]));
|
|
|
|
}
|
2010-10-04 01:18:47 +00:00
|
|
|
|
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();
|
2011-02-20 23:39:29 +00:00
|
|
|
#else
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2011-03-25 01:55:00 +00:00
|
|
|
|
|
|
|
/* must run before python initializes */
|
2011-02-20 23:39:29 +00:00
|
|
|
PyImport_ExtendInittab(bpy_internal_modules);
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 19:15:46 +00:00
|
|
|
bpy_intern_string_init();
|
|
|
|
|
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
|
|
|
|
2011-03-29 16:12:25 +00:00
|
|
|
bpy_import_init(PyEval_GetBuiltins());
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2009-08-15 09:53:38 +00:00
|
|
|
pyrna_alloc_types();
|
|
|
|
|
2012-01-17 15:19:11 +00:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
|
|
|
/* py module runs atexit when bpy is freed */
|
2011-09-20 15:17:24 +00:00
|
|
|
BPY_atexit_register(); /* this can init any time */
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
py_tstate = PyGILState_GetThisThreadState();
|
2008-11-29 13:36:08 +00:00
|
|
|
PyEval_ReleaseThread(py_tstate);
|
2011-02-20 23:39:29 +00:00
|
|
|
#endif
|
2008-11-29 13:36:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_python_end(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 */
|
2011-06-18 08:45:45 +00:00
|
|
|
|
|
|
|
bpy_intern_string_exit();
|
|
|
|
|
2012-01-17 15:19:11 +00:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2011-09-20 15:17:24 +00:00
|
|
|
BPY_atexit_unregister(); /* without this we get recursive calls to WM_exit */
|
2012-01-17 15:19:11 +00:00
|
|
|
#endif
|
2011-09-20 15:17:24 +00:00
|
|
|
|
2011-03-19 11:12:48 +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
|
2011-12-26 12:26:11 +00:00
|
|
|
bpy_timer = PIL_check_seconds_timer() - bpy_timer;
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
printf("*bpy stats* - ");
|
|
|
|
printf("tot exec: %d, ", bpy_timer_count);
|
|
|
|
printf("tot run: %.4fsec, ", bpy_timer_run_tot);
|
2011-12-26 12:26:11 +00:00
|
|
|
if (bpy_timer_count > 0)
|
2012-03-26 20:41:54 +00:00
|
|
|
printf("average run: %.6fsec, ", (bpy_timer_run_tot / bpy_timer_count));
|
2009-08-09 13:20:12 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (bpy_timer > 0.0)
|
2012-03-26 20:41:54 +00:00
|
|
|
printf("tot usage %.4f%%", (bpy_timer_run_tot / bpy_timer) * 100.0);
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-22 11:32:29 +00:00
|
|
|
static void python_script_error_jump_text(struct Text *text)
|
|
|
|
{
|
|
|
|
int lineno;
|
|
|
|
int offset;
|
2011-12-26 12:26:11 +00:00
|
|
|
python_script_error_jump(text->id.name + 2, &lineno, &offset);
|
2011-10-13 01:29:08 +00:00
|
|
|
if (lineno != -1) {
|
2011-02-22 11:32:29 +00:00
|
|
|
/* select the line with the error */
|
|
|
|
txt_move_to(text, lineno - 1, INT_MAX, FALSE);
|
|
|
|
txt_move_to(text, lineno - 1, offset, TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-01 09:02:49 +00:00
|
|
|
/* super annoying, undo _PyModule_Clear(), bug [#23871] */
|
|
|
|
#define PYMODULE_CLEAR_WORKAROUND
|
|
|
|
|
|
|
|
#ifdef PYMODULE_CLEAR_WORKAROUND
|
|
|
|
/* bad!, we should never do this, but currently only safe way I could find to keep namespace.
|
|
|
|
* from being cleared. - campbell */
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *md_dict;
|
|
|
|
/* ommit other values, we only want the dict. */
|
|
|
|
} PyModuleObject;
|
|
|
|
#endif
|
|
|
|
|
2011-11-26 15:18:30 +00:00
|
|
|
static int python_script_exec(bContext *C, const char *fn, struct Text *text,
|
|
|
|
struct ReportList *reports, const short do_jump)
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *main_mod = NULL;
|
|
|
|
PyObject *py_dict = NULL, *py_result = NULL;
|
2008-11-29 13:36:08 +00:00
|
|
|
PyGILState_STATE gilstate;
|
2011-01-05 02:08:54 +00:00
|
|
|
|
2011-01-09 15:12:08 +00:00
|
|
|
BLI_assert(fn || text);
|
2011-01-05 02:08:54 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (fn == NULL && text == NULL) {
|
2009-03-04 13:26:33 +00:00
|
|
|
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
|
|
|
|
2011-02-01 12:37:53 +00:00
|
|
|
PyC_MainModule_Backup(&main_mod);
|
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
if (text) {
|
2010-06-02 14:40:58 +00:00
|
|
|
char fn_dummy[FILE_MAXDIR];
|
2011-03-07 11:53:40 +00:00
|
|
|
bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text);
|
2011-01-05 02:08:54 +00:00
|
|
|
|
2012-03-26 20:41:54 +00:00
|
|
|
if (text->compiled == NULL) { /* if it wasn't already compiled, do it now */
|
2011-12-26 12:26:11 +00:00
|
|
|
char *buf = txt_to_buf(text);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
text->compiled = Py_CompileString(buf, fn_dummy, Py_file_input);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
MEM_freeN(buf);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
if (do_jump) {
|
2011-02-26 15:30:38 +00:00
|
|
|
python_script_error_jump_text(text);
|
|
|
|
}
|
2011-01-05 02:08:54 +00:00
|
|
|
BPY_text_free_code(text);
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-18 15:30:03 +00:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (text->compiled) {
|
2011-12-26 12:26:11 +00:00
|
|
|
py_dict = PyC_DefaultNameSpace(fn_dummy);
|
|
|
|
py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict);
|
2010-09-18 15:30:03 +00:00
|
|
|
}
|
2011-01-05 02:08:54 +00:00
|
|
|
|
2010-06-02 14:40:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-20 02:17:37 +00:00
|
|
|
FILE *fp = BLI_fopen(fn, "r");
|
2010-06-02 19:37:53 +00:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (fp) {
|
2011-12-26 12:26:11 +00:00
|
|
|
py_dict = PyC_DefaultNameSpace(fn);
|
2010-09-18 15:30:03 +00:00
|
|
|
|
2009-11-11 13:27:54 +00:00
|
|
|
#ifdef _WIN32
|
2011-01-05 02:08:54 +00:00
|
|
|
/* Previously we used PyRun_File to run directly the code on a FILE
|
2009-11-11 13:27:54 +00:00
|
|
|
* object, but as written in the Python/C API Ref Manual, chapter 2,
|
2011-01-05 02:08:54 +00:00
|
|
|
* 'FILE structs for different C libraries can be different and
|
2009-11-11 13:27:54 +00:00
|
|
|
* incompatible'.
|
|
|
|
* So now we load the script file data to a buffer */
|
2010-09-18 19:38:27 +00:00
|
|
|
{
|
|
|
|
char *pystring;
|
2009-11-11 13:27:54 +00:00
|
|
|
|
2010-09-18 19:38:27 +00:00
|
|
|
fclose(fp);
|
2009-11-11 13:27:54 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
pystring = MEM_mallocN(strlen(fn) + 32, "pystring");
|
|
|
|
pystring[0] = '\0';
|
2010-09-18 19:38:27 +00:00
|
|
|
sprintf(pystring, "exec(open(r'%s').read())", fn);
|
2011-12-26 12:26:11 +00:00
|
|
|
py_result = PyRun_String(pystring, Py_file_input, py_dict, py_dict);
|
2010-09-18 19:38:27 +00:00
|
|
|
MEM_freeN(pystring);
|
|
|
|
}
|
2009-11-11 13:27:54 +00:00
|
|
|
#else
|
2011-12-26 12:26:11 +00:00
|
|
|
py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
|
2009-07-26 18:18:14 +00:00
|
|
|
fclose(fp);
|
2009-11-11 13:27:54 +00:00
|
|
|
#endif
|
2009-07-26 18:18:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-04-30 13:58:31 +00:00
|
|
|
PyErr_Format(PyExc_IOError,
|
|
|
|
"Python file \"%s\" could not be opened: %s",
|
|
|
|
fn, strerror(errno));
|
2011-12-26 12:26:11 +00:00
|
|
|
py_result = NULL;
|
2009-07-26 18:18:14 +00:00
|
|
|
}
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
2011-01-05 02:08:54 +00:00
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
if (!py_result) {
|
2011-10-13 01:29:08 +00:00
|
|
|
if (text) {
|
|
|
|
if (do_jump) {
|
2011-02-26 15:30:38 +00:00
|
|
|
python_script_error_jump_text(text);
|
|
|
|
}
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|
2009-06-14 12:53:47 +00:00
|
|
|
BPy_errors_to_report(reports);
|
2011-03-19 11:12:48 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(py_result);
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
2010-11-24 10:23:23 +00:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (py_dict) {
|
2010-11-24 10:23:23 +00:00
|
|
|
#ifdef PYMODULE_CLEAR_WORKAROUND
|
2011-12-26 12:26:11 +00:00
|
|
|
PyModuleObject *mmod = (PyModuleObject *)PyDict_GetItemString(PyThreadState_GET()->interp->modules, "__main__");
|
|
|
|
PyObject *dict_back = mmod->md_dict;
|
2011-02-01 09:02:49 +00:00
|
|
|
/* freeing the module will clear the namespace,
|
|
|
|
* gives problems running classes defined in this namespace being used later. */
|
2011-12-26 12:26:11 +00:00
|
|
|
mmod->md_dict = NULL;
|
2011-02-01 09:02:49 +00:00
|
|
|
Py_DECREF(dict_back);
|
2010-11-24 10:23:23 +00:00
|
|
|
#endif
|
2011-02-01 09:02:49 +00:00
|
|
|
|
|
|
|
#undef PYMODULE_CLEAR_WORKAROUND
|
2010-11-24 10:23:23 +00:00
|
|
|
}
|
2011-01-05 02:08:54 +00:00
|
|
|
|
2011-02-01 12:37:53 +00:00
|
|
|
PyC_MainModule_Restore(main_mod);
|
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
return (py_result != NULL);
|
2008-11-29 13:36:08 +00:00
|
|
|
}
|
2009-02-28 13:27:45 +00:00
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
/* Can run a file or text block */
|
|
|
|
int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports)
|
2009-02-28 13:27:45 +00:00
|
|
|
{
|
2011-02-26 15:30:38 +00:00
|
|
|
return python_script_exec(C, filepath, NULL, reports, FALSE);
|
2009-02-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-26 15:30:38 +00:00
|
|
|
int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports, const short do_jump)
|
2009-02-28 13:27:45 +00:00
|
|
|
{
|
2011-02-26 15:30:38 +00:00
|
|
|
return python_script_exec(C, NULL, text, reports, do_jump);
|
2009-02-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 12:43:07 +00:00
|
|
|
void BPY_DECREF(void *pyob_ptr)
|
|
|
|
{
|
2011-12-26 12:26:11 +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
|
|
|
}
|
|
|
|
|
2011-04-17 12:47:20 +00:00
|
|
|
/* return -1 on error, else 0 */
|
|
|
|
int BPY_button_exec(bContext *C, const char *expr, double *value, const short verbose)
|
2009-08-10 11:58:53 +00:00
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2010-08-01 13:57:04 +00:00
|
|
|
PyObject *py_dict, *mod, *retval;
|
2011-12-26 12:26:11 +00:00
|
|
|
int error_ret = 0;
|
|
|
|
PyObject *main_mod = NULL;
|
2009-08-10 11:58:53 +00:00
|
|
|
|
2010-02-27 22:53:37 +00:00
|
|
|
if (!value || !expr) return -1;
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (expr[0] == '\0') {
|
|
|
|
*value = 0.0;
|
2010-02-27 22:53:37 +00:00
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
2011-02-01 12:37:53 +00:00
|
|
|
|
|
|
|
PyC_MainModule_Backup(&main_mod);
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
py_dict = PyC_DefaultNameSpace("<blender button>");
|
2009-10-22 23:23:09 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
mod = PyImport_ImportModule("math");
|
2009-10-22 23:23:09 +00:00
|
|
|
if (mod) {
|
2012-03-18 07:38:51 +00:00
|
|
|
PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't 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
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
2009-08-10 11:58:53 +00:00
|
|
|
|
|
|
|
if (retval == NULL) {
|
2011-12-26 12:26:11 +00:00
|
|
|
error_ret = -1;
|
2009-08-10 11:58:53 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (PyTuple_Check(retval)) {
|
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
|
|
|
/* Users my have typed in 10km, 2m
|
|
|
|
* add up all values */
|
|
|
|
int i;
|
2011-12-26 12:26:11 +00:00
|
|
|
val = 0.0;
|
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
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
for (i = 0; i < PyTuple_GET_SIZE(retval); i++) {
|
|
|
|
val += PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-12-26 12:26:11 +00:00
|
|
|
val = PyFloat_AsDouble(retval);
|
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
|
|
|
}
|
2009-08-10 11:58:53 +00:00
|
|
|
Py_DECREF(retval);
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (val == -1 && PyErr_Occurred()) {
|
|
|
|
error_ret = -1;
|
2009-08-10 11:58:53 +00:00
|
|
|
}
|
2010-03-14 21:04:02 +00:00
|
|
|
else if (!finite(val)) {
|
2011-12-26 12:26:11 +00:00
|
|
|
*value = 0.0;
|
2010-03-14 21:04:02 +00:00
|
|
|
}
|
2009-08-10 11:58:53 +00:00
|
|
|
else {
|
2011-12-26 12:26:11 +00:00
|
|
|
*value = val;
|
2009-08-10 11:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (error_ret) {
|
|
|
|
if (verbose) {
|
2011-04-17 12:47:20 +00:00
|
|
|
BPy_errors_to_report(CTX_wm_reports(C));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2009-08-10 11:58:53 +00:00
|
|
|
}
|
2010-07-28 23:24:17 +00:00
|
|
|
|
2011-02-01 12:37:53 +00:00
|
|
|
PyC_MainModule_Backup(&main_mod);
|
2010-08-01 13:57:04 +00:00
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
int BPY_string_exec(bContext *C, const char *expr)
|
2010-05-30 14:05:58 +00:00
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *main_mod = NULL;
|
2010-08-01 13:57:04 +00:00
|
|
|
PyObject *py_dict, *retval;
|
2011-12-26 12:26:11 +00:00
|
|
|
int error_ret = 0;
|
2011-04-13 00:51:48 +00:00
|
|
|
Main *bmain_back; /* XXX, quick fix for release (Copy Settings crash), needs further investigation */
|
2010-05-30 14:05:58 +00:00
|
|
|
|
|
|
|
if (!expr) return -1;
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (expr[0] == '\0') {
|
2010-05-30 14:05:58 +00:00
|
|
|
return error_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
2011-02-01 12:37:53 +00:00
|
|
|
PyC_MainModule_Backup(&main_mod);
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
py_dict = PyC_DefaultNameSpace("<blender string>");
|
2010-05-30 14:05:58 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
bmain_back = bpy_import_main_get();
|
2011-04-12 17:18:02 +00:00
|
|
|
bpy_import_main_set(CTX_data_main(C));
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
2010-05-30 14:05:58 +00:00
|
|
|
|
2011-04-12 17:18:02 +00:00
|
|
|
bpy_import_main_set(bmain_back);
|
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
if (retval == NULL) {
|
2011-12-26 12:26:11 +00:00
|
|
|
error_ret = -1;
|
2010-05-30 14:05:58 +00:00
|
|
|
|
|
|
|
BPy_errors_to_report(CTX_wm_reports(C));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
}
|
|
|
|
|
2011-02-01 12:37:53 +00:00
|
|
|
PyC_MainModule_Restore(main_mod);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_modules_load_user(bContext *C)
|
2009-11-20 15:01:09 +00:00
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2011-12-26 12:26:11 +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 */
|
2011-12-26 12:26:11 +00:00
|
|
|
if (bmain == NULL)
|
2010-01-10 20:01:13 +00:00
|
|
|
return;
|
|
|
|
|
2011-08-20 13:29:42 +00:00
|
|
|
/* update pointers since this can run from a nested script
|
|
|
|
* on file load */
|
2011-10-13 01:29:08 +00:00
|
|
|
if (py_call_level) {
|
2011-08-20 13:29:42 +00:00
|
|
|
bpy_context_update(C);
|
|
|
|
}
|
|
|
|
|
2009-11-20 15:01:09 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
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")) {
|
2011-10-13 01:29:08 +00:00
|
|
|
if (!(G.f & G_SCRIPT_AUTOEXEC)) {
|
2011-12-26 12:26:11 +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 {
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *module = bpy_text_import(text);
|
2010-02-27 01:27:22 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (module == NULL) {
|
2010-02-27 01:27:22 +00:00
|
|
|
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
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
|
2009-10-29 09:25:11 +00:00
|
|
|
{
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *pyctx = (PyObject *)CTX_py_dict_get(C);
|
|
|
|
PyObject *item = PyDict_GetItemString(pyctx, member);
|
|
|
|
PointerRNA *ptr = NULL;
|
|
|
|
int done = 0;
|
2009-10-29 09:25:11 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (item == NULL) {
|
2009-10-29 09:25:11 +00:00
|
|
|
/* pass */
|
|
|
|
}
|
2011-12-26 12:26:11 +00:00
|
|
|
else if (item == Py_None) {
|
2009-10-29 09:25:11 +00:00
|
|
|
/* pass */
|
|
|
|
}
|
2011-10-13 01:29:08 +00:00
|
|
|
else if (BPy_StructRNA_Check(item)) {
|
2011-12-26 12:26:11 +00:00
|
|
|
ptr = &(((BPy_StructRNA *)item)->ptr);
|
2009-10-29 09:25:11 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
//result->ptr = ((BPy_StructRNA *)item)->ptr;
|
2009-10-29 09:25:11 +00:00
|
|
|
CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
|
2011-12-26 12:26:11 +00:00
|
|
|
done = 1;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2009-10-29 10:03:34 +00:00
|
|
|
else if (PySequence_Check(item)) {
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *seq_fast = PySequence_Fast(item, "bpy_context_get sequence conversion");
|
|
|
|
if (seq_fast == NULL) {
|
2009-10-29 10:03:34 +00:00
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
else {
|
2011-12-26 12:26:11 +00:00
|
|
|
int len = PySequence_Fast_GET_SIZE(seq_fast);
|
2009-10-29 10:03:34 +00:00
|
|
|
int i;
|
2011-12-26 12:26:11 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
PyObject *list_item = PySequence_Fast_GET_ITEM(seq_fast, i);
|
2009-10-29 10:03:34 +00:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (BPy_StructRNA_Check(list_item)) {
|
2012-03-03 20:36:09 +00:00
|
|
|
#if 0
|
2011-12-26 12:26:11 +00:00
|
|
|
CollectionPointerLink *link = MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
|
|
|
|
link->ptr = ((BPy_StructRNA *)item)->ptr;
|
2009-10-29 10:03:34 +00:00
|
|
|
BLI_addtail(&result->list, link);
|
2012-03-03 20:36:09 +00:00
|
|
|
#endif
|
2011-12-26 12:26:11 +00:00
|
|
|
ptr = &(((BPy_StructRNA *)list_item)->ptr);
|
2009-10-29 10:03:34 +00:00
|
|
|
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
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
done = 1;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (done == 0) {
|
2012-03-26 20:41:54 +00:00
|
|
|
if (item) printf("PyContext '%s' not a valid type\n", member);
|
|
|
|
else printf("PyContext '%s' not found\n", member);
|
2009-11-10 19:57:04 +00:00
|
|
|
}
|
2010-01-31 21:52:26 +00:00
|
|
|
else {
|
2012-03-31 00:59:17 +00:00
|
|
|
if (G.debug & G_DEBUG_PYTHON) {
|
2011-04-11 13:56:58 +00:00
|
|
|
printf("PyContext '%s' found\n", member);
|
|
|
|
}
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2011-02-20 23:39:29 +00:00
|
|
|
#ifdef WITH_PYTHON_MODULE
|
2011-10-22 15:35:49 +00:00
|
|
|
#include "BLI_fileops.h"
|
2012-03-18 07:38:51 +00:00
|
|
|
/* TODO, reloading the module isn't functional at the moment. */
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2011-07-10 18:54:02 +00:00
|
|
|
static void bpy_module_free(void *mod);
|
|
|
|
extern int main_python_enter(int argc, const char **argv);
|
|
|
|
extern void main_python_exit(void);
|
2011-12-26 12:26:11 +00:00
|
|
|
static struct PyModuleDef bpy_proxy_def = {
|
2011-02-20 23:39:29 +00:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"bpy", /* m_name */
|
|
|
|
NULL, /* m_doc */
|
|
|
|
0, /* m_size */
|
|
|
|
NULL, /* m_methods */
|
|
|
|
NULL, /* m_reload */
|
|
|
|
NULL, /* m_traverse */
|
|
|
|
NULL, /* m_clear */
|
2011-07-10 18:54:02 +00:00
|
|
|
bpy_module_free, /* m_free */
|
|
|
|
};
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2011-02-22 14:19:09 +00:00
|
|
|
typedef struct {
|
2011-04-21 13:11:51 +00:00
|
|
|
PyObject_HEAD
|
|
|
|
/* Type-specific fields go here. */
|
2011-02-22 14:19:09 +00:00
|
|
|
PyObject *mod;
|
|
|
|
} dealloc_obj;
|
|
|
|
|
|
|
|
/* call once __file__ is set */
|
|
|
|
void bpy_module_delay_init(PyObject *bpy_proxy)
|
2011-02-20 23:39:29 +00:00
|
|
|
{
|
2011-12-26 12:26:11 +00:00
|
|
|
const int argc = 1;
|
2011-02-22 14:19:09 +00:00
|
|
|
const char *argv[2];
|
2011-11-26 15:18:30 +00:00
|
|
|
|
|
|
|
/* updating the module dict below will loose the reference to __file__ */
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *filename_obj = PyModule_GetFilenameObject(bpy_proxy);
|
2011-11-26 15:18:30 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
const char *filename_rel = _PyUnicode_AsString(filename_obj); /* can be relative */
|
2011-02-22 14:19:09 +00:00
|
|
|
char filename_abs[1024];
|
|
|
|
|
|
|
|
BLI_strncpy(filename_abs, filename_rel, sizeof(filename_abs));
|
|
|
|
BLI_path_cwd(filename_abs);
|
2011-03-09 04:58:44 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
argv[0] = filename_abs;
|
|
|
|
argv[1] = NULL;
|
2011-02-22 14:19:09 +00:00
|
|
|
|
|
|
|
// printf("module found %s\n", argv[0]);
|
2011-02-21 13:13:08 +00:00
|
|
|
|
2011-07-10 18:54:02 +00:00
|
|
|
main_python_enter(argc, argv);
|
2011-02-20 23:39:29 +00:00
|
|
|
|
|
|
|
/* initialized in BPy_init_modules() */
|
2011-02-22 14:19:09 +00:00
|
|
|
PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dealloc_obj_dealloc(PyObject *self);
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
static PyTypeObject dealloc_obj_Type = {{{0}}};
|
2011-02-22 14:19:09 +00:00
|
|
|
|
|
|
|
/* use our own dealloc so we can free a property if we use one */
|
|
|
|
static void dealloc_obj_dealloc(PyObject *self)
|
|
|
|
{
|
|
|
|
bpy_module_delay_init(((dealloc_obj *)self)->mod);
|
|
|
|
|
|
|
|
/* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
|
|
|
|
dealloc_obj_Type.tp_free(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_bpy(void)
|
|
|
|
{
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *bpy_proxy = PyModule_Create(&bpy_proxy_def);
|
2011-02-22 14:19:09 +00:00
|
|
|
|
|
|
|
/* Problem:
|
|
|
|
* 1) this init function is expected to have a private member defined - 'md_def'
|
|
|
|
* but this is only set for C defined modules (not py packages)
|
|
|
|
* so we cant return 'bpy_package_py' as is.
|
|
|
|
*
|
|
|
|
* 2) there is a 'bpy' C module for python to load which is basically all of blender,
|
|
|
|
* and there is scripts/bpy/__init__.py,
|
|
|
|
* we may end up having to rename this module so there is no naming conflict here eg:
|
|
|
|
* 'from blender import bpy'
|
|
|
|
*
|
2012-03-18 07:38:51 +00:00
|
|
|
* 3) we don't know the filename at this point, workaround by assigning a dummy value
|
2011-02-22 14:19:09 +00:00
|
|
|
* which calls back when its freed so the real loading can take place.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* assign an object which is freed after __file__ is assigned */
|
|
|
|
dealloc_obj *dob;
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2011-02-22 14:19:09 +00:00
|
|
|
/* assign dummy type */
|
2011-12-26 12:26:11 +00:00
|
|
|
dealloc_obj_Type.tp_name = "dealloc_obj";
|
|
|
|
dealloc_obj_Type.tp_basicsize = sizeof(dealloc_obj);
|
|
|
|
dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc;
|
|
|
|
dealloc_obj_Type.tp_flags = Py_TPFLAGS_DEFAULT;
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (PyType_Ready(&dealloc_obj_Type) < 0)
|
2011-02-22 14:19:09 +00:00
|
|
|
return NULL;
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
dob = (dealloc_obj *) dealloc_obj_Type.tp_alloc(&dealloc_obj_Type, 0);
|
|
|
|
dob->mod = bpy_proxy; /* borrow */
|
2011-02-22 14:19:09 +00:00
|
|
|
PyModule_AddObject(bpy_proxy, "__file__", (PyObject *)dob); /* borrow */
|
|
|
|
|
|
|
|
return bpy_proxy;
|
2011-02-20 23:39:29 +00:00
|
|
|
}
|
2011-02-22 14:19:09 +00:00
|
|
|
|
2011-07-10 18:54:02 +00:00
|
|
|
static void bpy_module_free(void *UNUSED(mod))
|
|
|
|
{
|
|
|
|
main_python_exit();
|
|
|
|
}
|
|
|
|
|
2011-02-20 23:39:29 +00:00
|
|
|
#endif
|