-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandRecorder.py
More file actions
1360 lines (1261 loc) · 57.3 KB
/
CommandRecorder.py
File metadata and controls
1360 lines (1261 loc) · 57.3 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
#==============================================================
#スタートアップ
#-------------------------------------------------------------------------------------------
import bpy #Blender内部のデータ構造にアクセスするために必要
from bpy.app.handlers import persistent
import os
import shutil
import json
from json.decoder import JSONDecodeError
import zipfile
from bpy.props import\
(#プロパティを使用するために必要
StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
EnumProperty,
PointerProperty,
CollectionProperty
)
from bpy.types import\
(
Panel,
UIList,
Operator,
PropertyGroup
)
from . import DefineCommon as Common
from bpy_extras.io_utils import ImportHelper, ExportHelper
def TempNameUpdate(self, context):
TempUpdate()
class CR_OT_String(PropertyGroup):#リストデータを保持するためのプロパティグループを作成
cname : StringProperty(default='', update= TempNameUpdate) #CR_Var.name
class CR_List_Selector(UIList):
def draw_item(self, context, layout, data, item, icon, active_data,active_propname, index):
layout.label(text = item.cname)
class CR_List_Command(UIList):
def draw_item(self, context, layout, data, item, icon, active_data,active_propname, index):
layout.label(text = item.cname)
class CR_List_Instance(UIList):
def draw_item(self, context, layout, data, item, icon, active_data,active_propname, index):
layout.label(text = item.cname)
#-------------------------------------------------------------------------------------------
def CR_(Data , Num):
scene = bpy.context.scene
if Data == 'List' :
return eval('scene.CR_Var.List_Command_{0:03d}'.format(Num))
elif Data == 'Index' :
return eval('scene.CR_Var.List_Index_{0:03d}'.format(Num))
else :
exec('scene.CR_Var.List_Index_{0:03d} = {1}'.format(Num,Data))
def Get_Recent(Return_Bool):#操作履歴にアクセス
#remove other Recent Reports
reports = \
[
bpy.data.texts.remove(t, do_unlink=True)
for t in bpy.data.texts
if t.name.startswith('Recent Reports')
]
# make a report
win = bpy.context.window_manager.windows[0]
area = win.screen.areas[0]
area_type = area.type
area.type = 'INFO'
override = bpy.context.copy()
override['window'] = win
override['screen'] = win.screen
override['area'] = win.screen.areas[0]
bpy.ops.info.select_all(override, action='SELECT')
bpy.ops.info.report_copy(override)
area.type = area_type
clipboard = bpy.context.window_manager.clipboard
bpy.data.texts.new('Recent Reports')
bpy.data.texts['Recent Reports'].write(clipboard)
# print the report
if Return_Bool == 'Reports_All':
return bpy.data.texts['Recent Reports'].lines#操作履歴全ての行
elif Return_Bool == 'Reports_Length':
return len(bpy.data.texts['Recent Reports'].lines)#操作履歴の要素数
def Record(Num, Mode):
Recent = Get_Recent('Reports_All')
if Mode == 'Start':
CR_PT_Panel.Bool_Record = 1
CR_Prop.Temp_Num = len(Recent)
else:
CR_PT_Panel.Bool_Record = 0
for i in range (CR_Prop.Temp_Num, len(Recent)):
TempText = Recent[i-1].body
if TempText.count('bpy'):
Item = CR_('List', Num).add()
Item.cname = TempText[TempText.find('bpy'):]
def CreateTempFile():
tpath = bpy.app.tempdir + "temp.json"
if not os.path.exists(tpath):
print(tpath)
with open(tpath, 'w', encoding='utf8') as tempfile:
json.dump({"0":[]}, tempfile)
return tpath
def TempSave(Num): # write new command to temp.json file
tpath = CreateTempFile()
with open(tpath, 'r+', encoding='utf8') as tempfile:
data = json.load(tempfile)
data.update({str(Num):[]})
data["0"].append(CR_('List', 0)[Num - 1]['cname'])
tempfile.seek(0)
json.dump(data, tempfile)
def TempUpdate(): # update all commands in temp.json file
tpath = CreateTempFile()
with open(tpath, 'r+', encoding='utf8') as tempfile:
tempfile.truncate(0)
tempfile.seek(0)
data = {}
for cmd in range(len(CR_('List', 0)) + 1):
data.update({str(cmd):[i.cname for i in CR_('List', cmd)]})
json.dump(data, tempfile)
def TempUpdateCommand(Key): # update one command in temp.json file
tpath = CreateTempFile()
with open(tpath, 'r+', encoding='utf8') as tempfile:
data = json.load(tempfile)
data[str(Key)] = [i.cname for i in CR_('List', int(Key))]
tempfile.truncate(0)
tempfile.seek(0)
json.dump(data, tempfile)
@persistent
def TempLoad(dummy): # load commands after undo
tpath = bpy.app.tempdir + "temp.json"
if bpy.context.scene.CR_Var.IgnoreUndo and os.path.exists(tpath):
with open(tpath, 'r', encoding='utf8') as tempfile:
data = json.load(tempfile)
command = CR_('List', 0)
command.clear()
keys = list(data.keys())
for i in range(1, len(data)):
Item = command.add()
Item.cname = data["0"][i - 1]
record = CR_('List', i)
record.clear()
for j in range(len(data[keys[i]])):
Item = record.add()
Item.cname = data[keys[i]][j]
UndoRedoStack = []
def GetCommand(index):
return eval('bpy.context.scene.CR_Var.List_Command_{0:03d}'.format(index))
@persistent
def SaveUndoStep(dummy):
All = []
l = []
l.append([i.cname for i in list(GetCommand(0))])
for x in range(1, len(l[0]) + 1):
l.append([ i.cname for i in list(GetCommand(x))])
UndoRedoStack.append(l)
@persistent
def GetRedoStep(dummy):
command = CR_('List', 0)
command.clear()
l = UndoRedoStack[len(UndoRedoStack) - 1]
for i in range(1, len(l[0]) + 1):
item = command.add()
item.cname = l[0][i - 1]
record = CR_('List', i)
record.clear()
for j in range(len(l[i])):
item = record.add()
item.cname = l[i][j]
UndoRedoStack.pop()
def Add(Num):
Recent = Get_Recent('Reports_All')
if Num or len(CR_('List', 0)) < 250:
Item = CR_('List', Num).add()
if Num:
if Recent[-2].body.count('bpy'):
Name_Temp = Recent[-2].body
Item.cname = Name_Temp[Name_Temp.find('bpy'):]
else:
Name_Temp = Recent[-3].body
Item.cname = Name_Temp[Name_Temp.find('bpy'):]
else:
Item.cname = 'Untitled_{0:03d}'.format(len(CR_('List', Num)))
CR_( len(CR_('List',Num))-1, Num )
def Remove(Num):
if not Num:
for Num_Loop in range(CR_('Index',0)+1 , len(CR_('List',0))+1) :
CR_('List',Num_Loop).clear()
for Num_Command in range(len(CR_('List',Num_Loop+1))) :
Item = CR_('List',Num_Loop).add()
Item.cname = CR_('List',Num_Loop+1)[Num_Command].cname
CR_(CR_('Index',Num_Loop+1),Num_Loop)
if len(CR_('List',Num)):
CR_('List',Num).remove(CR_('Index',Num))
if len(CR_('List',Num)) - 1 < CR_('Index',Num):
CR_(len(CR_('List',Num))-1,Num)
if CR_('Index',Num) < 0:
CR_(0,Num)
def Move(Num , Mode) :
index1 = CR_('Index',Num)
if Mode == 'Up' :
index2 = CR_('Index',Num) - 1
else :
index2 = CR_('Index',Num) + 1
LengthTemp = len(CR_('List',Num))
if (2 <= LengthTemp) and (0 <= index1 < LengthTemp) and (0 <= index2 <LengthTemp):
CR_('List',Num).move(index1, index2)
CR_(index2 , Num)
#コマンドの入れ替え処理
if not Num :
index1 += 1
index2 += 1
CR_('List',254).clear()
#254にIndex2を逃がす
for Num_Command in CR_('List',index2) :
Item = CR_('List',254).add()
Item.cname = Num_Command.cname
CR_(CR_('Index',index2),254)
CR_('List',index2).clear()
#Index1からIndex2へ
for Num_Command in CR_('List',index1) :
Item = CR_('List',index2).add()
Item.cname = Num_Command.cname
CR_(CR_('Index',index1),index2)
CR_('List',index1).clear()
#254からIndex1へ
for Num_Command in CR_('List',254) :
Item = CR_('List',index1).add()
Item.cname = Num_Command.cname
CR_(CR_('Index',254),index1)
CR_('List',254).clear()
def Select_Command(Mode):
currentIndex = CR_('Index',0)
listlen = len(CR_('List',0)) - 1
if Mode == 'Up':
if currentIndex == 0:
bpy.context.scene.CR_Var.List_Index_000 = listlen
else:
bpy.context.scene.CR_Var.List_Index_000 = currentIndex - 1
else:
if currentIndex == listlen:
bpy.context.scene.CR_Var.List_Index_000 = 0
else:
bpy.context.scene.CR_Var.List_Index_000 = currentIndex + 1
def Play(Commands) :
scene = bpy.context.scene
for Command in Commands :
if type(Command) == str :
exec(Command)
else :
exec(Command.cname)
def Clear(Num) :
CR_('List',Num).clear()
class CR_OT_Selector(Operator):
bl_idname = 'cr_selector.button'#大文字禁止
bl_label = 'Button_Selector'#メニューに登録される名前
#bl_options = {'REGISTER', 'UNDO'} # 処理の属性
Mode : bpy.props.StringProperty(default='')
def execute(self, context):
scene = bpy.context.scene
#追加
if self.Mode == 'Add' :
Add(0)
if scene.CR_Var.IgnoreUndo:
TempSave(CR_('Index',0) + 1)
#削除
elif self.Mode == 'Remove' :
Remove(0)
if scene.CR_Var.IgnoreUndo:
TempUpdate()
#上へ
elif self.Mode == 'Up' :
Move(0 , 'Up')
if scene.CR_Var.IgnoreUndo:
TempUpdate()
#下へ
elif self.Mode == 'Down' :
Move(0 , 'Down')
if scene.CR_Var.IgnoreUndo:
TempUpdate()
bpy.context.area.tag_redraw()
return{'FINISHED'}#UI系の関数の最後には必ず付ける
class CR_OT_Selector_Up(Operator):
bl_idname = 'cr_selector_up.button'
bl_label = 'Command_OT_Selection_Up'
#bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
Select_Command('Up')
bpy.context.area.tag_redraw()
return{'FINISHED'}
class CR_OT_Selector_Down(Operator):
bl_idname = 'cr_selector_down.button'
bl_label = 'Command_OT_Selection_Down'
#bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
Select_Command('Down')
bpy.context.area.tag_redraw()
return{'FINISHED'}
class Command_OT_Play(Operator):
bl_idname = 'cr_commandplay.button'#大文字禁止
bl_label = 'Command_OT_Play'#メニューに登録される名前
bl_options = {'REGISTER', 'UNDO'}#アンドゥ履歴に登録
def execute(self, context):
#コマンドを実行
Play(CR_('List',CR_('Index',0)+1))
return{'FINISHED'}#UI系の関数の最後には必ず付ける
class Command_OT_Add(Operator):
bl_idname = 'cr_commandadd.button'#大文字禁止
bl_label = 'Command_OT_Add'#メニューに登録される名前
#bl_options = {'REGISTER', 'UNDO'}#アンドゥ履歴に登録
def execute(self, context):
#コマンドを実行
Add(CR_('Index',0)+1)
if bpy.context.scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
bpy.context.area.tag_redraw()
return{'FINISHED'}#UI系の関数の最後には必ず付ける
class CR_OT_Command(Operator):
bl_idname = 'cr_command.button'#大文字禁止
bl_label = 'Button_Command'#メニューに登録される名前
#bl_options = {'REGISTER', 'UNDO'} # 処理の属性
Mode : bpy.props.StringProperty(default='')
def execute(self, context):
scene = bpy.context.scene
#録画を開始
if self.Mode == 'Record_Start' :
Record(CR_('Index',0)+1 , 'Start')
#録画を終了
elif self.Mode == 'Record_Stop' :
Record(CR_('Index',0)+1 , 'Stop')
if scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
#追加
elif self.Mode == 'Add' :
Add(CR_('Index',0)+1)
if scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
#削除
elif self.Mode == 'Remove' :
Remove(CR_('Index',0)+1)
if scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
#上へ
elif self.Mode == 'Up' :
Move(CR_('Index',0)+1 , 'Up')
if scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
#下へ
elif self.Mode == 'Down' :
Move(CR_('Index',0)+1 , 'Down')
if scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
#リストをクリア
elif self.Mode == 'Clear' :
Clear(CR_('Index',0)+1)
if scene.CR_Var.IgnoreUndo:
TempUpdateCommand(CR_('Index',0)+1)
bpy.context.area.tag_redraw()
return{'FINISHED'}#UI系の関数の最後には必ず付ける
def Save():
for savedfolder in os.listdir(path):
folderpath = path + "/" + savedfolder
for savedfile in os.listdir(folderpath):
os.remove(folderpath + "/" + savedfile)
os.rmdir(folderpath)
for cat in bpy.context.scene.cr_categories:
panelpath = path + "/" + f"{GetPanelIndex(cat)}~" + cat.pn_name
os.mkdir(panelpath)
start = cat.Instance_Start
for cmd_i in range(start, start + cat.Instance_length):
with open(panelpath + "/" + f"{cmd_i - start}~" + CR_Prop.Instance_Name[cmd_i] + ".txt", 'w', encoding='utf8') as cmd_file:
for cmd in CR_Prop.Instance_Command[cmd_i]:
cmd_file.write(cmd + "\n")
def Load():
print('------------------Load-----------------')
scene = bpy.context.scene
scene.cr_categories.clear()
scene.cr_enum.clear()
CR_Prop.Instance_Name.clear()
CR_Prop.Instance_Command.clear()
for folder in os.listdir(path):
folderpath = path + "/" + folder
if os.path.isdir(folderpath):
textfiles = os.listdir(folderpath)
new = scene.cr_categories.add()
name = "".join(folder.split('~')[1:])
new.name = name
new.pn_name = name
new.pn_show = True
new.Instance_Start = len(CR_Prop.Instance_Name)
new.Instance_length = len(textfiles)
sortedtxt = [None] * len(textfiles)
for txt in textfiles:
sortedtxt[int(os.path.splitext(txt)[0].split('~')[0])] = txt #remove the .txtending, join to string again, get the index ''.join(txt.split('.')[:-1])
for i in range(len(sortedtxt)):
txt = sortedtxt[i]
new_e = scene.cr_enum.add()
e_index = len(scene.cr_enum) - 1
new_e.name = str(e_index)
new_e.Index = e_index
CR_Prop.Instance_Name.append("".join(os.path.splitext(txt)[0].split('~')[1:]))
CmdList = []
with open(folderpath + "/" + txt, 'r', encoding='utf8') as text:
for line in text.readlines():
CmdList.append(line.strip())
CR_Prop.Instance_Command.append(CmdList)
SetEnumIndex()
def Recorder_to_Instance(panel):
scene = bpy.context.scene
i = panel.Instance_Start + panel.Instance_length
CR_Prop.Instance_Name.insert(i, CR_('List',0)[CR_('Index',0)].cname)
Temp_Command = []
for Command in CR_('List',CR_('Index',0)+1):
Temp_Command.append(Command.cname)
CR_Prop.Instance_Command.insert(i, Temp_Command)
panel.Instance_length += 1
new_e = scene.cr_enum.add()
e_index = len(scene.cr_enum) - 1
new_e.name = str(e_index)
new_e.Index = e_index
p_i = GetPanelIndex(panel)
categories = scene.cr_categories
if p_i < len(categories):
for cat in categories[ p_i + 1: ]:
cat.Instance_Start += 1
def Instance_to_Recorder():
scene = bpy.context.scene
Item = CR_('List' , 0 ).add()
Item.cname = CR_Prop.Instance_Name[scene.CR_Var.Instance_Index]
for Command in CR_Prop.Instance_Command[scene.CR_Var.Instance_Index] :
Item = CR_('List' , len(CR_('List',0)) ).add()
Item.cname = Command
CR_( len(CR_('List',0))-1 , 0 )
def Execute_Instance(Num):
Play(CR_Prop.Instance_Command[Num])
def Rename_Instance():
scene = bpy.context.scene
CR_Prop.Instance_Name[scene.CR_Var.Instance_Index] = scene.CR_Var.Rename
def I_Remove():
scene = bpy.context.scene
if len(CR_Prop.Instance_Name) :
Index = scene.CR_Var.Instance_Index
CR_Prop.Instance_Name.pop(Index)
CR_Prop.Instance_Command.pop(Index)
scene.cr_enum.remove(len(scene.cr_enum) - 1)
categories = scene.cr_categories
for cat in categories:
if Index >= cat.Instance_Start and Index < cat.Instance_Start + cat.Instance_length:
cat.Instance_length -= 1
p_i = GetPanelIndex(cat)
if p_i < len(categories):
for cat in categories[ p_i + 1: ]:
cat.Instance_Start -= 1
break
if len(CR_Prop.Instance_Name) and len(CR_Prop.Instance_Name)-1 < Index :
scene.CR_Var.Instance_Index = len(CR_Prop.Instance_Name)-1
SetEnumIndex()
def I_Move(Mode):
scene = bpy.context.scene
index1 = scene.CR_Var.Instance_Index
if Mode == 'Up' :
index2 = scene.CR_Var.Instance_Index - 1
else :
index2 = scene.CR_Var.Instance_Index + 1
LengthTemp = len(CR_Prop.Instance_Name)
if (2 <= LengthTemp) and (0 <= index1 < LengthTemp) and (0 <= index2 <LengthTemp):
CR_Prop.Instance_Name[index1] , CR_Prop.Instance_Name[index2] = CR_Prop.Instance_Name[index2] , CR_Prop.Instance_Name[index1]
CR_Prop.Instance_Command[index1] , CR_Prop.Instance_Command[index2] = CR_Prop.Instance_Command[index2] , CR_Prop.Instance_Command[index1]
scene.cr_enum[index2].Value = True
class CR_OT_Instance(Operator):
bl_idname = 'cr_instance.button'#大文字禁止
bl_label = 'Button_Instance'#メニューに登録される名前
#bl_options = {'REGISTER', 'UNDO'} # 処理の属性
Mode : bpy.props.StringProperty(default='')
def execute(self, context):
#追加
if self.Mode == 'Add' :
Add(255)
#削除
elif self.Mode == 'Remove' :
Remove(255)
#上へ
elif self.Mode == 'Up' :
Up(255)
#下へ
elif self.Mode == 'Down' :
Down(255)
#保存
elif self.Mode == 'Save' :
Save()
#読み込み
elif self.Mode == 'Load' :
Load()
TempSaveCats()
#コマンドをインスタンスに
elif self.Mode == 'Instance_to_Recorder' :
Instance_to_Recorder()
TempUpdate()
#削除
elif self.Mode == 'I_Remove' :
I_Remove()
TempSaveCats()
#上へ
elif self.Mode == 'I_Up' :
I_Move('Up')
TempSaveCats()
#下へ
elif self.Mode == 'I_Down' :
I_Move('Down')
TempSaveCats()
#インスタンスのリネーム
elif self.Mode == 'Rename' :
Rename_Instance()
TempSaveCats()
bpy.context.area.tag_redraw()
return{'FINISHED'}#UI系の関数の最後には必ず付ける
class CR_OT_Cmd(Operator):
bl_idname = 'cr_instance.cmd_button'
bl_label = 'ComRec Command'
bl_options = {'REGISTER', 'UNDO'}
Index : IntProperty()
def execute(self, context):
Execute_Instance(self.Index)
return{'FINISHED'}
class CR_PT_Panel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'# メニューを表示するエリア
bl_region_type = 'UI'# メニューを表示するリージョン
bl_category = 'CommandRecorder'# メニュータブのヘッダー名
bl_label = 'CommandButton'# タイトル
#変数の宣言
#-------------------------------------------------------------------------------------------
SelectedInctance = ''
Bool_Record = 0
Bool_Recent = ''
#レイアウト
#-------------------------------------------------------------------------------------------
def draw_header(self, context):
self.layout.label(text = '', icon = 'REC')
#メニューの描画処理
def draw(self, context):
scene = bpy.context.scene
layout = self.layout
layout.prop(scene.CR_Var, 'PanelType', text= scene.CR_Var.PanelType, expand= True)
if scene.CR_Var.PanelType == "button":
#Button --------------------------------------
box = layout.box()
row = box.row(align= True)
row.operator(CR_OT_Instance.bl_idname , text='Button to Recorder' ).Mode = 'Instance_to_Recorder'
row.prop(scene.CR_Var, 'HideMenu', text= "", icon= 'COLLAPSEMENU')
col = box.column(align= True)
if scene.CR_Var.HideMenu:
col.operator(CR_OT_Instance.bl_idname , text='Save to File' ).Mode = 'Save'
else:
col.operator(CR_OT_Instance.bl_idname , text='Save to File' ).Mode = 'Save'
col.operator(CR_OT_Instance.bl_idname , text='Load from File' ).Mode = 'Load'
col.operator(AddCategory.bl_idname, text= "Add from File").Mode = 'AddFromFile'
col = box.column(align= True)
col.operator(ImportButton.bl_idname, text= 'Import')
col.operator(ExportButton.bl_idname, text= 'Export')
row = box.row().split(factor= 0.4)
row.label(text= 'Category')
row2 = row.row(align= True)
row2.scale_x = 1.1737
row2.operator(AddCategory.bl_idname, text= '', icon= 'ADD').Mode = 'Add'
row2.operator(AddCategory.bl_idname, text= '', icon= 'TRASH').Mode = 'Delet'
row2.operator(AddCategory.bl_idname, text= '', icon= 'GREASEPENCIL').Mode = 'Rename'
if len(CR_Prop.Instance_Name) :
row = box.row().split(factor= 0.4)
row.label(text= 'Buttons')
row2 = row.row(align= True)
row2.operator(CR_OT_Instance.bl_idname , text='' , icon='REMOVE' ).Mode = 'I_Remove'
row2.operator(CR_OT_Instance.bl_idname , text='' , icon='TRIA_UP' ).Mode = 'I_Up'
row2.operator(CR_OT_Instance.bl_idname , text='' , icon='TRIA_DOWN' ).Mode = 'I_Down'
row2.operator(AddCategory.bl_idname, text= '', icon= 'PRESET').Mode = 'Move'
row = box.row()
row2 = row.split(factor= 0.7)
row2.prop(scene.CR_Var , 'Rename' , text='')
row2.operator(CR_OT_Instance.bl_idname , text='Rename').Mode = 'Rename'
categories = scene.cr_categories
for cat in categories:
box = layout.box()
col = box.column()
row = col.row()
if cat.pn_show:
row.prop(cat, 'pn_show', icon="TRIA_DOWN", text= "", emboss= False)
else:
row.prop(cat, 'pn_show', icon="TRIA_RIGHT", text= "", emboss= False)
row.label(text= cat.pn_name)
i = GetPanelIndex(cat)
row2 = row.row(align= True)
row2.operator(AddCategory.bl_idname, icon="TRIA_UP", text= "").Mode = f'Move_Up-{i}'
row2.operator(AddCategory.bl_idname, icon="TRIA_DOWN", text="").Mode = f'Move_Down-{i}'
if cat.pn_show:
split = box.split(factor=0.2)
col = split.column(align= True)
for i in range(cat.Instance_Start, cat.Instance_Start + cat.Instance_length):
col.prop(scene.cr_enum[i], 'Value' ,toggle = 1, text= str(i - cat.Instance_Start + 1))
col = split.column()
col.scale_y = 0.9493
for Num_Loop in range(cat.Instance_Start, cat.Instance_Start + cat.Instance_length):
col.operator(CR_OT_Cmd.bl_idname , text=CR_Prop.Instance_Name[Num_Loop]).Index = Num_Loop
else:
#Record----------------------------------------------
box = layout.box()
box_row = box.row()
box_row.label(text = '', icon = 'SETTINGS')
if len(CR_('List',0)) :
try:
box_row.prop(CR_('List',0)[CR_('Index',0)] , 'cname' , text='')
except:
pass
box_row = box.row()
col = box_row.column()
col.template_list('CR_List_Selector' , '' , scene.CR_Var , 'List_Command_000' , scene.CR_Var , 'List_Index_000', rows=4)
col = box_row.column()
col.operator(CR_OT_Selector.bl_idname , text='' , icon='ADD' ).Mode = 'Add'
col.operator(CR_OT_Selector.bl_idname , text='' , icon='REMOVE' ).Mode = 'Remove'
col.operator(CR_OT_Selector.bl_idname , text='' , icon='TRIA_UP' ).Mode = 'Up'
col.operator(CR_OT_Selector.bl_idname , text='' , icon='TRIA_DOWN' ).Mode = 'Down'
#
if len(CR_('List',0)) :
box2 = box.box()
box_row = box2.row()
if scene.CR_Var.ShowMacros:
box_row.prop(scene.CR_Var, 'ShowMacros', icon="TRIA_DOWN", text= "", emboss= False)
else:
box_row.prop(scene.CR_Var, 'ShowMacros', icon="TRIA_RIGHT", text= "", emboss= False)
box_row.label(text = 'Edit Macro', icon = 'TEXT')
if scene.CR_Var.ShowMacros:
box_row = box2.row()
if len(CR_('List',CR_('Index',0)+1)) :
box_row.prop(CR_('List',CR_('Index',0)+1)[CR_('Index',CR_('Index',0)+1)],'cname' , text='')
box_row = box2.row()
col = box_row.column()
col.template_list('CR_List_Command' , '' , scene.CR_Var , 'List_Command_{0:03d}'.format(CR_('Index',0)+1) , scene.CR_Var , 'List_Index_{0:03d}'.format(CR_('Index',0)+1), rows=4)
col = box_row.column()
if not CR_PT_Panel.Bool_Record :
col.operator(CR_OT_Command.bl_idname , text='' , icon='TRIA_UP' ).Mode = 'Up'
col.operator(CR_OT_Command.bl_idname , text='' , icon='TRIA_DOWN' ).Mode = 'Down'
row = box.row()
if CR_PT_Panel.Bool_Record :
row.operator(CR_OT_Command.bl_idname , text='' , icon='PAUSE' ).Mode = 'Record_Stop'
row.prop(scene.CR_Var, 'IgnoreUndo', toggle = 1, text="Ignore Undo")
else :
row2 = row.row(align= True)
row2.operator(CR_OT_Command.bl_idname , text='' , icon='REC' ).Mode = 'Record_Start'
row2.operator(Command_OT_Add.bl_idname , text='' , icon='ADD' )
row2.operator(CR_OT_Command.bl_idname , text='' , icon='REMOVE' ).Mode = 'Remove'
row.prop(scene.CR_Var, 'IgnoreUndo', toggle = 1, text="Ignore Undo")
if len(CR_('List',CR_('Index',0)+1)) :
col = box.column(align= True)
col.operator(Command_OT_Play.bl_idname , text='Play' )
col.operator(AddCategory.bl_idname , text='Recorder to Button' ).Mode = 'ToButton'
col.operator(CR_OT_Command.bl_idname , text='Clear').Mode = 'Clear'
currentselected = [None]
lastselected = [None]
def UseRadioButtons(self, context):
categories = context.scene.cr_categories
for cat in categories:
if not cat.pn_selected and lastselected[0] == cat and currentselected[0] == cat:
cat.pn_selected = True
elif cat.pn_selected and lastselected[0] != cat and currentselected[0] != cat:
currentselected[0] = cat
if lastselected[0] is not None:
lastselected[0].pn_selected = False
lastselected[0] = cat
class CategorizeProps(bpy.types.PropertyGroup):
pn_name : StringProperty()
pn_show : BoolProperty(default= True)
pn_selected : BoolProperty(default= False, update= UseRadioButtons)
Instance_Start : IntProperty(default= 0)
Instance_length : IntProperty(default= 0)
path = os.path.dirname(__file__) + "/Storage"
#Initalize Standert Button List
@persistent
def InitSavedPanel(dummy):
if not os.path.exists(path):
os.mkdir(path)
Load()
def GetPanelIndex(cat):
return int(cat.path_from_id().split("[")[1].split("]")[0])
def SetEnumIndex():
scene = bpy.context.scene
if len(scene.cr_enum):
enumIndex = scene.CR_Var.Instance_Index * (scene.CR_Var.Instance_Index < len(scene.cr_enum))
scene.cr_enum[enumIndex].Value = True
scene.CR_Var.Instance_Index = enumIndex
tempnotinited = [True]
@persistent
def InitTemp(dummy):
if tempnotinited[0]:
TempSaveCats()
import time
tempnotinited[0] = False
print(bpy.app.tempdir + " -------------------------------------------------------")
def CreateTempCats():
tcatpath = bpy.app.tempdir + "tempcats.json"
if not os.path.exists(tcatpath):
with open(tcatpath, 'x', encoding='utf8') as tempfile:
print(tcatpath)
return tcatpath
def TempSaveCats():
scene = bpy.context.scene
tcatpath = CreateTempCats()
with open(tcatpath, 'r+', encoding='utf8') as tempfile:
tempfile.truncate(0)
tempfile.seek(0)
cats = []
for cat in scene.cr_categories:
cats.append({
"name": cat.name,
"pn_name": cat.pn_name,
"pn_show": cat.pn_show,
"Instance_Start": cat.Instance_Start,
"Instance_length": cat.Instance_length
})
data = {
"Instance_Name": CR_Prop.Instance_Name,
"Instance_Command": CR_Prop.Instance_Command,
"Instance_Index": scene.CR_Var.Instance_Index,
"Categories": cats
}
json.dump(data, tempfile)
@persistent
def TempLoadCats(dummy):
scene = bpy.context.scene
tcatpath = bpy.app.tempdir + "tempcats.json"
scene.cr_enum.clear()
scene.cr_categories.clear()
CR_Prop.Instance_Name.clear()
CR_Prop.Instance_Command.clear()
with open(tcatpath, 'r', encoding='utf8') as tempfile:
data = json.load(tempfile)
CR_Prop.Instance_Name = data["Instance_Name"]
CR_Prop.Instance_Command = data["Instance_Command"]
index = data["Instance_Index"]
scene.CR_Var.Instance_Index = index
for i in range(len(CR_Prop.Instance_Name)):
new_e = scene.cr_enum.add()
new_e.name = str(i)
new_e.Index = i
scene.cr_enum[index].Value = True
for cat in data["Categories"]:
new = scene.cr_categories.add()
new.name = cat["name"]
new.pn_name = cat["pn_name"]
new.pn_show = cat["pn_show"]
new.Instance_Start = cat["Instance_Start"]
new.Instance_length = cat["Instance_length"]
class AddCategory(bpy.types.Operator):
bl_idname = "cr.add_category"
bl_label = "Category"
Mode : StringProperty()
PanelName : StringProperty(name = "Category Name", default="")
NewPanel : BoolProperty(default= False, description= "Create a new Category with all selected Buttons")
def execute(self, context):
categories = context.scene.cr_categories
scene = context.scene
if self.Mode == 'Add':
new = scene.cr_categories.add()
new.name = self.PanelName
new.pn_name = self.PanelName
new.Instance_Start = len(CR_Prop.Instance_Name)
new.Instance_length = 0
elif self.Mode == 'Delet':
for cat in categories:
if cat.pn_selected:
index = GetPanelIndex(cat)
start = cat.Instance_Start
for i in range(start, start + cat.Instance_length):
scene.cr_enum.remove(len(scene.cr_enum) - 1)
CR_Prop.Instance_Name.pop(start)
CR_Prop.Instance_Command.pop(start)
for nextcat in categories[index + 1 :]:
nextcat.Instance_Start -= cat.Instance_length
categories.remove(GetPanelIndex(cat))
SetEnumIndex()
break
elif self.Mode == 'Rename':
for cat in categories:
if cat.pn_selected:
cat.name = self.PanelName
cat.pn_name = self.PanelName
break
elif self.Mode == 'Move':
for cat in categories:
if cat.pn_selected:
Index = scene.CR_Var.Instance_Index
catendl = cat.Instance_Start + cat.Instance_length - 1
for curcat in categories:
if Index >= curcat.Instance_Start and Index < curcat.Instance_Start + curcat.Instance_length:
curcat.Instance_length -= 1
for nextcat in categories[GetPanelIndex(curcat) + 1 :]:
nextcat.Instance_Start -= 1
break
CR_Prop.Instance_Name.insert(catendl, CR_Prop.Instance_Name.pop(Index))
CR_Prop.Instance_Command.insert(catendl, CR_Prop.Instance_Command.pop(Index))
for nextcat in categories[GetPanelIndex(cat) + 1:]:
nextcat.Instance_Start += 1
cat.Instance_length += 1
SetEnumIndex()
break
elif self.Mode == 'ToButton':
for cat in categories:
if cat.pn_selected:
Recorder_to_Instance(cat)
break
elif self.Mode == 'AddFromFile':
if self.NewPanel:
new = scene.cr_categories.add()
new.name = "Add from File"
new.pn_name = "Add from File"
new.Instance_Start = len(CR_Prop.Instance_Name)
filedisp = scene.cr_filedisp
for filecat in scene.cr_filecategories:
if filecat.pn_selected:
for i in range(filecat.FileDisp_Start, filecat.FileDisp_Start + filecat.FileDisp_length):
filedisp[i].Index = True
for i in range(len(filedisp)):
if filedisp[i].Index:
new_e = scene.cr_enum.add()
e_index = len(scene.cr_enum) - 1
new_e.name = str(e_index)
new_e.Index = e_index
CR_Prop.Instance_Name.append(CR_Prop.FileDisp_Name[i])
CR_Prop.Instance_Command.append(CR_Prop.FileDisp_Command[i])
new.Instance_length = len(CR_Prop.Instance_Name) - new.Instance_Start
self.NewPanel = False
else:
for filecat in scene.cr_filecategories:
index = None
for i in range(len(categories)):
if categories[i].pn_name == filecat.pn_name:
index = i
break
if filecat.pn_selected:
if index is None:
new = scene.cr_categories.add()
new.name = filecat.name
new.pn_name = filecat.pn_name
new.Instance_Start = len(CR_Prop.Instance_Name)
new.Instance_length = filecat.FileDisp_length
for i in range(filecat.FileDisp_Start, filecat.FileDisp_Start + filecat.FileDisp_length):
new_e = scene.cr_enum.add()
e_index = len(scene.cr_enum) - 1
new_e.name = str(e_index)
new_e.Index = e_index
CR_Prop.Instance_Name.append(CR_Prop.FileDisp_Name[i])
CR_Prop.Instance_Command.append(CR_Prop.FileDisp_Command[i])
else:
for i_cat in range(index + 1, len(categories)):
categories[i_cat].Instance_Start += filecat.FileDisp_length
categories[index].Instance_length += filecat.FileDisp_length
i_start = categories[index].Instance_Start + categories[index].Instance_length - filecat.FileDisp_length
for i in range(i_start, categories[index].Instance_Start + categories[index].Instance_length):
new_e = scene.cr_enum.add()
e_index = len(scene.cr_enum) - 1
new_e.name = str(e_index)
new_e.Index = e_index
i_file = filecat.FileDisp_Start + i - i_start
CR_Prop.Instance_Name.insert(i , CR_Prop.FileDisp_Name[i_file])
CR_Prop.Instance_Command.insert(i, CR_Prop.FileDisp_Command[i_file])
else:
if index is None:
new = scene.cr_categories.add()
new.name = filecat.name
new.pn_name = filecat.pn_name
new.Instance_Start = len(CR_Prop.Instance_Name)
index = GetPanelIndex(new)
for i in range(filecat.FileDisp_Start, filecat.FileDisp_Start + filecat.FileDisp_length):
if scene.cr_filedisp[i].Index: # insert at i
new_e = scene.cr_enum.add()
new_e.name = str(len(scene.cr_enum) - 1)
i_cat = categories[index].Instance_Start + categories[index].Instance_length
CR_Prop.Instance_Name.insert(i_cat, CR_Prop.FileDisp_Name[i])
CR_Prop.Instance_Command.insert(i_cat, CR_Prop.FileDisp_Command[i])
categories[index].Instance_length += 1
for i_cat in range(index + 1, len(categories)):
categories[i_cat].Instance_Start += 1
bpy.context.area.tag_redraw()
TempSaveCats()
return {"FINISHED"}
def invoke(self, context, event):
m = self.Mode.split('-')[0]
categories = context.scene.cr_categories
if m == 'Move_Up':
i = int(self.Mode.split('-')[1])
if i - 1 >= 0:
cat1 = categories[i]
cat2 = categories[i - 1]
cat1.name, cat2.name = cat2.name, cat1.name
cat1.pn_name, cat2.pn_name = cat2.pn_name, cat1.pn_name
cat1.pn_show, cat2.pn_show = cat2.pn_show, cat1.pn_show
cat1.pn_selected, cat2.pn_selected = cat2.pn_selected, cat1.pn_selected
cat1.Instance_Start, cat2.Instance_Start = cat2.Instance_Start, cat1.Instance_Start
cat1.Instance_length, cat2.Instance_length = cat2.Instance_length, cat1.Instance_length
elif m == 'Move_Down':
i = int(self.Mode.split('-')[1])
if i + 1 < len(categories):
cat1 = categories[i]
cat2 = categories[i + 1]
cat1.name, cat2.name = cat2.name, cat1.name
cat1.pn_name, cat2.pn_name = cat2.pn_name, cat1.pn_name
cat1.pn_show, cat2.pn_show = cat2.pn_show, cat1.pn_show
cat1.pn_selected, cat2.pn_selected = cat2.pn_selected, cat1.pn_selected
cat1.Instance_Start, cat2.Instance_Start = cat2.Instance_Start, cat1.Instance_Start
cat1.Instance_length, cat2.Instance_length = cat2.Instance_length, cat1.Instance_length
elif self.Mode == 'AddFromFile':
#Load the File data to FileDisps
scene = bpy.context.scene
scene.cr_filecategories.clear()
scene.cr_filedisp.clear()
CR_Prop.FileDisp_Name.clear()
CR_Prop.FileDisp_Command.clear()
for folder in os.listdir(path):
folderpath = path + "/" + folder
if os.path.isdir(folderpath):
textfiles = os.listdir(folderpath)
new = scene.cr_filecategories.add()
name = "".join(folder.split('~')[1:])
new.name = name
new.pn_name = name
new.pn_show = True
new.FileDisp_Start = len(CR_Prop.FileDisp_Name)
new.FileDisp_length = len(textfiles)
sortedtxt = [None] * len(textfiles)
for txt in textfiles:
sortedtxt[int(os.path.splitext(txt)[0].split('~')[0])] = txt #remove the .txtending, join to string again, get the index
for txt in sortedtxt:
blnew = scene.cr_filedisp.add()
CR_Prop.FileDisp_Name.append("".join(txt.split('~')[1:]))
CmdList = []
with open(folderpath + "/" + txt, 'r', encoding='utf8') as text:
for line in text.readlines():
CmdList.append(line.strip())
CR_Prop.FileDisp_Command.append(CmdList)
bpy.context.area.tag_redraw()
TempSaveCats()