2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-12-25 14:17:54 +00:00
|
|
|
* 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.
|
2008-12-25 14:17:54 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2008 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup spscript
|
2011-02-27 20:29:51 +00:00
|
|
|
*/
|
|
|
|
|
2008-12-25 14:17:54 +00:00
|
|
|
#include <stdio.h>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <string.h>
|
2008-12-25 14:17:54 +00:00
|
|
|
|
2019-02-19 15:18:56 +11:00
|
|
|
#include "BLI_listbase.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2008-12-25 14:17:54 +00:00
|
|
|
|
|
|
|
#include "BKE_context.h"
|
2013-06-28 16:15:44 +00:00
|
|
|
#include "BKE_report.h"
|
2008-12-25 14:17:54 +00:00
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
#include "WM_types.h"
|
2013-06-28 16:15:44 +00:00
|
|
|
#include "wm_event_system.h"
|
2008-12-25 14:17:54 +00:00
|
|
|
|
|
|
|
#include "RNA_access.h"
|
|
|
|
#include "RNA_define.h"
|
|
|
|
|
|
|
|
#include "ED_screen.h"
|
|
|
|
|
2012-05-08 11:48:19 +00:00
|
|
|
#include "script_intern.h" /* own include */
|
2008-12-25 14:17:54 +00:00
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#ifdef WITH_PYTHON
|
2020-08-17 17:46:06 +10:00
|
|
|
# include "BPY_extern_run.h"
|
2010-06-06 01:15:44 +00:00
|
|
|
#endif
|
2008-12-25 14:17:54 +00:00
|
|
|
|
|
|
|
static int run_pyfile_exec(bContext *C, wmOperator *op)
|
|
|
|
{
|
2009-09-12 19:54:39 +00:00
|
|
|
char path[512];
|
2010-06-14 03:52:10 +00:00
|
|
|
RNA_string_get(op->ptr, "filepath", path);
|
2010-10-31 04:11:39 +00:00
|
|
|
#ifdef WITH_PYTHON
|
2020-08-17 17:46:06 +10:00
|
|
|
if (BPY_run_filepath(C, path, op->reports)) {
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region = CTX_wm_region(C);
|
|
|
|
ED_region_tag_redraw(region);
|
2009-06-14 12:53:47 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
}
|
2010-10-23 16:03:31 +00:00
|
|
|
#else
|
|
|
|
(void)C; /* unused */
|
2008-12-25 16:02:35 +00:00
|
|
|
#endif
|
2009-06-14 12:53:47 +00:00
|
|
|
return OPERATOR_CANCELLED; /* FAIL */
|
2008-12-25 14:17:54 +00:00
|
|
|
}
|
|
|
|
|
2009-04-12 23:05:40 +00:00
|
|
|
void SCRIPT_OT_python_file_run(wmOperatorType *ot)
|
2008-12-25 14:17:54 +00:00
|
|
|
{
|
|
|
|
/* identifiers */
|
2013-01-27 07:23:58 +00:00
|
|
|
ot->name = "Run Python File";
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->description = "Run Python file";
|
|
|
|
ot->idname = "SCRIPT_OT_python_file_run";
|
2008-12-25 14:17:54 +00:00
|
|
|
|
|
|
|
/* api callbacks */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->exec = run_pyfile_exec;
|
|
|
|
ot->poll = ED_operator_areaactive;
|
2008-12-25 14:17:54 +00:00
|
|
|
|
2020-03-15 21:46:18 +11:00
|
|
|
/* flags */
|
2014-02-26 13:48:41 +11:00
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
|
|
|
|
|
2014-01-16 21:43:22 +11:00
|
|
|
RNA_def_string_file_path(ot->srna, "filepath", NULL, FILE_MAX, "Path", "");
|
2008-12-25 14:17:54 +00:00
|
|
|
}
|
2009-01-16 23:53:11 +00:00
|
|
|
|
2013-06-28 16:15:44 +00:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
static bool script_test_modal_operators(bContext *C)
|
|
|
|
{
|
|
|
|
wmWindowManager *wm;
|
|
|
|
wmWindow *win;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-28 16:15:44 +00:00
|
|
|
wm = CTX_wm_manager(C);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-28 16:15:44 +00:00
|
|
|
for (win = wm->windows.first; win; win = win->next) {
|
2019-02-19 15:18:56 +11:00
|
|
|
LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
|
|
|
|
if (handler_base->type == WM_HANDLER_TYPE_OP) {
|
|
|
|
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
|
|
|
|
if (handler->op != NULL) {
|
|
|
|
wmOperatorType *ot = handler->op->type;
|
2020-04-03 18:24:08 +02:00
|
|
|
if (ot->rna_ext.srna) {
|
2019-02-19 15:18:56 +11:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-28 16:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-28 16:15:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int script_reload_exec(bContext *C, wmOperator *op)
|
2010-06-01 08:15:43 +00:00
|
|
|
{
|
2019-10-01 01:59:31 +10:00
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#ifdef WITH_PYTHON
|
2013-06-28 16:15:44 +00:00
|
|
|
|
|
|
|
/* clear running operators */
|
|
|
|
if (script_test_modal_operators(C)) {
|
|
|
|
BKE_report(op->reports, RPT_ERROR, "Can't reload with running modal operators");
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-09-14 17:50:01 +10:00
|
|
|
/* TODO(campbell): this crashes on netrender and keying sets, need to look into why
|
|
|
|
* disable for now unless running in debug mode. */
|
|
|
|
|
|
|
|
/* It would be nice if we could detect when this is called from the Python
|
|
|
|
* only postponing in that case, for now always do it. */
|
|
|
|
if (true) {
|
|
|
|
/* Postpone when called from Python so this can be called from an operator
|
|
|
|
* that might be re-registered, crashing Blender when we try to read from the
|
|
|
|
* freed operator type which, see T80694. */
|
|
|
|
BPY_run_string_exec(C,
|
|
|
|
(const char *[]){"bpy", NULL},
|
|
|
|
"def fn():\n"
|
|
|
|
" bpy.utils.load_scripts(reload_scripts=True)\n"
|
|
|
|
" return None\n"
|
|
|
|
"bpy.app.timers.register(fn)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WM_cursor_wait(true);
|
|
|
|
BPY_run_string_eval(
|
|
|
|
C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
|
|
|
|
WM_cursor_wait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
|
|
|
|
* any additional updates required by this operator should go there. */
|
|
|
|
|
2010-06-01 08:15:43 +00:00
|
|
|
return OPERATOR_FINISHED;
|
2010-10-23 16:03:31 +00:00
|
|
|
#else
|
2014-11-24 12:01:51 +01:00
|
|
|
UNUSED_VARS(C, op);
|
2010-06-01 08:15:43 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
2012-01-20 23:03:41 +00:00
|
|
|
#endif
|
2010-06-01 08:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SCRIPT_OT_reload(wmOperatorType *ot)
|
|
|
|
{
|
|
|
|
/* identifiers */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->name = "Reload Scripts";
|
2020-12-13 13:12:56 -08:00
|
|
|
ot->description = "Reload scripts";
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->idname = "SCRIPT_OT_reload";
|
2010-06-01 08:15:43 +00:00
|
|
|
|
|
|
|
/* api callbacks */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->exec = script_reload_exec;
|
2010-06-01 08:15:43 +00:00
|
|
|
}
|