-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_manager.py
More file actions
287 lines (235 loc) · 9.93 KB
/
Copy pathscript_manager.py
File metadata and controls
287 lines (235 loc) · 9.93 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
import os
import bpy
from bpy.types import Operator
from bpy.props import StringProperty, EnumProperty
def get_scripts_dir():
"""获取脚本目录路径"""
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "bb_scripts")
def ensure_scripts_dir():
"""确保脚本目录存在"""
try:
scripts_dir = get_scripts_dir()
print(f"正在创建脚本目录: {scripts_dir}")
if not os.path.exists(scripts_dir):
os.makedirs(scripts_dir, exist_ok=True)
print(f"成功创建脚本目录: {scripts_dir}")
else:
print(f"脚本目录已存在: {scripts_dir}")
return scripts_dir
except Exception as e:
print(f"创建脚本目录失败: {str(e)}")
raise
def get_icons_dir():
"""获取图标目录路径"""
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "bb_icons")
def ensure_icons_dir():
"""确保图标目录存在"""
try:
icons_dir = get_icons_dir()
print(f"正在创建图标目录: {icons_dir}")
if not os.path.exists(icons_dir):
os.makedirs(icons_dir, exist_ok=True)
print(f"成功创建图标目录: {icons_dir}")
else:
print(f"图标目录已存在: {icons_dir}")
return icons_dir
except Exception as e:
print(f"创建图标目录失败: {str(e)}")
raise
def load_custom_icons():
"""加载自定义图标"""
icons_dir = get_icons_dir()
if not os.path.exists(icons_dir):
return []
icons = []
for file in os.listdir(icons_dir):
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
icon_name = os.path.splitext(file)[0].upper()
icon_path = os.path.join(icons_dir, file)
icons.append((icon_name, icon_path))
return icons
class BBSM_OT_NewScript(Operator):
bl_idname = "bbsm.new_script"
bl_label = "新建脚本"
script_name: StringProperty(
name="脚本名称",
default="new_script",
description="新脚本的名称"
)
group_name: StringProperty(
name="组名",
default="",
description="新建脚本所属的组名"
)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.prop(self, "script_name")
def execute(self, context):
try:
# 确保脚本目录存在
scripts_dir = ensure_scripts_dir()
# 根据组名确定保存路径
if not self.group_name or self.group_name == "根目录":
save_dir = scripts_dir
else:
save_dir = os.path.join(scripts_dir, self.group_name)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 创建脚本文件路径
script_path = os.path.join(save_dir, f"{self.script_name}.py")
# 检查文件是否已存在
if os.path.exists(script_path):
self.report({'ERROR'}, f"脚本 {self.script_name}.py 已存在")
return {'CANCELLED'}
# 创建脚本内容
script_content = f'''custom_name = "{self.script_name}"
# 脚本图标
icon_name = "SCRIPT"
'''
# 直接写入文件
with open(script_path, 'w', encoding='utf-8') as f:
f.write(script_content)
# 创建文本数据块并打开
text = bpy.data.texts.new(name=f"{self.script_name}.py")
text.write(script_content)
text.filepath = script_path
# 切换到文本编辑器
self.switch_to_text_editor(context, text.name)
# 刷新脚本列表
bpy.ops.bbsm.refresh_scripts()
group_info = f"在{self.group_name}组中" if self.group_name and self.group_name != "根目录" else "在根目录"
self.report({'INFO'}, f"脚本已创建: {self.script_name}.py {group_info}")
return {'FINISHED'}
except Exception as e:
self.report({'ERROR'}, f"创建脚本失败: {str(e)}")
return {'CANCELLED'}
def switch_to_text_editor(self, context, text_name):
"""切换到文本编辑器并打开指定文本"""
# 查找文本编辑器区域
text_editor = None
for area in context.screen.areas:
if area.type == 'TEXT_EDITOR':
text_editor = area
break
if not text_editor:
# 如果没有找到文本编辑器,分割3D视图区域
for area in context.screen.areas:
if area.type == 'VIEW_3D':
# 分割区域
bpy.ops.screen.area_split(direction='VERTICAL', factor=0.5)
# 获取新创建的区域
new_area = context.screen.areas[-1]
new_area.type = 'TEXT_EDITOR'
text_editor = new_area
break
if text_editor:
# 设置活动文本
for space in text_editor.spaces:
if space.type == 'TEXT_EDITOR':
space.text = bpy.data.texts[text_name]
space.show_line_numbers = True
space.show_syntax_highlight = True
break
class BBSM_OT_ImportScript(Operator):
bl_idname = "bbsm.import_script"
bl_label = "导入脚本"
filepath: StringProperty(
name="文件路径",
description="导入脚本的路径",
subtype='FILE_PATH'
)
filter_glob: StringProperty(
default="*.py",
options={'HIDDEN'}
)
target_group: StringProperty(
name="目标组",
description="导入脚本的目标组",
default=""
)
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
try:
if not self.filepath:
self.report({'ERROR'}, "未选择文件")
return {'CANCELLED'}
# 确保脚本目录存在
scripts_dir = ensure_scripts_dir()
# 根据目标组确定保存路径
if not self.target_group or self.target_group == "根目录":
save_dir = scripts_dir
else:
save_dir = os.path.join(scripts_dir, self.target_group)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 获取目标路径
filename = os.path.basename(self.filepath)
target_path = os.path.join(save_dir, filename)
# 检查是否已存在同名文件
if os.path.exists(target_path):
self.report({'ERROR'}, f"脚本 {filename} 已存在")
return {'CANCELLED'}
# 读取源文件内容
with open(self.filepath, 'r', encoding='utf-8') as f:
content = f.read()
# 添加必要的变量(如果不存在)
if 'custom_name' not in content:
script_name = os.path.splitext(filename)[0]
content = f'custom_name = "{script_name}"\n' + content
if 'icon_name' not in content:
content = 'icon_name = "SCRIPT"\n' + content
# 保存到目标目录
with open(target_path, 'w', encoding='utf-8') as f:
f.write(content)
# 创建新的文本数据块
text = bpy.data.texts.new(name=filename)
text.write(content)
text.filepath = target_path
# 切换到文本编辑器
self.switch_to_text_editor(context, text.name)
# 刷新脚本列表
bpy.ops.bbsm.refresh_scripts()
group_info = f"到{self.target_group}组" if self.target_group and self.target_group != "根目录" else "到根目录"
self.report({'INFO'}, f"脚本已导入: {filename} {group_info}")
return {'FINISHED'}
except Exception as e:
self.report({'ERROR'}, f"导入脚本失败: {str(e)}")
return {'CANCELLED'}
def switch_to_text_editor(self, context, text_name):
"""切换到文本编辑器并打开指定文本"""
# 查找文本编辑器区域
text_editor = None
for area in context.screen.areas:
if area.type == 'TEXT_EDITOR':
text_editor = area
break
if not text_editor:
# 如果没有找到文本编辑器,分割3D视图区域
for area in context.screen.areas:
if area.type == 'VIEW_3D':
# 分割区域
bpy.ops.screen.area_split(direction='VERTICAL', factor=0.5)
# 获取新创建的区域
new_area = context.screen.areas[-1]
new_area.type = 'TEXT_EDITOR'
text_editor = new_area
break
if text_editor:
# 设置活动文本
for space in text_editor.spaces:
if space.type == 'TEXT_EDITOR':
space.text = bpy.data.texts[text_name]
space.show_line_numbers = True
space.show_syntax_highlight = True
break
# 注册和注销函数
def register():
bpy.utils.register_class(BBSM_OT_NewScript)
bpy.utils.register_class(BBSM_OT_ImportScript)
def unregister():
bpy.utils.unregister_class(BBSM_OT_NewScript)
bpy.utils.unregister_class(BBSM_OT_ImportScript)