Blender  V2.93
script_edit.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "BLI_listbase.h"
28 #include "BLI_utildefines.h"
29 
30 #include "BKE_context.h"
31 #include "BKE_report.h"
32 
33 #include "WM_api.h"
34 #include "WM_types.h"
35 #include "wm_event_system.h"
36 
37 #include "RNA_access.h"
38 #include "RNA_define.h"
39 
40 #include "ED_screen.h"
41 
42 #include "script_intern.h" /* own include */
43 
44 #ifdef WITH_PYTHON
45 # include "BPY_extern_run.h"
46 #endif
47 
49 {
50  char path[FILE_MAX];
51  RNA_string_get(op->ptr, "filepath", path);
52 #ifdef WITH_PYTHON
53  if (BPY_run_filepath(C, path, op->reports)) {
54  ARegion *region = CTX_wm_region(C);
55  ED_region_tag_redraw(region);
56  return OPERATOR_FINISHED;
57  }
58 #else
59  (void)C; /* unused */
60 #endif
61  return OPERATOR_CANCELLED; /* FAIL */
62 }
63 
65 {
66  /* identifiers */
67  ot->name = "Run Python File";
68  ot->description = "Run Python file";
69  ot->idname = "SCRIPT_OT_python_file_run";
70 
71  /* api callbacks */
74 
75  /* flags */
77 
78  RNA_def_string_file_path(ot->srna, "filepath", NULL, FILE_MAX, "Path", "");
79 }
80 
81 #ifdef WITH_PYTHON
82 static bool script_test_modal_operators(bContext *C)
83 {
84  wmWindowManager *wm;
85  wmWindow *win;
86 
87  wm = CTX_wm_manager(C);
88 
89  for (win = wm->windows.first; win; win = win->next) {
90  LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
91  if (handler_base->type == WM_HANDLER_TYPE_OP) {
92  wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
93  if (handler->op != NULL) {
94  wmOperatorType *ot = handler->op->type;
95  if (ot->rna_ext.srna) {
96  return true;
97  }
98  }
99  }
100  }
101  }
102 
103  return false;
104 }
105 #endif
106 
108 {
109 
110 #ifdef WITH_PYTHON
111 
112  /* clear running operators */
113  if (script_test_modal_operators(C)) {
114  BKE_report(op->reports, RPT_ERROR, "Can't reload with running modal operators");
115  return OPERATOR_CANCELLED;
116  }
117 
118  /* TODO(campbell): this crashes on netrender and keying sets, need to look into why
119  * disable for now unless running in debug mode. */
120 
121  /* It would be nice if we could detect when this is called from the Python
122  * only postponing in that case, for now always do it. */
123  if (true) {
124  /* Postpone when called from Python so this can be called from an operator
125  * that might be re-registered, crashing Blender when we try to read from the
126  * freed operator type which, see T80694. */
128  (const char *[]){"bpy", NULL},
129  "def fn():\n"
130  " bpy.utils.load_scripts(reload_scripts=True)\n"
131  " return None\n"
132  "bpy.app.timers.register(fn)");
133  }
134  else {
135  WM_cursor_wait(true);
137  C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
138  WM_cursor_wait(false);
139  }
140 
141  /* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
142  * any additional updates required by this operator should go there. */
143 
144  return OPERATOR_FINISHED;
145 #else
146  UNUSED_VARS(C, op);
147  return OPERATOR_CANCELLED;
148 #endif
149 }
150 
152 {
153  /* identifiers */
154  ot->name = "Reload Scripts";
155  ot->description = "Reload scripts";
156  ot->idname = "SCRIPT_OT_reload";
157 
158  /* api callbacks */
160 }
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#define FILE_MAX
#define UNUSED_VARS(...)
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)
bool BPY_run_filepath(struct bContext *C, const char *filepath, struct ReportList *reports)
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_operator_areaactive(struct bContext *C)
Definition: screen_ops.c:119
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
#define C
Definition: RandGen.cpp:39
@ OPTYPE_INTERNAL
Definition: WM_types.h:175
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
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:3699
void SCRIPT_OT_reload(wmOperatorType *ot)
Definition: script_edit.c:151
static int script_reload_exec(bContext *C, wmOperator *op)
Definition: script_edit.c:107
static int run_pyfile_exec(bContext *C, wmOperator *op)
Definition: script_edit.c:48
void SCRIPT_OT_python_file_run(wmOperatorType *ot)
Definition: script_edit.c:64
StructRNA * srna
Definition: RNA_types.h:681
void * first
Definition: DNA_listBase.h:47
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
ExtensionRNA rna_ext
Definition: WM_types.h:826
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct wmOperatorType * type
struct PointerRNA * ptr
struct wmWindow * next
void WM_cursor_wait(bool val)
Definition: wm_cursors.c:226
@ WM_HANDLER_TYPE_OP
wmOperatorType * ot
Definition: wm_files.c:3156