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