Blender  V3.3
script_edit.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2008 Blender Foundation. All rights reserved. */
3 
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include "BLI_listbase.h"
12 #include "BLI_utildefines.h"
13 
14 #include "BKE_context.h"
15 #include "BKE_report.h"
16 
17 #include "WM_api.h"
18 #include "WM_types.h"
19 #include "wm_event_system.h"
20 
21 #include "RNA_access.h"
22 #include "RNA_define.h"
23 
24 #include "ED_screen.h"
25 
26 #include "script_intern.h" /* own include */
27 
28 #ifdef WITH_PYTHON
29 # include "BPY_extern_run.h"
30 #endif
31 
33 {
34  char path[FILE_MAX];
35  RNA_string_get(op->ptr, "filepath", path);
36 #ifdef WITH_PYTHON
37  if (BPY_run_filepath(C, path, op->reports)) {
38  ARegion *region = CTX_wm_region(C);
39  if (region != NULL) {
40  ED_region_tag_redraw(region);
41  }
42  return OPERATOR_FINISHED;
43  }
44 #else
45  (void)C; /* unused */
46 #endif
47  return OPERATOR_CANCELLED; /* FAIL */
48 }
49 
51 {
52  /* identifiers */
53  ot->name = "Run Python File";
54  ot->description = "Run Python file";
55  ot->idname = "SCRIPT_OT_python_file_run";
56 
57  /* api callbacks */
59 
60  /* flags */
62 
63  RNA_def_string_file_path(ot->srna, "filepath", NULL, FILE_MAX, "Path", "");
64 }
65 
66 #ifdef WITH_PYTHON
67 static bool script_test_modal_operators(bContext *C)
68 {
69  wmWindowManager *wm;
70  wmWindow *win;
71 
72  wm = CTX_wm_manager(C);
73 
74  for (win = wm->windows.first; win; win = win->next) {
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 != NULL) {
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, "Can't reload with running modal operators");
100  return OPERATOR_CANCELLED;
101  }
102 
103  /* TODO(campbell): 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 T80694. */
113  (const char *[]){"bpy", NULL},
114  "def fn():\n"
115  " bpy.utils.load_scripts(reload_scripts=True)\n"
116  " return None\n"
117  "bpy.app.timers.register(fn)");
118  }
119  else {
120  WM_cursor_wait(true);
122  C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
123  WM_cursor_wait(false);
124  }
125 
126  /* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
127  * any additional updates required by this operator should go there. */
128 
129  return OPERATOR_FINISHED;
130 #else
131  UNUSED_VARS(C, op);
132  return OPERATOR_CANCELLED;
133 #endif
134 }
135 
137 {
138  /* identifiers */
139  ot->name = "Reload Scripts";
140  ot->description = "Reload scripts";
141  ot->idname = "SCRIPT_OT_reload";
142 
143  /* api callbacks */
145 }
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:713
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition: report.c:83
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
#define FILE_MAX
#define UNUSED_VARS(...)
bool BPY_run_filepath(struct bContext *C, const char *filepath, struct ReportList *reports) ATTR_NONNULL(1
bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr)
bool BPY_run_string_exec(struct bContext *C, const char *imports[], const char *expr)
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:655
#define C
Definition: RandGen.cpp:25
@ OPTYPE_INTERNAL
Definition: WM_types.h:168
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
SyclQueue void void size_t num_bytes void
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:5116
PropertyRNA * RNA_def_string_file_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3711
void SCRIPT_OT_reload(wmOperatorType *ot)
Definition: script_edit.c:136
static int script_reload_exec(bContext *C, wmOperator *op)
Definition: script_edit.c:92
static int run_pyfile_exec(bContext *C, wmOperator *op)
Definition: script_edit.c:32
void SCRIPT_OT_python_file_run(wmOperatorType *ot)
Definition: script_edit.c:50
StructRNA * srna
Definition: RNA_types.h:766
void * first
Definition: DNA_listBase.h:31
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
struct StructRNA * srna
Definition: WM_types.h:969
ExtensionRNA rna_ext
Definition: WM_types.h:993
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
struct ReportList * reports
struct wmOperatorType * type
struct PointerRNA * ptr
struct wmWindow * next
void WM_cursor_wait(bool val)
Definition: wm_cursors.c:209
@ WM_HANDLER_TYPE_OP
wmOperatorType * ot
Definition: wm_files.c:3479