-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Alias.py
More file actions
1186 lines (922 loc) · 34.6 KB
/
spb_Alias.py
File metadata and controls
1186 lines (922 loc) · 34.6 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This script helps managing aliases.
"""
#! python 2 Must be on a line less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
161209: Created.
...
180729: Reabled the use of rs.OpenFileNames since the bug fix in Rhino 6.5.
...
211103: Added export.
220601: Added EditLines.
220602: Bug fix.
220917: Added ExportDefault and OpenAliasFolder options. Modified a printed output.
231217: Now, output of Compare2Txt is sorted.
231222: Now, 2 alias .txt can be immediately selected in the file dialog and used.
Added option to export ListPerSubstring to file.
editMacro no longer asks for confirmation when only 1 macro is edited.
250210,13: Fixed codecs-related problems due to behavior of Python's readline vs. Rhino's _-Options _Aliases _Export.
250422: Some alias name/macro searches are now comma delimited AND.
250814: Refactored and added a couple of options.
250919: Bug fix.
"""
import Rhino
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import codecs
import re
CAL = Rhino.ApplicationSettings.CommandAliasList
_sCmdPrefix = 'x'
_sDefaultPyFolderPath = "C:\\My\\Rhino\\PythonScript"
_sDefaultAliasExportFolder = "C:\\My\\Rhino\\Aliases"
_ss_CommonMacroText = (
"noecho",
"loadscript",
"runpythonscript",
"runscript",
"scripteditor",
)
_sStringForAll = "__AllValues__"
_sOptions = (
'Count',
'AddPy',
'AddLines',
'DelLines',
'DelListPick',
'EditLines',
'EditAlias',
'EditMacro',
'ListPerSubstring',
'ListRunScript',
'ListRunPythonScript',
'ListPyNotFound',
'ListDupMacros',
'ReassignPy',
'Compare2Txt',
'ExportDefault',
'ExportBrowse',
'OpenAliasFolder',
)
def getOption():
idxOpt_Default = 0
go = ri.Custom.GetOption()
go.SetCommandPrompt('Alias')
go.SetCommandPromptDefault(defaultValue=_sOptions[idxOpt_Default])
go.AcceptNothing(True)
# for idxOpt of 1 - 5, not 0.
for sDirection in _sOptions:
go.AddOption(sDirection)
# for s in listPropOpts: gs.AddOption(s)
res = go.Get()
if res == ri.GetResult.Nothing:
idxOpt = idxOpt_Default
elif res == ri.GetResult.Option:
idxOpt = go.Option().Index - 1 # '- 1' because go.Option().Index is base 1.
else:
return
go.Dispose()
Rhino.RhinoApp.SetCommandPrompt("Alias")
return _sOptions[idxOpt]
def fileBaseName(sFileFullPath):
# Get full file name from full path.
sSplit = sFileFullPath.split('\\')
if len(sSplit) == 0: return
sScriptFileFullName = sSplit[-1]
# Remove '.' and extension.
sSplit = sScriptFileFullName.split('.')
if len(sSplit) != 2: return
return sSplit[0]
def addAlias(sScriptFileBaseName, bOverwrite=False):
"""
"""
sTitle = "Add Alias"
if sScriptFileBaseName[:4] == 'spb_':
sAlias = _sCmdPrefix + sScriptFileBaseName[4].capitalize() + sScriptFileBaseName[5:]
elif sScriptFileBaseName[0] != _sCmdPrefix:
# Add sCmdPrefix to beginning of command to distinguish them from Rhino commands, etc.
sAlias = _sCmdPrefix + sScriptFileBaseName[0].capitalize() + sScriptFileBaseName[1:]
else:
sAlias = sScriptFileBaseName[:]
sAlias = rs.StringBox("Alias to add", sAlias, sTitle)
if sAlias is None: return False
if CAL.IsAlias(sAlias):
sMacro = CAL.GetMacro(sAlias)
nMsgBox = rs.MessageBox("{} exists with command macro\n{}\n\nReplace?".format(
sAlias, sMacro), 3, sTitle)
if nMsgBox == 2: return None
elif nMsgBox == 7:
print("{} was not modified.".format(sAlias))
return False
sMacro = "_NoEcho ! _-RunPythonScript " + sScriptFileBaseName
sMacro = rs.StringBox("Macro for alias", sMacro, sTitle)
if sMacro is None:
print("{} was skipped.".format(sAlias))
return False
if CAL.Add(sAlias, sMacro):
print("{} alias created with command macro {}".format(sAlias, sMacro))
return sAlias, sMacro
else:
print("Error in creating alias for {}".format(sAlias))
def addAliases():
"""
"""
# Placed in try due to 'External component has thrown an exception.' from OpenFileNames.
try:
sFileFullPaths = rs.OpenFileNames(
title="Add Alias",
filter="python scripts|*.py||",
folder=_sDefaultPyFolderPath,
)
if len(sFileFullPaths) == 0: return
except:
return
sAliases = []
sMacros = []
for sFileFullPath in sFileFullPaths:
sScriptFileBaseName = fileBaseName(sFileFullPath)
if sScriptFileBaseName is None:
print("Error in understanding {} Exiting...".format(sFileFullPath))
return
rc = addAlias(sScriptFileBaseName, bOverwrite=True)
if rc is None:
print("Script canceled.")
return
elif rc is False:
continue
sAlias, sMacro = rc
if rc is None:
continue
else:
sAlias, sMacro = rc
sAliases.append(sAlias)
sMacros.append(sMacro)
if sAliases:
return sAliases, sMacros
def addLines_PerAliasExportFormat():
"""
"""
sTitle = "Add Aliases in Alias Export format"
bSuccess, sMultiInput = Rhino.UI.Dialogs.ShowEditBox(
title=sTitle,
message="Enter alias data in Alias Export format",
defaultText=None,
multiline=True)
if not bSuccess: return
for sSingleLine in sMultiInput.splitlines(keepends=False):
sAlias, sMacro = sSingleLine.split(sep=' ', maxsplit=1)
if sAlias is None:
print("Format for {} is incorrect. Alias name could not be determined.".format(
sSingleLine))
continue
if sMacro is None:
print("Format for {} is incorrect. Marco could not be determined.".format(
sSingleLine))
continue
if CAL.Add(sAlias, sMacro):
print("'{}' alias created with command macro '{}'".format(sAlias, sMacro))
else:
print("Error in creating alias'{}'.".format(sAlias))
def _constructAliasLines_All():
sNames = list(CAL.GetNames())
sMacros = [CAL.GetMacro(s) for s in sNames]
sLines_Starting = []
for i in xrange(len(sNames)):
sLine = sNames[i] + ' ' + sMacros[i]
sLines_Starting.append(sLine)
if not sLines_Starting:
print("No aliases!")
return
return sLines_Starting
def _splitLineTo_AliasName_and_Macro(sLine):
if ' ' not in sLine:
print("Line has no spaces.")
return
return sLine.split(sep=' ', maxsplit=1)
def deleteLines_PerAliasExportFormat():
"""
"""
sTitle = "Delete Aliases in Alias Export format"
bSuccess, sMultiInput = Rhino.UI.Dialogs.ShowEditBox(
title=sTitle,
message="Enter alias data in Alias Export format",
defaultText=None,
multiline=True)
if not bSuccess: return
sLines_ToDel = sMultiInput.splitlines(keepends=False)
sLines_Starting = _constructAliasLines_All()
if not sLines_Starting: return
sLines_Deleted = []
for sLine_ToDel in sLines_ToDel:
if sLine_ToDel in sLines_Starting:
rc = _splitLineTo_AliasName_and_Macro(sLine_ToDel)
if not rc: continue
sAlias, sMacro = rc
if CAL.Delete(alias=sAlias):
print("Deleted {}".format(sLine_ToDel))
sLines_Deleted.append(sLine_ToDel)
else:
print("{} not in aliases.".format(sLine_ToDel))
if len(sLines_Deleted) == len(sLines_ToDel):
print("Deleted all {} alias/macro lines.".format(len(sLines_Deleted)))
else:
print("Deleted {} out of {} intended alias/macro lines.".format(
len(sLines_Deleted), len(sLines_ToDel)))
def _replace_string_case_insensitive(text, old_string, new_string):
""" From Gemini."""
return re.sub(re.escape(old_string), new_string, text, flags=re.IGNORECASE)
def _areAllStringsInText(text, strings):
for string in strings:
if not string.lower() in text.lower():
return False
return True
def _removeCommonMacroTextFromString(s_In):
s_Out = s_In
for s_ToRemove in _ss_CommonMacroText:
s_Out = _replace_string_case_insensitive(s_Out, s_ToRemove, "")
return s_Out
def getSubstringToSearch(sTitle, bAllOpt=False):
"""
"""
if bAllOpt:
sSubstring = rs.StringBox(
message="Enter substring to find"
"\n\nSearch is case insensitive."
"\nUse commas to delimit strings for an AND search."
"\nNoEcho, RunPythonScript, etc., are ignored."
"\n{} will include all strings.".format(_sStringForAll)
,
default_value=_sStringForAll,
title=sTitle)
else:
sSubstring = rs.StringBox(
message="Enter substring to find",
default_value=None,
title=sTitle)
return sSubstring
def getAliasMacroLines(sSubstring_toInclude=None, bIgnoreCommonMacroText=True):
"""
"""
sLines = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
if sSubstring_toInclude == _sStringForAll:
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
sLines.append(sLine)
if not sLines:
print("No aliases.")
return
print("Found {} aliases.".format(len(sLines)))
return sLines
ssSearchSubString = sSubstring_toInclude.split(',')
def addLine(sName, sMacro):
sLine = sName + ' ' + sMacro
sLines.append(sLine)
if bIgnoreCommonMacroText:
for sName in sNames:
sMacro = CAL.GetMacro(sName)
if _areAllStringsInText(sName, ssSearchSubString):
addLine(sName, sMacro)
continue
sMacro_Cleaned = _removeCommonMacroTextFromString(sMacro)
if _areAllStringsInText(sMacro_Cleaned, ssSearchSubString):
addLine(sName, sMacro)
continue
else:
for sName in sNames:
sMacro = CAL.GetMacro(sName)
if _areAllStringsInText(sName, ssSearchSubString):
addLine(sName, sMacro)
continue
if _areAllStringsInText(sMacro, ssSearchSubString):
addLine(sName, sMacro)
continue
if not sLines:
print("Substring not found.")
return
print("Found {} aliases with substring {}.".format(
len(sLines),
sSubstring_toInclude))
return sLines
def getAliasMacroLines_AllWithSubstring(sSubstring="script"):
"""
sSubstring: ',' in sSubstring will delimit the string.
Unlike another function in this script, macros to check do not have 'RunPythonScript', etc., removed.
"""
sLines = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
ssSearchSubString = sSubstring.split(',')
# Only include macro lines with entered substring.
for sName in sNames:
sMacro = CAL.GetMacro(sName)
if _areAllStringsInText(sName, ssSearchSubString):
pass
elif _areAllStringsInText(sMacro, ssSearchSubString):
pass
else:
continue
sLine = sName + ' ' + sMacro
sLines.append(sLine)
if not sLines:
print("Substring not found.")
return
print("Found {} aliases with substring {}.".format(
len(sLines),
sSubstring))
return sLines
def editLines():
"""
"""
sTitle = "Edit Aliase/Macro Lines"
sSubstring_toInclude = getSubstringToSearch(sTitle, bAllOpt=False)
if sSubstring_toInclude is None: return
sLines = getAliasMacroLines(
sSubstring_toInclude=sSubstring_toInclude,
bIgnoreCommonMacroText=True)
if not sLines: return
bSuccess, sMultiLines_FromEdit = Rhino.UI.Dialogs.ShowEditBox(
title=sTitle,
message="Edit alias data per Alias Export format",
defaultText='\n'.join(sLines),
multiline=True)
if not bSuccess: return
sLines_FromEdit = sMultiLines_FromEdit.splitlines(keepends=False)
sAliasNames_BeforeEdit = list(CAL.GetNames())
sLines_All_BeforeEdit = _constructAliasLines_All()
if not sLines_All_BeforeEdit: return
sLines_Edited = []
for sLine_ToAdd in sLines_FromEdit:
if sLine_ToAdd in sLines_All_BeforeEdit:
# No change.
continue
rc = _splitLineTo_AliasName_and_Macro(sLine_ToAdd)
if not rc: continue
sAlias, sMacro = rc
if CAL.Add(sAlias, sMacro):
if sAlias in sAliasNames_BeforeEdit:
print("Modified {}".format(sLine_ToAdd))
else:
print("Added {}".format(sLine_ToAdd))
sLines_Edited.append(sLine_ToAdd)
else:
print("{} not added.".format(sLine_ToAdd))
print("Edited {} out of {} alias/macro lines.".format(
len(sLines_Edited), len(sLines_FromEdit)))
def editAlias():
"""
"""
sTitle = "Edit Alias Name"
sSearchSubString = rs.StringBox(
message="Enter substring to find or Enter (or Cancel) to list all",
default_value=None,
title=sTitle)
sItems = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
sItems.append(sLine)
if sSearchSubString is not None:
sItems_WIP = []
for sItem in sItems:
if sSearchSubString.lower() in sItem.lower():
sItems_WIP.append(sItem)
if not sItems_WIP:
print("Substring not found.")
return
sItems = sItems_WIP
sItemsSelected = rs.MultiListBox(
items=sItems,
message="Pick aliases to edit",
title=sTitle,
defaults=None)
if sItemsSelected is None: return
for sItem in sItemsSelected:
sAliasName_Sel = sItem.split(sep=' ')[0]
sAliasName_New = rs.StringBox(
message="Edit alias",
default_value=sAliasName_Sel,
title=sTitle)
if not sAliasName_New: return
if sAliasName_New == sAliasName_Sel: continue
iMbReturn = rs.MessageBox(
message="Replace alias\n{}\nwith\n{}\n?".format(
sAliasName_Sel,
sAliasName_New),
buttons=3, title='')
if iMbReturn == 2:
# Cancel
return
elif iMbReturn == 6:
# Yes
sMacro = CAL.GetMacro(alias=sAliasName_Sel)
if sMacro:
if CAL.Add(alias=sAliasName_New, macro=sMacro):
if CAL.Delete(alias=sAliasName_Sel):
print("Alias {} replaced with {}.".format(
sAliasName_Sel,
sAliasName_New))
else:
print("Alias {} macro WAS NOT DELETED (PHASE 1 OF 2 OF EDIT).".format(
sAliasName_Sel))
else:
print("Alias {} macro WAS NOT ADDED (PHASE 1 OF 2 OF EDIT).".format(
sAliasName_New))
elif iMbReturn == 7:
# No
continue
def editMacro():
"""
"""
sTitle = "Edit Alias Macro"
sSearchSubString = rs.StringBox(
message="Enter substring to find or Enter (or Cancel) to list all",
default_value=None,
title=sTitle)
sItems = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
sItems.append(sLine)
if sSearchSubString is not None:
sItems_WIP = []
for sItem in sItems:
if sSearchSubString.lower() in sItem.lower():
sItems_WIP.append(sItem)
if not sItems_WIP:
print("Substring not found.")
return
sItems = sItems_WIP
if len(sItems) > 1:
sItemsSelected = rs.MultiListBox(
items=sItems,
message="Pick aliases to edit their macros",
title=sTitle,
defaults=None)
if sItemsSelected is None: return
else:
sItemsSelected = sItems
for sItem in sItemsSelected:
sAliasName_Sel = sItem.split(sep=' ')[0]
sMacro_Old = CAL.GetMacro(alias=sAliasName_Sel)
sMacro_New = rs.StringBox(
message="Edit macro",
default_value=sMacro_Old,
title=sTitle)
if not sMacro_New: return
if sMacro_New == sMacro_Old: continue
if len(sItemsSelected) == 1:
iMbReturn = 6
else:
iMbReturn = rs.MessageBox(
message="Replace macro\n{}\nwith\n{}\n?".format(
sMacro_Old,
sMacro_New),
buttons=3, title='')
if iMbReturn == 2:
# Cancel
return
elif iMbReturn == 6:
# Yes
sMacro_Prev = CAL.GetMacro(alias=sAliasName_Sel)
if not CAL.SetMacro(alias=sAliasName_Sel, macro=sMacro_New):
sMacro_Prev = None
if sMacro_Prev == sMacro_Old:
print("Alias {} macro {} replaced with {}.".format(
sAliasName_Sel,
sMacro_Prev,
CAL.GetMacro(alias=sAliasName_Sel)))
else:
print("Alias {} macro WAS NOT EDITED.".format(sAliasName_Sel))
elif iMbReturn == 7:
# No
continue
def delete_Single_OLD():
"""
"""
sTitle = "Delete Alias"
sSearchSubString = rs.StringBox(
message="Enter substring to find or Enter (or Cancel) to list all",
default_value=None,
title=sTitle)
sItems = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
sItems.append(sLine)
if sSearchSubString is not None:
sItems_WIP = []
for sItem in sItems:
if sSearchSubString.lower() in sItem.lower():
sItems_WIP.append(sItem)
if not sItems_WIP:
print("Substring not found.")
return
sItems = sItems_WIP
sItemsSelected = rs.MultiListBox(
items=sItems,
message="Pick aliases to delete",
title=sTitle,
defaults=None)
if sItemsSelected is None: return
for sItem in sItemsSelected:
sAliasName_Sel = sItem.split(sep=' ')[0]
iMbReturn = rs.MessageBox(
message="Delete alias?:\n{}".format(sAliasName_Sel),
buttons=3, title='')
if iMbReturn == 2:
# Cancel
return
elif iMbReturn == 6:
# Yes
if CAL.Delete(alias=sAliasName_Sel):
print("Alias {} was deleted.".format(sAliasName_Sel))
else:
print("Alias {} COULD NOT BE DELETED.".format(sAliasName_Sel))
elif iMbReturn == 7:
# No
continue
def deletePickedFromList(sLines, sTitle=None):
"""
"""
rc = Rhino.UI.Dialogs.ShowMultiListBox(
title=sTitle,
message="Select macros to delete",
items=sLines,
defaults=None)
if rc is None:
for s in sLines:
print(s)
return
sLines_toDelete = list(rc)
sLines_Deleted = []
for sLine_toDelete in sLines_toDelete:
sName = sLine_toDelete.split(" ",1)[0]
#print(sName
if CAL.Delete(alias=sName):
print("Deleted {}".format(sLine_toDelete))
sLines_Deleted.append(sLine_toDelete)
return sLines_Deleted
def exportCustomList(sExport):
import os
sDesktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
sFilePath_Out = rs.SaveFileName(
title="Save custom list",
filter=None,
folder=sDesktop,
filename=None,
extension="txt")
if not sFilePath_Out: return
with open(sFilePath_Out, 'w') as f:
f.write('\n'.join(sExport))
def listAliasesMacrosWithSubstring(sSubstring_toInclude=None, bIgnoreCommonMacroText=True):
"""
"""
if sSubstring_toInclude is None:
sSubstring_toInclude = getSubstringToSearch("List Aliases", bAllOpt=True)
if sSubstring_toInclude is None: return
sLines = getAliasMacroLines(
sSubstring_toInclude=sSubstring_toInclude,
bIgnoreCommonMacroText=bIgnoreCommonMacroText,
)
if not sLines: return
for sLine in sLines:
print(sLine)
if len(sLines) < CAL.Count:
print("{} alias/macro lines listed out of {} total.".format(len(sLines), CAL.Count))
else:
print("All {} alias/macro lines listed.".format(len(sLines)))
if len(sLines) > 100:
sDecision = rs.GetString("Export to text file?", defaultString="No", strings=["Yes", "No"])
if sDecision and sDecision.lower()[0] == 'y':
exportCustomList(sLines)
def listAliasesMacrosWithSubstringForDelete():
"""
"""
sTitle = "List Aliases for Delete"
sSearchSubString = rs.StringBox(
message="Enter substring to find or Enter (or Cancel) to list all",
default_value=_sStringForAll,
title=sTitle)
if sSearchSubString is None: return
sLines = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
if sSearchSubString == _sStringForAll:
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
sLines.append(sLine)
if not sLines:
print("No aliases.")
return
print("Found {} aliases.".format(len(sLines)))
else:
# Only include macro lines with entered substring.
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
if sSearchSubString.lower() in sLine.lower():
sLines.append(sLine)
if not sLines:
print("Substring not found.")
return
print("Found {} aliases with substring {}.".format(
len(sLines),
sSearchSubString))
rc = deletePickedFromList(sLines, sTitle=sTitle)
if rc: return
def listPythonScriptsNotFound():
"""
"""
sTitle = "List Aliases Referencing Python Scripts Not Found"
import os
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
list_sMacroLines_with_files_not_found = []
for sName in sNames:
sMacro = CAL.GetMacro(sName)
if 'RunPythonScript' in sMacro:
sLine = sName + ' ' + sMacro
#print(sLine
sSplit = sMacro.split("RunPythonScript ",1)
if len(sSplit) < 2:
continue
sAfterRunPythonScript = sSplit[1]
sScriptNameOrPath = sAfterRunPythonScript.split(" ",1)[0]
if '"' in sScriptNameOrPath:
if sScriptNameOrPath[-1] != '"':
continue
sScriptNameOrPath = sScriptNameOrPath[:-1] + '.py"'
pass
elif sScriptNameOrPath[-3:] != '.py':
sScriptNameOrPath += '.py'
else:
pass
if not os.path.isfile(sScriptNameOrPath):
#print(sScriptNameOrPath
list_sMacroLines_with_files_not_found.append(sLine)
if not list_sMacroLines_with_files_not_found:
print("No python scripts with missing files.")
return
print("Found {} aliases with Python scripts not found.".format(
len(list_sMacroLines_with_files_not_found)))
deletePickedFromList(list_sMacroLines_with_files_not_found, sTitle=sTitle)
def listDuplicateMacros():
"""
"""
sTitle = "List and Optionally Delete Aliases with Macro Repeats"
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
sMacros = [CAL.GetMacro(s) for s in sNames]
sMacros_Repeats = []
sMacros_WithoutRepeats = list(set(sMacros))
if len(sMacros_WithoutRepeats) == len(sMacros):
print("No duplicate macros.")
return
sMacros_WithoutRepeats.sort(key=str.lower)
for i in xrange(len(sMacros_WithoutRepeats)):
sMacro = sMacros_WithoutRepeats[i]
if sMacros.count(sMacro) > 1:
if sMacro not in sMacros_Repeats:
sMacros_Repeats.append(sMacro)
sLines_with_RepeatMacros = []
for sMacro_Repeat in sMacros_Repeats:
for i in xrange(len(sNames)):
sMacro = sMacros[i]
if sMacro == sMacro_Repeat:
sLine = sNames[i] + ' ' + sMacro
sLines_with_RepeatMacros.append(sLine)
print("Found {} aliases with repeat macros.".format(len(sLines_with_RepeatMacros)))
deletePickedFromList(sLines_with_RepeatMacros, sTitle=sTitle)
def reAssignMacro():
"""
"""
sTitle = "Reassign Alias Macro to Python Script"
sSearchSubString = rs.StringBox(
message="Enter substring to find or Enter (or Cancel) to list all",
default_value=None,
title=sTitle)
sItems = []
sNames = list(CAL.GetNames())
sNames.sort(key=str.lower)
for sName in sNames:
sMacro = CAL.GetMacro(sName)
sLine = sName + ' ' + sMacro
sItems.append(sLine)
if sSearchSubString is not None:
sItems_WIP = []
for sItem in sItems:
if sSearchSubString.lower() in sItem.lower():
sItems_WIP.append(sItem)
if not sItems_WIP:
print("Substring not found.")
return
sItems = sItems_WIP
sItemsSelected = rs.MultiListBox(
items=sItems,
message="Pick aliases to edit their macros",
title=sTitle,
defaults=None)
if sItemsSelected is None: return
for sItem in sItemsSelected:
sAliasName_Sel = sItem.split(sep=' ')[0]
sMacro_Old = CAL.GetMacro(alias=sAliasName_Sel)
sFileFullPath = rs.OpenFileName(
title="Reassign Macro for Alias {}".format(sAliasName_Sel),
filter="python scripts|*.py||",
folder=_sDefaultPyFolderPath,
)
if len(sFileFullPath) == 0: return
sAliases = []
sMacros = []
sScriptFileBaseName = fileBaseName(sFileFullPath)
if sScriptFileBaseName is None:
print("Error in understanding {} Exiting...".format(sFileFullPath))
return
sMacro_New = "_NoEcho ! _-RunPythonScript " + sScriptFileBaseName
sMacro_New = rs.StringBox("Macro for alias", sMacro_New, sTitle)
if sMacro_New is None:
print("{} was skipped.".format(sAliasName_Sel))
return False
if sMacro_New == sMacro_Old: continue
iMbReturn = rs.MessageBox(
message="Replace macro\n{}\nwith\n{}\n?".format(
sMacro_Old,
sMacro_New),
buttons=3, title='')
if iMbReturn == 2:
# Cancel
return
elif iMbReturn == 6:
# Yes
sMacro_Prev = CAL.GetMacro(alias=sAliasName_Sel)
if not CAL.SetMacro(alias=sAliasName_Sel, macro=sMacro_New):
sMacro_Prev = None
if sMacro_Prev == sMacro_Old:
print("Alias {} macro {} replaced with {}.".format(
sAliasName_Sel,
sMacro_Prev,
CAL.GetMacro(alias=sAliasName_Sel)))