-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippetsLibrary_279.py
473 lines (391 loc) · 15.6 KB
/
snippetsLibrary_279.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
bl_info = {
"name": "snippets library",
"description": "Add a library list to quickly load/save personnal texts snippets from text editor",
"author": "Samuel Bernou",
"version": (0, 0, 2),
"blender": (2, 78, 0),
"location": "Text editor > toolbar",
"warning": "",
"wiki_url": "",
"category": "Text Editor" }
import bpy, os
import textwrap
from bpy.props import IntProperty, CollectionProperty #, StringProperty
from bpy.types import Panel, UIList
def get_addon_prefs():
#addon_name = os.path.splitext(os.path.basename(os.path.abspath(__file__)))[0]
# -> same as __name__
user_preferences = bpy.context.user_preferences
addon_prefs = user_preferences.addons[__name__].preferences
return (addon_prefs)
def openFolder(folderpath):
"""
open the folder at the path given
with cmd relative to user's OS
"""
from sys import platform
myOS = platform
if myOS.startswith('linux') or myOS.startswith('freebsd'):
# linux
cmd = 'xdg-open '
#print("operating system : Linux")
elif myOS.startswith('win'):
# Windows
#cmd = 'start '
cmd = 'explorer '
folderpath = folderpath.rstrip(r'\/')#kill end slash if exists
#print("operating system : Windows")
if not folderpath:
return('/')
else:#elif myOS == "darwin":
# OS X
#print("operating system : MACos")
cmd = 'open '
if not folderpath:
print ('in openFolder : no folderpath !', folderpath)
return('//')
#double quote the path to avoid problem with special character
folderpath = '"' + folderpath + '"'
fullcmd = cmd + folderpath
#print & launch open command
print(fullcmd)
os.system(fullcmd)
def locateLibrary(justGet=False):
#addon = bpy.context.user_preferences.addons.get('snippetsLibrary')
# print (addon)
# prefs = addon.preferences
prefs = get_addon_prefs()
cust_path = prefs.snippets_custom_path
cust_fp = prefs.snippets_filepath
if cust_path:#specified user location
if cust_fp:
snipDir = bpy.path.abspath(cust_fp)
else:
print ('!!! error with Custom file path (or empty), reading blend_directory')
snipDir = (bpy.data.filepath)
else:#default location (addon folders "snippets" subdir)
script_file = os.path.realpath(__file__)
directory = os.path.dirname(script_file)
snipDir = os.path.join(directory, 'snippets/')
if not os.path.exists(snipDir):
try:
os.mkdir(snipDir)
print('snippets directory created at:', snipDir)
except:
print('!!! could not create snippets directory created at:', snipDir)
if justGet:
return(snipdir)
else:
if os.path.exists(snipDir):
if os.path.isdir(snipDir):
return(snipDir)
else:
return (os.path.split(snipDir)[0])
else:
print('error with location:', snipDir)
return (0)
def insert_template(override, src_text):
bpy.ops.text.insert(override, text=src_text)
def reload_folder(fp):
'''take a filepath (location of the files) and return a list of filenames'''
#recursive in folder
snippetsList = []
for root, dirs, files in os.walk(fp, topdown=True):
for f in files:
if f.endswith('.txt') or f.endswith('.py'):
snippetsList.append(os.path.splitext(os.path.basename(f))[0])
return (snippetsList)
def load_text(fp):
if fp:
with open(fp, 'r') as fd:
text=fd.read()
return text
else:
print('in load_text: no fp to read !')
return(0)
def save_template(fp, name, text):
if not name.endswith(('.txt', '.py')):
name = name + '.txt'
fd = open(os.path.join(fp, name),'w')
fd.write(text)
fd.close()
return 0
def get_snippet(name):
'''take a name
and return a filepath if found in library
'''
library = locateLibrary()
if library:
for root, dirs, files in os.walk(library, topdown=True):#"."
for f in files:
if name.lower() == os.path.splitext(f)[0].lower():
return(os.path.join(root, f))
print('in get_snippet: not found >', name)
return (0)
else:
return(1)
def clipit(context):
library = locateLibrary()
bpy.ops.text.copy()
clip = bpy.context.window_manager.clipboard
if clip:
#print (clip)
###kill preceding spaces before saving (allow to copy at indentation 0)
clip = textwrap.dedent(clip)
if context.scene.new_snippets_name:
snipname = context.scene.new_snippets_name + '.txt'
else:#generate Unique snipName
from random import randrange
import time
snipname = 'snip'+ str(randrange(999)) + time.strftime("_%Y-%m-%d_%H-%M-%S") +'.txt'
save_template(library, snipname, clip)
return (snipname)
else:
return (0)
###--- UI List items
# ui list item actions
class Uilist_actions(bpy.types.Operator):
bl_idname = "sniptool.list_action"
bl_label = "List Action"
action = bpy.props.EnumProperty(
items=(
('UP', "Up", ""),
('DOWN', "Down", ""),
('REMOVE', "Remove", ""),
('ADD', "Add", ""),
)
)
def invoke(self, context, event):
scn = context.scene
idx = scn.sniptool_index
try:
item = scn.sniptool[idx]
except IndexError:
pass
else:
if self.action == 'DOWN' and idx < len(scn.sniptool) - 1:
item_next = scn.sniptool[idx+1].name
scn.sniptool_index += 1
info = 'Item %d selected' % (scn.sniptool_index + 1)
self.report({'INFO'}, info)
elif self.action == 'UP' and idx >= 1:
item_prev = scn.sniptool[idx-1].name
scn.sniptool_index -= 1
info = 'Item %d selected' % (scn.sniptool_index + 1)
self.report({'INFO'}, info)
elif self.action == 'REMOVE':
info = 'Item %s removed from list' % (scn.sniptool[scn.sniptool_index].name)
scn.sniptool_index -= 1
self.report({'INFO'}, info)
scn.sniptool.remove(idx)
if self.action == 'ADD':
###---mypart
snipname = clipit(context)
if snipname:
item = scn.sniptool.add()
item.id = len(scn.sniptool)
item.name = os.path.splitext(snipname)[0]
scn.sniptool_index = (len(scn.sniptool)-1)
info = '%s added to list' % (item.name)
self.report({'INFO'}, info)
else:
self.report({'warning'}, 'nothing selected')
return {"FINISHED"}
# -------------------------------------------------------------------
# draw
# -------------------------------------------------------------------
### addon preferences panel
class snippetsPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
snippets_custom_path = bpy.props.BoolProperty(
name='Use custom path',
description="Set a cutom directory for snippets library",
default=False)
snippets_filepath = bpy.props.StringProperty(
name="Snippets folder",
subtype='FILE_PATH',
)
def draw(self, context):
layout = self.layout
layout.prop(self, "snippets_custom_path")
layout.label(text="Snippets will be saved as invidual files")
layout.label(text="in a folder named 'snippets' (created at first use)")
layout.label(text="located aside the addon file (unless you enter a custom path)")
if self.snippets_custom_path:
#layout.label(text="Leave the field empty to get default location")#"Custom path to you text load/save folder\n"
layout.prop(self, "snippets_filepath")
layout.label(text="May not work if space are in path.")
# sniptool list
class UL_items(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
#split = layout.split(0.3)
##add to draw index (useless)
#split.label("%d" % (index))
#split.prop(item, "name", text="", emboss=False, translate=False, icon='WORDWRAP_ON')
##delete icon to remove sheets icons (alsouseless)
layout.prop(item, "name", text="", emboss=False, translate=False, icon='WORDWRAP_ON')
def invoke(self, context, event):
pass
# draw the panel
class UIListPanelExample(Panel):
"""Creates a Panel in the Object properties window"""
bl_idname = 'OBJECT_PT_my_panel'
bl_space_type = "TEXT_EDITOR"
bl_region_type = "UI"
bl_label = "Snippets List"
bpy.types.Scene.new_snippets_name = bpy.props.StringProperty(description='name that snippets will take, name will be generated')
def draw(self, context):
layout = self.layout
scn = bpy.context.scene
rows = 2
row = layout.row()
row.operator("sniptool.reload_list", icon="FILE_REFRESH")
row = layout.row()
row.template_list("UL_items", "", scn, "sniptool", scn, "sniptool_index", rows=rows)
col = row.column(align=True)
# col.operator("sniptool.list_action", icon='ZOOMIN', text="").action = 'ADD'
# col.operator("sniptool.list_action", icon='ZOOMOUT', text="").action = 'REMOVE'
col.separator()
col.operator("sniptool.list_action", icon='TRIA_UP', text="").action = 'UP'
col.operator("sniptool.list_action", icon='TRIA_DOWN', text="").action = 'DOWN'
row = layout.row()
col = row.column(align=True)
col.operator("sniptool.template_insert", icon="LIBRARY_DATA_DIRECT")#LIBRARY_DATA_DIRECT RIGHTARROW FORWARD
col.separator()
col.prop(context.scene, 'new_snippets_name', text='snippets name')
col.operator("sniptool.save_snippet", icon="SAVE_COPY")
col.operator("sniptool.open_snippet_folder", icon="FILE_FOLDER")
# col.separator()
class Uilist_saveSnippet(bpy.types.Operator):
bl_idname = "sniptool.save_snippet"
bl_label = "save snippet"
bl_description = "save selection to a file named after this"
def execute(self, context):
scn = context.scene
library = locateLibrary()
if library:
snipname = clipit(context)
if snipname:
item = scn.sniptool.add()
item.id = len(scn.sniptool)
item.name = os.path.splitext(snipname)[0]
scn.sniptool_index = (len(scn.sniptool)-1)
info = '%s added to list' % (item.name)
self.report({'INFO'}, info)
else:
self.report({'warning'}, 'nothing selected')
###print all snipsauce :
#for i in scn.sniptool:
# print (i.name, i.id)
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
# insert button
class Uilist_insertTemplate(bpy.types.Operator):
bl_idname = "sniptool.template_insert"
bl_label = "insert List Item"
bl_description = "insert Item in textBlock"
def execute(self, context):
scn = context.scene
if locateLibrary():
text = getattr(bpy.context.space_data, "text", None)
if text:
pass
#print(text.name)
else:
pass
#print('no text data')
#context override for the ops.text.insert() function
override = {'window': context.window,
'area' : context.area,
'region': context.region,
'space': context.space_data,
'edit_text' : text
}
snip = scn.sniptool[scn.sniptool_index].name
Loaded_text = load_text(get_snippet(snip))#load_text(r'G:\WORKS\Prog\blender\SB Blender addons\DEV_SamTool\generic name.txt')
#get character position in text
charPos = text.current_character
#print ('charPos', charPos)
#indentedText = Loaded_text
if Loaded_text:
FormattedText = Loaded_text
#indent text lines exept first (already at cursor position)
if charPos > 0:
textLines = Loaded_text.split('\n')
if not len(textLines) == 1:
#print("indent subsequent lines")
indentedLines = []
indentedLines.append(textLines[0])
for line in textLines[1:]:
indentedLines.append(' '*charPos + line)
FormattedText = '\n'.join(indentedLines)
# print(FormattedText)
insert_template(override, FormattedText)
else:
print('Fail to load snippet !')
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
# relaod button
class Uilist_reloadItems(bpy.types.Operator):
bl_idname = "sniptool.reload_list"
bl_label = "Reload List"
bl_description = "Reload all items in the list"
def execute(self, context):
scn = context.scene
lst = scn.sniptool
current_index = scn.sniptool_index
library = locateLibrary()
if library:
allsnip = reload_folder(library)
if len(lst) > 0:#remove all item in list
# reverse range to remove last item first
for i in range(len(lst)-1,-1,-1):
scn.sniptool.remove(i)
#self.report({'INFO'}, "All items removed")
for snipname in allsnip:#populate list
item = scn.sniptool.add()
item.id = len(scn.sniptool)
item.name = snipname
scn.sniptool_index = (len(scn.sniptool)-1)
#info = '%s added to list' % (item.name)
# else:
# self.report({'INFO'}, "Nothing to add")
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
class OpenSnippetsFolder(bpy.types.Operator):
bl_idname = "sniptool.open_snippet_folder"
bl_label = "open library folder"
bl_description = "open snippets folder location"
def execute(self, context):
scn = context.scene
library = locateLibrary()
if library:
#open folder
openFolder(library)
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
# Create sniptool property group
class sniptoolProp(bpy.types.PropertyGroup):
'''name = StringProperty() '''
id = IntProperty()
# -------------------------------------------------------------------
# register
# -------------------------------------------------------------------
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.sniptool = CollectionProperty(type=sniptoolProp)
bpy.types.Scene.sniptool_index = IntProperty()
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.sniptool
del bpy.types.Scene.sniptool_index
if __name__ == "__main__":
register()