Blender V5.3
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.hh"
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
33namespace blender {
34
36{
37 char filepath[FILE_MAX];
38 RNA_string_get(op->ptr, "filepath", filepath);
39#ifdef WITH_PYTHON
40 if (BPY_run_filepath(C, filepath, op->reports)) {
41 ARegion *region = CTX_wm_region(C);
42 if (region != nullptr) {
44 }
45 return OPERATOR_FINISHED;
46 }
47#else
48 (void)C; /* unused */
49#endif
50 return OPERATOR_CANCELLED; /* FAIL */
51}
52
54{
55 PropertyRNA *prop;
56
57 /* identifiers */
58 ot->name = "Run Python File";
59 ot->description = "Run Python file";
60 ot->idname = "SCRIPT_OT_python_file_run";
61
62 /* API callbacks. */
64
65 /* flags */
67
68 prop = RNA_def_string_file_path(ot->srna, "filepath", nullptr, FILE_MAX, "Path", "");
70}
71
72#ifdef WITH_PYTHON
73static bool script_test_modal_operators(bContext *C)
74{
75 wmWindowManager *wm = CTX_wm_manager(C);
76 for (wmWindow &win : wm->windows) {
77 for (wmEventHandler &handler_base : win.runtime->modalhandlers) {
78 if (handler_base.type == WM_HANDLER_TYPE_OP) {
79 wmEventHandler_Op *handler = reinterpret_cast<wmEventHandler_Op *>(&handler_base);
80 if (handler->op != nullptr) {
81 wmOperatorType *ot = handler->op->type;
82 if (ot->rna_ext.srna) {
83 return true;
84 }
85 }
86 }
87 }
88 }
89
90 return false;
91}
92#endif
93
95{
96
97#ifdef WITH_PYTHON
98
99 /* clear running operators */
100 if (script_test_modal_operators(C)) {
101 BKE_report(op->reports, RPT_ERROR, "Cannot reload with running modal operators");
102 return OPERATOR_CANCELLED;
103 }
104
105 /* TODO(@ideasman42): this crashes on netrender and keying sets, need to look into why
106 * disable for now unless running in debug mode. */
107
108 /* It would be nice if we could detect when this is called from the Python
109 * only postponing in that case, for now always do it. */
110 if (true) {
111 /* Postpone when called from Python so this can be called from an operator
112 * that might be re-registered, crashing Blender when we try to read from the
113 * freed operator type which, see #80694. */
114 const char *imports[] = {"bpy", nullptr};
116 imports,
117 "def fn():\n"
118 " bpy.utils.load_scripts(reload_scripts=True)\n"
119 " return None\n"
120 "bpy.app.timers.register(fn)");
121 }
122 else {
123 WM_cursor_wait(true);
124 const char *imports[] = {"bpy", nullptr};
125 BPY_run_string_eval(C, imports, "bpy.utils.load_scripts(reload_scripts=True)");
126 WM_cursor_wait(false);
127 }
128
129 /* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
130 * any additional updates required by this operator should go there. */
131
132 return OPERATOR_FINISHED;
133#else
134 UNUSED_VARS(C, op);
135 return OPERATOR_CANCELLED;
136#endif
137}
138
140{
141 /* identifiers */
142 ot->name = "Reload Scripts";
143 ot->description = "Reload scripts";
144 ot->idname = "SCRIPT_OT_reload";
145
146 /* API callbacks. */
148}
149
150} // namespace blender
#define FILE_MAX
#define BLT_I18NCONTEXT_EDITOR_FILEBROWSER
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS(...)
BMesh Iterators.
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 SCRIPT_OT_reload(wmOperatorType *ot)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
bool BPY_run_filepath(bContext *C, const char *filepath, ReportList *reports) ATTR_NONNULL(1
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
wmWindowManager * CTX_wm_manager(const bContext *C)
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
static wmOperatorStatus run_pyfile_exec(bContext *C, wmOperator *op)
static wmOperatorStatus script_reload_exec(bContext *C, wmOperator *op)
void SCRIPT_OT_python_file_run(wmOperatorType *ot)
bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void WM_cursor_wait(bool val)
ARegion * CTX_wm_region(const bContext *C)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
struct ReportList * reports