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
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
|
2018-04-02 11:05:15 +02:00
|
|
|
#include "CLG_log.h"
|
|
|
|
|
2013-03-04 18:36:37 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
#include "BLI_path_util.h"
|
|
|
|
#include "BLI_fileops.h"
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
#include "BLI_string.h"
|
|
|
|
#include "BLI_string_utf8.h"
|
|
|
|
#include "BLI_threads.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"
|
2013-01-04 03:47:37 +00:00
|
|
|
#include "bpy_path.h"
|
2017-11-29 21:11:29 +11:00
|
|
|
#include "bpy_capi_utils.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
|
|
|
|
2015-04-01 09:34:01 +02:00
|
|
|
#include "bpy_app_translations.h"
|
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
#include "DNA_text_types.h"
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2014-11-23 14:37:13 +01:00
|
|
|
#include "BKE_appdir.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"
|
|
|
|
|
2012-10-15 09:11:17 +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
|
|
|
|
2018-04-02 11:05:15 +02:00
|
|
|
/* Logging types to use anywhere in the Python modules. */
|
|
|
|
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_CONTEXT, "bpy.context");
|
|
|
|
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_RNA, "bpy.rna");
|
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 */
|
2012-05-08 22:07:06 +00:00
|
|
|
void BPY_context_update(bContext *C)
|
2011-08-20 13:29:42 +00:00
|
|
|
{
|
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
|
2012-04-22 11:54:53 +00:00
|
|
|
* condition on C->data.recursion. ideal solution would be to disable
|
|
|
|
* context entirely from non-main threads, but that's more complicated */
|
2012-04-21 12:51:47 +00:00
|
|
|
if (!BLI_thread_is_main())
|
2012-04-13 16:03:52 +00:00
|
|
|
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) {
|
2012-05-08 22:07:06 +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) {
|
2012-09-10 23:32:46 +00:00
|
|
|
PyGILState_STATE gilstate;
|
2013-09-18 23:21:24 +00:00
|
|
|
bool use_gil = !PyC_IsInterpreterActive();
|
2012-09-10 23:32:46 +00:00
|
|
|
|
|
|
|
if (use_gil)
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_DECREF((PyObject *)text->compiled);
|
2011-12-26 12:26:11 +00:00
|
|
|
text->compiled = NULL;
|
2012-09-10 23:32:46 +00:00
|
|
|
|
|
|
|
if (use_gil)
|
|
|
|
PyGILState_Release(gilstate);
|
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
|
|
|
{
|
2012-10-15 09:11:17 +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());
|
2012-10-15 09:11:17 +00:00
|
|
|
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();
|
2012-05-22 22:03:41 +00:00
|
|
|
if (bpy_context_module)
|
2012-05-08 22:07:06 +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[] = {
|
2014-07-01 13:39:36 +10:00
|
|
|
{"mathutils", PyInit_mathutils},
|
|
|
|
#if 0
|
|
|
|
{"mathutils.geometry", PyInit_mathutils_geometry},
|
|
|
|
{"mathutils.noise", PyInit_mathutils_noise},
|
|
|
|
{"mathutils.kdtree", PyInit_mathutils_kdtree},
|
|
|
|
#endif
|
|
|
|
{"_bpy_path", BPyInit__bpy_path},
|
|
|
|
{"bgl", BPyInit_bgl},
|
|
|
|
{"blf", BPyInit_blf},
|
|
|
|
{"bmesh", BPyInit_bmesh},
|
|
|
|
#if 0
|
|
|
|
{"bmesh.types", BPyInit_bmesh_types},
|
|
|
|
{"bmesh.utils", BPyInit_bmesh_utils},
|
|
|
|
{"bmesh.utils", BPyInit_bmesh_geometry},
|
|
|
|
#endif
|
2011-06-23 09:27:56 +00:00
|
|
|
#ifdef WITH_AUDASPACE
|
2014-07-01 13:39:36 +10:00
|
|
|
{"aud", AUD_initPython},
|
2011-08-16 16:15:34 +00:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_CYCLES
|
2014-07-01 13:39:36 +10:00
|
|
|
{"_cycles", CCL_initPython},
|
2011-06-23 09:27:56 +00:00
|
|
|
#endif
|
2014-07-01 13:39:36 +10:00
|
|
|
{"gpu", GPU_initPython},
|
|
|
|
{"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;
|
2014-11-23 15:54:29 +01:00
|
|
|
const char *py_path_bundle = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON, 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 */
|
2014-11-23 15:54:29 +01:00
|
|
|
BLI_strncpy_wchar_from_utf8(program_path_wchar, BKE_appdir_program_path(), ARRAY_SIZE(program_path_wchar));
|
2011-10-21 17:37:38 +00:00
|
|
|
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 */
|
2012-05-18 10:19:49 +00:00
|
|
|
PyC_SetHomePath(py_path_bundle);
|
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 */
|
2014-05-01 16:22:30 +02:00
|
|
|
Py_SetStandardStreamEncoding("utf-8", "surrogateescape");
|
2011-10-23 07:51:21 +00:00
|
|
|
|
2013-04-23 13:51:20 +00:00
|
|
|
/* Update, Py3.3 resolves attempting to parse non-existing header */
|
|
|
|
#if 0
|
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. */
|
2012-05-18 10:19:49 +00:00
|
|
|
if (py_path_bundle) {
|
|
|
|
Py_NoSiteFlag = 1;
|
|
|
|
}
|
2013-04-23 13:51:20 +00:00
|
|
|
#endif
|
2012-05-18 10:19:49 +00:00
|
|
|
|
|
|
|
Py_FrozenFlag = 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
|
|
|
|
2012-10-15 09:11:17 +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 */
|
2013-02-06 13:14:11 +00:00
|
|
|
/* broken in py3.3, load explicitly below */
|
|
|
|
// PyImport_ExtendInittab(bpy_internal_modules);
|
2011-02-20 23:39:29 +00:00
|
|
|
#endif
|
|
|
|
|
2011-07-03 19:15:46 +00:00
|
|
|
bpy_intern_string_init();
|
|
|
|
|
2013-02-06 13:14:11 +00:00
|
|
|
|
|
|
|
#ifdef WITH_PYTHON_MODULE
|
|
|
|
{
|
|
|
|
/* Manually load all modules */
|
|
|
|
struct _inittab *inittab_item;
|
|
|
|
PyObject *sys_modules = PyImport_GetModuleDict();
|
|
|
|
|
|
|
|
for (inittab_item = bpy_internal_modules; inittab_item->name; inittab_item++) {
|
|
|
|
PyObject *mod = inittab_item->initfunc();
|
|
|
|
if (mod) {
|
|
|
|
PyDict_SetItemString(sys_modules, inittab_item->name, mod);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2013-02-06 14:02:19 +00:00
|
|
|
// Py_DECREF(mod); /* ideally would decref, but in this case we never want to free */
|
2013-02-06 13:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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");
|
2014-02-13 06:53:42 +11:00
|
|
|
PyGILState_STATE gilstate;
|
2009-08-14 12:29:55 +00:00
|
|
|
|
2014-02-13 06:53:42 +11:00
|
|
|
/* finalizing, no need to grab the state, except when we are a module */
|
|
|
|
gilstate = PyGILState_Ensure();
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* 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();
|
|
|
|
|
2015-04-01 09:34:01 +02:00
|
|
|
/* bpy.app modules that need cleanup */
|
|
|
|
BPY_app_translations_end();
|
|
|
|
|
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 */
|
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_Finalize();
|
2014-02-14 10:55:19 +11:00
|
|
|
|
|
|
|
(void)gilstate;
|
2014-02-13 06:53:42 +11:00
|
|
|
#else
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
#endif
|
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#ifdef TIME_PY_RUN
|
2012-07-07 22:51:57 +00:00
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2013-06-09 23:31:53 +00:00
|
|
|
void BPY_python_reset(bContext *C)
|
|
|
|
{
|
2013-06-10 00:42:16 +00:00
|
|
|
/* unrelated security stuff */
|
|
|
|
G.f &= ~(G_SCRIPT_AUTOEXEC_FAIL | G_SCRIPT_AUTOEXEC_FAIL_QUIET);
|
|
|
|
G.autoexec_fail[0] = '\0';
|
|
|
|
|
2013-06-09 23:31:53 +00:00
|
|
|
BPY_driver_reset();
|
|
|
|
BPY_app_handlers_reset(false);
|
|
|
|
BPY_modules_load_user(C);
|
|
|
|
}
|
|
|
|
|
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 */
|
2013-01-07 05:26:12 +00:00
|
|
|
txt_move_to(text, lineno - 1, INT_MAX, false);
|
|
|
|
txt_move_to(text, lineno - 1, offset, true);
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2014-09-29 15:51:47 +10:00
|
|
|
/* omit other values, we only want the dict. */
|
2011-02-01 09:02:49 +00:00
|
|
|
} PyModuleObject;
|
|
|
|
#endif
|
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
static bool python_script_exec(
|
|
|
|
bContext *C, const char *fn, struct Text *text,
|
|
|
|
struct ReportList *reports, const bool do_jump)
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2012-09-23 06:25:39 +00:00
|
|
|
Main *bmain_old = CTX_data_main(C);
|
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 */
|
2014-04-30 23:43:01 +10:00
|
|
|
char *buf;
|
|
|
|
PyObject *fn_dummy_py;
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2014-04-30 23:43:01 +10:00
|
|
|
fn_dummy_py = PyC_UnicodeFromByte(fn_dummy);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2014-04-30 23:43:01 +10:00
|
|
|
buf = txt_to_buf(text);
|
|
|
|
text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1);
|
2011-03-19 11:12:48 +00:00
|
|
|
MEM_freeN(buf);
|
2009-03-04 13:26:33 +00:00
|
|
|
|
2014-04-30 23:43:01 +10:00
|
|
|
Py_DECREF(fn_dummy_py);
|
|
|
|
|
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'.
|
2017-05-25 18:56:00 +10:00
|
|
|
* So now we load the script file data to a buffer.
|
|
|
|
*
|
|
|
|
* Note on use of 'globals()', it's important not copy the dictionary because
|
|
|
|
* tools may inspect 'sys.modules["__main__"]' for variables defined in the code
|
|
|
|
* where using a copy of 'globals()' causes code execution
|
|
|
|
* to leave the main namespace untouched. see: T51444
|
|
|
|
*
|
|
|
|
* This leaves us with the problem of variables being included,
|
|
|
|
* currently this is worked around using 'dict.__del__' it's ugly but works.
|
|
|
|
*/
|
2010-09-18 19:38:27 +00:00
|
|
|
{
|
2015-08-31 23:41:13 +10:00
|
|
|
const char *pystring =
|
2017-05-25 18:56:00 +10:00
|
|
|
"with open(__file__, 'rb') as f:"
|
|
|
|
"exec(compile(f.read(), __file__, 'exec'), globals().__delitem__('f') or globals())";
|
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
|
|
|
|
2014-03-06 03:27:14 +11:00
|
|
|
py_result = PyRun_String(pystring, Py_file_input, py_dict, py_dict);
|
2010-09-18 19:38:27 +00:00
|
|
|
}
|
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) {
|
2012-09-23 06:25:39 +00:00
|
|
|
/* ensure text is valid before use, the script may have freed its self */
|
|
|
|
Main *bmain_new = CTX_data_main(C);
|
|
|
|
if ((bmain_old == bmain_new) && (BLI_findindex(&bmain_new->text, text) != -1)) {
|
|
|
|
python_script_error_jump_text(text);
|
|
|
|
}
|
2011-02-26 15:30:38 +00:00
|
|
|
}
|
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 */
|
2015-12-31 21:15:29 +11:00
|
|
|
bool BPY_execute_filepath(bContext *C, const char *filepath, struct ReportList *reports)
|
2009-02-28 13:27:45 +00:00
|
|
|
{
|
2013-01-07 05:26:12 +00:00
|
|
|
return python_script_exec(C, filepath, NULL, reports, false);
|
2009-02-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
bool BPY_execute_text(bContext *C, struct Text *text, struct ReportList *reports, const bool 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
|
|
|
}
|
|
|
|
|
2012-10-26 10:54:02 +00:00
|
|
|
void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
|
2012-10-26 10:33:57 +00:00
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
|
|
|
const int do_invalidate = (Py_REFCNT((PyObject *)pyob_ptr) > 1);
|
|
|
|
Py_DECREF((PyObject *)pyob_ptr);
|
|
|
|
if (do_invalidate) {
|
|
|
|
pyrna_invalidate(pyob_ptr);
|
|
|
|
}
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
}
|
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
/**
|
|
|
|
* \return success
|
|
|
|
*/
|
2017-03-18 09:39:36 +11:00
|
|
|
bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verbose, double *r_value)
|
2009-08-10 11:58:53 +00:00
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
2015-12-31 21:15:29 +11:00
|
|
|
bool ok = true;
|
2014-06-17 16:03:40 +02:00
|
|
|
|
2017-03-18 09:39:36 +11:00
|
|
|
if (!r_value || !expr) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-27 22:53:37 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (expr[0] == '\0') {
|
2017-03-18 09:39:36 +11:00
|
|
|
*r_value = 0.0;
|
2015-12-31 21:15:29 +11:00
|
|
|
return ok;
|
2010-02-27 22:53:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
2011-02-01 12:37:53 +00:00
|
|
|
|
2017-03-18 09:39:36 +11:00
|
|
|
ok = PyC_RunString_AsNumber(expr, "<blender button>", r_value);
|
2009-10-22 23:23:09 +00:00
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
if (ok == false) {
|
2011-10-13 01:29:08 +00:00
|
|
|
if (verbose) {
|
2015-05-18 09:12:26 +10:00
|
|
|
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
|
2011-04-17 12:47:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2009-08-10 11:58:53 +00:00
|
|
|
}
|
2010-07-28 23:24:17 +00:00
|
|
|
|
2009-08-10 11:58:53 +00:00
|
|
|
bpy_context_clear(C, &gilstate);
|
2014-06-17 16:03:40 +02:00
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
return ok;
|
2009-08-10 11:58:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 12:19:03 +11:00
|
|
|
/**
|
|
|
|
* \return success
|
|
|
|
*/
|
|
|
|
bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verbose, char **r_value)
|
|
|
|
{
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
if (!r_value || !expr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr[0] == '\0') {
|
|
|
|
*r_value = NULL;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
|
|
ok = PyC_RunString_AsString(expr, "<blender button>", r_value);
|
|
|
|
|
|
|
|
if (ok == false) {
|
|
|
|
if (verbose) {
|
|
|
|
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
bool BPY_execute_string_ex(bContext *C, const char *expr, bool use_eval)
|
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;
|
2015-12-31 21:15:29 +11:00
|
|
|
bool ok = true;
|
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') {
|
2015-12-31 21:15:29 +11:00
|
|
|
return ok;
|
2010-05-30 14:05:58 +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 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));
|
|
|
|
|
2015-06-11 16:57:31 +10:00
|
|
|
retval = PyRun_String(expr, use_eval ? Py_eval_input : Py_file_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) {
|
2015-12-31 21:15:29 +11:00
|
|
|
ok = false;
|
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
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
return ok;
|
2010-05-30 14:05:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 21:15:29 +11:00
|
|
|
bool BPY_execute_string(bContext *C, const char *expr)
|
2015-06-11 16:57:31 +10:00
|
|
|
{
|
2015-12-31 21:15:29 +11:00
|
|
|
return BPY_execute_string_ex(C, expr, true);
|
2015-06-11 16:57:31 +10:00
|
|
|
}
|
2010-05-30 14:05:58 +00:00
|
|
|
|
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) {
|
2012-05-08 22:07:06 +00:00
|
|
|
BPY_context_update(C);
|
2011-08-20 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 15:01:09 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
2012-08-05 14:11:51 +00:00
|
|
|
for (text = bmain->text.first; text; text = text->id.next) {
|
2011-12-26 12:26:11 +00:00
|
|
|
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)) {
|
2013-06-10 00:42:16 +00:00
|
|
|
if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
|
|
|
|
G.f |= G_SCRIPT_AUTOEXEC_FAIL;
|
2013-06-12 00:10:56 +00:00
|
|
|
BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Text '%s'", text->id.name + 2);
|
2013-06-10 00:42:16 +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);
|
|
|
|
}
|
2012-10-01 14:14:21 +00:00
|
|
|
|
|
|
|
/* check if the script loaded a new file */
|
|
|
|
if (bmain != CTX_data_main(C)) {
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
{
|
2012-09-21 22:31:02 +00:00
|
|
|
PyGILState_STATE gilstate;
|
2013-09-18 23:21:24 +00:00
|
|
|
bool use_gil = !PyC_IsInterpreterActive();
|
2012-09-21 22:31:02 +00:00
|
|
|
|
|
|
|
PyObject *pyctx;
|
|
|
|
PyObject *item;
|
2011-12-26 12:26:11 +00:00
|
|
|
PointerRNA *ptr = NULL;
|
2013-01-07 05:26:12 +00:00
|
|
|
bool done = false;
|
2009-10-29 09:25:11 +00:00
|
|
|
|
2012-09-21 22:31:02 +00:00
|
|
|
if (use_gil)
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
|
|
|
|
pyctx = (PyObject *)CTX_py_dict_get(C);
|
|
|
|
item = PyDict_GetItemString(pyctx, member);
|
|
|
|
|
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) {
|
2013-06-10 12:17:36 +00:00
|
|
|
done = true;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
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);
|
2013-02-07 04:04:12 +00:00
|
|
|
CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
|
2013-01-07 05:26:12 +00:00
|
|
|
done = true;
|
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);
|
2015-07-29 09:58:10 +10:00
|
|
|
PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
|
2009-10-29 10:03:34 +00:00
|
|
|
int i;
|
2015-07-29 09:58:10 +10:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2015-07-29 09:58:10 +10:00
|
|
|
PyObject *list_item = seq_fast_items[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 {
|
2018-04-02 11:05:15 +02:00
|
|
|
CLOG_INFO(BPY_LOG_CONTEXT, 1,
|
|
|
|
"'%s' list item not a valid type in sequence type '%s'",
|
|
|
|
member, Py_TYPE(item)->tp_name);
|
2009-10-29 10:03:34 +00:00
|
|
|
}
|
|
|
|
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2009-10-29 10:03:34 +00:00
|
|
|
Py_DECREF(seq_fast);
|
2013-02-07 04:04:12 +00:00
|
|
|
CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
|
2013-01-07 05:26:12 +00:00
|
|
|
done = true;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 04:04:12 +00:00
|
|
|
if (done == false) {
|
2013-09-02 19:28:44 +00:00
|
|
|
if (item) {
|
2018-04-02 11:05:15 +02:00
|
|
|
CLOG_INFO(BPY_LOG_CONTEXT, 1, "'%s' not a valid type", member);
|
2013-09-02 19:28:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-04-02 11:05:15 +02:00
|
|
|
CLOG_INFO(BPY_LOG_CONTEXT, 1, "'%s' not found\n", member);
|
2013-09-02 19:28:44 +00:00
|
|
|
}
|
2009-11-10 19:57:04 +00:00
|
|
|
}
|
2010-01-31 21:52:26 +00:00
|
|
|
else {
|
2018-04-02 11:05:15 +02:00
|
|
|
CLOG_INFO(BPY_LOG_CONTEXT, 2, "'%s' found", member);
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:31:02 +00:00
|
|
|
if (use_gil)
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
|
2009-10-29 09:25:11 +00:00
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2011-02-20 23:39:29 +00:00
|
|
|
#ifdef WITH_PYTHON_MODULE
|
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 */
|
2013-01-02 16:15:45 +00:00
|
|
|
static 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));
|
2015-10-08 15:05:58 +11:00
|
|
|
BLI_path_cwd(filename_abs, sizeof(filename_abs));
|
2017-07-17 12:44:03 +10:00
|
|
|
Py_DECREF(filename_obj);
|
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);
|
|
|
|
|
2015-04-16 22:26:20 +10:00
|
|
|
static PyTypeObject dealloc_obj_Type;
|
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);
|
|
|
|
}
|
|
|
|
|
2013-01-02 16:15:45 +00:00
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_bpy(void);
|
|
|
|
|
2011-02-22 14:19:09 +00:00
|
|
|
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
|
2013-02-19 16:13:41 +00:00
|
|
|
|
2016-04-01 09:43:11 +11:00
|
|
|
/**
|
|
|
|
* Avoids duplicating keyword list.
|
|
|
|
*/
|
|
|
|
bool BPY_string_is_keyword(const char *str)
|
|
|
|
{
|
|
|
|
/* list is from...
|
2016-07-02 10:02:04 +10:00
|
|
|
* ", ".join(['"%s"' % kw for kw in __import__("keyword").kwlist])
|
|
|
|
*/
|
2016-04-01 09:43:11 +11:00
|
|
|
const char *kwlist[] = {
|
|
|
|
"False", "None", "True",
|
|
|
|
"and", "as", "assert", "break",
|
|
|
|
"class", "continue", "def", "del", "elif", "else", "except",
|
|
|
|
"finally", "for", "from", "global", "if", "import", "in",
|
|
|
|
"is", "lambda", "nonlocal", "not", "or", "pass", "raise",
|
|
|
|
"return", "try", "while", "with", "yield", NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; kwlist[i]; i++) {
|
|
|
|
if (STREQ(str, kwlist[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:13:41 +00:00
|
|
|
|
|
|
|
/* EVIL, define text.c functions here... */
|
2013-03-11 20:27:38 +00:00
|
|
|
/* BKE_text.h */
|
2013-02-19 16:13:41 +00:00
|
|
|
int text_check_identifier_unicode(const unsigned int ch)
|
|
|
|
{
|
|
|
|
return (ch < 255 && text_check_identifier((char)ch)) || Py_UNICODE_ISALNUM(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
int text_check_identifier_nodigit_unicode(const unsigned int ch)
|
|
|
|
{
|
|
|
|
return (ch < 255 && text_check_identifier_nodigit((char)ch)) || Py_UNICODE_ISALPHA(ch);
|
|
|
|
}
|