Blender V4.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
9#include <cstdio>
10#include <cstring>
11
12#include "BLI_listbase.h"
13#include "BLI_utildefines.h"
14
15#include "BKE_context.hh"
16#include "BKE_report.hh"
17
18#include "BLT_translation.hh"
19
20#include "WM_api.hh"
21#include "WM_types.hh"
22#include "wm_event_system.hh"
23
24#include "RNA_access.hh"
25#include "RNA_define.hh"
26
27#include "ED_screen.hh"
28
29#include "script_intern.hh" /* own include */
30
31#ifdef WITH_PYTHON
32# include "BPY_extern_run.hh"
33#endif
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{
76 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
77 LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
78 if (handler_base->type == WM_HANDLER_TYPE_OP) {
79 wmEventHandler_Op *handler = (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, "Can't 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}
ARegion * CTX_wm_region(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
#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)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:634
@ OPTYPE_INTERNAL
Definition WM_types.hh:182
@ OPTYPE_UNDO
Definition WM_types.hh:162
@ OPTYPE_REGISTER
Definition WM_types.hh:160
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
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 int script_reload_exec(bContext *C, wmOperator *op)
static int run_pyfile_exec(bContext *C, wmOperator *op)
void SCRIPT_OT_python_file_run(wmOperatorType *ot)
StructRNA * srna
Definition RNA_types.hh:780
const char * name
Definition WM_types.hh:990
const char * idname
Definition WM_types.hh:992
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
ExtensionRNA rna_ext
Definition WM_types.hh:1104
const char * description
Definition WM_types.hh:996
StructRNA * srna
Definition WM_types.hh:1080
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:4125