File headers: use SPDX license identifiers
[blender-addons.git] / io_mesh_atomic / xyz_import.py
blob6a2bb5a22dafab57249b1ea6cea8ec0e6c340976
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import os
4 import bpy
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
13 # is as follows:
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)
23 ELEMENTS_DEFAULT = (
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.
139 ELEMENTS = []
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
143 # 'AtomProp'.
144 ALL_FRAMES = []
146 # A list of ALL balls which are put into the scene
147 STRUCTURE = []
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):
154 self.number = number
155 self.name = name
156 self.short_name = short_name
157 self.color = color
158 self.radii = radii
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
166 self.name = name
167 self.location = location
168 self.radius = radius
169 self.color = color
170 self.material = material
173 # -----------------------------------------------------------------------------
174 # Some basic routines
176 def read_elements():
178 del ELEMENTS[:]
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
185 # empty list.
186 radii_ionic = []
188 li = ElementProp(item[0],item[1],item[2],item[3],
189 radii,radii_ionic)
190 ELEMENTS.append(li)
193 # filepath_pdb: path to pdb file
194 # radiustype : '0' default
195 # '1' atomic radii
196 # '2' van der Waals
197 def read_xyz_file(filepath_xyz,radiustype):
199 number_frames = 0
200 total_number_atoms = 0
202 # Open the file ...
203 filepath_xyz_p = open(filepath_xyz, "r")
205 #Go through the whole file.
206 FLAG = False
207 for line in filepath_xyz_p:
209 # ... the loop is broken here (EOF) ...
210 if line == "":
211 continue
213 split_list = line.rsplit()
215 if len(split_list) == 1:
216 number_atoms = int(split_list[0])
217 FLAG = True
219 if FLAG == True:
221 line = filepath_xyz_p.readline()
222 line = line.rstrip()
224 all_atoms= []
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
233 # is used.
234 # By time, I will allow varying atom numbers ... but this takes
235 # some time ...
236 if number_frames != 0:
237 if i >= total_number_atoms:
238 break
241 line = filepath_xyz_p.readline()
242 line = line.rstrip()
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.
247 FLAG_FOUND = False
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:
251 name = element.name
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
256 FLAG_FOUND = True
257 break
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:
264 short_name = "VAC"
265 name = "Vacancy"
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.
271 else:
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
292 # is used.
293 # By time, I will allow varying atom numbers ... but this takes
294 # some time ...
295 total_number_atoms = number_atoms
298 elements = []
299 for atom in all_atoms:
300 FLAG_FOUND = False
301 for element in elements:
302 # If the atom name is already in the list,
303 # FLAG on 'True'.
304 if element == atom[1]:
305 FLAG_FOUND = True
306 break
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
314 structure = []
315 for element in elements:
316 atoms_one_type = []
317 for atom in all_atoms:
318 if atom[1] == element:
319 atoms_one_type.append(AtomProp(atom[0],
320 atom[1],
321 atom[2],
322 atom[3],
323 atom[4],[]))
324 structure.append(atoms_one_type)
326 ALL_FRAMES.append(structure)
327 number_frames += 1
328 FLAG = False
330 filepath_xyz_p.close()
332 return total_number_atoms
335 # Rotate an object.
336 def rotate_object(rot_mat, obj):
338 bpy.ops.object.select_all(action='DESELECT')
339 obj.select_set(True)
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,
356 use_light,
357 object_center_vec,
358 object_size):
360 camera_factor = 15.0
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,
371 0.0,
372 sqrt(object_size) * camera_factor))
373 camera_xyz_vec = object_center_vec + object_camera_vec
375 # Create the camera
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 # -----------------------------------------------------------------------------
434 # The main routine
436 def import_xyz(Ball_type,
437 Ball_azimuth,
438 Ball_zenith,
439 Ball_radius_factor,
440 radiustype,
441 Ball_distance_factor,
442 put_to_center,
443 put_to_center_all,
444 use_camera,
445 use_light,
446 filepath_xyz):
448 # List of materials
449 atom_material_list = []
451 # ------------------------------------------------------------------------
452 # INITIALIZE THE ELEMENT LIST
454 read_elements()
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
468 # (e.g. hydrogen)
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 = material.node_tree.nodes['Principled BSDF']
476 mat_P_BSDF.inputs['Base Color'].default_value = atom.color
477 material.name = atom.name
478 atom_material_list.append(material)
480 # Now, we go through all atoms and give them a material. For all atoms ...
481 for atoms_of_one_type in first_frame:
482 for atom in atoms_of_one_type:
483 # ... and all materials ...
484 for material in atom_material_list:
485 # ... select the correct material for the current atom via
486 # comparison of names ...
487 if atom.name in material.name:
488 # ... and give the atom its material properties.
489 # However, before we check if it is a vacancy
490 # The vacancy is represented by a transparent cube.
491 if atom.name == "Vacancy":
492 # For cycles and eevee.
493 material.use_nodes = True
494 mat_P_BSDF = material.node_tree.nodes['Principled BSDF']
495 mat_P_BSDF.inputs['Metallic'].default_value = 0.1
496 mat_P_BSDF.inputs['Specular'].default_value = 0.15
497 mat_P_BSDF.inputs['Roughness'].default_value = 0.05
498 mat_P_BSDF.inputs['Clearcoat Roughness'].default_value = 0.37
499 mat_P_BSDF.inputs['IOR'].default_value = 0.8
500 mat_P_BSDF.inputs['Transmission'].default_value = 0.6
501 mat_P_BSDF.inputs['Transmission Roughness'].default_value = 0.0
502 mat_P_BSDF.inputs['Alpha'].default_value = 0.5
503 # Some additional stuff for eevee.
504 material.blend_method = 'HASHED'
505 material.shadow_method = 'HASHED'
506 material.use_backface_culling = False
507 # The atom gets its properties.
508 atom.material = material
510 # ------------------------------------------------------------------------
511 # TRANSLATION OF THE STRUCTURE TO THE ORIGIN
513 # It may happen that the structure in a XYZ file already has an offset
516 # If chosen, the structure is put into the center of the scene
517 # (only the first frame).
518 if put_to_center == True and put_to_center_all == False:
520 sum_vec = Vector((0.0,0.0,0.0))
522 # Sum of all atom coordinates
523 for atoms_of_one_type in first_frame:
524 sum_vec = sum([atom.location for atom in atoms_of_one_type], sum_vec)
526 # Then the average is taken
527 sum_vec = sum_vec / Number_of_total_atoms
529 # After, for each atom the center of gravity is substracted
530 for atoms_of_one_type in first_frame:
531 for atom in atoms_of_one_type:
532 atom.location -= sum_vec
534 # If chosen, the structure is put into the center of the scene
535 # (all frames).
536 if put_to_center_all == True:
538 # For all frames
539 for frame in ALL_FRAMES:
541 sum_vec = Vector((0.0,0.0,0.0))
543 # Sum of all atom coordinates
544 for (i, atoms_of_one_type) in enumerate(frame):
546 # This is a guarantee that only the total number of atoms of the
547 # first frame is used. Condition is, so far, that the number of
548 # atoms in a xyz file is constant. However, sometimes the number
549 # may increase (or decrease). If it decreases, the addon crashes.
550 # If it increases, only the tot number of atoms of the first frame
551 # is used.
552 # By time, I will allow varying atom numbers ... but this takes
553 # some time ...
554 if i >= Number_of_total_atoms:
555 break
557 sum_vec = sum([atom.location for atom in atoms_of_one_type], sum_vec)
559 # Then the average is taken
560 sum_vec = sum_vec / Number_of_total_atoms
562 # After, for each atom the center of gravity is substracted
563 for atoms_of_one_type in frame:
564 for atom in atoms_of_one_type:
565 atom.location -= sum_vec
568 # ------------------------------------------------------------------------
569 # SCALING
571 # Take all atoms and adjust their radii and scale the distances.
572 for atoms_of_one_type in first_frame:
573 for atom in atoms_of_one_type:
574 atom.location *= Ball_distance_factor
576 # ------------------------------------------------------------------------
577 # DETERMINATION OF SOME GEOMETRIC PROPERTIES
579 # In the following, some geometric properties of the whole object are
580 # determined: center, size, etc.
581 sum_vec = Vector((0.0,0.0,0.0))
583 # First the center is determined. All coordinates are summed up ...
584 for atoms_of_one_type in first_frame:
585 sum_vec = sum([atom.location for atom in atoms_of_one_type], sum_vec)
587 # ... and the average is taken. This gives the center of the object.
588 object_center_vec = sum_vec / Number_of_total_atoms
590 # Now, we determine the size.The farthest atom from the object center is
591 # taken as a measure. The size is used to place well the camera and light
592 # into the scene.
594 object_size_vec = []
595 for atoms_of_one_type in first_frame:
596 object_size_vec += [atom.location - object_center_vec for atom in atoms_of_one_type]
598 object_size = 0.0
599 object_size = max(object_size_vec).length
601 # ------------------------------------------------------------------------
602 # COLLECTION
604 # Before we start to draw the atoms, we first create a collection for the
605 # atomic structure. All atoms (balls) are put into this collection.
606 coll_structure_name = os.path.basename(filepath_xyz)
607 scene = bpy.context.scene
608 coll_structure = bpy.data.collections.new(coll_structure_name)
609 scene.collection.children.link(coll_structure)
611 # ------------------------------------------------------------------------
612 # DRAWING THE ATOMS
614 bpy.ops.object.select_all(action='DESELECT')
616 # For each list of atoms of ONE type (e.g. Hydrogen)
617 for atoms_of_one_type in first_frame:
619 # Create first the vertices composed of the coordinates of all
620 # atoms of one type
621 atom_vertices = []
622 for atom in atoms_of_one_type:
623 # In fact, the object is created in the World's origin.
624 # This is why 'object_center_vec' is substracted. At the end
625 # the whole object is translated back to 'object_center_vec'.
626 atom_vertices.append( atom.location - object_center_vec )
628 # First, we create a collection of the element, which
629 # contains the atoms (balls + mesh)!
630 coll_element_name = atom.name # the element name
631 # Create the new collection and ...
632 coll_element = bpy.data.collections.new(coll_element_name)
633 # ... link it to the collection, which contains all parts of the
634 # structure.
635 coll_structure.children.link(coll_element)
637 # Now, create a collection for the atoms, which includes the
638 # representative ball and the mesh.
639 coll_atom_name = atom.name + "_atom"
640 # Create the new collection and ...
641 coll_atom = bpy.data.collections.new(coll_atom_name)
642 # ... link it to the collection, which contains all parts of the
643 # element (ball and mesh).
644 coll_element.children.link(coll_atom)
646 # Build the mesh
647 atom_mesh = bpy.data.meshes.new("Mesh_"+atom.name)
648 atom_mesh.from_pydata(atom_vertices, [], [])
649 atom_mesh.update()
650 new_atom_mesh = bpy.data.objects.new(atom.name + "_mesh", atom_mesh)
652 # Link active object to the new collection
653 coll_atom.objects.link(new_atom_mesh)
655 # Now, build a representative sphere (atom)
656 if atom.name == "Vacancy":
657 bpy.ops.mesh.primitive_cube_add(
658 align='WORLD', enter_editmode=False,
659 location=(0.0, 0.0, 0.0),
660 rotation=(0.0, 0.0, 0.0))
661 else:
662 # NURBS balls
663 if Ball_type == "0":
664 bpy.ops.surface.primitive_nurbs_surface_sphere_add(
665 align='WORLD', enter_editmode=False,
666 location=(0,0,0), rotation=(0.0, 0.0, 0.0))
667 # UV balls
668 elif Ball_type == "1":
669 bpy.ops.mesh.primitive_uv_sphere_add(
670 segments=Ball_azimuth, ring_count=Ball_zenith,
671 align='WORLD', enter_editmode=False,
672 location=(0,0,0), rotation=(0, 0, 0))
673 # Meta balls
674 elif Ball_type == "2":
675 bpy.ops.object.metaball_add(type='BALL', align='WORLD',
676 enter_editmode=False, location=(0, 0, 0),
677 rotation=(0, 0, 0))
679 ball = bpy.context.view_layer.objects.active
680 # Hide this ball because its appearance has no meaning. It is just the
681 # representative ball. The ball is visible at the vertices of the mesh.
682 # Rememmber, this is a dupliverts construct!
683 # However, hiding does not work with meta balls!
684 if Ball_type == "0" or Ball_type == "1":
685 ball.hide_set(True)
686 # Scale up/down the ball radius.
687 ball.scale = (atom.radius*Ball_radius_factor,) * 3
689 if atom.name == "Vacancy":
690 ball.name = atom.name + "_cube"
691 else:
692 ball.name = atom.name + "_ball"
693 ball.active_material = atom.material
694 ball.parent = new_atom_mesh
695 new_atom_mesh.instance_type = 'VERTS'
696 # The object is back translated to 'object_center_vec'.
697 new_atom_mesh.location = object_center_vec
698 STRUCTURE.append(new_atom_mesh)
700 # Note the collection where the ball was placed into.
701 coll_all = ball.users_collection
702 if len(coll_all) > 0:
703 coll_past = coll_all[0]
704 else:
705 coll_past = bpy.context.scene.collection
707 # Put the atom into the new collection 'atom' and ...
708 coll_atom.objects.link(ball)
709 # ... unlink the atom from the other collection.
710 coll_past.objects.unlink(ball)
712 # ------------------------------------------------------------------------
713 # CAMERA and LIGHT SOURCES
715 camera_light_source(use_camera,
716 use_light,
717 object_center_vec,
718 object_size)
720 # ------------------------------------------------------------------------
721 # SELECT ALL LOADED OBJECTS
723 bpy.ops.object.select_all(action='DESELECT')
724 obj = None
725 for obj in STRUCTURE:
726 obj.select_set(True)
727 # activate the last selected object (perhaps another should be active?)
728 if obj:
729 bpy.context.view_layer.objects.active = obj
733 def build_frames(frame_delta, frame_skip):
735 scn = bpy.context.scene
737 # Introduce the basis for all elements that appear in the structure.
738 for element in STRUCTURE:
740 bpy.ops.object.select_all(action='DESELECT')
741 bpy.context.view_layer.objects.active = element
742 element.select_set(True)
743 bpy.ops.object.shape_key_add(True)
745 frame_skip += 1
747 # Introduce the keys and reference the atom positions for each key.
748 i = 0
749 for j, frame in enumerate(ALL_FRAMES):
751 if j % frame_skip == 0:
753 for elements_frame, elements_structure in zip(frame,STRUCTURE):
755 key = elements_structure.shape_key_add()
757 for atom_frame, atom_structure in zip(elements_frame, key.data):
759 atom_structure.co = (atom_frame.location
760 - elements_structure.location)
762 key.name = atom_frame.name + "_frame_" + str(i)
764 i += 1
766 num_frames = i
768 scn.frame_start = 0
769 scn.frame_end = frame_delta * num_frames
771 # Manage the values of the keys
772 for element in STRUCTURE:
774 scn.frame_current = 0
776 element.data.shape_keys.key_blocks[1].value = 1.0
777 element.data.shape_keys.key_blocks[2].value = 0.0
778 element.data.shape_keys.key_blocks[1].keyframe_insert("value")
779 element.data.shape_keys.key_blocks[2].keyframe_insert("value")
781 scn.frame_current += frame_delta
783 number = 0
785 for number in range(num_frames)[2:]:#-1]:
787 element.data.shape_keys.key_blocks[number-1].value = 0.0
788 element.data.shape_keys.key_blocks[number].value = 1.0
789 element.data.shape_keys.key_blocks[number+1].value = 0.0
790 element.data.shape_keys.key_blocks[number-1].keyframe_insert("value")
791 element.data.shape_keys.key_blocks[number].keyframe_insert("value")
792 element.data.shape_keys.key_blocks[number+1].keyframe_insert("value")
794 scn.frame_current += frame_delta
796 number += 1
798 element.data.shape_keys.key_blocks[number].value = 1.0
799 element.data.shape_keys.key_blocks[number-1].value = 0.0
800 element.data.shape_keys.key_blocks[number].keyframe_insert("value")
801 element.data.shape_keys.key_blocks[number-1].keyframe_insert("value")