-
Notifications
You must be signed in to change notification settings - Fork 12
/
cellblender_molmaker.py
1952 lines (1605 loc) · 82.7 KB
/
cellblender_molmaker.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 #####
### NOTE: Portions of this were intentionally written to be more "C-like" than "Pythonic"
# This code is based on text representation that looks like this:
"""
[0] = A (m) at (0,0,0) with peers [1, 2, 3, 4, 5, 6, 7]
[1] = a (c) at (0.009999999776482582,0.0,0.0) with peers [0] <7,0.0>
[2] = a (c) at (-0.009999999776482582,0.0,0.0) with peers [0] <7,0.0>
[3] = a (c) at (0.0,0.009999999776482582,0.0) with peers [0] <7,0.0>
[4] = a (c) at (0.0,-0.009999999776482582,0.0) with peers [0] <7,0.0>
[5] = a (c) at (0.0,0.0,0.009999999776482582) with peers [0] <7,0.0>
[6] = a (c) at (0.0,0.0,-0.009999999776482582) with peers [0, 9] <7,0.0>
[7] = a (k) at (0.0,0.0,0.03) with peers [0]
[8] = A (m) at (-6.12323385887182e-19,0.0,-0.019999999552965164) with peers [9, 10, 11, 12, 13, 14, 15]
[9] = a (c) at (0.0,0.0,-0.009999999776482582) with peers [8, 6] <15,0.0>
[10] = a (c) at (-1.224646771774364e-18,0.0,-0.029999999329447746) with peers [8] <15,0.0>
[11] = a (c) at (0.009999999776482582,6.123233858871821e-19,-0.019999999552965164) with peers [8] <15,0.0>
[12] = a (c) at (-0.009999999776482582,-6.123233858871821e-19,-0.019999999552965164) with peers [8] <15,0.0>
[13] = a (c) at (-1.224646771774364e-18,0.009999999776482582,-0.019999999552965164) with peers [8] <15,0.0>
[14] = a (c) at (9.62964972193618e-35,-0.009999999776482582,-0.019999999552965164) with peers [8, 17] <15,0.0>
[15] = a (k) at (-2.4492935846082116e-18,0.03,-0.019999999552965164) with peers [8]
[16] = A (m) at (-6.123233858871819e-19,-0.019999999552965164,-0.019999999552965164) with peers [17, 18, 19, 20, 21, 22, 23]
[17] = a (c) at (9.62964972193618e-35,-0.009999999776482582,-0.019999999552965164) with peers [16, 14] <23,0.0>
[18] = a (c) at (-1.224646771774364e-18,-0.029999999329447746,-0.019999999552965164) with peers [16] <23,0.0>
[19] = a (c) at (-1.224646771774364e-18,-0.019999999552965164,-0.029999999329447746) with peers [16] <23,0.0>
[20] = a (c) at (1.925929944387236e-34,-0.019999999552965164,-0.009999999776482582) with peers [16] <23,0.0>
[21] = a (c) at (-0.009999999776482582,-0.019999999552965164,-0.019999999552965164) with peers [16] <23,0.0>
[22] = a (c) at (0.009999999776482582,-0.019999999552965164,-0.019999999552965164) with peers [16] <23,0.0>
[23] = a (k) at (-0.03,-0.01999999955296516,-0.01999999955296516) with peers [16]
"""
# This example text was printed by CellBlender's MolMaker code.
# The format shown here can be used as an "interchange" format.
# This example shows a complex of 3 "A" molecules (each denoted with an "(m)").
# Each "A" molecule has 6 "normal" components and one "key" component (all named "a").
# Normal components are denoted with a "(c)" and key components are denoted with a "(k)".
# All final coordinate positions are relative to the molecule located at the origin (0,0,0).
# All molecules, components, and keys are related through their "peer" lists.
# All entries in any "peer" list refer to the item's index in the table (such as "[3]").
# Each molecule lists all of its components (both "normal" and "key") in its list of peers.
# All of a molecule's peers will be rotated and translated along with the molecule itself.
# Each non-molecule ("component" or "key") has a list of either 1 or 2 peers.
# The first peer of each non-molecule is an index to that item's molecule.
# The second peer of each non-molecule (if it exists) references a binding partner.
# The binding between molecules is done by co-locating the binding components.
# Any two bound molecules and their respective binding components are collinear.
# The angle brackets "<...>" define rotation angles about the collinear binding axis.
# A rotation angle is specified as "reference,angle" pair: <reference,angle>
# The "reference" in a rotation angle specification is an index into that molecule's table.
# A rotation "reference" may refer to either a component (c) or a key (k) in that molecule.
# Rotation Alignment Planes are defined by the ordered triplet: (molecule, component, reference).
# Rotation Alignment Planes must be defined (molecule, component, reference must not be collinear).
# Rotational Alignment between two molecules specifies an angle between their Alignment Planes.
# The binding angle between bound Alignment Planes is the sum of the two angles in each component.
# The rotation angle is interpreted as counter-clockwise viewed from its associated molecule.
# When both rotation angles are 0, the normals to the Rotation Alignment Planes are opposites.
import bpy
from bpy.props import StringProperty
from bpy.props import BoolProperty
from bpy.props import IntProperty
from bpy.props import FloatProperty
from bpy.props import EnumProperty
from bpy.props import FloatVectorProperty
from bpy.props import IntVectorProperty
from bpy.props import CollectionProperty
from bpy.props import PointerProperty
from bpy.app.handlers import persistent
import math
import mathutils
import cellblender
from . import data_model
bl_info = {
"name": "Molecule Maker",
"author": "Bob Kuczewski",
"version": (1,1,0),
"blender": (2,93,0),
"location": "View 3D > Edit Mode > Tool Shelf",
"description": "Generate a Molecule",
"warning" : "",
"wiki_url" : "http://mcell.org",
"tracker_url" : "",
"category": "Add Mesh",
}
def checked_print ( s ):
context = bpy.context
mcell = context.scene.mcell
molmaker = mcell.molmaker
if molmaker.print_debug:
print ( s )
def update_available_scripts ( molmaker ):
# Delete current scripts list
while molmaker.molecule_texts_list:
molmaker.molecule_texts_list.remove(0)
# Find the current internal scripts and add them to the list
for txt in bpy.data.texts:
checked_print ( "\n" + txt.name + "\n" + txt.as_string() + "\n" )
if True or (txt.name[-3:] == ".py"):
molmaker.molecule_texts_list.add()
index = len(molmaker.molecule_texts_list)-1
molmaker.molecule_texts_list[index].name = txt.name
while molmaker.comp_loc_texts_list:
molmaker.comp_loc_texts_list.remove(0)
# Find the current internal scripts and add them to the list
for txt in bpy.data.texts:
checked_print ( "\n" + txt.name + "\n" + txt.as_string() + "\n" )
if True or (txt.name[-3:] == ".py"):
molmaker.comp_loc_texts_list.add()
index = len(molmaker.comp_loc_texts_list)-1
molmaker.comp_loc_texts_list[index].name = txt.name
class MolMaker_OT_update_files(bpy.types.Operator):
bl_idname = "mol.update_files"
bl_label = "Refresh Files"
bl_description = "Refresh the list of available script files"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
molmaker = context.scene.mcell.molmaker
update_available_scripts ( molmaker )
return {'FINISHED'}
# This one is called by Build 2D and Build 3D
def read_molcomp_data_from_text ( fdata ):
ldata = [ l.strip() for l in fdata.split('\n') if len(l.strip()) > 0 ]
ldata = [ l for l in ldata if not l.startswith('#') ]
molcomp_list = []
for l in ldata:
mc = { # 'line':l,
'ftype':'c',
'has_coords':False,
'is_final':False,
'coords':[0,0,0],
'name':"",
'graph_string':"",
'peer_list':[],
'key_list':[],
'angle': 0,
}
mc['name'] = l.split("=")[1].split('(')[0].strip()
mc['ftype'] = l.split('(')[1][0] # Pull out the type (m or c or k)
mc['coords'] = eval ( '[' + l.split('(')[2].split(')')[0] + ']' )
peer_part = l.split('with peers')[1].strip()
if '[' in peer_part:
peers = peer_part.split('[')[1].split(']')[0].split(',')
peers = [ p for p in peers if len(p) > 0 ] # Remove any empty peers
for p in peers:
mc['peer_list'].append ( int(p) )
if '<' in peer_part:
keys = peer_part.split('<')[1].split('>')[0].split(',')
if mc['ftype'] == 'm':
# Molecules have a plain list of keys to be transformed along with the molecule
keys = [ k for k in keys if len(k) > 0 ] # Remove any empty keys
for k in keys:
mc['key_list'].append ( int(k) )
elif mc['ftype'] == 'c':
# Components have a key and an optional angle referenced to that key
if len(keys) == 1:
mc['key_list'].append ( int(keys[0]) )
mc['angle'] = 0.0
elif len(keys) == 2:
mc['key_list'].append ( int(keys[0]) )
mc['angle'] = ( float(keys[1]) )
else:
print ( "Expected <KeyIndex [, Angle]> for " + mc['name'] + ", but got " + peer_part.split('<')[1] )
molcomp_list.append ( mc )
return molcomp_list
'''
MolDef Text File Format:
# Type M C x y z rfx rfy rfz Unused
XYZRef T t 0.01 0.00 -0.007071067811865476 0 0 0 0
XYZRef T t -0.01 0.00 -0.007071067811865476 0 0 0 0
XYZRef T t 0.00 0.01 0.007071067811865476 0 0 0 0
XYZRef T t 0.00 -0.01 0.007071067811865476 0 0 0 0
XYZRef B t 0.01 0.00 -0.007071067811865476 0 0 0 0
XYZRef B t -0.01 0.00 -0.007071067811865476 0 0 0 0
XYZRef B t 0.00 0.01 0.007071067811865476 0 0 0 0
XYZRef B t 0.00 -0.01 0.007071067811865476 0 0 0 0
XYZRef C c 0.00 0.01 -0.002 0 0 0 0
XYZRef C c 0.00 -0.01 0.002 0 0 0 0
'''
def update_molcomp_list_from_defs ( context, molcomp_list, moldef_text, build_as_3D ):
mcell = context.scene.mcell
mcell_mols = mcell.molecules.molecule_list
molmaker = mcell.molmaker
if moldef_text == None:
# Try reading from a file
if molmaker.comp_loc_text_name in bpy.data.texts:
# There is a component location definition text
checked_print ( "Read component definition text named " + molmaker.comp_loc_text_name )
# Read the component definitions
moldef_text = bpy.data.texts[molmaker.comp_loc_text_name].as_string()
if moldef_text != None:
# There is a component location definition text
# Set the has_coords flag to false on each component of each molecule
for mc in molcomp_list:
if mc['ftype'] == 'm':
mc['coords'] = [0,0,0]
mc['has_coords'] = False
else:
mc['has_coords'] = False
# Read the component definitions
ldata = moldef_text
comploc_lines = [ l.strip() for l in ldata.split('\n') if len(l.strip()) > 0 ]
comploc_lines = [ l for l in comploc_lines if not l.startswith('#') ]
mol_comp_loc_dict = {}
for cll in comploc_lines:
cl_parts = [ p.strip() for p in cll.split() if len(p.strip()) > 0 ]
cl_type = cl_parts[0]
cl_mol_name = cl_parts[1]
cl_comp_name = cl_parts[2]
cl_x = float(cl_parts[3])
cl_y = float(cl_parts[4])
cl_z = float(cl_parts[5])
cl_refx = float(cl_parts[6])
cl_refy = float(cl_parts[7])
cl_refz = float(cl_parts[8])
if not build_as_3D:
checked_print ( "Setting z to zero for " + cl_mol_name + "." + cl_comp_name )
cl_z = 0.0
cl_refz = 0.0
if not (cl_mol_name in mol_comp_loc_dict):
# Make a list for this mol name
mol_comp_loc_dict[cl_mol_name] = []
# Append the current component definition to the list for this molecule
mol_comp_loc_dict[cl_mol_name].append ( { 'cname':cl_comp_name, 'coords':[cl_x, cl_y, cl_z], 'ref':[cl_refx, cl_refy, cl_refz], 'assigned':False } )
# Assign the component locations by molecule
for mc in molcomp_list:
# Only assign to components connected to molecules
if (mc['ftype']=='m') and (len(mc['peer_list']) > 0):
# Clear out the usage for this molecule in the component location table
for c in mol_comp_loc_dict[mc['name']]:
checked_print ( " Clearing assigned for " + c['cname'] + " with coords: " + str(c['coords']) )
c['assigned'] = False
# Sweep through all the components for this molecule
checked_print ( " Assigning coordinates to components in mol " + mc['name'] )
for pi in mc['peer_list']:
# Only assign to components that don't have coords
checked_print ( " Checking peer " + str(pi) )
if not molcomp_list[pi]['has_coords']:
# Sweep through the component name list looking for an unassigned match
for c in mol_comp_loc_dict[mc['name']]:
if not c['assigned']:
if c['cname'] == molcomp_list[pi]['name']:
molcomp_list[pi]['coords'] = [ cc for cc in c['coords'] ]
checked_print ( " Assigning coordinates: " + str(molcomp_list[pi]['coords']) )
molcomp_list[pi]['has_coords'] = True
c['assigned'] = True
break
# This is called directly by the "build as is" operator.
# The SphereCyl format is simpler and intended for rendering.
# The SphereCyl format only contains Spheres ("SphereList") and Cylinders ("CylList").
def read_molcomp_data_SphereCyl ( fdata ):
ldata = [ l.strip() for l in fdata.split('\n') if len(l.strip()) > 0 ]
ldata = [ l for l in ldata if not l.startswith('#') ]
SphereCyl_data = { 'Version':1.1, 'SphereList':[], 'CylList':[], 'FaceList':[] }
this_index = 0
for l in ldata:
checked_print ( "Line: " + l )
m = { 'name':'', 'loc':[0,0,0], 'r':1, 'c':'MolMaker_mol', 'ftype':'' }
m['name'] = l.split("=")[1].split('(')[0].strip()
m['ftype'] = l.split('(')[1][0] # Pull out the type (m or c or k)
if m['ftype'] == 'm':
m['r'] = 0.005
peer_part = l.split('with peers')[1].strip()
if '[' in peer_part:
peers = peer_part.split('[')[1].split(']')[0].split(',')
peers = [ p for p in peers if len(p) > 0 ] # Remove any empty peers
for p in peers:
SphereCyl_data['CylList'].append ( [this_index, int(p), 0.001] ) # Add the connecting bond cylinder
checked_print ( " Bond: " + str(this_index) + " " + p.strip() )
elif m['ftype'] == 'c':
m['r'] = 0.0025
m['c'] = 'MolMaker_comp'
elif m['ftype'] == 'k':
m['r'] = 0.0015
m['c'] = 'MolMaker_key'
m['loc'] = eval ( '[' + l.split('(')[2].split(')')[0] + ']' )
SphereCyl_data['SphereList'].append ( m )
this_index += 1
return SphereCyl_data
# This converts MolComp format to SphereCyl format for simple 3D rendering.
def MolComp_to_SphereCyl ( molcomp_list, build_as_3D ):
SphereCyl_data = { 'Version':1.1, 'SphereList':[], 'CylList':[], 'FaceList':[] }
this_index = 0
for mc in molcomp_list:
m = { 'name':mc['name'], 'loc':mc['coords'], 'r':1, 'c':'MolMaker_mol', 'ftype':mc['ftype'] }
if mc['ftype'] == 'm':
m['r'] = 0.005
if len(mc['peer_list']) > 0:
peers = mc['peer_list']
for p in peers:
if molcomp_list[int(p)]['ftype'] != 'k':
SphereCyl_data['CylList'].append ( [this_index, int(p), 0.001] ) # Add the connecting bond cylinder
if build_as_3D and len(molcomp_list[int(p)]['key_list']) > 0:
# This component has a key
k = molcomp_list[int(p)]['key_list'][0]
SphereCyl_data['FaceList'].append ( [ mc['coords'], molcomp_list[p]['coords'], molcomp_list[k]['coords'] ] ) # , [0.01,0.01,0.0 1] ] )
elif mc['ftype'] == 'c':
m['r'] = 0.0025
m['c'] = 'MolMaker_comp'
elif mc['ftype'] == 'k':
m['r'] = 0.0015
m['c'] = 'MolMaker_key'
SphereCyl_data['SphereList'].append ( m )
this_index += 1
return SphereCyl_data
def dump_molcomp_list ( molcomp_list ):
i = 0
for mc in molcomp_list:
mc_str = "[" + str(i) + "] = " + mc['name'] + " (" + mc['ftype'] + ") at (" + str(mc['coords'][0]) + "," + str(mc['coords'][1]) + "," + str(mc['coords'][2]) + ") with peers " + str(mc['peer_list'])
if len(mc['key_list']) > 0:
mc_str += " <" + str(mc['key_list'][0]) + "," + str(mc['angle']) + ">"
checked_print ( mc_str )
i += 1
class MolMaker_OT_build_as_is(bpy.types.Operator):
bl_idname = "mol.build_as_is"
bl_label = "Build As Is"
bl_description = "Build a molecule from the coordinates in the file"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
checked_print ( "Build Molecule from values in file" )
molmaker = context.scene.mcell.molmaker
fdata = bpy.data.texts[molmaker.molecule_text_name].as_string()
checked_print ( "Read:\n" + fdata )
mol_data = read_molcomp_data_SphereCyl ( fdata )
new_blender_mol_from_SphereCyl_data ( context, mol_data, show_key_planes=molmaker.show_key_planes )
return {'FINISHED'}
def get_distributed_sphere_points ( num_points ):
points = None
if num_points == 0: # Define the single point along the x axis
points = [ ]
elif num_points == 1: # Define the single point along the x axis
points = [ [ 1, 0, 0 ] ]
elif num_points == 2: # Define the two points along the x axis
points = [ [ 1, 0, 0 ], [ -1, 0, 0 ] ]
elif num_points == 3: # Define an equilateral triangle in the x-y plane with one point on x axis
sr3o2 = math.sqrt(3.0) / 2.0
points = [ [ 1, 0, 0 ], [ -0.5, sr3o2, 0 ], [ -0.5, -sr3o2, 0 ] ]
elif num_points == 4: # Define the points on a tetrahedron
oosr2 = 1.0 / math.sqrt(2.0)
points = [ [ 1, 0, -oosr2 ], [ -1, 0, -oosr2 ], [ 0, 1, oosr2 ], [ 0, -1, oosr2 ] ]
elif num_points == 5: # The "best" answer isn't clear, so place one on each pole and 3 around the "equator"
sr3o2 = math.sqrt(3.0) / 2.0
points = [ [ 0, 0, 1 ], [ 1, 0, 0 ], [ -0.5, sr3o2, 0 ], [ -0.5, -sr3o2, 0 ], [ 0, 0, -1 ] ]
elif num_points == 6: # Define 2 points on each axis (x, y, z)
points = [
[ 1, 0, 0 ],
[-1, 0, 0 ],
[ 0, 1, 0 ],
[ 0,-1, 0 ],
[ 0, 0, 1 ],
[ 0, 0,-1 ] ]
elif num_points == 8: # Define 8 points at the corners of a cube
d = 1.0 / math.sqrt(3)
points = [
[ d, d, d ],
[ d, d, -d ],
[ d, -d, d ],
[ d, -d, -d ],
[ -d, d, d ],
[ -d, d, -d ],
[ -d, -d, d ],
[ -d, -d, -d ] ]
else: # Use the Fibonacci Sphere Algorithm ("Golden Spiral") for any undefined number of points
points = [ [0,0,0] for i in range(num_points) ]
rnd = 1
offset = 2.0 / num_points
increment = math.pi * (3.0 - math.sqrt(5.0))
for i in range(num_points):
y = ((i * offset) -1) + (offset / 2)
r = math.sqrt ( 1 - math.pow(y,2) )
phi = ( (i+rnd) % num_points ) * increment
x = math.cos(phi) * r
z = math.sin(phi) * r
points[i][0] = x
points[i][1] = y
points[i][2] = z
return ( points )
def set_component_positions_3D ( mc ):
scale = 0.02;
for mi in range(len(mc)):
if mc[mi]['ftype'] == 'm':
#### fprintf ( stdout, "Setting component positions for %s\n", mc[mi].name );
mc[mi]['coords'] = [0,0,0]
# Because the molecule's peer list includes keys, they must be removed by creating a separate list of components only
component_index_list = [ ci for ci in range(len(mc[mi]['peer_list'])) if mc[mc[mi]['peer_list'][ci]]['ftype'] == 'c' ]
num_points = len(component_index_list)
points = get_distributed_sphere_points ( num_points )
for ci in component_index_list:
mc[mc[mi]['peer_list'][ci]]['coords'][0] = scale * points[ci][0];
mc[mc[mi]['peer_list'][ci]]['coords'][1] = scale * points[ci][1];
mc[mc[mi]['peer_list'][ci]]['coords'][2] = scale * points[ci][2];
#### fprintf ( stdout, " Component %s is at (%g,%g)\n", mc[mc[mi].peers[ci]].name, mc[mc[mi].peers[ci]].x, mc[mc[mi].peers[ci]].y );
elif mc[mi]['ftype'] == 'k':
# Set the rotation keys straight up in Z?
#mc[mi]['coords'][0] = 0
#mc[mi]['coords'][1] = 0
#mc[mi]['coords'][2] = 1 * scale
# Actually, do nothing while testing
pass
def set_component_positions_2D ( mc ):
scale = 0.02;
for mi in range(len(mc)):
if mc[mi]['ftype'] == 'm':
#### fprintf ( stdout, "Setting component positions for %s\n", mc[mi].name );
mc[mi]['coords'] = [0,0,0]
# Because the molecule's peer list includes keys, they must be removed by creating a separate list of components only
component_index_list = [ ci for ci in range(len(mc[mi]['peer_list'])) if mc[mc[mi]['peer_list'][ci]]['ftype'] == 'c' ]
for ci in component_index_list:
angle = 2 * math.pi * ci / len(component_index_list)
mc[mc[mi]['peer_list'][ci]]['coords'][0] = scale * math.cos(angle)
mc[mc[mi]['peer_list'][ci]]['coords'][1] = scale * math.sin(angle)
mc[mc[mi]['peer_list'][ci]]['coords'][2] = 0
#### fprintf ( stdout, " Component %s is at (%g,%g)\n", mc[mc[mi].peers[ci]].name, mc[mc[mi].peers[ci]].x, mc[mc[mi].peers[ci]].y );
elif mc[mi]['ftype'] == 'k':
# For 2D, the rotation keys should be straight up in Z
mc[mi]['coords'][0] = 0
mc[mi]['coords'][1] = 0
mc[mi]['coords'][2] = 1 * scale
def bind_molecules_at_components ( mc, fixed_comp_index, var_comp_index, build_as_3D, axial_rotation=True, bending_rotation=False ):
# Bind these two molecules by aligning their axes and shifting to align their components
# The "fixed" parts (components and molecules) will NOT have their locations changed.
# The "variable" parts (components and molecules) will be transformed to properly match up with the "fixed" parts.
# num_parts = len(mc)
checked_print ( " Binding " + str(mc[fixed_comp_index]['name']) + " to " + str(mc[var_comp_index]['name']) );
##### dump_molcomp_array(mc,num_parts);
fixed_mol_index = mc[fixed_comp_index]['peer_list'][0]
var_mol_index = mc[var_comp_index]['peer_list'][0]
# These vectors are either 2D (x,y) or 3D (x,y,z)
fixed_vec = []
var_vec = []
fixed_vec.append ( mc[fixed_comp_index]['coords'][0] - mc[fixed_mol_index]['coords'][0] )
fixed_vec.append ( mc[fixed_comp_index]['coords'][1] - mc[fixed_mol_index]['coords'][1] )
var_vec.append ( mc[ var_comp_index]['coords'][0] - mc[ var_mol_index]['coords'][0] )
var_vec.append ( mc[ var_comp_index]['coords'][1] - mc[ var_mol_index]['coords'][1] )
if build_as_3D:
fixed_vec.append ( mc[fixed_comp_index]['coords'][2] - mc[fixed_mol_index]['coords'][2] )
var_vec.append ( mc[ var_comp_index]['coords'][2] - mc[ var_mol_index]['coords'][2] )
fixed_mag = None
var_mag = None
if build_as_3D:
fixed_mag = math.sqrt ( (fixed_vec[0]*fixed_vec[0]) + (fixed_vec[1]*fixed_vec[1]) + (fixed_vec[2]*fixed_vec[2]) )
var_mag = math.sqrt ( ( var_vec[0]* var_vec[0]) + ( var_vec[1]* var_vec[1]) + ( var_vec[2]* var_vec[2]) )
else:
fixed_mag = math.sqrt ( (fixed_vec[0]*fixed_vec[0]) + (fixed_vec[1]*fixed_vec[1]) )
var_mag = math.sqrt ( ( var_vec[0]* var_vec[0]) + ( var_vec[1]* var_vec[1]) )
dot_prod = None
if build_as_3D:
dot_prod = (fixed_vec[0] * var_vec[0]) + (fixed_vec[1] * var_vec[1]) + (fixed_vec[2] * var_vec[2])
else:
dot_prod = (fixed_vec[0] * var_vec[0]) + (fixed_vec[1] * var_vec[1])
# In general, the magnitudes should be checked for 0.
# However, in this case, they were generated as non-zero.
norm_dot_prod = dot_prod / ( fixed_mag * var_mag )
# Ensure that the dot product is a legal argument for the "acos" function:
if norm_dot_prod > 1:
print ( "Numerical Warning: normalized dot product " + str( ) + " was greater than 1" )
norm_dot_prod = 1
if norm_dot_prod < -1:
print ( "Numerical Warning: normalized dot product " + str(norm_dot_prod) + " was less than 11" )
norm_dot_prod = -1
##### fprintf ( stdout, "norm_dot_prod = %g\n", norm_dot_prod );
angle = math.acos ( norm_dot_prod )
##### fprintf ( stdout, "Angle (from acos) = %g\n", angle );
if not build_as_3D:
# Try using a 2D "cross product" to fix the direction issue
##### fprintf ( stdout, "2D Cross of (%g,%g) X (%g,%g) = %g\n", fixed_vec[0], fixed_vec[1], var_vec[0], var_vec[1], (fixed_vec[0] * var_vec[1]) - (fixed_vec[1] * var_vec[0]) );
if ( (fixed_vec[0] * var_vec[1]) - (fixed_vec[1] * var_vec[0]) ) > 0:
angle = -angle
else:
# This seems to be required to get everything right:
angle = -angle
# Reverse the direction since we want the components attached to each other
angle = math.pi + angle;
# Bound the angle between -PI and PI
while angle > math.pi:
angle = angle - (2 * math.pi)
while angle <= -math.pi:
angle = angle + (2 * math.pi)
#angle = -angle;
##### fprintf ( stdout, "Final corrected angle = %g\n", angle );
##### fprintf ( stdout, "Binding between f(%s: %g,%g) and v(%s: %g,%g) is at angle %g deg\n", mc[fixed_comp_index].name, fixed_vec[0], fixed_vec[1], mc[var_comp_index].name, var_vec[0], var_vec[1], 180*angle/math.pi );
# Rotate all of the components of the var_mol_index by the angle
ca = math.cos(angle)
sa = math.sin(angle)
if build_as_3D:
cross_prod = [ (fixed_vec[1] * var_vec[2]) - (fixed_vec[2] * var_vec[1]),
(fixed_vec[2] * var_vec[0]) - (fixed_vec[0] * var_vec[2]),
(fixed_vec[0] * var_vec[1]) - (fixed_vec[1] * var_vec[0]) ]
norm_cross_prod = [ cp / ( fixed_mag * var_mag ) for cp in cross_prod ]
xpx = norm_cross_prod[0]
xpy = norm_cross_prod[1]
xpz = norm_cross_prod[2]
axis_length = math.sqrt( (xpx*xpx) + (xpy*xpy) + (xpz*xpz) )
R = None
if axis_length < 1e-30:
# Can't compute a meaningful unit vector to use for rotation matrix or quaternion
if norm_dot_prod < 0:
R = [ [ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ] ]
else:
R = [ [ -1, 0, 0 ],
[ 0, -1, 0 ],
[ 0, 0, -1 ] ]
else:
ux = xpx / axis_length
uy = xpy / axis_length
uz = xpz / axis_length
if True: # Build the rotation matrix directly
# Build a 3D rotation matrix
omca = 1 - ca
R = [ [ca + (ux*ux*omca), (ux*uy*omca) - (uz*sa), (ux*uz*omca) + (uy*sa)],
[(uy*ux*omca) + (uz*sa), ca + (uy*uy*omca), (uy*uz*omca) - (ux*sa)],
[(uz*ux*omca) - (uy*sa), (uz*uy*omca) + (ux*sa), ca + (uz*uz*omca)] ]
else: # Build the rotation matrix from a quaternion
s2 = math.sin(angle/2)
c2 = math.cos(angle/2)
q = [ c2, s2*ux, s2*uy, s2*uz ]
# qconj = [ c2, -s2*ux, -s2*uy, -s2*uz ]
# Create convenience variables for components and squares
qr = q[0]
qi = q[1]
qj = q[2]
qk = q[3]
qi2 = qi * qi
qj2 = qj * qj
qk2 = qk * qk
# Create the rotation matrix itself from the quaternion
R = [ [ 1-(2*(qj2+qk2)), 2*((qi*qj)-(qk*qr)), 2*((qi*qk)+(qj*qr)) ],
[ 2*((qi*qj)+(qk*qr)), 1-(2*(qi2+qk2)), 2*((qj*qk)-(qi*qr)) ],
[ 2*((qi*qk)-(qj*qr)), 2*((qj*qk)+(qi*qr)), 1-(2*(qi2+qj2)) ] ]
##### fprintf ( stdout, "Rotating component positions for %s by %g\n", mc[var_mol_index].name, 180*angle/math.pi );
# for (int ci=0; ci<mc[var_mol_index].num_peers; ci++) {
for ci in range ( len(mc[var_mol_index]['peer_list']) ):
##### fprintf ( stdout, " Component %s before is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
x = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0]
y = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1]
z = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][2]
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0] = (R[0][0]*x) + (R[0][1]*y) + (R[0][2]*z)
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1] = (R[1][0]*x) + (R[1][1]*y) + (R[1][2]*z)
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][2] = (R[2][0]*x) + (R[2][1]*y) + (R[2][2]*z)
##### fprintf ( stdout, " Component %s after is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
for ci in range ( len(mc[var_mol_index]['key_list']) ):
##### fprintf ( stdout, " Component %s before is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
x = mc[mc[var_mol_index]['key_list'][ci]]['coords'][0]
y = mc[mc[var_mol_index]['key_list'][ci]]['coords'][1]
z = mc[mc[var_mol_index]['key_list'][ci]]['coords'][2]
mc[mc[var_mol_index]['key_list'][ci]]['coords'][0] = (R[0][0]*x) + (R[0][1]*y) + (R[0][2]*z)
mc[mc[var_mol_index]['key_list'][ci]]['coords'][1] = (R[1][0]*x) + (R[1][1]*y) + (R[1][2]*z)
mc[mc[var_mol_index]['key_list'][ci]]['coords'][2] = (R[2][0]*x) + (R[2][1]*y) + (R[2][2]*z)
##### fprintf ( stdout, " Component %s after is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
else:
R = [ [ca, -sa],
[sa, ca] ]
##### fprintf ( stdout, "Rotating component positions for %s by %g\n", mc[var_mol_index].name, 180*angle/math.pi );
# for (int ci=0; ci<mc[var_mol_index].num_peers; ci++) {
for ci in range ( len(mc[var_mol_index]['peer_list']) ):
##### fprintf ( stdout, " Component %s before is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
x = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0];
y = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1];
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0] = (R[0][0]*x) + (R[0][1]*y)
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1] = (R[1][0]*x) + (R[1][1]*y)
##### fprintf ( stdout, " Component %s after is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
for ci in range ( len(mc[var_mol_index]['key_list']) ):
##### fprintf ( stdout, " Component %s before is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
x = mc[mc[var_mol_index]['key_list'][ci]]['coords'][0];
y = mc[mc[var_mol_index]['key_list'][ci]]['coords'][1];
mc[mc[var_mol_index]['key_list'][ci]]['coords'][0] = (R[0][0]*x) + (R[0][1]*y)
mc[mc[var_mol_index]['key_list'][ci]]['coords'][1] = (R[1][0]*x) + (R[1][1]*y)
##### fprintf ( stdout, " Component %s after is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
# Now the molecules are aligned as they should be except for rotation along their bonding axis or bending axis
if build_as_3D and axial_rotation:
# dump_molcomp_list ( mc )
# Rotate the variable molecule along its bonding axis to align based on the rotation key angle
checked_print ( "Axial Rotation:" )
checked_print ( " Fixed mol " + str(fixed_mol_index) + " binding to Var mol " + str(var_mol_index) )
checked_print ( " Fixed component " + str(fixed_comp_index) + " binding to Var component " + str(var_comp_index) )
fixed_key_index = int(mc[fixed_comp_index]['key_list'][0])
var_key_index = int(mc[var_comp_index]['key_list'][0])
checked_print ( " Fixed component key: " + str(fixed_key_index) + ", Var component key: " + str(var_key_index) )
fixed_req_bond_angle = mc[fixed_comp_index]['angle']
var_req_bond_angle = mc[var_comp_index]['angle']
checked_print ( " Fixed angle = " + str(fixed_req_bond_angle) + ", Var angle = " + str(var_req_bond_angle) )
# fixed_vcomp will be the vector from the fixed molecule to the fixed component
fixed_vcomp = []
for i in range(3):
fixed_vcomp.append ( mc[fixed_comp_index]['coords'][i] - mc[fixed_mol_index]['coords'][i] )
# var_vcomp will be the vector from the var molecule to the var component
var_vcomp = []
for i in range(3):
var_vcomp.append ( mc[var_comp_index]['coords'][i] - mc[var_mol_index]['coords'][i] )
# fixed_vkey will be the vector from the fixed molecule to the fixed key
fixed_vkey = []
for i in range(3):
fixed_vkey.append ( mc[fixed_key_index]['coords'][i] - mc[fixed_mol_index]['coords'][i] )
# var_vkey will be the vector from the var molecule to the var key
var_vkey = []
for i in range(3):
var_vkey.append ( mc[var_key_index]['coords'][i] - mc[var_mol_index]['coords'][i] )
checked_print ( " Fixed vcomp = " + str(fixed_vcomp) )
checked_print ( " Var vcomp = " + str(var_vcomp) )
checked_print ( " Fixed vkey = " + str(fixed_vkey) )
checked_print ( " Var vkey = " + str(var_vkey) )
# Use the cross product to get the normal to the fixed molecule-component-key plane
fixed_normal = [ (fixed_vcomp[1] * fixed_vkey[2]) - (fixed_vcomp[2] * fixed_vkey[1]),
(fixed_vcomp[2] * fixed_vkey[0]) - (fixed_vcomp[0] * fixed_vkey[2]),
(fixed_vcomp[0] * fixed_vkey[1]) - (fixed_vcomp[1] * fixed_vkey[0]) ]
# Use the cross product to get the normal to the variable molecule-component-key plane
var_normal = [ (var_vcomp[1] * var_vkey[2]) - (var_vcomp[2] * var_vkey[1]),
(var_vcomp[2] * var_vkey[0]) - (var_vcomp[0] * var_vkey[2]),
(var_vcomp[0] * var_vkey[1]) - (var_vcomp[1] * var_vkey[0]) ]
# Get the magnitudes of the two vectors for normalization
fixed_norm_mag = math.sqrt ( (fixed_normal[0]*fixed_normal[0]) + (fixed_normal[1]*fixed_normal[1]) + (fixed_normal[2]*fixed_normal[2]) )
var_norm_mag = math.sqrt ( ( var_normal[0]* var_normal[0]) + ( var_normal[1]* var_normal[1]) + ( var_normal[2]* var_normal[2]) )
# Calculate unit vectors
fixed_unit = [ cp / fixed_norm_mag for cp in fixed_normal ]
var_unit = [ cp / var_norm_mag for cp in var_normal ]
checked_print ( " Fixed unit = " + str(fixed_unit) )
checked_print ( " Var unit = " + str(var_unit) )
norm_dot_prod = (fixed_unit[0] * var_unit[0]) + (fixed_unit[1] * var_unit[1]) + (fixed_unit[2] * var_unit[2])
# Ensure that the dot product is a legal argument for the "acos" function:
if norm_dot_prod > 1:
print ( "Numerical Warning: normalized dot product " + str(norm_dot_prod) + " was greater than 1" )
norm_dot_prod = 1
if norm_dot_prod < -1:
print ( "Numerical Warning: normalized dot product " + str(norm_dot_prod) + " was less than -1" )
norm_dot_prod = -1
checked_print ( " Normalized Dot Product between fixed and var is " + str(norm_dot_prod) )
# Compute the amount of rotation to bring the planes into alignment offset by the requested bond angles
cur_key_plane_angle = math.acos ( norm_dot_prod )
checked_print ( "Current key plane angle = " + str(180*cur_key_plane_angle/math.pi) )
cross_prod = [ (fixed_unit[1] * var_unit[2]) - (fixed_unit[2] * var_unit[1]),
(fixed_unit[2] * var_unit[0]) - (fixed_unit[0] * var_unit[2]),
(fixed_unit[0] * var_unit[1]) - (fixed_unit[1] * var_unit[0]) ]
dot_cross_rot = (cross_prod[0] * var_vcomp[0]) + (cross_prod[1] * var_vcomp[1]) + (cross_prod[2] * var_vcomp[2])
if dot_cross_rot > 0:
cur_key_plane_angle = (2*math.pi) - cur_key_plane_angle
checked_print ( "Current key plane angle = " + str(180*cur_key_plane_angle/math.pi) + ", dot_cross_rot = " + str(dot_cross_rot) )
composite_rot_angle = math.pi + cur_key_plane_angle # The "math.pi" adds 180 degrees to make the components "line up"
if not bending_rotation:
# Only add the requested axial rotation if there is no bending
composite_rot_angle += (var_req_bond_angle + fixed_req_bond_angle)
checked_print ( " Fixed angle is = " + str(180 * fixed_req_bond_angle / math.pi) + " degrees" )
checked_print ( " Var angle is = " + str(180 * var_req_bond_angle / math.pi) + " degrees" )
checked_print ( " Current angle between keys is = " + str(180 * cur_key_plane_angle / math.pi) + " degrees" )
checked_print ( " Composite rotation angle is = " + str(180 * composite_rot_angle / math.pi) + " degrees" )
# Build a 3D rotation matrix along the axis of the molecule to the component
var_vcomp_mag = math.sqrt ( (var_vcomp[0]*var_vcomp[0]) + (var_vcomp[1]*var_vcomp[1]) + (var_vcomp[2]*var_vcomp[2]) )
var_rot_unit = [ v / var_vcomp_mag for v in var_vcomp ]
ux = var_rot_unit[0]
uy = var_rot_unit[1]
uz = var_rot_unit[2]
R = None
if True: # Build the rotation matrix directly
cca = math.cos(composite_rot_angle)
sca = math.sin(composite_rot_angle)
omcca = 1 - cca
R = [ [cca + (ux*ux*omcca), (ux*uy*omcca) - (uz*sca), (ux*uz*omcca) + (uy*sca)],
[(uy*ux*omcca) + (uz*sca), cca + (uy*uy*omcca), (uy*uz*omcca) - (ux*sca)],
[(uz*ux*omcca) - (uy*sca), (uz*uy*omcca) + (ux*sca), cca + (uz*uz*omcca)] ]
else: # Build the rotation matrix from a quaternion
s2 = math.sin(composite_rot_angle/2)
c2 = math.cos(composite_rot_angle/2)
q = [ c2, s2*ux, s2*uy, s2*uz ]
# qconj = [ c2, -s2*ux, -s2*uy, -s2*uz ]
# Create convenience variables for components and squares
qr = q[0]
qi = q[1]
qj = q[2]
qk = q[3]
qi2 = qi * qi
qj2 = qj * qj
qk2 = qk * qk
# Create the rotation matrix itself from the quaternion
R = [ [ 1-(2*(qj2+qk2)), 2*((qi*qj)-(qk*qr)), 2*((qi*qk)+(qj*qr)) ],
[ 2*((qi*qj)+(qk*qr)), 1-(2*(qi2+qk2)), 2*((qj*qk)-(qi*qr)) ],
[ 2*((qi*qk)-(qj*qr)), 2*((qj*qk)+(qi*qr)), 1-(2*(qi2+qj2)) ] ]
mm = bpy.context.scene.mcell.molmaker
if (not mm.skip_rotation) or (fixed_comp_index != mm.skip_fixed_comp_index) or (var_comp_index != mm.skip_var_comp_index):
# Apply the rotation matrix after subtracting the molecule center location from all components and keys
for ci in range ( len(mc[var_mol_index]['peer_list']) ):
##### fprintf ( stdout, " Component %s before is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
x = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0] - mc[var_mol_index]['coords'][0]
y = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1] - mc[var_mol_index]['coords'][1]
z = mc[mc[var_mol_index]['peer_list'][ci]]['coords'][2] - mc[var_mol_index]['coords'][2]
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0] = (R[0][0]*x) + (R[0][1]*y) + (R[0][2]*z) + mc[var_mol_index]['coords'][0]
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1] = (R[1][0]*x) + (R[1][1]*y) + (R[1][2]*z) + mc[var_mol_index]['coords'][1]
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][2] = (R[2][0]*x) + (R[2][1]*y) + (R[2][2]*z) + mc[var_mol_index]['coords'][2]
##### fprintf ( stdout, " Component %s after is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
for ci in range ( len(mc[var_mol_index]['key_list']) ):
##### fprintf ( stdout, " Component %s before is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
x = mc[mc[var_mol_index]['key_list'][ci]]['coords'][0] - mc[var_mol_index]['coords'][0]
y = mc[mc[var_mol_index]['key_list'][ci]]['coords'][1] - mc[var_mol_index]['coords'][1]
z = mc[mc[var_mol_index]['key_list'][ci]]['coords'][2] - mc[var_mol_index]['coords'][2]
mc[mc[var_mol_index]['key_list'][ci]]['coords'][0] = (R[0][0]*x) + (R[0][1]*y) + (R[0][2]*z) + mc[var_mol_index]['coords'][0]
mc[mc[var_mol_index]['key_list'][ci]]['coords'][1] = (R[1][0]*x) + (R[1][1]*y) + (R[1][2]*z) + mc[var_mol_index]['coords'][1]
mc[mc[var_mol_index]['key_list'][ci]]['coords'][2] = (R[2][0]*x) + (R[2][1]*y) + (R[2][2]*z) + mc[var_mol_index]['coords'][2]
##### fprintf ( stdout, " Component %s after is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]]['coords'][0], mc[mc[var_mol_index].peers[ci]]['coords'][1] );
if build_as_3D and bending_rotation:
# dump_molcomp_list ( mc )
# Rotate the variable molecule about an axis through its binding component and normal to the fixed molecule/component/key plane
# Rotate by an amount that puts the pre-axially rotated variable key in line with the binding component to fixed key vector
# Unfortunately, the location of the pre-axially rotated variable key is not available since the composite rotation angle included the arbitrary user-selected rotation
pass
##### dump_molcomp_array(mc,num_parts);
# Shift the var molecule location and the locations of all of its components by the difference of the binding components
dx = mc[fixed_comp_index]['coords'][0] - mc[var_comp_index]['coords'][0]
dy = mc[fixed_comp_index]['coords'][1] - mc[var_comp_index]['coords'][1]
dz = 0
if build_as_3D:
dz = mc[fixed_comp_index]['coords'][2] - mc[var_comp_index]['coords'][2]
##### fprintf ( stdout, "Shifting molecule and component positions for %s\n", mc[var_mol_index].name );
mc[var_mol_index]['coords'][0] += dx
mc[var_mol_index]['coords'][1] += dy
mc[var_mol_index]['coords'][2] += dz
#for (int ci=0; ci<mc[var_mol_index].num_peers; ci++) {
for ci in range ( len(mc[var_mol_index]['peer_list']) ):
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][0] += dx
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][1] += dy
mc[mc[var_mol_index]['peer_list'][ci]]['coords'][2] += dz
##### fprintf ( stdout, " Component %s is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]].x, mc[mc[var_mol_index].peers[ci]]['coords'][1] );
for ci in range ( len(mc[var_mol_index]['key_list']) ):
mc[mc[var_mol_index]['key_list'][ci]]['coords'][0] += dx
mc[mc[var_mol_index]['key_list'][ci]]['coords'][1] += dy
mc[mc[var_mol_index]['key_list'][ci]]['coords'][2] += dz
##### fprintf ( stdout, " Component %s is at (%g,%g)\n", mc[mc[var_mol_index].peers[ci]].name, mc[mc[var_mol_index].peers[ci]].x, mc[mc[var_mol_index].peers[ci]]['coords'][1] );
##### dump_molcomp_array(mc,num_parts);
##### fprintf ( stdout, "########## Done Binding %s to %s\n", mc[fixed_comp_index].name, mc[var_comp_index].name );
def bind_all_molecules ( molcomp_array, build_as_3D, axial_rotation=True, bending_rotation=False ):
context = bpy.context
mcell = context.scene.mcell
molmaker = mcell.molmaker
old_debug = molmaker.print_debug
## molmaker.print_debug = True
checked_print ( "======================================================================================" )
checked_print ( "===================================== bind_all_molecules =============================" )
checked_print ( "======================================================================================" )
dump_molcomp_list ( molcomp_array )
checked_print ( "======================================================================================" )
molmaker.print_debug = old_debug
# Compute positions for all molecules/components in a molcomp_array
num_parts = len(molcomp_array)
mi=0;
pi=0;
# Set all molecules and components to not final
for mi in range(num_parts):
molcomp_array[mi]['is_final'] = False
# Find first molecule
found_mol = False
for mi in range(num_parts):
if molcomp_array[mi]['ftype'] == 'm':
found_mol = True
break;
if found_mol:
# Set this first molecule and all of its components to final
molcomp_array[mi]['is_final'] = True
num_peers = len(molcomp_array[mi]['peer_list'])
for ci in range(num_peers):
molcomp_array[molcomp_array[mi]['peer_list'][ci]]['is_final'] = True
done = False
while not done:
# Look for a bond between a non-final and a final component
done = True
for mi in range(num_parts):
if molcomp_array[mi]['ftype'] == 'c':
# Only search components for bonds
if len(molcomp_array[mi]['peer_list']) > 1:
# This component has bonds, so search them
for ci in range(1,len(molcomp_array[mi]['peer_list'])): # for (int ci=1; ci<molcomp_array[mi].num_peers; ci++) {
pi = molcomp_array[mi]['peer_list'][ci] # Peer index
if molcomp_array[mi]['is_final'] != molcomp_array[pi]['is_final']:
done = False
# One of these is final and the other is not so join them and make them all final
fci=0 # Fixed Component Index
vci=0 # Variable Component Index
vmi=0 # Variable Molecule Index
# Figure out which is fixed and which is not
if molcomp_array[mi]['is_final']:
fci = mi;
vci = pi;
else:
fci = pi;
vci = mi;
# Set the variable molecule index for the bond based on the component
vmi = molcomp_array[vci]['peer_list'][0]
# Perform the bond (changes the locations)
bind_molecules_at_components ( molcomp_array, fci, vci, build_as_3D, axial_rotation, bending_rotation )
# The bonding process will have specified the placement of the variable molecule
# Set the variable molecule and all of its components to final
molcomp_array[vmi]['is_final'] = True
#for (int vmici=0; vmici<molcomp_array[vmi].num_peers; vmici++) {
for vmici in range(len(molcomp_array[vmi]['peer_list'])):
molcomp_array[molcomp_array[vmi]['peer_list'][vmici]]['is_final'] = True
old_debug = molmaker.print_debug
## molmaker.print_debug = True
checked_print ( "======================================================================================" )
dump_molcomp_list ( molcomp_array )
checked_print ( "======================================================================================" )
molmaker.print_debug = old_debug
def build_all_mols ( context, molcomp_list, build_as_3D=True, axial_rotation=True, bending_rotation=False ):
if build_as_3D:
checked_print ( "\n\nBuilding as 3D" )
else:
checked_print ( "\n\nBuilding as 2D" )
molmaker = context.scene.mcell.molmaker