-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
410 lines (328 loc) · 15.1 KB
/
operators.py
File metadata and controls
410 lines (328 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import bpy
import subprocess
from .utils import *
from .i18n import *
from bpy.props import StringProperty, IntProperty
import sys
import webbrowser # 导入浏览器控制库
class SCRIPTMANAGER_OT_remove_all_handlers(bpy.types.Operator):
bl_idname = "script_manager.remove_all_handlers"
bl_label = "Remove All Handlers"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
for handler in list(bpy.app.handlers.frame_change_pre):
bpy.app.handlers.frame_change_pre.remove(handler)
for handler in list(bpy.app.handlers.depsgraph_update_post):
bpy.app.handlers.depsgraph_update_post.remove(handler)
prefs = context.scene.text_manager_prefs
for item in prefs.text_manager_collection:
item.run_in_desgaph_update = False
item.run_in_frame_update = False
return {"FINISHED"}
class SCRIPTMANAGER_OT_remove_addon_handlers(bpy.types.Operator):
bl_idname = "script_manager.remove_addon_handlers"
bl_label = "Remove All Handlers"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
for handler in list(bpy.app.handlers.frame_change_pre):
if hasattr(handler, "_ScriptManagerItem_FC_ID"):
bpy.app.handlers.frame_change_pre.remove(handler)
for handler in list(bpy.app.handlers.depsgraph_update_post):
if hasattr(handler, "_ScriptManagerItem_DC_ID"):
bpy.app.handlers.depsgraph_update_post.remove(handler)
prefs = context.scene.text_manager_prefs
for item in prefs.text_manager_collection:
item.run_in_desgaph_update = False
item.run_in_frame_update = False
return {"FINISHED"}
class SCRIPTMANAGER_OT_remove_handler(bpy.types.Operator):
bl_idname = "script_manager.remove_handler"
bl_label = "Remove Handler by Name"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
removed = False
frame_change_pre_names = [handler.__name__ for handler in bpy.app.handlers.frame_change_pre]
depsgraph_update_post_names = [handler.__name__ for handler in bpy.app.handlers.depsgraph_update_post]
tmep = frame_change_pre_names + depsgraph_update_post_names
target_handler_names = tmep[context.scene.text_manager_prefs.handler_index]
# 移除 frame_change_pre 中的 handler
for handler in list(bpy.app.handlers.frame_change_pre):
if handler.__name__ == target_handler_names:
bpy.app.handlers.frame_change_pre.remove(handler)
removed = True
# 移除 depsgraph_update_post 中的 handler
for handler in list(bpy.app.handlers.depsgraph_update_post):
if handler.__name__ == target_handler_names:
bpy.app.handlers.depsgraph_update_post.remove(handler)
removed = True
if removed:
self.report({"INFO"}, f"Handler removed: {target_handler_names}")
return {"FINISHED"}
else:
self.report({"WARNING"}, f"Handler not found: {target_handler_names}")
return {"CANCELLED"}
# 操作按钮
class SCRIPTMANAGER_OT_add_item(bpy.types.Operator):
bl_idname = "script_manager.add_item"
bl_label = "Add Item"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
prefs = context.scene.text_manager_prefs
item = prefs.text_manager_collection.add()
item.text_name = "New Text"
prefs.script_manager_index = len(prefs.text_manager_collection) - 1
return {"FINISHED"}
class SCRIPTMANAGER_OT_remove_item(bpy.types.Operator):
bl_idname = "script_manager.remove_item"
bl_label = "Remove Item"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
prefs = context.scene.text_manager_prefs
# 先收集所有选中项的索引
selected_indices = [i for i, item in enumerate(prefs.text_manager_collection) if getattr(item, "selected", False)]
if selected_indices:
# 倒序删除,避免索引错位
for idx in reversed(selected_indices):
prefs.text_manager_collection.remove(idx)
# 更新 active_index
prefs.script_manager_index = min(selected_indices[0], len(prefs.text_manager_collection) - 1)
else:
# 如果没有选中项,就删除当前 active_index
idx = prefs.script_manager_index
if 0 <= idx < len(prefs.text_manager_collection):
prefs.text_manager_collection.remove(idx)
prefs.script_manager_index = max(0, idx - 1)
return {"FINISHED"}
class SCRIPTMANAGER_OT_move_item_up(bpy.types.Operator):
bl_idname = "script_manager.move_item_up"
bl_label = "Move Item Up"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
prefs = context.scene.text_manager_prefs
idx = prefs.script_manager_index
if idx > 0:
prefs.text_manager_collection.move(idx, idx - 1)
prefs.script_manager_index = idx - 1
return {"FINISHED"}
class SCRIPTMANAGER_OT_new_text(bpy.types.Operator):
bl_idname = "script_manager.new_text"
bl_label = "New Text"
bl_description = "Create a new Blender text and add to the manager"
bl_options = {"REGISTER", "UNDO"}
text_name: StringProperty(name="Text Name", default="New Script.py")
def execute(self, context):
prefs = context.scene.text_manager_prefs
# 创建 Blender 文本数据块
new_text = bpy.data.texts.new(self.text_name)
# 添加到 text_manager_collection
item = prefs.text_manager_collection.add()
item.text_name = new_text.name
item.text_pointer = new_text
prefs.script_manager_index = len(prefs.text_manager_collection) - 1
return {"FINISHED"}
def invoke(self, context, event):
# 弹出输入框让用户修改名称
return context.window_manager.invoke_props_dialog(self)
class SCRIPTMANAGER_OT_move_item_down(bpy.types.Operator):
bl_idname = "script_manager.move_item_down"
bl_label = "Move Item Down"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
prefs = context.scene.text_manager_prefs
idx = prefs.script_manager_index
if idx < len(prefs.text_manager_collection) - 1:
prefs.text_manager_collection.move(idx, idx + 1)
prefs.script_manager_index = idx + 1
return {"FINISHED"}
class SCRIPTMANAGER_OT_open_in_vscode(bpy.types.Operator):
bl_idname = "text_manager.open_in_vscode"
bl_label = "Open in VSCode"
bl_description = "Open the selected Blender text in VSCode"
text_name: bpy.props.StringProperty(name="Text Name")
def execute(self, context):
addon_prefs = context.preferences.addons[__package__].preferences
vscode_path = addon_prefs.vscode_path.strip('"').strip()
# vscode_path = "D:/rj/vscode/Microsoft VS Code/Code.exe"#调试用
text = bpy.data.texts.get(self.text_name)
if text is None:
self.report({"ERROR"}, translations("No text selected"))
return {"CANCELLED"}
if not text.filepath:
self.report({"ERROR"}, translations("Text not saved"))
success = save_text_with_browser(text, context)
self.report({"INFO"}, f"{success}")
return {"FINISHED"}
filepath = bpy.path.abspath(text.filepath)
if not os.path.exists(filepath):
self.report({"ERROR"}, f"File not found: {filepath}")
return {"CANCELLED"}
# 处理 VSCode 路径
vscode_path = bpy.path.abspath(vscode_path)
if os.path.isdir(vscode_path):
# 如果是目录,自动补全 Code.exe
candidate = os.path.join(vscode_path, "Code.exe")
if os.path.exists(candidate):
vscode_path = candidate
else:
self.report({"ERROR"}, f"VSCode path is a directory, Code.exe not found: {candidate}")
return {"CANCELLED"}
elif os.path.isfile(vscode_path):
# 如果是文件,直接用
if not vscode_path.lower().endswith("code.exe"):
self.report({"WARNING"}, f"The specified file is not Code.exe: {vscode_path}")
else:
# 如果路径不存在,尝试补全 Code.exe
candidate = vscode_path + "\\Code.exe"
if os.path.exists(candidate):
vscode_path = candidate
else:
self.report({"ERROR"}, f"Invalid VSCode path: {vscode_path}")
return {"CANCELLED"}
try:
# 调用 VSCode 打开文件
subprocess.Popen([vscode_path, filepath])
return {"FINISHED"}
except Exception as e:
self.report({"ERROR"}, f"Failed to launch VSCode: {str(e)}")
return {"CANCELLED"}
class SCRIPT_MANAGER_OT_add_preview_property(bpy.types.Operator):
bl_idname = "script_manager.add_preview_property"
bl_label = "Add Preview Property"
def execute(self, context):
prefs = context.scene.text_manager_prefs
prefs.preview_properties.add()
return {"FINISHED"}
class SCRIPT_MANAGER_OT_remove_preview_property(bpy.types.Operator):
bl_idname = "script_manager.remove_preview_property"
bl_label = "Remove Preview Property"
def execute(self, context):
prefs = context.scene.text_manager_prefs
if prefs.preview_properties:
prefs.preview_properties.remove(len(prefs.preview_properties) - 1)
return {"FINISHED"}
class SCRIPTMANAGER_OT_run_text(bpy.types.Operator):
bl_idname = "script_manager.run_text"
bl_label = "Run Text"
bl_description = "Run the selected Blender text"
text_name: StringProperty(name="Text Name")
def execute(self, context):
text = bpy.data.texts.get(self.text_name)
ok, msg = run_text_block(text)
self.report({"INFO"} if ok else {"ERROR"}, msg)
return {"FINISHED"} if ok else {"CANCELLED"}
class ScriptManagerMsgBus_OT_add_item(bpy.types.Operator):
bl_idname = "script_manager.msgbus_add_item"
bl_label = "Add MsgBus Item"
def execute(self, context):
prefs = context.scene.text_manager_prefs
item = prefs.msgbus_collection.add()
item.RNA_path = "RNA Path"
prefs.msgbus_index = len(prefs.msgbus_collection) - 1
return {"FINISHED"}
class ScriptManagerMsgBus_OT_remove_item(bpy.types.Operator):
bl_idname = "script_manager.msgbus_remove_item"
bl_label = "Remove MsgBus Item"
def execute(self, context):
prefs = context.scene.text_manager_prefs
# 如果没有选中项,就删除当前 active_index
idx = prefs.msgbus_index
if 0 <= idx < len(prefs.msgbus_collection):
if prefs.msgbus_collection[idx].is_registered:
self.report({"ERROR"}, translations("This item is not unregistered. Please unregister it before deleting."))
return {"CANCELLED"}
prefs.msgbus_collection.remove(idx)
prefs.msgbus_index = max(0, idx - 1)
return {"FINISHED"}
class ScriptManagerMsgBus_OT_move_item_up(bpy.types.Operator):
bl_idname = "script_manager.msgbus_move_item_up"
bl_label = "Move MsgBus Item Up"
def execute(self, context):
prefs = context.scene.text_manager_prefs
index = prefs.msgbus_index
if index > 0:
prefs.msgbus_index = index - 1
prefs.msgbus_collection.move(index, index - 1)
return {"FINISHED"}
class ScriptManagerMsgBus_OT_move_item_down(bpy.types.Operator):
bl_idname = "script_manager.msgbus_move_item_down"
bl_label = "Move MsgBus Item Down"
def execute(self, context):
prefs = context.scene.text_manager_prefs
index = prefs.msgbus_index
if index < len(prefs.msgbus_collection) - 1:
prefs.msgbus_index = index + 1
prefs.msgbus_collection.move(index, index + 1)
return {"FINISHED"}
class ScriptManagerMsgBus_OT_register_msgbus(bpy.types.Operator):
bl_idname = "script_manager.msgbus_register_msgbus"
bl_label = "Register MsgBus Handlers"
bl_options = {"REGISTER", "UNDO"} # 确保 REGISTER 以记录报告到状态栏
index: IntProperty(name="Index", default=0)
def execute(self, context):
pref = context.scene.text_manager_prefs
RNA_path = pref.msgbus_collection[self.index].RNA_path
text_name = pref.msgbus_collection[self.index].text_pointer.name if pref.msgbus_collection[self.index].text_pointer else ""
if RNA_path == "" or text_name == "":
self.report({"ERROR"}, f"Path or script cannot be empty: {RNA_path} - {text_name}")
return {"CANCELLED"}
valid_path, key = get_msgbus_key(RNA_path)
if RNA_path and valid_path and text_name != "":
self.report({"INFO"}, f"Register Trigger monitoring: {RNA_path} - {text_name}-key: {key}")
bpy.msgbus.subscribe_rna(
key=key,
owner=sys.intern(str(RNA_path).strip()), # 保证注册时和注销时是完全一致的对象
args=(),
notify=make_ScriptManagerMsgBus_update_callback(self.index),
)
return {"FINISHED"}
else:
self.report({"ERROR"}, f"Invalid path or the specified script is empty: {RNA_path}")
return {"CANCELLED"}
class ScriptManagerMsgBus_OT_unregister_msgbus(bpy.types.Operator):
bl_idname = "script_manager.msgbus_unregister_msgbus"
bl_label = "Unregister MsgBus Handlers"
RNA_path: StringProperty(name="RNA Path")
def execute(self, context):
if self.RNA_path != "":
# print(f"注销MsgBus监控: {self.RNA_path}")
bpy.msgbus.clear_by_owner(sys.intern(str(self.RNA_path).strip()))
self.report({"INFO"}, f"Unregister Trigger monitoring: {self.RNA_path}")
else:
pass
self.report({"ERROR"}, f"Invalid path: {self.RNA_path}")
return {"FINISHED"}
class SCRIPTMANAGER_OT_open_github(bpy.types.Operator):
bl_idname = "script_manager.open_github"
bl_label = "Open GitHub Page"
bl_description = "Open the GitHub repository for this addon"
def execute(self, context):
github_url = "https://github.com/bb-yi/Script_Manager"
webbrowser.open(github_url)
return {"FINISHED"}
classes = [
SCRIPTMANAGER_OT_remove_all_handlers,
SCRIPTMANAGER_OT_remove_addon_handlers,
SCRIPTMANAGER_OT_remove_item,
SCRIPTMANAGER_OT_remove_handler,
SCRIPTMANAGER_OT_add_item,
SCRIPTMANAGER_OT_move_item_up,
SCRIPTMANAGER_OT_move_item_down,
SCRIPTMANAGER_OT_new_text,
SCRIPTMANAGER_OT_run_text,
SCRIPTMANAGER_OT_open_in_vscode,
SCRIPT_MANAGER_OT_add_preview_property,
SCRIPT_MANAGER_OT_remove_preview_property,
ScriptManagerMsgBus_OT_add_item,
ScriptManagerMsgBus_OT_remove_item,
ScriptManagerMsgBus_OT_move_item_up,
ScriptManagerMsgBus_OT_move_item_down,
ScriptManagerMsgBus_OT_register_msgbus,
ScriptManagerMsgBus_OT_unregister_msgbus,
SCRIPTMANAGER_OT_open_github,
]
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in reversed(classes):
bpy.utils.unregister_class(c)