Cleanup: simplify file name incrementing logic
[blender-addons.git] / io_mesh_atomic / xyz_import.py
blob270997f44504bc7cdebfdd2b23b7364cfe13ca31
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import os
20 import bpy
21 from math import pi, sqrt
22 from mathutils import Vector, Matrix
24 # -----------------------------------------------------------------------------
25 # Atom and element data
28 # This is a list that contains some data of all possible elements. The structure
29 # is as follows:
31 # 1, "Hydrogen", "H", [0.0,0.0,1.0], 0.32, 0.32, 0.32 , -1 , 1.54 means
33 # No., name, short name, color, radius (used), radius (covalent), radius (atomic),
35 # charge state 1, radius (ionic) 1, charge state 2, radius (ionic) 2, ... all
36 # charge states for any atom are listed, if existing.
37 # The list is fixed and cannot be changed ... (see below)
39 ELEMENTS_DEFAULT = (
40 ( 1, "Hydrogen", "H", ( 1.0, 1.0, 1.0, 1.0), 0.32, 0.32, 0.79 , -1 , 1.54 ),
41 ( 2, "Helium", "He", ( 0.85, 1.0, 1.0, 1.0), 0.93, 0.93, 0.49 ),
42 ( 3, "Lithium", "Li", ( 0.8, 0.50, 1.0, 1.0), 1.23, 1.23, 2.05 , 1 , 0.68 ),
43 ( 4, "Beryllium", "Be", ( 0.76, 1.0, 0.0, 1.0), 0.90, 0.90, 1.40 , 1 , 0.44 , 2 , 0.35 ),
44 ( 5, "Boron", "B", ( 1.0, 0.70, 0.70, 1.0), 0.82, 0.82, 1.17 , 1 , 0.35 , 3 , 0.23 ),
45 ( 6, "Carbon", "C", ( 0.56, 0.56, 0.56, 1.0), 0.77, 0.77, 0.91 , -4 , 2.60 , 4 , 0.16 ),
46 ( 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 ),
47 ( 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 ),
48 ( 9, "Fluorine", "F", ( 0.56, 0.87, 0.31, 1.0), 0.72, 0.72, 0.57 , -1 , 1.33 , 7 , 0.08 ),
49 (10, "Neon", "Ne", ( 0.70, 0.89, 0.96, 1.0), 0.71, 0.71, 0.51 , 1 , 1.12 ),
50 (11, "Sodium", "Na", ( 0.67, 0.36, 0.94, 1.0), 1.54, 1.54, 2.23 , 1 , 0.97 ),
51 (12, "Magnesium", "Mg", ( 0.54, 1.0, 0.0, 1.0), 1.36, 1.36, 1.72 , 1 , 0.82 , 2 , 0.66 ),
52 (13, "Aluminium", "Al", ( 0.74, 0.65, 0.65, 1.0), 1.18, 1.18, 1.82 , 3 , 0.51 ),
53 (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 ),
54 (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 ),
55 (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 ),
56 (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 ),
57 (18, "Argon", "Ar", ( 0.50, 0.81, 0.89, 1.0), 0.98, 0.98, 0.88 , 1 , 1.54 ),
58 (19, "Potassium", "K", ( 0.56, 0.25, 0.83, 1.0), 2.03, 2.03, 2.77 , 1 , 0.81 ),
59 (20, "Calcium", "Ca", ( 0.23, 1.0, 0.0, 1.0), 1.74, 1.74, 2.23 , 1 , 1.18 , 2 , 0.99 ),
60 (21, "Scandium", "Sc", ( 0.90, 0.90, 0.90, 1.0), 1.44, 1.44, 2.09 , 3 , 0.73 ),
61 (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 ),
62 (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 ),
63 (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 ),
64 (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 ),
65 (26, "Iron", "Fe", ( 0.87, 0.4, 0.2, 1.0), 1.17, 1.17, 1.72 , 2 , 0.74 , 3 , 0.64 ),
66 (27, "Cobalt", "Co", ( 0.94, 0.56, 0.62, 1.0), 1.16, 1.16, 1.67 , 2 , 0.72 , 3 , 0.63 ),
67 (28, "Nickel", "Ni", ( 0.31, 0.81, 0.31, 1.0), 1.15, 1.15, 1.62 , 2 , 0.69 ),
68 (29, "Copper", "Cu", ( 0.78, 0.50, 0.2, 1.0), 1.17, 1.17, 1.57 , 1 , 0.96 , 2 , 0.72 ),
69 (30, "Zinc", "Zn", ( 0.49, 0.50, 0.69, 1.0), 1.25, 1.25, 1.53 , 1 , 0.88 , 2 , 0.74 ),
70 (31, "Gallium", "Ga", ( 0.76, 0.56, 0.56, 1.0), 1.26, 1.26, 1.81 , 1 , 0.81 , 3 , 0.62 ),
71 (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 ),
72 (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 ),
73 (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 ),
74 (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 ),
75 (36, "Krypton", "Kr", ( 0.36, 0.72, 0.81, 1.0), 1.31, 1.31, 1.24 ),
76 (37, "Rubidium", "Rb", ( 0.43, 0.18, 0.69, 1.0), 2.16, 2.16, 2.98 , 1 , 1.47 ),
77 (38, "Strontium", "Sr", ( 0.0, 1.0, 0.0, 1.0), 1.91, 1.91, 2.45 , 2 , 1.12 ),
78 (39, "Yttrium", "Y", ( 0.58, 1.0, 1.0, 1.0), 1.62, 1.62, 2.27 , 3 , 0.89 ),
79 (40, "Zirconium", "Zr", ( 0.58, 0.87, 0.87, 1.0), 1.45, 1.45, 2.16 , 1 , 1.09 , 4 , 0.79 ),
80 (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 ),
81 (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 ),
82 (43, "Technetium", "Tc", ( 0.23, 0.61, 0.61, 1.0), 1.27, 1.27, 1.95 , 7 , 0.97 ),
83 (44, "Ruthenium", "Ru", ( 0.14, 0.56, 0.56, 1.0), 1.25, 1.25, 1.89 , 4 , 0.67 ),
84 (45, "Rhodium", "Rh", ( 0.03, 0.49, 0.54, 1.0), 1.25, 1.25, 1.83 , 3 , 0.68 ),
85 (46, "Palladium", "Pd", ( 0.0, 0.41, 0.52, 1.0), 1.28, 1.28, 1.79 , 2 , 0.80 , 4 , 0.65 ),
86 (47, "Silver", "Ag", ( 0.75, 0.75, 0.75, 1.0), 1.34, 1.34, 1.75 , 1 , 1.26 , 2 , 0.89 ),
87 (48, "Cadmium", "Cd", ( 1.0, 0.85, 0.56, 1.0), 1.48, 1.48, 1.71 , 1 , 1.14 , 2 , 0.97 ),
88 (49, "Indium", "In", ( 0.65, 0.45, 0.45, 1.0), 1.44, 1.44, 2.00 , 3 , 0.81 ),
89 (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 ),
90 (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 ),
91 (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 ),
92 (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 ),
93 (54, "Xenon", "Xe", ( 0.25, 0.61, 0.69, 1.0), 1.31, 1.31, 1.24 ),
94 (55, "Caesium", "Cs", ( 0.34, 0.09, 0.56, 1.0), 2.35, 2.35, 3.35 , 1 , 1.67 ),
95 (56, "Barium", "Ba", ( 0.0, 0.78, 0.0, 1.0), 1.98, 1.98, 2.78 , 1 , 1.53 , 2 , 1.34 ),
96 (57, "Lanthanum", "La", ( 0.43, 0.83, 1.0, 1.0), 1.69, 1.69, 2.74 , 1 , 1.39 , 3 , 1.06 ),
97 (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 ),
98 (59, "Praseodymium", "Pr", ( 0.85, 1.0, 0.78, 1.0), 1.65, 1.65, 2.67 , 3 , 1.01 , 4 , 0.90 ),
99 (60, "Neodymium", "Nd", ( 0.78, 1.0, 0.78, 1.0), 1.64, 1.64, 2.64 , 3 , 0.99 ),
100 (61, "Promethium", "Pm", ( 0.63, 1.0, 0.78, 1.0), 1.63, 1.63, 2.62 , 3 , 0.97 ),
101 (62, "Samarium", "Sm", ( 0.56, 1.0, 0.78, 1.0), 1.62, 1.62, 2.59 , 3 , 0.96 ),
102 (63, "Europium", "Eu", ( 0.38, 1.0, 0.78, 1.0), 1.85, 1.85, 2.56 , 2 , 1.09 , 3 , 0.95 ),
103 (64, "Gadolinium", "Gd", ( 0.27, 1.0, 0.78, 1.0), 1.61, 1.61, 2.54 , 3 , 0.93 ),
104 (65, "Terbium", "Tb", ( 0.18, 1.0, 0.78, 1.0), 1.59, 1.59, 2.51 , 3 , 0.92 , 4 , 0.84 ),
105 (66, "Dysprosium", "Dy", ( 0.12, 1.0, 0.78, 1.0), 1.59, 1.59, 2.49 , 3 , 0.90 ),
106 (67, "Holmium", "Ho", ( 0.0, 1.0, 0.61, 1.0), 1.58, 1.58, 2.47 , 3 , 0.89 ),
107 (68, "Erbium", "Er", ( 0.0, 0.90, 0.45, 1.0), 1.57, 1.57, 2.45 , 3 , 0.88 ),
108 (69, "Thulium", "Tm", ( 0.0, 0.83, 0.32, 1.0), 1.56, 1.56, 2.42 , 3 , 0.87 ),
109 (70, "Ytterbium", "Yb", ( 0.0, 0.74, 0.21, 1.0), 1.74, 1.74, 2.40 , 2 , 0.93 , 3 , 0.85 ),
110 (71, "Lutetium", "Lu", ( 0.0, 0.67, 0.14, 1.0), 1.56, 1.56, 2.25 , 3 , 0.85 ),
111 (72, "Hafnium", "Hf", ( 0.30, 0.76, 1.0, 1.0), 1.44, 1.44, 2.16 , 4 , 0.78 ),
112 (73, "Tantalum", "Ta", ( 0.30, 0.65, 1.0, 1.0), 1.34, 1.34, 2.09 , 5 , 0.68 ),
113 (74, "Tungsten", "W", ( 0.12, 0.58, 0.83, 1.0), 1.30, 1.30, 2.02 , 4 , 0.70 , 6 , 0.62 ),
114 (75, "Rhenium", "Re", ( 0.14, 0.49, 0.67, 1.0), 1.28, 1.28, 1.97 , 4 , 0.72 , 7 , 0.56 ),
115 (76, "Osmium", "Os", ( 0.14, 0.4, 0.58, 1.0), 1.26, 1.26, 1.92 , 4 , 0.88 , 6 , 0.69 ),
116 (77, "Iridium", "Ir", ( 0.09, 0.32, 0.52, 1.0), 1.27, 1.27, 1.87 , 4 , 0.68 ),
117 (78, "Platinum", "Pt", ( 0.81, 0.81, 0.87, 1.0), 1.30, 1.30, 1.83 , 2 , 0.80 , 4 , 0.65 ),
118 (79, "Gold", "Au", ( 1.0, 0.81, 0.13, 1.0), 1.34, 1.34, 1.79 , 1 , 1.37 , 3 , 0.85 ),
119 (80, "Mercury", "Hg", ( 0.72, 0.72, 0.81, 1.0), 1.49, 1.49, 1.76 , 1 , 1.27 , 2 , 1.10 ),
120 (81, "Thallium", "Tl", ( 0.65, 0.32, 0.30, 1.0), 1.48, 1.48, 2.08 , 1 , 1.47 , 3 , 0.95 ),
121 (82, "Lead", "Pb", ( 0.34, 0.34, 0.38, 1.0), 1.47, 1.47, 1.81 , 2 , 1.20 , 4 , 0.84 ),
122 (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 ),
123 (84, "Polonium", "Po", ( 0.67, 0.36, 0.0, 1.0), 1.46, 1.46, 1.53 , 6 , 0.67 ),
124 (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 ),
125 (86, "Radon", "Rn", ( 0.25, 0.50, 0.58, 1.0), 1.00, 1.00, 1.34 ),
126 (87, "Francium", "Fr", ( 0.25, 0.0, 0.4, 1.0), 1.00, 1.00, 1.00 , 1 , 1.80 ),
127 (88, "Radium", "Ra", ( 0.0, 0.49, 0.0, 1.0), 1.00, 1.00, 1.00 , 2 , 1.43 ),
128 (89, "Actinium", "Ac", ( 0.43, 0.67, 0.98, 1.0), 1.00, 1.00, 1.00 , 3 , 1.18 ),
129 (90, "Thorium", "Th", ( 0.0, 0.72, 1.0, 1.0), 1.65, 1.65, 1.00 , 4 , 1.02 ),
130 (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 ),
131 (92, "Uranium", "U", ( 0.0, 0.56, 1.0, 1.0), 1.42, 1.42, 1.00 , 4 , 0.97 , 6 , 0.80 ),
132 (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 ),
133 (94, "Plutonium", "Pu", ( 0.0, 0.41, 1.0, 1.0), 1.00, 1.00, 1.00 , 3 , 1.08 , 4 , 0.93 ),
134 (95, "Americium", "Am", ( 0.32, 0.36, 0.94, 1.0), 1.00, 1.00, 1.00 , 3 , 1.07 , 4 , 0.92 ),
135 (96, "Curium", "Cm", ( 0.47, 0.36, 0.89, 1.0), 1.00, 1.00, 1.00 ),
136 (97, "Berkelium", "Bk", ( 0.54, 0.30, 0.89, 1.0), 1.00, 1.00, 1.00 ),
137 (98, "Californium", "Cf", ( 0.63, 0.21, 0.83, 1.0), 1.00, 1.00, 1.00 ),
138 (99, "Einsteinium", "Es", ( 0.70, 0.12, 0.83, 1.0), 1.00, 1.00, 1.00 ),
139 (100, "Fermium", "Fm", ( 0.70, 0.12, 0.72, 1.0), 1.00, 1.00, 1.00 ),
140 (101, "Mendelevium", "Md", ( 0.70, 0.05, 0.65, 1.0), 1.00, 1.00, 1.00 ),
141 (102, "Nobelium", "No", ( 0.74, 0.05, 0.52, 1.0), 1.00, 1.00, 1.00 ),
142 (103, "Lawrencium", "Lr", ( 0.78, 0.0, 0.4, 1.0), 1.00, 1.00, 1.00 ),
143 (104, "Vacancy", "Vac", ( 0.5, 0.5, 0.5, 1.0), 1.00, 1.00, 1.00),
144 (105, "Default", "Default", ( 1.0, 1.0, 1.0, 1.0), 1.00, 1.00, 1.00),
145 (106, "Stick", "Stick", ( 0.5, 0.5, 0.5, 1.0), 1.00, 1.00, 1.00),
148 # This list here contains all data of the elements and will be used during
149 # runtime. It is a list of classes.
150 # During executing Atomic Blender, the list will be initialized with the fixed
151 # data from above via the class structure below (ElementProp). We
152 # have then one fixed list (above), which will never be changed, and a list of
153 # classes with same data. The latter can be modified via loading a separate
154 # custom data file for instance.
155 ELEMENTS = []
157 # This is the list, which contains all atoms of all frames! Each item is a
158 # list which contains the atoms of a single frame. It is a list of
159 # 'AtomProp'.
160 ALL_FRAMES = []
162 # A list of ALL balls which are put into the scene
163 STRUCTURE = []
166 # This is the class, which stores the properties for one element.
167 class ElementProp(object):
168 __slots__ = ('number', 'name', 'short_name', 'color', 'radii', 'radii_ionic')
169 def __init__(self, number, name, short_name, color, radii, radii_ionic):
170 self.number = number
171 self.name = name
172 self.short_name = short_name
173 self.color = color
174 self.radii = radii
175 self.radii_ionic = radii_ionic
177 # This is the class, which stores the properties of one atom.
178 class AtomProp(object):
179 __slots__ = ('element', 'name', 'location', 'radius', 'color', 'material')
180 def __init__(self, element, name, location, radius, color, material):
181 self.element = element
182 self.name = name
183 self.location = location
184 self.radius = radius
185 self.color = color
186 self.material = material
189 # -----------------------------------------------------------------------------
190 # Some basic routines
192 def read_elements():
194 del ELEMENTS[:]
196 for item in ELEMENTS_DEFAULT:
198 # All three radii into a list
199 radii = [item[4],item[5],item[6]]
200 # The handling of the ionic radii will be done later. So far, it is an
201 # empty list.
202 radii_ionic = []
204 li = ElementProp(item[0],item[1],item[2],item[3],
205 radii,radii_ionic)
206 ELEMENTS.append(li)
209 # filepath_pdb: path to pdb file
210 # radiustype : '0' default
211 # '1' atomic radii
212 # '2' van der Waals
213 def read_xyz_file(filepath_xyz,radiustype):
215 number_frames = 0
216 total_number_atoms = 0
218 # Open the file ...
219 filepath_xyz_p = open(filepath_xyz, "r")
221 #Go through the whole file.
222 FLAG = False
223 for line in filepath_xyz_p:
225 # ... the loop is broken here (EOF) ...
226 if line == "":
227 continue
229 split_list = line.rsplit()
231 if len(split_list) == 1:
232 number_atoms = int(split_list[0])
233 FLAG = True
235 if FLAG == True:
237 line = filepath_xyz_p.readline()
238 line = line.rstrip()
240 all_atoms= []
241 for i in range(number_atoms):
244 # This is a guarantee that only the total number of atoms of the
245 # first frame is used. Condition is, so far, that the number of
246 # atoms in a xyz file is constant. However, sometimes the number
247 # may increase (or decrease). If it decreases, the addon crashes.
248 # If it increases, only the tot number of atoms of the first frame
249 # is used.
250 # By time, I will allow varying atom numbers ... but this takes
251 # some time ...
252 if number_frames != 0:
253 if i >= total_number_atoms:
254 break
257 line = filepath_xyz_p.readline()
258 line = line.rstrip()
259 split_list = line.rsplit()
260 short_name = str(split_list[0])
262 # Go through all elements and find the element of the current atom.
263 FLAG_FOUND = False
264 for element in ELEMENTS:
265 if str.upper(short_name) == str.upper(element.short_name):
266 # Give the atom its proper name, color and radius:
267 name = element.name
268 # int(radiustype) => type of radius:
269 # pre-defined (0), atomic (1) or van der Waals (2)
270 radius = float(element.radii[int(radiustype)])
271 color = element.color
272 FLAG_FOUND = True
273 break
275 # Is it a vacancy or an 'unknown atom' ?
276 if FLAG_FOUND == False:
277 # Give this atom also a name. If it is an 'X' then it is a
278 # vacancy. Otherwise ...
279 if "X" in short_name:
280 short_name = "VAC"
281 name = "Vacancy"
282 radius = float(ELEMENTS[-3].radii[int(radiustype)])
283 color = ELEMENTS[-3].color
284 # ... take what is written in the xyz file. These are somewhat
285 # unknown atoms. This should never happen, the element list is
286 # almost complete. However, we do this due to security reasons.
287 else:
288 name = str.upper(short_name)
289 radius = float(ELEMENTS[-2].radii[int(radiustype)])
290 color = ELEMENTS[-2].color
292 x = float(split_list[1])
293 y = float(split_list[2])
294 z = float(split_list[3])
296 location = Vector((x,y,z))
298 all_atoms.append([short_name, name, location, radius, color])
300 # We note here all elements. This needs to be done only once.
301 if number_frames == 0:
303 # This is a guarantee that only the total number of atoms of the
304 # first frame is used. Condition is, so far, that the number of
305 # atoms in a xyz file is constant. However, sometimes the number
306 # may increase (or decrease). If it decreases, the addon crashes.
307 # If it increases, only the tot number of atoms of the first frame
308 # is used.
309 # By time, I will allow varying atom numbers ... but this takes
310 # some time ...
311 total_number_atoms = number_atoms
314 elements = []
315 for atom in all_atoms:
316 FLAG_FOUND = False
317 for element in elements:
318 # If the atom name is already in the list,
319 # FLAG on 'True'.
320 if element == atom[1]:
321 FLAG_FOUND = True
322 break
323 # No name in the current list has been found? => New entry.
324 if FLAG_FOUND == False:
325 # Stored are: Atom label (e.g. 'Na'), the corresponding
326 # atom name (e.g. 'Sodium') and its color.
327 elements.append(atom[1])
329 # Sort the atoms: create lists of atoms of one type
330 structure = []
331 for element in elements:
332 atoms_one_type = []
333 for atom in all_atoms:
334 if atom[1] == element:
335 atoms_one_type.append(AtomProp(atom[0],
336 atom[1],
337 atom[2],
338 atom[3],
339 atom[4],[]))
340 structure.append(atoms_one_type)
342 ALL_FRAMES.append(structure)
343 number_frames += 1
344 FLAG = False
346 filepath_xyz_p.close()
348 return total_number_atoms
351 # Rotate an object.
352 def rotate_object(rot_mat, obj):
354 bpy.ops.object.select_all(action='DESELECT')
355 obj.select_set(True)
357 # Decompose world_matrix's components, and from them assemble 4x4 matrices.
358 orig_loc, orig_rot, orig_scale = obj.matrix_world.decompose()
360 orig_loc_mat = Matrix.Translation(orig_loc)
361 orig_rot_mat = orig_rot.to_matrix().to_4x4()
362 orig_scale_mat = (Matrix.Scale(orig_scale[0],4,(1,0,0)) @
363 Matrix.Scale(orig_scale[1],4,(0,1,0)) @
364 Matrix.Scale(orig_scale[2],4,(0,0,1)))
366 # Assemble the new matrix.
367 obj.matrix_world = orig_loc_mat @ rot_mat @ orig_rot_mat @ orig_scale_mat
370 # Function, which puts a camera and light source into the 3D scene
371 def camera_light_source(use_camera,
372 use_light,
373 object_center_vec,
374 object_size):
376 camera_factor = 15.0
378 # If chosen a camera is put into the scene.
379 if use_camera == True:
381 # Assume that the object is put into the global origin. Then, the
382 # camera is moved in x and z direction, not in y. The object has its
383 # size at distance sqrt(object_size) from the origin. So, move the
384 # camera by this distance times a factor of camera_factor in x and z.
385 # Then add x, y and z of the origin of the object.
386 object_camera_vec = Vector((sqrt(object_size) * camera_factor,
387 0.0,
388 sqrt(object_size) * camera_factor))
389 camera_xyz_vec = object_center_vec + object_camera_vec
391 # Create the camera
392 camera_data = bpy.data.cameras.new("A_camera")
393 camera_data.lens = 45
394 camera_data.clip_end = 500.0
395 camera = bpy.data.objects.new("A_camera", camera_data)
396 camera.location = camera_xyz_vec
397 bpy.context.collection.objects.link(camera)
399 # Here the camera is rotated such it looks towards the center of
400 # the object. The [0.0, 0.0, 1.0] vector along the z axis
401 z_axis_vec = Vector((0.0, 0.0, 1.0))
402 # The angle between the last two vectors
403 angle = object_camera_vec.angle(z_axis_vec, 0)
404 # The cross-product of z_axis_vec and object_camera_vec
405 axis_vec = z_axis_vec.cross(object_camera_vec)
406 # Rotate 'axis_vec' by 'angle' and convert this to euler parameters.
407 # 4 is the size of the matrix.
408 camera.rotation_euler = Matrix.Rotation(angle, 4, axis_vec).to_euler()
410 # Rotate the camera around its axis by 90° such that we have a nice
411 # camera position and view onto the object.
412 bpy.ops.object.select_all(action='DESELECT')
413 camera.select_set(True)
415 # Rotate the camera around its axis 'object_camera_vec' by 90° such
416 # that we have a nice camera view onto the object.
417 matrix_rotation = Matrix.Rotation(90/360*2*pi, 4, object_camera_vec)
418 rotate_object(matrix_rotation, camera)
420 # Here a lamp is put into the scene, if chosen.
421 if use_light == True:
423 # This is the distance from the object measured in terms of %
424 # of the camera distance. It is set onto 50% (1/2) distance.
425 light_dl = sqrt(object_size) * 15 * 0.5
426 # This is a factor to which extend the lamp shall go to the right
427 # (from the camera point of view).
428 light_dy_right = light_dl * (3.0/4.0)
430 # Create x, y and z for the lamp.
431 object_light_vec = Vector((light_dl,light_dy_right,light_dl))
432 light_xyz_vec = object_center_vec + object_light_vec
434 # Create the lamp
435 light_data = bpy.data.lights.new(name="A_light", type="SUN")
436 light_data.distance = 500.0
437 light_data.energy = 3.0
438 lamp = bpy.data.objects.new("A_light", light_data)
439 lamp.location = light_xyz_vec
440 bpy.context.collection.objects.link(lamp)
442 # Some settings for the World: a bit ambient occlusion
443 bpy.context.scene.world.light_settings.use_ambient_occlusion = True
444 bpy.context.scene.world.light_settings.ao_factor = 0.2
445 # Some properties for cycles
446 lamp.data.use_nodes = True
447 lmp_P_BSDF = lamp.data.node_tree.nodes['Emission']
448 lmp_P_BSDF.inputs['Strength'].default_value = 5
450 # -----------------------------------------------------------------------------
451 # The main routine
453 def import_xyz(Ball_type,
454 Ball_azimuth,
455 Ball_zenith,
456 Ball_radius_factor,
457 radiustype,
458 Ball_distance_factor,
459 put_to_center,
460 put_to_center_all,
461 use_camera,
462 use_light,
463 filepath_xyz):
465 # List of materials
466 atom_material_list = []
468 # ------------------------------------------------------------------------
469 # INITIALIZE THE ELEMENT LIST
471 read_elements()
473 # ------------------------------------------------------------------------
474 # READING DATA OF ATOMS
476 Number_of_total_atoms = read_xyz_file(filepath_xyz, radiustype)
478 # We show the atoms of the first frame.
479 first_frame = ALL_FRAMES[0]
481 # ------------------------------------------------------------------------
482 # MATERIAL PROPERTIES FOR ATOMS
484 # Create first a new list of materials for each type of atom
485 # (e.g. hydrogen)
486 for atoms_of_one_type in first_frame:
487 # Take the first atom
488 atom = atoms_of_one_type[0]
489 material = bpy.data.materials.new(atom.name)
490 material.name = atom.name
491 material.diffuse_color = atom.color
492 atom_material_list.append(material)
494 # Now, we go through all atoms and give them a material. For all atoms ...
495 for atoms_of_one_type in first_frame:
496 for atom in atoms_of_one_type:
497 # ... and all materials ...
498 for material in atom_material_list:
499 # ... select the correct material for the current atom via
500 # comparison of names ...
501 if atom.name in material.name:
502 # ... and give the atom its material properties.
503 # However, before we check if it is a vacancy
504 # The vacancy is represented by a transparent cube.
505 if atom.name == "Vacancy":
506 # Some properties for eevee.
507 material.metallic = 0.8
508 material.specular_intensity = 0.5
509 material.roughness = 0.3
510 material.blend_method = 'OPAQUE'
511 material.show_transparent_back = False
512 # Some properties for cycles
513 material.use_nodes = True
514 mat_P_BSDF = material.node_tree.nodes['Principled BSDF']
515 mat_P_BSDF.inputs['Metallic'].default_value = 0.1
516 mat_P_BSDF.inputs['Roughness'].default_value = 0.2
517 mat_P_BSDF.inputs['Transmission'].default_value = 0.97
518 mat_P_BSDF.inputs['IOR'].default_value = 0.8
519 # The atom gets its properties.
520 atom.material = material
522 # ------------------------------------------------------------------------
523 # TRANSLATION OF THE STRUCTURE TO THE ORIGIN
525 # It may happen that the structure in a XYZ file already has an offset
528 # If chosen, the structure is put into the center of the scene
529 # (only the first frame).
530 if put_to_center == True and put_to_center_all == False:
532 sum_vec = Vector((0.0,0.0,0.0))
534 # Sum of all atom coordinates
535 for atoms_of_one_type in first_frame:
536 sum_vec = sum([atom.location for atom in atoms_of_one_type], sum_vec)
538 # Then the average is taken
539 sum_vec = sum_vec / Number_of_total_atoms
541 # After, for each atom the center of gravity is substracted
542 for atoms_of_one_type in first_frame:
543 for atom in atoms_of_one_type:
544 atom.location -= sum_vec
546 # If chosen, the structure is put into the center of the scene
547 # (all frames).
548 if put_to_center_all == True:
550 # For all frames
551 for frame in ALL_FRAMES:
553 sum_vec = Vector((0.0,0.0,0.0))
555 # Sum of all atom coordinates
556 for (i, atoms_of_one_type) in enumerate(frame):
558 # This is a guarantee that only the total number of atoms of the
559 # first frame is used. Condition is, so far, that the number of
560 # atoms in a xyz file is constant. However, sometimes the number
561 # may increase (or decrease). If it decreases, the addon crashes.
562 # If it increases, only the tot number of atoms of the first frame
563 # is used.
564 # By time, I will allow varying atom numbers ... but this takes
565 # some time ...
566 if i >= Number_of_total_atoms:
567 break
569 sum_vec = sum([atom.location for atom in atoms_of_one_type], sum_vec)
571 # Then the average is taken
572 sum_vec = sum_vec / Number_of_total_atoms
574 # After, for each atom the center of gravity is substracted
575 for atoms_of_one_type in frame:
576 for atom in atoms_of_one_type:
577 atom.location -= sum_vec
580 # ------------------------------------------------------------------------
581 # SCALING
583 # Take all atoms and adjust their radii and scale the distances.
584 for atoms_of_one_type in first_frame:
585 for atom in atoms_of_one_type:
586 atom.location *= Ball_distance_factor
588 # ------------------------------------------------------------------------
589 # DETERMINATION OF SOME GEOMETRIC PROPERTIES
591 # In the following, some geometric properties of the whole object are
592 # determined: center, size, etc.
593 sum_vec = Vector((0.0,0.0,0.0))
595 # First the center is determined. All coordinates are summed up ...
596 for atoms_of_one_type in first_frame:
597 sum_vec = sum([atom.location for atom in atoms_of_one_type], sum_vec)
599 # ... and the average is taken. This gives the center of the object.
600 object_center_vec = sum_vec / Number_of_total_atoms
602 # Now, we determine the size.The farthest atom from the object center is
603 # taken as a measure. The size is used to place well the camera and light
604 # into the scene.
606 object_size_vec = []
607 for atoms_of_one_type in first_frame:
608 object_size_vec += [atom.location - object_center_vec for atom in atoms_of_one_type]
610 object_size = 0.0
611 object_size = max(object_size_vec).length
613 # ------------------------------------------------------------------------
614 # COLLECTION
616 # Before we start to draw the atoms, we first create a collection for the
617 # atomic structure. All atoms (balls) are put into this collection.
618 coll_structure_name = os.path.basename(filepath_xyz)
619 scene = bpy.context.scene
620 coll_structure = bpy.data.collections.new(coll_structure_name)
621 scene.collection.children.link(coll_structure)
623 # ------------------------------------------------------------------------
624 # DRAWING THE ATOMS
626 bpy.ops.object.select_all(action='DESELECT')
628 # For each list of atoms of ONE type (e.g. Hydrogen)
629 for atoms_of_one_type in first_frame:
631 # Create first the vertices composed of the coordinates of all
632 # atoms of one type
633 atom_vertices = []
634 for atom in atoms_of_one_type:
635 # In fact, the object is created in the World's origin.
636 # This is why 'object_center_vec' is substracted. At the end
637 # the whole object is translated back to 'object_center_vec'.
638 atom_vertices.append( atom.location - object_center_vec )
640 # First, we create a collection of the element, which
641 # contains the atoms (balls + mesh)!
642 coll_element_name = atom.name # the element name
643 # Create the new collection and ...
644 coll_element = bpy.data.collections.new(coll_element_name)
645 # ... link it to the collection, which contains all parts of the
646 # structure.
647 coll_structure.children.link(coll_element)
649 # Now, create a collection for the atoms, which includes the
650 # representative ball and the mesh.
651 coll_atom_name = atom.name + "_atom"
652 # Create the new collection and ...
653 coll_atom = bpy.data.collections.new(coll_atom_name)
654 # ... link it to the collection, which contains all parts of the
655 # element (ball and mesh).
656 coll_element.children.link(coll_atom)
658 # Build the mesh
659 atom_mesh = bpy.data.meshes.new("Mesh_"+atom.name)
660 atom_mesh.from_pydata(atom_vertices, [], [])
661 atom_mesh.update()
662 new_atom_mesh = bpy.data.objects.new(atom.name + "_mesh", atom_mesh)
664 # Link active object to the new collection
665 coll_atom.objects.link(new_atom_mesh)
667 # Now, build a representative sphere (atom)
668 if atom.name == "Vacancy":
669 bpy.ops.mesh.primitive_cube_add(
670 align='WORLD', enter_editmode=False,
671 location=(0.0, 0.0, 0.0),
672 rotation=(0.0, 0.0, 0.0))
673 else:
674 # NURBS balls
675 if Ball_type == "0":
676 bpy.ops.surface.primitive_nurbs_surface_sphere_add(
677 align='WORLD', enter_editmode=False,
678 location=(0,0,0), rotation=(0.0, 0.0, 0.0))
679 # UV balls
680 elif Ball_type == "1":
681 bpy.ops.mesh.primitive_uv_sphere_add(
682 segments=Ball_azimuth, ring_count=Ball_zenith,
683 align='WORLD', enter_editmode=False,
684 location=(0,0,0), rotation=(0, 0, 0))
685 # Meta balls
686 elif Ball_type == "2":
687 bpy.ops.object.metaball_add(type='BALL', align='WORLD',
688 enter_editmode=False, location=(0, 0, 0),
689 rotation=(0, 0, 0))
691 ball = bpy.context.view_layer.objects.active
692 # Hide this ball because its appearance has no meaning. It is just the
693 # representative ball. The ball is visible at the vertices of the mesh.
694 # Rememmber, this is a dupliverts construct!
695 # However, hiding does not work with meta balls!
696 if Ball_type == "0" or Ball_type == "1":
697 ball.hide_set(True)
698 # Scale up/down the ball radius.
699 ball.scale = (atom.radius*Ball_radius_factor,) * 3
701 if atom.name == "Vacancy":
702 ball.name = atom.name + "_cube"
703 else:
704 ball.name = atom.name + "_ball"
705 ball.active_material = atom.material
706 ball.parent = new_atom_mesh
707 new_atom_mesh.instance_type = 'VERTS'
708 # The object is back translated to 'object_center_vec'.
709 new_atom_mesh.location = object_center_vec
710 STRUCTURE.append(new_atom_mesh)
712 # Note the collection where the ball was placed into.
713 coll_all = ball.users_collection
714 if len(coll_all) > 0:
715 coll_past = coll_all[0]
716 else:
717 coll_past = bpy.context.scene.collection
719 # Put the atom into the new collection 'atom' and ...
720 coll_atom.objects.link(ball)
721 # ... unlink the atom from the other collection.
722 coll_past.objects.unlink(ball)
724 # ------------------------------------------------------------------------
725 # CAMERA and LIGHT SOURCES
727 camera_light_source(use_camera,
728 use_light,
729 object_center_vec,
730 object_size)
732 # ------------------------------------------------------------------------
733 # SELECT ALL LOADED OBJECTS
735 bpy.ops.object.select_all(action='DESELECT')
736 obj = None
737 for obj in STRUCTURE:
738 obj.select_set(True)
739 # activate the last selected object (perhaps another should be active?)
740 if obj:
741 bpy.context.view_layer.objects.active = obj
745 def build_frames(frame_delta, frame_skip):
747 scn = bpy.context.scene
749 # Introduce the basis for all elements that appear in the structure.
750 for element in STRUCTURE:
752 bpy.ops.object.select_all(action='DESELECT')
753 bpy.context.view_layer.objects.active = element
754 element.select_set(True)
755 bpy.ops.object.shape_key_add(True)
757 frame_skip += 1
759 # Introduce the keys and reference the atom positions for each key.
760 i = 0
761 for j, frame in enumerate(ALL_FRAMES):
763 if j % frame_skip == 0:
765 for elements_frame, elements_structure in zip(frame,STRUCTURE):
767 key = elements_structure.shape_key_add()
769 for atom_frame, atom_structure in zip(elements_frame, key.data):
771 atom_structure.co = (atom_frame.location
772 - elements_structure.location)
774 key.name = atom_frame.name + "_frame_" + str(i)
776 i += 1
778 num_frames = i
780 scn.frame_start = 0
781 scn.frame_end = frame_delta * num_frames
783 # Manage the values of the keys
784 for element in STRUCTURE:
786 scn.frame_current = 0
788 element.data.shape_keys.key_blocks[1].value = 1.0
789 element.data.shape_keys.key_blocks[2].value = 0.0
790 element.data.shape_keys.key_blocks[1].keyframe_insert("value")
791 element.data.shape_keys.key_blocks[2].keyframe_insert("value")
793 scn.frame_current += frame_delta
795 number = 0
797 for number in range(num_frames)[2:]:#-1]:
799 element.data.shape_keys.key_blocks[number-1].value = 0.0
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-1].keyframe_insert("value")
803 element.data.shape_keys.key_blocks[number].keyframe_insert("value")
804 element.data.shape_keys.key_blocks[number+1].keyframe_insert("value")
806 scn.frame_current += frame_delta
808 number += 1
810 element.data.shape_keys.key_blocks[number].value = 1.0
811 element.data.shape_keys.key_blocks[number-1].value = 0.0
812 element.data.shape_keys.key_blocks[number].keyframe_insert("value")
813 element.data.shape_keys.key_blocks[number-1].keyframe_insert("value")