-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcellblender_scripting.py
1326 lines (1075 loc) · 55.4 KB
/
cellblender_scripting.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
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
"""
This file contains the classes for CellBlender's Scripting.
"""
import glob
import os
import cellblender
# blender imports
import bpy
from bpy.props import BoolProperty, CollectionProperty, EnumProperty, \
FloatProperty, FloatVectorProperty, IntProperty, \
IntVectorProperty, PointerProperty, StringProperty
#from bpy.app.handlers import persistent
#import math
#import mathutils
# python imports
import re
# CellBlender imports
import cellblender
from . import data_model
from . import parameter_system
from . import cellblender_utils
###############################
# Begin Data_Browser code #
###############################
class DataBrowserStringProperty(bpy.types.PropertyGroup):
v: StringProperty(name="DBString")
class DataBrowserIntProperty(bpy.types.PropertyGroup):
v: IntProperty(name="DBInt")
class DataBrowserFloatProperty(bpy.types.PropertyGroup):
v: FloatProperty(name="DBFloat")
class DataBrowserBoolProperty(bpy.types.PropertyGroup):
v: BoolProperty(name="DBBool")
class leaf_item:
def __init__ ( value, index ):
self.value = value
self.index = index
dm = None
dmi = None
class DataBrowserPropertyGroup(bpy.types.PropertyGroup):
show_list : CollectionProperty(type=DataBrowserBoolProperty, name="Show List")
string_list: CollectionProperty(type=DataBrowserStringProperty, name="String List")
int_list : CollectionProperty(type=DataBrowserIntProperty, name="Int List")
float_list : CollectionProperty(type=DataBrowserFloatProperty, name="Float List")
bool_list : CollectionProperty(type=DataBrowserBoolProperty, name="Bool List")
internal_file_name: StringProperty ( name = "Internal Text Name" )
def draw_layout ( self, context, layout ):
mcell = context.scene.mcell
scripting = mcell.scripting
row = layout.row()
col = row.column()
col.operator ( "cb.regenerate_data_model", icon='FILE_REFRESH' )
col = row.column()
col.operator ( "cb.print_dm_keys", icon='KEYINGSET' )
col = row.column()
col.operator ( "cb.print_data_model", icon='COLLAPSEMENU' )
row = layout.row()
col = row.column()
col.prop ( scripting, "include_geometry_in_dm" )
col = row.column()
col.prop ( scripting, "include_scripts_in_dm" )
col = row.column()
col.prop ( scripting, "include_dyn_geom_in_dm" )
row = layout.row()
col = row.column()
col.operator ( "browser.from_dm" )
col = row.column()
col.operator ( "browser.clear" )
if len(self.show_list) > 0:
row = layout.row()
col = row.column()
col.operator ( "browser.open_all" )
col = row.column()
col.operator ( "browser.close_all" )
# row = layout.row()
# row.prop ( self, "internal_file_name" )
#col = row.column()
#col.operator ( "browser.from_file" )
#col = row.column()
#col.operator ( "browser.to_file" )
global dm
global dmi
self.draw_recurse ( layout, "Model", dm, dmi )
def convert_recurse ( self, dm ):
# Convert any structure into an index structure
# print ( " Convert: got a dm of type " + str(type(dm)) )
dmi = None
if type(dm) == type({'a':1}):
# Process a dictionary
new_val = self.show_list.add()
new_val.v = False
dmi = ( {}, len(self.show_list)-1 )
#for k in sorted(dm.keys()):
#for k in dm.keys():
for k in sorted([str(k) for k in dm.keys()]):
dmi[0][k] = self.convert_recurse ( dm[k] )
elif type(dm) == type(['a',1]):
# Process a list
new_val = self.show_list.add()
new_val.v = False
dmi = ( [], len(self.show_list)-1 )
for v in dm:
dmi[0].append ( self.convert_recurse ( v ) )
elif (type(dm) == type('a')) or (type(dm) == type(u'a')): #dm is a string
new_val = self.string_list.add()
new_val.v = dm
dmi = len(self.string_list) - 1
elif type(dm) == type(1): # dm is an integer
new_val = self.int_list.add()
new_val.v = dm
dmi = len(self.int_list) - 1
elif type(dm) == type(1.0): # dm is a float
new_val = self.float_list.add()
new_val.v = dm
dmi = len(self.float_list) - 1
elif type(dm) == type(True): # dm is a boolean
new_val = self.bool_list.add()
new_val.v = dm
dmi = len(self.bool_list) - 1
else: # dm is unknown
dmi = None
# print ( "convert_recurse returning a dmi of type " + str(type(dmi)) )
return ( dmi )
def draw_recurse ( self, layout, name, dm, dmi ):
# Draw the structure
if type(dmi) == type ( (0,1) ):
# A tuple represents either a dictionary ({},show_index) or a list ([],show_index)
row = layout.row()
box = row.box()
row = box.row(align=True)
row.alignment = 'LEFT'
if type(dmi[0]) == type({'a':1}):
# Draw a dictionary
dname = str(name) + " {" + str(len(dm)) + "}"
if 'name' in dm.keys():
dname = dname + " (\"" + str(dm['name']) + "\")"
else:
possible_names = [ n for n in dm.keys() if 'name' in n ]
if len(possible_names) > 0:
dname = dname + " (\"" + str(dm[possible_names[0]]) + "\")"
if self.show_list[dmi[1]].v == False:
row.prop ( self.show_list[dmi[1]], "v", text=dname, icon='TRIA_RIGHT', emboss=False )
else:
row.prop ( self.show_list[dmi[1]], "v", text=dname, icon='TRIA_DOWN', emboss=False )
#for k in sorted(dm.keys()):
#for k in dm.keys():
for k in sorted([str(k) for k in dm.keys()]):
self.draw_recurse ( box, k, dm[k], dmi[0][k] )
elif type(dmi[0]) == type(['a',1]):
# Draw a list
dname = str(name) + " [" + str(len(dm)) + "]"
if self.show_list[dmi[1]].v == False:
row.prop ( self.show_list[dmi[1]], "v", text=dname, icon='TRIA_RIGHT', emboss=False )
else:
row.prop ( self.show_list[dmi[1]], "v", text=dname, icon='TRIA_DOWN', emboss=False )
for k in range(len(dm)):
self.draw_recurse ( box, str(name)+'['+str(k)+']', dm[k], dmi[0][k] )
elif (type(dm) == type('a')) or (type(dm) == type(u'a')): #dm is a string
row = layout.row()
row.prop ( self.string_list[dmi], "v", text=str(name) )
elif type(dm) == type(1): # dm is an integer
row = layout.row()
row.prop ( self.int_list[dmi], "v", text=str(name) )
elif type(dm) == type(1.0): # dm is a float
row = layout.row()
row.prop ( self.float_list[dmi], "v", text=str(name) )
elif type(dm) == type(True): # dm is a boolean
row = layout.row()
row.prop ( self.bool_list[dmi], "v", text=str(name) )
else: # dm is unknown
pass
def clear_lists ( self ):
self.show_list.clear()
self.string_list.clear()
self.int_list.clear()
self.float_list.clear()
self.bool_list.clear()
def convert_text_to_properties ( self, context, layout ):
global dm
global dmi
if not self.internal_file_name in bpy.data.texts:
print ( "Error: Specify a script name. Name \"" + self.internal_file_name + "\" is not an internal script name. Try refreshing the scripts list." )
else:
script_text = bpy.data.texts[self.internal_file_name].as_string()
print ( 80*"=" )
print ( script_text )
print ( 80*"=" )
dm = eval ( script_text, locals() )
print ( str(dm) )
# Clear out the properties that will be used for display
self.clear_lists()
dmi = self.convert_recurse ( dm )
print ( "dmi = \n" + str(dmi) )
def convert_dm_to_properties ( self, layout ):
global dm
global dmi
# Clear out the properties that will be used for display
self.clear_lists()
dmi = self.convert_recurse ( dm )
return dmi
class FromDMOperator(bpy.types.Operator):
bl_idname = "browser.from_dm"
bl_label = "Build Tree"
def invoke(self, context, event):
global dm
global dmi
mcell = context.scene.mcell
scripting = mcell.scripting
db = scripting.data_browser
dm = mcell.build_data_model_from_properties ( context, geometry=scripting.include_geometry_in_dm,
scripts=scripting.include_scripts_in_dm,
dyn_geo=scripting.include_dyn_geom_in_dm )
dmi = db.convert_dm_to_properties ( self.layout)
return{'FINISHED'}
class ClearBrowserOperator(bpy.types.Operator):
bl_idname = "browser.clear"
bl_label = "Clear Tree"
def invoke(self, context, event):
global dm
global dmi
mcell = context.scene.mcell
scripting = mcell.scripting
db = scripting.data_browser
db.clear_lists()
dm = None
dmi = None
return{'FINISHED'}
class BrowserOpenAllOperator(bpy.types.Operator):
bl_idname = "browser.open_all"
bl_label = "Open All"
def invoke(self, context, event):
mcell = context.scene.mcell
scripting = mcell.scripting
db = scripting.data_browser
for s in db.show_list:
s.v = True
return{'FINISHED'}
class BrowserCloseAllOperator(bpy.types.Operator):
bl_idname = "browser.close_all"
bl_label = "Close All"
def invoke(self, context, event):
mcell = context.scene.mcell
scripting = mcell.scripting
db = scripting.data_browser
for s in db.show_list:
s.v = False
return{'FINISHED'}
class FromFileOperator(bpy.types.Operator):
bl_idname = "browser.from_file"
bl_label = "From File"
def invoke(self, context, event):
db = context.scene.mcell.scripting.data_browser
db.convert_text_to_properties ( context, self.layout)
return{'FINISHED'}
class ToFileOperator(bpy.types.Operator):
bl_idname = "browser.to_file"
bl_label = "To File"
def invoke(self, context, event):
return{'FINISHED'}
"""
class Data_Browser_Panel(bpy.types.Panel):
bl_label = "Data Browser"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_category = "Data Browser"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
db = context.scene.data_browser
db.draw_layout(context, self.layout)
"""
#############################
# End Data_Browser code #
#############################
# Scripting Operators:
def update_available_scripts ( scripting ):
#mdl_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="MDL Scripts")
#python_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="Python Scripts")
# Delete current scripts list
while scripting.internal_mdl_scripts_list:
scripting.internal_mdl_scripts_list.remove(0)
while scripting.internal_python_scripts_list:
scripting.internal_python_scripts_list.remove(0)
# Find the current internal scripts
for txt in bpy.data.texts:
# print ( "\n" + txt.name + "\n" + txt.as_string() + "\n" )
scripting.internal_python_scripts_list.add()
index = len(scripting.internal_python_scripts_list)-1
scripting.internal_python_scripts_list[index].name = txt.name
class MCELL_OT_scripting_add(bpy.types.Operator):
bl_idname = "mcell.scripting_add"
bl_label = "Add Script"
bl_description = "Add a new script to the model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scripting = context.scene.mcell.scripting
scripting.scripting_list.add()
scripting.active_scripting_index = len(scripting.scripting_list)-1
check_scripting(self, context)
update_available_scripts ( scripting )
return {'FINISHED'}
class MCELL_OT_mcell4_scripting_add(bpy.types.Operator):
bl_idname = "mcell.mcell4_scripting_add"
bl_label = "Add Script"
bl_description = "Add a new script to the model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scripting = context.scene.mcell.scripting
scripting.mcell4_scripting_list.add()
scripting.active_mcell4_scripting_index = len(scripting.mcell4_scripting_list)-1
check_scripting(self, context)
update_available_scripts ( scripting )
return {'FINISHED'}
class MCELL_OT_scripting_remove(bpy.types.Operator):
bl_idname = "mcell.scripting_remove"
bl_label = "Remove Script"
bl_description = "Remove selected script from the model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scripting = context.scene.mcell.scripting
scripting.scripting_list.remove(scripting.active_scripting_index)
scripting.active_scripting_index -= 1
if (scripting.active_scripting_index < 0):
scripting.active_scripting_index = 0
if scripting.scripting_list:
check_scripting(self, context)
update_available_scripts ( scripting )
return {'FINISHED'}
class MCELL_OT_mcell4_scripting_remove(bpy.types.Operator):
bl_idname = "mcell.mcell4_scripting_remove"
bl_label = "Remove Script"
bl_description = "Remove selected script from the model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scripting = context.scene.mcell.scripting
scripting.mcell4_scripting_list.remove(scripting.active_mcell4_scripting_index)
scripting.active_mcell4_scripting_index -= 1
if (scripting.active_mcell4_scripting_index < 0):
scripting.active_mcell4_scripting_index = 0
if scripting.mcell4_scripting_list:
check_scripting(self, context)
update_available_scripts ( scripting )
return {'FINISHED'}
class MCELL_OT_scripting_refresh(bpy.types.Operator):
bl_idname = "mcell.scripting_refresh"
bl_label = "Refresh Files"
bl_description = "Refresh the list of available script files"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scripting = context.scene.mcell.scripting
check_scripting(self, context)
update_available_scripts ( scripting )
return {'FINISHED'}
class MCELL_OT_scripting_execute(bpy.types.Operator):
bl_idname = "mcell.scripting_execute"
bl_label = "Execute Script on Current Data Model"
bl_description = "Execute the selected script to REPLACE the current data model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scripting = context.scene.mcell.scripting
print ( "Executing Script" )
scripting.execute_selected_script(context)
return {'FINISHED'}
class CopyDataModelFromSelectedProps(bpy.types.Operator):
'''Copy the selected data model section to the Clipboard'''
bl_idname = "cb.copy_sel_data_model_to_cbd"
bl_label = "Copy"
bl_description = "Copy the selected data model section to the Clipboard"
def execute(self, context):
print ( "Copying CellBlender Data Model:" )
mcell = context.scene.mcell
scripting = mcell.scripting
section = str(mcell.scripting.dm_section)
print ( "Copying section " + section )
full_dm = mcell.build_data_model_from_properties ( context, geometry=scripting.include_geometry_in_dm,
scripts=scripting.include_scripts_in_dm,
dyn_geo=scripting.include_dyn_geom_in_dm )
selected_dm = full_dm
selected_key = "dm['mcell']"
if section != 'ALL':
if section in full_dm:
selected_dm = full_dm[section]
else:
selected_dm = ""
selected_key += '[\'' + section + '\']'
# Clean up selected_dm as desired
if 'mol_viz' in selected_dm:
selected_dm['mol_viz']['file_dir'] = ""
else:
if ('file_dir' in selected_dm) and ('viz_list' in selected_dm):
# This is likely to be a "mol_viz" entry
selected_dm['file_dir'] = ""
#s = "dm['mcell'] = " + pprint.pformat ( selected_dm, indent=4, width=40 ) + "\n"
s = selected_key + " = " + data_model.data_model_as_text ( selected_dm ) + "\n"
#s = "dm['mcell'] = " + str(selected_dm) + "\n"
bpy.context.window_manager.clipboard = s
return {'FINISHED'}
# Scripting callback functions
def check_scripting(self, context):
mcell = context.scene.mcell
scripting_list = mcell.scripting.scripting_list
if len(scripting_list) > 0:
scripting = scripting_list[mcell.scripting.active_scripting_index]
mcell4_scripting_list = mcell.scripting.scripting_list
if len(mcell4_scripting_list) > 0:
mcell4_scripting = mcell4_scripting_list[mcell.scripting.active_mcell4_scripting_index]
return
# Scripting Panel Classes
class MCELL_UL_scripting_item(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
desc = item.get_description()
if item.include_where == "dont_include":
layout.label ( icon='ERROR', text=desc )
else:
layout.label ( icon='CHECKMARK', text=desc )
class MCELL_UL_mcell4_scripting_item(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
desc = item.get_description()
layout.label ( icon='CHECKMARK', text=desc )
# Scripting Property Groups
class CellBlenderScriptingProperty(bpy.types.PropertyGroup):
name: StringProperty(name="Scripting", update=check_scripting)
status: StringProperty(name="Status")
internal_file_name: StringProperty ( name = "Internal File Name" )
external_file_name: StringProperty ( name = "External File Name", subtype='FILE_PATH', default="" )
include_where_enum = [
('before', "Include Before", ""),
('after', "Include After", ""),
("dont_include", "Don't Include", "")]
include_where: EnumProperty(
items=include_where_enum, name="Include Where",
default='before',
description="Choose relative location to include this script.",
update=check_scripting)
include_section_enum = [
('everything', "Everything", ""),
('parameters', "Parameters", ""),
('initialization', "Initialization", ""),
('partitions', "Partitions", ""),
("molecules", "Molecules", ""),
("surface_classes", "Surface Classes", ""),
("reactions", "Reactions", ""),
("geometry", "Geometry", ""),
("mod_surf_regions", "Modify Surface Regions", ""),
("release_patterns", "Release Patterns", ""),
("instantiate", "Instantiate Objects", ""),
("release_sites", "Release Sites", ""),
("seed", "Seed", ""),
("viz_output", "Visualization Output", ""),
("rxn_output", "Reaction Output", "")]
include_section: EnumProperty(
items=include_section_enum, name="Include Section",
default='initialization',
description="Choose MDL section to include this script.",
update=check_scripting)
internal_external_enum = [
('internal', "Internal", ""),
("external", "External", "")]
internal_external: EnumProperty(
items=internal_external_enum, name="Internal/External",
default='internal',
description="Choose location of file (internal text or external file).",
update=check_scripting)
mdl_python_enum = [
('mdl', "MDL", ""),
("python", "Python", "")]
mdl_python: EnumProperty(
items=mdl_python_enum, name="MDL/Python",
default='mdl',
description="Choose type of scripting (MDL or Python).",
update=check_scripting)
def init_properties ( self, parameter_system ):
pass
def build_data_model_from_properties ( self, context ):
print ( "Scripting Item building Data Model" )
dm = {}
dm['data_model_version'] = "DM_2016_03_15_1900"
dm['name'] = self.name
dm['internal_file_name'] = self.internal_file_name
dm['external_file_name'] = self.external_file_name
dm['include_where'] = self.include_where
dm['include_section'] = self.include_section
dm['internal_external'] = self.internal_external
dm['mdl_python'] = self.mdl_python
return dm
@staticmethod
def upgrade_data_model ( dm ):
# Upgrade the data model as needed. Return updated data model or None if it can't be upgraded.
print ( "------------------------->>> Upgrading CellBlenderScriptingProperty Data Model" )
# Upgrade the data model as needed
if not ('data_model_version' in dm):
# Make changes to move from unversioned to DM_2016_03_15_1900
dm['data_model_version'] = "DM_2016_03_15_1900"
# Check that the upgraded data model version matches the version for this property group
if dm['data_model_version'] != "DM_2016_03_15_1900":
data_model.flag_incompatible_data_model ( "Error: Unable to upgrade CellBlenderScriptingProperty data model to current version." )
return None
return dm
def build_properties_from_data_model ( self, context, dm ):
# Check that the data model version matches the version for this property group
if dm['data_model_version'] != "DM_2016_03_15_1900":
data_model.handle_incompatible_data_model ( "Error: Unable to upgrade CellBlenderScriptingProperty data model to current version." )
self.init_properties(context.scene.mcell.parameter_system)
self.name = dm["name"]
self.internal_file_name = dm["internal_file_name"]
self.external_file_name = dm["external_file_name"]
self.include_where = dm["include_where"]
self.include_section = dm["include_section"]
self.internal_external = dm["internal_external"]
self.mdl_python = dm["mdl_python"]
def get_description ( self ):
desc = ""
if self.include_where == "dont_include":
desc = "Don't include "
if self.internal_external == "internal":
desc += "internal \"" + self.internal_file_name + "\" "
if self.internal_external == "external":
desc += "external \"" + self.external_file_name + "\" "
else:
int_ext = ""
fname = ""
if self.internal_external == "internal":
int_ext = "internal "
fname = "\"" + self.internal_file_name + "\" "
if self.internal_external == "external":
int_ext = "external "
fname = "\"" + self.external_file_name + "\" "
where = ""
if self.include_where == "before":
where = "before "
if self.include_where == "after":
where = "after "
mdl_py = self.mdl_python + " "
desc = "Include " + int_ext + mdl_py + fname + where + self.include_section
return ( desc )
def draw_layout ( self, context, layout ):
mcell = context.scene.mcell
ps = mcell.parameter_system
if not mcell.initialized:
mcell.draw_uninitialized ( layout )
else:
row = layout.row()
row.prop(self, "internal_external", expand=True)
row.prop(self, "mdl_python", expand=True)
row = layout.row()
if (self.internal_external == "internal"):
if (self.mdl_python == "mdl"):
row.prop_search ( self, "internal_file_name",
context.scene.mcell.scripting, "internal_mdl_scripts_list",
text="File:", icon='TEXT' )
row.operator("mcell.scripting_refresh", icon='FILE_REFRESH', text="")
"""
layout.label ( "Internal MDL Scripts:" )
for txt in context.scene.mcell.scripting.internal_mdl_scripts_list:
box = layout.box()
box.label ( bpy.data.texts[txt.name].name )
box.label ( bpy.data.texts[txt.name].as_string() )
"""
if (self.mdl_python == "python"):
row.prop_search ( self, "internal_file_name",
context.scene.mcell.scripting, "internal_python_scripts_list",
text="File:", icon='TEXT' )
row.operator("mcell.scripting_refresh", icon='FILE_REFRESH', text="")
"""
layout.label ( "Internal Python Scripts:" )
for txt in context.scene.mcell.scripting.internal_python_scripts_list:
box = layout.box()
box.label ( bpy.data.texts[txt.name].name )
box.label ( bpy.data.texts[txt.name].as_string() )
"""
if (self.internal_external == "external"):
row.prop ( self, "external_file_name" )
row.operator("mcell.scripting_refresh", icon='FILE_REFRESH', text="")
row = layout.row()
row.prop(self, "include_where", text="", expand=False)
row.prop(self, "include_section", text="", expand=False)
class CellBlenderMCell4ScriptingProperty(bpy.types.PropertyGroup):
name: StringProperty(name="Scripting", update=check_scripting)
status: StringProperty(name="Status")
internal_file_name: StringProperty ( name = "Internal File Name" )
external_file_name: StringProperty ( name = "External File Name", subtype='FILE_PATH', default="" )
internal_external_enum = [
('internal', "Internal", ""),
("external", "External", "")]
internal_external: EnumProperty(
items=internal_external_enum, name="Internal/External",
default='internal',
description="Choose location of file (internal text or external file).",
update=check_scripting)
def init_properties ( self, parameter_system ):
pass
def build_data_model_from_properties ( self, context ):
print ( "Scripting Item building Data Model" )
dm = {}
dm['data_model_version'] = "DM_2016_03_15_1900"
dm['name'] = self.name
dm['internal_file_name'] = self.internal_file_name
dm['external_file_name'] = self.external_file_name
dm['internal_external'] = self.internal_external
return dm
@staticmethod
def upgrade_data_model ( dm ):
# Upgrade the data model as needed. Return updated data model or None if it can't be upgraded.
return dm
def build_properties_from_data_model ( self, context, dm ):
# Check that the data model version matches the version for this property group
if dm['data_model_version'] != "DM_2016_03_15_1900":
data_model.handle_incompatible_data_model ( "Error: Unable to upgrade CellBlenderScriptingProperty data model to current version." )
self.init_properties(context.scene.mcell.parameter_system)
self.name = dm["name"]
self.internal_file_name = dm["internal_file_name"]
self.external_file_name = dm["external_file_name"]
self.internal_external = dm["internal_external"]
def get_description ( self ):
desc = ""
int_ext = ""
fname = ""
if self.internal_external == "internal":
int_ext = "internal "
fname = "\"" + self.internal_file_name + "\" "
if self.internal_external == "external":
int_ext = "external "
fname = "\"" + self.external_file_name + "\" "
desc = "Include " + int_ext + fname
return ( desc )
def draw_layout ( self, context, layout ):
mcell = context.scene.mcell
ps = mcell.parameter_system
if not mcell.initialized:
mcell.draw_uninitialized ( layout )
else:
row = layout.row()
row.prop(self, "internal_external", expand=True)
row = layout.row()
if (self.internal_external == "internal"):
row.prop_search ( self, "internal_file_name",
context.scene.mcell.scripting, "internal_python_scripts_list",
text="File:", icon='TEXT' )
row.operator("mcell.scripting_refresh", icon='FILE_REFRESH', text="")
if (self.internal_external == "external"):
row.prop ( self, "external_file_name" )
row.operator("mcell.scripting_refresh", icon='FILE_REFRESH', text="")
row = layout.row()
class CellBlenderScriptProperty(bpy.types.PropertyGroup):
name: StringProperty(name="Script")
class CellBlenderScriptingPropertyGroup(bpy.types.PropertyGroup):
active_scripting_index: IntProperty(name="Active Scripting Index", default=0)
scripting_list: CollectionProperty(type=CellBlenderScriptingProperty, name="Scripting List")
ignore_cellblender_data: BoolProperty(name="Ignore CellBlender Data", default=False)
internal_mdl_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="MDL Internal Scripts")
external_mdl_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="MDL External Scripts")
internal_python_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="Python Internal Scripts")
external_python_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="Python External Scripts")
active_mcell4_scripting_index: IntProperty(name="Active MCell4 Scripting Index", default=0)
mcell4_scripting_list: CollectionProperty(type=CellBlenderMCell4ScriptingProperty, name="MCell4 Scripting List")
internal_mcell4_scripts_list: CollectionProperty(type=CellBlenderScriptProperty, name="MCell4 Internal Scripts")
show_mcell4_scripting: BoolProperty(name="MCell4 Scripting", default=False)
show_simulation_scripting: BoolProperty(name="Export Scripting", default=False)
show_data_model_scripting: BoolProperty(name="Data Model Scripting", default=False)
show_data_model_script_make: BoolProperty(name="Make Script", default=False)
show_data_model_script_run: BoolProperty(name="Run Script", default=False)
show_data_model_browser: BoolProperty(name="Data Model Browser", default=False)
dm_internal_file_name: StringProperty ( name = "Internal File Name" )
dm_external_file_name: StringProperty ( name = "External File Name", subtype='FILE_PATH', default="" )
force_property_update: BoolProperty(name="Update CellBlender from Data Model", default=True)
# upgrade_data_model_for_script: BoolProperty(name="Upgrade Script", default=False)
# The following properties are associated with Data Model Scripting
include_geometry_in_dm: BoolProperty ( name = "Include Geometry", description = "Include Geometry in the Data Model", default = False )
include_scripts_in_dm: BoolProperty ( name = "Include Scripts", description = "Include Scripts in the Data Model", default = False )
include_dyn_geom_in_dm: BoolProperty ( name = "Dynamic Geometry", description = "Include Dynamic Geometry in the Data Model", default = False )
dm_section_enum = [
('ALL', "All", ""),
('define_molecules', "Molecules", ""),
('define_reactions', "Reactions", ""),
('define_release_patterns', "Release Time Patterns", ""),
('define_surface_classes', "Surface Classes", ""),
('geometrical_objects', "Geometrical Objects", ""),
('initialization', "Initialization", ""),
('materials', "Materials", ""),
('model_objects', "Model Objects", ""),
('modify_surface_regions', "Surface Regions", ""),
('mol_viz', "Molecule Visualization", ""),
('parameter_system', "Parameters", ""),
('reaction_data_output', "Plot Data", ""),
('release_sites', "Release Sites", ""),
('simulation_control', "Simulation Control", ""),
('viz_output', "Visualization Data", "")]
dm_section: EnumProperty(
items=dm_section_enum, name="Data Model Section",
default='define_molecules',
description="Data Model Section to copy to the Clipboard" )
dm_internal_external_enum = [
('internal', "Internal", ""),
("external", "External", "")]
dm_internal_external: EnumProperty(
items=dm_internal_external_enum, name="Internal/External",
default='internal',
description="Choose location of file (internal text or external file).",
update=check_scripting)
data_browser: PointerProperty(type=DataBrowserPropertyGroup)
def init_properties ( self, parameter_system ):
pass
def build_data_model_from_properties ( self, context, scripts=False ):
dm = {}
dm['data_model_version'] = "DM_2017_11_30_1830"
dm['ignore_cellblender_data'] = self.ignore_cellblender_data
#dm['show_simulation_scripting'] = self.show_simulation_scripting
#dm['show_data_model_scripting'] = self.show_data_model_scripting
dm['dm_internal_file_name'] = self.dm_internal_file_name
dm['dm_external_file_name'] = self.dm_external_file_name
dm['force_property_update'] = self.force_property_update
s_list = []
for s in self.scripting_list:
s_list.append ( s.build_data_model_from_properties(context) )
dm['scripting_list'] = s_list
s4_list = []
for s in self.mcell4_scripting_list:
s4_list.append ( s.build_data_model_from_properties(context) )
dm['mcell4_scripting_list'] = s4_list
# Don't: Store the scripts lists in the data model for now - they are regenerated when rebuilding properties
# Do: Store all .mdl text files and all .py text files if scripts flag is True (defaults to false)
texts = {}
if scripts:
for txt in bpy.data.texts:
texts[txt.name] = txt.as_string()
dm['script_texts'] = texts
return dm
@staticmethod
def upgrade_data_model ( dm ):
# Upgrade the data model as needed. Return updated data model or None if it can't be upgraded.
print ( "------------------------->>> Upgrading CellBlenderScriptingPropertyGroup Data Model" )
# Upgrade the data model as needed
if not ('data_model_version' in dm):
# Make changes to move from unversioned to DM_2016_03_15_1900
dm['data_model_version'] = "DM_2016_03_15_1900"
if dm['data_model_version'] == "DM_2016_03_15_1900":
# Add the ignore_cellblender_data flag as False (the prior behaviour before this change)
dm['ignore_cellblender_data'] = False
dm['data_model_version'] = "DM_2017_11_30_1830"
# Check that the upgraded data model version matches the version for this property group
if dm['data_model_version'] != "DM_2017_11_30_1830":
data_model.flag_incompatible_data_model ( "Error: Unable to upgrade CellBlenderScriptingPropertyGroup data model to current version." )
return None
return dm
def build_properties_from_data_model ( self, context, dm, scripts=True ):
# Check that the data model version matches the version for this property group
if dm['data_model_version'] != "DM_2017_11_30_1830":
data_model.handle_incompatible_data_model ( "Error: Unable to upgrade CellBlenderScriptingPropertyGroup data model to current version." )
self.init_properties(context.scene.mcell.parameter_system)
self.ignore_cellblender_data = dm['ignore_cellblender_data']
#self.show_simulation_scripting = dm["show_simulation_scripting"]
#self.show_data_model_scripting = dm["show_data_model_scripting"]
self.dm_internal_file_name = dm["dm_internal_file_name"]
self.dm_external_file_name = dm["dm_external_file_name"]
self.force_property_update = dm["force_property_update"]
while len(self.scripting_list) > 0:
self.scripting_list.remove(0)
if "scripting_list" in dm:
for dm_s in dm["scripting_list"]:
self.scripting_list.add()
self.active_scripting_index = len(self.scripting_list)-1
s = self.scripting_list[self.active_scripting_index]
# s.init_properties(context.scene.mcell.parameter_system)
s.build_properties_from_data_model ( context, dm_s )
while len(self.mcell4_scripting_list) > 0:
self.mcell4_scripting_list.remove(0)
if "mcell4_scripting_list" in dm:
for dm_s in dm["mcell4_scripting_list"]:
self.mcell4_scripting_list.add()
self.active_mcell4_scripting_index = len(self.mcell4_scripting_list)-1
s = self.mcell4_scripting_list[self.active_mcell4_scripting_index]
# s.init_properties(context.scene.mcell.parameter_system)
s.build_properties_from_data_model ( context, dm_s )
if scripts:
print ( "\nReading scripts because \"scripts\" parameter is true\n" )
if 'script_texts' in dm:
for key_name in dm['script_texts'].keys():
print ( " Script: " + key_name )
if key_name in bpy.data.texts:
bpy.data.texts[key_name].clear()
else:
bpy.data.texts.new(key_name)
bpy.data.texts[key_name].write ( dm['script_texts'][key_name] )
else:
print ( "\nNot reading scripts because \"scripts\" parameter is false\n" )
# Update the list of available scripts (for the user interface list)