Blender V5.0
script_edit.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "BLI_listbase.h"
12
13#include "BKE_context.hh"
14#include "BKE_report.hh"
15
16#include "BLT_translation.hh"
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20#include "wm_event_system.hh"
21
22#include "RNA_access.hh"
23#include "RNA_define.hh"
24
25#include "ED_screen.hh"
26
27#include "script_intern.hh" /* own include */
28
29#ifdef WITH_PYTHON
30# include "BPY_extern_run.hh"
31#endif
32
34{
35 char filepath[FILE_MAX];
36 RNA_string_get(op->ptr, "filepath", filepath);
37#ifdef WITH_PYTHON
38 if (BPY_run_filepath(C, filepath, op->reports)) {
39 ARegion *region = CTX_wm_region(C);
40 if (region != nullptr) {
42 }
43 return OPERATOR_FINISHED;
44 }
45#else
46 (void)C; /* unused */
47#endif
48 return OPERATOR_CANCELLED; /* FAIL */
49}
50
52{
53 PropertyRNA *prop;
54
55 /* identifiers */
56 ot->name = "Run Python File";
57 ot->description = "Run Python file";
58 ot->idname = "SCRIPT_OT_python_file_run";
59
60 /* API callbacks. */
61 ot->exec = run_pyfile_exec;
62
63 /* flags */
65
66 prop = RNA_def_string_file_path(ot->srna, "filepath", nullptr, FILE_MAX, "Path", "");
68}
69
70#ifdef WITH_PYTHON
71static bool script_test_modal_operators(bContext *C)
72{
74 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
75 LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
76 if (handler_base->type == WM_HANDLER_TYPE_OP) {
77 wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
78 if (handler->op != nullptr) {
79 wmOperatorType *ot = handler->op->type;
80 if (ot->rna_ext.srna) {
81 return true;
82 }
83 }
84 }
85 }
86 }
87
88 return false;
89}
90#endif
91
93{
94
95#ifdef WITH_PYTHON
96
97 /* clear running operators */
98 if (script_test_modal_operators(C)) {
99 BKE_report(op->reports, RPT_ERROR, "Cannot reload with running modal operators");
100 return OPERATOR_CANCELLED;
101 }
102
103 /* TODO(@ideasman42): this crashes on netrender and keying sets, need to look into why
104 * disable for now unless running in debug mode. */
105
106 /* It would be nice if we could detect when this is called from the Python
107 * only postponing in that case, for now always do it. */
108 if (true) {
109 /* Postpone when called from Python so this can be called from an operator
110 * that might be re-registered, crashing Blender when we try to read from the
111 * freed operator type which, see #80694. */
112 const char *imports[] = {"bpy", nullptr};
114 imports,
115 "def fn():\n"
116 " bpy.utils.load_scripts(reload_scripts=True)\n"
117 " return None\n"
118 "bpy.app.timers.register(fn)");
119 }
120 else {
121 WM_cursor_wait(true);
122 const char *imports[] = {"bpy", nullptr};
123 BPY_run_string_eval(C, imports, "bpy.utils.load_scripts(reload_scripts=True)");
124 WM_cursor_wait(false);
125 }
126
127 /* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
128 * any additional updates required by this operator should go there. */
129
130 return OPERATOR_FINISHED;
131#else
132 UNUSED_VARS(C, op);
133 return OPERATOR_CANCELLED;
134#endif
135}
136
138{
139 /* identifiers */
140 ot->name = "Reload Scripts";
141 ot->description = "Reload scripts";
142 ot->idname = "SCRIPT_OT_reload";
143
144 /* API callbacks. */
145 ot->exec = script_reload_exec;
146}
ARegion * CTX_wm_region(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
@ RPT_ERROR
Definition BKE_report.hh:39
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
#define LISTBASE_FOREACH(type, var, list)
#define FILE_MAX
#define UNUSED_VARS(...)
#define BLT_I18NCONTEXT_EDITOR_FILEBROWSER
bool BPY_run_filepath(bContext *C, const char *filepath, ReportList *reports) ATTR_NONNULL(1
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:618
#define C
Definition RandGen.cpp:29
@ OPTYPE_INTERNAL
Definition WM_types.hh:202
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
std::string RNA_string_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string_file_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void SCRIPT_OT_reload(wmOperatorType *ot)
static wmOperatorStatus script_reload_exec(bContext *C, wmOperator *op)
static wmOperatorStatus run_pyfile_exec(bContext *C, wmOperator *op)
void SCRIPT_OT_python_file_run(wmOperatorType *ot)
struct ReportList * reports
struct wmOperatorType * type
struct PointerRNA * ptr
void WM_cursor_wait(bool val)
@ WM_HANDLER_TYPE_OP
wmOperatorType * ot
Definition wm_files.cc:4237