1 # SPDX-License-Identifier: GPL-2.0-or-later
5 from math
import pi
, sqrt
6 from mathutils
import Vector
, Matrix
8 # -----------------------------------------------------------------------------
9 # Atom and element data
12 # This is a list that contains some data of all possible elements. The structure
15 # 1, "Hydrogen", "H", [0.0,0.0,1.0], 0.32, 0.32, 0.32 , -1 , 1.54 means
17 # No., name, short name, color, radius (used), radius (covalent), radius (atomic),
19 # charge state 1, radius (ionic) 1, charge state 2, radius (ionic) 2, ... all
20 # charge states for any atom are listed, if existing.
21 # The list is fixed and cannot be changed ... (see below)
24 ( 1, "Hydrogen", "H", ( 1.0, 1.0, 1.0, 1.0), 0.32, 0.32, 0.79 , -1 , 1.54 ),
25 ( 2, "Helium", "He", ( 0.85, 1.0, 1.0, 1.0), 0.93, 0.93, 0.49 ),
26 ( 3, "Lithium", "Li", ( 0.8, 0.50, 1.0, 1.0), 1.23, 1.23, 2.05 , 1 , 0.68 ),
27 ( 4, "Beryllium", "Be", ( 0.76, 1.0, 0.0, 1.0), 0.90, 0.90, 1.40 , 1 , 0.44 , 2 , 0.35 ),
28 ( 5, "Boron", "B", ( 1.0, 0.70, 0.70, 1.0), 0.82, 0.82, 1.17 , 1 , 0.35 , 3 , 0.23 ),
29 ( 6, "Carbon", "C", ( 0.56, 0.56, 0.56, 1.0), 0.77, 0.77, 0.91 , -4 , 2.60 , 4 , 0.16 ),
30 ( 7, "Nitrogen", "N", ( 0.18, 0.31, 0.97, 1.0), 0.75, 0.75, 0.75 , -3 , 1.71 , 1 , 0.25 , 3 , 0.16 , 5 , 0.13 ),
31 ( 8, "Oxygen", "O", ( 1.0, 0.05, 0.05, 1.0), 0.73, 0.73, 0.65 , -2 , 1.32 , -1 , 1.76 , 1 , 0.22 , 6 , 0.09 ),
32 ( 9, "Fluorine", "F", ( 0.56, 0.87, 0.31, 1.0), 0.72, 0.72, 0.57 , -1 , 1.33 , 7 , 0.08 ),
33 (10, "Neon", "Ne", ( 0.70, 0.89, 0.96, 1.0), 0.71, 0.71, 0.51 , 1 , 1.12 ),
34 (11, "Sodium", "Na", ( 0.67, 0.36, 0.94, 1.0), 1.54, 1.54, 2.23 , 1 , 0.97 ),
35 (12, "Magnesium", "Mg", ( 0.54, 1.0, 0.0, 1.0), 1.36, 1.36, 1.72 , 1 , 0.82 , 2 , 0.66 ),
36 (13, "Aluminium", "Al", ( 0.74, 0.65, 0.65, 1.0), 1.18, 1.18, 1.82 , 3 , 0.51 ),
37 (14, "Silicon", "Si", ( 0.94, 0.78, 0.62, 1.0), 1.11, 1.11, 1.46 , -4 , 2.71 , -1 , 3.84 , 1 , 0.65 , 4 , 0.42 ),
38 (15, "Phosphorus", "P", ( 1.0, 0.50, 0.0, 1.0), 1.06, 1.06, 1.23 , -3 , 2.12 , 3 , 0.44 , 5 , 0.35 ),
39 (16, "Sulfur", "S", ( 1.0, 1.0, 0.18, 1.0), 1.02, 1.02, 1.09 , -2 , 1.84 , 2 , 2.19 , 4 , 0.37 , 6 , 0.30 ),
40 (17, "Chlorine", "Cl", ( 0.12, 0.94, 0.12, 1.0), 0.99, 0.99, 0.97 , -1 , 1.81 , 5 , 0.34 , 7 , 0.27 ),
41 (18, "Argon", "Ar", ( 0.50, 0.81, 0.89, 1.0), 0.98, 0.98, 0.88 , 1 , 1.54 ),
42 (19, "Potassium", "K", ( 0.56, 0.25, 0.83, 1.0), 2.03, 2.03, 2.77 , 1 , 0.81 ),
43 (20, "Calcium", "Ca", ( 0.23, 1.0, 0.0, 1.0), 1.74, 1.74, 2.23 , 1 , 1.18 , 2 , 0.99 ),
44 (21, "Scandium", "Sc", ( 0.90, 0.90, 0.90, 1.0), 1.44, 1.44, 2.09 , 3 , 0.73 ),
45 (22, "Titanium", "Ti", ( 0.74, 0.76, 0.78, 1.0), 1.32, 1.32, 2.00 , 1 , 0.96 , 2 , 0.94 , 3 , 0.76 , 4 , 0.68 ),
46 (23, "Vanadium", "V", ( 0.65, 0.65, 0.67, 1.0), 1.22, 1.22, 1.92 , 2 , 0.88 , 3 , 0.74 , 4 , 0.63 , 5 , 0.59 ),
47 (24, "Chromium", "Cr", ( 0.54, 0.6, 0.78, 1.0), 1.18, 1.18, 1.85 , 1 , 0.81 , 2 , 0.89 , 3 , 0.63 , 6 , 0.52 ),
48 (25, "Manganese", "Mn", ( 0.61, 0.47, 0.78, 1.0), 1.17, 1.17, 1.79 , 2 , 0.80 , 3 , 0.66 , 4 , 0.60 , 7 , 0.46 ),
49 (26, "Iron", "Fe", ( 0.87, 0.4, 0.2, 1.0), 1.17, 1.17, 1.72 , 2 , 0.74 , 3 , 0.64 ),
50 (27, "Cobalt", "Co", ( 0.94, 0.56, 0.62, 1.0), 1.16, 1.16, 1.67 , 2 , 0.72 , 3 , 0.63 ),
51 (28, "Nickel", "Ni", ( 0.31, 0.81, 0.31, 1.0), 1.15, 1.15, 1.62 , 2 , 0.69 ),
52 (29, "Copper", "Cu", ( 0.78, 0.50, 0.2, 1.0), 1.17, 1.17, 1.57 , 1 , 0.96 , 2 , 0.72 ),
53 (30, "Zinc", "Zn", ( 0.49, 0.50, 0.69, 1.0), 1.25, 1.25, 1.53 , 1 , 0.88 , 2 , 0.74 ),
54 (31, "Gallium", "Ga", ( 0.76, 0.56, 0.56, 1.0), 1.26, 1.26, 1.81 , 1 , 0.81 , 3 , 0.62 ),
55 (32, "Germanium", "Ge", ( 0.4, 0.56, 0.56, 1.0), 1.22, 1.22, 1.52 , -4 , 2.72 , 2 , 0.73 , 4 , 0.53 ),
56 (33, "Arsenic", "As", ( 0.74, 0.50, 0.89, 1.0), 1.20, 1.20, 1.33 , -3 , 2.22 , 3 , 0.58 , 5 , 0.46 ),
57 (34, "Selenium", "Se", ( 1.0, 0.63, 0.0, 1.0), 1.16, 1.16, 1.22 , -2 , 1.91 , -1 , 2.32 , 1 , 0.66 , 4 , 0.50 , 6 , 0.42 ),
58 (35, "Bromine", "Br", ( 0.65, 0.16, 0.16, 1.0), 1.14, 1.14, 1.12 , -1 , 1.96 , 5 , 0.47 , 7 , 0.39 ),
59 (36, "Krypton", "Kr", ( 0.36, 0.72, 0.81, 1.0), 1.31, 1.31, 1.24 ),
60 (37, "Rubidium", "Rb", ( 0.43, 0.18, 0.69, 1.0), 2.16, 2.16, 2.98 , 1 , 1.47 ),
61 (38, "Strontium", "Sr", ( 0.0, 1.0, 0.0, 1.0), 1.91, 1.91, 2.45 , 2 , 1.12 ),
62 (39, "Yttrium", "Y", ( 0.58, 1.0, 1.0, 1.0), 1.62, 1.62, 2.27 , 3 , 0.89 ),
63 (40, "Zirconium", "Zr", ( 0.58, 0.87, 0.87, 1.0), 1.45, 1.45, 2.16 , 1 , 1.09 , 4 , 0.79 ),
64 (41, "Niobium", "Nb", ( 0.45, 0.76, 0.78, 1.0), 1.34, 1.34, 2.08 , 1 , 1.00 , 4 , 0.74 , 5 , 0.69 ),
65 (42, "Molybdenum", "Mo", ( 0.32, 0.70, 0.70, 1.0), 1.30, 1.30, 2.01 , 1 , 0.93 , 4 , 0.70 , 6 , 0.62 ),
66 (43, "Technetium", "Tc", ( 0.23, 0.61, 0.61, 1.0), 1.27, 1.27, 1.95 , 7 , 0.97 ),
67 (44, "Ruthenium", "Ru", ( 0.14, 0.56, 0.56, 1.0), 1.25, 1.25, 1.89 , 4 , 0.67 ),
68 (45, "Rhodium", "Rh", ( 0.03, 0.49, 0.54, 1.0), 1.25, 1.25, 1.83 , 3 , 0.68 ),
69 (46, "Palladium", "Pd", ( 0.0, 0.41, 0.52, 1.0), 1.28, 1.28, 1.79 , 2 , 0.80 , 4 , 0.65 ),
70 (47, "Silver", "Ag", ( 0.75, 0.75, 0.75, 1.0), 1.34, 1.34, 1.75 , 1 , 1.26 , 2 , 0.89 ),
71 (48, "Cadmium", "Cd", ( 1.0, 0.85, 0.56, 1.0), 1.48, 1.48, 1.71 , 1 , 1.14 , 2 , 0.97 ),
72 (49, "Indium", "In", ( 0.65, 0.45, 0.45, 1.0), 1.44, 1.44, 2.00 , 3 , 0.81 ),
73 (50, "Tin", "Sn", ( 0.4, 0.50, 0.50, 1.0), 1.41, 1.41, 1.72 , -4 , 2.94 , -1 , 3.70 , 2 , 0.93 , 4 , 0.71 ),
74 (51, "Antimony", "Sb", ( 0.61, 0.38, 0.70, 1.0), 1.40, 1.40, 1.53 , -3 , 2.45 , 3 , 0.76 , 5 , 0.62 ),
75 (52, "Tellurium", "Te", ( 0.83, 0.47, 0.0, 1.0), 1.36, 1.36, 1.42 , -2 , 2.11 , -1 , 2.50 , 1 , 0.82 , 4 , 0.70 , 6 , 0.56 ),
76 (53, "Iodine", "I", ( 0.58, 0.0, 0.58, 1.0), 1.33, 1.33, 1.32 , -1 , 2.20 , 5 , 0.62 , 7 , 0.50 ),
77 (54, "Xenon", "Xe", ( 0.25, 0.61, 0.69, 1.0), 1.31, 1.31, 1.24 ),
78 (55, "Caesium", "Cs", ( 0.34, 0.09, 0.56, 1.0), 2.35, 2.35, 3.35 , 1 , 1.67 ),
79 (56, "Barium", "Ba", ( 0.0, 0.78, 0.0, 1.0), 1.98, 1.98, 2.78 , 1 , 1.53 , 2 , 1.34 ),
80 (57, "Lanthanum", "La", ( 0.43, 0.83, 1.0, 1.0), 1.69, 1.69, 2.74 , 1 , 1.39 , 3 , 1.06 ),
81 (58, "Cerium", "Ce", ( 1.0, 1.0, 0.78, 1.0), 1.65, 1.65, 2.70 , 1 , 1.27 , 3 , 1.03 , 4 , 0.92 ),
82 (59, "Praseodymium", "Pr", ( 0.85, 1.0, 0.78, 1.0), 1.65, 1.65, 2.67 , 3 , 1.01 , 4 , 0.90 ),
83 (60, "Neodymium", "Nd", ( 0.78, 1.0, 0.78, 1.0), 1.64, 1.64, 2.64 , 3 , 0.99 ),
84 (61, "Promethium", "Pm", ( 0.63, 1.0, 0.78, 1.0), 1.63, 1.63, 2.62 , 3 , 0.97 ),
85 (62, "Samarium", "Sm", ( 0.56, 1.0, 0.78, 1.0), 1.62, 1.62, 2.59 , 3 , 0.96 ),
86 (63, "Europium", "Eu", ( 0.38, 1.0, 0.78, 1.0), 1.85, 1.85, 2.56 , 2 , 1.09 , 3 , 0.95 ),
87 (64, "Gadolinium", "Gd", ( 0.27, 1.0, 0.78, 1.0), 1.61, 1.61, 2.54 , 3 , 0.93 ),
88 (65, "Terbium", "Tb", ( 0.18, 1.0, 0.78, 1.0), 1.59, 1.59, 2.51 , 3 , 0.92 , 4 , 0.84 ),
89 (66, "Dysprosium", "Dy", ( 0.12, 1.0, 0.78, 1.0), 1.59, 1.59, 2.49 , 3 , 0.90 ),
90 (67, "Holmium", "Ho", ( 0.0, 1.0, 0.61, 1.0), 1.58, 1.58, 2.47 , 3 , 0.89 ),
91 (68, "Erbium", "Er", ( 0.0, 0.90, 0.45, 1.0), 1.57, 1.57, 2.45 , 3 , 0.88 ),
92 (69, "Thulium", "Tm", ( 0.0, 0.83, 0.32, 1.0), 1.56, 1.56, 2.42 , 3 , 0.87 ),
93 (70, "Ytterbium", "Yb", ( 0.0, 0.74, 0.21, 1.0), 1.74, 1.74, 2.40 , 2 , 0.93 , 3 , 0.85 ),
94 (71, "Lutetium", "Lu", ( 0.0, 0.67, 0.14, 1.0), 1.56, 1.56, 2.25 , 3 , 0.85 ),
95 (72, "Hafnium", "Hf", ( 0.30, 0.76, 1.0, 1.0), 1.44, 1.44, 2.16 , 4 , 0.78 ),
96 (73, "Tantalum", "Ta", ( 0.30, 0.65, 1.0, 1.0), 1.34, 1.34, 2.09 , 5 , 0.68 ),
97 (74, "Tungsten", "W", ( 0.12, 0.58, 0.83, 1.0), 1.30, 1.30, 2.02 , 4 , 0.70 , 6 , 0.62 ),
98 (75, "Rhenium", "Re", ( 0.14, 0.49, 0.67, 1.0), 1.28, 1.28, 1.97 , 4 , 0.72 , 7 , 0.56 ),
99 (76, "Osmium", "Os", ( 0.14, 0.4, 0.58, 1.0), 1.26, 1.26, 1.92 , 4 , 0.88 , 6 , 0.69 ),
100 (77, "Iridium", "Ir", ( 0.09, 0.32, 0.52, 1.0), 1.27, 1.27, 1.87 , 4 , 0.68 ),
101 (78, "Platinum", "Pt", ( 0.81, 0.81, 0.87, 1.0), 1.30, 1.30, 1.83 , 2 , 0.80 , 4 , 0.65 ),
102 (79, "Gold", "Au", ( 1.0, 0.81, 0.13, 1.0), 1.34, 1.34, 1.79 , 1 , 1.37 , 3 , 0.85 ),
103 (80, "Mercury", "Hg", ( 0.72, 0.72, 0.81, 1.0), 1.49, 1.49, 1.76 , 1 , 1.27 , 2 , 1.10 ),
104 (81, "Thallium", "Tl", ( 0.65, 0.32, 0.30, 1.0), 1.48, 1.48, 2.08 , 1 , 1.47 , 3 , 0.95 ),
105 (82, "Lead", "Pb", ( 0.34, 0.34, 0.38, 1.0), 1.47, 1.47, 1.81 , 2 , 1.20 , 4 , 0.84 ),
106 (83, "Bismuth", "Bi", ( 0.61, 0.30, 0.70, 1.0), 1.46, 1.46, 1.63 , 1 , 0.98 , 3 , 0.96 , 5 , 0.74 ),
107 (84, "Polonium", "Po", ( 0.67, 0.36, 0.0, 1.0), 1.46, 1.46, 1.53 , 6 , 0.67 ),
108 (85, "Astatine", "At", ( 0.45, 0.30, 0.27, 1.0), 1.45, 1.45, 1.43 , -3 , 2.22 , 3 , 0.85 , 5 , 0.46 ),
109 (86, "Radon", "Rn", ( 0.25, 0.50, 0.58, 1.0), 1.00, 1.00, 1.34 ),
110 (87, "Francium", "Fr", ( 0.25, 0.0, 0.4, 1.0), 1.00, 1.00, 1.00 , 1 , 1.80 ),
111 (88, "Radium", "Ra", ( 0.0, 0.49, 0.0, 1.0), 1.00, 1.00, 1.00 , 2 , 1.43 ),
112 (89, "Actinium", "Ac", ( 0.43, 0.67, 0.98, 1.0), 1.00, 1.00, 1.00 , 3 , 1.18 ),
113 (90, "Thorium", "Th", ( 0.0, 0.72, 1.0, 1.0), 1.65, 1.65, 1.00 , 4 , 1.02 ),
114 (91, "Protactinium", "Pa", ( 0.0, 0.63, 1.0, 1.0), 1.00, 1.00, 1.00 , 3 , 1.13 , 4 , 0.98 , 5 , 0.89 ),
115 (92, "Uranium", "U", ( 0.0, 0.56, 1.0, 1.0), 1.42, 1.42, 1.00 , 4 , 0.97 , 6 , 0.80 ),
116 (93, "Neptunium", "Np", ( 0.0, 0.50, 1.0, 1.0), 1.00, 1.00, 1.00 , 3 , 1.10 , 4 , 0.95 , 7 , 0.71 ),
117 (94, "Plutonium", "Pu", ( 0.0, 0.41, 1.0, 1.0), 1.00, 1.00, 1.00 , 3 , 1.08 , 4 , 0.93 ),
118 (95, "Americium", "Am", ( 0.32, 0.36, 0.94, 1.0), 1.00, 1.00, 1.00 , 3 , 1.07 , 4 , 0.92 ),
119 (96, "Curium", "Cm", ( 0.47, 0.36, 0.89, 1.0), 1.00, 1.00, 1.00 ),
120 (97, "Berkelium", "Bk", ( 0.54, 0.30, 0.89, 1.0), 1.00, 1.00, 1.00 ),
121 (98, "Californium", "Cf", ( 0.63, 0.21, 0.83, 1.0), 1.00, 1.00, 1.00 ),
122 (99, "Einsteinium", "Es", ( 0.70, 0.12, 0.83, 1.0), 1.00, 1.00, 1.00 ),
123 (100, "Fermium", "Fm", ( 0.70, 0.12, 0.72, 1.0), 1.00, 1.00, 1.00 ),
124 (101, "Mendelevium", "Md", ( 0.70, 0.05, 0.65, 1.0), 1.00, 1.00, 1.00 ),
125 (102, "Nobelium", "No", ( 0.74, 0.05, 0.52, 1.0), 1.00, 1.00, 1.00 ),
126 (103, "Lawrencium", "Lr", ( 0.78, 0.0, 0.4, 1.0), 1.00, 1.00, 1.00 ),
127 (104, "Vacancy", "Vac", ( 0.5, 0.5, 0.5, 1.0), 1.00, 1.00, 1.00),
128 (105, "Default", "Default", ( 1.0, 1.0, 1.0, 1.0), 1.00, 1.00, 1.00),
129 (106, "Stick", "Stick", ( 0.5, 0.5, 0.5, 1.0), 1.00, 1.00, 1.00),
132 # This list here contains all data of the elements and will be used during
133 # runtime. It is a list of classes.
134 # During executing Atomic Blender, the list will be initialized with the fixed
135 # data from above via the class structure below (ElementProp). We
136 # have then one fixed list (above), which will never be changed, and a list of
137 # classes with same data. The latter can be modified via loading a separate
138 # custom data file for instance.
141 # This is the list, which contains all atoms of all frames! Each item is a
142 # list which contains the atoms of a single frame. It is a list of
146 # A list of ALL balls which are put into the scene
150 # This is the class, which stores the properties for one element.
151 class ElementProp(object):
152 __slots__
= ('number', 'name', 'short_name', 'color', 'radii', 'radii_ionic')
153 def __init__(self
, number
, name
, short_name
, color
, radii
, radii_ionic
):
156 self
.short_name
= short_name
159 self
.radii_ionic
= radii_ionic
161 # This is the class, which stores the properties of one atom.
162 class AtomProp(object):
163 __slots__
= ('element', 'name', 'location', 'radius', 'color', 'material')
164 def __init__(self
, element
, name
, location
, radius
, color
, material
):
165 self
.element
= element
167 self
.location
= location
170 self
.material
= material
173 # -----------------------------------------------------------------------------
174 # Some basic routines
180 for item
in ELEMENTS_DEFAULT
:
182 # All three radii into a list
183 radii
= [item
[4],item
[5],item
[6]]
184 # The handling of the ionic radii will be done later. So far, it is an
188 li
= ElementProp(item
[0],item
[1],item
[2],item
[3],
193 # filepath_pdb: path to pdb file
194 # radiustype : '0' default
197 def read_xyz_file(filepath_xyz
,radiustype
):
200 total_number_atoms
= 0
203 filepath_xyz_p
= open(filepath_xyz
, "r")
205 #Go through the whole file.
207 for line
in filepath_xyz_p
:
209 # ... the loop is broken here (EOF) ...
213 split_list
= line
.rsplit()
215 if len(split_list
) == 1:
216 number_atoms
= int(split_list
[0])
221 line
= filepath_xyz_p
.readline()
225 for i
in range(number_atoms
):
228 # This is a guarantee that only the total number of atoms of the
229 # first frame is used. Condition is, so far, that the number of
230 # atoms in a xyz file is constant. However, sometimes the number
231 # may increase (or decrease). If it decreases, the addon crashes.
232 # If it increases, only the tot number of atoms of the first frame
234 # By time, I will allow varying atom numbers ... but this takes
236 if number_frames
!= 0:
237 if i
>= total_number_atoms
:
241 line
= filepath_xyz_p
.readline()
243 split_list
= line
.rsplit()
244 short_name
= str(split_list
[0])
246 # Go through all elements and find the element of the current atom.
248 for element
in ELEMENTS
:
249 if str.upper(short_name
) == str.upper(element
.short_name
):
250 # Give the atom its proper name, color and radius:
252 # int(radiustype) => type of radius:
253 # pre-defined (0), atomic (1) or van der Waals (2)
254 radius
= float(element
.radii
[int(radiustype
)])
255 color
= element
.color
259 # Is it a vacancy or an 'unknown atom' ?
260 if FLAG_FOUND
== False:
261 # Give this atom also a name. If it is an 'X' then it is a
262 # vacancy. Otherwise ...
263 if "X" in short_name
:
266 radius
= float(ELEMENTS
[-3].radii
[int(radiustype
)])
267 color
= ELEMENTS
[-3].color
268 # ... take what is written in the xyz file. These are somewhat
269 # unknown atoms. This should never happen, the element list is
270 # almost complete. However, we do this due to security reasons.
272 name
= str.upper(short_name
)
273 radius
= float(ELEMENTS
[-2].radii
[int(radiustype
)])
274 color
= ELEMENTS
[-2].color
276 x
= float(split_list
[1])
277 y
= float(split_list
[2])
278 z
= float(split_list
[3])
280 location
= Vector((x
,y
,z
))
282 all_atoms
.append([short_name
, name
, location
, radius
, color
])
284 # We note here all elements. This needs to be done only once.
285 if number_frames
== 0:
287 # This is a guarantee that only the total number of atoms of the
288 # first frame is used. Condition is, so far, that the number of
289 # atoms in a xyz file is constant. However, sometimes the number
290 # may increase (or decrease). If it decreases, the addon crashes.
291 # If it increases, only the tot number of atoms of the first frame
293 # By time, I will allow varying atom numbers ... but this takes
295 total_number_atoms
= number_atoms
299 for atom
in all_atoms
:
301 for element
in elements
:
302 # If the atom name is already in the list,
304 if element
== atom
[1]:
307 # No name in the current list has been found? => New entry.
308 if FLAG_FOUND
== False:
309 # Stored are: Atom label (e.g. 'Na'), the corresponding
310 # atom name (e.g. 'Sodium') and its color.
311 elements
.append(atom
[1])
313 # Sort the atoms: create lists of atoms of one type
315 for element
in elements
:
317 for atom
in all_atoms
:
318 if atom
[1] == element
:
319 atoms_one_type
.append(AtomProp(atom
[0],
324 structure
.append(atoms_one_type
)
326 ALL_FRAMES
.append(structure
)
330 filepath_xyz_p
.close()
332 return total_number_atoms
336 def rotate_object(rot_mat
, obj
):
338 bpy
.ops
.object.select_all(action
='DESELECT')
341 # Decompose world_matrix's components, and from them assemble 4x4 matrices.
342 orig_loc
, orig_rot
, orig_scale
= obj
.matrix_world
.decompose()
344 orig_loc_mat
= Matrix
.Translation(orig_loc
)
345 orig_rot_mat
= orig_rot
.to_matrix().to_4x4()
346 orig_scale_mat
= (Matrix
.Scale(orig_scale
[0],4,(1,0,0)) @
347 Matrix
.Scale(orig_scale
[1],4,(0,1,0)) @
348 Matrix
.Scale(orig_scale
[2],4,(0,0,1)))
350 # Assemble the new matrix.
351 obj
.matrix_world
= orig_loc_mat
@ rot_mat
@ orig_rot_mat
@ orig_scale_mat
354 # Function, which puts a camera and light source into the 3D scene
355 def camera_light_source(use_camera
,
362 # If chosen a camera is put into the scene.
363 if use_camera
== True:
365 # Assume that the object is put into the global origin. Then, the
366 # camera is moved in x and z direction, not in y. The object has its
367 # size at distance sqrt(object_size) from the origin. So, move the
368 # camera by this distance times a factor of camera_factor in x and z.
369 # Then add x, y and z of the origin of the object.
370 object_camera_vec
= Vector((sqrt(object_size
) * camera_factor
,
372 sqrt(object_size
) * camera_factor
))
373 camera_xyz_vec
= object_center_vec
+ object_camera_vec
376 camera_data
= bpy
.data
.cameras
.new("A_camera")
377 camera_data
.lens
= 45
378 camera_data
.clip_end
= 500.0
379 camera
= bpy
.data
.objects
.new("A_camera", camera_data
)
380 camera
.location
= camera_xyz_vec
381 bpy
.context
.collection
.objects
.link(camera
)
383 # Here the camera is rotated such it looks towards the center of
384 # the object. The [0.0, 0.0, 1.0] vector along the z axis
385 z_axis_vec
= Vector((0.0, 0.0, 1.0))
386 # The angle between the last two vectors
387 angle
= object_camera_vec
.angle(z_axis_vec
, 0)
388 # The cross-product of z_axis_vec and object_camera_vec
389 axis_vec
= z_axis_vec
.cross(object_camera_vec
)
390 # Rotate 'axis_vec' by 'angle' and convert this to euler parameters.
391 # 4 is the size of the matrix.
392 camera
.rotation_euler
= Matrix
.Rotation(angle
, 4, axis_vec
).to_euler()
394 # Rotate the camera around its axis by 90° such that we have a nice
395 # camera position and view onto the object.
396 bpy
.ops
.object.select_all(action
='DESELECT')
397 camera
.select_set(True)
399 # Rotate the camera around its axis 'object_camera_vec' by 90° such
400 # that we have a nice camera view onto the object.
401 matrix_rotation
= Matrix
.Rotation(90/360*2*pi
, 4, object_camera_vec
)
402 rotate_object(matrix_rotation
, camera
)
404 # Here a lamp is put into the scene, if chosen.
405 if use_light
== True:
407 # This is the distance from the object measured in terms of %
408 # of the camera distance. It is set onto 50% (1/2) distance.
409 lamp_dl
= sqrt(object_size
) * 15 * 0.5
410 # This is a factor to which extend the lamp shall go to the right
411 # (from the camera point of view).
412 lamp_dy_right
= lamp_dl
* (3.0/4.0)
414 # Create x, y and z for the lamp.
415 object_lamp_vec
= Vector((lamp_dl
,lamp_dy_right
,lamp_dl
))
416 lamp_xyz_vec
= object_center_vec
+ object_lamp_vec
417 length
= lamp_xyz_vec
.length
419 # As a lamp we use a point source.
420 lamp_data
= bpy
.data
.lights
.new(name
="A_lamp", type="POINT")
421 # We now determine the emission strength of the lamp. Note that the
422 # intensity depends on 1/r^2. For this we use a value of 100000.0 at a
423 # distance of 58. This value was determined manually inside Blender.
424 lamp_data
.energy
= 500000.0 * ( (length
* length
) / (58.0 * 58.0) )
425 lamp
= bpy
.data
.objects
.new("A_lamp", lamp_data
)
426 lamp
.location
= lamp_xyz_vec
427 bpy
.context
.collection
.objects
.link(lamp
)
429 # Some settings for the World: a bit ambient occlusion
430 bpy
.context
.scene
.world
.light_settings
.use_ambient_occlusion
= True
431 bpy
.context
.scene
.world
.light_settings
.ao_factor
= 0.1
433 # -----------------------------------------------------------------------------
436 def import_xyz(Ball_type
,
441 Ball_distance_factor
,
449 atom_material_list
= []
451 # ------------------------------------------------------------------------
452 # INITIALIZE THE ELEMENT LIST
456 # ------------------------------------------------------------------------
457 # READING DATA OF ATOMS
459 Number_of_total_atoms
= read_xyz_file(filepath_xyz
, radiustype
)
461 # We show the atoms of the first frame.
462 first_frame
= ALL_FRAMES
[0]
464 # ------------------------------------------------------------------------
465 # MATERIAL PROPERTIES FOR ATOMS
467 # Create first a new list of materials for each type of atom
469 for atoms_of_one_type
in first_frame
:
470 # Take the first atom
471 atom
= atoms_of_one_type
[0]
472 material
= bpy
.data
.materials
.new(atom
.name
)
473 material
.diffuse_color
= atom
.color
474 material
.use_nodes
= True
475 mat_P_BSDF
= next(n
for n
in material
.node_tree
.nodes
476 if n
.type == "BSDF_PRINCIPLED")
477 mat_P_BSDF
.inputs
['Base Color'].default_value
= atom
.color
478 material
.name
= atom
.name
479 atom_material_list
.append(material
)
481 # Now, we go through all atoms and give them a material. For all atoms ...
482 for atoms_of_one_type
in first_frame
:
483 for atom
in atoms_of_one_type
:
484 # ... and all materials ...
485 for material
in atom_material_list
:
486 # ... select the correct material for the current atom via
487 # comparison of names ...
488 if atom
.name
in material
.name
:
489 # ... and give the atom its material properties.
490 # However, before we check if it is a vacancy
491 # The vacancy is represented by a transparent cube.
492 if atom
.name
== "Vacancy":
493 # For cycles and eevee.
494 material
.use_nodes
= True
495 mat_P_BSDF
= next(n
for n
in material
.node_tree
.nodes
496 if n
.type == "BSDF_PRINCIPLED")
497 mat_P_BSDF
.inputs
['Metallic'].default_value
= 0.1
498 mat_P_BSDF
.inputs
['Specular'].default_value
= 0.15
499 mat_P_BSDF
.inputs
['Roughness'].default_value
= 0.05
500 mat_P_BSDF
.inputs
['Clearcoat Roughness'].default_value
= 0.37
501 mat_P_BSDF
.inputs
['IOR'].default_value
= 0.8
502 mat_P_BSDF
.inputs
['Transmission'].default_value
= 0.6
503 mat_P_BSDF
.inputs
['Transmission Roughness'].default_value
= 0.0
504 mat_P_BSDF
.inputs
['Alpha'].default_value
= 0.5
505 # Some additional stuff for eevee.
506 material
.blend_method
= 'HASHED'
507 material
.shadow_method
= 'HASHED'
508 material
.use_backface_culling
= False
509 # The atom gets its properties.
510 atom
.material
= material
512 # ------------------------------------------------------------------------
513 # TRANSLATION OF THE STRUCTURE TO THE ORIGIN
515 # It may happen that the structure in a XYZ file already has an offset
518 # If chosen, the structure is put into the center of the scene
519 # (only the first frame).
520 if put_to_center
== True and put_to_center_all
== False:
522 sum_vec
= Vector((0.0,0.0,0.0))
524 # Sum of all atom coordinates
525 for atoms_of_one_type
in first_frame
:
526 sum_vec
= sum([atom
.location
for atom
in atoms_of_one_type
], sum_vec
)
528 # Then the average is taken
529 sum_vec
= sum_vec
/ Number_of_total_atoms
531 # After, for each atom the center of gravity is substracted
532 for atoms_of_one_type
in first_frame
:
533 for atom
in atoms_of_one_type
:
534 atom
.location
-= sum_vec
536 # If chosen, the structure is put into the center of the scene
538 if put_to_center_all
== True:
541 for frame
in ALL_FRAMES
:
543 sum_vec
= Vector((0.0,0.0,0.0))
545 # Sum of all atom coordinates
546 for (i
, atoms_of_one_type
) in enumerate(frame
):
548 # This is a guarantee that only the total number of atoms of the
549 # first frame is used. Condition is, so far, that the number of
550 # atoms in a xyz file is constant. However, sometimes the number
551 # may increase (or decrease). If it decreases, the addon crashes.
552 # If it increases, only the tot number of atoms of the first frame
554 # By time, I will allow varying atom numbers ... but this takes
556 if i
>= Number_of_total_atoms
:
559 sum_vec
= sum([atom
.location
for atom
in atoms_of_one_type
], sum_vec
)
561 # Then the average is taken
562 sum_vec
= sum_vec
/ Number_of_total_atoms
564 # After, for each atom the center of gravity is substracted
565 for atoms_of_one_type
in frame
:
566 for atom
in atoms_of_one_type
:
567 atom
.location
-= sum_vec
570 # ------------------------------------------------------------------------
573 # Take all atoms and adjust their radii and scale the distances.
574 for atoms_of_one_type
in first_frame
:
575 for atom
in atoms_of_one_type
:
576 atom
.location
*= Ball_distance_factor
578 # ------------------------------------------------------------------------
579 # DETERMINATION OF SOME GEOMETRIC PROPERTIES
581 # In the following, some geometric properties of the whole object are
582 # determined: center, size, etc.
583 sum_vec
= Vector((0.0,0.0,0.0))
585 # First the center is determined. All coordinates are summed up ...
586 for atoms_of_one_type
in first_frame
:
587 sum_vec
= sum([atom
.location
for atom
in atoms_of_one_type
], sum_vec
)
589 # ... and the average is taken. This gives the center of the object.
590 object_center_vec
= sum_vec
/ Number_of_total_atoms
592 # Now, we determine the size.The farthest atom from the object center is
593 # taken as a measure. The size is used to place well the camera and light
597 for atoms_of_one_type
in first_frame
:
598 object_size_vec
+= [atom
.location
- object_center_vec
for atom
in atoms_of_one_type
]
601 object_size
= max(object_size_vec
).length
603 # ------------------------------------------------------------------------
606 # Before we start to draw the atoms, we first create a collection for the
607 # atomic structure. All atoms (balls) are put into this collection.
608 coll_structure_name
= os
.path
.basename(filepath_xyz
)
609 scene
= bpy
.context
.scene
610 coll_structure
= bpy
.data
.collections
.new(coll_structure_name
)
611 scene
.collection
.children
.link(coll_structure
)
613 # ------------------------------------------------------------------------
616 bpy
.ops
.object.select_all(action
='DESELECT')
618 # For each list of atoms of ONE type (e.g. Hydrogen)
619 for atoms_of_one_type
in first_frame
:
621 # Create first the vertices composed of the coordinates of all
624 for atom
in atoms_of_one_type
:
625 # In fact, the object is created in the World's origin.
626 # This is why 'object_center_vec' is substracted. At the end
627 # the whole object is translated back to 'object_center_vec'.
628 atom_vertices
.append( atom
.location
- object_center_vec
)
630 # First, we create a collection of the element, which
631 # contains the atoms (balls + mesh)!
632 coll_element_name
= atom
.name
# the element name
633 # Create the new collection and ...
634 coll_element
= bpy
.data
.collections
.new(coll_element_name
)
635 # ... link it to the collection, which contains all parts of the
637 coll_structure
.children
.link(coll_element
)
639 # Now, create a collection for the atoms, which includes the
640 # representative ball and the mesh.
641 coll_atom_name
= atom
.name
+ "_atom"
642 # Create the new collection and ...
643 coll_atom
= bpy
.data
.collections
.new(coll_atom_name
)
644 # ... link it to the collection, which contains all parts of the
645 # element (ball and mesh).
646 coll_element
.children
.link(coll_atom
)
649 atom_mesh
= bpy
.data
.meshes
.new("Mesh_"+atom
.name
)
650 atom_mesh
.from_pydata(atom_vertices
, [], [])
652 new_atom_mesh
= bpy
.data
.objects
.new(atom
.name
+ "_mesh", atom_mesh
)
654 # Link active object to the new collection
655 coll_atom
.objects
.link(new_atom_mesh
)
657 # Now, build a representative sphere (atom)
658 if atom
.name
== "Vacancy":
659 bpy
.ops
.mesh
.primitive_cube_add(
660 align
='WORLD', enter_editmode
=False,
661 location
=(0.0, 0.0, 0.0),
662 rotation
=(0.0, 0.0, 0.0))
666 bpy
.ops
.surface
.primitive_nurbs_surface_sphere_add(
667 align
='WORLD', enter_editmode
=False,
668 location
=(0,0,0), rotation
=(0.0, 0.0, 0.0))
670 elif Ball_type
== "1":
671 bpy
.ops
.mesh
.primitive_uv_sphere_add(
672 segments
=Ball_azimuth
, ring_count
=Ball_zenith
,
673 align
='WORLD', enter_editmode
=False,
674 location
=(0,0,0), rotation
=(0, 0, 0))
676 elif Ball_type
== "2":
677 bpy
.ops
.object.metaball_add(type='BALL', align
='WORLD',
678 enter_editmode
=False, location
=(0, 0, 0),
681 ball
= bpy
.context
.view_layer
.objects
.active
682 # Hide this ball because its appearance has no meaning. It is just the
683 # representative ball. The ball is visible at the vertices of the mesh.
684 # Rememmber, this is a dupliverts construct!
685 # However, hiding does not work with meta balls!
686 if Ball_type
== "0" or Ball_type
== "1":
688 # Scale up/down the ball radius.
689 ball
.scale
= (atom
.radius
*Ball_radius_factor
,) * 3
691 if atom
.name
== "Vacancy":
692 ball
.name
= atom
.name
+ "_cube"
694 ball
.name
= atom
.name
+ "_ball"
695 ball
.active_material
= atom
.material
696 ball
.parent
= new_atom_mesh
697 new_atom_mesh
.instance_type
= 'VERTS'
698 # The object is back translated to 'object_center_vec'.
699 new_atom_mesh
.location
= object_center_vec
700 STRUCTURE
.append(new_atom_mesh
)
702 # Note the collection where the ball was placed into.
703 coll_all
= ball
.users_collection
704 if len(coll_all
) > 0:
705 coll_past
= coll_all
[0]
707 coll_past
= bpy
.context
.scene
.collection
709 # Put the atom into the new collection 'atom' and ...
710 coll_atom
.objects
.link(ball
)
711 # ... unlink the atom from the other collection.
712 coll_past
.objects
.unlink(ball
)
714 # ------------------------------------------------------------------------
715 # CAMERA and LIGHT SOURCES
717 camera_light_source(use_camera
,
722 # ------------------------------------------------------------------------
723 # SELECT ALL LOADED OBJECTS
725 bpy
.ops
.object.select_all(action
='DESELECT')
727 for obj
in STRUCTURE
:
729 # activate the last selected object (perhaps another should be active?)
731 bpy
.context
.view_layer
.objects
.active
= obj
735 def build_frames(frame_delta
, frame_skip
):
737 scn
= bpy
.context
.scene
739 # Introduce the basis for all elements that appear in the structure.
740 for element
in STRUCTURE
:
742 bpy
.ops
.object.select_all(action
='DESELECT')
743 bpy
.context
.view_layer
.objects
.active
= element
744 element
.select_set(True)
745 bpy
.ops
.object.shape_key_add(True)
749 # Introduce the keys and reference the atom positions for each key.
751 for j
, frame
in enumerate(ALL_FRAMES
):
753 if j
% frame_skip
== 0:
755 for elements_frame
, elements_structure
in zip(frame
,STRUCTURE
):
757 key
= elements_structure
.shape_key_add()
759 for atom_frame
, atom_structure
in zip(elements_frame
, key
.data
):
761 atom_structure
.co
= (atom_frame
.location
762 - elements_structure
.location
)
764 key
.name
= atom_frame
.name
+ "_frame_" + str(i
)
771 scn
.frame_end
= frame_delta
* num_frames
773 # Manage the values of the keys
774 for element
in STRUCTURE
:
776 scn
.frame_current
= 0
778 element
.data
.shape_keys
.key_blocks
[1].value
= 1.0
779 element
.data
.shape_keys
.key_blocks
[2].value
= 0.0
780 element
.data
.shape_keys
.key_blocks
[1].keyframe_insert("value")
781 element
.data
.shape_keys
.key_blocks
[2].keyframe_insert("value")
783 scn
.frame_current
+= frame_delta
787 for number
in range(num_frames
)[2:]:#-1]:
789 element
.data
.shape_keys
.key_blocks
[number
-1].value
= 0.0
790 element
.data
.shape_keys
.key_blocks
[number
].value
= 1.0
791 element
.data
.shape_keys
.key_blocks
[number
+1].value
= 0.0
792 element
.data
.shape_keys
.key_blocks
[number
-1].keyframe_insert("value")
793 element
.data
.shape_keys
.key_blocks
[number
].keyframe_insert("value")
794 element
.data
.shape_keys
.key_blocks
[number
+1].keyframe_insert("value")
796 scn
.frame_current
+= frame_delta
800 element
.data
.shape_keys
.key_blocks
[number
].value
= 1.0
801 element
.data
.shape_keys
.key_blocks
[number
-1].value
= 0.0
802 element
.data
.shape_keys
.key_blocks
[number
].keyframe_insert("value")
803 element
.data
.shape_keys
.key_blocks
[number
-1].keyframe_insert("value")